memspi_host_driver.h 6.3 KB

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