Skip to content

Commit b52b4ea

Browse files
committed
spi_flash: Update docs
1 parent c3bec5b commit b52b4ea

File tree

2 files changed

+79
-55
lines changed

2 files changed

+79
-55
lines changed

components/spi_flash/README.rst

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ SPI flash access APIs
1818

1919
This is the set of APIs for working with data in flash:
2020

21-
- ``spi_flash_read`` used to read data from flash to RAM
22-
- ``spi_flash_write`` used to write data from RAM to flash
23-
- ``spi_flash_erase_sector`` used to erase individual sectors of flash
24-
- ``spi_flash_erase_range`` used to erase range of addresses in flash
25-
- ``spi_flash_get_chip_size`` returns flash chip size, in bytes, as configured in menuconfig
21+
- :cpp:func:`spi_flash_read` used to read data from flash to RAM
22+
- :cpp:func:`spi_flash_write` used to write data from RAM to flash
23+
- :cpp:func:`spi_flash_erase_sector` used to erase individual sectors of flash
24+
- :cpp:func:`spi_flash_erase_range` used to erase range of addresses in flash
25+
- :cpp:func:`spi_flash_get_chip_size` returns flash chip size, in bytes, as configured in menuconfig
2626

2727
Generally, try to avoid using the raw SPI flash functions in favour of
28-
partition-specific functions.
28+
:ref:`partition-specific functions <flash-partition-apis>`.
2929

3030
SPI Flash Size
3131
--------------
@@ -35,8 +35,8 @@ image header, flashed at offset 0x1000.
3535

3636
By default, the SPI flash size is detected by esptool.py when this bootloader is
3737
written to flash, and the header is updated with the correct
38-
size. Alternatively, it is possible to generate a fixed flash size by disabling
39-
detection in ``make menuconfig`` (under Serial Flasher Config).
38+
size. Alternatively, it is possible to generate a fixed flash size by setting
39+
:ref:`CONFIG_ESPTOOLPY_FLASHSIZE` in ``make menuconfig``.
4040

4141
If it is necessary to override the configured flash size at runtime, is is
4242
possible to set the ``chip_size`` member of ``g_rom_flashchip`` structure. This
@@ -47,10 +47,14 @@ Concurrency Constraints
4747
-----------------------
4848

4949
Because the SPI flash is also used for firmware execution (via the instruction &
50-
data caches), these caches much be disabled while reading/writing/erasing. This
50+
data caches), these caches must be disabled while reading/writing/erasing. This
5151
means that both CPUs must be running code from IRAM and only reading data from
5252
DRAM while flash write operations occur.
5353

54+
If you use the APIs documented here, then this happens automatically and
55+
transparently. However note that it will have some performance impact on other
56+
tasks in the system.
57+
5458
Refer to the :ref:`application memory layout <memory-layout>` documentation for
5559
an explanation of the differences between IRAM, DRAM and flash cache.
5660

@@ -99,6 +103,8 @@ handler reads from the flash cache during a flash operation, it will cause a
99103
crash due to Illegal Instruction exception (for code which should be in IRAM) or
100104
garbage data to be read (for constant data which should be in DRAM).
101105

106+
.. _flash-partition-apis:
107+
102108
Partition table APIs
103109
--------------------
104110

