spi_flash_os_func_noos.c 3.5 KB

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