esp_flash.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. // Copyright 2015-2019 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. #pragma once
  15. #include "esp_err.h"
  16. #include <stdint.h>
  17. #include <stdbool.h>
  18. #include "hal/spi_flash_types.h"
  19. #ifdef __cplusplus
  20. extern "C" {
  21. #endif
  22. struct spi_flash_chip_t;
  23. typedef struct spi_flash_chip_t spi_flash_chip_t;
  24. typedef struct esp_flash_t esp_flash_t;
  25. /** @brief Structure for describing a region of flash */
  26. typedef struct {
  27. uint32_t offset; ///< Start address of this region
  28. uint32_t size; ///< Size of the region
  29. } esp_flash_region_t;
  30. /** @brief OS-level integration hooks for accessing flash chips inside a running OS
  31. *
  32. * It's in the public header because some instances should be allocated statically in the startup
  33. * code. May be updated according to hardware version and new flash chip feature requirements,
  34. * shouldn't be treated as public API.
  35. *
  36. * For advanced developers, you may replace some of them with your implementations at your own
  37. * risk.
  38. */
  39. typedef struct {
  40. /**
  41. * Called before commencing any flash operation. Does not need to be
  42. * recursive (ie is called at most once for each call to 'end').
  43. */
  44. esp_err_t (*start)(void *arg);
  45. /** Called after completing any flash operation. */
  46. esp_err_t (*end)(void *arg);
  47. /** Called before any erase/write operations to check whether the region is limited by the OS */
  48. esp_err_t (*region_protected)(void* arg, size_t start_addr, size_t size);
  49. /** Delay for at least 'us' microseconds. Called in between 'start' and 'end'. */
  50. esp_err_t (*delay_us)(void *arg, uint32_t us);
  51. /** Called for get temp buffer when buffer from application cannot be directly read into/write from. */
  52. void *(*get_temp_buffer)(void* arg, size_t reqest_size, size_t* out_size);
  53. /** Called for release temp buffer. */
  54. void (*release_temp_buffer)(void* arg, void *temp_buf);
  55. #define SPI_FLASH_YIELD_REQ_YIELD BIT(0)
  56. #define SPI_FLASH_YIELD_REQ_SUSPEND BIT(1)
  57. /** Yield to other tasks. Called during erase operations.
  58. * @return ESP_OK means yield needs to be called (got an event to handle), while ESP_ERR_TIMEOUT means skip yield.*/
  59. esp_err_t (*check_yield)(void *arg, uint32_t chip_status, uint32_t* out_request);
  60. #define SPI_FLASH_YIELD_STA_RESUME BIT(2)
  61. /** Yield to other tasks. Called during erase operations. */
  62. esp_err_t (*yield)(void *arg, uint32_t* out_status);
  63. /** Called for get system time. */
  64. int64_t (*get_system_time)(void *arg);
  65. } esp_flash_os_functions_t;
  66. /** @brief Structure to describe a SPI flash chip connected to the system.
  67. Structure must be initialized before use (passed to esp_flash_init()). It's in the public
  68. header because some instances should be allocated statically in the startup code. May be
  69. updated according to hardware version and new flash chip feature requirements, shouldn't be
  70. treated as public API.
  71. For advanced developers, you may replace some of them with your implementations at your own
  72. risk.
  73. */
  74. struct esp_flash_t {
  75. spi_flash_host_inst_t* host; ///< Pointer to hardware-specific "host_driver" structure. Must be initialized before used.
  76. const spi_flash_chip_t *chip_drv; ///< Pointer to chip-model-specific "adapter" structure. If NULL, will be detected during initialisation.
  77. const esp_flash_os_functions_t *os_func; ///< Pointer to os-specific hook structure. Call ``esp_flash_init_os_functions()`` to setup this field, after the host is properly initialized.
  78. void *os_func_data; ///< Pointer to argument for os-specific hooks. Left NULL and will be initialized with ``os_func``.
  79. esp_flash_io_mode_t read_mode; ///< Configured SPI flash read mode. Set before ``esp_flash_init`` is called.
  80. uint32_t size; ///< Size of SPI flash in bytes. If 0, size will be detected during initialisation.
  81. uint32_t chip_id; ///< Detected chip id.
  82. uint32_t busy :1; ///< This flag is used to verify chip's status.
  83. uint32_t reserved_flags :31; ///< reserved.
  84. };
  85. /** @brief Initialise SPI flash chip interface.
  86. *
  87. * This function must be called before any other API functions are called for this chip.
  88. *
  89. * @note Only the ``host`` and ``read_mode`` fields of the chip structure must
  90. * be initialised before this function is called. Other fields may be
  91. * auto-detected if left set to zero or NULL.
  92. *
  93. * @note If the chip->drv pointer is NULL, chip chip_drv will be auto-detected
  94. * based on its manufacturer & product IDs. See
  95. * ``esp_flash_registered_flash_drivers`` pointer for details of this process.
  96. *
  97. * @param chip Pointer to SPI flash chip to use. If NULL, esp_flash_default_chip is substituted.
  98. * @return ESP_OK on success, or a flash error code if initialisation fails.
  99. */
  100. esp_err_t esp_flash_init(esp_flash_t *chip);
  101. /**
  102. * Check if appropriate chip driver is set.
  103. *
  104. * @param chip Pointer to SPI flash chip to use. If NULL, esp_flash_default_chip is substituted.
  105. *
  106. * @return true if set, otherwise false.
  107. */
  108. bool esp_flash_chip_driver_initialized(const esp_flash_t *chip);
  109. /** @brief Read flash ID via the common "RDID" SPI flash command.
  110. *
  111. * @param chip Pointer to identify flash chip. Must have been successfully initialised via esp_flash_init()
  112. * @param[out] out_id Pointer to receive ID value.
  113. *
  114. * ID is a 24-bit value. Lower 16 bits of 'id' are the chip ID, upper 8 bits are the manufacturer ID.
  115. *
  116. * @return ESP_OK on success, or a flash error code if operation failed.
  117. */
  118. esp_err_t esp_flash_read_id(esp_flash_t *chip, uint32_t *out_id);
  119. /** @brief Detect flash size based on flash ID.
  120. *
  121. * @param chip Pointer to identify flash chip. Must have been successfully initialised via esp_flash_init()
  122. * @param[out] out_size Detected size in bytes.
  123. *
  124. * @note Most flash chips use a common format for flash ID, where the lower 4 bits specify the size as a power of 2. If
  125. * the manufacturer doesn't follow this convention, the size may be incorrectly detected.
  126. *
  127. * @return ESP_OK on success, or a flash error code if operation failed.
  128. */
  129. esp_err_t esp_flash_get_size(esp_flash_t *chip, uint32_t *out_size);
  130. /** @brief Read flash unique ID via the common "RDUID" SPI flash command.
  131. *
  132. * @param chip Pointer to identify flash chip. Must have been successfully initialised via esp_flash_init().
  133. * @param[out] out_id Pointer to receive unique ID value.
  134. *
  135. * ID is a 64-bit value.
  136. *
  137. * @return
  138. * - ESP_OK on success, or a flash error code if operation failed.
  139. * - ESP_ERR_NOT_SUPPORTED if the chip doesn't support read id.
  140. */
  141. esp_err_t esp_flash_read_unique_chip_id(esp_flash_t *chip, uint64_t *out_id);
  142. /** @brief Erase flash chip contents
  143. *
  144. * @param chip Pointer to identify flash chip. Must have been successfully initialised via esp_flash_init()
  145. *
  146. *
  147. * @return
  148. * - ESP_OK on success,
  149. * - ESP_ERR_NOT_SUPPORTED if the chip is not able to perform the operation. This is indicated by WREN = 1 after the command is sent.
  150. * - Other flash error code if operation failed.
  151. */
  152. esp_err_t esp_flash_erase_chip(esp_flash_t *chip);
  153. /** @brief Erase a region of the flash chip
  154. *
  155. * @param chip Pointer to identify flash chip. Must have been successfully initialised via esp_flash_init()
  156. * @param start Address to start erasing flash. Must be sector aligned.
  157. * @param len Length of region to erase. Must also be sector aligned.
  158. *
  159. * Sector size is specifyed in chip->drv->sector_size field (typically 4096 bytes.) ESP_ERR_INVALID_ARG will be
  160. * returned if the start & length are not a multiple of this size.
  161. *
  162. * Erase is performed using block (multi-sector) erases where possible (block size is specified in
  163. * chip->drv->block_erase_size field, typically 65536 bytes). Remaining sectors are erased using individual sector erase
  164. * commands.
  165. *
  166. * @return
  167. * - ESP_OK on success,
  168. * - ESP_ERR_NOT_SUPPORTED if the chip is not able to perform the operation. This is indicated by WREN = 1 after the command is sent.
  169. * - Other flash error code if operation failed.
  170. */
  171. esp_err_t esp_flash_erase_region(esp_flash_t *chip, uint32_t start, uint32_t len);
  172. /** @brief Read if the entire chip is write protected
  173. *
  174. * @param chip Pointer to identify flash chip. Must have been successfully initialised via esp_flash_init()
  175. * @param[out] write_protected Pointer to boolean, set to the value of the write protect flag.
  176. *
  177. * @note A correct result for this flag depends on the SPI flash chip model and chip_drv in use (via the 'chip->drv'
  178. * field).
  179. *
  180. * @return ESP_OK on success, or a flash error code if operation failed.
  181. */
  182. esp_err_t esp_flash_get_chip_write_protect(esp_flash_t *chip, bool *write_protected);
  183. /** @brief Set write protection for the SPI flash chip
  184. *
  185. * @param chip Pointer to identify flash chip. Must have been successfully initialised via esp_flash_init()
  186. * @param write_protect Boolean value for the write protect flag
  187. *
  188. * @note Correct behaviour of this function depends on the SPI flash chip model and chip_drv in use (via the 'chip->drv'
  189. * field).
  190. *
  191. * Some SPI flash chips may require a power cycle before write protect status can be cleared. Otherwise,
  192. * write protection can be removed via a follow-up call to this function.
  193. *
  194. * @return ESP_OK on success, or a flash error code if operation failed.
  195. */
  196. esp_err_t esp_flash_set_chip_write_protect(esp_flash_t *chip, bool write_protect);
  197. /** @brief Read the list of individually protectable regions of this SPI flash chip.
  198. *
  199. * @param chip Pointer to identify flash chip. Must have been successfully initialised via esp_flash_init()
  200. * @param[out] out_regions Pointer to receive a pointer to the array of protectable regions of the chip.
  201. * @param[out] out_num_regions Pointer to an integer receiving the count of protectable regions in the array returned in 'regions'.
  202. *
  203. * @note Correct behaviour of this function depends on the SPI flash chip model and chip_drv in use (via the 'chip->drv'
  204. * field).
  205. *
  206. * @return ESP_OK on success, or a flash error code if operation failed.
  207. */
  208. esp_err_t esp_flash_get_protectable_regions(const esp_flash_t *chip, const esp_flash_region_t **out_regions, uint32_t *out_num_regions);
  209. /** @brief Detect if a region of the SPI flash chip is protected
  210. *
  211. * @param chip Pointer to identify flash chip. Must have been successfully initialised via esp_flash_init()
  212. * @param region Pointer to a struct describing a protected region. This must match one of the regions returned from esp_flash_get_protectable_regions(...).
  213. * @param[out] out_protected Pointer to a flag which is set based on the protected status for this region.
  214. *
  215. * @note It is possible for this result to be false and write operations to still fail, if protection is enabled for the entire chip.
  216. *
  217. * @note Correct behaviour of this function depends on the SPI flash chip model and chip_drv in use (via the 'chip->drv'
  218. * field).
  219. *
  220. * @return ESP_OK on success, or a flash error code if operation failed.
  221. */
  222. esp_err_t esp_flash_get_protected_region(esp_flash_t *chip, const esp_flash_region_t *region, bool *out_protected);
  223. /** @brief Update the protected status for a region of the SPI flash chip
  224. *
  225. * @param chip Pointer to identify flash chip. Must have been successfully initialised via esp_flash_init()
  226. * @param region Pointer to a struct describing a protected region. This must match one of the regions returned from esp_flash_get_protectable_regions(...).
  227. * @param protect Write protection flag to set.
  228. *
  229. * @note It is possible for the region protection flag to be cleared and write operations to still fail, if protection is enabled for the entire chip.
  230. *
  231. * @note Correct behaviour of this function depends on the SPI flash chip model and chip_drv in use (via the 'chip->drv'
  232. * field).
  233. *
  234. * @return ESP_OK on success, or a flash error code if operation failed.
  235. */
  236. esp_err_t esp_flash_set_protected_region(esp_flash_t *chip, const esp_flash_region_t *region, bool protect);
  237. /** @brief Read data from the SPI flash chip
  238. *
  239. * @param chip Pointer to identify flash chip. Must have been successfully initialised via esp_flash_init()
  240. * @param buffer Pointer to a buffer where the data will be read. To get better performance, this should be in the DRAM and word aligned.
  241. * @param address Address on flash to read from. Must be less than chip->size field.
  242. * @param length Length (in bytes) of data to read.
  243. *
  244. * There are no alignment constraints on buffer, address or length.
  245. *
  246. * @note If on-chip flash encryption is used, this function returns raw (ie encrypted) data. Use the flash cache
  247. * to transparently decrypt data.
  248. *
  249. * @return
  250. * - ESP_OK: success
  251. * - ESP_ERR_NO_MEM: Buffer is in external PSRAM which cannot be concurrently accessed, and a temporary internal buffer could not be allocated.
  252. * - or a flash error code if operation failed.
  253. */
  254. esp_err_t esp_flash_read(esp_flash_t *chip, void *buffer, uint32_t address, uint32_t length);
  255. /** @brief Write data to the SPI flash chip
  256. *
  257. * @param chip Pointer to identify flash chip. Must have been successfully initialised via esp_flash_init()
  258. * @param address Address on flash to write to. Must be previously erased (SPI NOR flash can only write bits 1->0).
  259. * @param buffer Pointer to a buffer with the data to write. To get better performance, this should be in the DRAM and word aligned.
  260. * @param length Length (in bytes) of data to write.
  261. *
  262. * There are no alignment constraints on buffer, address or length.
  263. *
  264. * @return
  265. * - ESP_OK on success,
  266. * - ESP_ERR_NOT_SUPPORTED if the chip is not able to perform the operation. This is indicated by WREN = 1 after the command is sent.
  267. * - Other flash error code if operation failed.
  268. */
  269. esp_err_t esp_flash_write(esp_flash_t *chip, const void *buffer, uint32_t address, uint32_t length);
  270. /** @brief Encrypted and write data to the SPI flash chip using on-chip hardware flash encryption
  271. *
  272. * @param chip Pointer to identify flash chip. Must be NULL (the main flash chip). For other chips, encrypted write is not supported.
  273. * @param address Address on flash to write to. 16 byte aligned. Must be previously erased (SPI NOR flash can only write bits 1->0).
  274. * @param buffer Pointer to a buffer with the data to write.
  275. * @param length Length (in bytes) of data to write. 16 byte aligned.
  276. *
  277. * @note Both address & length must be 16 byte aligned, as this is the encryption block size
  278. *
  279. * @return
  280. * - ESP_OK: on success
  281. * - ESP_ERR_NOT_SUPPORTED: encrypted write not supported for this chip.
  282. * - ESP_ERR_INVALID_ARG: Either the address, buffer or length is invalid.
  283. * - or other flash error code from spi_flash_write_encrypted().
  284. */
  285. esp_err_t esp_flash_write_encrypted(esp_flash_t *chip, uint32_t address, const void *buffer, uint32_t length);
  286. /** @brief Read and decrypt data from the SPI flash chip using on-chip hardware flash encryption
  287. *
  288. * @param chip Pointer to identify flash chip. Must be NULL (the main flash chip). For other chips, encrypted read is not supported.
  289. * @param address Address on flash to read from.
  290. * @param out_buffer Pointer to a buffer for the data to read to.
  291. * @param length Length (in bytes) of data to read.
  292. *
  293. * @return
  294. * - ESP_OK: on success
  295. * - ESP_ERR_NOT_SUPPORTED: encrypted read not supported for this chip.
  296. * - or other flash error code from spi_flash_read_encrypted().
  297. */
  298. esp_err_t esp_flash_read_encrypted(esp_flash_t *chip, uint32_t address, void *out_buffer, uint32_t length);
  299. /** @brief Pointer to the "default" SPI flash chip, ie the main chip attached to the MCU.
  300. This chip is used if the 'chip' argument pass to esp_flash_xxx API functions is ever NULL.
  301. */
  302. extern esp_flash_t *esp_flash_default_chip;
  303. /*******************************************************************************
  304. * Utility Functions
  305. ******************************************************************************/
  306. /**
  307. * @brief Returns true if chip is configured for Quad I/O or Quad Fast Read.
  308. *
  309. * @param chip Pointer to SPI flash chip to use. If NULL, esp_flash_default_chip is substituted.
  310. *
  311. * @return true if flash works in quad mode, otherwise false
  312. */
  313. static inline bool esp_flash_is_quad_mode(const esp_flash_t *chip)
  314. {
  315. return (chip->read_mode == SPI_FLASH_QIO) || (chip->read_mode == SPI_FLASH_QOUT);
  316. }
  317. #ifdef __cplusplus
  318. }
  319. #endif