spi_flash_hal.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. // Copyright 2010-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. /*******************************************************************************
  15. * NOTICE
  16. * The HAL is not public api, don't use in application code.
  17. * See readme.md in soc/include/hal/readme.md
  18. ******************************************************************************/
  19. // The HAL layer for SPI Flash (common part)
  20. #pragma once
  21. #include "hal/spi_flash_ll.h"
  22. #include "hal/spi_types.h"
  23. #include "hal/spi_flash_types.h"
  24. #include "soc/soc_memory_layout.h"
  25. /* Hardware host-specific constants */
  26. #define SPI_FLASH_HAL_MAX_WRITE_BYTES 64
  27. #define SPI_FLASH_HAL_MAX_READ_BYTES 64
  28. /**
  29. * Generic driver context structure for all chips using the SPI peripheral.
  30. * Include this into the HEAD of the driver data for other driver
  31. * implementations that also use the SPI peripheral.
  32. */
  33. typedef struct {
  34. spi_dev_t *spi; ///< Pointer to SPI peripheral registers (SP1, SPI2 or SPI3). Set before initialisation.
  35. int cs_num; ///< Which cs pin is used, 0-2.
  36. int extra_dummy;
  37. spi_flash_ll_clock_reg_t clock_conf;
  38. } spi_flash_memspi_data_t;
  39. /// Configuration structure for the SPI driver.
  40. typedef struct {
  41. spi_host_device_t host_id; ///< SPI peripheral ID.
  42. int cs_num; ///< Which cs pin is used, 0-2.
  43. bool iomux; ///< Whether the IOMUX is used, used for timing compensation.
  44. int input_delay_ns; ///< Input delay on the MISO pin after the launch clock, used for timing compensation.
  45. esp_flash_speed_t speed;///< SPI flash clock speed to work at.
  46. } spi_flash_memspi_config_t;
  47. /**
  48. * Configure SPI flash hal settings.
  49. *
  50. * @param data Buffer to hold configured data, the buffer should be in DRAM to be available when cache disabled
  51. * @param cfg Configurations to set
  52. *
  53. * @return
  54. * - ESP_OK: success
  55. * - ESP_ERR_INVALID_ARG: the data buffer is not in the DRAM.
  56. */
  57. esp_err_t spi_flash_hal_init(spi_flash_memspi_data_t *data_out, const spi_flash_memspi_config_t *cfg);
  58. /**
  59. * Configure the device-related register before transactions.
  60. *
  61. * @param driver The driver context.
  62. *
  63. * @return always return ESP_OK.
  64. */
  65. esp_err_t spi_flash_hal_device_config(spi_flash_host_driver_t *driver);
  66. /**
  67. * Send an user-defined spi transaction to the device.
  68. *
  69. * @note This is usually used when the memspi interface doesn't support some
  70. * particular commands. Since this function supports timing compensation, it is
  71. * also used to receive some data when the frequency is high.
  72. *
  73. * @param driver The driver context.
  74. * @param trans The transaction to send, also holds the received data.
  75. *
  76. * @return always return ESP_OK.
  77. */
  78. esp_err_t spi_flash_hal_common_command(spi_flash_host_driver_t *driver, spi_flash_trans_t *trans);
  79. /**
  80. * Erase whole flash chip by using the erase chip (C7h) command.
  81. *
  82. * @param driver The driver context.
  83. */
  84. void spi_flash_hal_erase_chip(spi_flash_host_driver_t *driver);
  85. /**
  86. * Erase a specific sector by its start address through the sector erase (20h)
  87. * command.
  88. *
  89. * @param driver The driver context.
  90. * @param start_address Start address of the sector to erase.
  91. */
  92. void spi_flash_hal_erase_sector(spi_flash_host_driver_t *driver, uint32_t start_address);
  93. /**
  94. * Erase a specific 64KB block by its start address through the 64KB block
  95. * erase (D8h) command.
  96. *
  97. * @param driver The driver context.
  98. * @param start_address Start address of the block to erase.
  99. */
  100. void spi_flash_hal_erase_block(spi_flash_host_driver_t *driver, uint32_t start_address);
  101. /**
  102. * Program a page of the flash using the page program (02h) command.
  103. *
  104. * @param driver The driver context.
  105. * @param address Address of the page to program
  106. * @param buffer Data to program
  107. * @param length Size of the buffer in bytes, no larger than ``SPI_FLASH_HAL_MAX_WRITE_BYTES`` (64) bytes.
  108. */
  109. void spi_flash_hal_program_page(spi_flash_host_driver_t *driver, const void *buffer, uint32_t address, uint32_t length);
  110. /**
  111. * Read from the flash. Call ``spi_flash_hal_configure_host_read_mode`` to
  112. * configure the read command before calling this function.
  113. *
  114. * @param driver The driver context.
  115. * @param buffer Buffer to store the read data
  116. * @param address Address to read
  117. * @param length Length to read, no larger than ``SPI_FLASH_HAL_MAX_READ_BYTES`` (64) bytes.
  118. *
  119. * @return always return ESP_OK.
  120. */
  121. esp_err_t spi_flash_hal_read(spi_flash_host_driver_t *driver, void *buffer, uint32_t address, uint32_t read_len);
  122. /**
  123. * @brief Send the write enable (06h) or write disable (04h) command to the flash chip.
  124. *
  125. * @param driver The driver context.
  126. * @param wp true to enable the write protection, otherwise false.
  127. *
  128. * @return always return ESP_OK.
  129. */
  130. esp_err_t spi_flash_hal_set_write_protect(spi_flash_host_driver_t *chip_drv, bool wp);
  131. /**
  132. * Check whether the SPI host is idle and can perform other operations.
  133. *
  134. * @param driver The driver context.
  135. *
  136. * @return ture if idle, otherwise false.
  137. */
  138. bool spi_flash_hal_host_idle(spi_flash_host_driver_t *driver);
  139. /**
  140. * @brief Configure the SPI host hardware registers for the specified io mode.
  141. *
  142. * Note that calling this configures SPI host registers, so if running any
  143. * other commands as part of set_io_mode() then these must be run before
  144. * calling this function.
  145. *
  146. * The command value, address length and dummy cycles are configured according
  147. * to the format of read commands:
  148. *
  149. * - command: 8 bits, value set.
  150. * - address: 24 bits
  151. * - dummy: cycles to compensate the input delay
  152. * - out & in data: 0 bits.
  153. *
  154. * The following commands still need to:
  155. *
  156. * - Read data: set address value and data (length and contents), no need
  157. * to touch command and dummy phases.
  158. * - Common read: set command value, address value (or length to 0 if not used)
  159. * - Common write: set command value, address value (or length to 0 if not
  160. * used), disable dummy phase, and set output data.
  161. *
  162. * @param driver The driver context
  163. * @param io_mode The HW read mode to use
  164. * @param addr_bitlen Length of the address phase, in bits
  165. * @param dummy_cyclelen_base Base cycles of the dummy phase, some extra dummy cycles may be appended to compensate the timing.
  166. * @param command Actual reading command to send to flash chip on the bus.
  167. *
  168. * @return always return ESP_OK.
  169. */
  170. esp_err_t spi_flash_hal_configure_host_io_mode(spi_flash_host_driver_t *driver, uint32_t command, uint32_t addr_bitlen,
  171. int dummy_cyclelen_base, esp_flash_io_mode_t io_mode);
  172. /**
  173. * Poll until the last operation is done.
  174. *
  175. * @param driver The driver context.
  176. */
  177. void spi_flash_hal_poll_cmd_done(spi_flash_host_driver_t *driver);
  178. /**
  179. * Check whether the given buffer can be used as the write buffer directly. If 'chip' is connected to the main SPI bus, we can only write directly from
  180. * regions that are accessible ith cache disabled. *
  181. *
  182. * @param driver The driver context
  183. * @param p The buffer holding data to send.
  184. *
  185. * @return True if the buffer can be used to send data, otherwise false.
  186. */
  187. bool spi_flash_hal_supports_direct_write(spi_flash_host_driver_t *driver, const void *p);
  188. /**
  189. * Check whether the given buffer can be used as the read buffer directly. If 'chip' is connected to the main SPI bus, we can only read directly from
  190. * regions that are accessible ith cache disabled. *
  191. *
  192. * @param driver The driver context
  193. * @param p The buffer to hold the received data.
  194. *
  195. * @return True if the buffer can be used to receive data, otherwise false.
  196. */
  197. bool spi_flash_hal_supports_direct_read(spi_flash_host_driver_t *driver, const void *p);