Skip to content

Commit 77c9eb7

Browse files
committed
esp32: Add "Free RAM" optimisation config flags.
This is necessary for ESP32-C2 Wi-Fi & BT to work reliably (and for TLS to work at all). On IDF 5.4.2 the free static RAM goes from 60KB to 100KB, and there will also be a reduction in lwIP & Wi-Fi memory use at runtime. The performance trade-off seems low for most use cases, although it will probably be significant for certain combinations of load (i.e. heavy TCP/IP, heavy BT throughput, and some peripheral driver functions). Added as a set of config flags because this is potentially useful on other SoCs where the goal is to maximise RAM available for MicroPython. This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <[email protected]>
1 parent ca99169 commit 77c9eb7

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

ports/esp32/boards/ESP32_GENERIC_C2/mpconfigboard.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@ set(SDKCONFIG_DEFAULTS
44
boards/sdkconfig.base
55
boards/sdkconfig.ble
66
boards/sdkconfig.c2
7+
# C2 has unusably low free RAM without these optimisations
8+
boards/sdkconfig.free_ram
79
)

ports/esp32/boards/sdkconfig.free_ram

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# This is a collection of sdkconfig settings that frees RAM at runtime,
2+
# at the expense of performance.
3+
#
4+
# Not all options will work on all SoC families, but adding this sdkconfig
5+
# set to a board should increase the free memory.
6+
#
7+
# - Many options free IRAM, which on most ESP32 families leads to
8+
# free DRAM at runtime (original ESP32 and S2 may not).
9+
# - The other options reduce runtime DRAM usage from the heap.
10+
#
11+
# IMPORTANT: If you enable these config settings on a custom build then you may
12+
# encounter bugs or crashes. If you choose to open a MicroPython bug report then
13+
# please mention these config settings!
14+
15+
# Place functions in flash whenever possible to free IRAM
16+
CONFIG_RINGBUF_PLACE_FUNCTIONS_INTO_FLASH=y
17+
CONFIG_FREERTOS_PLACE_FUNCTIONS_INTO_FLASH=y
18+
CONFIG_HEAP_PLACE_FUNCTION_INTO_FLASH=y
19+
20+
# Use the SPI flash functions in ROM (when available). This may limit flash chip
21+
# support and cause issues with some flash chips. Each SoC family has different
22+
# set of chip support baked into ROM.
23+
CONFIG_SPI_FLASH_ROM_IMPL=y
24+
25+
# Run the Bluetooth controller from flash not IRAM
26+
CONFIG_BT_CTRL_RUN_IN_FLASH_ONLY=y
27+
28+
# lwIP adjustments to limit runtime memory usage (at expense of performance, and/or
29+
# a reduction in number of active connections).
30+
CONFIG_LWIP_TCPIP_RECVMBOX_SIZE=16
31+
CONFIG_LWIP_MAX_SOCKETS=6
32+
CONFIG_LWIP_MAX_ACTIVE_TCP=8
33+
34+
# These lwIP values are recommended to scale relative to the Wi-Fi buffer numbers
35+
CONFIG_LWIP_TCP_WND_DEFAULT=3072
36+
CONFIG_LWIP_TCP_SND_BUF_DEFAULT=3072
37+
38+
# Wi-Fi adjustments to reduce peak runtime memory usage, at expense of peak
39+
# performance
40+
CONFIG_ESP_WIFI_STATIC_RX_BUFFER_NUM=8
41+
CONFIG_ESP_WIFI_DYNAMIC_RX_BUFFER_NUM=12
42+
CONFIG_ESP_WIFI_DYNAMIC_TX_BUFFER_NUM=12
43+
CONFIG_ESP_WIFI_RX_MGMT_BUF_NUM_DEF=2
44+
CONFIG_ESP_WIFI_RX_BA_WIN=12

0 commit comments

Comments
 (0)