Skip to content

Commit

Permalink
samd: Always provide the machine.RTC class.
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
robert-hh authored and dpgeorge committed Mar 13, 2023
1 parent d5c45a8 commit 05bb260
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 35 deletions.
5 changes: 0 additions & 5 deletions ports/samd/mcu/samd21/mpconfigmcu.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 0 additions & 6 deletions ports/samd/mcu/samd51/mpconfigmcu.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions ports/samd/modmachine.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
25 changes: 1 addition & 24 deletions ports/samd/modutime.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,36 +27,20 @@
#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 {
seconds = mp_obj_get_int(args[0]);
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),
Expand Down Expand Up @@ -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);

Expand Down
1 change: 1 addition & 0 deletions ports/samd/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 05bb260

Please sign in to comment.