spi_flash_chip_generic.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. * Generic SPI flash chip_drv, uses all the above functions for its operations.
  228. * In default autodetection, this is used as a catchall if a more specific
  229. * chip_drv is not found.
  230. */
  231. extern const spi_flash_chip_t esp_flash_chip_generic;
  232. /*******************************************************************************
  233. * Utilities
  234. *******************************************************************************/
  235. /**
  236. * @brief Wait for the SPI host hardware state machine to be idle.
  237. *
  238. * This isn't a flash chip_drv operation, but it's called by
  239. * spi_flash_chip_generic_wait_idle() and may be useful when implementing
  240. * alternative drivers.
  241. *
  242. * timeout_us will be decremented if the function needs to wait until the host hardware is idle.
  243. *
  244. * @param chip Pointer to SPI flash chip to use. If NULL, esp_flash_default_chip is substituted.
  245. *
  246. * @return
  247. * - ESP_OK if success
  248. * - ESP_ERR_TIMEOUT if not idle before timeout
  249. * - or other error passed from the ``set_write_protect`` or ``common_command`` function of host driver
  250. */
  251. esp_err_t spi_flash_generic_wait_host_idle(esp_flash_t *chip, uint32_t *timeout_us);
  252. /// Function pointer type for reading status register with QE bit.
  253. typedef esp_err_t (*esp_flash_rdsr_func_t)(esp_flash_t* chip, uint32_t* out_sr);
  254. /**
  255. * Use RDSR2 (35H) to read bit 15-8 of the SR, and RDSR (05H) to read bit 7-0.
  256. *
  257. * @param chip Pointer to SPI flash chip to use.
  258. * @param out_sr Pointer to buffer to hold the status register, 16 bits.
  259. *
  260. * @return ESP_OK if success, otherwise error code passed from the
  261. * `common_command` function of the host driver.
  262. */
  263. esp_err_t spi_flash_common_read_status_16b_rdsr_rdsr2(esp_flash_t* chip, uint32_t* out_sr);
  264. /**
  265. * Use RDSR2 (35H) to read bit 15-8 of the SR.
  266. *
  267. * @param chip Pointer to SPI flash chip to use.
  268. * @param out_sr Pointer to buffer to hold the status register, 8 bits.
  269. *
  270. * @return ESP_OK if success, otherwise error code passed from the
  271. * `common_command` function of the host driver.
  272. */
  273. esp_err_t spi_flash_common_read_status_8b_rdsr2(esp_flash_t* chip, uint32_t* out_sr);
  274. /**
  275. * Use RDSR (05H) to read bit 7-0 of the SR.
  276. *
  277. * @param chip Pointer to SPI flash chip to use.
  278. * @param out_sr Pointer to buffer to hold the status register, 8 bits.
  279. *
  280. * @return ESP_OK if success, otherwise error code passed from the
  281. * `common_command` function of the host driver.
  282. */
  283. esp_err_t spi_flash_common_read_status_8b_rdsr(esp_flash_t* chip, uint32_t* out_sr);
  284. /// Function pointer type for writing status register with QE bit.
  285. typedef esp_err_t (*esp_flash_wrsr_func_t)(esp_flash_t* chip, uint32_t sr);
  286. /**
  287. * Use WRSR (01H) to write bit 7-0 of the SR.
  288. *
  289. * @param chip Pointer to SPI flash chip to use.
  290. * @param sr Value of the status register to write, 8 bits.
  291. *
  292. * @return ESP_OK if success, otherwise error code passed from the
  293. * `common_command` function of the host driver.
  294. */
  295. esp_err_t spi_flash_common_write_status_8b_wrsr(esp_flash_t* chip, uint32_t sr);
  296. /**
  297. * Use WRSR (01H) to write bit 15-0 of the SR.
  298. *
  299. * @param chip Pointer to SPI flash chip to use.
  300. * @param sr Value of the status register to write, 16 bits.
  301. *
  302. * @return ESP_OK if success, otherwise error code passed from the
  303. * `common_command` function of the host driver.
  304. */
  305. esp_err_t spi_flash_common_write_status_16b_wrsr(esp_flash_t* chip, uint32_t sr);
  306. /**
  307. * Use WRSR2 (31H) to write bit 15-8 of the SR.
  308. *
  309. * @param chip Pointer to SPI flash chip to use.
  310. * @param sr Value of the status register to write, 8 bits.
  311. *
  312. * @return ESP_OK if success, otherwise error code passed from the
  313. * `common_command` function of the host driver.
  314. */
  315. esp_err_t spi_flash_common_write_status_8b_wrsr2(esp_flash_t* chip, uint32_t sr);
  316. /**
  317. * @brief Utility function for set_read_mode chip_drv function. If required,
  318. * set and check the QE bit in the flash chip to enable the QIO/QOUT mode.
  319. *
  320. * Most chip QE enable follows a common pattern, though commands to read/write
  321. * the status register may be different, as well as the position of QE bit.
  322. *
  323. * Registers to actually do Quad transtions and command to be sent in reading
  324. * should also be configured via
  325. * spi_flash_chip_generic_config_host_io_mode().
  326. *
  327. * Note that the bit length and qe position of wrsr_func, rdsr_func and
  328. * qe_sr_bit should be consistent.
  329. *
  330. * @param chip Pointer to SPI flash chip to use.
  331. * @param wrsr_func Function pointer for writing the status register
  332. * @param rdsr_func Function pointer for reading the status register
  333. * @param qe_sr_bit status with the qe bit only.
  334. *
  335. * @return always ESP_OK (currently).
  336. */
  337. 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);
  338. /**
  339. * @brief Configure the host registers to use the specified read mode set in
  340. * the ``chip->read_mode``.
  341. *
  342. * Usually called in chip_drv read() functions before actual reading
  343. * transactions. Also prepare the command to be sent in read functions.
  344. *
  345. * @param chip Pointer to SPI flash chip to use. If NULL, esp_flash_default_chip is substituted.
  346. * @param addr_32bit Whether 32 bit commands will be used (Currently only W25Q256 is supported)
  347. *
  348. * @return
  349. * - ESP_OK if success
  350. * - ESP_ERR_FLASH_NOT_INITIALISED if chip not initialized properly
  351. * - or other error passed from the ``configure_host_mode`` function of host driver
  352. */
  353. esp_err_t spi_flash_chip_generic_config_host_io_mode(esp_flash_t *chip, bool addr_32bit);
  354. /**
  355. * @brief Handle explicit yield requests
  356. *
  357. * @param chip Pointer to SPI flash chip to use. If NULL, esp_flash_default_chip is substituted.
  358. * @param wip Write (erase) in progress, `true` if this function is called during waiting idle of a erase/write command; else `false`.
  359. * @return ESP_OK if success, otherwise failed.
  360. */
  361. esp_err_t spi_flash_chip_generic_yield(esp_flash_t* chip, bool wip);
  362. /// Default timeout configuration used by most chips
  363. const flash_chip_op_timeout_t spi_flash_chip_generic_timeout;