diff --git a/src/rtc.c b/src/rtc.c --- a/src/rtc.c +++ b/src/rtc.c @@ -7,7 +7,15 @@ #include "gpio.h" -RTC_HandleTypeDef hrtc; +static RTC_HandleTypeDef hrtc; +static volatile uint32_t seconds_ctr = 0; + + +void rtc_incseconds(void) +{ + //HAL_GPIO_TogglePin(LED_BLUE); + seconds_ctr += 1; +} static void Error_Handler(void) { @@ -94,14 +102,16 @@ void rtc_init(void) } -RTC_TimeTypeDef time_last = {0}; +static RTC_TimeTypeDef time_last = {0}; +// Get the time from the RTC RTC_TimeTypeDef* rtc_time(void) { HAL_RTC_GetTime(&hrtc, &time_last, RTC_FORMAT_BCD); return &time_last; } +// Calculate value in milliseconds from subseconds of the RTC uint16_t __rtc_millis(uint32_t sub_seconds) { // Subsecond counter counts down from SynchPrediv value to zero @@ -110,6 +120,7 @@ uint16_t __rtc_millis(uint32_t sub_secon #define JULIAN_DATE_BASE 2440588 // Unix epoch time in Julian calendar (UnixTime = 00:00:00 01.01.1970 => JDN = 2440588) +// Calculate timestamp in milliseconds since Unix epoch uint64_t rtc_timestamp(void) { RTC_TimeTypeDef time = {0}; @@ -154,12 +165,20 @@ uint64_t rtc_timestamp(void) } +uint32_t rtc_timestamp_seconds(void) +{ + return seconds_ctr; +} + +// Calibrate the RTC somehow void rtc_cal(void) { // Do something with hrtc.Instance->CALR; // this has a plus and minus component, see refman } + +// RTC accessor RTC_HandleTypeDef* rtc_gethandle(void) { return &hrtc;