diff --git a/spi_nand_flash/Kconfig b/spi_nand_flash/Kconfig new file mode 100644 index 0000000000..4bfdee2913 --- /dev/null +++ b/spi_nand_flash/Kconfig @@ -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 diff --git a/spi_nand_flash/README.md b/spi_nand_flash/README.md index 88f68ccd0c..23d4635a55 100644 --- a/spi_nand_flash/README.md +++ b/spi_nand_flash/README.md @@ -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 +``` diff --git a/spi_nand_flash/src/nand.c b/spi_nand_flash/src/nand.c index b0a0979f98..5baef0310c 100644 --- a/spi_nand_flash/src/nand.c +++ b/spi_nand_flash/src/nand.c @@ -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; @@ -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;