memspi_host_driver.h 6.6 KB

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