spi_flash_chip_generic.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. * - or other error passed from the ``set_write_protect``, ``wait_idle`` or ``erase_chip`` function of host driver
  68. */
  69. esp_err_t spi_flash_chip_generic_erase_chip(esp_flash_t *chip);
  70. /**
  71. * @brief Erase sector by using the generic sector erase command.
  72. *
  73. * @param chip Pointer to SPI flash chip to use. If NULL, esp_flash_default_chip is substituted.
  74. * @param start_address Start address of the sector to erase
  75. *
  76. * @return
  77. * - ESP_OK if success
  78. * - or other error passed from the ``set_write_protect``, ``wait_idle`` or ``erase_sector`` function of host driver
  79. */
  80. esp_err_t spi_flash_chip_generic_erase_sector(esp_flash_t *chip, uint32_t start_address);
  81. /**
  82. * @brief Erase block by the generic 64KB block erase command
  83. *
  84. * @param chip Pointer to SPI flash chip to use. If NULL, esp_flash_default_chip is substituted.
  85. * @param start_address Start address of the block to erase
  86. *
  87. * @return
  88. * - ESP_OK if success
  89. * - or other error passed from the ``set_write_protect``, ``wait_idle`` or ``erase_block`` function of host driver
  90. */
  91. esp_err_t spi_flash_chip_generic_erase_block(esp_flash_t *chip, uint32_t start_address);
  92. /**
  93. * @brief Read from flash by using a read command that matches the programmed
  94. * read mode.
  95. *
  96. * @param chip Pointer to SPI flash chip to use. If NULL, esp_flash_default_chip is substituted.
  97. * @param buffer Buffer to hold the data read from flash
  98. * @param address Start address of the data on the flash
  99. * @param length Length to read
  100. *
  101. * @return always ESP_OK currently
  102. */
  103. esp_err_t spi_flash_chip_generic_read(esp_flash_t *chip, void *buffer, uint32_t address, uint32_t length);
  104. /**
  105. * @brief Perform a page program using the page program command.
  106. *
  107. * @note Length of each call should not excced the limitation in
  108. * ``chip->host->max_write_bytes``. This function is called in
  109. * ``spi_flash_chip_generic_write`` recursively until the whole page is
  110. * programmed. Strongly suggest to call ``spi_flash_chip_generic_write``
  111. * instead.
  112. *
  113. * @param chip Pointer to SPI flash chip to use. If NULL, esp_flash_default_chip is substituted.
  114. * @param buffer Buffer holding the data to program
  115. * @param address Start address to write to flash
  116. * @param length Length to write, no longer than ``chip->host->max_write_bytes``.
  117. *
  118. * @return
  119. * - ESP_OK if success
  120. * - or other error passed from the ``wait_idle`` or ``program_page`` function of host driver
  121. */
  122. esp_err_t
  123. spi_flash_chip_generic_page_program(esp_flash_t *chip, const void *buffer, uint32_t address, uint32_t length);
  124. /**
  125. * @brief Perform a generic write. Split the write buffer into page program
  126. * operations, and call chip->chip_drv->page-program() for each.
  127. *
  128. * @param chip Pointer to SPI flash chip to use. If NULL, esp_flash_default_chip is substituted.
  129. * @param buffer Buffer holding the data to program
  130. * @param address Start address to write to flash
  131. * @param length Length to write
  132. *
  133. * @return
  134. * - ESP_OK if success
  135. * - or other error passed from the ``wait_idle`` or ``program_page`` function of host driver
  136. */
  137. esp_err_t spi_flash_chip_generic_write(esp_flash_t *chip, const void *buffer, uint32_t address, uint32_t length);
  138. /**
  139. * @brief Perform a write using on-chip flash encryption. Not implemented yet.
  140. *
  141. * @param chip Pointer to SPI flash chip to use. If NULL, esp_flash_default_chip is substituted.
  142. * @param buffer Buffer holding the data to program
  143. * @param address Start address to write to flash
  144. * @param length Length to write
  145. *
  146. * @return always ESP_ERR_FLASH_UNSUPPORTED_HOST.
  147. */
  148. esp_err_t
  149. spi_flash_chip_generic_write_encrypted(esp_flash_t *chip, const void *buffer, uint32_t address, uint32_t length);
  150. /**
  151. * @brief Send the write enable or write disable command and verify the expected bit (1) in
  152. * the status register is set.
  153. *
  154. * @param chip Pointer to SPI flash chip to use. If NULL, esp_flash_default_chip is substituted.
  155. * @param write_protect true to enable write protection, false to send write enable.
  156. *
  157. * @return
  158. * - ESP_OK if success
  159. * - or other error passed from the ``wait_idle``, ``read_status`` or
  160. * ``set_write_protect`` function of host driver
  161. */
  162. esp_err_t spi_flash_chip_generic_set_write_protect(esp_flash_t *chip, bool write_protect);
  163. /**
  164. * @brief Check whether WEL (write enable latch) bit is set in the Status Register read from RDSR.
  165. *
  166. * @param chip Pointer to SPI flash chip to use. If NULL, esp_flash_default_chip is substituted.
  167. * @param out_write_protect Output of whether the write protect is set.
  168. *
  169. * @return
  170. * - ESP_OK if success
  171. * - or other error passed from the ``read_status`` function of host driver
  172. */
  173. esp_err_t spi_flash_chip_generic_get_write_protect(esp_flash_t *chip, bool *out_write_protect);
  174. /**
  175. * @brief Read flash status via the RDSR command and wait for bit 0 (write in
  176. * progress bit) to be cleared.
  177. *
  178. * @param chip Pointer to SPI flash chip to use. If NULL, esp_flash_default_chip is substituted.
  179. * @param timeout_us Time to wait before timeout, in us.
  180. *
  181. * @return
  182. * - ESP_OK if success
  183. * - ESP_ERR_TIMEOUT if not idle before timeout
  184. * - or other error passed from the ``wait_idle`` or ``read_status`` function of host driver
  185. */
  186. esp_err_t spi_flash_chip_generic_wait_idle(esp_flash_t *chip, uint32_t timeout_us);
  187. /**
  188. * @brief Set the specified SPI read mode according to the data in the chip
  189. * context. Set quad enable status register bit if needed.
  190. *
  191. * @param chip Pointer to SPI flash chip to use. If NULL, esp_flash_default_chip is substituted.
  192. *
  193. * @return
  194. * - ESP_OK if success
  195. * - ESP_ERR_TIMEOUT if not idle before timeout
  196. * - or other error passed from the ``set_write_protect`` or ``common_command`` function of host driver
  197. */
  198. esp_err_t spi_flash_chip_generic_set_io_mode(esp_flash_t *chip);
  199. /**
  200. * Get whether the Quad Enable (QE) is set.
  201. *
  202. * @param chip Pointer to SPI flash chip to use. If NULL, esp_flash_default_chip is substituted.
  203. * @param out_quad_mode Pointer to store the output mode.
  204. * - SPI_FLASH_QOUT: QE is enabled
  205. * - otherwise: QE is disabled
  206. *
  207. * @return
  208. * - ESP_OK if success
  209. * - or other error passed from the ``common_command`` function of host driver
  210. */
  211. esp_err_t spi_flash_chip_generic_get_io_mode(esp_flash_t *chip, esp_flash_io_mode_t* out_quad_mode);
  212. /**
  213. * Generic SPI flash chip_drv, uses all the above functions for its operations.
  214. * In default autodetection, this is used as a catchall if a more specific
  215. * chip_drv is not found.
  216. */
  217. extern const spi_flash_chip_t esp_flash_chip_generic;
  218. /*******************************************************************************
  219. * Utilities
  220. *******************************************************************************/
  221. /**
  222. * @brief Wait for the SPI host hardware state machine to be idle.
  223. *
  224. * This isn't a flash chip_drv operation, but it's called by
  225. * spi_flash_chip_generic_wait_idle() and may be useful when implementing
  226. * alternative drivers.
  227. *
  228. * timeout_us will be decremented if the function needs to wait until the host hardware is idle.
  229. *
  230. * @param chip Pointer to SPI flash chip to use. If NULL, esp_flash_default_chip is substituted.
  231. *
  232. * @return
  233. * - ESP_OK if success
  234. * - ESP_ERR_TIMEOUT if not idle before timeout
  235. * - or other error passed from the ``set_write_protect`` or ``common_command`` function of host driver
  236. */
  237. esp_err_t spi_flash_generic_wait_host_idle(esp_flash_t *chip, uint32_t *timeout_us);
  238. /// Function pointer type for reading status register with QE bit.
  239. typedef esp_err_t (*esp_flash_rdsr_func_t)(esp_flash_t* chip, uint32_t* out_sr);
  240. /**
  241. * Use RDSR2 (35H) to read bit 15-8 of the SR, and RDSR (05H) to read bit 7-0.
  242. *
  243. * @param chip Pointer to SPI flash chip to use.
  244. * @param out_sr Pointer to buffer to hold the status register, 16 bits.
  245. *
  246. * @return ESP_OK if success, otherwise error code passed from the
  247. * `common_command` function of the host driver.
  248. */
  249. esp_err_t spi_flash_common_read_status_16b_rdsr_rdsr2(esp_flash_t* chip, uint32_t* out_sr);
  250. /**
  251. * Use RDSR2 (35H) to read bit 15-8 of the SR.
  252. *
  253. * @param chip Pointer to SPI flash chip to use.
  254. * @param out_sr Pointer to buffer to hold the status register, 8 bits.
  255. *
  256. * @return ESP_OK if success, otherwise error code passed from the
  257. * `common_command` function of the host driver.
  258. */
  259. esp_err_t spi_flash_common_read_status_8b_rdsr2(esp_flash_t* chip, uint32_t* out_sr);
  260. /**
  261. * Use RDSR (05H) to read bit 7-0 of the SR.
  262. *
  263. * @param chip Pointer to SPI flash chip to use.
  264. * @param out_sr Pointer to buffer to hold the status register, 8 bits.
  265. *
  266. * @return ESP_OK if success, otherwise error code passed from the
  267. * `common_command` function of the host driver.
  268. */
  269. esp_err_t spi_flash_common_read_status_8b_rdsr(esp_flash_t* chip, uint32_t* out_sr);
  270. /// Function pointer type for writing status register with QE bit.
  271. typedef esp_err_t (*esp_flash_wrsr_func_t)(esp_flash_t* chip, uint32_t sr);
  272. /**
  273. * Use WRSR (01H) to write bit 7-0 of the SR.
  274. *
  275. * @param chip Pointer to SPI flash chip to use.
  276. * @param sr Value of the status register to write, 8 bits.
  277. *
  278. * @return ESP_OK if success, otherwise error code passed from the
  279. * `common_command` function of the host driver.
  280. */
  281. esp_err_t spi_flash_common_write_status_8b_wrsr(esp_flash_t* chip, uint32_t sr);
  282. /**
  283. * Use WRSR (01H) to write bit 15-0 of the SR.
  284. *
  285. * @param chip Pointer to SPI flash chip to use.
  286. * @param sr Value of the status register to write, 16 bits.
  287. *
  288. * @return ESP_OK if success, otherwise error code passed from the
  289. * `common_command` function of the host driver.
  290. */
  291. esp_err_t spi_flash_common_write_status_16b_wrsr(esp_flash_t* chip, uint32_t sr);
  292. /**
  293. * Use WRSR2 (31H) to write bit 15-8 of the SR.
  294. *
  295. * @param chip Pointer to SPI flash chip to use.
  296. * @param sr Value of the status register to write, 8 bits.
  297. *
  298. * @return ESP_OK if success, otherwise error code passed from the
  299. * `common_command` function of the host driver.
  300. */
  301. esp_err_t spi_flash_common_write_status_8b_wrsr2(esp_flash_t* chip, uint32_t sr);
  302. /**
  303. * @brief Utility function for set_read_mode chip_drv function. If required,
  304. * set and check the QE bit in the flash chip to enable the QIO/QOUT mode.
  305. *
  306. * Most chip QE enable follows a common pattern, though commands to read/write
  307. * the status register may be different, as well as the position of QE bit.
  308. *
  309. * Registers to actually do Quad transtions and command to be sent in reading
  310. * should also be configured via
  311. * spi_flash_chip_generic_config_host_io_mode().
  312. *
  313. * Note that the bit length and qe position of wrsr_func, rdsr_func and
  314. * qe_sr_bit should be consistent.
  315. *
  316. * @param chip Pointer to SPI flash chip to use.
  317. * @param wrsr_func Function pointer for writing the status register
  318. * @param rdsr_func Function pointer for reading the status register
  319. * @param qe_sr_bit status with the qe bit only.
  320. *
  321. * @return always ESP_OK (currently).
  322. */
  323. 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);
  324. /**
  325. * @brief Configure the host registers to use the specified read mode set in
  326. * the ``chip->read_mode``.
  327. *
  328. * Usually called in chip_drv read() functions before actual reading
  329. * transactions. Also prepare the command to be sent in read functions.
  330. *
  331. * @param chip Pointer to SPI flash chip to use. If NULL, esp_flash_default_chip is substituted.
  332. *
  333. * @return
  334. * - ESP_OK if success
  335. * - ESP_ERR_FLASH_NOT_INITIALISED if chip not initialized properly
  336. * - or other error passed from the ``configure_host_mode`` function of host driver
  337. */
  338. esp_err_t spi_flash_chip_generic_config_host_io_mode(esp_flash_t *chip);