Skip to content

Commit d311655

Browse files
committed
stmhal: Add time module with sleep function.
1 parent ad7b84a commit d311655

5 files changed

Lines changed: 53 additions & 1 deletion

File tree

stmhal/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ SRC_C = \
7474
pyexec.c \
7575
pybmodule.c \
7676
osmodule.c \
77+
timemodule.c \
7778
import.c \
7879
lexerfatfs.c \
7980
gpio.c \

stmhal/main.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "pyexec.h"
2424
#include "pybmodule.h"
2525
#include "osmodule.h"
26+
#include "timemodule.h"
2627
#include "usart.h"
2728
#include "led.h"
2829
#include "exti.h"
@@ -276,9 +277,10 @@ int main(void) {
276277
// probably shouldn't do this, so we are compatible with CPython
277278
rt_store_name(MP_QSTR_pyb, (mp_obj_t)&pyb_module);
278279

279-
// pre-import the os module
280+
// pre-import the os and time modules
280281
// TODO don't do this! (need a way of registering builtin modules...)
281282
rt_store_name(MP_QSTR_os, (mp_obj_t)&os_module);
283+
rt_store_name(MP_QSTR_time, (mp_obj_t)&time_module);
282284

283285
// check if user switch held (initiates reset of filesystem)
284286
bool reset_filesystem = false;

stmhal/qstrdefsport.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,7 @@ Q(rmdir)
6666
Q(unlink)
6767
Q(sep)
6868
Q(urandom)
69+
70+
// for time module
71+
Q(time)
72+
Q(sleep)

stmhal/timemodule.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include <stm32f4xx_hal.h>
2+
3+
#include "nlr.h"
4+
#include "misc.h"
5+
#include "mpconfig.h"
6+
#include "qstr.h"
7+
#include "obj.h"
8+
#include "map.h"
9+
#include "timemodule.h"
10+
11+
STATIC mp_obj_t time_sleep(mp_obj_t seconds_o) {
12+
#if MICROPY_ENABLE_FLOAT
13+
if (MP_OBJ_IS_INT(seconds_o)) {
14+
#endif
15+
HAL_Delay(1000 * mp_obj_get_int(seconds_o));
16+
#if MICROPY_ENABLE_FLOAT
17+
} else {
18+
HAL_Delay((uint32_t)(1000 * mp_obj_get_float(seconds_o)));
19+
}
20+
#endif
21+
return mp_const_none;
22+
}
23+
24+
MP_DEFINE_CONST_FUN_OBJ_1(time_sleep_obj, time_sleep);
25+
26+
STATIC const mp_map_elem_t time_module_globals_table[] = {
27+
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_time) },
28+
29+
{ MP_OBJ_NEW_QSTR(MP_QSTR_sleep), (mp_obj_t)&time_sleep_obj },
30+
};
31+
32+
STATIC const mp_map_t time_module_globals = {
33+
.all_keys_are_qstrs = 1,
34+
.table_is_fixed_array = 1,
35+
.used = sizeof(time_module_globals_table) / sizeof(mp_map_elem_t),
36+
.alloc = sizeof(time_module_globals_table) / sizeof(mp_map_elem_t),
37+
.table = (mp_map_elem_t*)time_module_globals_table,
38+
};
39+
40+
const mp_obj_module_t time_module = {
41+
.base = { &mp_type_module },
42+
.name = MP_QSTR_time,
43+
.globals = (mp_map_t*)&time_module_globals,
44+
};

stmhal/timemodule.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
extern const mp_obj_module_t time_module;

0 commit comments

Comments
 (0)