spi_flash_chip_generic.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. // Copyright 2015-2019 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 "esp_flash.h"
  17. #include "spi_flash_chip_driver.h"
  18. /*
  19. * The 'chip_generic' SPI flash operations are a lowest common subset of SPI
  20. * flash commands, that work across most chips.
  21. *
  22. * These can be used as-is via the esp_flash_common_chip_driver chip_drv, or
  23. * they can be used as "base chip_drv" functions when creating a new
  24. * spi_flash_host_driver_t chip_drv structure.
  25. *
  26. * All of the functions in this header are internal functions, not part of a
  27. * public API. See esp_flash.h for the public API.
  28. */
  29. /**
  30. * @brief Generic probe function
  31. *
  32. * @param chip Pointer to SPI flash chip to use. If NULL, esp_flash_default_chip is substituted.
  33. * @param flash_id expected manufacture id.
  34. *
  35. * @return ESP_OK if the id read from chip->drv_read_id matches (always).
  36. */
  37. esp_err_t spi_flash_chip_generic_probe(esp_flash_t *chip, uint32_t flash_id);
  38. /**
  39. * @brief Generic reset function
  40. *
  41. * @param chip Pointer to SPI flash chip to use. If NULL, esp_flash_default_chip is substituted.
  42. *
  43. * @return ESP_OK if sending success, or error code passed from ``common_command`` or ``wait_idle`` functions of host driver.
  44. */
  45. esp_err_t spi_flash_chip_generic_reset(esp_flash_t *chip);
  46. /**
  47. * @brief Generic size detection function
  48. *
  49. * Tries to detect the size of chip by using the lower 4 bits of the chip->drv->read_id result = N, and assuming size is 2 ^ N.
  50. *
  51. * @param chip Pointer to SPI flash chip to use. If NULL, esp_flash_default_chip is substituted.
  52. * @param size Output of the detected size
  53. *
  54. * @return
  55. * - ESP_OK if success
  56. * - ESP_ERR_FLASH_UNSUPPORTED_CHIP if the manufacturer id is not correct, which may means an error in the reading
  57. * - or other error passed from the ``read_id`` function of host driver
  58. */
  59. esp_err_t spi_flash_chip_generic_detect_size(esp_flash_t *chip, uint32_t *size);
  60. /**
  61. * @brief Erase chip by using the generic erase chip command.
  62. *
  63. * @param chip Pointer to SPI flash chip to use. If NULL, esp_flash_default_chip is substituted.
  64. *
  65. * @return
  66. * - ESP_OK if success
  67. * - ESP_ERR_NOT_SUPPORTED if the chip is not able to perform the operation. This is indicated by WREN = 1 after the command is sent.
  68. * - or other error passed from the ``set_write_protect``, ``wait_idle`` or ``erase_chip`` function of host driver
  69. */
  70. esp_err_t spi_flash_chip_generic_erase_chip(esp_flash_t *chip);
  71. /**
  72. * @brief Erase sector by using the generic sector erase command.
  73. *
  74. * @param chip Pointer to SPI flash chip to use. If NULL, esp_flash_default_chip is substituted.
  75. * @param start_address Start address of the sector to erase
  76. *
  77. * @return
  78. * - ESP_OK if success
  79. * - ESP_ERR_NOT_SUPPORTED if the chip is not able to perform the operation. This is indicated by WREN = 1 after the command is sent.
  80. * - or other error passed from the ``set_write_protect``, ``wait_idle`` or ``erase_sector`` function of host driver
  81. */
  82. esp_err_t spi_flash_chip_generic_erase_sector(esp_flash_t *chip, uint32_t start_address);
  83. /**
  84. * @brief Erase block by the generic 64KB block erase command
  85. *
  86. * @param chip Pointer to SPI flash chip to use. If NULL, esp_flash_default_chip is substituted.
  87. * @param start_address Start address of the block to erase
  88. *
  89. * @return
  90. * - ESP_OK if success
  91. * - ESP_ERR_NOT_SUPPORTED if the chip is not able to perform the operation. This is indicated by WREN = 1 after the command is sent.
  92. * - or other error passed from the ``set_write_protect``, ``wait_idle`` or ``erase_block`` function of host driver
  93. */
  94. esp_err_t spi_flash_chip_generic_erase_block(esp_flash_t *chip, uint32_t start_address);
  95. /**
  96. * @brief Read from flash by using a read command that matches the programmed
  97. * read mode.
  98. *
  99. * @param chip Pointer to SPI flash chip to use. If NULL, esp_flash_default_chip is substituted.
  100. * @param buffer Buffer to hold the data read from flash
  101. * @param address Start address of the data on the flash
  102. * @param length Length to read
  103. *
  104. * @return always ESP_OK currently
  105. */
  106. esp_err_t spi_flash_chip_generic_read(esp_flash_t *chip, void *buffer, uint32_t address, uint32_t length);
  107. /**
  108. * @brief Perform a page program using the page program command.
  109. *
  110. * @note Length of each call should not excced the limitation in
  111. * ``chip->host->max_write_bytes``. This function is called in
  112. * ``spi_flash_chip_generic_write`` recursively until the whole page is
  113. * programmed. Strongly suggest to call ``spi_flash_chip_generic_write``
  114. * instead.
  115. *
  116. * @param chip Pointer to SPI flash chip to use. If NULL, esp_flash_default_chip is substituted.
  117. * @param buffer Buffer holding the data to program
  118. * @param address Start address to write to flash
  119. * @param length Length to write, no longer than ``chip->host->max_write_bytes``.
  120. *
  121. * @return
  122. * - ESP_OK if success
  123. * - ESP_ERR_NOT_SUPPORTED if the chip is not able to perform the operation. This is indicated by WREN = 1 after the command is sent.
  124. * - or other error passed from the ``wait_idle`` or ``program_page`` function of host driver
  125. */
  126. esp_err_t
  127. spi_flash_chip_generic_page_program(esp_flash_t *chip, const void *buffer, uint32_t address, uint32_t length);
  128. /**
  129. * @brief Perform a generic write. Split the write buffer into page program
  130. * operations, and call chip->chip_drv->page-program() for each.
  131. *
  132. * @param chip Pointer to SPI flash chip to use. If NULL, esp_flash_default_chip is substituted.
  133. * @param buffer Buffer holding the data to program
  134. * @param address Start address to write to flash
  135. * @param length Length to write
  136. *
  137. * @return
  138. * - ESP_OK if success
  139. * - or other error passed from the ``wait_idle`` or ``program_page`` function of host driver
  140. */
  141. esp_err_t spi_flash_chip_generic_write(esp_flash_t *chip, const void *buffer, uint32_t address, uint32_t length);
  142. /**
  143. * @brief Perform a write using on-chip flash encryption. Not implemented yet.
  144. *
  145. * @param chip Pointer to SPI flash chip to use. If NULL, esp_flash_default_chip is substituted.
  146. * @param buffer Buffer holding the data to program
  147. * @param address Start address to write to flash
  148. * @param length Length to write
  149. *
  150. * @return always ESP_ERR_FLASH_UNSUPPORTED_HOST.
  151. */
  152. esp_err_t
  153. spi_flash_chip_generic_write_encrypted(esp_flash_t *chip, const void *buffer, uint32_t address, uint32_t length);
  154. /**
  155. * @brief Send the write enable or write disable command and verify the expected bit (1) in
  156. * the status register is set.
  157. *
  158. * @param chip Pointer to SPI flash chip to use. If NULL, esp_flash_default_chip is substituted.
  159. * @param write_protect true to enable write protection, false to send write enable.
  160. *
  161. * @return
  162. * - ESP_OK if success
  163. * - or other error passed from the ``wait_idle``, ``read_status`` or
  164. * ``set_write_protect`` function of host driver
  165. */
  166. esp_err_t spi_flash_chip_generic_set_write_protect(esp_flash_t *chip, bool write_protect);
  167. /**
  168. * @brief Check whether WEL (write enable latch) bit is set in the Status Register read from RDSR.
  169. *
  170. * @param chip Pointer to SPI flash chip to use. If NULL, esp_flash_default_chip is substituted.
  171. * @param out_write_protect Output of whether the write protect is set.
  172. *
  173. * @return
  174. * - ESP_OK if success
  175. * - or other error passed from the ``read_status`` function of host driver
  176. */
  177. esp_err_t spi_flash_chip_generic_get_write_protect(esp_flash_t *chip, bool *out_write_protect);
  178. #define ESP_FLASH_CHIP_GENERIC_NO_TIMEOUT -1
  179. /**
  180. * @brief Send commands to read one of the reg of the chip
  181. *
  182. * @param chip Pointer to SPI flash chip to use. If NULL, esp_flash_default_chip is substituted.
  183. * @param reg_id Type of the register to read
  184. * @param out_reg Output of the register value
  185. * @return esp_err_t Error code passed from the ``read_status`` function of host driver.
  186. */
  187. esp_err_t spi_flash_chip_generic_read_reg(esp_flash_t* chip, spi_flash_register_t reg_id, uint32_t* out_reg);
  188. /**
  189. * @brief Read flash status via the RDSR command and wait for bit 0 (write in
  190. * progress bit) to be cleared.
  191. *
  192. * @param chip Pointer to SPI flash chip to use. If NULL, esp_flash_default_chip is substituted.
  193. * @param timeout_us Time to wait before timeout, in us.
  194. *
  195. * @return
  196. * - ESP_OK if success
  197. * - ESP_ERR_TIMEOUT if not idle before timeout
  198. * - or other error passed from the ``wait_idle`` or ``read_status`` function of host driver
  199. */
  200. esp_err_t spi_flash_chip_generic_wait_idle(esp_flash_t *chip, uint32_t timeout_us);
  201. /**
  202. * @brief Set the specified SPI read mode according to the data in the chip
  203. * context. Set quad enable status register bit if needed.
  204. *
  205. * @param chip Pointer to SPI flash chip to use. If NULL, esp_flash_default_chip is substituted.
  206. *
  207. * @return
  208. * - ESP_OK if success
  209. * - ESP_ERR_TIMEOUT if not idle before timeout
  210. * - or other error passed from the ``set_write_protect`` or ``common_command`` function of host driver
  211. */
  212. esp_err_t spi_flash_chip_generic_set_io_mode(esp_flash_t *chip);
  213. /**
  214. * Get whether the Quad Enable (QE) is set.
  215. *
  216. * @param chip Pointer to SPI flash chip to use. If NULL, esp_flash_default_chip is substituted.
  217. * @param out_quad_mode Pointer to store the output mode.
  218. * - SPI_FLASH_QOUT: QE is enabled
  219. * - otherwise: QE is disabled
  220. *
  221. * @return
  222. * - ESP_OK if success
  223. * - or other error passed from the ``common_command`` function of host driver
  224. */
  225. esp_err_t spi_flash_chip_generic_get_io_mode(esp_flash_t *chip, esp_flash_io_mode_t* out_quad_mode);
  226. /**
  227. * @brief Read the chip unique ID.
  228. *
  229. * @param chip Pointer to SPI flash chip to use. If NULL, esp_flash_default_chip is substituted.
  230. * @param flash_unique_id Pointer to store output unique id.
  231. *
  232. * @return
  233. * - ESP_OK if success
  234. * - ESP_ERR_NOT_SUPPORTED if the chip doesn't support read id.
  235. */
  236. esp_err_t spi_flash_chip_generic_read_unique_id(esp_flash_t *chip, uint64_t* flash_unique_id);
  237. /**
  238. * Generic SPI flash chip_drv, uses all the above functions for its operations.
  239. * In default autodetection, this is used as a catchall if a more specific
  240. * chip_drv is not found.
  241. */
  242. extern const spi_flash_chip_t esp_flash_chip_generic;
  243. /*******************************************************************************
  244. * Utilities
  245. *******************************************************************************/
  246. /// Function pointer type for reading status register with QE bit.
  247. typedef esp_err_t (*esp_flash_rdsr_func_t)(esp_flash_t* chip, uint32_t* out_sr);
  248. /**
  249. * Use RDSR2 (35H) to read bit 15-8 of the SR, and RDSR (05H) to read bit 7-0.
  250. *
  251. * @param chip Pointer to SPI flash chip to use.
  252. * @param out_sr Pointer to buffer to hold the status register, 16 bits.
  253. *
  254. * @return ESP_OK if success, otherwise error code passed from the
  255. * `common_command` function of the host driver.
  256. */
  257. esp_err_t spi_flash_common_read_status_16b_rdsr_rdsr2(esp_flash_t* chip, uint32_t* out_sr);
  258. /**
  259. * Use RDSR2 (35H) to read bit 15-8 of the SR.
  260. *
  261. * @param chip Pointer to SPI flash chip to use.
  262. * @param out_sr Pointer to buffer to hold the status register, 8 bits.
  263. *
  264. * @return ESP_OK if success, otherwise error code passed from the
  265. * `common_command` function of the host driver.
  266. */
  267. esp_err_t spi_flash_common_read_status_8b_rdsr2(esp_flash_t* chip, uint32_t* out_sr);
  268. /**
  269. * Use RDSR (05H) to read bit 7-0 of the SR.
  270. *
  271. * @param chip Pointer to SPI flash chip to use.
  272. * @param out_sr Pointer to buffer to hold the status register, 8 bits.
  273. *
  274. * @return ESP_OK if success, otherwise error code passed from the
  275. * `common_command` function of the host driver.
  276. */
  277. esp_err_t spi_flash_common_read_status_8b_rdsr(esp_flash_t* chip, uint32_t* out_sr);
  278. /// Function pointer type for writing status register with QE bit.
  279. typedef esp_err_t (*esp_flash_wrsr_func_t)(esp_flash_t* chip, uint32_t sr);
  280. /**
  281. * Use WRSR (01H) to write bit 7-0 of the SR.
  282. *
  283. * @param chip Pointer to SPI flash chip to use.
  284. * @param sr Value of the status register to write, 8 bits.
  285. *
  286. * @return ESP_OK if success, otherwise error code passed from the
  287. * `common_command` function of the host driver.
  288. */
  289. esp_err_t spi_flash_common_write_status_8b_wrsr(esp_flash_t* chip, uint32_t sr);
  290. /**
  291. * Use WRSR (01H) to write bit 15-0 of the SR.
  292. *
  293. * @param chip Pointer to SPI flash chip to use.
  294. * @param sr Value of the status register to write, 16 bits.
  295. *
  296. * @return ESP_OK if success, otherwise error code passed from the
  297. * `common_command` function of the host driver.
  298. */
  299. esp_err_t spi_flash_common_write_status_16b_wrsr(esp_flash_t* chip, uint32_t sr);
  300. /**
  301. * Use WRSR2 (31H) to write bit 15-8 of the SR.
  302. *
  303. * @param chip Pointer to SPI flash chip to use.
  304. * @param sr Value of the status register to write, 8 bits.
  305. *
  306. * @return ESP_OK if success, otherwise error code passed from the
  307. * `common_command` function of the host driver.
  308. */
  309. esp_err_t spi_flash_common_write_status_8b_wrsr2(esp_flash_t* chip, uint32_t sr);
  310. /**
  311. * @brief Utility function for set_read_mode chip_drv function. If required,
  312. * set and check the QE bit in the flash chip to enable the QIO/QOUT mode.
  313. *
  314. * Most chip QE enable follows a common pattern, though commands to read/write
  315. * the status register may be different, as well as the position of QE bit.
  316. *
  317. * Registers to actually do Quad transtions and command to be sent in reading
  318. * should also be configured via
  319. * spi_flash_chip_generic_config_host_io_mode().
  320. *
  321. * Note that the bit length and qe position of wrsr_func, rdsr_func and
  322. * qe_sr_bit should be consistent.
  323. *
  324. * @param chip Pointer to SPI flash chip to use.
  325. * @param wrsr_func Function pointer for writing the status register
  326. * @param rdsr_func Function pointer for reading the status register
  327. * @param qe_sr_bit status with the qe bit only.
  328. *
  329. * @return always ESP_OK (currently).
  330. */
  331. esp_err_t spi_flash_common_set_io_mode(esp_flash_t *chip, esp_flash_wrsr_func_t wrsr_func, esp_flash_rdsr_func_t rdsr_func, uint32_t qe_sr_bit);
  332. /**
  333. * @brief Configure the host registers to use the specified read mode set in
  334. * the ``chip->read_mode``.
  335. *
  336. * Usually called in chip_drv read() functions before actual reading
  337. * transactions. Also prepare the command to be sent in read functions.
  338. *
  339. * @param chip Pointer to SPI flash chip to use. If NULL, esp_flash_default_chip is substituted.
  340. * @param flags Special rules to configure io mode, (i.e. Whether 32 bit commands will be used (Currently only W25Q256 and GD25Q256 are supported))
  341. *
  342. * @return
  343. * - ESP_OK if success
  344. * - ESP_ERR_FLASH_NOT_INITIALISED if chip not initialized properly
  345. * - or other error passed from the ``configure_host_mode`` function of host driver
  346. */
  347. esp_err_t spi_flash_chip_generic_config_host_io_mode(esp_flash_t *chip, uint32_t flags);
  348. #define SPI_FLASH_CONFIG_IO_MODE_32B_ADDR BIT(0)
  349. /**
  350. * @brief Handle explicit yield requests
  351. *
  352. * @param chip Pointer to SPI flash chip to use. If NULL, esp_flash_default_chip is substituted.
  353. * @param wip Write (erase) in progress, `true` if this function is called during waiting idle of a erase/write command; else `false`.
  354. * @return ESP_OK if success, otherwise failed.
  355. */
  356. esp_err_t spi_flash_chip_generic_yield(esp_flash_t* chip, uint32_t wip);
  357. /**
  358. * @brief Setup for flash suspend command configuration.
  359. *
  360. * @param chip Pointer to SPI flash chip to use. If NULL, esp_flash_default_chip is substituted.
  361. * @return ESP_OK
  362. */
  363. esp_err_t spi_flash_chip_generic_suspend_cmd_conf(esp_flash_t *chip);
  364. /**
  365. *
  366. * @brief Read the chip unique ID unsupported function.
  367. *
  368. * @param chip Pointer to SPI flash chip to use.
  369. * @param flash_unique_id Pointer to store output unique id (Although this function is an unsupported function, but the parameter should be kept for the consistence of the function pointer).
  370. * @return Always ESP_ERR_NOT_SUPPORTED.
  371. */
  372. esp_err_t spi_flash_chip_generic_read_unique_id_none(esp_flash_t *chip, uint64_t* flash_unique_id);
  373. /// Default timeout configuration used by most chips
  374. extern const flash_chip_op_timeout_t spi_flash_chip_generic_timeout;