spi_flash_os_func_noos.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdarg.h>
  7. #include "sdkconfig.h"
  8. #include "esp_flash.h"
  9. #include "esp_attr.h"
  10. #include "esp_rom_sys.h"
  11. #if CONFIG_IDF_TARGET_ESP32
  12. #include "esp32/rom/cache.h"
  13. #elif CONFIG_IDF_TARGET_ESP32S2
  14. #include "esp32s2/rom/cache.h"
  15. #elif CONFIG_IDF_TARGET_ESP32S3
  16. #include "esp32s3/rom/ets_sys.h"
  17. #include "esp32s3/rom/cache.h"
  18. #elif CONFIG_IDF_TARGET_ESP32C3
  19. #include "esp32c3/rom/ets_sys.h"
  20. #include "esp32c3/rom/cache.h"
  21. #elif CONFIG_IDF_TARGET_ESP32H2
  22. #include "esp32h2/rom/ets_sys.h"
  23. #include "esp32h2/rom/cache.h"
  24. #elif CONFIG_IDF_TARGET_ESP32C2
  25. #include "esp32c2/rom/ets_sys.h"
  26. #include "esp32c2/rom/cache.h"
  27. #endif
  28. #include "esp_attr.h"
  29. #if CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
  30. typedef struct {
  31. uint32_t icache_autoload;
  32. uint32_t dcache_autoload;
  33. } spi_noos_arg_t;
  34. static DRAM_ATTR spi_noos_arg_t spi_arg = { 0 };
  35. #elif CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32H2 || CONFIG_IDF_TARGET_ESP32C2
  36. typedef struct {
  37. uint32_t icache_autoload;
  38. } spi_noos_arg_t;
  39. static DRAM_ATTR spi_noos_arg_t spi_arg = { 0 };
  40. #endif
  41. static IRAM_ATTR esp_err_t start(void *arg)
  42. {
  43. #if CONFIG_IDF_TARGET_ESP32
  44. Cache_Read_Disable(0);
  45. Cache_Read_Disable(1);
  46. #elif CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
  47. spi_noos_arg_t *spi_arg = arg;
  48. spi_arg->icache_autoload = Cache_Suspend_ICache();
  49. spi_arg->dcache_autoload = Cache_Suspend_DCache();
  50. #elif CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32H2 || CONFIG_IDF_TARGET_ESP32C2
  51. spi_noos_arg_t *spi_arg = arg;
  52. spi_arg->icache_autoload = Cache_Suspend_ICache();
  53. #endif
  54. return ESP_OK;
  55. }
  56. static IRAM_ATTR esp_err_t end(void *arg)
  57. {
  58. #if CONFIG_IDF_TARGET_ESP32
  59. Cache_Flush(0);
  60. Cache_Flush(1);
  61. Cache_Read_Enable(0);
  62. Cache_Read_Enable(1);
  63. #elif CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
  64. spi_noos_arg_t *spi_arg = arg;
  65. Cache_Invalidate_ICache_All();
  66. Cache_Resume_ICache(spi_arg->icache_autoload);
  67. Cache_Resume_DCache(spi_arg->dcache_autoload);
  68. #elif CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32H2 || CONFIG_IDF_TARGET_ESP32C2
  69. spi_noos_arg_t *spi_arg = arg;
  70. Cache_Invalidate_ICache_All();
  71. Cache_Resume_ICache(spi_arg->icache_autoload);
  72. #endif
  73. return ESP_OK;
  74. }
  75. static IRAM_ATTR esp_err_t delay_us(void *arg, uint32_t us)
  76. {
  77. esp_rom_delay_us(us);
  78. return ESP_OK;
  79. }
  80. // Currently when the os is not up yet, the caller is supposed to call esp_flash APIs with proper
  81. // buffers.
  82. IRAM_ATTR void* get_temp_buffer_not_supported(void* arg, size_t reqest_size, size_t* out_size)
  83. {
  84. return NULL;
  85. }
  86. const DRAM_ATTR esp_flash_os_functions_t esp_flash_noos_functions = {
  87. .start = start,
  88. .end = end,
  89. .delay_us = delay_us,
  90. .region_protected = NULL,
  91. /* the caller is supposed to call esp_flash_read/esp_flash_write APIs with buffers in DRAM */
  92. .get_temp_buffer = NULL,
  93. .release_temp_buffer = NULL,
  94. .yield = NULL,
  95. };
  96. esp_err_t IRAM_ATTR esp_flash_app_disable_os_functions(esp_flash_t* chip)
  97. {
  98. chip->os_func = &esp_flash_noos_functions;
  99. #if !CONFIG_IDF_TARGET_ESP32
  100. chip->os_func_data = &spi_arg;
  101. #endif
  102. return ESP_OK;
  103. }