esp_rom_spiflash.h 13 KB

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