spi_flash_os.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. * SPDX-FileCopyrightText: 2019-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * System level MSPI APIs (private)
  8. */
  9. #pragma once
  10. #include <stdint.h>
  11. #include <stdbool.h>
  12. #include "sdkconfig.h"
  13. #include "esp_rom_spiflash.h"
  14. #include "esp_err.h"
  15. #include "esp_flash.h"
  16. #include "hal/spi_flash_hal.h"
  17. #include "spi_flash_override.h"
  18. #include "soc/soc_caps.h"
  19. #include "soc/clk_tree_defs.h"
  20. #ifdef __cplusplus
  21. extern "C" {
  22. #endif
  23. // Type of MSPI IO
  24. typedef enum {
  25. ESP_MSPI_IO_CLK = 0,
  26. ESP_MSPI_IO_Q,
  27. ESP_MSPI_IO_D,
  28. ESP_MSPI_IO_CS0, /* cs for spi flash */
  29. ESP_MSPI_IO_HD,
  30. ESP_MSPI_IO_WP,
  31. #if SOC_SPI_MEM_SUPPORT_OPI_MODE
  32. ESP_MSPI_IO_DQS,
  33. ESP_MSPI_IO_D4,
  34. ESP_MSPI_IO_D5,
  35. ESP_MSPI_IO_D6,
  36. ESP_MSPI_IO_D7,
  37. #endif // SOC_SPI_MEM_SUPPORT_OPI_MODE
  38. #if CONFIG_SPIRAM
  39. ESP_MSPI_IO_CS1, /* cs for spi ram */
  40. #endif
  41. ESP_MSPI_IO_MAX, /* Maximum IO MSPI occupied */
  42. } esp_mspi_io_t;
  43. /**
  44. * @brief To setup Flash chip
  45. */
  46. esp_err_t spi_flash_init_chip_state(void);
  47. /**
  48. * @brief To initislize the MSPI pins
  49. */
  50. void esp_mspi_pin_init(void);
  51. /**
  52. * @brief Get the number of the GPIO corresponding to the given MSPI io
  53. *
  54. * @param[in] io MSPI io
  55. *
  56. * @return MSPI IO number
  57. */
  58. uint8_t esp_mspi_get_io(esp_mspi_io_t io);
  59. /**
  60. * @brief Set SPI1 registers to make ROM functions work
  61. * @note This function is used for setting SPI1 registers to the state that ROM SPI functions work
  62. */
  63. void spi_flash_set_rom_required_regs(void);
  64. /**
  65. * @brief Initialize main flash
  66. * @param chip Pointer to main SPI flash(SPI1 CS0) chip to use..
  67. */
  68. esp_err_t esp_flash_init_main(esp_flash_t *chip);
  69. /**
  70. * @brief Should be only used by SPI1 Flash driver to know the necessary timing registers
  71. * @param out_timing_config Pointer to timing_tuning parameters.
  72. */
  73. void spi_timing_get_flash_timing_param(spi_flash_hal_timing_config_t *out_timing_config);
  74. /**
  75. * @brief Get the knowledge if the MSPI timing is tuned or not
  76. */
  77. bool spi_timing_is_tuned(void);
  78. /**
  79. * @brief Set Flash chip specifically required MSPI register settings here
  80. */
  81. void spi_flash_set_vendor_required_regs(void);
  82. /**
  83. * @brief Judge whether need to reset flash when brownout.
  84. * Set` flash_brownout_needs_reset` inside the function if really need reset.
  85. */
  86. void spi_flash_needs_reset_check(void);
  87. /**
  88. * @brief Set flag to reset flash. set when erase chip or program chip
  89. *
  90. * @param bool status. True if flash is eraing. False if flash is not erasing.
  91. *
  92. * @return None.
  93. */
  94. void spi_flash_set_erasing_flag(bool status);
  95. /**
  96. * @brief Judge whether need to reset flash when brownout.
  97. *
  98. * @return true if need reset, otherwise false.
  99. */
  100. bool spi_flash_brownout_need_reset(void);
  101. #if CONFIG_SPI_FLASH_HPM_ON
  102. /**
  103. * @brief Enable SPI flash high performance mode.
  104. *
  105. * @note 1. When `CONFIG_SPI_FLASH_HPM_ON` is True, caller can always call this function without taking whether the used
  106. * frequency falls into the HPM range into consideration.
  107. * 2. However, caller shouldn't attempt to call this function on Octal flash. `CONFIG_SPI_FLASH_HPM_ON` may be
  108. * True when `CONFIG_ESPTOOLPY_FLASH_MODE_AUTO_DETECT && !CONFIG_ESPTOOLPY_OCT_FLASH`
  109. *
  110. * @return ESP_OK if success.
  111. */
  112. esp_err_t spi_flash_enable_high_performance_mode(void);
  113. /**
  114. * @brief Get the flash dummy through this function
  115. * This can be used when one flash has several dummy configurations to enable the high performance mode.
  116. * @note Don't forget to subtract one when assign to the register of mspi e.g. if the value you get is 4, (4-1=3) should be assigned to the register.
  117. *
  118. * @return Pointer to spi_flash_hpm_dummy_conf_t.
  119. */
  120. const spi_flash_hpm_dummy_conf_t *spi_flash_hpm_get_dummy(void);
  121. /**
  122. * @brief Used to judge whether flash works under HPM mode with dummy adjustment.
  123. *
  124. * @return true Yes, and work under HPM with adjusting dummy. Otherwise, false.
  125. */
  126. bool spi_flash_hpm_dummy_adjust(void);
  127. #endif //CONFIG_SPI_FLASH_HPM_ON
  128. #if SOC_SPI_MEM_SUPPORT_WRAP
  129. /**
  130. * @brief set wrap size of flash
  131. *
  132. * @param wrap_size: wrap mode support disable, 16 32, 64 byte
  133. *
  134. * @return esp_err_t : ESP_OK for successful.
  135. *
  136. */
  137. esp_err_t spi_flash_wrap_enable(spi_flash_wrap_size_t wrap_size);
  138. /**
  139. * @brief Probe flash wrap method
  140. *
  141. * @return esp_err_t: ESP_OK for success
  142. */
  143. esp_err_t spi_flash_wrap_probe(void);
  144. /**
  145. * @brief disable cache wrap
  146. */
  147. esp_err_t spi_flash_wrap_disable(void);
  148. /**
  149. * @brief Check whether flash and esp chip supports wrap mode.
  150. *
  151. * @param wrap_size wrap size.
  152. * @return true: wrap support, otherwise, false.
  153. */
  154. bool spi_flash_support_wrap_size(uint32_t wrap_size);
  155. #endif //SOC_SPI_MEM_SUPPORT_WRAP
  156. /**
  157. * @brief SPI flash critical section enter function.
  158. *
  159. */
  160. typedef void (*spi_flash_guard_start_func_t)(void);
  161. /**
  162. * @brief SPI flash critical section exit function.
  163. */
  164. typedef void (*spi_flash_guard_end_func_t)(void);
  165. /**
  166. * Structure holding SPI flash access critical sections management functions.
  167. *
  168. * Flash API uses two types of flash access management functions:
  169. * 1) Functions which prepare/restore flash cache and interrupts before calling
  170. * appropriate ROM functions (SPIWrite, SPIRead and SPIEraseBlock):
  171. * - 'start' function should disables flash cache and non-IRAM interrupts and
  172. * is invoked before the call to one of ROM function above.
  173. * - 'end' function should restore state of flash cache and non-IRAM interrupts and
  174. * is invoked after the call to one of ROM function above.
  175. * These two functions are not recursive.
  176. *
  177. * Different versions of the guarding functions should be used depending on the context of
  178. * execution (with or without functional OS). In normal conditions when flash API is called
  179. * from task the functions use OS primitives. When there is no OS at all or when
  180. * it is not guaranteed that OS is functional (accessing flash from exception handler) these
  181. * functions cannot use OS primitives or even does not need them (multithreaded access is not possible).
  182. *
  183. * @note Structure and corresponding guard functions should not reside in flash.
  184. * For example structure can be placed in DRAM and functions in IRAM sections.
  185. */
  186. typedef struct {
  187. spi_flash_guard_start_func_t start; /**< critical section start function. */
  188. spi_flash_guard_end_func_t end; /**< critical section end function. */
  189. } spi_flash_guard_funcs_t;
  190. /**
  191. * @brief Sets guard functions to access flash.
  192. *
  193. * @note Pointed structure and corresponding guard functions should not reside in flash.
  194. * For example structure can be placed in DRAM and functions in IRAM sections.
  195. *
  196. * @param funcs pointer to structure holding flash access guard functions.
  197. */
  198. void spi_flash_guard_set(const spi_flash_guard_funcs_t* funcs);
  199. /**
  200. * @brief Get the guard functions used for flash access
  201. *
  202. * @return The guard functions that were set via spi_flash_guard_set(). These functions
  203. * can be called if implementing custom low-level SPI flash operations.
  204. */
  205. const spi_flash_guard_funcs_t *spi_flash_guard_get(void);
  206. /**
  207. * @brief Default OS-aware flash access guard functions
  208. */
  209. extern const spi_flash_guard_funcs_t g_flash_guard_default_ops;
  210. /**
  211. * @brief Non-OS flash access guard functions
  212. *
  213. * @note This version of flash guard functions is to be used when no OS is present or from panic handler.
  214. * It does not use any OS primitives and IPC and implies that only calling CPU is active.
  215. */
  216. extern const spi_flash_guard_funcs_t g_flash_guard_no_os_ops;
  217. /**
  218. * @brief This function is used to re-initialize the flash mmap when using ROM flash
  219. * implementations.
  220. *
  221. * @note Only called in startup. User should not call this function.
  222. */
  223. void spi_flash_rom_impl_init(void);
  224. #ifdef __cplusplus
  225. }
  226. #endif