Skip to content

Commit ee3c9cc

Browse files
committed
esp32: Increase NimBLE task stack size and overflow detection headroom.
The Python BLE IRQ handler will most likely run on the NimBLE task, so its C stack must be large enough to accommodate reasonably complicated Python code (eg a few call depths). So increase this stack size. Also increase the headroom from 1024 to 2048 bytes. This is needed because (1) the esp32 architecture uses a fair amount of stack in general; and (2) by the time execution gets to setting the Python stack top via `mp_stack_set_top()` in this interlock code, about 600 bytes of stack are already used, which reduces the amount available for Python. Fixes issue micropython#12349. Signed-off-by: Damien George <[email protected]>
1 parent e72d038 commit ee3c9cc

File tree

3 files changed

+6
-2
lines changed

3 files changed

+6
-2
lines changed

extmod/modbluetooth.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1274,7 +1274,7 @@ STATIC mp_obj_t invoke_irq_handler(uint16_t event,
12741274
if (ts_orig == NULL) {
12751275
mp_thread_set_state(&ts);
12761276
mp_stack_set_top(&ts + 1); // need to include ts in root-pointer scan
1277-
mp_stack_set_limit(MICROPY_PY_BLUETOOTH_SYNC_EVENT_STACK_SIZE - 1024);
1277+
mp_stack_set_limit(MICROPY_PY_BLUETOOTH_SYNC_EVENT_STACK_SIZE);
12781278
ts.gc_lock_depth = 0;
12791279
ts.nlr_jump_callback_top = NULL;
12801280
ts.mp_pending_exception = MP_OBJ_NULL;

ports/esp32/boards/sdkconfig.ble

+4
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,7 @@ CONFIG_BT_NIMBLE_MAX_CONNECTIONS=4
1414
CONFIG_BT_NIMBLE_PINNED_TO_CORE_0=n
1515
CONFIG_BT_NIMBLE_PINNED_TO_CORE_1=y
1616
CONFIG_BT_NIMBLE_PINNED_TO_CORE=1
17+
18+
# Increase NimBLE task stack size from the default, because Python code
19+
# (BLE IRQ handlers) will most likely run on this task.
20+
CONFIG_BT_NIMBLE_TASK_STACK_SIZE=6144

ports/esp32/mpconfigport.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@
9595
#define MICROPY_PY_BLUETOOTH (1)
9696
#define MICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS (1)
9797
#define MICROPY_PY_BLUETOOTH_USE_SYNC_EVENTS_WITH_INTERLOCK (1)
98-
#define MICROPY_PY_BLUETOOTH_SYNC_EVENT_STACK_SIZE (CONFIG_BT_NIMBLE_TASK_STACK_SIZE)
98+
#define MICROPY_PY_BLUETOOTH_SYNC_EVENT_STACK_SIZE (CONFIG_BT_NIMBLE_TASK_STACK_SIZE - 2048)
9999
#define MICROPY_PY_BLUETOOTH_ENABLE_CENTRAL_MODE (1)
100100
#define MICROPY_PY_BLUETOOTH_ENABLE_PAIRING_BONDING (1)
101101
#define MICROPY_BLUETOOTH_NIMBLE (1)

0 commit comments

Comments
 (0)