Skip to content

Commit b70b8ce

Browse files
robert-hhdpgeorge
authored andcommitted
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.
1 parent e3030f7 commit b70b8ce

File tree

5 files changed

+35
-4
lines changed

5 files changed

+35
-4
lines changed

ports/mimxrt/board_init.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ void board_init(void) {
100100
#if MICROPY_PY_MACHINE_I2S
101101
machine_i2s_init0();
102102
#endif
103+
// RTC
104+
machine_rtc_start();
103105
}
104106

105107
void USB_OTG1_IRQHandler(void) {

ports/mimxrt/machine_rtc.c

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,31 @@ typedef struct _machine_rtc_obj_t {
4040
STATIC const machine_rtc_obj_t machine_rtc_obj = {{&machine_rtc_type}};
4141
uint32_t us_offset = 0;
4242

43+
// Start the RTC Timer.
44+
void machine_rtc_start(void) {
45+
46+
SNVS_LP_SRTC_StartTimer(SNVS);
47+
// If the date is not set, set it to a more recent start date,
48+
// MicroPython's first commit.
49+
snvs_lp_srtc_datetime_t srtc_date;
50+
SNVS_LP_SRTC_GetDatetime(SNVS, &srtc_date);
51+
if (srtc_date.year <= 1970) {
52+
srtc_date = (snvs_lp_srtc_datetime_t) {
53+
.year = 2013,
54+
.month = 10,
55+
.day = 14,
56+
.hour = 19,
57+
.minute = 53,
58+
.second = 11,
59+
};
60+
SNVS_LP_SRTC_SetDatetime(SNVS, &srtc_date);
61+
}
62+
}
63+
4364
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) {
4465
// Check arguments.
4566
mp_arg_check_num(n_args, n_kw, 0, 0, false);
4667

47-
// Start up the RTC if needed.
48-
SNVS_LP_SRTC_StartTimer(SNVS);
49-
5068
// Return constant object.
5169
return (mp_obj_t)&machine_rtc_obj;
5270
}

ports/mimxrt/modmachine.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,6 @@ void machine_sdcard_init0(void);
4848
void mimxrt_sdram_init(void);
4949
void machine_i2s_init0();
5050
void machine_i2s_deinit_all(void);
51+
void machine_rtc_start(void);
5152

5253
#endif // MICROPY_INCLUDED_MIMXRT_MODMACHINE_H

ports/mimxrt/modutime.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,16 @@ MP_DEFINE_CONST_FUN_OBJ_1(time_mktime_obj, time_mktime);
9595
STATIC mp_obj_t time_time(void) {
9696
snvs_lp_srtc_datetime_t t;
9797
SNVS_LP_SRTC_GetDatetime(SNVS, &t);
98-
return mp_obj_new_int_from_ull(timeutils_seconds_since_epoch(t.year, t.month, t.day, t.hour, t.minute, t.second));
98+
// EPOCH is 1970 for this port, which leads to the following trouble:
99+
// timeutils_seconds_since_epoch() calls timeutils_seconds_since_2000(), and
100+
// timeutils_seconds_since_2000() subtracts 2000 from year, but uses
101+
// an unsigned number for seconds, That causes an underrun, which is not
102+
// fixed by adding the TIMEUTILS_SECONDS_1970_TO_2000.
103+
// Masking it to 32 bit for year < 2000 fixes it.
104+
return mp_obj_new_int_from_ull(
105+
timeutils_seconds_since_epoch(t.year, t.month, t.day, t.hour, t.minute, t.second)
106+
& (t.year < 2000 ? 0xffffffff : 0xffffffffffff)
107+
);
99108
}
100109
STATIC MP_DEFINE_CONST_FUN_OBJ_0(time_time_obj, time_time);
101110

shared/timeutils/timeutils.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ static inline uint64_t timeutils_mktime(mp_uint_t year, mp_int_t month, mp_int_t
6969

7070
static inline uint64_t timeutils_seconds_since_epoch(mp_uint_t year, mp_uint_t month,
7171
mp_uint_t date, mp_uint_t hour, mp_uint_t minute, mp_uint_t second) {
72+
// TODO this will give incorrect results for dates before 2000/1/1
7273
return timeutils_seconds_since_2000(year, month, date, hour, minute, second) + TIMEUTILS_SECONDS_1970_TO_2000;
7374
}
7475

0 commit comments

Comments
 (0)