mimxrt/machine_rtc: Start RTC at boot and set datetime if not set.

Changes in this commit:
- Start the RTC Timer at system boot.  Otherwise time.time() will advance
  only if an RTC() object was created.
- Set the time to a more recent date than Jan 1, 1970, if not set.  That is
  2013/10/14, 19:53:11, MicroPython's first commit.
- Compensate an underflow in in timeutils_seconds_since_2000(), called by
  time.time(), if the time is set to a pre-2000 date.
pull/8439/head
robert-hh 2022-03-14 20:38:46 +01:00 zatwierdzone przez Damien George
rodzic e3030f7436
commit b70b8ce3e4
5 zmienionych plików z 35 dodań i 4 usunięć

Wyświetl plik

@ -100,6 +100,8 @@ void board_init(void) {
#if MICROPY_PY_MACHINE_I2S
machine_i2s_init0();
#endif
// RTC
machine_rtc_start();
}
void USB_OTG1_IRQHandler(void) {

Wyświetl plik

@ -40,13 +40,31 @@ typedef struct _machine_rtc_obj_t {
STATIC const machine_rtc_obj_t machine_rtc_obj = {{&machine_rtc_type}};
uint32_t us_offset = 0;
// Start the RTC Timer.
void machine_rtc_start(void) {
SNVS_LP_SRTC_StartTimer(SNVS);
// If the date is not set, set it to a more recent start date,
// MicroPython's first commit.
snvs_lp_srtc_datetime_t srtc_date;
SNVS_LP_SRTC_GetDatetime(SNVS, &srtc_date);
if (srtc_date.year <= 1970) {
srtc_date = (snvs_lp_srtc_datetime_t) {
.year = 2013,
.month = 10,
.day = 14,
.hour = 19,
.minute = 53,
.second = 11,
};
SNVS_LP_SRTC_SetDatetime(SNVS, &srtc_date);
}
}
STATIC mp_obj_t machine_rtc_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
// Check arguments.
mp_arg_check_num(n_args, n_kw, 0, 0, false);
// Start up the RTC if needed.
SNVS_LP_SRTC_StartTimer(SNVS);
// Return constant object.
return (mp_obj_t)&machine_rtc_obj;
}

Wyświetl plik

@ -48,5 +48,6 @@ void machine_sdcard_init0(void);
void mimxrt_sdram_init(void);
void machine_i2s_init0();
void machine_i2s_deinit_all(void);
void machine_rtc_start(void);
#endif // MICROPY_INCLUDED_MIMXRT_MODMACHINE_H

Wyświetl plik

@ -95,7 +95,16 @@ MP_DEFINE_CONST_FUN_OBJ_1(time_mktime_obj, time_mktime);
STATIC mp_obj_t time_time(void) {
snvs_lp_srtc_datetime_t t;
SNVS_LP_SRTC_GetDatetime(SNVS, &t);
return mp_obj_new_int_from_ull(timeutils_seconds_since_epoch(t.year, t.month, t.day, t.hour, t.minute, t.second));
// EPOCH is 1970 for this port, which leads to the following trouble:
// timeutils_seconds_since_epoch() calls timeutils_seconds_since_2000(), and
// timeutils_seconds_since_2000() subtracts 2000 from year, but uses
// an unsigned number for seconds, That causes an underrun, which is not
// fixed by adding the TIMEUTILS_SECONDS_1970_TO_2000.
// Masking it to 32 bit for year < 2000 fixes it.
return mp_obj_new_int_from_ull(
timeutils_seconds_since_epoch(t.year, t.month, t.day, t.hour, t.minute, t.second)
& (t.year < 2000 ? 0xffffffff : 0xffffffffffff)
);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_0(time_time_obj, time_time);

Wyświetl plik

@ -69,6 +69,7 @@ static inline uint64_t timeutils_mktime(mp_uint_t year, mp_int_t month, mp_int_t
static inline uint64_t timeutils_seconds_since_epoch(mp_uint_t year, mp_uint_t month,
mp_uint_t date, mp_uint_t hour, mp_uint_t minute, mp_uint_t second) {
// TODO this will give incorrect results for dates before 2000/1/1
return timeutils_seconds_since_2000(year, month, date, hour, minute, second) + TIMEUTILS_SECONDS_1970_TO_2000;
}