Skip to content

Commit b18665a

Browse files
authored
Merge pull request #8835 from tannewt/allocate_spiram_first
Allocate to SPIRAM first on ESP
2 parents 177cfe4 + 6080b13 commit b18665a

File tree

1 file changed

+11
-8
lines changed
  • ports/espressif/supervisor

1 file changed

+11
-8
lines changed

ports/espressif/supervisor/port.c

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,6 @@
107107
#include "esp_log.h"
108108
#define TAG "port"
109109

110-
uint32_t *heap;
111-
uint32_t heap_size;
112-
113110
STATIC esp_timer_handle_t _tick_timer;
114111
STATIC esp_timer_handle_t _sleep_timer;
115112

@@ -255,9 +252,6 @@ safe_mode_t port_init(void) {
255252
esp_rom_install_uart_printf();
256253
#endif
257254

258-
heap = NULL;
259-
heap_size = 0;
260-
261255
#define pin_GPIOn(n) pin_GPIO##n
262256
#define pin_GPIOn_EXPAND(x) pin_GPIOn(x)
263257

@@ -329,7 +323,16 @@ void *port_malloc(size_t size, bool dma_capable) {
329323
if (dma_capable) {
330324
caps |= MALLOC_CAP_DMA;
331325
}
332-
return heap_caps_malloc(size, caps);
326+
327+
void *ptr = NULL;
328+
// Try SPIRAM first when available.
329+
#ifdef CONFIG_SPIRAM
330+
ptr = heap_caps_malloc(size, caps | MALLOC_CAP_SPIRAM);
331+
#endif
332+
if (ptr == NULL) {
333+
ptr = heap_caps_malloc(size, caps);
334+
}
335+
return ptr;
333336
}
334337

335338
void port_free(void *ptr) {
@@ -341,7 +344,7 @@ void *port_realloc(void *ptr, size_t size) {
341344
}
342345

343346
size_t port_heap_get_largest_free_size(void) {
344-
size_t free_size = heap_caps_get_largest_free_block(0);
347+
size_t free_size = heap_caps_get_largest_free_block(MALLOC_CAP_8BIT);
345348
return free_size;
346349
}
347350

0 commit comments

Comments
 (0)