From 1b575b0328497f6d43a6ee678badff14a429d454 Mon Sep 17 00:00:00 2001
From: Rahix <rahix@rahix.de>
Date: Wed, 31 Jul 2019 23:54:25 +0200
Subject: [PATCH] fix(rtc): Fix rtc values not being synchronized

Use RTC_GetTime() instead of RTC_GetSecond() because otherwise a read
immediately following the RTC alarm will return the previous value.
RTC_GetTime() will return E_BUSY a number of times before finally being
successful and returning a timestamp.  To keep the system load minimal
during this waiting, the task is put to sleep for 4ms everytime the read
fails.  This value might need further adjustment.

Signed-off-by: Rahix <rahix@rahix.de>
---
 epicardium/modules/rtc.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/epicardium/modules/rtc.c b/epicardium/modules/rtc.c
index f50355e1..edfb0441 100644
--- a/epicardium/modules/rtc.c
+++ b/epicardium/modules/rtc.c
@@ -2,13 +2,27 @@
 #include "modules/log.h"
 #include "api/interrupt-sender.h"
 
+#include "FreeRTOS.h"
+#include "task.h"
+
 #include "rtc.h"
 
 #include <stdint.h>
 
 uint32_t epic_rtc_get_seconds(void)
 {
-	return RTC_GetSecond();
+	uint32_t sec, subsec;
+
+	/*
+	 * TODO:  Find out what causes the weird behavior of this function.  The
+	 *        time needed for this call seems to depend on the frequency at
+	 *        which it is called.
+	 */
+	while (RTC_GetTime(&sec, &subsec) == E_BUSY) {
+		vTaskDelay(pdMS_TO_TICKS(4));
+	}
+
+	return sec;
 }
 
 void RTC_IRQHandler(void)
-- 
GitLab