spi_flash.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. // Copyright 2020 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. #ifndef _ROM_SPI_FLASH_H_
  15. #define _ROM_SPI_FLASH_H_
  16. #include <stdint.h>
  17. #include <stdbool.h>
  18. #include "esp_attr.h"
  19. #ifdef __cplusplus
  20. extern "C" {
  21. #endif
  22. /** \defgroup spi_flash_apis, spi flash operation related apis
  23. * @brief spi_flash apis
  24. */
  25. /** @addtogroup spi_flash_apis
  26. * @{
  27. */
  28. #define PERIPHS_SPI_FLASH_CMD SPI_MEM_CMD_REG(1)
  29. #define PERIPHS_SPI_FLASH_ADDR SPI_MEM_ADDR_REG(1)
  30. #define PERIPHS_SPI_FLASH_CTRL SPI_MEM_CTRL_REG(1)
  31. #define PERIPHS_SPI_FLASH_CTRL1 SPI_MEM_CTRL1_REG(1)
  32. #define PERIPHS_SPI_FLASH_STATUS SPI_MEM_RD_STATUS_REG(1)
  33. #define PERIPHS_SPI_FLASH_USRREG SPI_MEM_USER_REG(1)
  34. #define PERIPHS_SPI_FLASH_USRREG1 SPI_MEM_USER1_REG(1)
  35. #define PERIPHS_SPI_FLASH_USRREG2 SPI_MEM_USER2_REG(1)
  36. #define PERIPHS_SPI_FLASH_C0 SPI_MEM_W0_REG(1)
  37. #define PERIPHS_SPI_FLASH_C1 SPI_MEM_W1_REG(1)
  38. #define PERIPHS_SPI_FLASH_C2 SPI_MEM_W2_REG(1)
  39. #define PERIPHS_SPI_FLASH_C3 SPI_MEM_W3_REG(1)
  40. #define PERIPHS_SPI_FLASH_C4 SPI_MEM_W4_REG(1)
  41. #define PERIPHS_SPI_FLASH_C5 SPI_MEM_W5_REG(1)
  42. #define PERIPHS_SPI_FLASH_C6 SPI_MEM_W6_REG(1)
  43. #define PERIPHS_SPI_FLASH_C7 SPI_MEM_W7_REG(1)
  44. #define PERIPHS_SPI_FLASH_TX_CRC SPI_MEM_TX_CRC_REG(1)
  45. #define SPI0_R_QIO_DUMMY_CYCLELEN 5
  46. #define SPI0_R_QIO_ADDR_BITSLEN 23
  47. #define SPI0_R_FAST_DUMMY_CYCLELEN 7
  48. #define SPI0_R_DIO_DUMMY_CYCLELEN 3
  49. #define SPI0_R_FAST_ADDR_BITSLEN 23
  50. #define SPI0_R_SIO_ADDR_BITSLEN 23
  51. #define SPI1_R_QIO_DUMMY_CYCLELEN 5
  52. #define SPI1_R_QIO_ADDR_BITSLEN 23
  53. #define SPI1_R_FAST_DUMMY_CYCLELEN 7
  54. #define SPI1_R_DIO_DUMMY_CYCLELEN 3
  55. #define SPI1_R_DIO_ADDR_BITSLEN 23
  56. #define SPI1_R_FAST_ADDR_BITSLEN 23
  57. #define SPI1_R_SIO_ADDR_BITSLEN 23
  58. #define ESP_ROM_SPIFLASH_W_SIO_ADDR_BITSLEN 23
  59. #define ESP_ROM_SPIFLASH_TWO_BYTE_STATUS_EN SPI_MEM_WRSR_2B
  60. //SPI address register
  61. #define ESP_ROM_SPIFLASH_BYTES_LEN 24
  62. #define ESP_ROM_SPIFLASH_BUFF_BYTE_WRITE_NUM 32
  63. #define ESP_ROM_SPIFLASH_BUFF_BYTE_READ_NUM 16
  64. #define ESP_ROM_SPIFLASH_BUFF_BYTE_READ_BITS 0xf
  65. //SPI status register
  66. #define ESP_ROM_SPIFLASH_BUSY_FLAG BIT0
  67. #define ESP_ROM_SPIFLASH_WRENABLE_FLAG BIT1
  68. #define ESP_ROM_SPIFLASH_BP0 BIT2
  69. #define ESP_ROM_SPIFLASH_BP1 BIT3
  70. #define ESP_ROM_SPIFLASH_BP2 BIT4
  71. #define ESP_ROM_SPIFLASH_WR_PROTECT (ESP_ROM_SPIFLASH_BP0|ESP_ROM_SPIFLASH_BP1|ESP_ROM_SPIFLASH_BP2)
  72. #define ESP_ROM_SPIFLASH_QE BIT9
  73. #define FLASH_ID_GD25LQ32C 0xC86016
  74. typedef enum {
  75. ESP_ROM_SPIFLASH_QIO_MODE = 0,
  76. ESP_ROM_SPIFLASH_QOUT_MODE,
  77. ESP_ROM_SPIFLASH_DIO_MODE,
  78. ESP_ROM_SPIFLASH_DOUT_MODE,
  79. ESP_ROM_SPIFLASH_FASTRD_MODE,
  80. ESP_ROM_SPIFLASH_SLOWRD_MODE
  81. } esp_rom_spiflash_read_mode_t;
  82. typedef enum {
  83. ESP_ROM_SPIFLASH_RESULT_OK,
  84. ESP_ROM_SPIFLASH_RESULT_ERR,
  85. ESP_ROM_SPIFLASH_RESULT_TIMEOUT
  86. } esp_rom_spiflash_result_t;
  87. typedef struct {
  88. uint32_t device_id;
  89. uint32_t chip_size; // chip size in bytes
  90. uint32_t block_size;
  91. uint32_t sector_size;
  92. uint32_t page_size;
  93. uint32_t status_mask;
  94. } esp_rom_spiflash_chip_t;
  95. typedef struct {
  96. uint8_t data_length;
  97. uint8_t read_cmd0;
  98. uint8_t read_cmd1;
  99. uint8_t write_cmd;
  100. uint16_t data_mask;
  101. uint16_t data;
  102. } esp_rom_spiflash_common_cmd_t;
  103. /**
  104. * @brief Fix the bug in SPI hardware communication with Flash/Ext-SRAM in High Speed.
  105. * Please do not call this function in SDK.
  106. *
  107. * @param uint8_t spi: 0 for SPI0(Cache Access), 1 for SPI1(Flash read/write).
  108. *
  109. * @param uint8_t freqdiv: Pll is 80M, 4 for 20M, 3 for 26.7M, 2 for 40M, 1 for 80M.
  110. *
  111. * @return None
  112. */
  113. void esp_rom_spiflash_fix_dummylen(uint8_t spi, uint8_t freqdiv);
  114. /**
  115. * @brief Select SPI Flash to QIO mode when WP pad is read from Flash.
  116. * Please do not call this function in SDK.
  117. *
  118. * @param uint8_t wp_gpio_num: WP gpio number.
  119. *
  120. * @param uint32_t ishspi: 0 for spi, 1 for hspi, flash pad decided by strapping
  121. * else, bit[5:0] spiclk, bit[11:6] spiq, bit[17:12] spid, bit[23:18] spics0, bit[29:24] spihd
  122. *
  123. * @return None
  124. */
  125. void esp_rom_spiflash_select_qiomode(uint8_t wp_gpio_num, uint32_t ishspi);
  126. /**
  127. * @brief Set SPI Flash pad drivers.
  128. * Please do not call this function in SDK.
  129. *
  130. * @param uint8_t wp_gpio_num: WP gpio number.
  131. *
  132. * @param uint32_t ishspi: 0 for spi, 1 for hspi, flash pad decided by strapping
  133. * else, bit[5:0] spiclk, bit[11:6] spiq, bit[17:12] spid, bit[23:18] spics0, bit[29:24] spihd
  134. *
  135. * @param uint8_t *drvs: drvs[0]-bit[3:0] for cpiclk, bit[7:4] for spiq, drvs[1]-bit[3:0] for spid, drvs[1]-bit[7:4] for spid
  136. * drvs[2]-bit[3:0] for spihd, drvs[2]-bit[7:4] for spiwp.
  137. * Values usually read from falsh by rom code, function usually callde by rom code.
  138. * if value with bit(3) set, the value is valid, bit[2:0] is the real value.
  139. *
  140. * @return None
  141. */
  142. void esp_rom_spiflash_set_drvs(uint8_t wp_gpio_num, uint32_t ishspi, uint8_t *drvs);
  143. /**
  144. * @brief Select SPI Flash function for pads.
  145. * Please do not call this function in SDK.
  146. *
  147. * @param uint32_t ishspi: 0 for spi, 1 for hspi, flash pad decided by strapping
  148. * else, bit[5:0] spiclk, bit[11:6] spiq, bit[17:12] spid, bit[23:18] spics0, bit[29:24] spihd
  149. *
  150. * @return None
  151. */
  152. void esp_rom_spiflash_select_padsfunc(uint32_t ishspi);
  153. /**
  154. * @brief SPI Flash init, clock divisor is 4, use 1 line Slow read mode.
  155. * Please do not call this function in SDK.
  156. *
  157. * @param uint32_t ishspi: 0 for spi, 1 for hspi, flash pad decided by strapping
  158. * else, bit[5:0] spiclk, bit[11:6] spiq, bit[17:12] spid, bit[23:18] spics0, bit[29:24] spihd
  159. *
  160. * @param uint8_t legacy: In legacy mode, more SPI command is used in line.
  161. *
  162. * @return None
  163. */
  164. void esp_rom_spiflash_attach(uint32_t ishspi, bool legacy);
  165. /**
  166. * @brief SPI Read Flash status register. We use CMD 0x05 (RDSR).
  167. * Please do not call this function in SDK.
  168. *
  169. * @param esp_rom_spiflash_chip_t *spi : The information for Flash, which is exported from ld file.
  170. *
  171. * @param uint32_t *status : The pointer to which to return the Flash status value.
  172. *
  173. * @return ESP_ROM_SPIFLASH_RESULT_OK : read OK.
  174. * ESP_ROM_SPIFLASH_RESULT_ERR : read error.
  175. * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : read timeout.
  176. */
  177. esp_rom_spiflash_result_t esp_rom_spiflash_read_status(esp_rom_spiflash_chip_t *spi, uint32_t *status);
  178. /**
  179. * @brief SPI Read Flash status register bits 8-15. We use CMD 0x35 (RDSR2).
  180. * Please do not call this function in SDK.
  181. *
  182. * @param esp_rom_spiflash_chip_t *spi : The information for Flash, which is exported from ld file.
  183. *
  184. * @param uint32_t *status : The pointer to which to return the Flash status value.
  185. *
  186. * @return ESP_ROM_SPIFLASH_RESULT_OK : read OK.
  187. * ESP_ROM_SPIFLASH_RESULT_ERR : read error.
  188. * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : read timeout.
  189. */
  190. esp_rom_spiflash_result_t esp_rom_spiflash_read_statushigh(esp_rom_spiflash_chip_t *spi, uint32_t *status);
  191. /**
  192. * @brief Write status to Falsh status register.
  193. * Please do not call this function in SDK.
  194. *
  195. * @param esp_rom_spiflash_chip_t *spi : The information for Flash, which is exported from ld file.
  196. *
  197. * @param uint32_t status_value : Value to .
  198. *
  199. * @return ESP_ROM_SPIFLASH_RESULT_OK : write OK.
  200. * ESP_ROM_SPIFLASH_RESULT_ERR : write error.
  201. * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : write timeout.
  202. */
  203. esp_rom_spiflash_result_t esp_rom_spiflash_write_status(esp_rom_spiflash_chip_t *spi, uint32_t status_value);
  204. /**
  205. * @brief Use a command to Read Flash status register.
  206. * Please do not call this function in SDK.
  207. *
  208. * @param esp_rom_spiflash_chip_t *spi : The information for Flash, which is exported from ld file.
  209. *
  210. * @param uint32_t*status : The pointer to which to return the Flash status value.
  211. *
  212. * @return ESP_ROM_SPIFLASH_RESULT_OK : read OK.
  213. * ESP_ROM_SPIFLASH_RESULT_ERR : read error.
  214. * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : read timeout.
  215. */
  216. esp_rom_spiflash_result_t esp_rom_spiflash_read_user_cmd(uint32_t *status, uint8_t cmd);
  217. /**
  218. * @brief Config SPI Flash read mode when init.
  219. * Please do not call this function in SDK.
  220. *
  221. * @param esp_rom_spiflash_read_mode_t mode : QIO/QOUT/DIO/DOUT/FastRD/SlowRD.
  222. *
  223. * This function does not try to set the QIO Enable bit in the status register, caller is responsible for this.
  224. *
  225. * @return ESP_ROM_SPIFLASH_RESULT_OK : config OK.
  226. * ESP_ROM_SPIFLASH_RESULT_ERR : config error.
  227. * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : config timeout.
  228. */
  229. esp_rom_spiflash_result_t esp_rom_spiflash_config_readmode(esp_rom_spiflash_read_mode_t mode);
  230. /**
  231. * @brief Config SPI Flash clock divisor.
  232. * Please do not call this function in SDK.
  233. *
  234. * @param uint8_t freqdiv: clock divisor.
  235. *
  236. * @param uint8_t spi: 0 for SPI0, 1 for SPI1.
  237. *
  238. * @return ESP_ROM_SPIFLASH_RESULT_OK : config OK.
  239. * ESP_ROM_SPIFLASH_RESULT_ERR : config error.
  240. * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : config timeout.
  241. */
  242. esp_rom_spiflash_result_t esp_rom_spiflash_config_clk(uint8_t freqdiv, uint8_t spi);
  243. /**
  244. * @brief Send CommonCmd to Flash so that is can go into QIO mode, some Flash use different CMD.
  245. * Please do not call this function in SDK.
  246. *
  247. * @param esp_rom_spiflash_common_cmd_t *cmd : A struct to show the action of a command.
  248. *
  249. * @return uint16_t 0 : do not send command any more.
  250. * 1 : go to the next command.
  251. * n > 1 : skip (n - 1) commands.
  252. */
  253. uint16_t esp_rom_spiflash_common_cmd(esp_rom_spiflash_common_cmd_t *cmd);
  254. /**
  255. * @brief Unlock SPI write protect.
  256. * Please do not call this function in SDK.
  257. *
  258. * @param None.
  259. *
  260. * @return ESP_ROM_SPIFLASH_RESULT_OK : Unlock OK.
  261. * ESP_ROM_SPIFLASH_RESULT_ERR : Unlock error.
  262. * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Unlock timeout.
  263. */
  264. esp_rom_spiflash_result_t esp_rom_spiflash_unlock(void);
  265. /**
  266. * @brief SPI write protect.
  267. * Please do not call this function in SDK.
  268. *
  269. * @param None.
  270. *
  271. * @return ESP_ROM_SPIFLASH_RESULT_OK : Lock OK.
  272. * ESP_ROM_SPIFLASH_RESULT_ERR : Lock error.
  273. * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Lock timeout.
  274. */
  275. esp_rom_spiflash_result_t esp_rom_spiflash_lock(void);
  276. /**
  277. * @brief Update SPI Flash parameter.
  278. * Please do not call this function in SDK.
  279. *
  280. * @param uint32_t deviceId : Device ID read from SPI, the low 32 bit.
  281. *
  282. * @param uint32_t chip_size : The Flash size.
  283. *
  284. * @param uint32_t block_size : The Flash block size.
  285. *
  286. * @param uint32_t sector_size : The Flash sector size.
  287. *
  288. * @param uint32_t page_size : The Flash page size.
  289. *
  290. * @param uint32_t status_mask : The Mask used when read status from Flash(use single CMD).
  291. *
  292. * @return ESP_ROM_SPIFLASH_RESULT_OK : Update OK.
  293. * ESP_ROM_SPIFLASH_RESULT_ERR : Update error.
  294. * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Update timeout.
  295. */
  296. esp_rom_spiflash_result_t esp_rom_spiflash_config_param(uint32_t deviceId, uint32_t chip_size, uint32_t block_size,
  297. uint32_t sector_size, uint32_t page_size, uint32_t status_mask);
  298. /**
  299. * @brief Erase whole flash chip.
  300. * Please do not call this function in SDK.
  301. *
  302. * @param None
  303. *
  304. * @return ESP_ROM_SPIFLASH_RESULT_OK : Erase OK.
  305. * ESP_ROM_SPIFLASH_RESULT_ERR : Erase error.
  306. * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Erase timeout.
  307. */
  308. esp_rom_spiflash_result_t esp_rom_spiflash_erase_chip(void);
  309. /**
  310. * @brief Erase a 64KB block of flash
  311. * Uses SPI flash command D8H.
  312. * Please do not call this function in SDK.
  313. *
  314. * @param uint32_t block_num : Which block to erase.
  315. *
  316. * @return ESP_ROM_SPIFLASH_RESULT_OK : Erase OK.
  317. * ESP_ROM_SPIFLASH_RESULT_ERR : Erase error.
  318. * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Erase timeout.
  319. */
  320. esp_rom_spiflash_result_t esp_rom_spiflash_erase_block(uint32_t block_num);
  321. /**
  322. * @brief Erase a sector of flash.
  323. * Uses SPI flash command 20H.
  324. * Please do not call this function in SDK.
  325. *
  326. * @param uint32_t sector_num : Which sector to erase.
  327. *
  328. * @return ESP_ROM_SPIFLASH_RESULT_OK : Erase OK.
  329. * ESP_ROM_SPIFLASH_RESULT_ERR : Erase error.
  330. * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Erase timeout.
  331. */
  332. esp_rom_spiflash_result_t esp_rom_spiflash_erase_sector(uint32_t sector_num);
  333. /**
  334. * @brief Erase some sectors.
  335. * Please do not call this function in SDK.
  336. *
  337. * @param uint32_t start_addr : Start addr to erase, should be sector aligned.
  338. *
  339. * @param uint32_t area_len : Length to erase, should be sector aligned.
  340. *
  341. * @return ESP_ROM_SPIFLASH_RESULT_OK : Erase OK.
  342. * ESP_ROM_SPIFLASH_RESULT_ERR : Erase error.
  343. * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Erase timeout.
  344. */
  345. esp_rom_spiflash_result_t esp_rom_spiflash_erase_area(uint32_t start_addr, uint32_t area_len);
  346. /**
  347. * @brief Write Data to Flash, you should Erase it yourself if need.
  348. * Please do not call this function in SDK.
  349. *
  350. * @param uint32_t dest_addr : Address to write, should be 4 bytes aligned.
  351. *
  352. * @param const uint32_t *src : The pointer to data which is to write.
  353. *
  354. * @param uint32_t len : Length to write, should be 4 bytes aligned.
  355. *
  356. * @return ESP_ROM_SPIFLASH_RESULT_OK : Write OK.
  357. * ESP_ROM_SPIFLASH_RESULT_ERR : Write error.
  358. * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Write timeout.
  359. */
  360. esp_rom_spiflash_result_t esp_rom_spiflash_write(uint32_t dest_addr, const uint32_t *src, int32_t len);
  361. /**
  362. * @brief Read Data from Flash, you should Erase it yourself if need.
  363. * Please do not call this function in SDK.
  364. *
  365. * @param uint32_t src_addr : Address to read, should be 4 bytes aligned.
  366. *
  367. * @param uint32_t *dest : The buf to read the data.
  368. *
  369. * @param uint32_t len : Length to read, should be 4 bytes aligned.
  370. *
  371. * @return ESP_ROM_SPIFLASH_RESULT_OK : Read OK.
  372. * ESP_ROM_SPIFLASH_RESULT_ERR : Read error.
  373. * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Read timeout.
  374. */
  375. esp_rom_spiflash_result_t esp_rom_spiflash_read(uint32_t src_addr, uint32_t *dest, int32_t len);
  376. /**
  377. * @brief SPI1 go into encrypto mode.
  378. * Please do not call this function in SDK.
  379. *
  380. * @param None
  381. *
  382. * @return None
  383. */
  384. void esp_rom_spiflash_write_encrypted_enable(void);
  385. /**
  386. * @brief Prepare 32 Bytes data to encrpto writing, you should Erase it yourself if need.
  387. * Please do not call this function in SDK.
  388. *
  389. * @param uint32_t flash_addr : Address to write, should be 32 bytes aligned.
  390. *
  391. * @param uint32_t *data : The pointer to data which is to write.
  392. *
  393. * @return ESP_ROM_SPIFLASH_RESULT_OK : Prepare OK.
  394. * ESP_ROM_SPIFLASH_RESULT_ERR : Prepare error.
  395. * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Prepare timeout.
  396. */
  397. esp_rom_spiflash_result_t esp_rom_spiflash_prepare_encrypted_data(uint32_t flash_addr, uint32_t *data);
  398. /**
  399. * @brief SPI1 go out of encrypto mode.
  400. * Please do not call this function in SDK.
  401. *
  402. * @param None
  403. *
  404. * @return None
  405. */
  406. void esp_rom_spiflash_write_encrypted_disable(void);
  407. /**
  408. * @brief Write data to flash with transparent encryption.
  409. * @note Sectors to be written should already be erased.
  410. *
  411. * @note Please do not call this function in SDK.
  412. *
  413. * @param uint32_t flash_addr : Address to write, should be 32 byte aligned.
  414. *
  415. * @param uint32_t *data : The pointer to data to write. Note, this pointer must
  416. * be 32 bit aligned and the content of the data will be
  417. * modified by the encryption function.
  418. *
  419. * @param uint32_t len : Length to write, should be 32 bytes aligned.
  420. *
  421. * @return ESP_ROM_SPIFLASH_RESULT_OK : Data written successfully.
  422. * ESP_ROM_SPIFLASH_RESULT_ERR : Encryption write error.
  423. * ESP_ROM_SPIFLASH_RESULT_TIMEOUT : Encrypto write timeout.
  424. */
  425. esp_rom_spiflash_result_t esp_rom_spiflash_write_encrypted(uint32_t flash_addr, uint32_t *data, uint32_t len);
  426. /* TODO: figure out how to map these to their new names */
  427. typedef enum {
  428. SPI_ENCRYPT_DESTINATION_FLASH,
  429. } SpiEncryptDest;
  430. typedef esp_rom_spiflash_result_t SpiFlashOpResult;
  431. SpiFlashOpResult SPI_Encrypt_Write(uint32_t flash_addr, const void *data, uint32_t len);
  432. SpiFlashOpResult SPI_Encrypt_Write_Dest(SpiEncryptDest dest, uint32_t flash_addr, const void *data, uint32_t len);
  433. void SPI_Write_Encrypt_Enable(void);
  434. void SPI_Write_Encrypt_Disable(void);
  435. /** @brief Wait until SPI flash write operation is complete
  436. *
  437. * @note Please do not call this function in SDK.
  438. *
  439. * Reads the Write In Progress bit of the SPI flash status register,
  440. * repeats until this bit is zero (indicating write complete).
  441. *
  442. * @return ESP_ROM_SPIFLASH_RESULT_OK : Write is complete
  443. * ESP_ROM_SPIFLASH_RESULT_ERR : Error while reading status.
  444. */
  445. esp_rom_spiflash_result_t esp_rom_spiflash_wait_idle(esp_rom_spiflash_chip_t *spi);
  446. /** @brief Enable Quad I/O pin functions
  447. *
  448. * @note Please do not call this function in SDK.
  449. *
  450. * Sets the HD & WP pin functions for Quad I/O modes, based on the
  451. * efuse SPI pin configuration.
  452. *
  453. * @param wp_gpio_num - Number of the WP pin to reconfigure for quad I/O.
  454. *
  455. * @param spiconfig - Pin configuration, as returned from ets_efuse_get_spiconfig().
  456. * - If this parameter is 0, default SPI pins are used and wp_gpio_num parameter is ignored.
  457. * - If this parameter is 1, default HSPI pins are used and wp_gpio_num parameter is ignored.
  458. * - For other values, this parameter encodes the HD pin number and also the CLK pin number. CLK pin selection is used
  459. * to determine if HSPI or SPI peripheral will be used (use HSPI if CLK pin is the HSPI clock pin, otherwise use SPI).
  460. * Both HD & WP pins are configured via GPIO matrix to map to the selected peripheral.
  461. */
  462. void esp_rom_spiflash_select_qio_pins(uint8_t wp_gpio_num, uint32_t spiconfig);
  463. typedef void (* spi_flash_func_t)(void);
  464. typedef SpiFlashOpResult (* spi_flash_op_t)(void);
  465. typedef SpiFlashOpResult (* spi_flash_erase_t)(uint32_t);
  466. typedef SpiFlashOpResult (* spi_flash_rd_t)(uint32_t, uint32_t*, int);
  467. typedef SpiFlashOpResult (* spi_flash_wr_t)(uint32_t, const uint32_t*, int);
  468. typedef SpiFlashOpResult (* spi_flash_ewr_t)(uint32_t, const void*, uint32_t);
  469. typedef SpiFlashOpResult (* spi_flash_wren_t)(void*);
  470. typedef struct {
  471. uint32_t read_sub_len;
  472. uint32_t write_sub_len;
  473. spi_flash_op_t unlock;
  474. spi_flash_erase_t erase_sector;
  475. spi_flash_erase_t erase_block;
  476. spi_flash_rd_t read;
  477. spi_flash_wr_t write;
  478. spi_flash_ewr_t encrypt_write;
  479. spi_flash_func_t check_sus;
  480. spi_flash_wren_t wren;
  481. spi_flash_op_t wait_idle;
  482. } spiflash_legacy_funcs_t;
  483. extern const spiflash_legacy_funcs_t *rom_spiflash_legacy_funcs;
  484. /** @brief Global ROM spiflash data, as used by legacy
  485. SPI flash functions
  486. */
  487. typedef struct {
  488. esp_rom_spiflash_chip_t chip;
  489. uint8_t dummy_len_plus[3];
  490. uint8_t sig_matrix;
  491. } spiflash_legacy_data_t;
  492. extern spiflash_legacy_data_t *rom_spiflash_legacy_data;
  493. /* Defines to make the C3 ROM legacvy data access compatible with previous chips */
  494. #define g_rom_flashchip (rom_spiflash_legacy_data->chip)
  495. #define g_rom_spiflash_dummy_len_plus (rom_spiflash_legacy_data->dummy_len_plus)
  496. /**
  497. * @brief Clear WEL bit unconditionally.
  498. *
  499. * @return always ESP_ROM_SPIFLASH_RESULT_OK
  500. */
  501. esp_rom_spiflash_result_t esp_rom_spiflash_write_disable(void);
  502. /**
  503. * @}
  504. */
  505. #ifdef __cplusplus
  506. }
  507. #endif
  508. #endif /* _ROM_SPI_FLASH_H_ */