spi_flash.h 17 KB

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