esp_flash.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. /** OS-level integration hooks for accessing flash chips inside a running OS */
  31. typedef struct {
  32. /**
  33. * Called before commencing any flash operation. Does not need to be
  34. * recursive (ie is called at most once for each call to 'end').
  35. */
  36. esp_err_t (*start)(void *arg);
  37. /** Called after completing any flash operation. */
  38. esp_err_t (*end)(void *arg);
  39. /** Called before any erase/write operations to check whether the region is limited by the OS */
  40. esp_err_t (*region_protected)(void* arg, size_t start_addr, size_t size);
  41. /** Delay for at least 'ms' milliseconds. Called in between 'start' and 'end'. */
  42. esp_err_t (*delay_ms)(void *arg, unsigned ms);
  43. } esp_flash_os_functions_t;
  44. /** @brief Structure to describe a SPI flash chip connected to the system.
  45. Structure must be initialized before use (passed to esp_flash_init()).
  46. */
  47. struct esp_flash_t {
  48. spi_flash_host_driver_t *host; ///< Pointer to hardware-specific "host_driver" structure. Must be initialized before used.
  49. const spi_flash_chip_t *chip_drv; ///< Pointer to chip-model-specific "adapter" structure. If NULL, will be detected during initialisation.
  50. 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.
  51. void *os_func_data; ///< Pointer to argument for os-specific hooks. Left NULL and will be initialized with ``os_func``.
  52. esp_flash_io_mode_t read_mode; ///< Configured SPI flash read mode. Set before ``esp_flash_init`` is called.
  53. uint32_t size; ///< Size of SPI flash in bytes. If 0, size will be detected during initialisation.
  54. uint32_t chip_id; ///< Detected chip id.
  55. };
  56. /** @brief Initialise SPI flash chip interface.
  57. *
  58. * This function must be called before any other API functions are called for this chip.
  59. *
  60. * @note Only the ``host`` and ``read_mode`` fields of the chip structure must
  61. * be initialised before this function is called. Other fields may be
  62. * auto-detected if left set to zero or NULL.
  63. *
  64. * @note If the chip->drv pointer is NULL, chip chip_drv will be auto-detected
  65. * based on its manufacturer & product IDs. See
  66. * ``esp_flash_registered_flash_drivers`` pointer for details of this process.
  67. *
  68. * @param chip Pointer to SPI flash chip to use. If NULL, esp_flash_default_chip is substituted.
  69. * @return ESP_OK on success, or a flash error code if initialisation fails.
  70. */
  71. esp_err_t esp_flash_init(esp_flash_t *chip);
  72. /**
  73. * Check if appropriate chip driver is set.
  74. *
  75. * @param chip Pointer to SPI flash chip to use. If NULL, esp_flash_default_chip is substituted.
  76. *
  77. * @return true if set, otherwise false.
  78. */
  79. bool esp_flash_chip_driver_initialized(const esp_flash_t *chip);
  80. /** @brief Read flash ID via the common "RDID" SPI flash command.
  81. *
  82. * @param chip Pointer to identify flash chip. Must have been successfully initialised via esp_flash_init()
  83. * @param[out] out_id Pointer to receive ID value.
  84. *
  85. * ID is a 24-bit value. Lower 16 bits of 'id' are the chip ID, upper 8 bits are the manufacturer ID.
  86. *
  87. * @return ESP_OK on success, or a flash error code if operation failed.
  88. */
  89. esp_err_t esp_flash_read_id(esp_flash_t *chip, uint32_t *out_id);
  90. /** @brief Detect flash size based on flash ID.
  91. *
  92. * @param chip Pointer to identify flash chip. Must have been successfully initialised via esp_flash_init()
  93. * @param[out] out_size Detected size in bytes.
  94. *
  95. * @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
  96. * the manufacturer doesn't follow this convention, the size may be incorrectly detected.
  97. *
  98. * @return ESP_OK on success, or a flash error code if operation failed.
  99. */
  100. esp_err_t esp_flash_get_size(esp_flash_t *chip, uint32_t *out_size);
  101. /** @brief Erase flash chip contents
  102. *
  103. * @param chip Pointer to identify flash chip. Must have been successfully initialised via esp_flash_init()
  104. *
  105. *
  106. * @return ESP_OK on success, or a flash error code if operation failed.
  107. */
  108. esp_err_t esp_flash_erase_chip(esp_flash_t *chip);
  109. /** @brief Erase a region of the flash chip
  110. *
  111. * @param chip Pointer to identify flash chip. Must have been successfully initialised via esp_flash_init()
  112. * @param start Address to start erasing flash. Must be sector aligned.
  113. * @param len Length of region to erase. Must also be sector aligned.
  114. *
  115. * Sector size is specifyed in chip->drv->sector_size field (typically 4096 bytes.) ESP_ERR_INVALID_ARG will be
  116. * returned if the start & length are not a multiple of this size.
  117. *
  118. * Erase is performed using block (multi-sector) erases where possible (block size is specified in
  119. * chip->drv->block_erase_size field, typically 65536 bytes). Remaining sectors are erased using individual sector erase
  120. * commands.
  121. *
  122. * @return ESP_OK on success, or a flash error code if operation failed.
  123. */
  124. esp_err_t esp_flash_erase_region(esp_flash_t *chip, uint32_t start, uint32_t len);
  125. /** @brief Read if the entire chip is write protected
  126. *
  127. * @param chip Pointer to identify flash chip. Must have been successfully initialised via esp_flash_init()
  128. * @param[out] write_protected Pointer to boolean, set to the value of the write protect flag.
  129. *
  130. * @note A correct result for this flag depends on the SPI flash chip model and chip_drv in use (via the 'chip->drv'
  131. * field).
  132. *
  133. * @return ESP_OK on success, or a flash error code if operation failed.
  134. */
  135. esp_err_t esp_flash_get_chip_write_protect(esp_flash_t *chip, bool *write_protected);
  136. /** @brief Set write protection for the SPI flash chip
  137. *
  138. * @param chip Pointer to identify flash chip. Must have been successfully initialised via esp_flash_init()
  139. * @param write_protect Boolean value for the write protect flag
  140. *
  141. * @note Correct behaviour of this function depends on the SPI flash chip model and chip_drv in use (via the 'chip->drv'
  142. * field).
  143. *
  144. * Some SPI flash chips may require a power cycle before write protect status can be cleared. Otherwise,
  145. * write protection can be removed via a follow-up call to this function.
  146. *
  147. * @return ESP_OK on success, or a flash error code if operation failed.
  148. */
  149. esp_err_t esp_flash_set_chip_write_protect(esp_flash_t *chip, bool write_protect);
  150. /** @brief Read the list of individually protectable regions of this SPI flash chip.
  151. *
  152. * @param chip Pointer to identify flash chip. Must have been successfully initialised via esp_flash_init()
  153. * @param[out] out_regions Pointer to receive a pointer to the array of protectable regions of the chip.
  154. * @param[out] out_num_regions Pointer to an integer receiving the count of protectable regions in the array returned in 'regions'.
  155. *
  156. * @note Correct behaviour of this function depends on the SPI flash chip model and chip_drv in use (via the 'chip->drv'
  157. * field).
  158. *
  159. * @return ESP_OK on success, or a flash error code if operation failed.
  160. */
  161. 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);
  162. /** @brief Detect if a region of the SPI flash chip is protected
  163. *
  164. * @param chip Pointer to identify flash chip. Must have been successfully initialised via esp_flash_init()
  165. * @param region Pointer to a struct describing a protected region. This must match one of the regions returned from esp_flash_get_protectable_regions(...).
  166. * @param[out] out_protected Pointer to a flag which is set based on the protected status for this region.
  167. *
  168. * @note It is possible for this result to be false and write operations to still fail, if protection is enabled for the entire chip.
  169. *
  170. * @note Correct behaviour of this function depends on the SPI flash chip model and chip_drv in use (via the 'chip->drv'
  171. * field).
  172. *
  173. * @return ESP_OK on success, or a flash error code if operation failed.
  174. */
  175. esp_err_t esp_flash_get_protected_region(esp_flash_t *chip, const esp_flash_region_t *region, bool *out_protected);
  176. /** @brief Update the protected status for a region of the SPI flash chip
  177. *
  178. * @param chip Pointer to identify flash chip. Must have been successfully initialised via esp_flash_init()
  179. * @param region Pointer to a struct describing a protected region. This must match one of the regions returned from esp_flash_get_protectable_regions(...).
  180. * @param protect Write protection flag to set.
  181. *
  182. * @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.
  183. *
  184. * @note Correct behaviour of this function depends on the SPI flash chip model and chip_drv in use (via the 'chip->drv'
  185. * field).
  186. *
  187. * @return ESP_OK on success, or a flash error code if operation failed.
  188. */
  189. esp_err_t esp_flash_set_protected_region(esp_flash_t *chip, const esp_flash_region_t *region, bool protect);
  190. /** @brief Read data from the SPI flash chip
  191. *
  192. * @param chip Pointer to identify flash chip. Must have been successfully initialised via esp_flash_init()
  193. * @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.
  194. * @param address Address on flash to read from. Must be less than chip->size field.
  195. * @param length Length (in bytes) of data to read.
  196. *
  197. * There are no alignment constraints on buffer, address or length.
  198. *
  199. * @note If on-chip flash encryption is used, this function returns raw (ie encrypted) data. Use the flash cache
  200. * to transparently decrypt data.
  201. *
  202. * @return
  203. * - ESP_OK: success
  204. * - ESP_ERR_NO_MEM: Buffer is in external PSRAM which cannot be concurrently accessed, and a temporary internal buffer could not be allocated.
  205. * - or a flash error code if operation failed.
  206. */
  207. esp_err_t esp_flash_read(esp_flash_t *chip, void *buffer, uint32_t address, uint32_t length);
  208. /** @brief Write data to the SPI flash chip
  209. *
  210. * @param chip Pointer to identify flash chip. Must have been successfully initialised via esp_flash_init()
  211. * @param address Address on flash to write to. Must be previously erased (SPI NOR flash can only write bits 1->0).
  212. * @param buffer Pointer to a buffer with the data to write. To get better performance, this should be in the DRAM and word aligned.
  213. * @param length Length (in bytes) of data to write.
  214. *
  215. * There are no alignment constraints on buffer, address or length.
  216. *
  217. * @return ESP_OK on success, or a flash error code if operation failed.
  218. */
  219. esp_err_t esp_flash_write(esp_flash_t *chip, const void *buffer, uint32_t address, uint32_t length);
  220. /** @brief Encrypted and write data to the SPI flash chip using on-chip hardware flash encryption
  221. *
  222. * @param chip Pointer to identify flash chip. Must be NULL (the main flash chip). For other chips, encrypted write is not supported.
  223. * @param address Address on flash to write to. 16 byte aligned. Must be previously erased (SPI NOR flash can only write bits 1->0).
  224. * @param buffer Pointer to a buffer with the data to write.
  225. * @param length Length (in bytes) of data to write. 16 byte aligned.
  226. *
  227. * @note Both address & length must be 16 byte aligned, as this is the encryption block size
  228. *
  229. * @return
  230. * - ESP_OK: on success
  231. * - ESP_ERR_NOT_SUPPORTED: encrypted write not supported for this chip.
  232. * - ESP_ERR_INVALID_ARG: Either the address, buffer or length is invalid.
  233. * - or other flash error code from spi_flash_write_encrypted().
  234. */
  235. esp_err_t esp_flash_write_encrypted(esp_flash_t *chip, uint32_t address, const void *buffer, uint32_t length);
  236. /** @brief Read and decrypt data from the SPI flash chip using on-chip hardware flash encryption
  237. *
  238. * @param chip Pointer to identify flash chip. Must be NULL (the main flash chip). For other chips, encrypted read is not supported.
  239. * @param address Address on flash to read from.
  240. * @param out_buffer Pointer to a buffer for the data to read to.
  241. * @param length Length (in bytes) of data to read.
  242. *
  243. * @return
  244. * - ESP_OK: on success
  245. * - ESP_ERR_NOT_SUPPORTED: encrypted read not supported for this chip.
  246. * - or other flash error code from spi_flash_read_encrypted().
  247. */
  248. esp_err_t esp_flash_read_encrypted(esp_flash_t *chip, uint32_t address, void *out_buffer, uint32_t length);
  249. /** @brief Pointer to the "default" SPI flash chip, ie the main chip attached to the MCU.
  250. This chip is used if the 'chip' argument pass to esp_flash_xxx API functions is ever NULL.
  251. */
  252. extern esp_flash_t *esp_flash_default_chip;
  253. /*******************************************************************************
  254. * Utility Functions
  255. ******************************************************************************/
  256. /**
  257. * @brief Returns true if chip is configured for Quad I/O or Quad Fast Read.
  258. *
  259. * @param chip Pointer to SPI flash chip to use. If NULL, esp_flash_default_chip is substituted.
  260. *
  261. * @return true if flash works in quad mode, otherwise false
  262. */
  263. static inline bool esp_flash_is_quad_mode(const esp_flash_t *chip)
  264. {
  265. return (chip->read_mode == SPI_FLASH_QIO) || (chip->read_mode == SPI_FLASH_QOUT);
  266. }
  267. #ifdef __cplusplus
  268. }
  269. #endif