Commit f0595635 authored by Paul Sokolovsky's avatar Paul Sokolovsky

stmhal/modutime: Refactor to use extmod/utime_mphal.c.

This includes making sure that utime_mphal's sleep_ms() and sleep_us()
don't sleep on negative arguments.
parent a9728442
...@@ -62,6 +62,7 @@ ...@@ -62,6 +62,7 @@
#include "portmodules.h" #include "portmodules.h"
#include "modmachine.h" #include "modmachine.h"
#include "extmod/fsusermount.h" #include "extmod/fsusermount.h"
#include "extmod/utime_mphal.h"
/// \function millis() /// \function millis()
/// Returns the number of milliseconds since the board was last reset. /// Returns the number of milliseconds since the board was last reset.
...@@ -162,8 +163,8 @@ STATIC const mp_map_elem_t pyb_module_globals_table[] = { ...@@ -162,8 +163,8 @@ STATIC const mp_map_elem_t pyb_module_globals_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR_elapsed_millis), (mp_obj_t)&pyb_elapsed_millis_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_elapsed_millis), (mp_obj_t)&pyb_elapsed_millis_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_micros), (mp_obj_t)&pyb_micros_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_micros), (mp_obj_t)&pyb_micros_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_elapsed_micros), (mp_obj_t)&pyb_elapsed_micros_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_elapsed_micros), (mp_obj_t)&pyb_elapsed_micros_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_delay), (mp_obj_t)&time_sleep_ms_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_delay), (mp_obj_t)&mp_utime_sleep_ms_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_udelay), (mp_obj_t)&time_sleep_us_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_udelay), (mp_obj_t)&mp_utime_sleep_us_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_sync), (mp_obj_t)&mod_os_sync_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_sync), (mp_obj_t)&mod_os_sync_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_mount), (mp_obj_t)&fsuser_mount_obj }, { MP_OBJ_NEW_QSTR(MP_QSTR_mount), (mp_obj_t)&fsuser_mount_obj },
......
...@@ -31,6 +31,7 @@ ...@@ -31,6 +31,7 @@
#include "py/nlr.h" #include "py/nlr.h"
#include "py/smallint.h" #include "py/smallint.h"
#include "py/obj.h" #include "py/obj.h"
#include "extmod/utime_mphal.h"
#include "systick.h" #include "systick.h"
#include "timeutils.h" #include "timeutils.h"
#include "portmodules.h" #include "portmodules.h"
...@@ -129,51 +130,6 @@ STATIC mp_obj_t time_time(void) { ...@@ -129,51 +130,6 @@ STATIC mp_obj_t time_time(void) {
} }
MP_DEFINE_CONST_FUN_OBJ_0(time_time_obj, time_time); 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 mp_obj_t time_ticks_cpu(void) {
static bool enabled = false; static bool enabled = false;
if (!enabled) { if (!enabled) {
...@@ -186,27 +142,19 @@ STATIC mp_obj_t time_ticks_cpu(void) { ...@@ -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_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[] = { 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___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_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_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_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), (mp_obj_t)&mp_utime_sleep_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_sleep_ms), (mp_obj_t)&time_sleep_ms_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)&time_sleep_us_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)&time_ticks_ms_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)&time_ticks_us_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_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); STATIC MP_DEFINE_CONST_DICT(time_module_globals, time_module_globals_table);
......
...@@ -94,6 +94,7 @@ ...@@ -94,6 +94,7 @@
#define MICROPY_PY_URE (1) #define MICROPY_PY_URE (1)
#define MICROPY_PY_UHEAPQ (1) #define MICROPY_PY_UHEAPQ (1)
#define MICROPY_PY_UHASHLIB (1) #define MICROPY_PY_UHASHLIB (1)
#define MICROPY_PY_UTIME_MP_HAL (1)
#define MICROPY_PY_MACHINE (1) #define MICROPY_PY_MACHINE (1)
#define MICROPY_PY_MACHINE_PULSE (1) #define MICROPY_PY_MACHINE_PULSE (1)
#define MICROPY_PY_MACHINE_I2C (1) #define MICROPY_PY_MACHINE_I2C (1)
......
...@@ -37,6 +37,7 @@ void mp_hal_set_interrupt_char(int c); // -1 to disable ...@@ -37,6 +37,7 @@ void mp_hal_set_interrupt_char(int c); // -1 to disable
// timing functions // timing functions
#include "stmhal/systick.h" #include "stmhal/systick.h"
#define mp_hal_delay_ms HAL_Delay #define mp_hal_delay_ms HAL_Delay
#define mp_hal_delay_us(us) sys_tick_udelay(us)
#define mp_hal_delay_us_fast(us) sys_tick_udelay(us) #define mp_hal_delay_us_fast(us) sys_tick_udelay(us)
#define mp_hal_ticks_ms HAL_GetTick #define mp_hal_ticks_ms HAL_GetTick
#define mp_hal_ticks_us() sys_tick_get_microseconds() #define mp_hal_ticks_us() sys_tick_get_microseconds()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment