Skip to content

Commit e3955f4

Browse files
projectgusdpgeorge
authored andcommitted
esp32: Fix thread stack limit margin, change to new cstack API.
This change moves that complexity out into the stack checker and fixes the bug where stack margin wasn't set correctly by ESP32-C3 threads. This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <[email protected]>
1 parent 86f2c28 commit e3955f4

File tree

3 files changed

+8
-14
lines changed

3 files changed

+8
-14
lines changed

ports/esp32/main.c

+2-10
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
#include "esp_log.h"
4242
#include "esp_psram.h"
4343

44-
#include "py/stackctrl.h"
44+
#include "py/cstack.h"
4545
#include "py/nlr.h"
4646
#include "py/compile.h"
4747
#include "py/runtime.h"
@@ -71,13 +71,6 @@
7171
// MicroPython runs as a task under FreeRTOS
7272
#define MP_TASK_PRIORITY (ESP_TASK_PRIO_MIN + 1)
7373

74-
// Set the margin for detecting stack overflow, depending on the CPU architecture.
75-
#if CONFIG_IDF_TARGET_ESP32C3
76-
#define MP_TASK_STACK_LIMIT_MARGIN (2048)
77-
#else
78-
#define MP_TASK_STACK_LIMIT_MARGIN (1024)
79-
#endif
80-
8174
typedef struct _native_code_node_t {
8275
struct _native_code_node_t *next;
8376
uint32_t data[];
@@ -132,8 +125,7 @@ void mp_task(void *pvParameter) {
132125

133126
soft_reset:
134127
// initialise the stack pointer for the main thread
135-
mp_stack_set_top((void *)sp);
136-
mp_stack_set_limit(MICROPY_TASK_STACK_SIZE - MP_TASK_STACK_LIMIT_MARGIN);
128+
mp_cstack_init_with_top((void *)sp, MICROPY_TASK_STACK_SIZE);
137129
gc_init(mp_task_heap, mp_task_heap + MICROPY_GC_INITIAL_HEAP_SIZE);
138130
mp_init();
139131
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_lib));

ports/esp32/mpconfigport.h

+5
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@
6262
// Python internal features
6363
#define MICROPY_READER_VFS (1)
6464
#define MICROPY_ENABLE_GC (1)
65+
#if CONFIG_IDF_TARGET_ARCH_RISCV // RISC-V SoCs use more stack than Xtensa
66+
#define MICROPY_STACK_CHECK_MARGIN (2048) // This may be unnecessarily conservative
67+
#else
68+
#define MICROPY_STACK_CHECK_MARGIN (1024)
69+
#endif
6570
#define MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF (1)
6671
#define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_MPZ)
6772
#define MICROPY_ERROR_REPORTING (MICROPY_ERROR_REPORTING_NORMAL)

ports/esp32/mpthreadport.c

+1-4
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
#if MICROPY_PY_THREAD
3939

4040
#define MP_THREAD_MIN_STACK_SIZE (4 * 1024)
41-
#define MP_THREAD_DEFAULT_STACK_SIZE (MP_THREAD_MIN_STACK_SIZE + 1024)
41+
#define MP_THREAD_DEFAULT_STACK_SIZE (MP_THREAD_MIN_STACK_SIZE + MICROPY_STACK_CHECK_MARGIN)
4242
#define MP_THREAD_PRIORITY (ESP_TASK_PRIO_MIN + 1)
4343

4444
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 2, 0) && !CONFIG_FREERTOS_ENABLE_STATIC_TASK_CLEAN_UP
@@ -160,9 +160,6 @@ mp_uint_t mp_thread_create_ex(void *(*entry)(void *), void *arg, size_t *stack_s
160160
th->next = thread;
161161
thread = th;
162162

163-
// adjust the stack_size to provide room to recover from hitting the limit
164-
*stack_size -= 1024;
165-
166163
mp_thread_mutex_unlock(&thread_mutex);
167164

168165
return (mp_uint_t)th->id;

0 commit comments

Comments
 (0)