@@ -109,20 +115,21 @@ More information about partition tables can be found :doc:`here </api-guides/par
109115
This component provides APIs to enumerate partitions found in the partition table
110116
and perform operations on them. These functions are declared in ``esp_partition.h``:
111117

112-
- ``esp_partition_find`` used to search partition table for entries with
118+
- :cpp:func:`esp_partition_find` used to search partition table for entries with
113119
specific type, returns an opaque iterator
114-
- ``esp_partition_get`` returns a structure describing the partition, for the given iterator
115-
- ``esp_partition_next`` advances iterator to the next partition found
116-
- ``esp_partition_iterator_release`` releases iterator returned by ``esp_partition_find``
117-
- ``esp_partition_find_first`` is a convenience function which returns structure
120+
- :cpp:func:`esp_partition_get` returns a structure describing the partition, for the given iterator
121+
- :cpp:func:`esp_partition_next` advances iterator to the next partition found
122+
- :cpp:func:`esp_partition_iterator_release` releases iterator returned by ``esp_partition_find``
123+
- :cpp:func:`esp_partition_find_first` is a convenience function which returns structure
118124
describing the first partition found by esp_partition_find
119-
- ``esp_partition_read``, ``esp_partition_write``, ``esp_partition_erase_range``
120-
are equivalent to ``spi_flash_read``, ``spi_flash_write``,
121-
``spi_flash_erase_range``, but operate within partition boundaries
125+
- :cpp:func:`esp_partition_read`, :cpp:func:`esp_partition_write`, :cpp:func:`esp_partition_erase_range`
126+
are equivalent to :cpp:func:`spi_flash_read`, :cpp:func:`spi_flash_write`,
127+
:cpp:func:`spi_flash_erase_range`, but operate within partition boundaries
122128

123-
Most application code should use ``esp_partition_*`` APIs instead of lower level
124-
``spi_flash_*`` APIs. Partition APIs do bounds checking and calculate correct
125-
offsets in flash based on data stored in partition table.
129+
.. note::
130+
Most application code should use these ``esp_partition_*`` APIs instead of lower level
131+
``spi_flash_*`` APIs. Partition APIs do bounds checking and calculate correct
132+
offsets in flash based on data stored in partition table.
126133

127134
SPI Flash Encryption
128135
--------------------
@@ -151,14 +158,14 @@ Decryption is performed at hardware level.
151158

152159
Memory mapping APIs are declared in ``esp_spi_flash.h`` and ``esp_partition.h``:
153160

154-
- ``spi_flash_mmap`` maps a region of physical flash addresses into instruction space or data space of the CPU
155-
- ``spi_flash_munmap`` unmaps previously mapped region
156-
- ``esp_partition_mmap`` maps part of a partition into the instruction space or data space of the CPU
161+
- :cpp:func:`spi_flash_mmap` maps a region of physical flash addresses into instruction space or data space of the CPU
162+
- :cpp:func:`spi_flash_munmap` unmaps previously mapped region
163+
- :cpp:func:`esp_partition_mmap` maps part of a partition into the instruction space or data space of the CPU
157164

158-
Differences between ``spi_flash_mmap`` and ``esp_partition_mmap`` are as follows:
165+
Differences between :cpp:func:`spi_flash_mmap` and :cpp:func:`esp_partition_mmap` are as follows:
159166

160-
- ``spi_flash_mmap`` must be given a 64KB aligned physical address
161-
- ``esp_partition_mmap`` may be given an arbitrary offset within the partition,
167+
- :cpp:func:`spi_flash_mmap` must be given a 64KB aligned physical address
168+
- :cpp:func:`esp_partition_mmap` may be given any arbitrary offset within the partition,
162169
it will adjust returned pointer to mapped memory as necessary
163170

164171
Note that because memory mapping happens in 64KB blocks, it may be possible to

components/spi_flash/include/esp_spi_flash.h

Lines changed: 46 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,16 @@ esp_err_t spi_flash_erase_range(size_t start_address, size_t size);
7777
/**
7878
* @brief Write data to Flash.
7979
*
80-
* @note If source address is in DROM, this function will return
81-
* ESP_ERR_INVALID_ARG.
80+
* @note For fastest write performance, write a 4 byte aligned size at a
81+
* 4 byte aligned offset in flash from a source buffer in DRAM. Varying any of
82+
* these parameters will still work, but will be slower due to buffering.
8283
*
83-
* @param dest_addr destination address in Flash. Must be a multiple of 4 bytes.
84-
* @param src pointer to the source buffer.
85-
* @param size length of data, in bytes. Must be a multiple of 4 bytes.
84+
* @note Writing more than 8KB at a time will be split into multiple
85+
* write operations to avoid disrupting other tasks in the system.
86+
*
87+
* @param dest_addr Destination address in Flash.
88+
* @param src Pointer to the source buffer.
89+
* @param size Length of data, in bytes.
8690
*
8791
* @return esp_err_t
8892
*/
@@ -103,9 +107,9 @@ esp_err_t spi_flash_write(size_t dest_addr, const void *src, size_t size);
103107
* absolute best performance, both dest_addr and size arguments should
104108
* be multiples of 32 bytes.
105109
*
106-
* @param dest_addr destination address in Flash. Must be a multiple of 16 bytes.
107-
* @param src pointer to the source buffer.
108-
* @param size length of data, in bytes. Must be a multiple of 16 bytes.
110+
* @param dest_addr Destination address in Flash. Must be a multiple of 16 bytes.
111+
* @param src Pointer to the source buffer.
112+
* @param size Length of data, in bytes. Must be a multiple of 16 bytes.
109113
*
110114
* @return esp_err_t
111115
*/
@@ -114,10 +118,22 @@ esp_err_t spi_flash_write_encrypted(size_t dest_addr, const void *src, size_t si
114118
/**
115119
* @brief Read data from Flash.
116120
*
121+
* @note For fastest read performance, all parameters should be
122+
* 4 byte aligned. If source address and read size are not 4 byte
123+
* aligned, read may be split into multiple flash operations. If
124+
* destination buffer is not 4 byte aligned, a temporary buffer will
125+
* be allocated on the stack.
126+
*
127+
* @note Reading more than 16KB of data at a time will be split
128+
* into multiple reads to avoid disruption to other tasks in the
129+
* system. Consider using spi_flash_mmap() to read large amounts
130+
* of data.
131+
*
117132
* @param src_addr source address of the data in Flash.
118133
* @param dest pointer to the destination buffer
119134
* @param size length of data
120135
*
136+
*
121137
* @return esp_err_t
122138
*/
123139
esp_err_t spi_flash_read(size_t src_addr, void *dest, size_t size);
@@ -155,19 +171,20 @@ typedef uint32_t spi_flash_mmap_handle_t;
155171
/**
156172
* @brief Map region of flash memory into data or instruction address space
157173
*
158-
* This function allocates sufficient number of 64k MMU pages and configures
159-
* them to map request region of flash memory into data address space or into
160-
* instruction address space. It may reuse MMU pages which already provide
161-
* required mapping. As with any allocator, there is possibility of fragmentation
162-
* of address space if mmap/munmap are heavily used. To troubleshoot issues with
163-
* page allocation, use spi_flash_mmap_dump function.
174+
* This function allocates sufficient number of 64kB MMU pages and configures
175+
* them to map the requested region of flash memory into the address space.
176+
* It may reuse MMU pages which already provide the required mapping.
177+
*
178+
* As with any allocator, if mmap/munmap are heavily used then the address space
179+
* may become fragmented. To troubleshoot issues with page allocation, use
180+
* spi_flash_mmap_dump() function.
164181
*
165182
* @param src_addr Physical address in flash where requested region starts.
166183
* This address *must* be aligned to 64kB boundary
167-
* (SPI_FLASH_MMU_PAGE_SIZE).
168-
* @param size Size of region which has to be mapped. This size will be rounded
169-
* up to a 64k boundary.
170-
* @param memory Memory space where the region should be mapped
184+
* (SPI_FLASH_MMU_PAGE_SIZE)
185+
* @param size Size of region to be mapped. This size will be rounded
186+
* up to a 64kB boundary
187+
* @param memory Address space where the region should be mapped (data or instruction)
171188
* @param out_ptr Output, pointer to the mapped memory region
172189
* @param out_handle Output, handle which should be used for spi_flash_munmap call
173190
*
@@ -179,17 +196,16 @@ esp_err_t spi_flash_mmap(size_t src_addr, size_t size, spi_flash_mmap_memory_t m
179196
/**
180197
* @brief Map sequences of pages of flash memory into data or instruction address space
181198
*
182-
* This function allocates sufficient number of 64k MMU pages and configures
183-
* them to map the indicated pages of flash memory contiguously into data address
184-
* space or into instruction address space. In this respect, it works in a similar
185-
* way as spi_flash_mmap but it allows mapping a (maybe non-contiguous) set of pages
186-
* into a contiguous region of memory.
199+
* This function allocates sufficient number of 64kB MMU pages and configures
200+
* them to map the indicated pages of flash memory contiguously into address space.
201+
* In this respect, it works in a similar way as spi_flash_mmap() but it allows mapping
202+
* a (maybe non-contiguous) set of pages into a contiguous region of memory.
187203
*
188-
* @param pages An array of numbers indicating the 64K pages in flash to be mapped
189-
* contiguously into memory. These indicate the indexes of the 64K pages,
204+
* @param pages An array of numbers indicating the 64kB pages in flash to be mapped
205+
* contiguously into memory. These indicate the indexes of the 64kB pages,
190206
* not the byte-size addresses as used in other functions.
191-
* @param pagecount Size of the pages array
192-
* @param memory Memory space where the region should be mapped
207+
* @param pagecount Number of entries in the pages array
208+
* @param memory Address space where the region should be mapped (instruction or data)
193209
* @param out_ptr Output, pointer to the mapped memory region
194210
* @param out_handle Output, handle which should be used for spi_flash_munmap call
195211
*
@@ -226,7 +242,7 @@ void spi_flash_mmap_dump();
226242
/**
227243
* @brief Given a memory address where flash is mapped, return the corresponding physical flash offset.
228244
*
229-
* Cache address does not have have been assigned via spi_flash_mmap(), any address in flash map space can be looked up.
245+
* Cache address does not have have been assigned via spi_flash_mmap(), any address in memory mapped flash space can be looked up.
230246
*
231247
* @param cached Pointer to flashed cached memory.
232248
*
@@ -248,7 +264,7 @@ size_t spi_flash_cache2phys(const void *cached);
248264
* phys_offs is not 4-byte aligned, then reading from the returned pointer will result in a crash.
249265
*
250266
* @param phys_offs Physical offset in flash memory to look up.
251-
* @param memory Memory type to look up a flash cache address mapping for (IROM or DROM)
267+
* @param memory Address space type to look up a flash cache address mapping for (instruction or data)
252268
*
253269
* @return
254270
* - NULL if the physical address is invalid or not mapped to flash cache of the specified memory type.
@@ -264,6 +280,7 @@ bool spi_flash_cache_enabled();
264280

265281
/**
266282
* @brief SPI flash critical section enter function.
283+
*
267284
*/
268285
typedef void (*spi_flash_guard_start_func_t)(void);
269286
/**

0 commit comments

Comments
 (0)