spiram.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. static bool spiram_inited=false;
  47. #define DRAM0_ONLY_CACHE_SIZE BUS_IRAM0_CACHE_SIZE
  48. #define DRAM0_DRAM1_CACHE_SIZE (BUS_IRAM0_CACHE_SIZE + BUS_IRAM1_CACHE_SIZE)
  49. #define DRAM0_DRAM1_DPORT_CACHE_SIZE (BUS_IRAM0_CACHE_SIZE + BUS_IRAM1_CACHE_SIZE + BUS_DPORT_CACHE_SIZE)
  50. #define SPIRAM_SIZE_EXC_DRAM0_DRAM1_DPORT (spiram_size - DRAM0_DRAM1_DPORT_CACHE_SIZE)
  51. #if CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY
  52. extern uint8_t _ext_ram_bss_start, _ext_ram_bss_end;
  53. #define ALIGN_UP_BY(num, align) (((num) + ((align) - 1)) & ~((align) - 1))
  54. #define EXT_BSS_SIZE ((uint32_t)(&_ext_ram_bss_end - &_ext_ram_bss_start))
  55. #define EXT_BSS_PAGE_ALIGN_SIZE (ALIGN_UP_BY(EXT_BSS_SIZE, 0x10000))
  56. #endif
  57. #if CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY
  58. #define SPIRAM_MAP_PADDR_START EXT_BSS_PAGE_ALIGN_SIZE
  59. #define FREE_DRAM0_DRAM1_DPORT_CACHE_START (DPORT_CACHE_ADDRESS_LOW + EXT_BSS_PAGE_ALIGN_SIZE)
  60. #define FREE_DRAM0_DRAM1_DPORT_CACHE_SIZE (DRAM0_DRAM1_DPORT_CACHE_SIZE - EXT_BSS_PAGE_ALIGN_SIZE)
  61. #else
  62. #define SPIRAM_MAP_PADDR_START 0
  63. #define FREE_DRAM0_DRAM1_DPORT_CACHE_START (DPORT_CACHE_ADDRESS_LOW)
  64. #define FREE_DRAM0_DRAM1_DPORT_CACHE_SIZE (DRAM0_DRAM1_DPORT_CACHE_SIZE)
  65. #endif // if CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY
  66. #define SPIRAM_MAP_VADDR_START (DRAM0_CACHE_ADDRESS_HIGH - spiram_map_size)
  67. #define SPIRAM_MAP_SIZE spiram_map_size
  68. static uint32_t next_map_page_num = 0;
  69. static uint32_t instruction_in_spiram = 0;
  70. static uint32_t rodata_in_spiram = 0;
  71. static size_t spiram_size = 0;
  72. static size_t spiram_map_size = 0;
  73. #if CONFIG_SPIRAM_FETCH_INSTRUCTIONS
  74. static int instr_flash2spiram_offs = 0;
  75. static uint32_t instr_start_page = 0;
  76. static uint32_t instr_end_page = 0;
  77. #endif
  78. #if CONFIG_SPIRAM_RODATA
  79. static int rodata_flash2spiram_offs = 0;
  80. static uint32_t rodata_start_page = 0;
  81. static uint32_t rodata_end_page = 0;
  82. #endif
  83. #if CONFIG_SPIRAM_FETCH_INSTRUCTIONS || CONFIG_SPIRAM_RODATA
  84. static uint32_t page0_mapped = 0;
  85. static uint32_t page0_page = INVALID_PHY_PAGE;
  86. #endif
  87. void IRAM_ATTR esp_spiram_init_cache(void)
  88. {
  89. spiram_map_size = spiram_size;
  90. Cache_Suspend_DCache();
  91. #if CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY
  92. /*if instruction or rodata in flash will be load to spiram, some subsequent operations require the start
  93. address to be aligned by page, so allocate N pages address space for spiram's bss*/
  94. Cache_Dbus_MMU_Set(MMU_ACCESS_SPIRAM, DPORT_CACHE_ADDRESS_LOW, 0, 64, EXT_BSS_PAGE_ALIGN_SIZE >> 16, 0);
  95. REG_CLR_BIT(EXTMEM_PRO_DCACHE_CTRL1_REG, EXTMEM_PRO_DCACHE_MASK_DPORT);
  96. next_map_page_num += (EXT_BSS_PAGE_ALIGN_SIZE >> 16);
  97. spiram_map_size -= EXT_BSS_PAGE_ALIGN_SIZE;
  98. #endif
  99. /* map the address from SPIRAM end to the start, map the address in order: DRAM0, DRAM1, DPORT */
  100. if (spiram_map_size <= DRAM0_ONLY_CACHE_SIZE) {
  101. /* psram need to be mapped vaddr size <= 3MB + 512 KB, only map DRAM0 bus */
  102. Cache_Dbus_MMU_Set(MMU_ACCESS_SPIRAM, SPIRAM_MAP_VADDR_START, SPIRAM_MAP_PADDR_START, 64, SPIRAM_MAP_SIZE >> 16, 0);
  103. REG_CLR_BIT(EXTMEM_PRO_DCACHE_CTRL1_REG, EXTMEM_PRO_DCACHE_MASK_DRAM0);
  104. } else if (spiram_map_size <= DRAM0_DRAM1_CACHE_SIZE) {
  105. /* psram need to be mapped vaddr size <= 7MB + 512KB, only map DRAM0 and DRAM1 bus */
  106. Cache_Dbus_MMU_Set(MMU_ACCESS_SPIRAM, SPIRAM_MAP_VADDR_START, SPIRAM_MAP_PADDR_START, 64, SPIRAM_MAP_SIZE >> 16, 0);
  107. REG_CLR_BIT(EXTMEM_PRO_DCACHE_CTRL1_REG, EXTMEM_PRO_DCACHE_MASK_DRAM1 | EXTMEM_PRO_DCACHE_MASK_DRAM0);
  108. } else if (spiram_size <= DRAM0_DRAM1_DPORT_CACHE_SIZE) { // Equivalent to {spiram_map_size < DRAM0_DRAM1_DPORT_CACHE_SIZE - (spiram_size - spiram_map_size)/*bss size*/}
  109. /* psram need to be mapped vaddr size <= 10MB + 512KB - bss_page_align_size, map DRAM0, DRAM1, DPORT bus */
  110. Cache_Dbus_MMU_Set(MMU_ACCESS_SPIRAM, SPIRAM_MAP_VADDR_START, SPIRAM_MAP_PADDR_START, 64, SPIRAM_MAP_SIZE >> 16, 0);
  111. REG_CLR_BIT(EXTMEM_PRO_DCACHE_CTRL1_REG, EXTMEM_PRO_DCACHE_MASK_DRAM1 | EXTMEM_PRO_DCACHE_MASK_DRAM0 | EXTMEM_PRO_DCACHE_MASK_DPORT);
  112. } else {
  113. /* psram need to be mapped vaddr size > 10MB + 512KB - bss_page_align_size, map DRAM0, DRAM1, DPORT bus ,discard the memory in the end of spiram */
  114. Cache_Dbus_MMU_Set(MMU_ACCESS_SPIRAM, FREE_DRAM0_DRAM1_DPORT_CACHE_START, SPIRAM_MAP_PADDR_START, 64, FREE_DRAM0_DRAM1_DPORT_CACHE_SIZE >> 16, 0);
  115. REG_CLR_BIT(EXTMEM_PRO_DCACHE_CTRL1_REG, EXTMEM_PRO_DCACHE_MASK_DRAM1 | EXTMEM_PRO_DCACHE_MASK_DRAM0 | EXTMEM_PRO_DCACHE_MASK_DPORT);
  116. }
  117. Cache_Resume_DCache(0);
  118. }
  119. uint32_t esp_spiram_instruction_access_enabled(void)
  120. {
  121. return instruction_in_spiram;
  122. }
  123. uint32_t esp_spiram_rodata_access_enabled(void)
  124. {
  125. return rodata_in_spiram;
  126. }
  127. #if CONFIG_SPIRAM_FETCH_INSTRUCTIONS
  128. esp_err_t esp_spiram_enable_instruction_access(void)
  129. {
  130. uint32_t pages_in_flash = 0;
  131. pages_in_flash += Cache_Count_Flash_Pages(PRO_CACHE_IBUS0, &page0_mapped);
  132. pages_in_flash += Cache_Count_Flash_Pages(PRO_CACHE_IBUS1, &page0_mapped);
  133. if ((pages_in_flash + next_map_page_num) > (spiram_size >> 16)) {
  134. ESP_EARLY_LOGE(TAG, "SPI RAM space not enough for the instructions, has %d pages, need %d pages.", (spiram_size >> 16), (pages_in_flash + next_map_page_num));
  135. return ESP_FAIL;
  136. }
  137. ESP_EARLY_LOGI(TAG, "Instructions copied and mapped to SPIRAM");
  138. uint32_t instr_mmu_offset = ((uint32_t)&_instruction_reserved_start & 0xFFFFFF)/MMU_PAGE_SIZE;
  139. uint32_t mmu_value = *(volatile uint32_t *)(DR_REG_MMU_TABLE + PRO_CACHE_IBUS0_MMU_START + instr_mmu_offset*sizeof(uint32_t));
  140. mmu_value &= MMU_ADDRESS_MASK;
  141. instr_flash2spiram_offs = mmu_value - next_map_page_num;
  142. ESP_EARLY_LOGV(TAG, "Instructions from flash page%d copy to SPIRAM page%d, Offset: %d", mmu_value, next_map_page_num, instr_flash2spiram_offs);
  143. next_map_page_num = Cache_Flash_To_SPIRAM_Copy(PRO_CACHE_IBUS0, IRAM0_ADDRESS_LOW, next_map_page_num, &page0_page);
  144. next_map_page_num = Cache_Flash_To_SPIRAM_Copy(PRO_CACHE_IBUS1, IRAM1_ADDRESS_LOW, next_map_page_num, &page0_page);
  145. instruction_in_spiram = 1;
  146. return ESP_OK;
  147. }
  148. #endif
  149. #if CONFIG_SPIRAM_RODATA
  150. esp_err_t esp_spiram_enable_rodata_access(void)
  151. {
  152. uint32_t pages_in_flash = 0;
  153. pages_in_flash += Cache_Count_Flash_Pages(PRO_CACHE_IBUS2, &page0_mapped);
  154. pages_in_flash += Cache_Count_Flash_Pages(PRO_CACHE_DBUS0, &page0_mapped);
  155. pages_in_flash += Cache_Count_Flash_Pages(PRO_CACHE_DBUS1, &page0_mapped);
  156. pages_in_flash += Cache_Count_Flash_Pages(PRO_CACHE_DBUS2, &page0_mapped);
  157. if ((pages_in_flash + next_map_page_num) > (spiram_size >> 16)) {
  158. ESP_EARLY_LOGE(TAG, "SPI RAM space not enough for the read only data.");
  159. return ESP_FAIL;
  160. }
  161. ESP_EARLY_LOGI(TAG, "Read only data copied and mapped to SPIRAM");
  162. uint32_t rodata_mmu_offset = ((uint32_t)&_rodata_reserved_start & 0xFFFFFF)/MMU_PAGE_SIZE;
  163. uint32_t mmu_value = *(volatile uint32_t *)(DR_REG_MMU_TABLE + PRO_CACHE_IBUS2_MMU_START + rodata_mmu_offset*sizeof(uint32_t));
  164. mmu_value &= MMU_ADDRESS_MASK;
  165. rodata_flash2spiram_offs = mmu_value - next_map_page_num;
  166. ESP_EARLY_LOGV(TAG, "Rodata from flash page%d copy to SPIRAM page%d, Offset: %d", mmu_value, next_map_page_num, rodata_flash2spiram_offs);
  167. next_map_page_num = Cache_Flash_To_SPIRAM_Copy(PRO_CACHE_IBUS2, DROM0_ADDRESS_LOW, next_map_page_num, &page0_page);
  168. next_map_page_num = Cache_Flash_To_SPIRAM_Copy(PRO_CACHE_DBUS0, DRAM0_ADDRESS_LOW, next_map_page_num, &page0_page);
  169. next_map_page_num = Cache_Flash_To_SPIRAM_Copy(PRO_CACHE_DBUS1, DRAM1_ADDRESS_LOW, next_map_page_num, &page0_page);
  170. next_map_page_num = Cache_Flash_To_SPIRAM_Copy(PRO_CACHE_DBUS2, DPORT_ADDRESS_LOW, next_map_page_num, &page0_page);
  171. rodata_in_spiram = 1;
  172. return ESP_OK;
  173. }
  174. #endif
  175. #if CONFIG_SPIRAM_FETCH_INSTRUCTIONS
  176. void instruction_flash_page_info_init(void)
  177. {
  178. uint32_t instr_page_cnt = ((uint32_t)&_instruction_reserved_end - SOC_IROM_LOW + MMU_PAGE_SIZE - 1)/MMU_PAGE_SIZE;
  179. uint32_t instr_mmu_offset = ((uint32_t)&_instruction_reserved_start & 0xFFFFFF)/MMU_PAGE_SIZE;
  180. instr_start_page = *(volatile uint32_t *)(DR_REG_MMU_TABLE + PRO_CACHE_IBUS0_MMU_START + instr_mmu_offset*sizeof(uint32_t));
  181. instr_start_page &= MMU_ADDRESS_MASK;
  182. instr_end_page = instr_start_page + instr_page_cnt - 1;
  183. }
  184. uint32_t IRAM_ATTR instruction_flash_start_page_get(void)
  185. {
  186. return instr_start_page;
  187. }
  188. uint32_t IRAM_ATTR instruction_flash_end_page_get(void)
  189. {
  190. return instr_end_page;
  191. }
  192. int IRAM_ATTR instruction_flash2spiram_offset(void)
  193. {
  194. return instr_flash2spiram_offs;
  195. }
  196. #endif
  197. #if CONFIG_SPIRAM_RODATA
  198. void rodata_flash_page_info_init(void)
  199. {
  200. uint32_t rodata_page_cnt = ((uint32_t)&_rodata_reserved_end - SOC_DROM_LOW + MMU_PAGE_SIZE - 1)/MMU_PAGE_SIZE;
  201. uint32_t rodata_mmu_offset = ((uint32_t)&_rodata_reserved_start & 0xFFFFFF)/MMU_PAGE_SIZE;
  202. rodata_start_page = *(volatile uint32_t *)(DR_REG_MMU_TABLE + PRO_CACHE_IBUS2_MMU_START + rodata_mmu_offset*sizeof(uint32_t));
  203. rodata_start_page &= MMU_ADDRESS_MASK;
  204. rodata_end_page = rodata_start_page + rodata_page_cnt - 1;
  205. }
  206. uint32_t IRAM_ATTR rodata_flash_start_page_get(void)
  207. {
  208. return rodata_start_page;
  209. }
  210. uint32_t IRAM_ATTR rodata_flash_end_page_get(void)
  211. {
  212. return rodata_end_page;
  213. }
  214. int IRAM_ATTR rodata_flash2spiram_offset(void)
  215. {
  216. return rodata_flash2spiram_offs;
  217. }
  218. #endif
  219. esp_err_t esp_spiram_init(void)
  220. {
  221. esp_err_t r;
  222. r = psram_enable(PSRAM_SPEED, PSRAM_MODE);
  223. if (r != ESP_OK) {
  224. #if CONFIG_SPIRAM_IGNORE_NOTFOUND
  225. ESP_EARLY_LOGE(TAG, "SPI RAM enabled but initialization failed. Bailing out.");
  226. #endif
  227. return r;
  228. }
  229. spiram_inited = true;
  230. spiram_size = esp_spiram_get_size();
  231. #if (CONFIG_SPIRAM_SIZE != -1)
  232. if (spiram_size != CONFIG_SPIRAM_SIZE) {
  233. ESP_EARLY_LOGE(TAG, "Expected %dKiB chip but found %dKiB chip. Bailing out..", CONFIG_SPIRAM_SIZE/1024, spiram_size/1024);
  234. return ESP_ERR_INVALID_SIZE;
  235. }
  236. #endif
  237. ESP_EARLY_LOGI(TAG, "Found %dMBit SPI RAM device",
  238. (spiram_size*8)/(1024*1024));
  239. ESP_EARLY_LOGI(TAG, "SPI RAM mode: %s", PSRAM_SPEED == PSRAM_CACHE_S40M ? "sram 40m" : \
  240. PSRAM_SPEED == PSRAM_CACHE_S80M ? "sram 80m" : "sram 20m");
  241. ESP_EARLY_LOGI(TAG, "PSRAM initialized, cache is in %s mode.", \
  242. (PSRAM_MODE==PSRAM_VADDR_MODE_EVENODD)?"even/odd (2-core)": \
  243. (PSRAM_MODE==PSRAM_VADDR_MODE_LOWHIGH)?"low/high (2-core)": \
  244. (PSRAM_MODE==PSRAM_VADDR_MODE_NORMAL)?"normal (1-core)":"ERROR");
  245. return ESP_OK;
  246. }
  247. esp_err_t esp_spiram_add_to_heapalloc(void)
  248. {
  249. size_t recycle_pages_size = 0;
  250. size_t map_size = 0;
  251. intptr_t map_vaddr, map_paddr;
  252. ESP_EARLY_LOGI(TAG, "Adding pool of %dK of external SPI memory to heap allocator", (spiram_size - (next_map_page_num << 16))/1024);
  253. #if CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY
  254. if(EXT_BSS_SIZE){
  255. ESP_EARLY_LOGI(TAG, "Adding pool of %d Byte(spiram .bss page unused area) of external SPI memory to heap allocator", EXT_BSS_PAGE_ALIGN_SIZE - EXT_BSS_SIZE);
  256. esp_err_t err_status = heap_caps_add_region(DPORT_CACHE_ADDRESS_LOW + EXT_BSS_SIZE, FREE_DRAM0_DRAM1_DPORT_CACHE_START - 1);
  257. if (err_status != ESP_OK){
  258. return err_status;
  259. }
  260. }
  261. #endif
  262. #if CONFIG_SPIRAM_FETCH_INSTRUCTIONS || CONFIG_SPIRAM_RODATA
  263. /* Part of the physical address space in spiram is mapped by IRAM0/DROM0,
  264. so the DPORT_DRAM0_DRAM1 address space of the same size can be released */
  265. uint32_t occupied_pages_size = (next_map_page_num << 16);
  266. recycle_pages_size = occupied_pages_size - SPIRAM_MAP_PADDR_START;
  267. #endif
  268. // Small size: means DPORT_DRAM0_DRAM1 bus virtrual address space larger than the spiram size
  269. if (spiram_size <= DRAM0_DRAM1_DPORT_CACHE_SIZE) {
  270. map_vaddr = SPIRAM_MAP_VADDR_START;
  271. return heap_caps_add_region(map_vaddr + recycle_pages_size, map_vaddr + spiram_map_size - 1); // pass rodata & instruction section
  272. }
  273. // Middle size: means DPORT_DRAM0_DRAM1 bus virtrual address space less than the
  274. // spiram size, but after releasing the virtual address space mapped
  275. // from the rodata or instruction copied from the flash, the released
  276. // virtual address space is enough to map the abandoned physical address
  277. // space in spiram
  278. if (recycle_pages_size >= SPIRAM_SIZE_EXC_DRAM0_DRAM1_DPORT) {
  279. map_vaddr = SPIRAM_MAP_VADDR_START + recycle_pages_size;
  280. map_paddr = SPIRAM_MAP_PADDR_START + recycle_pages_size;
  281. map_size = SPIRAM_MAP_SIZE - recycle_pages_size;
  282. Cache_Dbus_MMU_Set(MMU_ACCESS_SPIRAM, map_vaddr, map_paddr, 64, map_size >> 16, 0);
  283. return heap_caps_add_region(map_vaddr , map_vaddr + map_size - 1);
  284. }
  285. // Large size: means after releasing the virtual address space mapped from the rodata
  286. // or instruction copied from the flash, the released virtual address space
  287. // still not enough to map the abandoned physical address space in spiram,
  288. // so use all the virtual address space as much as possible
  289. map_vaddr = FREE_DRAM0_DRAM1_DPORT_CACHE_START;
  290. map_paddr = SPIRAM_MAP_PADDR_START + recycle_pages_size;
  291. map_size = FREE_DRAM0_DRAM1_DPORT_CACHE_SIZE;
  292. Cache_Dbus_MMU_Set(MMU_ACCESS_SPIRAM, map_vaddr, map_paddr, 64, map_size >> 16, 0);
  293. return heap_caps_add_region(map_vaddr, map_vaddr + FREE_DRAM0_DRAM1_DPORT_CACHE_SIZE -1);
  294. }
  295. static uint8_t *dma_heap;
  296. esp_err_t esp_spiram_reserve_dma_pool(size_t size) {
  297. if (size==0) return ESP_OK; //no-op
  298. ESP_EARLY_LOGI(TAG, "Reserving pool of %dK of internal memory for DMA/internal allocations", size/1024);
  299. dma_heap=heap_caps_malloc(size, MALLOC_CAP_DMA|MALLOC_CAP_INTERNAL);
  300. if (!dma_heap) return ESP_ERR_NO_MEM;
  301. uint32_t caps[]={MALLOC_CAP_DMA|MALLOC_CAP_INTERNAL, 0, MALLOC_CAP_8BIT|MALLOC_CAP_32BIT};
  302. return heap_caps_add_region_with_caps(caps, (intptr_t) dma_heap, (intptr_t) dma_heap+size-1);
  303. }
  304. size_t esp_spiram_get_size(void)
  305. {
  306. if (!spiram_inited) {
  307. ESP_EARLY_LOGE(TAG, "SPI RAM not initialized");
  308. abort();
  309. }
  310. psram_size_t size=psram_get_size();
  311. if (size==PSRAM_SIZE_16MBITS) return 2*1024*1024;
  312. if (size==PSRAM_SIZE_32MBITS) return 4*1024*1024;
  313. if (size==PSRAM_SIZE_64MBITS) return 8*1024*1024;
  314. return CONFIG_SPIRAM_SIZE;
  315. }
  316. /*
  317. 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,
  318. otherwise it will get lost. For now, we just read 64/128K of random PSRAM memory to do this.
  319. */
  320. void IRAM_ATTR esp_spiram_writeback_cache(void)
  321. {
  322. extern void Cache_WriteBack_All(void);
  323. Cache_WriteBack_All();
  324. }
  325. uint8_t esp_spiram_get_cs_io(void)
  326. {
  327. return psram_get_cs_io();
  328. }
  329. /*
  330. Simple RAM test. Writes a word every 32 bytes. Takes about a second to complete for 4MiB. Returns
  331. true when RAM seems OK, false when test fails. WARNING: Do not run this before the 2nd cpu has been
  332. initialized (in a two-core system) or after the heap allocator has taken ownership of the memory.
  333. */
  334. bool esp_spiram_test(void)
  335. {
  336. volatile int *spiram = (volatile int*)(SOC_EXTRAM_DATA_HIGH - spiram_map_size);
  337. size_t p;
  338. size_t s = spiram_map_size;
  339. int errct=0;
  340. int initial_err=-1;
  341. if (SOC_EXTRAM_DATA_SIZE < spiram_map_size) {
  342. ESP_EARLY_LOGW(TAG, "Only test spiram from %08x to %08x\n", SOC_EXTRAM_DATA_LOW, SOC_EXTRAM_DATA_HIGH);
  343. spiram=(volatile int*)SOC_EXTRAM_DATA_LOW;
  344. s = SOC_EXTRAM_DATA_SIZE;
  345. }
  346. for (p=0; p<(s/sizeof(int)); p+=8) {
  347. spiram[p]=p^0xAAAAAAAA;
  348. }
  349. for (p=0; p<(s/sizeof(int)); p+=8) {
  350. if (spiram[p]!=(p^0xAAAAAAAA)) {
  351. errct++;
  352. if (errct==1) initial_err=p*4;
  353. if (errct < 4) {
  354. ESP_EARLY_LOGE(TAG, "SPI SRAM error@%08x:%08x/%08x \n", &spiram[p], spiram[p], p^0xAAAAAAAA);
  355. }
  356. }
  357. }
  358. if (errct) {
  359. 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);
  360. return false;
  361. } else {
  362. ESP_EARLY_LOGI(TAG, "SPI SRAM memory test OK");
  363. return true;
  364. }
  365. }
  366. #endif