spiram.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*
  2. Abstraction layer for spi-ram. For now, it's no more than a stub for the spiram_psram functions, but if
  3. we add more types of external RAM memory, this can be made into a more intelligent dispatcher.
  4. */
  5. // Copyright 2015-2017 Espressif Systems (Shanghai) PTE LTD
  6. //
  7. // Licensed under the Apache License, Version 2.0 (the "License");
  8. // you may not use this file except in compliance with the License.
  9. // You may obtain a copy of the License at
  10. //
  11. // http://www.apache.org/licenses/LICENSE-2.0
  12. //
  13. // Unless required by applicable law or agreed to in writing, software
  14. // distributed under the License is distributed on an "AS IS" BASIS,
  15. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. // See the License for the specific language governing permissions and
  17. // limitations under the License.
  18. #include <stdint.h>
  19. #include <string.h>
  20. #include <sys/param.h>
  21. #include "sdkconfig.h"
  22. #include "esp_attr.h"
  23. #include "esp_err.h"
  24. #include "esp32s2/spiram.h"
  25. #include "spiram_psram.h"
  26. #include "esp_log.h"
  27. #include "freertos/FreeRTOS.h"
  28. #include "freertos/xtensa_api.h"
  29. #include "soc/soc.h"
  30. #include "esp_heap_caps_init.h"
  31. #include "soc/soc_memory_layout.h"
  32. #include "soc/dport_reg.h"
  33. #include "esp32s2/rom/cache.h"
  34. #include "soc/cache_memory.h"
  35. #include "soc/extmem_reg.h"
  36. #define PSRAM_MODE PSRAM_VADDR_MODE_NORMAL
  37. #if CONFIG_SPIRAM
  38. static const char* TAG = "spiram";
  39. #if CONFIG_SPIRAM_SPEED_40M
  40. #define PSRAM_SPEED PSRAM_CACHE_S40M
  41. #elif CONFIG_SPIRAM_SPEED_80M
  42. #define PSRAM_SPEED PSRAM_CACHE_S80M
  43. #else
  44. #define PSRAM_SPEED PSRAM_CACHE_S20M
  45. #endif
  46. #define SPIRAM_SIZE esp_spiram_get_size()
  47. static bool spiram_inited=false;
  48. /*
  49. Simple RAM test. Writes a word every 32 bytes. Takes about a second to complete for 4MiB. Returns
  50. true when RAM seems OK, false when test fails. WARNING: Do not run this before the 2nd cpu has been
  51. initialized (in a two-core system) or after the heap allocator has taken ownership of the memory.
  52. */
  53. bool esp_spiram_test(void)
  54. {
  55. volatile int *spiram=(volatile int*)(SOC_EXTRAM_DATA_HIGH - SPIRAM_SIZE);
  56. size_t p;
  57. size_t s=SPIRAM_SIZE;
  58. int errct=0;
  59. int initial_err=-1;
  60. if ((SOC_EXTRAM_DATA_HIGH - SOC_EXTRAM_DATA_LOW) < SPIRAM_SIZE) {
  61. ESP_EARLY_LOGW(TAG, "Only test spiram from %08x to %08x\n", SOC_EXTRAM_DATA_LOW, SOC_EXTRAM_DATA_HIGH);
  62. spiram=(volatile int*)SOC_EXTRAM_DATA_LOW;
  63. s = SOC_EXTRAM_DATA_HIGH - SOC_EXTRAM_DATA_LOW;
  64. }
  65. for (p=0; p<(s/sizeof(int)); p+=8) {
  66. spiram[p]=p^0xAAAAAAAA;
  67. }
  68. for (p=0; p<(s/sizeof(int)); p+=8) {
  69. if (spiram[p]!=(p^0xAAAAAAAA)) {
  70. errct++;
  71. if (errct==1) initial_err=p*4;
  72. if (errct < 4) {
  73. ESP_EARLY_LOGE(TAG, "SPI SRAM error@%08x:%08x/%08x \n", &spiram[p], spiram[p], p^0xAAAAAAAA);
  74. }
  75. }
  76. }
  77. if (errct) {
  78. ESP_EARLY_LOGE(TAG, "SPI SRAM memory test fail. %d/%d writes failed, first @ %X\n", errct, s/32, initial_err+SOC_EXTRAM_DATA_LOW);
  79. return false;
  80. } else {
  81. ESP_EARLY_LOGI(TAG, "SPI SRAM memory test OK");
  82. return true;
  83. }
  84. }
  85. #define DRAM0_ONLY_CACHE_SIZE BUS_IRAM0_CACHE_SIZE
  86. #define DRAM0_DRAM1_CACHE_SIZE (BUS_IRAM0_CACHE_SIZE + BUS_IRAM1_CACHE_SIZE)
  87. #define DRAM0_DRAM1_DPORT_CACHE_SIZE (BUS_IRAM0_CACHE_SIZE + BUS_IRAM1_CACHE_SIZE + BUS_DPORT_CACHE_SIZE)
  88. #define DBUS3_ONLY_CACHE_SIZE BUS_AHB_DBUS3_CACHE_SIZE
  89. #define DRAM0_DRAM1_DPORT_DBUS3_CACHE_SIZE (DRAM0_DRAM1_DPORT_CACHE_SIZE + DBUS3_ONLY_CACHE_SIZE)
  90. #define SPIRAM_SIZE_EXC_DRAM0_DRAM1_DPORT (SPIRAM_SIZE - DRAM0_DRAM1_DPORT_CACHE_SIZE)
  91. #define SPIRAM_SIZE_EXC_DATA_CACHE (SPIRAM_SIZE - DRAM0_DRAM1_DPORT_DBUS3_CACHE_SIZE)
  92. #define SPIRAM_SMALL_SIZE_MAP_VADDR (DRAM0_CACHE_ADDRESS_HIGH - SPIRAM_SIZE)
  93. #define SPIRAM_SMALL_SIZE_MAP_PADDR 0
  94. #define SPIRAM_SMALL_SIZE_MAP_SIZE SPIRAM_SIZE
  95. #define SPIRAM_MID_SIZE_MAP_VADDR (AHB_DBUS3_ADDRESS_HIGH - SPIRAM_SIZE_EXC_DRAM0_DRAM1_DPORT)
  96. #define SPIRAM_MID_SIZE_MAP_PADDR 0
  97. #define SPIRAM_MID_SIZE_MAP_SIZE (SPIRAM_SIZE_EXC_DRAM0_DRAM1_DPORT)
  98. #define SPIRAM_BIG_SIZE_MAP_VADDR AHB_DBUS3_ADDRESS_LOW
  99. #define SPIRAM_BIG_SIZE_MAP_PADDR (AHB_DBUS3_ADDRESS_HIGH - DRAM0_DRAM1_DPORT_DBUS3_CACHE_SIZE)
  100. #define SPIRAM_BIG_SIZE_MAP_SIZE DBUS3_ONLY_CACHE_SIZE
  101. #define SPIRAM_MID_BIG_SIZE_MAP_VADDR DPORT_CACHE_ADDRESS_LOW
  102. #define SPIRAM_MID_BIG_SIZE_MAP_PADDR SPIRAM_SIZE_EXC_DRAM0_DRAM1_DPORT
  103. #define SPIRAM_MID_BIG_SIZE_MAP_SIZE DRAM0_DRAM1_DPORT_DBUS3_CACHE_SIZE
  104. void IRAM_ATTR esp_spiram_init_cache(void)
  105. {
  106. Cache_Suspend_DCache();
  107. /* map the address from SPIRAM end to the start, map the address in order: DRAM1, DRAM1, DPORT, DBUS3 */
  108. if (SPIRAM_SIZE <= DRAM0_ONLY_CACHE_SIZE) {
  109. /* cache size <= 3MB + 512 KB, only map DRAM0 bus */
  110. Cache_Dbus_MMU_Set(MMU_ACCESS_SPIRAM, SPIRAM_SMALL_SIZE_MAP_VADDR, SPIRAM_SMALL_SIZE_MAP_PADDR, 64, SPIRAM_SMALL_SIZE_MAP_SIZE >> 16, 0);
  111. REG_CLR_BIT(EXTMEM_PRO_DCACHE_CTRL1_REG, EXTMEM_PRO_DCACHE_MASK_DRAM0);
  112. } else if (SPIRAM_SIZE <= DRAM0_DRAM1_CACHE_SIZE) {
  113. /* cache size <= 7MB + 512KB, only map DRAM0 and DRAM1 bus */
  114. Cache_Dbus_MMU_Set(MMU_ACCESS_SPIRAM, SPIRAM_SMALL_SIZE_MAP_VADDR, SPIRAM_SMALL_SIZE_MAP_PADDR, 64, SPIRAM_SMALL_SIZE_MAP_SIZE >> 16, 0);
  115. REG_CLR_BIT(EXTMEM_PRO_DCACHE_CTRL1_REG, EXTMEM_PRO_DCACHE_MASK_DRAM1 | EXTMEM_PRO_DCACHE_MASK_DRAM0);
  116. } else if (SPIRAM_SIZE <= DRAM0_DRAM1_DPORT_CACHE_SIZE) {
  117. /* cache size <= 10MB + 512KB, map DRAM0, DRAM1, DPORT bus */
  118. Cache_Dbus_MMU_Set(MMU_ACCESS_SPIRAM, SPIRAM_SMALL_SIZE_MAP_VADDR, SPIRAM_SMALL_SIZE_MAP_PADDR, 64, SPIRAM_SMALL_SIZE_MAP_SIZE >> 16, 0);
  119. REG_CLR_BIT(EXTMEM_PRO_DCACHE_CTRL1_REG, EXTMEM_PRO_DCACHE_MASK_DRAM1 | EXTMEM_PRO_DCACHE_MASK_DRAM0 | EXTMEM_PRO_DCACHE_MASK_DPORT);
  120. } else {
  121. #if CONFIG_SPIRAM_USE_AHB_DBUS3// TODO Ready to remove this macro esp32s2 no AHB bus access cache
  122. if (SPIRAM_SIZE <= DRAM0_DRAM1_DPORT_DBUS3_CACHE_SIZE) {
  123. /* cache size <= 14MB + 512KB, map DRAM0, DRAM1, DPORT bus, as well as data bus3 */
  124. Cache_Dbus_MMU_Set(MMU_ACCESS_SPIRAM, SPIRAM_MID_SIZE_MAP_VADDR, SPIRAM_MID_SIZE_MAP_PADDR, 64, SPIRAM_MID_SIZE_MAP_SIZE >> 16, 0);
  125. } else {
  126. /* cache size > 14MB + 512KB, map DRAM0, DRAM1, DPORT bus, as well as data bus3 */
  127. Cache_Dbus_MMU_Set(MMU_ACCESS_SPIRAM, SPIRAM_BIG_SIZE_MAP_VADDR, SPIRAM_BIG_SIZE_MAP_PADDR, 64, SPIRAM_BIG_SIZE_MAP_SIZE >> 16, 0);
  128. }
  129. Cache_Dbus_MMU_Set(MMU_ACCESS_SPIRAM, SPIRAM_MID_BIG_SIZE_MAP_VADDR, SPIRAM_MID_BIG_SIZE_MAP_PADDR, 64, SPIRAM_MID_BIG_SIZE_MAP_SIZE >> 16, 0);
  130. REG_CLR_BIT(EXTMEM_PRO_DCACHE_CTRL1_REG, EXTMEM_PRO_DCACHE_MASK_DRAM1 | EXTMEM_PRO_DCACHE_MASK_DRAM0 | EXTMEM_PRO_DCACHE_MASK_DPORT | EXTMEM_PRO_DCACHE_MASK_BUS3);
  131. #else
  132. /* cache size > 10MB + 512KB, map DRAM0, DRAM1, DPORT bus , only remap 0x3f500000 ~ 0x3ff90000*/
  133. Cache_Dbus_MMU_Set(MMU_ACCESS_SPIRAM, DPORT_CACHE_ADDRESS_LOW, SPIRAM_SMALL_SIZE_MAP_PADDR, 64, DRAM0_DRAM1_DPORT_CACHE_SIZE >> 16, 0);
  134. REG_CLR_BIT(EXTMEM_PRO_DCACHE_CTRL1_REG, EXTMEM_PRO_DCACHE_MASK_DRAM1 | EXTMEM_PRO_DCACHE_MASK_DRAM0 | EXTMEM_PRO_DCACHE_MASK_DPORT);
  135. #endif
  136. }
  137. Cache_Resume_DCache(0);
  138. }
  139. static uint32_t pages_for_flash = 0;
  140. static uint32_t page0_mapped = 0;
  141. static uint32_t page0_page = INVALID_PHY_PAGE;
  142. static uint32_t instrcution_in_spiram = 0;
  143. static uint32_t rodata_in_spiram = 0;
  144. uint32_t esp_spiram_instruction_access_enabled(void)
  145. {
  146. return instrcution_in_spiram;
  147. }
  148. uint32_t esp_spiram_rodata_access_enabled(void)
  149. {
  150. return rodata_in_spiram;
  151. }
  152. esp_err_t esp_spiram_enable_instruction_access(void)
  153. {
  154. uint32_t pages_in_flash = 0;
  155. pages_in_flash += Cache_Count_Flash_Pages(PRO_CACHE_IBUS0, &page0_mapped);
  156. pages_in_flash += Cache_Count_Flash_Pages(PRO_CACHE_IBUS1, &page0_mapped);
  157. if ((pages_in_flash + pages_for_flash) > (SPIRAM_SIZE >> 16)) {
  158. ESP_EARLY_LOGE(TAG, "SPI RAM space not enough for the instructions, has %d pages, need %d pages.", (SPIRAM_SIZE >> 16), (pages_in_flash + pages_for_flash));
  159. return ESP_FAIL;
  160. }
  161. ESP_EARLY_LOGI(TAG, "Instructions copied and mapped to SPIRAM");
  162. pages_for_flash = Cache_Flash_To_SPIRAM_Copy(PRO_CACHE_IBUS0, IRAM0_ADDRESS_LOW, pages_for_flash, &page0_page);
  163. pages_for_flash = Cache_Flash_To_SPIRAM_Copy(PRO_CACHE_IBUS1, IRAM1_ADDRESS_LOW, pages_for_flash, &page0_page);
  164. instrcution_in_spiram = 1;
  165. return ESP_OK;
  166. }
  167. esp_err_t esp_spiram_enable_rodata_access(void)
  168. {
  169. uint32_t pages_in_flash = 0;
  170. pages_in_flash += Cache_Count_Flash_Pages(PRO_CACHE_IBUS2, &page0_mapped);
  171. pages_in_flash += Cache_Count_Flash_Pages(PRO_CACHE_DBUS0, &page0_mapped);
  172. pages_in_flash += Cache_Count_Flash_Pages(PRO_CACHE_DBUS1, &page0_mapped);
  173. pages_in_flash += Cache_Count_Flash_Pages(PRO_CACHE_DBUS2, &page0_mapped);
  174. if ((pages_in_flash + pages_for_flash) > (SPIRAM_SIZE >> 16)) {
  175. ESP_EARLY_LOGE(TAG, "SPI RAM space not enough for the read only data.");
  176. return ESP_FAIL;
  177. }
  178. ESP_EARLY_LOGI(TAG, "Read only data copied and mapped to SPIRAM");
  179. pages_for_flash = Cache_Flash_To_SPIRAM_Copy(PRO_CACHE_IBUS2, DROM0_ADDRESS_LOW, pages_for_flash, &page0_page);
  180. pages_for_flash = Cache_Flash_To_SPIRAM_Copy(PRO_CACHE_DBUS0, DRAM0_ADDRESS_LOW, pages_for_flash, &page0_page);
  181. pages_for_flash = Cache_Flash_To_SPIRAM_Copy(PRO_CACHE_DBUS1, DRAM1_ADDRESS_LOW, pages_for_flash, &page0_page);
  182. pages_for_flash = Cache_Flash_To_SPIRAM_Copy(PRO_CACHE_DBUS2, DPORT_ADDRESS_LOW, pages_for_flash, &page0_page);
  183. rodata_in_spiram = 1;
  184. return ESP_OK;
  185. }
  186. esp_err_t esp_spiram_init(void)
  187. {
  188. esp_err_t r;
  189. r = psram_enable(PSRAM_SPEED, PSRAM_MODE);
  190. if (r != ESP_OK) {
  191. #if CONFIG_SPIRAM_IGNORE_NOTFOUND
  192. ESP_EARLY_LOGE(TAG, "SPI RAM enabled but initialization failed. Bailing out.");
  193. #endif
  194. return r;
  195. }
  196. #if (CONFIG_SPIRAM_SIZE != -1)
  197. if (esp_spiram_get_size()!=CONFIG_SPIRAM_SIZE) {
  198. ESP_EARLY_LOGE(TAG, "Expected %dKiB chip but found %dKiB chip. Bailing out..", CONFIG_SPIRAM_SIZE/1024, esp_spiram_get_size()/1024);
  199. return ESP_ERR_INVALID_SIZE;
  200. }
  201. #endif
  202. ESP_EARLY_LOGI(TAG, "Found %dMBit SPI RAM device",
  203. (esp_spiram_get_size()*8)/(1024*1024));
  204. ESP_EARLY_LOGI(TAG, "SPI RAM mode: %s", PSRAM_SPEED == PSRAM_CACHE_S40M ? "sram 40m" : \
  205. PSRAM_SPEED == PSRAM_CACHE_S80M ? "sram 80m" : "sram 20m");
  206. ESP_EARLY_LOGI(TAG, "PSRAM initialized, cache is in %s mode.", \
  207. (PSRAM_MODE==PSRAM_VADDR_MODE_EVENODD)?"even/odd (2-core)": \
  208. (PSRAM_MODE==PSRAM_VADDR_MODE_LOWHIGH)?"low/high (2-core)": \
  209. (PSRAM_MODE==PSRAM_VADDR_MODE_NORMAL)?"normal (1-core)":"ERROR");
  210. spiram_inited=true;
  211. return ESP_OK;
  212. }
  213. esp_err_t esp_spiram_add_to_heapalloc(void)
  214. {
  215. uint32_t size_for_flash = (pages_for_flash << 16);
  216. ESP_EARLY_LOGI(TAG, "Adding pool of %dK of external SPI memory to heap allocator", (SPIRAM_SIZE - (pages_for_flash << 16))/1024);
  217. //Add entire external RAM region to heap allocator. Heap allocator knows the capabilities of this type of memory, so there's
  218. //no need to explicitly specify them.
  219. if (SPIRAM_SIZE <= DRAM0_DRAM1_DPORT_CACHE_SIZE) {
  220. /* cache size <= 10MB + 512KB, map DRAM0, DRAM1, DPORT bus */
  221. return heap_caps_add_region((intptr_t)SPIRAM_SMALL_SIZE_MAP_VADDR + size_for_flash, (intptr_t)SPIRAM_SMALL_SIZE_MAP_VADDR + SPIRAM_SMALL_SIZE_MAP_SIZE -1);
  222. } else {
  223. #if CONFIG_SPIRAM_USE_AHB_DBUS3 //TODO
  224. if (SPIRAM_SIZE <= DRAM0_DRAM1_DPORT_DBUS3_CACHE_SIZE) {
  225. /* cache size <= 14MB + 512KB, map DRAM0, DRAM1, DPORT bus, as well as data bus3 */
  226. if (size_for_flash <= SPIRAM_MID_SIZE_MAP_SIZE) {
  227. esp_err_t err = heap_caps_add_region((intptr_t)SPIRAM_MID_SIZE_MAP_VADDR + size_for_flash, (intptr_t)SPIRAM_MID_SIZE_MAP_VADDR + SPIRAM_MID_SIZE_MAP_SIZE -1);
  228. if (err) {
  229. return err;
  230. }
  231. return heap_caps_add_region((intptr_t)SPIRAM_MID_BIG_SIZE_MAP_VADDR, (intptr_t)SPIRAM_MID_BIG_SIZE_MAP_VADDR + SPIRAM_MID_BIG_SIZE_MAP_SIZE -1);
  232. } else {
  233. return heap_caps_add_region((intptr_t)SPIRAM_MID_BIG_SIZE_MAP_VADDR + size_for_flash - SPIRAM_MID_SIZE_MAP_SIZE, (intptr_t)SPIRAM_MID_BIG_SIZE_MAP_VADDR + SPIRAM_MID_BIG_SIZE_MAP_SIZE -1);
  234. }
  235. } else {
  236. if (size_for_flash <= SPIRAM_SIZE_EXC_DATA_CACHE) {
  237. esp_err_t err = heap_caps_add_region((intptr_t)SPIRAM_BIG_SIZE_MAP_VADDR, (intptr_t)SPIRAM_BIG_SIZE_MAP_VADDR + SPIRAM_BIG_SIZE_MAP_SIZE -1);
  238. if (err) {
  239. return err;
  240. }
  241. return heap_caps_add_region((intptr_t)SPIRAM_MID_BIG_SIZE_MAP_VADDR, (intptr_t)SPIRAM_MID_BIG_SIZE_MAP_VADDR + SPIRAM_MID_BIG_SIZE_MAP_SIZE -1);
  242. } else if (size_for_flash <= SPIRAM_SIZE_EXC_DRAM0_DRAM1_DPORT) {
  243. esp_err_t err = heap_caps_add_region((intptr_t)SPIRAM_BIG_SIZE_MAP_VADDR + size_for_flash - SPIRAM_SIZE_EXC_DATA_CACHE, (intptr_t)SPIRAM_MID_SIZE_MAP_VADDR + SPIRAM_MID_SIZE_MAP_SIZE -1);
  244. if (err) {
  245. return err;
  246. }
  247. return heap_caps_add_region((intptr_t)SPIRAM_MID_BIG_SIZE_MAP_VADDR, (intptr_t)SPIRAM_MID_BIG_SIZE_MAP_VADDR + SPIRAM_MID_BIG_SIZE_MAP_SIZE -1);
  248. } else {
  249. return heap_caps_add_region((intptr_t)SPIRAM_MID_BIG_SIZE_MAP_VADDR + size_for_flash - SPIRAM_SIZE_EXC_DRAM0_DRAM1_DPORT, (intptr_t)SPIRAM_MID_BIG_SIZE_MAP_VADDR + SPIRAM_MID_BIG_SIZE_MAP_SIZE -1);
  250. }
  251. }
  252. #else
  253. Cache_Dbus_MMU_Set(MMU_ACCESS_SPIRAM, DPORT_CACHE_ADDRESS_LOW, SPIRAM_SMALL_SIZE_MAP_PADDR, 64, DRAM0_DRAM1_DPORT_CACHE_SIZE >> 16, 0);
  254. if (size_for_flash <= SPIRAM_SIZE_EXC_DRAM0_DRAM1_DPORT) {
  255. return heap_caps_add_region((intptr_t)DPORT_CACHE_ADDRESS_LOW, (intptr_t)DPORT_CACHE_ADDRESS_LOW + DRAM0_DRAM1_DPORT_CACHE_SIZE -1);
  256. } else {
  257. return heap_caps_add_region((intptr_t)DPORT_CACHE_ADDRESS_LOW + size_for_flash, (intptr_t)DPORT_CACHE_ADDRESS_LOW + DRAM0_DRAM1_DPORT_CACHE_SIZE -1);
  258. }
  259. return ESP_OK;
  260. #endif
  261. }
  262. }
  263. static uint8_t *dma_heap;
  264. esp_err_t esp_spiram_reserve_dma_pool(size_t size) {
  265. if (size==0) return ESP_OK; //no-op
  266. ESP_EARLY_LOGI(TAG, "Reserving pool of %dK of internal memory for DMA/internal allocations", size/1024);
  267. dma_heap=heap_caps_malloc(size, MALLOC_CAP_DMA|MALLOC_CAP_INTERNAL);
  268. if (!dma_heap) return ESP_ERR_NO_MEM;
  269. uint32_t caps[]={MALLOC_CAP_DMA|MALLOC_CAP_INTERNAL, 0, MALLOC_CAP_8BIT|MALLOC_CAP_32BIT};
  270. return heap_caps_add_region_with_caps(caps, (intptr_t) dma_heap, (intptr_t) dma_heap+size-1);
  271. }
  272. size_t esp_spiram_get_size(void)
  273. {
  274. psram_size_t size=psram_get_size();
  275. if (size==PSRAM_SIZE_16MBITS) return 2*1024*1024;
  276. if (size==PSRAM_SIZE_32MBITS) return 4*1024*1024;
  277. if (size==PSRAM_SIZE_64MBITS) return 8*1024*1024;
  278. return CONFIG_SPIRAM_SIZE;
  279. }
  280. /*
  281. Before flushing the cache, if psram is enabled as a memory-mapped thing, we need to write back the data in the cache to the psram first,
  282. otherwise it will get lost. For now, we just read 64/128K of random PSRAM memory to do this.
  283. */
  284. void IRAM_ATTR esp_spiram_writeback_cache(void)
  285. {
  286. extern void Cache_WriteBack_All(void);
  287. Cache_WriteBack_All();
  288. }
  289. #endif