I’m using the Renesas RA6M4 QSPI API to write to QSPI EEPROM (MX25L256 From Macronix), but every time when I write to the third page, the second page is somehow erased, only first page data is retained. Worked on this problem for a couple of weeks, need someone with a fresh eye to point out the bug in my code.
below is the function. memcpy is used to check each page data after a write. historyIndex increment from 10 to 250 with 25 steps and return to 10. pageNo increment from 0 to 2 for 3 pages.
/* QSPI flash address through page*/
#define QSPI_FLASH_ADDRESS(page_no) (uint8_t *) (QSPI_DEVICE_START_ADDRESS + (page_no * PAGE_WRITE_SIZE))
void qspi_write(uint32_t historyIndex, uint8_t pageNo) {
fsp_err_t err = FSP_SUCCESS;
uint8_t sector_buffer[SECTOR_SIZE] = {0}; // Buffer for the entire sector
uint8_t data_write[] = {0,0,0,1,1,1,0,0,0,37};
// Align sector_address to sector boundary
uint32_t page_offset = (pageNo * PAGE_WRITE_SIZE) % SECTOR_SIZE;
// Read the entire sector into buffer using memcpy
memcpy(sector_buffer, (uint8_t *)QSPI_DEVICE_START_ADDRESS, SECTOR_SIZE);
// Modify the buffer for specific address within the sector
for (uint32_t i = 0; i < HISTORY_EVENT_SIZE; i++)
{
sector_buffer[page_offset + historyIndex + i] = data_write[i];
}
// Erase the corresponding sector before writing new data
qspi_erase((uint8_t *)QSPI_DEVICE_START_ADDRESS, SECTOR_SIZE); //erase 16 pages
// Write modified sector data back to QSPI Flash
err = R_QSPI_Write(&g_qspi_ctrl, §or_buffer[0 * PAGE_WRITE_SIZE], (uint8_t *)QSPI_FLASH_ADDRESS(0), PAGE_WRITE_SIZE);
memcpy(read_buffer, (uint8_t *)QSPI_FLASH_ADDRESS(0), PAGE_WRITE_SIZE);
err = R_QSPI_Write(&g_qspi_ctrl, §or_buffer[1 * PAGE_WRITE_SIZE], (uint8_t *)QSPI_FLASH_ADDRESS(1), PAGE_WRITE_SIZE);
memcpy(read_buffer1, (uint8_t *)QSPI_FLASH_ADDRESS(1), PAGE_WRITE_SIZE);
err = R_QSPI_Write(&g_qspi_ctrl, §or_buffer[2 * PAGE_WRITE_SIZE], (uint8_t *)QSPI_FLASH_ADDRESS(2), PAGE_WRITE_SIZE);
memcpy(read_buffer2, (uint8_t *)QSPI_FLASH_ADDRESS(2), PAGE_WRITE_SIZE);
err = get_flash_status();
if (FSP_SUCCESS != err)
{
APP_ERR_PRINT("Failed to get status for QSPI operationrn");
deinit_qspi(g_qspi_cfg.spi_protocol);
return;
}
}
I added memcpy to read the flash content back for each page, as described, the second page will turn into FFFFs when third page is written, and the third page will turn into FFFFs as well when reading the values for last two pages.
I checked the read function, the whole sector is read back and I only see first page being written to the flash