spi_flash.h 22 KB

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