spi_flash_os.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * SPDX-FileCopyrightText: 2019-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * System level MSPI APIs (private)
  8. */
  9. /**
  10. * Currently the MSPI timing tuning related APIs are designed to be private.
  11. * Because:
  12. * 1. now we don't split SPI0 and SPI1, we don't have a component for SPI0, including PSRAM, Cache, etc..
  13. * 2. SPI0 and SPI1 are strongly coupling.
  14. *
  15. * In the future, we may consider creating a component for SPI0, and spi_flash component will only work on SPI1 (and it
  16. * can rely on SPI0). Therefore, we can put these APIs there.
  17. *
  18. */
  19. #pragma once
  20. #include <stdint.h>
  21. #include <stdbool.h>
  22. #include "sdkconfig.h"
  23. #include "esp_err.h"
  24. #if CONFIG_IDF_TARGET_ESP32
  25. #include "esp32/rom/spi_flash.h"
  26. #elif CONFIG_IDF_TARGET_ESP32S2
  27. #include "esp32s2/rom/spi_flash.h"
  28. #elif CONFIG_IDF_TARGET_ESP32C3
  29. #include "esp32c3/rom/spi_flash.h"
  30. #elif CONFIG_IDF_TARGET_ESP32S3
  31. #include "esp32s3/rom/spi_flash.h"
  32. #endif
  33. #include "esp_flash.h"
  34. #include "hal/spi_flash_hal.h"
  35. #ifdef __cplusplus
  36. extern "C" {
  37. #endif
  38. /**
  39. * @brief To setup Flash chip
  40. */
  41. esp_err_t spi_flash_init_chip_state(void);
  42. /**
  43. * @brief Make MSPI work under 20Mhz, remove the timing tuning required delays.
  44. * @param control_spi1 Select whether to control SPI1. For tuning, we need to use SPI1. After tuning (during startup stage), let the flash driver to control SPI1
  45. */
  46. void spi_timing_enter_mspi_low_speed_mode(bool control_spi1);
  47. /**
  48. * @brief Make MSPI work under the frequency as users set, may add certain delays to MSPI RX direction to meet timing requirements.
  49. * @param control_spi1 Select whether to control SPI1. For tuning, we need to use SPI1. After tuning (during startup stage), let the flash driver to control SPI1
  50. */
  51. void spi_timing_enter_mspi_high_speed_mode(bool control_spi1);
  52. /**
  53. * @brief Switch MSPI into low speed mode / high speed mode.
  54. * @note This API is cache safe, it will freeze both D$ and I$ and restore them after MSPI is switched
  55. * @note For some of the MSPI high frequency settings (e.g. 80M DDR mode Flash or PSRAM), timing tuning is required.
  56. * Certain delays will be added to the MSPI RX direction. When CPU clock switches from PLL to XTAL, should call
  57. * this API first to enter MSPI low speed mode to remove the delays, and vice versa.
  58. */
  59. void spi_timing_change_speed_mode_cache_safe(bool switch_down);
  60. /**
  61. * @brief Tune MSPI flash timing to make it work under high frequency
  62. */
  63. void spi_timing_flash_tuning(void);
  64. /**
  65. * @brief Tune MSPI psram timing to make it work under high frequency
  66. */
  67. void spi_timing_psram_tuning(void);
  68. /**
  69. * @brief To initislize the MSPI pins
  70. */
  71. void esp_mspi_pin_init(void);
  72. /**
  73. * @brief Set SPI1 registers to make ROM functions work
  74. * @note This function is used for setting SPI1 registers to the state that ROM SPI functions work
  75. */
  76. void spi_flash_set_rom_required_regs(void);
  77. /**
  78. * @brief Initialize main flash
  79. * @param chip Pointer to main SPI flash(SPI1 CS0) chip to use..
  80. */
  81. esp_err_t esp_flash_init_main(esp_flash_t *chip);
  82. /**
  83. * @brief Should be only used by SPI1 Flash driver to know the necessary timing registers
  84. * @param out_timing_config Pointer to timing_tuning parameters.
  85. */
  86. void spi_timing_get_flash_timing_param(spi_flash_hal_timing_config_t *out_timing_config);
  87. /**
  88. * @brief Get the knowledge if the MSPI timing is tuned or not
  89. */
  90. bool spi_timing_is_tuned(void);
  91. /**
  92. * @brief Set Flash chip specifically required MSPI register settings here
  93. */
  94. void spi_flash_set_vendor_required_regs(void);
  95. #ifdef __cplusplus
  96. }
  97. #endif