esp_rom_spiflash.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /*
  2. * SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #include "sdkconfig.h"
  8. #include <stdint.h>
  9. #include <stdbool.h>
  10. #include "esp_rom_spiflash_defs.h"
  11. #ifdef __cplusplus
  12. extern "C" {
  13. #endif
  14. typedef enum {
  15. ESP_ROM_SPIFLASH_QIO_MODE = 0,
  16. ESP_ROM_SPIFLASH_QOUT_MODE,
  17. ESP_ROM_SPIFLASH_DIO_MODE,
  18. ESP_ROM_SPIFLASH_DOUT_MODE,
  19. ESP_ROM_SPIFLASH_FASTRD_MODE,
  20. ESP_ROM_SPIFLASH_SLOWRD_MODE,
  21. ESP_ROM_SPIFLASH_OPI_STR_MODE,
  22. ESP_ROM_SPIFLASH_OPI_DTR_MODE,
  23. ESP_ROM_SPIFLASH_OOUT_MODE,
  24. ESP_ROM_SPIFLASH_OIO_STR_MODE,
  25. ESP_ROM_SPIFLASH_OIO_DTR_MODE,
  26. ESP_ROM_SPIFLASH_QPI_MODE,
  27. ESP_ROM_SPIFLASH_OPI_HEX_DTR_MODE,
  28. } esp_rom_spiflash_read_mode_t;
  29. typedef struct {
  30. uint32_t device_id;
  31. uint32_t chip_size; // chip size in bytes
  32. uint32_t block_size;
  33. uint32_t sector_size;
  34. uint32_t page_size;
  35. uint32_t status_mask;
  36. } esp_rom_spiflash_chip_t;
  37. typedef enum {
  38. ESP_ROM_SPIFLASH_RESULT_OK,
  39. ESP_ROM_SPIFLASH_RESULT_ERR,
  40. ESP_ROM_SPIFLASH_RESULT_TIMEOUT
  41. } esp_rom_spiflash_result_t;
  42. /**
  43. * @brief SPI Flash init, clock divisor is 4, use 1 line Slow read mode.
  44. * Please do not call this function in SDK.
  45. *
  46. * @param uint32_t ishspi: 0 for spi, 1 for hspi, flash pad decided by strapping
  47. * else, bit[5:0] spiclk, bit[11:6] spiq, bit[17:12] spid, bit[23:18] spics0, bit[29:24] spihd
  48. *
  49. * @param uint8_t legacy: always keeping false.
  50. *
  51. * @return None
  52. */
  53. void esp_rom_spiflash_attach(uint32_t ishspi, bool legacy);
  54. /**
  55. * @brief SPI Read Flash status register. We use CMD 0x05 (RDSR).
  56. * Please do not call this function in SDK.
  57. *
  58. * @param esp_rom_spiflash_chip_t *spi : The information for Flash, which is exported from ld file.
  59. *
  60. * @param uint32_t *status : The pointer to which to return the Flash status value.
  61. *
  62. * @return ESP_ROM_SPIFLASH_RESULT_OK : read OK.
  63. * ESP_ROM_SPIFLASH_RESULT_ERR : read error.
  64. * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : read timeout.
  65. */
  66. esp_rom_spiflash_result_t esp_rom_spiflash_read_status(esp_rom_spiflash_chip_t *spi, uint32_t *status);
  67. /**
  68. * @brief SPI Read Flash status register bits 8-15. We use CMD 0x35 (RDSR2).
  69. * Please do not call this function in SDK.
  70. *
  71. * @param esp_rom_spiflash_chip_t *spi : The information for Flash, which is exported from ld file.
  72. *
  73. * @param uint32_t *status : The pointer to which to return the Flash status value.
  74. *
  75. * @return ESP_ROM_SPIFLASH_RESULT_OK : read OK.
  76. * ESP_ROM_SPIFLASH_RESULT_ERR : read error.
  77. * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : read timeout.
  78. */
  79. esp_rom_spiflash_result_t esp_rom_spiflash_read_statushigh(esp_rom_spiflash_chip_t *spi, uint32_t *status);
  80. /**
  81. * @brief Write status to Flash status register.
  82. * Please do not call this function in SDK.
  83. *
  84. * @param esp_rom_spiflash_chip_t *spi : The information for Flash, which is exported from ld file.
  85. *
  86. * @param uint32_t status_value : Value to .
  87. *
  88. * @return ESP_ROM_SPIFLASH_RESULT_OK : write OK.
  89. * ESP_ROM_SPIFLASH_RESULT_ERR : write error.
  90. * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : write timeout.
  91. */
  92. esp_rom_spiflash_result_t esp_rom_spiflash_write_status(esp_rom_spiflash_chip_t *spi, uint32_t status_value);
  93. /**
  94. * @brief Use a command to Read Flash status register.
  95. * Please do not call this function in SDK.
  96. *
  97. * @param esp_rom_spiflash_chip_t *spi : The information for Flash, which is exported from ld file.
  98. *
  99. * @param uint32_t*status : The pointer to which to return the Flash status value.
  100. *
  101. * @return ESP_ROM_SPIFLASH_RESULT_OK : read OK.
  102. * ESP_ROM_SPIFLASH_RESULT_ERR : read error.
  103. * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : read timeout.
  104. */
  105. esp_rom_spiflash_result_t esp_rom_spiflash_read_user_cmd(uint32_t *status, uint8_t cmd);
  106. /**
  107. * @brief Config SPI Flash read mode when init.
  108. * Please do not call this function in SDK.
  109. *
  110. * @param esp_rom_spiflash_read_mode_t mode : QIO/QOUT/DIO/DOUT/FastRD/SlowRD.
  111. *
  112. * This function does not try to set the QIO Enable bit in the status register, caller is responsible for this.
  113. *
  114. * @return ESP_ROM_SPIFLASH_RESULT_OK : config OK.
  115. * ESP_ROM_SPIFLASH_RESULT_ERR : config error.
  116. * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : config timeout.
  117. */
  118. esp_rom_spiflash_result_t esp_rom_spiflash_config_readmode(esp_rom_spiflash_read_mode_t mode);
  119. /**
  120. * @brief Config SPI Flash clock divisor.
  121. * Please do not call this function in SDK.
  122. *
  123. * @param uint8_t freqdiv: clock divisor.
  124. *
  125. * @param uint8_t spi: 0 for SPI0, 1 for SPI1.
  126. *
  127. * @return ESP_ROM_SPIFLASH_RESULT_OK : config OK.
  128. * ESP_ROM_SPIFLASH_RESULT_ERR : config error.
  129. * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : config timeout.
  130. */
  131. esp_rom_spiflash_result_t esp_rom_spiflash_config_clk(uint8_t freqdiv, uint8_t spi);
  132. /**
  133. * @brief Update SPI Flash parameter.
  134. * Please do not call this function in SDK.
  135. *
  136. * @param uint32_t deviceId : Device ID read from SPI, the low 32 bit.
  137. *
  138. * @param uint32_t chip_size : The Flash size.
  139. *
  140. * @param uint32_t block_size : The Flash block size.
  141. *
  142. * @param uint32_t sector_size : The Flash sector size.
  143. *
  144. * @param uint32_t page_size : The Flash page size.
  145. *
  146. * @param uint32_t status_mask : The Mask used when read status from Flash(use single CMD).
  147. *
  148. * @return ESP_ROM_SPIFLASH_RESULT_OK : Update OK.
  149. * ESP_ROM_SPIFLASH_RESULT_ERR : Update error.
  150. * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Update timeout.
  151. */
  152. esp_rom_spiflash_result_t esp_rom_spiflash_config_param(uint32_t deviceId, uint32_t chip_size, uint32_t block_size,
  153. uint32_t sector_size, uint32_t page_size, uint32_t status_mask);
  154. /**
  155. * @brief Erase whole flash chip.
  156. * Please do not call this function in SDK.
  157. *
  158. * @param None
  159. *
  160. * @return ESP_ROM_SPIFLASH_RESULT_OK : Erase OK.
  161. * ESP_ROM_SPIFLASH_RESULT_ERR : Erase error.
  162. * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Erase timeout.
  163. */
  164. esp_rom_spiflash_result_t esp_rom_spiflash_erase_chip(void);
  165. /**
  166. * @brief Erase a 64KB block of flash
  167. * Uses SPI flash command D8H.
  168. * Please do not call this function in SDK.
  169. *
  170. * @param uint32_t block_num : Which block to erase.
  171. *
  172. * @return ESP_ROM_SPIFLASH_RESULT_OK : Erase OK.
  173. * ESP_ROM_SPIFLASH_RESULT_ERR : Erase error.
  174. * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Erase timeout.
  175. */
  176. esp_rom_spiflash_result_t esp_rom_spiflash_erase_block(uint32_t block_num);
  177. /**
  178. * @brief Erase a sector of flash.
  179. * Uses SPI flash command 20H.
  180. * Please do not call this function in SDK.
  181. *
  182. * @param uint32_t sector_num : Which sector to erase.
  183. *
  184. * @return ESP_ROM_SPIFLASH_RESULT_OK : Erase OK.
  185. * ESP_ROM_SPIFLASH_RESULT_ERR : Erase error.
  186. * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Erase timeout.
  187. */
  188. esp_rom_spiflash_result_t esp_rom_spiflash_erase_sector(uint32_t sector_num);
  189. /**
  190. * @brief Erase some sectors.
  191. * Please do not call this function in SDK.
  192. *
  193. * @param uint32_t start_addr : Start addr to erase, should be sector aligned.
  194. *
  195. * @param uint32_t area_len : Length to erase, should be sector aligned.
  196. *
  197. * @return ESP_ROM_SPIFLASH_RESULT_OK : Erase OK.
  198. * ESP_ROM_SPIFLASH_RESULT_ERR : Erase error.
  199. * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Erase timeout.
  200. */
  201. esp_rom_spiflash_result_t esp_rom_spiflash_erase_area(uint32_t start_addr, uint32_t area_len);
  202. /**
  203. * @brief Write Data to Flash, you should Erase it yourself if need.
  204. * Please do not call this function in SDK.
  205. *
  206. * @param uint32_t dest_addr : Address to write, should be 4 bytes aligned.
  207. *
  208. * @param const uint32_t *src : The pointer to data which is to write.
  209. *
  210. * @param uint32_t len : Length to write, should be 4 bytes aligned.
  211. *
  212. * @return ESP_ROM_SPIFLASH_RESULT_OK : Write OK.
  213. * ESP_ROM_SPIFLASH_RESULT_ERR : Write error.
  214. * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Write timeout.
  215. */
  216. esp_rom_spiflash_result_t esp_rom_spiflash_write(uint32_t dest_addr, const uint32_t *src, int32_t len);
  217. /**
  218. * @brief Read Data from Flash, you should Erase it yourself if need.
  219. * Please do not call this function in SDK.
  220. *
  221. * @param uint32_t src_addr : Address to read, should be 4 bytes aligned.
  222. *
  223. * @param uint32_t *dest : The buf to read the data.
  224. *
  225. * @param uint32_t len : Length to read, should be 4 bytes aligned.
  226. *
  227. * @return ESP_ROM_SPIFLASH_RESULT_OK : Read OK.
  228. * ESP_ROM_SPIFLASH_RESULT_ERR : Read error.
  229. * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Read timeout.
  230. */
  231. esp_rom_spiflash_result_t esp_rom_spiflash_read(uint32_t src_addr, uint32_t *dest, int32_t len);
  232. /**
  233. * @brief SPI1 go into encrypto mode.
  234. * Please do not call this function in SDK.
  235. *
  236. * @param None
  237. *
  238. * @return None
  239. */
  240. void esp_rom_spiflash_write_encrypted_enable(void);
  241. /**
  242. * @brief Prepare 32 Bytes data to encrpto writing, you should Erase it yourself if need.
  243. * Please do not call this function in SDK.
  244. *
  245. * @param uint32_t flash_addr : Address to write, should be 32 bytes aligned.
  246. *
  247. * @param uint32_t *data : The pointer to data which is to write.
  248. *
  249. * @return ESP_ROM_SPIFLASH_RESULT_OK : Prepare OK.
  250. * ESP_ROM_SPIFLASH_RESULT_ERR : Prepare error.
  251. * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Prepare timeout.
  252. */
  253. esp_rom_spiflash_result_t esp_rom_spiflash_prepare_encrypted_data(uint32_t flash_addr, uint32_t *data);
  254. /**
  255. * @brief SPI1 go out of encrypto mode.
  256. * Please do not call this function in SDK.
  257. *
  258. * @param None
  259. *
  260. * @return None
  261. */
  262. void esp_rom_spiflash_write_encrypted_disable(void);
  263. /**
  264. * @brief Write data to flash with transparent encryption.
  265. * @note Sectors to be written should already be erased.
  266. *
  267. * @note Please do not call this function in SDK.
  268. *
  269. * @param uint32_t flash_addr : Address to write, should be 32 byte aligned.
  270. *
  271. * @param uint32_t *data : The pointer to data to write. Note, this pointer must
  272. * be 32 bit aligned and the content of the data will be
  273. * modified by the encryption function.
  274. *
  275. * @param uint32_t len : Length to write, should be 32 bytes aligned.
  276. *
  277. * @return ESP_ROM_SPIFLASH_RESULT_OK : Data written successfully.
  278. * ESP_ROM_SPIFLASH_RESULT_ERR : Encryption write error.
  279. * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Encrypto write timeout.
  280. */
  281. esp_rom_spiflash_result_t esp_rom_spiflash_write_encrypted(uint32_t flash_addr, uint32_t *data, uint32_t len);
  282. /** @brief Wait until SPI flash write operation is complete
  283. *
  284. * @note Please do not call this function in SDK.
  285. *
  286. * Reads the Write In Progress bit of the SPI flash status register,
  287. * repeats until this bit is zero (indicating write complete).
  288. *
  289. * @return ESP_ROM_SPIFLASH_RESULT_OK : Write is complete
  290. * ESP_ROM_SPIFLASH_RESULT_ERR : Error while reading status.
  291. */
  292. esp_rom_spiflash_result_t esp_rom_spiflash_wait_idle(esp_rom_spiflash_chip_t *spi);
  293. /** @brief Enable Quad I/O pin functions
  294. *
  295. * @note Please do not call this function in SDK.
  296. *
  297. * Sets the HD & WP pin functions for Quad I/O modes, based on the
  298. * efuse SPI pin configuration.
  299. *
  300. * @param wp_gpio_num - Number of the WP pin to reconfigure for quad I/O.
  301. *
  302. * @param spiconfig - Pin configuration, as returned from ets_efuse_get_spiconfig().
  303. * - If this parameter is 0, default SPI pins are used and wp_gpio_num parameter is ignored.
  304. * - If this parameter is 1, default HSPI pins are used and wp_gpio_num parameter is ignored.
  305. * - For other values, this parameter encodes the HD pin number and also the CLK pin number. CLK pin selection is used
  306. * to determine if HSPI or SPI peripheral will be used (use HSPI if CLK pin is the HSPI clock pin, otherwise use SPI).
  307. * Both HD & WP pins are configured via GPIO matrix to map to the selected peripheral.
  308. */
  309. void esp_rom_spiflash_select_qio_pins(uint8_t wp_gpio_num, uint32_t spiconfig);
  310. /**
  311. * @brief Clear WEL bit unconditionally.
  312. *
  313. * @return always ESP_ROM_SPIFLASH_RESULT_OK
  314. */
  315. esp_rom_spiflash_result_t esp_rom_spiflash_write_disable(void);
  316. /**
  317. * @brief Set WREN bit.
  318. *
  319. * @param esp_rom_spiflash_chip_t *spi : The information for Flash, which is exported from ld file.
  320. *
  321. * @return always ESP_ROM_SPIFLASH_RESULT_OK
  322. */
  323. esp_rom_spiflash_result_t esp_rom_spiflash_write_enable(esp_rom_spiflash_chip_t *spi);
  324. /* Flash data defined in ROM*/
  325. #if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2
  326. extern esp_rom_spiflash_chip_t g_rom_flashchip;
  327. extern uint8_t g_rom_spiflash_dummy_len_plus[];
  328. #else
  329. typedef struct {
  330. esp_rom_spiflash_chip_t chip;
  331. uint8_t dummy_len_plus[3];
  332. uint8_t sig_matrix;
  333. } esp_rom_spiflash_legacy_data_t;
  334. extern esp_rom_spiflash_legacy_data_t *rom_spiflash_legacy_data;
  335. #define g_rom_flashchip (rom_spiflash_legacy_data->chip)
  336. #define g_rom_spiflash_dummy_len_plus (rom_spiflash_legacy_data->dummy_len_plus)
  337. #endif
  338. #ifdef __cplusplus
  339. }
  340. #endif