@@ -97,11 +97,12 @@ void mp_task(void *pvParameter) {
97
97
#endif
98
98
machine_init ();
99
99
100
- // TODO: CONFIG_SPIRAM_SUPPORT is for 3.3 compatibility, remove after move to 4.0.
101
- #if CONFIG_ESP32_SPIRAM_SUPPORT || CONFIG_SPIRAM_SUPPORT
102
- // Try to use the entire external SPIRAM directly for the heap
103
100
size_t mp_task_heap_size ;
104
- void * mp_task_heap = (void * )SOC_EXTRAM_DATA_LOW ;
101
+ void * mp_task_heap = NULL ;
102
+
103
+ #if CONFIG_ESP32_SPIRAM_SUPPORT
104
+ // Try to use the entire external SPIRAM directly for the heap
105
+ mp_task_heap = (void * )SOC_EXTRAM_DATA_LOW ;
105
106
switch (esp_spiram_get_chip_size ()) {
106
107
case ESP_SPIRAM_SIZE_16MBITS :
107
108
mp_task_heap_size = 2 * 1024 * 1024 ;
@@ -112,28 +113,28 @@ void mp_task(void *pvParameter) {
112
113
break ;
113
114
default :
114
115
// No SPIRAM, fallback to normal allocation
115
- mp_task_heap_size = heap_caps_get_largest_free_block (MALLOC_CAP_8BIT );
116
- mp_task_heap = malloc (mp_task_heap_size );
116
+ mp_task_heap = NULL ;
117
117
break ;
118
118
}
119
119
#elif CONFIG_ESP32S2_SPIRAM_SUPPORT || CONFIG_ESP32S3_SPIRAM_SUPPORT
120
120
// Try to use the entire external SPIRAM directly for the heap
121
- size_t mp_task_heap_size ;
122
121
size_t esp_spiram_size = esp_spiram_get_size ();
123
- void * mp_task_heap = (void * )SOC_EXTRAM_DATA_HIGH - esp_spiram_size ;
124
122
if (esp_spiram_size > 0 ) {
123
+ mp_task_heap = (void * )SOC_EXTRAM_DATA_HIGH - esp_spiram_size ;
125
124
mp_task_heap_size = esp_spiram_size ;
126
- } else {
127
- // No SPIRAM, fallback to normal allocation
128
- mp_task_heap_size = heap_caps_get_largest_free_block (MALLOC_CAP_8BIT );
129
- mp_task_heap = malloc (mp_task_heap_size );
130
125
}
131
- #else
132
- // Allocate the uPy heap using malloc and get the largest available region
133
- size_t mp_task_heap_size = heap_caps_get_largest_free_block (MALLOC_CAP_8BIT );
134
- void * mp_task_heap = malloc (mp_task_heap_size );
135
126
#endif
136
127
128
+ if (mp_task_heap == NULL ) {
129
+ // Allocate the uPy heap using malloc and get the largest available region,
130
+ // limiting to 1/2 total available memory to leave memory for the OS.
131
+ mp_task_heap_size = MIN (
132
+ heap_caps_get_largest_free_block (MALLOC_CAP_8BIT ),
133
+ heap_caps_get_total_size (MALLOC_CAP_8BIT ) / 2
134
+ );
135
+ mp_task_heap = malloc (mp_task_heap_size );
136
+ }
137
+
137
138
soft_reset :
138
139
// initialise the stack pointer for the main thread
139
140
mp_stack_set_top ((void * )sp );
0 commit comments