External NAND Flash with ESP32-C3

Dakshk
Posts: 16
Joined: Tue May 21, 2024 11:02 am

External NAND Flash with ESP32-C3

Postby Dakshk » Fri Feb 21, 2025 10:27 am

Hello,

I am trying to interface external NAND flash with ESP32c3 board, I have gone through IDF example, but it turns out to be for NOR flash and from online references and going through datasheet the code needs modifications, can anyone help me with it, I am getting following output.

Code: Select all

I (262) example: Initializing external SPI Flash
I (272) example: Pin assignments:
I (272) example: MOSI:  7   MISO:  5   SCLK:  6   CS:  4
I (282) example: DMA CHANNEL: 3
Here's my code for it

Code: Select all

esp_err_t spi_nand_send_cmd(uint8_t *cmd, size_t cmd_len, uint8_t *rx_data, size_t rx_len) {
    spi_transaction_t t = {
        .length = cmd_len * 8,
        .tx_buffer = cmd,
        .rxlength = 0,
        .rx_buffer = rx_data
    };
    return spi_device_transmit(handle, &t);
}
void read_nand_id() {
    uint8_t cmd = CMD_READ_ID;
    uint8_t id[3] = {0};

    if (spi_nand_send_cmd(&cmd, 1, id, 3) == ESP_OK) {
        ESP_LOGI(TAG, "Manufacturer ID: 0x%X, Device ID: 0x%X 0x%X", id[0], id[1], id[2]);
    } else {
        ESP_LOGE(TAG, "Failed to read NAND ID!");
    }
}
void app_main(void)
{
	/* Set up SPI bus and initialize the external SPI Flash chip */
	example_init_ext_flash();
	read_nand_id();
}
	
Thank you!

ESP_haberturdeur
Posts: 3
Joined: Thu Dec 12, 2024 10:17 am

Re: External NAND Flash with ESP32-C3

Postby ESP_haberturdeur » Wed Feb 26, 2025 7:30 am

Hi,
there is a official driver for NAND flashes: https://components.espressif.com/compon ... nand_flash
Depending on your flash model, this might work out of the box.

If that doesn't help, could you please post the model of your Flash, so we can assist you further?

Thank you.

Dakshk
Posts: 16
Joined: Tue May 21, 2024 11:02 am

Re: External NAND Flash with ESP32-C3

Postby Dakshk » Wed Feb 26, 2025 10:16 am

ESP_haberturdeur wrote:
Wed Feb 26, 2025 7:30 am
Hi,
there is a official driver for NAND flashes: https://components.espressif.com/compon ... nand_flash
Depending on your flash model, this might work out of the box.

If that doesn't help, could you please post the model of your Flash, so we can assist you further?

Thank you.
I tried this and got the following output. I am using W25N512GW

Code: Select all

E (492) spi_master: setup_priv_desc(918): Set flag SPI_TRANS_DMA_BUFFER_ALIGN_MANUAL but RX buffer addr&len not align to 4, or not dma_capable
E (502) spi_nand: nand_is_bad(90):
E (512) spi_nand: Error in nand_is_bad 258
E (512) spi_master: setup_priv_desc(918): Set flag SPI_TRANS_DMA_BUFFER_ALIGN_MANUAL but RX buffer addr&len not align to 4, or not dma_capable
E (522) spi_nand: nand_is_bad(90):
E (532) spi_nand: Error in nand_is_bad 258
E (532) spi_master: setup_priv_desc(918): Set flag SPI_TRANS_DMA_BUFFER_ALIGN_MANUAL but RX buffer addr&len not align to 4, or not dma_capable
E (552) spi_nand: nand_is_bad(90):
E (552) spi_nand: Error in nand_is_bad 258
E (562) spi_master: setup_priv_desc(918): Set flag SPI_TRANS_DMA_BUFFER_ALIGN_MANUAL but RX buffer addr&len not align to 4, or not dma_capable
E (572) spi_nand: nand_is_bad(90):
E (572) spi_nand: Error in nand_is_bad 258
E (582) spi_master: setup_priv_desc(918): Set flag SPI_TRANS_DMA_BUFFER_ALIGN_MANUAL but RX buffer addr&len not align to 4, or not dma_capable
E (592) spi_nand: nand_is_bad(90):
E (602) spi_nand: Error in nand_is_bad 258
E (602) spi_master: setup_priv_desc(918): Set flag SPI_TRANS_DMA_BUFFER_ALIGN_MANUAL but RX buffer addr&len not align to 4, or not dma_capable
E (612) spi_nand: nand_is_bad(90):
E (622) spi_nand: Error in nand_is_bad 258
E (622) spi_master: setup_priv_desc(918): Set flag SPI_TRANS_DMA_BUFFER_ALIGN_MANUAL but RX buffer addr&len not align to 4, or not dma_capable
E (642) spi_nand: nand_is_bad(90):
E (642) spi_nand: Error in nand_is_bad 258
E (652) spi_master: setup_priv_desc(918): Set flag SPI_TRANS_DMA_BUFFER_ALIGN_MANUAL but RX buffer addr&len not align to 4, or not dma_capable
E (662) spi_nand: nand_is_bad(90):
E (662) spi_nand: Error in nand_is_bad 258
E (672) diskio_nand: ff_nand_write(66): spi_nand_flash_write failed
E (672) diskio_nand: ff_nand_write failed with error 0x6003
E (682) vfs_fat_nand: f_mkfs failed (1)
E (692) example: Failed to mount filesystem. If you want the flash memory to be formatted, set the CONFIG_EXAMPLE_FORMAT_IF_MOUNT_FAILED menuconfig option.
I (702) main_task: Returned from app_main()
UPDATE:
I tried again this time I changed the verbosity level and got the following output:

Code: Select all

I (523) example: DMA CHANNEL: 3
D (523) gdma: new group (0) at 0x3fc92294
D (533) gdma: new pair (0,0) at 0x3fc92310
D (533) gdma: new tx channel (0,0) at 0x3fc9225c
D (543) gdma: new rx channel (0,0) at 0x3fc92330
D (543) spi: SPI2 use iomux pins.
D (543) intr_alloc: Connected src 19 to int 7 (cpu 0)
D (553) spi_hal: eff: 1000, limit: 80000k(/0), 0 dummy, -1 delay
D (553) spi_master: SPI2: New device added to CS0, effective clock: 1000000 Hz
D (563) nand_flash: detect_chip: manufacturer_id: ff

E (573) nand_flash: spi_nand_flash_init_device(86): Failed to detect nand chip

assert failed: vQueueDelete queue.c:2355 (pxQueue)
Core  0 register dump:
MEPC    : 0x403808f0  RA      : 0x40384f86  SP      : 0x3fc91520  GP      : 0x3fc8e200
I am using IDF version 5.4 and ESP32c3 board with W25N512GW.
Last edited by Dakshk on Wed Feb 26, 2025 12:31 pm, edited 1 time in total.

Dakshk
Posts: 16
Joined: Tue May 21, 2024 11:02 am

Re: External NAND Flash with ESP32-C3

Postby Dakshk » Wed Feb 26, 2025 10:21 am

Also I interfaced the NAND flash with MCU using IDF version 5.2.2 and got the chip ID successfully by reading from flash but when I try it using 5.4 IDF version all I get is 0xFF in whole buffer. Here I have shared link to project reference I am using for this project. REF: https://github.com/well-techn/Aquila/tree/master/Aquila

Upon debugging I found that CS pin is not working as expected in IDF v5.4, but works fine with IDF v5.2.2

ESP_adokitkat
Posts: 56
Joined: Thu Jun 22, 2023 12:50 pm

Re: External NAND Flash with ESP32-C3

Postby ESP_adokitkat » Mon Mar 03, 2025 12:50 am

Hello, thank you for sharing this. Will look into it.

RathiSonika
Posts: 18
Joined: Thu Jun 22, 2023 2:58 pm

Re: External NAND Flash with ESP32-C3

Postby RathiSonika » Mon Mar 03, 2025 9:29 am

Hi @Dakshk, Regarding the first error log, this issue occurs on ESP32-C3 due to a memory alignment issue. I have already raised a PR https://github.com/espressif/idf-extra- ... s/pull/480 to address this.
As for the 'CS pin not working as expected in IDF v5.4', I don’t see any relevant changes in the SPI driver between the mentioned versions. However, I will verify this further.

Meanwhile, could you try the changes from the PR and run your code again? If the issue persists, please attach the debug log and pin configurations for further analysis.
Also, you can try running this example on your setup https://github.com/espressif/idf-extra- ... nand_flash to verify if the hardware setup is working fine.

Dakshk
Posts: 16
Joined: Tue May 21, 2024 11:02 am

Re: [SOLVED]External NAND Flash with ESP32-C3

Postby Dakshk » Mon Mar 03, 2025 11:11 am

Hello @RathiSonika,
Thank you for the response, the issue was solved turns out that while debugging the issue previously I changed values of following macros
SPI2_IOMUX_PIN_NUM_MOSI and SPI2_IOMUX_PIN_NUM_MISO
in driver itself which I should not do, but when I reverted it to original values it started to work. I had given hardcoded values of SPI pins in config structure, but I think changing values of these macros may cause issue. Please do let me know if I am wrong here.

RathiSonika
Posts: 18
Joined: Thu Jun 22, 2023 2:58 pm

Re: External NAND Flash with ESP32-C3

Postby RathiSonika » Mon Mar 03, 2025 12:08 pm

Yes, the issue likely occurred because modifying the values of macros in the driver itself can lead to unexpected behavior. These macros are predefined to match the correct pins for the SPI interface on the hardware and changing them can result in incorrect pin assignments, causing the driver to fail to function as expected. Thank you for the confirmation. Let me know if you encounter any other issues.

Who is online

Users browsing this forum: No registered users and 89 guests