@@ -273,28 +273,19 @@ int QSPIFlashBlockDevice::write(const void *buffer, bd_addr_t add, bd_size_t _si
273
273
debug_mem ((uint8_t *)buffer, _size);
274
274
#endif
275
275
276
- uint32_t num_of_blocks = (_size / WRITE_INTERNAL_BLOCK_SIZE);
277
- debug_if (QSPIF_DBG, " QSPIF write %d blocks, status %d" , num_of_blocks, rv);
278
- for (int i = 0 ; i < num_of_blocks && rv == FSP_SUCCESS; i++) {
279
- /* set bank */
280
- uint32_t bank = add / READ_PAGE_SIZE;
281
- uint32_t address = base_address + ((add + i * WRITE_INTERNAL_BLOCK_SIZE) % READ_PAGE_SIZE);
282
- debug_if (QSPIF_DBG, " QSPIF write bank %d, address 0x%x" , bank, address);
283
- R_QSPI_BankSet (&ctrl, bank);
284
- rv = R_QSPI_Write (&ctrl, (uint8_t *)(buffer + (i * WRITE_INTERNAL_BLOCK_SIZE)), (uint8_t *)address, WRITE_INTERNAL_BLOCK_SIZE);
285
- if (rv == FSP_SUCCESS) {
286
- rv = get_flash_status ();
287
- } else {
288
- debug_if (QSPIF_DBG, " QSPIF R_QSPI_Write() failed %d" , rv);
289
- }
290
- }
291
-
292
- if (_size % WRITE_INTERNAL_BLOCK_SIZE != 0 ) {
276
+ uint32_t address = add + base_address;
277
+ const uint32_t end_address = address + _size;
278
+ while (address < end_address && rv == FSP_SUCCESS) {
293
279
uint32_t bank = add / READ_PAGE_SIZE;
294
- uint32_t address = base_address + ((add + num_of_blocks * WRITE_INTERNAL_BLOCK_SIZE) % READ_PAGE_SIZE);
295
- debug_if (QSPIF_DBG, " QSPIF write bank %d, address 0x%x" , bank, address);
280
+ uint32_t block_end = (((address / WRITE_INTERNAL_BLOCK_SIZE) + 1 ) * WRITE_INTERNAL_BLOCK_SIZE);
281
+ uint32_t left = block_end - address;
282
+ uint32_t chunk = address + left > end_address ? end_address - address : left;
283
+ debug_if (QSPIF_DBG, " 1 QSPIF write bank %d, address 0x%x block_end 0x%x left %d size %d" , bank, address, block_end, left, chunk);
296
284
R_QSPI_BankSet (&ctrl, bank);
297
- rv = R_QSPI_Write (&ctrl, (uint8_t *)(buffer + (num_of_blocks * WRITE_INTERNAL_BLOCK_SIZE)), (uint8_t *)address, WRITE_INTERNAL_BLOCK_SIZE);
285
+ rv = R_QSPI_Write (&ctrl, (uint8_t *)(buffer), (uint8_t *)address, chunk);
286
+ address += chunk;
287
+ buffer += chunk;
288
+
298
289
if (rv == FSP_SUCCESS) {
299
290
rv = get_flash_status ();
300
291
} else {
0 commit comments