Skip to content

Commit

Permalink
feat(spi_nand_flash): Add Kconfig option to verify write operations
Browse files Browse the repository at this point in the history
  • Loading branch information
RathiSonika committed Oct 23, 2024
1 parent 6c23b9c commit 0ac8c3c
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
10 changes: 10 additions & 0 deletions spi_nand_flash/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
menu "SPI NAND Flash configuration"

config NAND_FLASH_VERIFY_WRITE
bool "Verify SPI NAND flash writes"
default n
help
If this option is enabled, any time SPI NAND flash is written then the data will be read
back and verified. This can catch hardware problems with SPI NAND flash, or flash which
was not erased before verification.
endmenu
20 changes: 20 additions & 0 deletions spi_nand_flash/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,23 @@ At present, `spi_nand_flash` component is compatible with the chips produced by
* Winbond - W25N01GVxxxG/T/R, W25N512GVxIG/IT, W25N512GWxxR/T, W25N01JWxxxG/T, W25N01JWxxxG/T
* Gigadevice - GD5F1GQ5UExxG, GD5F1GQ5RExxG, GD5F2GQ5UExxG, GD5F2GQ5RExxG, GD5F4GQ6UExxG, GD5F4GQ6RExxG, GD5F4GQ6UExxG, GD5F4GQ6RExxG
* Alliance - AS5F31G04SND-08LIN, AS5F32G04SND-08LIN, AS5F12G04SND-10LIN, AS5F34G04SND-08LIN, AS5F14G04SND-10LIN, AS5F38G04SND-08LIN, AS5F18G04SND-10LIN
* Micron - MT29F4G01ABAFDWB

## Troubleshooting

To verify SPI NAND Flash writes, enable the `NAND_FLASH_VERIFY_WRITE` option in menuconfig. When this option is enabled, every time data is written to the SPI NAND Flash, it will be read back and verified. This helps in identifying hardware issues with the SPI NAND Flash.

To configure the project for this setting, follow these steps:

```
idf.py menuconfig
-> Component config
-> SPI NAND Flash configuration
-> NAND_FLASH_VERIFY_WRITE
```

Run `idf.py -p PORT flash monitor` and if the write verification is successful, the following result will be printed:

```
I nand_flash: spi_nand_flash_write_sector: Write buffer verification succeeded
```
27 changes: 27 additions & 0 deletions spi_nand_flash/src/nand.c
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,23 @@ esp_err_t spi_nand_flash_read_sector(spi_nand_flash_device_t *handle, uint8_t *b
return ret;
}

#if CONFIG_NAND_FLASH_VERIFY_WRITE
static esp_err_t s_verify_write(spi_nand_flash_device_t *handle, dhara_sector_t sector_id, const uint8_t *expected_buffer, dhara_error_t *err)
{
esp_err_t ret = ESP_OK;
if (dhara_map_read(&handle->dhara_map, sector_id, handle->read_buffer, err)) {
ret = ESP_ERR_FLASH_BASE + *err;
ESP_LOGE(TAG, "failed to read nand flash to verify previous write, err: 0x%x", *err);
return ret;
}

if (memcmp(handle->read_buffer, expected_buffer, handle->page_size)) {
ret = ESP_FAIL;
}
return ret;
}
#endif //CONFIG_NAND_FLASH_VERIFY_WRITE

esp_err_t spi_nand_flash_write_sector(spi_nand_flash_device_t *handle, const uint8_t *buffer, dhara_sector_t sector_id)
{
dhara_error_t err;
Expand All @@ -320,6 +337,16 @@ esp_err_t spi_nand_flash_write_sector(spi_nand_flash_device_t *handle, const uin
if (dhara_map_write(&handle->dhara_map, sector_id, buffer, &err)) {
ret = ESP_ERR_FLASH_BASE + err;
}
#if CONFIG_NAND_FLASH_VERIFY_WRITE
if (ret == ESP_OK) {
ret = s_verify_write(handle, sector_id, buffer, &err);
if (ret == ESP_OK) {
ESP_LOGI(TAG, "%s: Write buffer verification succeeded", __func__);
} else {
ESP_LOGE(TAG, "%s: Write buffer verification failed", __func__);
}
}
#endif //CONFIG_NAND_FLASH_VERIFY_WRITE

xSemaphoreGive(handle->mutex);
return ret;
Expand Down

0 comments on commit 0ac8c3c

Please sign in to comment.