1 /*
2  * Copyright (c) 2024 Espressif Systems (Shanghai) Co., Ltd.
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 
6 #include <string.h>
7 #include <zephyr/kernel.h>
8 #include <rom/ets_sys.h>
9 #include <esp_psram.h>
10 #include <esp_private/esp_psram_extram.h>
11 #include <zephyr/multi_heap/shared_multi_heap.h>
12 
13 extern int _ext_ram_bss_start;
14 extern int _ext_ram_bss_end;
15 extern int _ext_ram_heap_start;
16 
17 struct shared_multi_heap_region smh_psram = {
18 	.addr = (uintptr_t)&_ext_ram_heap_start,
19 	.size = CONFIG_ESP_SPIRAM_HEAP_SIZE,
20 	.attr = SMH_REG_ATTR_EXTERNAL,
21 };
22 
esp_psram_smh_init(void)23 int esp_psram_smh_init(void)
24 {
25 	shared_multi_heap_pool_init();
26 	return shared_multi_heap_add(&smh_psram, NULL);
27 }
28 
esp_init_psram(void)29 void esp_init_psram(void)
30 {
31 	if (esp_psram_init()) {
32 		ets_printf("Failed to Initialize external RAM, aborting.\n");
33 		return;
34 	}
35 
36 	if (esp_psram_get_size() < CONFIG_ESP_SPIRAM_SIZE) {
37 		ets_printf("External RAM size is less than configured.\n");
38 	}
39 
40 	if (IS_ENABLED(CONFIG_ESP_SPIRAM_MEMTEST)) {
41 		if (esp_psram_is_initialized()) {
42 			if (!esp_psram_extram_test()) {
43 				ets_printf("External RAM failed memory test!");
44 				return;
45 			}
46 		}
47 	}
48 
49 	memset(&_ext_ram_bss_start, 0,
50 	       (&_ext_ram_bss_end - &_ext_ram_bss_start) * sizeof(_ext_ram_bss_start));
51 }
52