]> granicus.if.org Git - esp-idf/blob - components/spi_flash/include/esp_spi_flash.h
Merge branch 'bugfix/pthread_test_mem_leaks' into 'master'
[esp-idf] / components / spi_flash / include / esp_spi_flash.h
1 // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #ifndef ESP_SPI_FLASH_H
16 #define ESP_SPI_FLASH_H
17
18 #include <stdint.h>
19 #include <stdbool.h>
20 #include <stddef.h>
21 #include "esp_err.h"
22 #include "sdkconfig.h"
23
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27
28 #define ESP_ERR_FLASH_BASE       0x10010
29 #define ESP_ERR_FLASH_OP_FAIL    (ESP_ERR_FLASH_BASE + 1)
30 #define ESP_ERR_FLASH_OP_TIMEOUT (ESP_ERR_FLASH_BASE + 2)
31
32 #define SPI_FLASH_SEC_SIZE  4096    /**< SPI Flash sector size */
33
34 #define SPI_FLASH_MMU_PAGE_SIZE 0x10000 /**< Flash cache MMU mapping page size */
35
36 /**
37  * @brief  Initialize SPI flash access driver
38  *
39  *  This function must be called exactly once, before any other 
40  *  spi_flash_* functions are called.
41  *  Currently this function is called from startup code. There is
42  *  no need to call it from application code.
43  *
44  */
45 void spi_flash_init();
46
47 /**
48  * @brief  Get flash chip size, as set in binary image header
49  *
50  * @note This value does not necessarily match real flash size.
51  *
52  * @return size of flash chip, in bytes
53  */
54 size_t spi_flash_get_chip_size();
55
56 /**
57  * @brief  Erase the Flash sector.
58  *
59  * @param  sector  Sector number, the count starts at sector 0, 4KB per sector.
60  *
61  * @return esp_err_t
62  */
63 esp_err_t spi_flash_erase_sector(size_t sector);
64
65 /**
66  * @brief  Erase a range of flash sectors
67  *
68  * @param  start_address  Address where erase operation has to start.
69  *                                  Must be 4kB-aligned
70  * @param  size  Size of erased range, in bytes. Must be divisible by 4kB.
71  *
72  * @return esp_err_t
73  */
74 esp_err_t spi_flash_erase_range(size_t start_address, size_t size);
75
76
77 /**
78  * @brief  Write data to Flash.
79  *
80  * @note If source address is in DROM, this function will return
81  *       ESP_ERR_INVALID_ARG.
82  *
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.
86  *
87  * @return esp_err_t
88  */
89 esp_err_t spi_flash_write(size_t dest_addr, const void *src, size_t size);
90
91
92 /**
93  * @brief  Write data encrypted to Flash.
94  *
95  * @note Flash encryption must be enabled for this function to work.
96  *
97  * @note Flash encryption must be enabled when calling this function.
98  * If flash encryption is disabled, the function returns
99  * ESP_ERR_INVALID_STATE.  Use esp_flash_encryption_enabled()
100  * function to determine if flash encryption is enabled.
101  *
102  * @note Both dest_addr and size must be multiples of 16 bytes. For
103  * absolute best performance, both dest_addr and size arguments should
104  * be multiples of 32 bytes.
105  *
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.
109  *
110  * @return esp_err_t
111  */
112 esp_err_t spi_flash_write_encrypted(size_t dest_addr, const void *src, size_t size);
113
114 /**
115  * @brief  Read data from Flash.
116  *
117  * @param  src_addr source address of the data in Flash.
118  * @param  dest     pointer to the destination buffer
119  * @param  size     length of data
120  *
121  * @return esp_err_t
122  */
123 esp_err_t spi_flash_read(size_t src_addr, void *dest, size_t size);
124
125
126 /**
127  * @brief  Read data from Encrypted Flash.
128  *
129  * If flash encryption is enabled, this function will transparently decrypt data as it is read.
130  * If flash encryption is not enabled, this function behaves the same as spi_flash_read().
131  *
132  * See esp_flash_encryption_enabled() for a function to check if flash encryption is enabled.
133  *
134  * @param  src   source address of the data in Flash.
135  * @param  dest  pointer to the destination buffer
136  * @param  size  length of data
137  *
138  * @return esp_err_t
139  */
140 esp_err_t spi_flash_read_encrypted(size_t src, void *dest, size_t size);
141
142 /**
143  * @brief Enumeration which specifies memory space requested in an mmap call
144  */
145 typedef enum {
146     SPI_FLASH_MMAP_DATA,    /**< map to data memory (Vaddr0), allows byte-aligned access, 4 MB total */
147     SPI_FLASH_MMAP_INST,    /**< map to instruction memory (Vaddr1-3), allows only 4-byte-aligned access, 11 MB total */
148 } spi_flash_mmap_memory_t;
149
150 /**
151  * @brief Opaque handle for memory region obtained from spi_flash_mmap.
152  */
153 typedef uint32_t spi_flash_mmap_handle_t;
154
155 /**
156  * @brief Map region of flash memory into data or instruction address space
157  *
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.
164  *
165  * @param src_addr  Physical address in flash where requested region starts.
166  *                  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
171  * @param out_ptr  Output, pointer to the mapped memory region
172  * @param out_handle  Output, handle which should be used for spi_flash_munmap call
173  *
174  * @return  ESP_OK on success, ESP_ERR_NO_MEM if pages can not be allocated
175  */
176 esp_err_t spi_flash_mmap(size_t src_addr, size_t size, spi_flash_mmap_memory_t memory,
177                          const void** out_ptr, spi_flash_mmap_handle_t* out_handle);
178
179 /**
180  * @brief Map sequences of pages of flash memory into data or instruction address space
181  *
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.
187  *
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,
190  *              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
193  * @param out_ptr  Output, pointer to the mapped memory region
194  * @param out_handle  Output, handle which should be used for spi_flash_munmap call
195  *
196  * @return  ESP_OK on success, ESP_ERR_NO_MEM if pages can not be allocated
197  */
198 esp_err_t spi_flash_mmap_pages(int *pages, size_t pagecount, spi_flash_mmap_memory_t memory,
199                          const void** out_ptr, spi_flash_mmap_handle_t* out_handle);
200
201
202 /**
203  * @brief Release region previously obtained using spi_flash_mmap
204  *
205  * @note Calling this function will not necessarily unmap memory region.
206  *       Region will only be unmapped when there are no other handles which
207  *       reference this region. In case of partially overlapping regions
208  *       it is possible that memory will be unmapped partially.
209  *
210  * @param handle  Handle obtained from spi_flash_mmap
211  */
212 void spi_flash_munmap(spi_flash_mmap_handle_t handle);
213
214 /**
215  * @brief Display information about mapped regions
216  *
217  * This function lists handles obtained using spi_flash_mmap, along with range
218  * of pages allocated to each handle. It also lists all non-zero entries of
219  * MMU table and corresponding reference counts.
220  */
221 void spi_flash_mmap_dump();
222
223
224 #define SPI_FLASH_CACHE2PHYS_FAIL UINT32_MAX /*<! Result from spi_flash_cache2phys() if flash cache address is invalid */
225
226 /**
227  * @brief Given a memory address where flash is mapped, return the corresponding physical flash offset.
228  *
229  * Cache address does not have have been assigned via spi_flash_mmap(), any address in flash map space can be looked up.
230  *
231  * @param cached Pointer to flashed cached memory.
232  *
233  * @return
234  * - SPI_FLASH_CACHE2PHYS_FAIL If cache address is outside flash cache region, or the address is not mapped.
235  * - Otherwise, returns physical offset in flash
236  */
237 size_t spi_flash_cache2phys(const void *cached);
238
239 /** @brief Given a physical offset in flash, return the address where it is mapped in the memory space.
240  *
241  * Physical address does not have to have been assigned via spi_flash_mmap(), any address in flash can be looked up.
242  *
243  * @note Only the first matching cache address is returned. If MMU flash cache table is configured so multiple entries
244  * point to the same physical address, there may be more than one cache address corresponding to that physical
245  * address. It is also possible for a single physical address to be mapped to both the IROM and DROM regions.
246  *
247  * @note This function doesn't impose any alignment constraints, but if memory argument is SPI_FLASH_MMAP_INST and
248  * phys_offs is not 4-byte aligned, then reading from the returned pointer will result in a crash.
249  *
250  * @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)
252  *
253  * @return
254  * - NULL if the physical address is invalid or not mapped to flash cache of the specified memory type.
255  * - Cached memory address (in IROM or DROM space) corresponding to phys_offs.
256  */
257 const void *spi_flash_phys2cache(size_t phys_offs, spi_flash_mmap_memory_t memory);
258
259 /** @brief Check at runtime if flash cache is enabled on both CPUs
260  *
261  * @return true if both CPUs have flash cache enabled, false otherwise.
262  */
263 bool spi_flash_cache_enabled();
264
265 /**
266  * @brief SPI flash critical section enter function.
267  */
268 typedef void (*spi_flash_guard_start_func_t)(void);
269 /**
270  * @brief SPI flash critical section exit function.
271  */
272 typedef void (*spi_flash_guard_end_func_t)(void);
273 /**
274  * @brief SPI flash operation lock function.
275  */
276 typedef void (*spi_flash_op_lock_func_t)(void);
277 /**
278  * @brief SPI flash operation unlock function.
279  */
280 typedef void (*spi_flash_op_unlock_func_t)(void);
281
282 /**
283  * Structure holding SPI flash access critical sections management functions.
284  *
285  * Flash API uses two types of flash access management functions:
286  * 1) Functions which prepare/restore flash cache and interrupts before calling
287  *    appropriate ROM functions (SPIWrite, SPIRead and SPIEraseBlock):
288  *   - 'start' function should disables flash cache and non-IRAM interrupts and
289  *      is invoked before the call to one of ROM function above.
290  *   - 'end' function should restore state of flash cache and non-IRAM interrupts and
291  *      is invoked after the call to one of ROM function above.
292  *    These two functions are not recursive.
293  * 2) Functions which synchronizes access to internal data used by flash API.
294  *    This functions are mostly intended to synchronize access to flash API internal data
295  *    in multithreaded environment and use OS primitives:
296  *   - 'op_lock' locks access to flash API internal data.
297  *   - 'op_unlock' unlocks access to flash API internal data.
298  *   These two functions are recursive and can be used around the outside of multiple calls to
299  *   'start' & 'end', in order to create atomic multi-part flash operations.
300  *
301  * Different versions of the guarding functions should be used depending on the context of
302  * execution (with or without functional OS). In normal conditions when flash API is called
303  * from task the functions use OS primitives. When there is no OS at all or when
304  * it is not guaranteed that OS is functional (accessing flash from exception handler) these
305  * functions cannot use OS primitives or even does not need them (multithreaded access is not possible).
306  *
307  * @note Structure and corresponding guard functions should not reside in flash.
308  *       For example structure can be placed in DRAM and functions in IRAM sections.
309  */
310 typedef struct {
311     spi_flash_guard_start_func_t    start;      /**< critical section start function. */
312     spi_flash_guard_end_func_t      end;        /**< critical section end function. */
313     spi_flash_op_lock_func_t        op_lock;    /**< flash access API lock function.*/
314     spi_flash_op_unlock_func_t      op_unlock;  /**< flash access API unlock function.*/
315 } spi_flash_guard_funcs_t;
316
317 /**
318  * @brief  Sets guard functions to access flash.
319  *
320  * @note Pointed structure and corresponding guard functions should not reside in flash.
321  *       For example structure can be placed in DRAM and functions in IRAM sections.
322  *
323  * @param funcs pointer to structure holding flash access guard functions.
324  */
325 void spi_flash_guard_set(const spi_flash_guard_funcs_t* funcs);
326
327
328 /**
329  * @brief Get the guard functions used for flash access
330  *
331  * @return The guard functions that were set via spi_flash_guard_set(). These functions
332  * can be called if implementing custom low-level SPI flash operations.
333  */
334 const spi_flash_guard_funcs_t *spi_flash_guard_get();
335
336 /**
337  * @brief Default OS-aware flash access guard functions
338  */
339 extern const spi_flash_guard_funcs_t g_flash_guard_default_ops;
340
341 /**
342  * @brief Non-OS flash access guard functions
343  *
344  * @note This version of flash guard functions is to be used when no OS is present or from panic handler.
345  *       It does not use any OS primitives and IPC and implies that only calling CPU is active.
346  */
347 extern const spi_flash_guard_funcs_t g_flash_guard_no_os_ops;
348
349 #if CONFIG_SPI_FLASH_ENABLE_COUNTERS
350
351 /**
352  * Structure holding statistics for one type of operation
353  */
354 typedef struct {
355     uint32_t count;     // number of times operation was executed
356     uint32_t time;      // total time taken, in microseconds
357     uint32_t bytes;     // total number of bytes
358 } spi_flash_counter_t;
359
360 typedef struct {
361     spi_flash_counter_t read;
362     spi_flash_counter_t write;
363     spi_flash_counter_t erase;
364 } spi_flash_counters_t;
365
366 /**
367  * @brief  Reset SPI flash operation counters
368  */
369 void spi_flash_reset_counters();
370
371 /**
372  * @brief  Print SPI flash operation counters
373  */
374 void spi_flash_dump_counters();
375
376 /**
377  * @brief  Return current SPI flash operation counters
378  *
379  * @return  pointer to the spi_flash_counters_t structure holding values
380  *          of the operation counters
381  */
382 const spi_flash_counters_t* spi_flash_get_counters();
383
384 #endif //CONFIG_SPI_FLASH_ENABLE_COUNTERS
385
386 #ifdef __cplusplus
387 }
388 #endif
389
390
391 #endif /* ESP_SPI_FLASH_H */
392