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>
This commit is contained in:
danicampora
2024-06-28 09:34:59 +02:00
committed by Damien George
parent aefd48b801
commit 6833f3dda9
9 changed files with 419 additions and 3 deletions

View File

@@ -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();
}