Skip to content

Commit 882ec01

Browse files
committed
stmhal: Initial implementation of multithreading, currently disabled.
This patch brings the _thread module to stmhal/pyboard. There is a very simple round-robin thread scheduler, which is disabled if there is only one thread (for efficiency when threading is not used). The scheduler currently switches threads at a rate of 250Hz using the systick timer and the pend-SV interrupt. The GIL is disabled so one must be careful to use lock objects to prevent concurrent access of objects. The threading is disabled by default, one can enabled it with the config option MICROPY_PY_THREAD to test it out.
1 parent bebb3a6 commit 882ec01

10 files changed

Lines changed: 392 additions & 5 deletions

File tree

stmhal/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,11 @@ SRC_C = \
131131
usbd_hid_interface.c \
132132
usbd_msc_storage.c \
133133
mphalport.c \
134+
mpthreadport.c \
134135
irq.c \
135136
pendsv.c \
136137
systick.c \
138+
pybthread.c \
137139
timer.c \
138140
led.c \
139141
pin.c \

stmhal/gccollect.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@
2727
#include <stdio.h>
2828
#include <stdint.h>
2929

30+
#include "py/mpstate.h"
3031
#include "py/obj.h"
3132
#include "py/gc.h"
33+
#include "py/mpthread.h"
3234
#include "gccollect.h"
3335
#include "systick.h"
3436

