spi_flash.h 17 KB

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