esp_spi_flash.h 13 KB

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