From 05bb26010e4a466a82cfed179f8d8d0b406a78ca Mon Sep 17 00:00:00 2001 From: robert-hh Date: Fri, 10 Mar 2023 12:59:43 +0100 Subject: [PATCH] samd: Always provide the machine.RTC class. Even if boards do not have a clock crystal. In that case, the clock quality will be very poor. Always having machine.RTC means that the date/time can be set in a way that is consistent with other ports. This commit also removes the special code in modutime.c for devices without the RTC class. Signed-off-by: Damien George --- ports/samd/mcu/samd21/mpconfigmcu.h | 5 ----- ports/samd/mcu/samd51/mpconfigmcu.h | 6 ------ ports/samd/modmachine.h | 3 +++ ports/samd/modutime.c | 25 +------------------------ ports/samd/mpconfigport.h | 1 + 5 files changed, 5 insertions(+), 35 deletions(-) diff --git a/ports/samd/mcu/samd21/mpconfigmcu.h b/ports/samd/mcu/samd21/mpconfigmcu.h index db4e58753c..0c5bfe5ae7 100644 --- a/ports/samd/mcu/samd21/mpconfigmcu.h +++ b/ports/samd/mcu/samd21/mpconfigmcu.h @@ -31,11 +31,6 @@ unsigned long trng_random_u32(int delay); #define MICROPY_HW_UART_TXBUF (1) #endif -#ifndef MICROPY_PY_MACHINE_RTC -#if MICROPY_HW_XOSC32K -#define MICROPY_PY_MACHINE_RTC (1) -#endif -#endif #define MICROPY_PY_UOS_URANDOM (1) #ifndef MICROPY_PY_MACHINE_PIN_BOARD_CPU diff --git a/ports/samd/mcu/samd51/mpconfigmcu.h b/ports/samd/mcu/samd51/mpconfigmcu.h index 64261951ba..5ac431b297 100644 --- a/ports/samd/mcu/samd51/mpconfigmcu.h +++ b/ports/samd/mcu/samd51/mpconfigmcu.h @@ -28,12 +28,6 @@ #define MICROPY_PY_URANDOM_SEED_INIT_FUNC (trng_random_u32()) unsigned long trng_random_u32(void); -#ifndef MICROPY_PY_MACHINE_RTC -#if MICROPY_HW_XOSC32K -#define MICROPY_PY_MACHINE_RTC (1) -#endif -#endif - #ifndef MICROPY_PY_MACHINE_PIN_BOARD_CPU #define MICROPY_PY_MACHINE_PIN_BOARD_CPU (1) #endif diff --git a/ports/samd/modmachine.h b/ports/samd/modmachine.h index e8738df4b5..8b3059d9fc 100644 --- a/ports/samd/modmachine.h +++ b/ports/samd/modmachine.h @@ -27,6 +27,7 @@ #define MICROPY_INCLUDED_SAMD_MODMACHINE_H #include "py/obj.h" +#include "shared/timeutils/timeutils.h" extern const mp_obj_type_t machine_adc_type; extern const mp_obj_type_t machine_dac_type; @@ -43,4 +44,6 @@ extern const mp_obj_type_t machine_rtc_type; NORETURN mp_obj_t machine_bootloader(size_t n_args, const mp_obj_t *args); +void rtc_gettime(timeutils_struct_time_t *tm); + #endif // MICROPY_INCLUDED_SAMD_MODMACHINE_H diff --git a/ports/samd/modutime.c b/ports/samd/modutime.c index 6b04134497..6fce203154 100644 --- a/ports/samd/modutime.c +++ b/ports/samd/modutime.c @@ -27,19 +27,13 @@ #include "py/runtime.h" #include "extmod/utime_mphal.h" #include "shared/timeutils/timeutils.h" -#include "mphalport.h" - -#if !MICROPY_PY_MACHINE_RTC -uint32_t time_offset = 0; -#endif // !MICROPY_PY_MACHINE_RTC +#include "modmachine.h" // localtime([secs]) STATIC mp_obj_t time_localtime(size_t n_args, const mp_obj_t *args) { timeutils_struct_time_t tm; mp_int_t seconds; - #if MICROPY_PY_MACHINE_RTC - extern void rtc_gettime(timeutils_struct_time_t *tm); if (n_args == 0 || args[0] == mp_const_none) { rtc_gettime(&tm); } else { @@ -47,16 +41,6 @@ STATIC mp_obj_t time_localtime(size_t n_args, const mp_obj_t *args) { timeutils_seconds_since_epoch_to_struct_time(seconds, &tm); } - #else - if (n_args == 0 || args[0] == mp_const_none) { - seconds = mp_hal_ticks_ms_64() / 1000 + time_offset; - } else { - seconds = mp_obj_get_int(args[0]); - time_offset = seconds - mp_hal_ticks_ms_64() / 1000; - } - timeutils_seconds_since_epoch_to_struct_time(seconds, &tm); - - #endif // MICROPY_PY_MACHINE_RTC mp_obj_t tuple[8] = { tuple[0] = mp_obj_new_int(tm.tm_year), tuple[1] = mp_obj_new_int(tm.tm_mon), @@ -90,17 +74,10 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(time_mktime_obj, time_mktime); // time() STATIC mp_obj_t time_time(void) { - #if MICROPY_PY_MACHINE_RTC - extern void rtc_gettime(timeutils_struct_time_t *tm); timeutils_struct_time_t tm; rtc_gettime(&tm); return mp_obj_new_int_from_uint(timeutils_mktime( tm.tm_year, tm.tm_mon, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec)); - - #else - return mp_obj_new_int_from_uint(mp_hal_ticks_ms_64() / 1000 + time_offset); - - #endif // MICROPY_PY_MACHINE_RTC } STATIC MP_DEFINE_CONST_FUN_OBJ_0(time_time_obj, time_time); diff --git a/ports/samd/mpconfigport.h b/ports/samd/mpconfigport.h index 613c24c970..a048ad54be 100644 --- a/ports/samd/mpconfigport.h +++ b/ports/samd/mpconfigport.h @@ -92,6 +92,7 @@ #define MICROPY_PY_UZLIB (1) #define MICROPY_PY_UASYNCIO (1) #define MICROPY_PY_MACHINE_I2C (1) +#define MICROPY_PY_MACHINE_RTC (1) #define MICROPY_PY_MACHINE_SOFTI2C (1) #define MICROPY_PY_MACHINE_SPI (1) #define MICROPY_PY_MACHINE_SOFTSPI (1)