esp_psram.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /*----------------------------------------------------------------------------------------------------
  7. * Abstraction layer for PSRAM. PSRAM device related registers and MMU/Cache related code shouls be
  8. * abstracted to lower layers.
  9. *
  10. * When we add more types of external RAM memory, this can be made into a more intelligent dispatcher.
  11. *----------------------------------------------------------------------------------------------------*/
  12. #include <sys/param.h>
  13. #include "sdkconfig.h"
  14. #include "esp_attr.h"
  15. #include "esp_err.h"
  16. #include "esp_log.h"
  17. #include "freertos/FreeRTOS.h"
  18. #include "esp_heap_caps_init.h"
  19. #include "hal/mmu_hal.h"
  20. #include "hal/mmu_ll.h"
  21. #include "hal/cache_ll.h"
  22. #include "esp_private/esp_psram_io.h"
  23. #include "esp_private/esp_psram_extram.h"
  24. #include "esp_private/mmu_psram_flash.h"
  25. #include "esp_psram_impl.h"
  26. #include "esp_psram.h"
  27. #include "esp_private/esp_mmu_map_private.h"
  28. #include "esp_mmu_map.h"
  29. #if CONFIG_IDF_TARGET_ESP32
  30. #include "esp32/himem.h"
  31. #include "esp32/rom/cache.h"
  32. #include "esp_private/esp_cache_esp32_private.h"
  33. #endif
  34. /**
  35. * Two types of PSRAM memory regions for now:
  36. * - 8bit aligned
  37. * - 32bit aligned
  38. */
  39. #define PSRAM_MEM_TYPE_NUM 2
  40. #define PSRAM_MEM_8BIT_ALIGNED 0
  41. #define PSRAM_MEM_32BIT_ALIGNED 1
  42. #if CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY
  43. extern uint8_t _ext_ram_bss_start;
  44. extern uint8_t _ext_ram_bss_end;
  45. #endif //#if CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY
  46. #if CONFIG_SPIRAM_ALLOW_NOINIT_SEG_EXTERNAL_MEMORY
  47. extern uint8_t _ext_ram_noinit_start;
  48. extern uint8_t _ext_ram_noinit_end;
  49. #endif //#if CONFIG_SPIRAM_ALLOW_NOINIT_SEG_EXTERNAL_MEMORY
  50. typedef struct {
  51. intptr_t vaddr_start;
  52. intptr_t vaddr_end;
  53. size_t size; //in bytes
  54. } psram_mem_t;
  55. typedef struct {
  56. bool is_initialised;
  57. /**
  58. * @note 1
  59. * As we can't use heap allocator during this stage, we need to statically declare these regions.
  60. * Luckily only S2 has two different types of memory regions:
  61. * - byte-aligned memory
  62. * - word-aligned memory
  63. * On the other hand, the type number usually won't be very big
  64. *
  65. * On other chips, only one region is needed.
  66. * So for chips other than S2, size of `regions_to_heap[1]` and `mapped_regions[1]`will always be zero.
  67. *
  68. * If in the future, this condition is worse (dbus memory isn't consecutive), we need to delegate this context
  69. * to chip-specific files, and only keep a (void *) pointer here pointing to those chip-specific contexts
  70. */
  71. psram_mem_t regions_to_heap[PSRAM_MEM_TYPE_NUM]; //memory regions that are available to be added to the heap allocator
  72. psram_mem_t mapped_regions[PSRAM_MEM_TYPE_NUM]; //mapped memory regions
  73. } psram_ctx_t;
  74. static psram_ctx_t s_psram_ctx;
  75. static const char* TAG = "esp_psram";
  76. #if CONFIG_IDF_TARGET_ESP32
  77. //If no function in esp_himem.c is used, this function will be linked into the
  78. //binary instead of the one in esp_himem.c, automatically making sure no memory
  79. //is reserved if no himem function is used.
  80. size_t __attribute__((weak)) esp_himem_reserved_area_size(void)
  81. {
  82. return 0;
  83. }
  84. static void IRAM_ATTR s_mapping(int v_start, int size)
  85. {
  86. //Enable external RAM in MMU
  87. cache_sram_mmu_set(0, 0, v_start, 0, 32, (size / 1024 / 32));
  88. //Flush and enable icache for APP CPU
  89. #if !CONFIG_FREERTOS_UNICORE
  90. DPORT_CLEAR_PERI_REG_MASK(DPORT_APP_CACHE_CTRL1_REG, DPORT_APP_CACHE_MASK_DRAM1);
  91. cache_sram_mmu_set(1, 0, v_start, 0, 32, (size / 1024 / 32));
  92. #endif
  93. }
  94. #endif //CONFIG_IDF_TARGET_ESP32
  95. esp_err_t esp_psram_init(void)
  96. {
  97. if (s_psram_ctx.is_initialised) {
  98. return ESP_ERR_INVALID_STATE;
  99. }
  100. esp_err_t ret = ESP_FAIL;
  101. ret = esp_psram_impl_enable();
  102. if (ret != ESP_OK) {
  103. #if CONFIG_SPIRAM_IGNORE_NOTFOUND
  104. ESP_EARLY_LOGE(TAG, "PSRAM enabled but initialization failed. Bailing out.");
  105. #endif
  106. return ret;
  107. }
  108. s_psram_ctx.is_initialised = true;
  109. uint32_t psram_physical_size = 0;
  110. ret = esp_psram_impl_get_physical_size(&psram_physical_size);
  111. assert(ret == ESP_OK);
  112. ESP_EARLY_LOGI(TAG, "Found %dMB PSRAM device", psram_physical_size / (1024 * 1024));
  113. ESP_EARLY_LOGI(TAG, "Speed: %dMHz", CONFIG_SPIRAM_SPEED);
  114. #if CONFIG_IDF_TARGET_ESP32
  115. #if CONFIG_FREERTOS_UNICORE
  116. ESP_EARLY_LOGI(TAG, "PSRAM initialized, cache is in normal (1-core) mode.");
  117. #else
  118. ESP_EARLY_LOGI(TAG, "PSRAM initialized, cache is in low/high (2-core) mode.");
  119. #endif
  120. #endif
  121. uint32_t psram_available_size = 0;
  122. ret = esp_psram_impl_get_available_size(&psram_available_size);
  123. assert(ret == ESP_OK);
  124. __attribute__((unused)) uint32_t total_available_size = psram_available_size;
  125. /**
  126. * `start_page` is the psram physical address in MMU page size.
  127. * MMU page size on ESP32S2 is 64KB
  128. * e.g.: psram physical address 16 is in page 0
  129. *
  130. * Here we plan to copy FLASH instructions to psram physical address 0, which is the No.0 page.
  131. */
  132. __attribute__((unused)) uint32_t start_page = 0;
  133. #if CONFIG_SPIRAM_FETCH_INSTRUCTIONS || CONFIG_SPIRAM_RODATA
  134. uint32_t used_page = 0;
  135. #endif
  136. //------------------------------------Copy Flash .text to PSRAM-------------------------------------//
  137. #if CONFIG_SPIRAM_FETCH_INSTRUCTIONS
  138. ret = mmu_config_psram_text_segment(start_page, total_available_size, &used_page);
  139. if (ret != ESP_OK) {
  140. ESP_EARLY_LOGE(TAG, "No enough psram memory for instructon!");
  141. abort();
  142. }
  143. start_page += used_page;
  144. psram_available_size -= MMU_PAGE_TO_BYTES(used_page);
  145. ESP_EARLY_LOGV(TAG, "after copy .text, used page is %d, start_page is %d, psram_available_size is %d B", used_page, start_page, psram_available_size);
  146. #endif //#if CONFIG_SPIRAM_FETCH_INSTRUCTIONS
  147. //------------------------------------Copy Flash .rodata to PSRAM-------------------------------------//
  148. #if CONFIG_SPIRAM_RODATA
  149. ret = mmu_config_psram_rodata_segment(start_page, total_available_size, &used_page);
  150. if (ret != ESP_OK) {
  151. ESP_EARLY_LOGE(TAG, "No enough psram memory for rodata!");
  152. abort();
  153. }
  154. start_page += used_page;
  155. psram_available_size -= MMU_PAGE_TO_BYTES(used_page);
  156. ESP_EARLY_LOGV(TAG, "after copy .rodata, used page is %d, start_page is %d, psram_available_size is %d B", used_page, start_page, psram_available_size);
  157. #endif //#if CONFIG_SPIRAM_RODATA
  158. //----------------------------------Map the PSRAM physical range to MMU-----------------------------//
  159. /**
  160. * @note 2
  161. * Similarly to @note 1, we expect HW DBUS memory to be consecutive.
  162. *
  163. * If situation is worse in the future (memory region isn't consecutive), we need to put these logics into chip-specific files
  164. */
  165. size_t total_mapped_size = 0;
  166. size_t size_to_map = 0;
  167. size_t byte_aligned_size = 0;
  168. ret = esp_mmu_map_get_max_consecutive_free_block_size(MMU_MEM_CAP_READ | MMU_MEM_CAP_WRITE | MMU_MEM_CAP_8BIT | MMU_MEM_CAP_32BIT, MMU_TARGET_PSRAM0, &byte_aligned_size);
  169. assert(ret == ESP_OK);
  170. size_to_map = MIN(byte_aligned_size, psram_available_size);
  171. const void *v_start_8bit_aligned = NULL;
  172. ret = esp_mmu_map_reserve_block_with_caps(size_to_map, MMU_MEM_CAP_READ | MMU_MEM_CAP_WRITE | MMU_MEM_CAP_8BIT | MMU_MEM_CAP_32BIT, MMU_TARGET_PSRAM0, &v_start_8bit_aligned);
  173. assert(ret == ESP_OK);
  174. #if CONFIG_IDF_TARGET_ESP32
  175. s_mapping((int)v_start_8bit_aligned, size_to_map);
  176. #else
  177. uint32_t actual_mapped_len = 0;
  178. #if MMU_LL_MMU_PER_TARGET
  179. mmu_hal_map_region(1, MMU_TARGET_PSRAM0, (intptr_t)v_start_8bit_aligned, MMU_PAGE_TO_BYTES(start_page), size_to_map, &actual_mapped_len);
  180. #else
  181. mmu_hal_map_region(0, MMU_TARGET_PSRAM0, (intptr_t)v_start_8bit_aligned, MMU_PAGE_TO_BYTES(start_page), size_to_map, &actual_mapped_len);
  182. #endif
  183. start_page += BYTES_TO_MMU_PAGE(actual_mapped_len);
  184. ESP_EARLY_LOGV(TAG, "8bit-aligned-region: actual_mapped_len is 0x%x bytes", actual_mapped_len);
  185. cache_bus_mask_t bus_mask = cache_ll_l1_get_bus(0, (uint32_t)v_start_8bit_aligned, actual_mapped_len);
  186. cache_ll_l1_enable_bus(0, bus_mask);
  187. #if !CONFIG_FREERTOS_UNICORE
  188. bus_mask = cache_ll_l1_get_bus(1, (uint32_t)v_start_8bit_aligned, actual_mapped_len);
  189. cache_ll_l1_enable_bus(1, bus_mask);
  190. #endif
  191. #endif //#if CONFIG_IDF_TARGET_ESP32
  192. s_psram_ctx.mapped_regions[PSRAM_MEM_8BIT_ALIGNED].size = size_to_map;
  193. s_psram_ctx.mapped_regions[PSRAM_MEM_8BIT_ALIGNED].vaddr_start = (intptr_t)v_start_8bit_aligned;
  194. s_psram_ctx.mapped_regions[PSRAM_MEM_8BIT_ALIGNED].vaddr_end = (intptr_t)v_start_8bit_aligned + size_to_map;
  195. s_psram_ctx.regions_to_heap[PSRAM_MEM_8BIT_ALIGNED].size = size_to_map;
  196. s_psram_ctx.regions_to_heap[PSRAM_MEM_8BIT_ALIGNED].vaddr_start = (intptr_t)v_start_8bit_aligned;
  197. s_psram_ctx.regions_to_heap[PSRAM_MEM_8BIT_ALIGNED].vaddr_end = (intptr_t)v_start_8bit_aligned + size_to_map;
  198. ESP_EARLY_LOGV(TAG, "8bit-aligned-range: 0x%x B, starting from: 0x%x", s_psram_ctx.mapped_regions[PSRAM_MEM_8BIT_ALIGNED].size, v_start_8bit_aligned);
  199. total_mapped_size += size_to_map;
  200. #if CONFIG_IDF_TARGET_ESP32S2
  201. /**
  202. * On ESP32S2, there are 2 types of DBUS memory:
  203. * - byte-aligned-memory
  204. * - word-aligned-memory
  205. *
  206. * If byte-aligned-memory isn't enough, we search for word-aligned-memory to do mapping
  207. */
  208. if (total_mapped_size < psram_available_size) {
  209. size_to_map = psram_available_size - total_mapped_size;
  210. size_t word_aligned_size = 0;
  211. ret = esp_mmu_map_get_max_consecutive_free_block_size(MMU_MEM_CAP_READ | MMU_MEM_CAP_WRITE | MMU_MEM_CAP_32BIT, MMU_TARGET_PSRAM0, &word_aligned_size);
  212. assert(ret == ESP_OK);
  213. size_to_map = MIN(word_aligned_size, size_to_map);
  214. const void *v_start_32bit_aligned = NULL;
  215. ret = esp_mmu_map_reserve_block_with_caps(size_to_map, MMU_MEM_CAP_READ | MMU_MEM_CAP_WRITE | MMU_MEM_CAP_32BIT, MMU_TARGET_PSRAM0, &v_start_32bit_aligned);
  216. assert(ret == ESP_OK);
  217. mmu_hal_map_region(0, MMU_TARGET_PSRAM0, (intptr_t)v_start_32bit_aligned, MMU_PAGE_TO_BYTES(start_page), size_to_map, &actual_mapped_len);
  218. ESP_EARLY_LOGV(TAG, "32bit-aligned-region: actual_mapped_len is 0x%x bytes", actual_mapped_len);
  219. cache_bus_mask_t bus_mask = cache_ll_l1_get_bus(0, (uint32_t)v_start_32bit_aligned, actual_mapped_len);
  220. cache_ll_l1_enable_bus(0, bus_mask);
  221. s_psram_ctx.mapped_regions[PSRAM_MEM_32BIT_ALIGNED].size = size_to_map;
  222. s_psram_ctx.mapped_regions[PSRAM_MEM_32BIT_ALIGNED].vaddr_start = (intptr_t)v_start_32bit_aligned;
  223. s_psram_ctx.mapped_regions[PSRAM_MEM_32BIT_ALIGNED].vaddr_end = (intptr_t)v_start_32bit_aligned + size_to_map;
  224. s_psram_ctx.regions_to_heap[PSRAM_MEM_32BIT_ALIGNED].size = size_to_map;
  225. s_psram_ctx.regions_to_heap[PSRAM_MEM_32BIT_ALIGNED].vaddr_start = (intptr_t)v_start_32bit_aligned;
  226. s_psram_ctx.regions_to_heap[PSRAM_MEM_32BIT_ALIGNED].vaddr_end = (intptr_t)v_start_32bit_aligned + size_to_map;
  227. ESP_EARLY_LOGV(TAG, "32bit-aligned-range: 0x%x B, starting from: 0x%x", s_psram_ctx.mapped_regions[PSRAM_MEM_32BIT_ALIGNED].size, v_start_32bit_aligned);
  228. total_mapped_size += size_to_map;
  229. }
  230. #endif // #if CONFIG_IDF_TARGET_ESP32S2
  231. if (total_mapped_size < psram_available_size) {
  232. ESP_EARLY_LOGW(TAG, "Virtual address not enough for PSRAM, map as much as we can. %dMB is mapped", total_mapped_size / 1024 / 1024);
  233. }
  234. /*------------------------------------------------------------------------------
  235. * After mapping, we DON'T care about the PSRAM PHYSICAL ADDRESSS ANYMORE!
  236. *----------------------------------------------------------------------------*/
  237. //------------------------------------Configure .bss in PSRAM-------------------------------------//
  238. #if CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY
  239. //should never be negative number
  240. uint32_t ext_bss_size = ((intptr_t)&_ext_ram_bss_end - (intptr_t)&_ext_ram_bss_start);
  241. ESP_EARLY_LOGV(TAG, "ext_bss_size is %d", ext_bss_size);
  242. s_psram_ctx.regions_to_heap[PSRAM_MEM_8BIT_ALIGNED].vaddr_start += ext_bss_size;
  243. s_psram_ctx.regions_to_heap[PSRAM_MEM_8BIT_ALIGNED].size -= ext_bss_size;
  244. #endif //#if CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY
  245. #if CONFIG_SPIRAM_ALLOW_NOINIT_SEG_EXTERNAL_MEMORY
  246. uint32_t ext_noinit_size = ((intptr_t)&_ext_ram_noinit_end - (intptr_t)&_ext_ram_noinit_start);
  247. ESP_EARLY_LOGV(TAG, "ext_noinit_size is %d", ext_noinit_size);
  248. s_psram_ctx.regions_to_heap[PSRAM_MEM_8BIT_ALIGNED].vaddr_start += ext_noinit_size;
  249. s_psram_ctx.regions_to_heap[PSRAM_MEM_8BIT_ALIGNED].size -= ext_noinit_size;
  250. #endif
  251. #if CONFIG_IDF_TARGET_ESP32
  252. s_psram_ctx.regions_to_heap[PSRAM_MEM_8BIT_ALIGNED].size -= esp_himem_reserved_area_size() - 1;
  253. #endif
  254. //will be removed, TODO: IDF-6944
  255. #if CONFIG_IDF_TARGET_ESP32
  256. cache_driver_t drv = {
  257. NULL,
  258. esp_psram_extram_writeback_cache,
  259. };
  260. cache_register_writeback(&drv);
  261. #endif
  262. return ESP_OK;
  263. }
  264. esp_err_t esp_psram_extram_add_to_heap_allocator(void)
  265. {
  266. esp_err_t ret = ESP_FAIL;
  267. uint32_t byte_aligned_caps[] = {MALLOC_CAP_SPIRAM | MALLOC_CAP_DEFAULT, 0, MALLOC_CAP_8BIT | MALLOC_CAP_32BIT};
  268. ret = heap_caps_add_region_with_caps(byte_aligned_caps,
  269. s_psram_ctx.regions_to_heap[PSRAM_MEM_8BIT_ALIGNED].vaddr_start,
  270. s_psram_ctx.regions_to_heap[PSRAM_MEM_8BIT_ALIGNED].vaddr_end);
  271. if (ret != ESP_OK) {
  272. return ret;
  273. }
  274. if (s_psram_ctx.regions_to_heap[PSRAM_MEM_32BIT_ALIGNED].size) {
  275. assert(s_psram_ctx.regions_to_heap[PSRAM_MEM_32BIT_ALIGNED].vaddr_start);
  276. uint32_t word_aligned_caps[] = {MALLOC_CAP_SPIRAM | MALLOC_CAP_DEFAULT, 0, MALLOC_CAP_32BIT};
  277. ret = heap_caps_add_region_with_caps(word_aligned_caps,
  278. s_psram_ctx.regions_to_heap[PSRAM_MEM_32BIT_ALIGNED].vaddr_start,
  279. s_psram_ctx.regions_to_heap[PSRAM_MEM_32BIT_ALIGNED].vaddr_end);
  280. if (ret != ESP_OK) {
  281. return ret;
  282. }
  283. }
  284. ESP_EARLY_LOGI(TAG, "Adding pool of %dK of PSRAM memory to heap allocator",
  285. (s_psram_ctx.regions_to_heap[PSRAM_MEM_8BIT_ALIGNED].size + s_psram_ctx.regions_to_heap[PSRAM_MEM_32BIT_ALIGNED].size) / 1024);
  286. return ESP_OK;
  287. }
  288. bool IRAM_ATTR esp_psram_check_ptr_addr(const void *p)
  289. {
  290. if (!s_psram_ctx.is_initialised) {
  291. return false;
  292. }
  293. return ((intptr_t)p >= s_psram_ctx.mapped_regions[PSRAM_MEM_8BIT_ALIGNED].vaddr_start && (intptr_t)p < s_psram_ctx.mapped_regions[PSRAM_MEM_8BIT_ALIGNED].vaddr_end) ||
  294. ((intptr_t)p >= s_psram_ctx.mapped_regions[PSRAM_MEM_32BIT_ALIGNED].vaddr_start && (intptr_t)p < s_psram_ctx.mapped_regions[PSRAM_MEM_32BIT_ALIGNED].vaddr_end);
  295. }
  296. esp_err_t esp_psram_extram_reserve_dma_pool(size_t size)
  297. {
  298. if (size == 0) {
  299. return ESP_OK; //no-op
  300. }
  301. ESP_EARLY_LOGI(TAG, "Reserving pool of %dK of internal memory for DMA/internal allocations", size / 1024);
  302. /* Pool may be allocated in multiple non-contiguous chunks, depending on available RAM */
  303. while (size > 0) {
  304. size_t next_size = heap_caps_get_largest_free_block(MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL);
  305. next_size = MIN(next_size, size);
  306. ESP_EARLY_LOGD(TAG, "Allocating block of size %d bytes", next_size);
  307. uint8_t *dma_heap = heap_caps_malloc(next_size, MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL);
  308. if (!dma_heap || next_size == 0) {
  309. return ESP_ERR_NO_MEM;
  310. }
  311. uint32_t caps[] = {0, MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL, MALLOC_CAP_8BIT | MALLOC_CAP_32BIT};
  312. esp_err_t e = heap_caps_add_region_with_caps(caps, (intptr_t)dma_heap, (intptr_t)dma_heap + next_size - 1);
  313. if (e != ESP_OK) {
  314. return e;
  315. }
  316. size -= next_size;
  317. }
  318. return ESP_OK;
  319. }
  320. bool IRAM_ATTR __attribute__((pure)) esp_psram_is_initialized(void)
  321. {
  322. return s_psram_ctx.is_initialised;
  323. }
  324. size_t esp_psram_get_size(void)
  325. {
  326. uint32_t available_size = 0;
  327. esp_err_t ret = esp_psram_impl_get_available_size(&available_size);
  328. if (ret != ESP_OK) {
  329. //This means PSRAM isn't initialised, to keep back-compatibility, set size to 0.
  330. available_size = 0;
  331. }
  332. return (size_t)available_size;
  333. }
  334. uint8_t esp_psram_io_get_cs_io(void)
  335. {
  336. return esp_psram_impl_get_cs_io();
  337. }
  338. /*
  339. Simple RAM test. Writes a word every 32 bytes. Takes about a second to complete for 4MiB. Returns
  340. true when RAM seems OK, false when test fails. WARNING: Do not run this before the 2nd cpu has been
  341. initialized (in a two-core system) or after the heap allocator has taken ownership of the memory.
  342. */
  343. static bool s_test_psram(intptr_t v_start, size_t size, intptr_t reserved_start, intptr_t reserved_end)
  344. {
  345. volatile int *spiram = (volatile int *)v_start;
  346. size_t p;
  347. int errct = 0;
  348. int initial_err = -1;
  349. for (p = 0; p < (size / sizeof(int)); p += 8) {
  350. intptr_t addr = (intptr_t)&spiram[p];
  351. if ((reserved_start <= addr) && (addr < reserved_end)) {
  352. continue;
  353. }
  354. spiram[p] = p ^ 0xAAAAAAAA;
  355. }
  356. for (p = 0; p < (size / sizeof(int)); p += 8) {
  357. intptr_t addr = (intptr_t)&spiram[p];
  358. if ((reserved_start <= addr) && (addr < reserved_end)) {
  359. continue;
  360. }
  361. if (spiram[p] != (p ^ 0xAAAAAAAA)) {
  362. errct++;
  363. if (errct == 1) {
  364. initial_err = p * 4;
  365. }
  366. }
  367. }
  368. if (errct) {
  369. ESP_EARLY_LOGE(TAG, "SPI SRAM memory test fail. %d/%d writes failed, first @ %X\n", errct, size / 32, initial_err + v_start);
  370. return false;
  371. } else {
  372. ESP_EARLY_LOGI(TAG, "SPI SRAM memory test OK");
  373. return true;
  374. }
  375. }
  376. bool esp_psram_extram_test(void)
  377. {
  378. bool test_success = false;
  379. #if CONFIG_SPIRAM_ALLOW_NOINIT_SEG_EXTERNAL_MEMORY
  380. intptr_t noinit_vstart = (intptr_t)&_ext_ram_noinit_start;
  381. intptr_t noinit_vend = (intptr_t)&_ext_ram_noinit_end;
  382. #else
  383. intptr_t noinit_vstart = 0;
  384. intptr_t noinit_vend = 0;
  385. #endif
  386. test_success = s_test_psram(s_psram_ctx.mapped_regions[PSRAM_MEM_8BIT_ALIGNED].vaddr_start,
  387. s_psram_ctx.mapped_regions[PSRAM_MEM_8BIT_ALIGNED].size,
  388. noinit_vstart,
  389. noinit_vend);
  390. if (!test_success) {
  391. return false;
  392. }
  393. if (s_psram_ctx.mapped_regions[PSRAM_MEM_32BIT_ALIGNED].size) {
  394. test_success = s_test_psram(s_psram_ctx.mapped_regions[PSRAM_MEM_32BIT_ALIGNED].vaddr_start,
  395. s_psram_ctx.mapped_regions[PSRAM_MEM_32BIT_ALIGNED].size,
  396. 0,
  397. 0);
  398. }
  399. if (!test_success) {
  400. return false;
  401. }
  402. return true;
  403. }