From 7a68e17dd09c3c148ba9d50a5728491988940d69 Mon Sep 17 00:00:00 2001
From: barthess <barthess@35acf78f-673a-0410-8e92-d51de3d6d3f4>
Date: Fri, 21 Sep 2012 09:26:41 +0000
Subject: RTCv1. Added FAT timestamp support in driver using chrtclib for
 deviced without hardware calendar.

git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@4706 35acf78f-673a-0410-8e92-d51de3d6d3f4
---
 os/hal/platforms/STM32/RTCv1/rtc_lld.c | 27 +++++++++++++++++++++++++--
 os/hal/platforms/STM32/RTCv1/rtc_lld.h |  1 +
 os/various/chrtclib.c                  |  4 ++--
 3 files changed, 28 insertions(+), 4 deletions(-)

diff --git a/os/hal/platforms/STM32/RTCv1/rtc_lld.c b/os/hal/platforms/STM32/RTCv1/rtc_lld.c
index 1b0fd433c..bc083ffbd 100644
--- a/os/hal/platforms/STM32/RTCv1/rtc_lld.c
+++ b/os/hal/platforms/STM32/RTCv1/rtc_lld.c
@@ -30,10 +30,9 @@
  * @{
  */
 
-#include <time.h>
-
 #include "ch.h"
 #include "hal.h"
+#include "chrtclib.h"
 
 #if HAL_USE_RTC || defined(__DOXYGEN__)
 
@@ -300,6 +299,30 @@ void rtc_lld_set_callback(RTCDriver *rtcp, rtccb_t callback) {
     rtcp->callback = NULL;
   }
 }
+
+/**
+ * @brief   Get current time in format suitable for usage in FatFS.
+ *
+ * @param[in] rtcp      pointer to RTC driver structure
+ * @return              FAT time value.
+ *
+ * @api
+ */
+uint32_t rtc_lld_get_time_fat(RTCDriver *rtcp) {
+  uint32_t fattime;
+  struct tm timp;
+
+  rtcGetTimeTm(rtcp, &timp);
+
+  fattime  = (timp.tm_sec)       >> 1;
+  fattime |= (timp.tm_min)       << 5;
+  fattime |= (timp.tm_hour)      << 11;
+  fattime |= (timp.tm_mday)      << 16;
+  fattime |= (timp.tm_mon + 1)   << 21;
+  fattime |= (timp.tm_year - 80) << 25;
+
+  return fattime;
+}
 #endif /* HAL_USE_RTC */
 
 /** @} */
diff --git a/os/hal/platforms/STM32/RTCv1/rtc_lld.h b/os/hal/platforms/STM32/RTCv1/rtc_lld.h
index 4944ff735..c0ad92525 100644
--- a/os/hal/platforms/STM32/RTCv1/rtc_lld.h
+++ b/os/hal/platforms/STM32/RTCv1/rtc_lld.h
@@ -178,6 +178,7 @@ extern "C" {
                          rtcalarm_t alarm,
                          RTCAlarm *alarmspec);
   void rtc_lld_set_callback(RTCDriver *rtcp, rtccb_t callback);
+  uint32_t rtc_lld_get_time_fat(RTCDriver *rtcp);
 #ifdef __cplusplus
 }
 #endif
diff --git a/os/various/chrtclib.c b/os/various/chrtclib.c
index 04b3cb25c..04c0b67db 100644
--- a/os/various/chrtclib.c
+++ b/os/various/chrtclib.c
@@ -337,13 +337,13 @@ uint64_t rtcGetTimeUnixUsec(RTCDriver *rtcp) {
  *
  * @api
  */
-uint32_t rtcGetTimeFat(RTCDriver *rtcp) {
+uint32_t rtcGetTimeFatFromCounter(RTCDriver *rtcp) {
   uint32_t fattime;
   struct tm timp;
 
   rtcGetTimeTm(rtcp, &timp);
 
-  fattime  = (timp.tm_sec)       << 1;
+  fattime  = (timp.tm_sec)       >> 1;
   fattime |= (timp.tm_min)       << 5;
   fattime |= (timp.tm_hour)      << 11;
   fattime |= (timp.tm_mday)      << 16;
-- 
cgit v1.2.3