From f25e788fcf06f64f4f42cf266a9eb63fe83af680 Mon Sep 17 00:00:00 2001 From: Reinis Veips Date: Mon, 14 Mar 2022 23:36:55 +0200 Subject: [PATCH 1/3] Use statically allocated lib_deflate_reader instance instead of dynamically allocating it for every png decoding --- .../components/driver_framebuffer/png/deflate_reader.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/ports/esp32/components/driver_framebuffer/png/deflate_reader.c b/ports/esp32/components/driver_framebuffer/png/deflate_reader.c index 45770254b04f..4c745bb11e55 100644 --- a/ports/esp32/components/driver_framebuffer/png/deflate_reader.c +++ b/ports/esp32/components/driver_framebuffer/png/deflate_reader.c @@ -165,10 +165,15 @@ lib_deflate_init(struct lib_deflate_reader *dr, lib_reader_read_t read, void *re dr->read_p = read_p; } +//statically allocated deflate reader eases pressure on heap fragmentation +//the size of this struct is quite large, which required large reserved dynamic +//memory +static struct lib_deflate_reader _reader; + struct lib_deflate_reader * lib_deflate_new(lib_reader_read_t read, void *read_p) { - struct lib_deflate_reader *dr = (struct lib_deflate_reader *) malloc(sizeof(struct lib_deflate_reader)); + struct lib_deflate_reader *dr = &_reader; if (unlikely(dr == NULL)) return NULL; @@ -526,5 +531,5 @@ lib_deflate_read(struct lib_deflate_reader *dr, uint8_t *buf, size_t buf_len) void lib_deflate_destroy(struct lib_deflate_reader *dr) { - free(dr); + //nothing to do for statically allocated deflate reader } From 7ccb8bf9f094cbbb0ba768bdb866f644108f34b7 Mon Sep 17 00:00:00 2001 From: Reinis Veips Date: Mon, 14 Mar 2022 23:37:40 +0200 Subject: [PATCH 2/3] Disable BT for airguard (saves some precious dynamic memory) --- ports/esp32/boards/AIRGUARD/mpconfigboard.h | 4 ++++ ports/esp32/boards/AIRGUARD/sdkconfig | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/ports/esp32/boards/AIRGUARD/mpconfigboard.h b/ports/esp32/boards/AIRGUARD/mpconfigboard.h index 644807f78e60..3dfffa9d93a5 100644 --- a/ports/esp32/boards/AIRGUARD/mpconfigboard.h +++ b/ports/esp32/boards/AIRGUARD/mpconfigboard.h @@ -1,2 +1,6 @@ #define MICROPY_HW_BOARD_NAME "ESP32 module" #define MICROPY_HW_MCU_NAME "ESP32" + +#define MICROPY_PY_BLUETOOTH (0) +#define MICROPY_BLUETOOTH_NIMBLE (0) + diff --git a/ports/esp32/boards/AIRGUARD/sdkconfig b/ports/esp32/boards/AIRGUARD/sdkconfig index 8bfa42cf93ec..78c44486d645 100644 --- a/ports/esp32/boards/AIRGUARD/sdkconfig +++ b/ports/esp32/boards/AIRGUARD/sdkconfig @@ -10,3 +10,7 @@ CONFIG_I2C_ADDR_SSD1306=60 CONFIG_PIN_NUM_SSD1306_RESET=-1 CONFIG_DRIVER_FRAMEBUFFER_ENABLE=y + +# disabling bt saves a couple kb of ram +CONFIG_BT_ENABLED=n +CONFIG_BT_NIMBLE_ENABLED=n From 3dd35485b5864e2804c425f0887af93eeaeded16 Mon Sep 17 00:00:00 2001 From: Reinis Veips Date: Mon, 14 Mar 2022 23:38:05 +0200 Subject: [PATCH 3/3] Don't reserve 36KiB of dynamic memory (it was mostly reserved for lib_deflate_reader) --- ports/esp32/main.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/ports/esp32/main.c b/ports/esp32/main.c index 66d772ddc121..75143ab2a0b5 100644 --- a/ports/esp32/main.c +++ b/ports/esp32/main.c @@ -142,11 +142,15 @@ void mp_task(void *pvParameter) { } #else // Allocate the uPy heap using malloc and get the largest available region - size_t mp_task_heap_size = heap_caps_get_largest_free_block(MALLOC_CAP_8BIT) - 36*1024; + size_t mp_task_heap_size = heap_caps_get_largest_free_block(MALLOC_CAP_8BIT); void *mp_task_heap = malloc(mp_task_heap_size); #endif + multi_heap_info_t info; soft_reset: + heap_caps_get_info(&info, MALLOC_CAP_8BIT); + printf("C heap: total=%d, free=%d, allocd=%d, mpy heap=%d bytes\n", info.total_free_bytes + info.total_allocated_bytes, + info.total_free_bytes, info.total_allocated_bytes, mp_task_heap_size); // initialise the stack pointer for the main thread mp_stack_set_top((void *)sp); mp_stack_set_limit(MP_TASK_STACK_SIZE - MP_TASK_STACK_LIMIT_MARGIN);