@@ -99,6 +99,7 @@ typedef struct {
99
99
static mmu_ttbl_desc_section_t desc_tbl [(SPIBSC_ADDR_END >> 20 ) - (SPIBSC_ADDR_START >> 20 ) + 1 ];
100
100
static volatile struct st_spibsc * SPIBSC = & SPIBSC0 ;
101
101
static st_spibsc_spimd_reg_t spimd_reg ;
102
+ static uint8_t write_tmp_buf [FLASH_PAGE_SIZE ];
102
103
103
104
#if defined(__ICCARM__ )
104
105
#define RAM_CODE_SEC __ramfunc
@@ -136,24 +137,12 @@ int32_t flash_free(flash_t *obj)
136
137
137
138
int32_t flash_erase_sector (flash_t * obj , uint32_t address )
138
139
{
139
- int32_t ret ;
140
-
141
- core_util_critical_section_enter ();
142
- ret = _sector_erase (address - FLASH_BASE );
143
- core_util_critical_section_exit ();
144
-
145
- return ret ;
140
+ return _sector_erase (address - FLASH_BASE );
146
141
}
147
142
148
143
int32_t flash_program_page (flash_t * obj , uint32_t address , const uint8_t * data , uint32_t size )
149
144
{
150
- int32_t ret ;
151
-
152
- core_util_critical_section_enter ();
153
- ret = _page_program (address - FLASH_BASE , data , size );
154
- core_util_critical_section_exit ();
155
-
156
- return ret ;
145
+ return _page_program (address - FLASH_BASE , data , size );
157
146
}
158
147
159
148
uint32_t flash_get_sector_size (const flash_t * obj , uint32_t address )
@@ -184,12 +173,14 @@ int32_t _sector_erase(uint32_t addr)
184
173
{
185
174
int32_t ret ;
186
175
176
+ core_util_critical_section_enter ();
187
177
spi_mode ();
188
178
189
179
/* ---- Write enable ---- */
190
180
ret = write_enable (); /* WREN Command */
191
181
if (ret != 0 ) {
192
182
ex_mode ();
183
+ core_util_critical_section_exit ();
193
184
return ret ;
194
185
}
195
186
@@ -210,12 +201,14 @@ int32_t _sector_erase(uint32_t addr)
210
201
ret = spibsc_transfer (& spimd_reg );
211
202
if (ret != 0 ) {
212
203
ex_mode ();
204
+ core_util_critical_section_exit ();
213
205
return ret ;
214
206
}
215
207
216
208
ret = busy_wait ();
217
209
218
210
ex_mode ();
211
+ core_util_critical_section_exit ();
219
212
return ret ;
220
213
}
221
214
@@ -226,8 +219,6 @@ int32_t _page_program(uint32_t addr, const uint8_t * buf, int32_t size)
226
219
int32_t remainder ;
227
220
int32_t idx = 0 ;
228
221
229
- spi_mode ();
230
-
231
222
while (size > 0 ) {
232
223
if (size > FLASH_PAGE_SIZE ) {
233
224
program_size = FLASH_PAGE_SIZE ;
@@ -239,10 +230,15 @@ int32_t _page_program(uint32_t addr, const uint8_t * buf, int32_t size)
239
230
program_size = remainder ;
240
231
}
241
232
233
+ core_util_critical_section_enter ();
234
+ memcpy (write_tmp_buf , & buf [idx ], program_size );
235
+ spi_mode ();
236
+
242
237
/* ---- Write enable ---- */
243
238
ret = write_enable (); /* WREN Command */
244
239
if (ret != 0 ) {
245
240
ex_mode ();
241
+ core_util_critical_section_exit ();
246
242
return ret ;
247
243
}
248
244
@@ -267,28 +263,33 @@ int32_t _page_program(uint32_t addr, const uint8_t * buf, int32_t size)
267
263
ret = spibsc_transfer (& spimd_reg ); /* Command,Address */
268
264
if (ret != 0 ) {
269
265
ex_mode ();
266
+ core_util_critical_section_exit ();
270
267
return ret ;
271
268
}
272
269
273
270
/* ----------- 2. Data ---------------*/
274
- ret = data_send (SPIBSC_1BIT , SPIBSC_SPISSL_NEGATE , & buf [ idx ] , program_size );
271
+ ret = data_send (SPIBSC_1BIT , SPIBSC_SPISSL_NEGATE , write_tmp_buf , program_size );
275
272
if (ret != 0 ) {
276
273
ex_mode ();
274
+ core_util_critical_section_exit ();
277
275
return ret ;
278
276
}
279
277
280
278
ret = busy_wait ();
281
279
if (ret != 0 ) {
282
280
ex_mode ();
281
+ core_util_critical_section_exit ();
283
282
return ret ;
284
283
}
285
284
285
+ ex_mode ();
286
+ core_util_critical_section_exit ();
287
+
286
288
addr += program_size ;
287
289
idx += program_size ;
288
290
size -= program_size ;
289
291
}
290
292
291
- ex_mode ();
292
293
return ret ;
293
294
}
294
295
0 commit comments