@@ -48,7 +50,16 @@ void gc_collect(void) {
4850
mp_uint_t sp = gc_helper_get_regs_and_sp(regs);
4951

5052
// trace the stack, including the registers (since they live on the stack in this function)
53+
#if MICROPY_PY_THREAD
54+
gc_collect_root((void**)sp, ((uint32_t)MP_STATE_THREAD(stack_top) - sp) / sizeof(uint32_t));
55+
#else
5156
gc_collect_root((void**)sp, ((uint32_t)&_ram_end - sp) / sizeof(uint32_t));
57+
#endif
58+
59+
// trace root pointers from any threads
60+
#if MICROPY_PY_THREAD
61+
mp_thread_gc_others();
62+
#endif
5263

5364
// end the GC
5465
gc_collect_end();

stmhal/main.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343

4444
#include "systick.h"
4545
#include "pendsv.h"
46+
#include "pybthread.h"
4647
#include "gccollect.h"
4748
#include "readline.h"
4849
#include "modmachine.h"
@@ -67,6 +68,7 @@
6768

6869
void SystemClock_Config(void);
6970

71+
pyb_thread_t pyb_thread_main;
7072
fs_user_mount_t fs_user_mount_flash;
7173
mp_vfs_mount_t mp_vfs_mount_flash;
7274

@@ -419,11 +421,6 @@ STATIC uint update_reset_mode(uint reset_mode) {
419421
int main(void) {
420422
// TODO disable JTAG
421423

422-
// Stack limit should be less than real stack size, so we have a chance
423-
// to recover from limit hit. (Limit is measured in bytes.)
424-
mp_stack_ctrl_init();
425-
mp_stack_set_limit((char*)&_ram_end - (char*)&_heap_end - 1024);
426-
427424
/* STM32F4xx HAL library initialization:
428425
- Configure the Flash prefetch, instruction and Data caches
429426
- Configure the Systick to generate an interrupt each 1 msec
@@ -457,6 +454,7 @@ int main(void) {
457454
#endif
458455

459456
// basic sub-system init
457+
pyb_thread_init(&pyb_thread_main);
460458
pendsv_init();
461459
led_init();
462460
#if MICROPY_HW_HAS_SWITCH
@@ -502,6 +500,17 @@ int main(void) {
502500
storage_init();
503501
}
504502

503+
// Python threading init
504+
#if MICROPY_PY_THREAD
505+
mp_thread_init();
506+
#endif
507+
508+
// Stack limit should be less than real stack size, so we have a chance
509+
// to recover from limit hit. (Limit is measured in bytes.)
510+
// Note: stack control relies on main thread being initialised above
511+
mp_stack_ctrl_init();
512+
mp_stack_set_limit((char*)&_ram_end - (char*)&_heap_end - 1024);
513+
505514
// GC init
506515
gc_init(&_heap_start, &_heap_end);
507516

stmhal/mpconfigport.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@
102102
#define MICROPY_PY_SYS_PLATFORM "pyboard"
103103
#endif
104104
#define MICROPY_PY_UERRNO (1)
105+
#define MICROPY_PY_THREAD (0)
106+
#define MICROPY_PY_THREAD_GIL (0)
105107

106108
// extended modules
107109
#define MICROPY_PY_UCTYPES (1)

stmhal/mpthreadport.c

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2016 Damien P. George
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include <stdio.h>
28+
29+
#include "py/mpconfig.h"
30+
#include "py/mpstate.h"
31+
#include "py/gc.h"
32+
#include "py/mpthread.h"
33+
#include "gccollect.h"
34+
35+
#if MICROPY_PY_THREAD
36+
37+
// the mutex controls access to the linked list
38+
STATIC mp_thread_mutex_t thread_mutex;
39+
40+
void mp_thread_init(void) {
41+
mp_thread_mutex_init(&thread_mutex);
42+
mp_thread_set_state(&mp_state_ctx.thread);
43+
}
44+
45+
void mp_thread_gc_others(void) {
46+
mp_thread_mutex_lock(&thread_mutex, 1);
47+
gc_collect_root((void**)&pyb_thread_cur, 1);
48+
for (pyb_thread_t *th = pyb_thread_cur;; th = th->next) {
49+
gc_collect_root(&th->arg, 1);
50+
if (th != pyb_thread_cur) {
51+
gc_collect_root(th->stack, th->stack_len);
52+
}
53+
if (th->next == pyb_thread_cur) {
54+
break;
55+
}
56+
}
57+
mp_thread_mutex_unlock(&thread_mutex);
58+
}
59+
60+
void mp_thread_create(void *(*entry)(void*), void *arg, size_t *stack_size) {
61+
if (*stack_size == 0) {
62+
*stack_size = 4096; // default stack size
63+
} else if (*stack_size < 2048) {
64+
*stack_size = 2048; // minimum stack size
65+
}
66+
67+
// round stack size to a multiple of the word size
68+
size_t stack_len = *stack_size / sizeof(uint32_t);
69+
*stack_size = stack_len * sizeof(uint32_t);
70+
71+
// allocate stack and linked-list node (must be done outside thread_mutex lock)
72+
uint32_t *stack = m_new(uint32_t, stack_len);
73+
pyb_thread_t *th = m_new_obj(pyb_thread_t);
74+
75+
mp_thread_mutex_lock(&thread_mutex, 1);
76+
77+
// create thread
78+
uint32_t id = pyb_thread_new(th, stack, stack_len, entry, arg);
79+
if (id == 0) {
80+
mp_thread_mutex_unlock(&thread_mutex);
81+
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, "can't create thread"));
82+
}
83+
84+
mp_thread_mutex_unlock(&thread_mutex);
85+
86+
// adjust stack_size to provide room to recover from hitting the limit
87+
*stack_size -= 1024;
88+
}
89+
90+
void mp_thread_start(void) {
91+
}
92+
93+
void mp_thread_finish(void) {
94+
}
95+
96+
void mp_thread_mutex_init(mp_thread_mutex_t *mutex) {
97+
*mutex = 0;
98+
}
99+
100+
int mp_thread_mutex_lock(mp_thread_mutex_t *mutex, int wait) {
101+
uint32_t irq_state = disable_irq();
102+
if (*mutex) {
103+
// mutex is locked
104+
if (!wait) {
105+
enable_irq(irq_state);
106+
return 0; // failed to lock mutex
107+
}
108+
while (*mutex) {
109+
enable_irq(irq_state);
110+
pyb_thread_yield();
111+
irq_state = disable_irq();
112+
}
113+
}
114+
*mutex = 1;
115+
enable_irq(irq_state);
116+
return 1; // have mutex
117+
}
118+
119+
void mp_thread_mutex_unlock(mp_thread_mutex_t *mutex) {
120+
*mutex = 0;
121+
}
122+
123+
#endif // MICROPY_PY_THREAD

stmhal/mpthreadport.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2016 Damien P. George
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
#ifndef __MICROPY_INCLUDED_STMHAL_MPTHREADPORT_H__
27+
#define __MICROPY_INCLUDED_STMHAL_MPTHREADPORT_H__
28+
29+
#include "py/mpthread.h"
30+
#include "pybthread.h"
31+
32+
typedef uint32_t mp_thread_mutex_t;
33+
34+
void mp_thread_init(void);
35+
void mp_thread_gc_others(void);
36+
37+
static inline void mp_thread_set_state(void *state) {
38+
pyb_thread_set_local(state);
39+
}
40+
41+
static inline struct _mp_state_thread_t *mp_thread_get_state(void) {
42+
return pyb_thread_get_local();
43+
}
44+
45+
#endif // __MICROPY_INCLUDED_STMHAL_MPTHREADPORT_H__

stmhal/pendsv.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,35 @@ void pendsv_isr_handler(void) {
8989
// sp[1]: 0xfffffff9
9090
// sp[0]: ?
9191

92+
#if MICROPY_PY_THREAD
93+
__asm volatile (
94+
"ldr r1, pendsv_object_ptr\n"
95+
"ldr r0, [r1]\n"
96+
"cmp r0, 0\n"
97+
"beq .no_obj\n"
98+
"str r0, [sp, #0]\n" // store to r0 on stack
99+
"mov r0, #0\n"
100+
"str r0, [r1]\n" // clear pendsv_object
101+
"ldr r0, nlr_jump_ptr\n"
102+
"str r0, [sp, #24]\n" // store to pc on stack
103+
"bx lr\n" // return from interrupt; will return to nlr_jump
104+
105+
".no_obj:\n" // pendsv_object==NULL
106+
"push {r4-r11, lr}\n"
107+
"vpush {s16-s31}\n"
108+
"mov r0, sp\n" // pass sp to save
109+
"mov r4, lr\n" // save lr because we are making a call
110+
"bl pyb_thread_next\n" // get next thread to execute
111+
"mov lr, r4\n" // restore lr
112+
"mov sp, r0\n" // switch stacks
113+
"vpop {s16-s31}\n"
114+
"pop {r4-r11, lr}\n"
115+
"bx lr\n" // return from interrupt; will return to new thread
116+
".align 2\n"
117+
"pendsv_object_ptr: .word pendsv_object\n"
118+
"nlr_jump_ptr: .word nlr_jump\n"
119+
);
120+
#else
92121
__asm volatile (
93122
"ldr r0, pendsv_object_ptr\n"
94123
"ldr r0, [r0]\n"
@@ -108,6 +137,7 @@ void pendsv_isr_handler(void) {
108137
"pendsv_object_ptr: .word pendsv_object\n"
109138
"nlr_jump_ptr: .word nlr_jump\n"
110139
);
140+
#endif
111141

112142
/*
113143
uint32_t x[2] = {0x424242, 0xdeaddead};

0 commit comments

Comments
 (0)