Skip to content

Commit 8e63f4d

Browse files
committed
QSPIFlashBlockDevice: fix unaligned writes
1 parent 76623ff commit 8e63f4d

File tree

1 file changed

+11
-20
lines changed

1 file changed

+11
-20
lines changed

libraries/BlockDevices/QSPIFlashBlockDevice.cpp

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -273,28 +273,19 @@ int QSPIFlashBlockDevice::write(const void *buffer, bd_addr_t add, bd_size_t _si
273273
debug_mem((uint8_t*)buffer, _size);
274274
#endif
275275

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) {
293279
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);
296284
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+
298289
if(rv == FSP_SUCCESS) {
299290
rv = get_flash_status();
300291
} else {

0 commit comments

Comments
 (0)