Skip to content

Commit f39708a

Browse files
committed
Add PSRAM support to ESP32S2
When configured the CircuitPython heap will be on the external RAM. When not available, the heap will be 48k inside the IDF heap.
1 parent 5771be9 commit f39708a

File tree

4 files changed

+90
-6
lines changed

4 files changed

+90
-6
lines changed

main.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -436,11 +436,12 @@ int run_repl(void) {
436436
}
437437

438438
int __attribute__((used)) main(void) {
439-
memory_init();
440-
441439
// initialise the cpu and peripherals
442440
safe_mode_t safe_mode = port_init();
443441

442+
// Init memory after the port in case the port needs to set aside memory.
443+
memory_init();
444+
444445
// Turn on LEDs
445446
init_status_leds();
446447
rgb_led_status_init();
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
CONFIG_ESP32S2_SPIRAM_SUPPORT=y
2+
3+
#
4+
# SPI RAM config
5+
#
6+
# CONFIG_SPIRAM_TYPE_AUTO is not set
7+
CONFIG_SPIRAM_TYPE_ESPPSRAM16=y
8+
# CONFIG_SPIRAM_TYPE_ESPPSRAM32 is not set
9+
# CONFIG_SPIRAM_TYPE_ESPPSRAM64 is not set
10+
CONFIG_SPIRAM_SIZE=2097152
11+
12+
#
13+
# PSRAM clock and cs IO for ESP32S2
14+
#
15+
CONFIG_DEFAULT_PSRAM_CLK_IO=30
16+
CONFIG_DEFAULT_PSRAM_CS_IO=26
17+
# end of PSRAM clock and cs IO for ESP32S2
18+
19+
# CONFIG_SPIRAM_FETCH_INSTRUCTIONS is not set
20+
# CONFIG_SPIRAM_RODATA is not set
21+
# CONFIG_SPIRAM_SPEED_80M is not set
22+
CONFIG_SPIRAM_SPEED_40M=y
23+
# CONFIG_SPIRAM_SPEED_26M is not set
24+
# CONFIG_SPIRAM_SPEED_20M is not set
25+
CONFIG_SPIRAM=y
26+
CONFIG_SPIRAM_BOOT_INIT=y
27+
# CONFIG_SPIRAM_IGNORE_NOTFOUND is not set
28+
CONFIG_SPIRAM_USE_MEMMAP=y
29+
# CONFIG_SPIRAM_USE_CAPS_ALLOC is not set
30+
# CONFIG_SPIRAM_USE_MALLOC is not set
31+
CONFIG_SPIRAM_MEMTEST=y
32+
# CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY is not set
33+
# end of SPI RAM config
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
CONFIG_ESP32S2_SPIRAM_SUPPORT=y
2+
3+
#
4+
# SPI RAM config
5+
#
6+
# CONFIG_SPIRAM_TYPE_AUTO is not set
7+
# CONFIG_SPIRAM_TYPE_ESPPSRAM16 is not set
8+
# CONFIG_SPIRAM_TYPE_ESPPSRAM32 is not set
9+
CONFIG_SPIRAM_TYPE_ESPPSRAM64=y
10+
CONFIG_SPIRAM_SIZE=8388608
11+
12+
#
13+
# PSRAM clock and cs IO for ESP32S2
14+
#
15+
CONFIG_DEFAULT_PSRAM_CLK_IO=30
16+
CONFIG_DEFAULT_PSRAM_CS_IO=26
17+
# end of PSRAM clock and cs IO for ESP32S2
18+
19+
CONFIG_SPIRAM_SPIWP_SD3_PIN=28
20+
# CONFIG_SPIRAM_FETCH_INSTRUCTIONS is not set
21+
# CONFIG_SPIRAM_RODATA is not set
22+
# CONFIG_SPIRAM_USE_AHB_DBUS3 is not set
23+
# CONFIG_SPIRAM_SPEED_80M is not set
24+
CONFIG_SPIRAM_SPEED_40M=y
25+
# CONFIG_SPIRAM_SPEED_26M is not set
26+
# CONFIG_SPIRAM_SPEED_20M is not set
27+
CONFIG_SPIRAM=y
28+
CONFIG_SPIRAM_BOOT_INIT=y
29+
# CONFIG_SPIRAM_IGNORE_NOTFOUND is not set
30+
CONFIG_SPIRAM_USE_MEMMAP=y
31+
# CONFIG_SPIRAM_USE_CAPS_ALLOC is not set
32+
# CONFIG_SPIRAM_USE_MALLOC is not set
33+
CONFIG_SPIRAM_MEMTEST=y
34+
# CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY is not set
35+
# end of SPI RAM config

ports/esp32s2/supervisor/port.c

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,14 @@
4343
#include "supervisor/memory.h"
4444
#include "supervisor/shared/tick.h"
4545

46-
#include "rmt.h"
46+
#include "peripherals/rmt.h"
47+
#include "esp-idf/components/heap/include/esp_heap_caps.h"
48+
#include "esp-idf/components/soc/soc/esp32s2/include/soc/cache_memory.h"
49+
50+
#define HEAP_SIZE (48 * 1024)
51+
52+
uint32_t* heap;
53+
uint32_t heap_size;
4754

4855
STATIC esp_timer_handle_t _tick_timer;
4956

@@ -59,6 +66,16 @@ safe_mode_t port_init(void) {
5966
args.name = "CircuitPython Tick";
6067
esp_timer_create(&args, &_tick_timer);
6168
never_reset_module_internal_pins();
69+
70+
#ifdef CONFIG_SPIRAM
71+
heap = (uint32_t*) (DRAM0_CACHE_ADDRESS_HIGH - CONFIG_SPIRAM_SIZE);
72+
heap_size = CONFIG_SPIRAM_SIZE / sizeof(uint32_t);
73+
#endif
74+
75+
if (heap == NULL) {
76+
heap = malloc(HEAP_SIZE);
77+
heap_size = HEAP_SIZE / sizeof(uint32_t);
78+
}
6279
return NO_SAFE_MODE;
6380
}
6481

@@ -86,14 +103,12 @@ void reset_to_bootloader(void) {
86103
void reset_cpu(void) {
87104
}
88105

89-
uint32_t heap[64 / sizeof(uint32_t) * 1024];
90-
91106
uint32_t *port_heap_get_bottom(void) {
92107
return heap;
93108
}
94109

95110
uint32_t *port_heap_get_top(void) {
96-
return heap + sizeof(heap) / sizeof(heap[0]);
111+
return heap + heap_size;
97112
}
98113

99114
uint32_t *port_stack_get_limit(void) {

0 commit comments

Comments
 (0)