|
|
@ -31,6 +31,7 @@ |
|
|
|
#include "py/nlr.h" |
|
|
|
#include "py/smallint.h" |
|
|
|
#include "py/obj.h" |
|
|
|
#include "extmod/utime_mphal.h" |
|
|
|
#include "systick.h" |
|
|
|
#include "timeutils.h" |
|
|
|
#include "portmodules.h" |
|
|
@ -129,51 +130,6 @@ STATIC mp_obj_t time_time(void) { |
|
|
|
} |
|
|
|
MP_DEFINE_CONST_FUN_OBJ_0(time_time_obj, time_time); |
|
|
|
|
|
|
|
/// \function sleep(seconds)
|
|
|
|
/// Sleep for the given number of seconds. Seconds can be a floating-point number to
|
|
|
|
/// sleep for a fractional number of seconds.
|
|
|
|
STATIC mp_obj_t time_sleep(mp_obj_t seconds_o) { |
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT |
|
|
|
if (MP_OBJ_IS_INT(seconds_o)) { |
|
|
|
#endif |
|
|
|
HAL_Delay(1000 * mp_obj_get_int(seconds_o)); |
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT |
|
|
|
} else { |
|
|
|
HAL_Delay((uint32_t)(1000 * mp_obj_get_float(seconds_o))); |
|
|
|
} |
|
|
|
#endif |
|
|
|
return mp_const_none; |
|
|
|
} |
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(time_sleep_obj, time_sleep); |
|
|
|
|
|
|
|
STATIC mp_obj_t time_sleep_ms(mp_obj_t ms_in) { |
|
|
|
mp_int_t ms = mp_obj_get_int(ms_in); |
|
|
|
if (ms > 0) { |
|
|
|
HAL_Delay(ms); |
|
|
|
} |
|
|
|
return mp_const_none; |
|
|
|
} |
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(time_sleep_ms_obj, time_sleep_ms); |
|
|
|
|
|
|
|
STATIC mp_obj_t time_sleep_us(mp_obj_t usec_in) { |
|
|
|
mp_int_t usec = mp_obj_get_int(usec_in); |
|
|
|
if (usec > 0) { |
|
|
|
sys_tick_udelay(usec); |
|
|
|
} |
|
|
|
return mp_const_none; |
|
|
|
} |
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(time_sleep_us_obj, time_sleep_us); |
|
|
|
|
|
|
|
STATIC mp_obj_t time_ticks_ms(void) { |
|
|
|
return MP_OBJ_NEW_SMALL_INT(HAL_GetTick() & MP_SMALL_INT_POSITIVE_MASK); |
|
|
|
} |
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_0(time_ticks_ms_obj, time_ticks_ms); |
|
|
|
|
|
|
|
STATIC mp_obj_t time_ticks_us(void) { |
|
|
|
return MP_OBJ_NEW_SMALL_INT(sys_tick_get_microseconds() & MP_SMALL_INT_POSITIVE_MASK); |
|
|
|
} |
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_0(time_ticks_us_obj, time_ticks_us); |
|
|
|
|
|
|
|
STATIC mp_obj_t time_ticks_cpu(void) { |
|
|
|
static bool enabled = false; |
|
|
|
if (!enabled) { |
|
|
@ -186,27 +142,19 @@ STATIC mp_obj_t time_ticks_cpu(void) { |
|
|
|
} |
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_0(time_ticks_cpu_obj, time_ticks_cpu); |
|
|
|
|
|
|
|
STATIC mp_obj_t time_ticks_diff(mp_obj_t start_in, mp_obj_t end_in) { |
|
|
|
// we assume that the arguments come from ticks_xx so are small ints
|
|
|
|
uint32_t start = MP_OBJ_SMALL_INT_VALUE(start_in); |
|
|
|
uint32_t end = MP_OBJ_SMALL_INT_VALUE(end_in); |
|
|
|
return MP_OBJ_NEW_SMALL_INT((end - start) & MP_SMALL_INT_POSITIVE_MASK); |
|
|
|
} |
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(time_ticks_diff_obj, time_ticks_diff); |
|
|
|
|
|
|
|
STATIC const mp_map_elem_t time_module_globals_table[] = { |
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_utime) }, |
|
|
|
|
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_localtime), (mp_obj_t)&time_localtime_obj }, |
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_mktime), (mp_obj_t)&time_mktime_obj }, |
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_time), (mp_obj_t)&time_time_obj }, |
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_sleep), (mp_obj_t)&time_sleep_obj }, |
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_sleep_ms), (mp_obj_t)&time_sleep_ms_obj }, |
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_sleep_us), (mp_obj_t)&time_sleep_us_obj }, |
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_ticks_ms), (mp_obj_t)&time_ticks_ms_obj }, |
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_ticks_us), (mp_obj_t)&time_ticks_us_obj }, |
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_sleep), (mp_obj_t)&mp_utime_sleep_obj }, |
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_sleep_ms), (mp_obj_t)&mp_utime_sleep_ms_obj }, |
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_sleep_us), (mp_obj_t)&mp_utime_sleep_us_obj }, |
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_ticks_ms), (mp_obj_t)&mp_utime_ticks_ms_obj }, |
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_ticks_us), (mp_obj_t)&mp_utime_ticks_us_obj }, |
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_ticks_cpu), (mp_obj_t)&time_ticks_cpu_obj }, |
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_ticks_diff), (mp_obj_t)&time_ticks_diff_obj }, |
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_ticks_diff), (mp_obj_t)&mp_utime_ticks_diff_obj }, |
|
|
|
}; |
|
|
|
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(time_module_globals, time_module_globals_table); |
|
|
|