@@ -186,114 +186,88 @@ int QSPIFlashBlockDevice::close() {
186
186
187
187
188
188
189
- # define READ_PAGE_SIZE 0x4000000
189
+
190
190
/* -------------------------------------------------------------------------- */
191
191
/* READ at the "logical" address 'add' 'size' bytes and copy them at the address
192
192
specified by buffer
193
193
NOTE: buffer MUST be equal or greater than 'size' */
194
194
/* -------------------------------------------------------------------------- */
195
195
int QSPIFlashBlockDevice::read (void *buffer, bd_addr_t add, bd_size_t _size) {
196
- fsp_err_t rv = FSP_ERR_INVALID_ADDRESS;
196
+ fsp_err_t rv = (fsp_err_t )BLOCK_DEVICE_OK;;
197
+
198
+ if (!is_valid_read (add,_size)) {
199
+ return (int )FSP_ERR_INVALID_ADDRESS;
200
+ }
197
201
198
202
if (!opened) {
199
203
rv = (fsp_err_t )open ();
200
204
if (rv != BLOCK_DEVICE_OK) {
201
- return rv ;
205
+ return ( int )FSP_ERR_NOT_OPEN ;
202
206
}
203
207
}
204
208
205
209
if (buffer == nullptr ) {
206
- rv = FSP_ERR_INVALID_ARGUMENT;
210
+ return ( int ) FSP_ERR_INVALID_ARGUMENT;
207
211
}
208
- else {
209
- if (is_address_correct (add+_size-1 )) {
210
- #ifdef DEBUG_MSD
211
- mylogadd (" QSPI READ %i, %i" , add, _size);
212
- #endif
213
- rv = (fsp_err_t )BLOCK_DEVICE_OK;
214
- int64_t internal_size = _size;
215
- uint32_t byte_left_in_bank = read_block_size - read_block_size % add;
216
- uint8_t *dest = (uint8_t *)buffer;
217
- while (internal_size > 0 ) {
218
- uint32_t bank = add / READ_PAGE_SIZE;
219
- #ifdef QSPI_FLASH_DEBUG
220
- Serial.print (" +++++ READ Selected bank: " );
221
- Serial.println (bank);
222
- #endif
223
- uint32_t address = base_address + (add % READ_PAGE_SIZE);
224
- #ifdef QSPI_FLASH_DEBUG
225
- Serial.print (" +++++ READ physical address: " );
226
- Serial.println (address,HEX);
227
- #endif
228
- R_QSPI_BankSet (&ctrl, bank);
229
- uint32_t bytes_to_copy = (internal_size > byte_left_in_bank) ? byte_left_in_bank : internal_size;
230
- memcpy (dest,(uint8_t *)address, bytes_to_copy);
231
- internal_size -= bytes_to_copy;
232
- add += bytes_to_copy;
233
- dest += bytes_to_copy;
234
- byte_left_in_bank = read_block_size;
235
- }
236
- }
212
+
213
+ /* check if start and end belongs to 2 different sector
214
+ (should never happens because the READ_PAGE_SIZE is very BIG) */
215
+ while ( (add / READ_PAGE_SIZE) != ((add + _size - 1 ) / READ_PAGE_SIZE)) {
216
+ uint32_t bytes_left_in_page = READ_PAGE_SIZE - add % READ_PAGE_SIZE;
217
+
218
+ uint32_t bank = add / READ_PAGE_SIZE;
219
+ uint32_t address = base_address + (add % READ_PAGE_SIZE);
220
+ rv = R_QSPI_BankSet (&ctrl, bank);
221
+ memcpy ((uint8_t *)(buffer),(uint8_t *)address, bytes_left_in_page);
222
+ add += bytes_left_in_page;
223
+ _size -= bytes_left_in_page;
237
224
}
238
225
226
+ /* set bank */
227
+ uint32_t bank = add / READ_PAGE_SIZE;
228
+ uint32_t address = base_address + (add % READ_PAGE_SIZE);
229
+ rv = R_QSPI_BankSet (&ctrl, bank);
230
+ memcpy ((uint8_t *)(buffer),(uint8_t *)address, _size);
231
+
239
232
return (int )rv;
240
233
}
241
234
242
- # define INTERNAL_BLOCK_SIZE 256
235
+
243
236
244
237
/* -------------------------------------------------------------------------- */
245
238
/* WRITE 'size' byte form buffer to the "logical" address specified by 'addr'
246
239
NOTE: buffer MUST be equal or greater than 'size' */
247
240
/* -------------------------------------------------------------------------- */
248
241
int QSPIFlashBlockDevice::write (const void *buffer, bd_addr_t add, bd_size_t _size) {
249
- fsp_err_t rv = FSP_ERR_INVALID_ADDRESS;
250
-
242
+ fsp_err_t rv = (fsp_err_t )BLOCK_DEVICE_OK;;
243
+
244
+ if (!is_valid_program (add,_size)) {
245
+ return (int )FSP_ERR_INVALID_ADDRESS;
246
+ }
247
+
251
248
if (!opened) {
252
249
rv = (fsp_err_t )open ();
253
250
if (rv != BLOCK_DEVICE_OK) {
254
- return rv ;
251
+ return ( int )FSP_ERR_NOT_OPEN ;
255
252
}
256
253
}
257
254
258
255
if (buffer == nullptr ) {
259
- rv = FSP_ERR_INVALID_ARGUMENT;
256
+ return ( int ) FSP_ERR_INVALID_ARGUMENT;
260
257
}
261
- else {
262
- if (is_address_correct (add+_size-1 )) {
263
- #ifdef DEBUG_MSD
264
- mylogadd (" QSPI WRITE %i, %i" , add, _size);
265
- #endif
266
- int64_t internal_size = _size;
267
- uint32_t bytes_left_in_page = INTERNAL_BLOCK_SIZE - (add % INTERNAL_BLOCK_SIZE);
268
- uint8_t *source = (uint8_t *)buffer;
269
- rv = FSP_SUCCESS;
270
- while (internal_size > 0 && rv == FSP_SUCCESS) {
271
-
272
- uint32_t bank = add / READ_PAGE_SIZE;
273
- #ifdef QSPI_FLASH_DEBUG
274
- Serial.print (" +++++ WRITE Selected bank: " );
275
- Serial.println (bank);
276
- #endif
277
-
278
- uint32_t address = base_address + (add % READ_PAGE_SIZE);
279
- #ifdef QSPI_FLASH_DEBUG
280
- Serial.print (" +++++ WRITE physical address: " );
281
- Serial.println (address,HEX);
282
- #endif
283
-
284
- R_QSPI_BankSet (&ctrl, bank);
285
- uint32_t bytes_to_write = (internal_size > bytes_left_in_page) ? bytes_left_in_page : internal_size;
286
-
287
- rv = R_QSPI_Write (&ctrl, source, (uint8_t *)address, bytes_to_write);
288
- internal_size -= bytes_to_write;
289
- source += bytes_to_write;
290
- add += bytes_to_write;
291
- bytes_left_in_page = INTERNAL_BLOCK_SIZE;
292
- if (rv == FSP_SUCCESS)
293
- rv = get_flash_status ();
294
- }
258
+
259
+ uint32_t num_of_blocks = (_size / WRITE_INTERNAL_BLOCK_SIZE);
260
+ for (int i = 0 ; i < num_of_blocks && rv == FSP_SUCCESS; i++) {
261
+ /* set bank */
262
+ uint32_t bank = add / READ_PAGE_SIZE;
263
+ uint32_t address = base_address + ((add + i * WRITE_INTERNAL_BLOCK_SIZE) % READ_PAGE_SIZE);
264
+ R_QSPI_BankSet (&ctrl, bank);
265
+ rv = R_QSPI_Write (&ctrl, (uint8_t *)(buffer + (i * WRITE_INTERNAL_BLOCK_SIZE)), (uint8_t *)address, WRITE_INTERNAL_BLOCK_SIZE);
266
+ if (rv == FSP_SUCCESS) {
267
+ rv = get_flash_status ();
295
268
}
296
269
}
270
+
297
271
return (int )rv;
298
272
}
299
273
@@ -309,42 +283,32 @@ bool QSPIFlashBlockDevice::is_address_correct(bd_addr_t add) {
309
283
/* -------------------------------------------------------------------------- */
310
284
int QSPIFlashBlockDevice::erase (bd_addr_t add, bd_size_t _size) {
311
285
312
- fsp_err_t rv = FSP_ERR_INVALID_ADDRESS;
313
-
286
+ fsp_err_t rv = (fsp_err_t )BLOCK_DEVICE_OK;;
287
+
288
+ if (!is_valid_erase (add,_size)) {
289
+ return (int )FSP_ERR_INVALID_ADDRESS;
290
+ }
291
+
314
292
if (!opened) {
315
293
rv = (fsp_err_t )open ();
316
294
if (rv != BLOCK_DEVICE_OK) {
317
- return rv ;
295
+ return ( int )FSP_ERR_NOT_OPEN ;
318
296
}
319
297
}
320
298
321
- if (is_address_correct (add+_size-1 )) {
322
- int64_t internal_size = _size;
323
- /* get the starting address of the block */
324
- uint32_t erase_block_address = erase_block_size * (add / erase_block_size);
325
- uint32_t byte_left_in_erase_block = erase_block_size - (add % erase_block_size);
326
- rv = FSP_SUCCESS;
327
- while (internal_size > 0 && rv == FSP_SUCCESS) {
328
- uint32_t bank = erase_block_address / READ_PAGE_SIZE;
329
- #ifdef QSPI_FLASH_DEBUG
330
- Serial.print (" +++++ ERASE Selected bank: " );
331
- Serial.println (bank);
332
- #endif
333
- uint32_t address = base_address + (erase_block_address % READ_PAGE_SIZE);
334
- #ifdef QSPI_FLASH_DEBUG
335
- Serial.print (" +++++ ERASE physical address: " );
336
- Serial.println (address,HEX);
337
- #endif
338
- R_QSPI_BankSet (&ctrl, bank);
339
- rv = R_QSPI_Erase (&ctrl, (uint8_t *)address, erase_block_size);
340
- erase_block_address += byte_left_in_erase_block;
341
- internal_size -= byte_left_in_erase_block;
342
- byte_left_in_erase_block = erase_block_size;
343
- if (rv == FSP_SUCCESS)
344
- rv = get_flash_status ();
299
+ uint32_t num_of_blocks = (_size / erase_block_size);
300
+
301
+ for (int i = 0 ; i < num_of_blocks && rv == FSP_SUCCESS; i++) {
302
+ /* set bank */
303
+ uint32_t bank = add / READ_PAGE_SIZE;
304
+ uint32_t address = base_address + ((add + i * erase_block_size) % READ_PAGE_SIZE);
305
+ R_QSPI_BankSet (&ctrl, bank);
306
+ rv = R_QSPI_Erase (&ctrl, (uint8_t *)address, erase_block_size);
307
+ if (rv == FSP_SUCCESS) {
308
+ rv = get_flash_status ();
345
309
}
346
310
}
347
-
311
+
348
312
return (int )rv;
349
313
}
350
314
0 commit comments