memspi_host_driver.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 "hal/spi_flash_hal.h"
  16. /** Default configuration for the memspi (high speed version) */
  17. #define ESP_FLASH_DEFAULT_HOST_DRIVER() (spi_flash_host_driver_t) { \
  18. .dev_config = spi_flash_hal_device_config, \
  19. .common_command = spi_flash_hal_common_command, \
  20. .read_id = memspi_host_read_id_hs, \
  21. .erase_chip = spi_flash_hal_erase_chip, \
  22. .erase_sector = spi_flash_hal_erase_sector, \
  23. .erase_block = spi_flash_hal_erase_block, \
  24. .read_status = memspi_host_read_status_hs, \
  25. .set_write_protect = spi_flash_hal_set_write_protect, \
  26. .supports_direct_write = spi_flash_hal_supports_direct_write, \
  27. .supports_direct_read = spi_flash_hal_supports_direct_read, \
  28. .program_page = spi_flash_hal_program_page, \
  29. .write_data_slicer = memspi_host_write_data_slicer, \
  30. .read = spi_flash_hal_read, \
  31. .read_data_slicer = memspi_host_read_data_slicer, \
  32. .host_status = spi_flash_hal_check_status, \
  33. .configure_host_io_mode = spi_flash_hal_configure_host_io_mode, \
  34. .poll_cmd_done = spi_flash_hal_poll_cmd_done, \
  35. .flush_cache = memspi_host_flush_cache, \
  36. .check_suspend = NULL, \
  37. .resume = spi_flash_hal_resume, \
  38. .suspend = spi_flash_hal_suspend,\
  39. .sus_setup = spi_flash_hal_setup_read_suspend,\
  40. }
  41. /// configuration for the memspi host
  42. typedef spi_flash_hal_config_t memspi_host_config_t;
  43. /// context for the memspi host
  44. typedef spi_flash_hal_context_t memspi_host_inst_t;
  45. /**
  46. * Initialize the memory SPI host.
  47. *
  48. * @param host Pointer to the host structure.
  49. * @param cfg Pointer to configuration structure
  50. *
  51. * @return always return ESP_OK
  52. */
  53. esp_err_t memspi_host_init_pointers(memspi_host_inst_t *host, const memspi_host_config_t *cfg);
  54. /*******************************************************************************
  55. * NOTICE
  56. * Rest part of this file are part of the HAL layer
  57. * The HAL is not public api, don't use in application code.
  58. * See readme.md in hal/include/hal/readme.md
  59. ******************************************************************************/
  60. /**
  61. * @brief Read the Status Register read from RDSR (05h).
  62. *
  63. * High speed implementation of RDID through memspi interface relying on the
  64. * ``common_command``.
  65. *
  66. * @param host The driver context.
  67. * @param id Output of the read ID from the slave.
  68. *
  69. * @return
  70. * - ESP_OK: if success
  71. * - ESP_ERR_FLASH_NO_RESPONSE: if no response from chip
  72. * - or other cases from ``spi_hal_common_command``
  73. */
  74. esp_err_t memspi_host_read_id_hs(spi_flash_host_inst_t *host, uint32_t *id);
  75. /**
  76. * High speed implementation of RDSR through memspi interface relying on the
  77. * ``common_command``.
  78. *
  79. * @param host The driver context.
  80. * @param id Output of the read ID from the slave.
  81. *
  82. * @return
  83. * - ESP_OK: if success
  84. * - or other cases from ``spi_hal_common_command``
  85. */
  86. esp_err_t memspi_host_read_status_hs(spi_flash_host_inst_t *host, uint8_t *out_sr);
  87. /**
  88. * Flush the cache (if needed) after the contents are modified.
  89. *
  90. * @param host The driver context.
  91. * @param addr Start address of the modified region
  92. * @param size Size of the region modified.
  93. *
  94. * @return always ESP_OK.
  95. */
  96. esp_err_t memspi_host_flush_cache(spi_flash_host_inst_t *host, uint32_t addr, uint32_t size);
  97. /**
  98. * Erase contents of entire chip.
  99. *
  100. * @param host The driver context.
  101. */
  102. void memspi_host_erase_chip(spi_flash_host_inst_t *host);
  103. /**
  104. * Erase a sector starting from a given address. For 24bit address only.
  105. *
  106. * @param host The driver context.
  107. * @param start_address Starting address of the sector.
  108. */
  109. void memspi_host_erase_sector(spi_flash_host_inst_t *host, uint32_t start_address);
  110. /**
  111. * Erase a block starting from a given address. For 24bit address only.
  112. *
  113. * @param host The driver context.
  114. * @param start_address Starting address of the block.
  115. */
  116. void memspi_host_erase_block(spi_flash_host_inst_t *host, uint32_t start_address);
  117. /**
  118. * Program a page with contents of a buffer. For 24bit address only.
  119. *
  120. * @param host The driver context.
  121. * @param buffer Buffer which contains the data to be flashed.
  122. * @param address Starting address of where to flash the data.
  123. * @param length The number of bytes to flash.
  124. */
  125. void memspi_host_program_page(spi_flash_host_inst_t *host, const void *buffer, uint32_t address, uint32_t length);
  126. /**
  127. * Set ability to write to chip.
  128. *
  129. * @param host The driver context.
  130. * @param wp Enable or disable write protect (true - enable, false - disable).
  131. */
  132. esp_err_t memspi_host_set_write_protect(spi_flash_host_inst_t *host, bool wp);
  133. /**
  134. * Read data to buffer.
  135. *
  136. * @param host The driver context.
  137. * @param buffer Buffer which contains the data to be read.
  138. * @param address Starting address of where to read the data.
  139. * @param length The number of bytes to read.
  140. */
  141. esp_err_t memspi_host_read(spi_flash_host_inst_t *host, void *buffer, uint32_t address, uint32_t read_len);
  142. /**
  143. * @brief Slicer for read data used in non-encrypted regions. This slicer does nothing but
  144. * limit the length to the maximum size the host supports.
  145. *
  146. * @param address Flash address to read
  147. * @param len Length to read
  148. * @param align_address Output of the address to read, should be equal to the input `address`
  149. * @param page_size Physical SPI flash page size
  150. *
  151. * @return Length that can actually be read in one `read` call in `spi_flash_host_driver_t`.
  152. */
  153. int memspi_host_read_data_slicer(spi_flash_host_inst_t *host, uint32_t address, uint32_t len, uint32_t *align_address, uint32_t page_size);
  154. /**
  155. * @brief Slicer for write data used in non-encrypted regions. This slicer limit the length to the
  156. * maximum size the host supports, and truncate if the write data lie accross the page boundary
  157. * (256 bytes)
  158. *
  159. * @param address Flash address to write
  160. * @param len Length to write
  161. * @param align_address Output of the address to write, should be equal to the input `address`
  162. * @param page_size Physical SPI flash page size
  163. *
  164. * @return Length that can actually be written in one `program_page` call in `spi_flash_host_driver_t`.
  165. */
  166. int memspi_host_write_data_slicer(spi_flash_host_inst_t *host, uint32_t address, uint32_t len, uint32_t *align_address, uint32_t page_size);