diff options
| author | danicampora <danicampora@gmail.com> | 2024-06-28 09:34:59 +0200 |
|---|---|---|
| committer | Damien George <damien@micropython.org> | 2024-09-06 20:42:56 +1000 |
| commit | 6833f3dda9a1557789d4675a05811cc9a1d2c1a6 (patch) | |
| tree | 604fb374dfa8719399eac3b1b14899c9019ce06e /ports/zephyr/main.c | |
| parent | aefd48b80118c45e43e591e73aea1cda778e3676 (diff) | |
zephyr: Add threading support.
This commit implements the `_thread` module on the zephyr port.
Due to the fact that we are still using a rather old version of Zephyr,
`CONFIG_DYNAMIC_THREAD` is not available and therefore the stack for
threads cannot be allocated dynamically, only at compile time. So for the
time being and for the purpose of this commit, a maximum of 4 Zephyr
threads (besides the main thread) can be created. Once we manage to update
to the latest version of Zephyr this won't be a problem anymore.
Configuration for the nrf52840dk is added as part of this change, because
this board was used to test the threading support.
The Zephyr option `CONFIG_THREAD_CUSTOM_DATA` is used to enable threading
on a per board basis. The `thread.conf` file is added as a convenient way
to enable threading.
Signed-off-by: danicampora <danicampora@gmail.com>
Diffstat (limited to 'ports/zephyr/main.c')
| -rw-r--r-- | ports/zephyr/main.c | 23 |
1 files changed, 20 insertions, 3 deletions
diff --git a/ports/zephyr/main.c b/ports/zephyr/main.c index c4a135aa5..47c954622 100644 --- a/ports/zephyr/main.c +++ b/ports/zephyr/main.c @@ -114,14 +114,20 @@ static void vfs_init(void) { #endif // MICROPY_VFS int real_main(void) { - mp_stack_ctrl_init(); - // Make MicroPython's stack limit somewhat smaller than full stack available - mp_stack_set_limit(CONFIG_MAIN_STACK_SIZE - 512); + volatile int stack_dummy = 0; + + #if MICROPY_PY_THREAD + struct k_thread *z_thread = (struct k_thread *)k_current_get(); + mp_thread_init((void *)z_thread->stack_info.start, z_thread->stack_info.size / sizeof(uintptr_t)); + #endif init_zephyr(); mp_hal_init(); soft_reset: + mp_stack_set_top((void *)&stack_dummy); + // Make MicroPython's stack limit somewhat smaller than full stack available + mp_stack_set_limit(CONFIG_MAIN_STACK_SIZE - 512); #if MICROPY_ENABLE_GC gc_init(heap, heap + sizeof(heap)); #endif @@ -160,6 +166,14 @@ soft_reset: machine_pin_deinit(); #endif + #if MICROPY_PY_THREAD + mp_thread_deinit(); + gc_collect(); + #endif + + gc_sweep_all(); + mp_deinit(); + goto soft_reset; return 0; @@ -171,6 +185,9 @@ void gc_collect(void) { void *dummy; gc_collect_start(); gc_collect_root(&dummy, ((mp_uint_t)MP_STATE_THREAD(stack_top) - (mp_uint_t)&dummy) / sizeof(mp_uint_t)); + #if MICROPY_PY_THREAD + mp_thread_gc_others(); + #endif gc_collect_end(); } |
