|
|
@ -29,7 +29,9 @@ |
|
|
|
#include STM32_HAL_H |
|
|
|
|
|
|
|
#include "py/nlr.h" |
|
|
|
#include "py/smallint.h" |
|
|
|
#include "py/obj.h" |
|
|
|
#include "systick.h" |
|
|
|
#include "timeutils.h" |
|
|
|
#include "portmodules.h" |
|
|
|
#include "rtc.h" |
|
|
@ -112,6 +114,18 @@ STATIC mp_obj_t time_mktime(mp_obj_t tuple) { |
|
|
|
} |
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(time_mktime_obj, time_mktime); |
|
|
|
|
|
|
|
/// \function time()
|
|
|
|
/// Returns the number of seconds, as an integer, since 1/1/2000.
|
|
|
|
STATIC mp_obj_t time_time(void) { |
|
|
|
// get date and time
|
|
|
|
// note: need to call get time then get date to correctly access the registers
|
|
|
|
RTC_DateTypeDef date; |
|
|
|
RTC_TimeTypeDef time; |
|
|
|
HAL_RTC_GetTime(&RTCHandle, &time, FORMAT_BIN); |
|
|
|
HAL_RTC_GetDate(&RTCHandle, &date, FORMAT_BIN); |
|
|
|
return mp_obj_new_int(timeutils_seconds_since_2000(2000 + date.Year, date.Month, date.Date, time.Hours, time.Minutes, time.Seconds)); |
|
|
|
} |
|
|
|
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
|
|
|
@ -130,26 +144,67 @@ STATIC mp_obj_t time_sleep(mp_obj_t seconds_o) { |
|
|
|
} |
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(time_sleep_obj, time_sleep); |
|
|
|
|
|
|
|
/// \function time()
|
|
|
|
/// Returns the number of seconds, as an integer, since 1/1/2000.
|
|
|
|
STATIC mp_obj_t time_time(void) { |
|
|
|
// get date and time
|
|
|
|
// note: need to call get time then get date to correctly access the registers
|
|
|
|
RTC_DateTypeDef date; |
|
|
|
RTC_TimeTypeDef time; |
|
|
|
HAL_RTC_GetTime(&RTCHandle, &time, FORMAT_BIN); |
|
|
|
HAL_RTC_GetDate(&RTCHandle, &date, FORMAT_BIN); |
|
|
|
return mp_obj_new_int(timeutils_seconds_since_2000(2000 + date.Year, date.Month, date.Date, time.Hours, time.Minutes, time.Seconds)); |
|
|
|
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_0(time_time_obj, time_time); |
|
|
|
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) { |
|
|
|
CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk; |
|
|
|
DWT->CYCCNT = 0; |
|
|
|
DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk; |
|
|
|
enabled = true; |
|
|
|
} |
|
|
|
return MP_OBJ_NEW_SMALL_INT(DWT->CYCCNT & MP_SMALL_INT_POSITIVE_MASK); |
|
|
|
} |
|
|
|
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_sleep), (mp_obj_t)&time_sleep_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_ticks_cpu), (mp_obj_t)&time_ticks_cpu_obj }, |
|
|
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_ticks_diff), (mp_obj_t)&time_ticks_diff_obj }, |
|
|
|
}; |
|
|
|
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(time_module_globals, time_module_globals_table); |
|
|
|