Overriding Default Chip Driversο
Warning
Customizing SPI Flash Chip Drivers is considered an "expert" feature. Users should only do so at their own risk. (See the notes below)
During the SPI Flash driver's initialization (i.e., esp_flash_init()), there is a chip detection step during which the driver iterates through a Default Chip Driver List and determine which chip driver can properly support the currently connected flash chip. The Default Chip Drivers are provided by the ESP-IDF, thus are updated in together with each ESP-IDF version. However ESP-IDF also allows users to customize their own chip drivers.
Users should note the following when customizing chip drivers:
You may need to rely on some non-public ESP-IDF functions, which have slight possibility to change between ESP-IDF versions. On the one hand, these changes may be useful bug fixes for your driver, on the other hand, they may also be breaking changes (i.e., breaks your code).
Some ESP-IDF bug fixes to other chip drivers are not automatically applied to your own custom chip drivers.
If the protection of flash is not handled properly, there may be some random reliability issues.
If you update to a newer ESP-IDF version that has support for more chips, you will have to manually add those new chip drivers into your custom chip driver list. Otherwise the driver will only search for the drivers in custom list you provided.
Steps For Creating Custom Chip Drivers and Overriding the ESP-IDF Default Driver Listο
Enable the CONFIG_SPI_FLASH_OVERRIDE_CHIP_DRIVER_LIST config option. This prevents compilation and linking of the Default Chip Driver List (
default_registered_chips) provided by ESP-IDF. Instead, the linker searches for the structure of the same name (default_registered_chips) that must be provided by the user.Add a new component in your project, e.g.,
custom_chip_driver.Copy the necessary chip driver files from the
spi_flashcomponent in ESP-IDF. This may include:spi_flash_chip_drivers.c(to provide thedefault_registered_chipsstructure)Any of the
spi_flash_chip_*.cfiles that matches your own flash model bestCMakeLists.txtandlinker.lffiles
Modify the files above properly. Including:
Change the
default_registered_chipsvariable to non-static and remove the #ifdef logic around it.Update
linker.lffile to rename the fragment header and the library name to match the new component.If reusing other drivers, some header names need prefixing with
spi_flash/when included from outside spi_flash component.
Note
When writing your own flash chip driver, you can set your flash chip capabilities through
spi_flash_chip_***(vendor)_get_capsand points the function pointerget_chip_capsfor protection to thespi_flash_chip_***_get_capsfunction. The steps are as follows.Please check whether your flash chip have the capabilities listed in
spi_flash_caps_tby checking the flash datasheet.Write a function named
spi_flash_chip_***(vendor)_get_caps. Take the example below as a reference (if the flash supportsuspendandread unique id).Points the pointer
get_chip_caps(inspi_flash_chip_t) to the function mentioned above.
spi_flash_caps_t spi_flash_chip_***(vendor)_get_caps(esp_flash_t *chip) { spi_flash_caps_t caps_flags = 0; // 32-bit-address flash is not supported flash-suspend is supported caps_flags |= SPI_FLAHS_CHIP_CAP_SUSPEND; // flash read unique id. caps_flags |= SPI_FLASH_CHIP_CAP_UNIQUE_ID; return caps_flags; }
const spi_flash_chip_t esp_flash_chip_eon = { // Other function pointers .get_chip_caps = spi_flash_chip_eon_get_caps, };
You also can see how to implement this in the example storage/custom_flash_driver. This example demonstrates how to override the default chip driver list.
Write a new
CMakeLists.txtfile for thecustom_chip_drivercomponent, including an additional line to add a linker dependency fromspi_flashtocustom_chip_driver:idf_component_register(SRCS "spi_flash_chip_drivers.c" "spi_flash_chip_mychip.c" # modify as needed REQUIRES hal PRIV_REQUIRES spi_flash LDFRAGMENTS linker.lf) idf_component_add_link_dependency(FROM spi_flash)
An example of this component CMakeLists.txt can be found in storage/custom_flash_driver/components/custom_chip_driver/CMakeLists.txt
The
linker.lfis used to put every chip driver that you are going to use whilst cache is disabled into internal RAM. See Linker Script Generation for more details. Make sure this file covers all the source files that you add.Build your project, and you will see the new flash driver is used.