8000 zephyr: Add threading support. · cuatrobits/micropython@6833f3d · GitHub
[go: up one dir, main page]

Skip to content

Commit 6833f3d

Browse files
danicamporadpgeorge
authored andcommitted
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>
1 parent aefd48b commit 6833f3d

File tree

9 files changed

+419
-3
lines changed

9 files changed

+419
-3
lines changed

ports/zephyr/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ set(MICROPY_SOURCE_PORT
4747
mphalport.c
4848
uart_core.c
4949
zephyr_storage.c
50+
mpthreadport.c
5051
)
5152
list(TRANSFORM MICROPY_SOURCE_PORT PREPEND ${MICROPY_PORT_DIR}/)
5253

ports/zephyr/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ To build for QEMU instead:
6363

6464
$ west build -b qemu_x86 ~/micropython/ports/zephyr
6565

66+
To build any board with the `_thread` module enabled,
67+
add `-DOVERLAY_CONFIG=thread.conf`, for instance:
68+
69+
$ west build -b frdm_k64f ~/micropython/ports/zephyr -DOVERLAY_CONFIG=thread.conf
70+
6671
Consult the Zephyr documentation above for the list of
6772
supported boards. Board configuration files appearing in `ports/zephyr/boards/`
6873
correspond to boards that have been tested with MicroPython and may have
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
CONFIG_NETWORKING=n
2+
CONFIG_BT=y
3+
CONFIG_BT_DEVICE_NAME_DYNAMIC=y
4+
CONFIG_BT_PERIPHERAL=y
5+
CONFIG_BT_CENTRAL=y
6+
7+
CONFIG_MICROPY_HEAP_SIZE=98304
8+
CONFIG_MAIN_STACK_SIZE=8192
9+
10+
# CONFIG_DYNAMIC_THREAD=y
11+
CONFIG_THREAD_CUSTOM_DATA=y
12+
CONFIG_THREAD_MONITOR=y
13+
CONFIG_THREAD_STACK_INFO=y

ports/zephyr/main.c

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,20 @@ static void vfs_init(void) {
114114
#endif // MICROPY_VFS
115115

116116
int real_main(void) {
117-
mp_stack_ctrl_init();
118-
// Make MicroPython's stack limit somewhat smaller than full stack available
119-
mp_stack_set_limit(CONFIG_MAIN_STACK_SIZE - 512);
117+
volatile int stack_dummy = 0;
118+
119+
#if MICROPY_PY_THREAD
120+
struct k_thread *z_thread = (struct k_thread *)k_current_get();
121+
mp_thread_init((void *)z_thread->stack_info.start, z_thread->stack_info.size / sizeof(uintptr_t));
122+
#endif
120123

121124
init_zephyr();
122125
mp_hal_init();
123126

124127
soft_reset:
128+
mp_stack_set_top((void *)&stack_dummy);
129+
// Make MicroPython's stack limit somewhat smaller than full stack available
130+
mp_stack_set_limit(CONFIG_MAIN_STACK_SIZE - 512);
125131
#if MICROPY_ENABLE_GC
126132
gc_init(heap, heap + sizeof(heap));
127133
#endif
@@ -160,6 +166,14 @@ int real_main(void) {
160166
machine_pin_deinit();
161167
#endif
162168

169+
#if MICROPY_PY_THREAD
170+
mp_thread_deinit();
171+
gc_collect();
172+
#endif
173+
174+
gc_sweep_all();
175+
mp_deinit();
176+
163177
goto soft_reset;
164178

165179
return 0;
@@ -171,6 +185,9 @@ void gc_collect(void) {
171185
void *dummy;
172186
gc_collect_start();
173187
gc_collect_root(&dummy, ((mp_uint_t)MP_STATE_THREAD(stack_top) - (mp_uint_t)&dummy) / sizeof(mp_uint_t));
188+
#if MICROPY_PY_THREAD
189+
mp_thread_gc_others();
190+
#endif
174191
gc_collect_end();
175192
}
176193

ports/zephyr/mpconfigport.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,13 @@
113113
#define MICROPY_COMP_CONST (0)
114114
#define MICROPY_COMP_DOUBLE_TUPLE_ASSIGN (0)
115115

116+
// When CONFIG_THREAD_CUSTOM_DATA is enabled, MICROPY_PY_THREAD is enabled automatically
117+
#ifdef CONFIG_THREAD_CUSTOM_DATA
118+
#define MICROPY_PY_THREAD (1)
119+
#define MICROPY_PY_THREAD_GIL (1)
120+
#define MICROPY_PY_THREAD_GIL_VM_DIVISOR (32)
121+
#endif
122+
116123
void mp_hal_signal_event(void);
117124
#define MICROPY_SCHED_HOOK_SCHEDULED mp_hal_signal_event()
118125

@@ -139,3 +146,21 @@ typedef long mp_off_t;
139146
// extra built in names to add to the global namespace
140147
#define MICROPY_PORT_BUILTINS \
141148
{ MP_ROM_QSTR(MP_QSTR_open), MP_ROM_PTR(&mp_builtin_open_obj) },
149+
150+
#if MICROPY_PY_THREAD
151+
#define MICROPY_EVENT_POLL_HOOK \
152+
do { \
153+
extern void mp_handle_pending(bool); \
154+
mp_handle_pending(true); \
155+
MP_THREAD_GIL_EXIT(); \
156+
k_msleep(1); \
157+
MP_THREAD_GIL_ENTER(); \
158+
} while (0);
159+
#else
160+
#define MICROPY_EVENT_POLL_HOOK \
161+
do { \
162+
extern void mp_handle_pending(bool); \
163+
mp_handle_pending(true); \
164+
k_msleep(1); \
165+
} while (0);
166+
#endif

ports/zephyr/mpconfigport_minimal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,5 @@ typedef unsigned mp_uint_t; // must be pointer size
8383
typedef long mp_off_t;
8484

8585
#define MP_STATE_PORT MP_STATE_VM
86+
87+
#define MICROPY_EVENT_POLL_HOOK

0 commit comments

Comments
 (0)
0