32
32
#include "mpconfigboard.h" // for EXTERNAL_FLASH_QSPI_DUAL
33
33
34
34
#include "external_flash/common_commands.h"
35
+ #include "peripherals.h"
35
36
#include "shared_dma.h"
36
37
37
38
#include "atmel_start_pins.h"
@@ -55,6 +56,8 @@ bool spi_flash_command(uint8_t command) {
55
56
}
56
57
57
58
bool spi_flash_read_command (uint8_t command , uint8_t * response , uint32_t length ) {
59
+ samd_peripherals_disable_and_clear_cache ();
60
+
58
61
QSPI -> INSTRCTRL .bit .INSTR = command ;
59
62
60
63
QSPI -> INSTRFRAME .reg = QSPI_INSTRFRAME_WIDTH_SINGLE_BIT_SPI |
@@ -63,6 +66,11 @@ bool spi_flash_read_command(uint8_t command, uint8_t* response, uint32_t length)
63
66
QSPI_INSTRFRAME_INSTREN |
64
67
QSPI_INSTRFRAME_DATAEN ;
65
68
69
+ // Dummy read of INSTRFRAME needed to synchronize.
70
+ // See Instruction Transmission Flow Diagram, figure 37.9, page 995
71
+ // and Example 4, page 998, section 37.6.8.5.
72
+ (volatile uint32_t ) QSPI -> INSTRFRAME .reg ;
73
+
66
74
memcpy (response , (uint8_t * ) QSPI_AHB , length );
67
75
68
76
QSPI -> CTRLA .reg = QSPI_CTRLA_ENABLE | QSPI_CTRLA_LASTXFER ;
@@ -71,20 +79,28 @@ bool spi_flash_read_command(uint8_t command, uint8_t* response, uint32_t length)
71
79
72
80
QSPI -> INTFLAG .reg = QSPI_INTFLAG_INSTREND ;
73
81
82
+ samd_peripherals_enable_cache ();
83
+
74
84
return true;
75
85
}
76
86
77
87
bool spi_flash_write_command (uint8_t command , uint8_t * data , uint32_t length ) {
88
+ samd_peripherals_disable_and_clear_cache ();
89
+
78
90
QSPI -> INSTRCTRL .bit .INSTR = command ;
79
91
80
92
QSPI -> INSTRFRAME .reg = QSPI_INSTRFRAME_WIDTH_SINGLE_BIT_SPI |
81
93
QSPI_INSTRFRAME_ADDRLEN_24BITS |
82
94
QSPI_INSTRFRAME_TFRTYPE_WRITE |
83
- QSPI_INSTRFRAME_INSTREN ;
95
+ QSPI_INSTRFRAME_INSTREN |
96
+ (data != NULL ? QSPI_INSTRFRAME_DATAEN : 0 );
84
97
85
- if (data != NULL ) {
86
- QSPI -> INSTRFRAME .bit .DATAEN = true;
98
+ // Dummy read of INSTRFRAME needed to synchronize.
99
+ // See Instruction Transmission Flow Diagram, figure 37.9, page 995
100
+ // and Example 4, page 998, section 37.6.8.5.
101
+ (volatile uint32_t ) QSPI -> INSTRFRAME .reg ;
87
102
103
+ if (data != NULL ) {
88
104
memcpy ((uint8_t * ) QSPI_AHB , data , length );
89
105
}
90
106
@@ -94,6 +110,8 @@ bool spi_flash_write_command(uint8_t command, uint8_t* data, uint32_t length) {
94
110
95
111
QSPI -> INTFLAG .reg = QSPI_INTFLAG_INSTREND ;
96
112
113
+ samd_peripherals_enable_cache ();
114
+
97
115
return true;
98
116
}
99
117
@@ -117,6 +135,8 @@ bool spi_flash_sector_command(uint8_t command, uint32_t address) {
117
135
}
118
136
119
137
bool spi_flash_write_data (uint32_t address , uint8_t * data , uint32_t length ) {
138
+ samd_peripherals_disable_and_clear_cache ();
139
+
120
140
QSPI -> INSTRCTRL .bit .INSTR = CMD_PAGE_PROGRAM ;
121
141
uint32_t mode = QSPI_INSTRFRAME_WIDTH_SINGLE_BIT_SPI ;
122
142
@@ -137,10 +157,14 @@ bool spi_flash_write_data(uint32_t address, uint8_t* data, uint32_t length) {
137
157
138
158
QSPI -> INTFLAG .reg = QSPI_INTFLAG_INSTREND ;
139
159
160
+ samd_peripherals_enable_cache ();
161
+
140
162
return true;
141
163
}
142
164
143
165
bool spi_flash_read_data (uint32_t address , uint8_t * data , uint32_t length ) {
166
+ samd_peripherals_disable_and_clear_cache ();
167
+
144
168
#ifdef EXTERNAL_FLASH_QSPI_DUAL
145
169
QSPI -> INSTRCTRL .bit .INSTR = CMD_DUAL_READ ;
146
170
uint32_t mode = QSPI_INSTRFRAME_WIDTH_DUAL_OUTPUT ;
@@ -167,6 +191,8 @@ bool spi_flash_read_data(uint32_t address, uint8_t* data, uint32_t length) {
167
191
168
192
QSPI -> INTFLAG .reg = QSPI_INTFLAG_INSTREND ;
169
193
194
+ samd_peripherals_enable_cache ();
195
+
170
196
return true;
171
197
}
172
198
@@ -183,7 +209,7 @@ void spi_flash_init(void) {
183
209
// QSPI->BAUD.bit.BAUD = 32;
184
210
// Super fast, may be unreliable when Saleae is connected to high speed lines.
185
211
QSPI -> BAUD .bit .BAUD = 2 ;
186
- QSPI -> CTRLB .reg = QSPI_CTRLB_MODE_MEMORY |
212
+ QSPI -> CTRLB .reg = QSPI_CTRLB_MODE_MEMORY | // Serial memory mode (map to QSPI_AHB)
187
213
QSPI_CTRLB_DATALEN_8BITS |
188
214
QSPI_CTRLB_CSMODE_LASTXFER ;
189
215
0 commit comments