Chapter 2. API Reference
following lines after the idf_component_register, in the CMakeLists.txt file of the custom_chip_driver compo-
nent:
idf_component_get_property(spi_flash_lib spi_flash COMPONENT_LIB)
set_property(TARGET ${spi_flash_lib} APPEND PROPERTY INTER-
FACE_LINK_LIBRARIES $<LINK_ONLY:${COMPONENT_LIB}>)
5. The linker.lf is 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.
6. Build your project, and you will see the new flash driver is used.
Example See also storage/custom_flash_driver.
Initializing a flash device
To use esp_flash_* APIs, you need to have a chip initialized on a certain SPI bus.
1. Call spi_bus_initialize() to properly initialize an SPI bus. This functions initialize the resources
(I/O, DMA, interrupts) shared among devices attached to this bus.
2. Call spi_bus_add_flash_device() to attach the flash device onto the bus. This allocates memory,
and fill the members for the esp_flash_t structure. The CS I/O is also initialized here.
3. Call esp_flash_init() to actually communicate with the chip. This will also detect the chip type, and
influence the following operations.
Note: Multiple flash chips can be attached to the same bus now. However, using esp_flash_* devices and
spi_device_* devices on the same SPI bus is not supported yet.
SPI flash access API
This is the set of API functions for working with data in flash:
• esp_flash_read() reads data from flash to RAM
• esp_flash_write() writes data from RAM to flash
• esp_flash_erase_region() erases specific region of flash
• esp_flash_erase_chip() erases the whole flash
• esp_flash_get_chip_size() returns flash chip size, in bytes, as configured in menuconfig
Generally, try to avoid using the raw SPI flash functions to the “main”SPI flash chip in favour of partition-specific
functions.
SPI Flash Size
The SPI flash size is configured by writing a field in the software bootloader image header, flashed at offset 0x1000.
By default, the SPI flash size is detected by esptool.py when this bootloader is written to flash, and the header
is updated with the correct size. Alternatively, it is possible to generate a fixed flash size by setting CON-
FIG_ESPTOOLPY_FLASHSIZE in project configuration.
If it is necessary to override the configured flash size at runtime, it is possible to set the chip_size member of
the g_rom_flashchip structure. This size is used by esp_flash_* functions (in both software & ROM) to
check the bounds.
Concurrency Constraints for flash on SPI1
Concurrency Constraints for flash on SPI1 The SPI0/1 bus is shared between the instruction & data cache
(for firmware execution) and the SPI1 peripheral (controlled by the drivers including this SPI Flash driver). Hence,
operations to SPI1 will cause significant influence to the whole system. This kind of operations include calling SPI
Espressif Systems 742
Submit Document Feedback
Release v4.4