spi_flash.h 17 KB

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