From 16bfe3b41c46b1c8070f025bdbc932e21acf1753 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Wed, 10 Mar 2021 16:46:42 -0600 Subject: [PATCH] raspberrypi: RTC: Ensure a time is set Until a time is set, the RTC is not running, and rtc_get_datetime() returns false without assigning to the out-parameter. In CircuitPython, this would manifest as arbitrary values being returned, since uninitialized storage on the stack was being converted into a timestamp. --- ports/raspberrypi/common-hal/rtc/RTC.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/ports/raspberrypi/common-hal/rtc/RTC.c b/ports/raspberrypi/common-hal/rtc/RTC.c index 89c23fc190..e6c5d88712 100644 --- a/ports/raspberrypi/common-hal/rtc/RTC.c +++ b/ports/raspberrypi/common-hal/rtc/RTC.c @@ -29,9 +29,23 @@ #include "py/runtime.h" #include "src/rp2_common/hardware_rtc/include/hardware/rtc.h" +#include "src/rp2_common/hardware_clocks/include/hardware/clocks.h" void common_hal_rtc_init(void) { + datetime_t t = { + .year = 2020, + .month = 1, + .day = 1, + .dotw = 3, // 0 is Sunday, so 3 is Wednesday + .hour = 0, + .min = 0, + .sec = 0 + }; + + // Start the RTC rtc_init(); + rtc_set_datetime(&t); + } void common_hal_rtc_get_time(timeutils_struct_time_t *tm) {