aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorbarthess <barthess@35acf78f-673a-0410-8e92-d51de3d6d3f4>2012-03-09 18:33:26 +0000
committerbarthess <barthess@35acf78f-673a-0410-8e92-d51de3d6d3f4>2012-03-09 18:33:26 +0000
commit3c311dfe589b6a6b1fd46af2e3138512fe2135fa (patch)
treecda6528f0bfc9523d6c72ce4f43934feaf023bd1
parent2b35b5a2a5a9484332edebaca861a87910cf6715 (diff)
downloadChibiOS-3c311dfe589b6a6b1fd46af2e3138512fe2135fa.tar.gz
ChibiOS-3c311dfe589b6a6b1fd46af2e3138512fe2135fa.tar.bz2
ChibiOS-3c311dfe589b6a6b1fd46af2e3138512fe2135fa.zip
RTC. High level staff moved to chrtclib.
git-svn-id: svn://svn.code.sf.net/p/chibios/svn/branches/sdc_dev2@4032 35acf78f-673a-0410-8e92-d51de3d6d3f4
-rw-r--r--os/hal/include/rtc.h1
-rw-r--r--os/hal/platforms/STM32/RTCv1/rtc_lld.c23
-rw-r--r--os/hal/platforms/STM32/RTCv1/rtc_lld.h1
-rw-r--r--os/hal/platforms/STM32/RTCv2/rtc_lld.c120
-rw-r--r--os/hal/platforms/STM32/RTCv2/rtc_lld.h5
-rw-r--r--os/hal/platforms/STM32F1xx/hal_lld.h2
-rw-r--r--os/hal/src/rtc.c20
-rwxr-xr-xos/various/chrtclib.c338
-rwxr-xr-xos/various/chrtclib.h56
-rw-r--r--testhal/STM32F1xx/RTC/Makefile1
-rw-r--r--testhal/STM32F1xx/RTC/main.c2
-rw-r--r--testhal/STM32F4xx/RTC/Makefile1
-rw-r--r--testhal/STM32F4xx/RTC/main.c10
-rwxr-xr-xtesthal/STM32F4xx/SDC/Makefile1
14 files changed, 404 insertions, 177 deletions
diff --git a/os/hal/include/rtc.h b/os/hal/include/rtc.h
index 3acce33e4..9b5cba848 100644
--- a/os/hal/include/rtc.h
+++ b/os/hal/include/rtc.h
@@ -151,7 +151,6 @@ extern "C" {
void rtcInit(void);
void rtcSetTime(RTCDriver *rtcp, const RTCTime *timespec);
void rtcGetTime(RTCDriver *rtcp, RTCTime *timespec);
- uint32_t rtcGetFatTime(RTCDriver *rtcp);
#if RTC_ALARMS > 0
void rtcSetAlarm(RTCDriver *rtcp,
rtcalarm_t alarm,
diff --git a/os/hal/platforms/STM32/RTCv1/rtc_lld.c b/os/hal/platforms/STM32/RTCv1/rtc_lld.c
index 6496cfae9..a417194b1 100644
--- a/os/hal/platforms/STM32/RTCv1/rtc_lld.c
+++ b/os/hal/platforms/STM32/RTCv1/rtc_lld.c
@@ -291,29 +291,6 @@ 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] timespec pointer to RTCTime structure
- * @return FAT time value.
- *
- * @api
- */
-uint32_t rtc_lld_calc_fat_time(RTCTime *timespec){
- uint32_t fattime = 0;
- struct tm *timp;
-
- timp = localtime((time_t *)(timespec->tv_sec));
-
- fattime |= (timp->tm_sec / 2);
- 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 64287d978..4944ff735 100644
--- a/os/hal/platforms/STM32/RTCv1/rtc_lld.h
+++ b/os/hal/platforms/STM32/RTCv1/rtc_lld.h
@@ -178,7 +178,6 @@ extern "C" {
rtcalarm_t alarm,
RTCAlarm *alarmspec);
void rtc_lld_set_callback(RTCDriver *rtcp, rtccb_t callback);
- uint32_t rtc_lld_calc_fat_time(RTCTime *timespec);
#ifdef __cplusplus
}
#endif
diff --git a/os/hal/platforms/STM32/RTCv2/rtc_lld.c b/os/hal/platforms/STM32/RTCv2/rtc_lld.c
index c2268fd82..870734fba 100644
--- a/os/hal/platforms/STM32/RTCv2/rtc_lld.c
+++ b/os/hal/platforms/STM32/RTCv2/rtc_lld.c
@@ -264,126 +264,6 @@ void rtcGetPeriodicWakeup_v2(RTCDriver *rtcp, RTCWakeup *wakeupspec){
wakeupspec->wakeup |= (((uint32_t)rtcp->id_rtc->CR) & 0x7) << 16;
}
-/**
- * @brief Get current time in format suitable for usage in FatFS.
- *
- * @param[in] timespec pointer to RTCTime structure
- * @return FAT time value.
- *
- * @api
- */
-uint32_t rtc_lld_calc_fat_time(RTCTime *timespec){
- uint32_t fattime = 0;
- struct tm timp;
-
- stm32_rtc_bcd2tm(&timp, timespec);
-
- fattime |= (timp.tm_sec / 2);
- 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;
-}
-
-/**
- * @brief Converts from STM32 BCD to canonicalized time format.
- *
- * @param[out] timp pointer to a @p tm structure defined in time.h
- * @param[in] timespec pointer to a @p RTCTime structure
- *
- * @api
- */
-void stm32_rtc_bcd2tm(struct tm *timp, RTCTime *timespec){
- uint32_t tv_time = timespec->tv_time;
- uint32_t tv_date = timespec->tv_date;
-
-#if CH_DBG_ENABLE_CHECKS
- timp->tm_isdst = 0;
- timp->tm_wday = 0;
- timp->tm_mday = 0;
- timp->tm_yday = 0;
- timp->tm_mon = 0;
- timp->tm_year = 0;
- timp->tm_sec = 0;
- timp->tm_min = 0;
- timp->tm_hour = 0;
-#endif
-
- timp->tm_isdst = -1;
-
- timp->tm_wday = (tv_date & RTC_DR_WDU) >> RTC_DR_WDU_OFFSET;
- if(timp->tm_wday == 7)
- timp->tm_wday = 0;
-
- timp->tm_mday = (tv_date & RTC_DR_DU) >> RTC_DR_DU_OFFSET;
- timp->tm_mday += ((tv_date & RTC_DR_DT) >> RTC_DR_DT_OFFSET) * 10;
-
- timp->tm_mon = (tv_date & RTC_DR_MU) >> RTC_DR_MU_OFFSET;
- timp->tm_mon += ((tv_date & RTC_DR_MT) >> RTC_DR_MT_OFFSET) * 10;
- timp->tm_mon -= 1;
-
- timp->tm_year = (tv_date & RTC_DR_YU) >> RTC_DR_YU_OFFSET;
- timp->tm_year += ((tv_date & RTC_DR_YT) >> RTC_DR_YT_OFFSET) * 10;
- timp->tm_year += 2000 - 1900;
-
- timp->tm_sec = (tv_time & RTC_TR_SU) >> RTC_TR_SU_OFFSET;
- timp->tm_sec += ((tv_time & RTC_TR_ST) >> RTC_TR_ST_OFFSET) * 10;
-
- timp->tm_min = (tv_time & RTC_TR_MNU) >> RTC_TR_MNU_OFFSET;
- timp->tm_min += ((tv_time & RTC_TR_MNT) >> RTC_TR_MNT_OFFSET) * 10;
-
- timp->tm_hour = (tv_time & RTC_TR_HU) >> RTC_TR_HU_OFFSET;
- timp->tm_hour += ((tv_time & RTC_TR_HT) >> RTC_TR_HT_OFFSET) * 10;
- timp->tm_hour += 12 * ((tv_time & RTC_TR_PM) >> RTC_TR_PM_OFFSET);
-}
-
-/**
- * @brief Converts from canonicalized to STM32 BCD time format.
- *
- * @param[in] timp pointer to a @p tm structure defined in time.h
- * @param[out] timespec pointer to a @p RTCTime structure
- *
- * @api
- */
-void stm32_rtc_tm2bcd(struct tm *timp, RTCTime *timespec){
- uint32_t v = 0;
-
- timespec->tv_date = 0;
- timespec->tv_time = 0;
-
- v = timp->tm_year - 100;
- timespec->tv_date |= ((v / 10) << RTC_DR_YT_OFFSET) & RTC_DR_YT;
- timespec->tv_date |= (v % 10) << RTC_DR_YU_OFFSET;
-
- if (timp->tm_wday == 0)
- v = 7;
- else
- v = timp->tm_wday;
- timespec->tv_date |= (v << RTC_DR_WDU_OFFSET) & RTC_DR_WDU;
-
- v = timp->tm_mon + 1;
- timespec->tv_date |= ((v / 10) << RTC_DR_MT_OFFSET) & RTC_DR_MT;
- timespec->tv_date |= (v % 10) << RTC_DR_MU_OFFSET;
-
- v = timp->tm_mday;
- timespec->tv_date |= ((v / 10) << RTC_DR_DT_OFFSET) & RTC_DR_DT;
- timespec->tv_date |= (v % 10) << RTC_DR_DU_OFFSET;
-
- v = timp->tm_hour;
- timespec->tv_time |= ((v / 10) << RTC_TR_HT_OFFSET) & RTC_TR_HT;
- timespec->tv_time |= (v % 10) << RTC_TR_HU_OFFSET;
-
- v = timp->tm_min;
- timespec->tv_time |= ((v / 10) << RTC_TR_MNT_OFFSET) & RTC_TR_MNT;
- timespec->tv_time |= (v % 10) << RTC_TR_MNU_OFFSET;
-
- v = timp->tm_sec;
- timespec->tv_time |= ((v / 10) << RTC_TR_ST_OFFSET) & RTC_TR_ST;
- timespec->tv_time |= (v % 10) << RTC_TR_SU_OFFSET;
-}
-
#endif /* HAL_USE_RTC */
/** @} */
diff --git a/os/hal/platforms/STM32/RTCv2/rtc_lld.h b/os/hal/platforms/STM32/RTCv2/rtc_lld.h
index 88959294c..4cae5744d 100644
--- a/os/hal/platforms/STM32/RTCv2/rtc_lld.h
+++ b/os/hal/platforms/STM32/RTCv2/rtc_lld.h
@@ -35,8 +35,6 @@
#if HAL_USE_RTC || defined(__DOXYGEN__)
-#include <time.h>
-
/*===========================================================================*/
/* Driver constants. */
/*===========================================================================*/
@@ -200,9 +198,6 @@ extern "C" {
RTCAlarm *alarmspec);
void rtcSetPeriodicWakeup_v2(RTCDriver *rtcp, RTCWakeup *wakeupspec);
void rtcGetPeriodicWakeup_v2(RTCDriver *rtcp, RTCWakeup *wakeupspec);
- uint32_t rtc_lld_calc_fat_time(RTCTime *timespec);
- void stm32_rtc_bcd2tm(struct tm *timp, RTCTime *timespec);
- void stm32_rtc_tm2bcd(struct tm *timp, RTCTime *timespec);
#ifdef __cplusplus
}
#endif
diff --git a/os/hal/platforms/STM32F1xx/hal_lld.h b/os/hal/platforms/STM32F1xx/hal_lld.h
index 4a375ab4e..c8d87b7c5 100644
--- a/os/hal/platforms/STM32F1xx/hal_lld.h
+++ b/os/hal/platforms/STM32F1xx/hal_lld.h
@@ -88,7 +88,7 @@
/* RTC attributes.*/
#define STM32_HAS_RTC TRUE
#define STM32_RTC_HAS_SUBSECONDS TRUE
-#define STM32_RTC_IS_CALENDAR TRUE
+#define STM32_RTC_IS_CALENDAR FALSE
/** @} */
/*===========================================================================*/
diff --git a/os/hal/src/rtc.c b/os/hal/src/rtc.c
index a6601fd1c..01f88e82d 100644
--- a/os/hal/src/rtc.c
+++ b/os/hal/src/rtc.c
@@ -101,26 +101,6 @@ void rtcGetTime(RTCDriver *rtcp, RTCTime *timespec) {
chSysUnlock();
}
-/**
- * @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 rtcGetFatTime(RTCDriver *rtcp) {
- RTCTime timespec;
-
- chDbgCheck((rtcp != NULL), "rtcGetTime");
-
- chSysLock();
- rtcGetTimeI(rtcp, &timespec);
- chSysUnlock();
-
- return rtc_lld_calc_fat_time(&timespec);
-}
-
#if (RTC_ALARMS > 0) || defined(__DOXYGEN__)
/**
* @brief Set alarm time.
diff --git a/os/various/chrtclib.c b/os/various/chrtclib.c
new file mode 100755
index 000000000..38f5a3627
--- /dev/null
+++ b/os/various/chrtclib.c
@@ -0,0 +1,338 @@
+/*
+ ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010,
+ 2011,2012 Giovanni Di Sirio.
+
+ This file is part of ChibiOS/RT.
+
+ ChibiOS/RT is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ ChibiOS/RT is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+/*
+ Concepts and parts of this file have been contributed by Uladzimir Pylinsky
+ aka barthess.
+ */
+
+#include <time.h>
+
+#include "ch.h"
+#include "hal.h"
+
+#include "chrtclib.h"
+
+#if (defined(STM32F4XX) || defined(STM32F2XX) || defined(STM32L1XX) || \
+ defined(STM32F1XX) || defined(STM32F10X_MD) || defined(STM32F10X_LD) || \
+ defined(STM32F10X_HD))
+#if STM32_RTC_IS_CALENDAR
+/**
+ * @brief Converts from STM32 BCD to canonicalized time format.
+ *
+ * @param[out] timp pointer to a @p tm structure defined in time.h
+ * @param[in] timespec pointer to a @p RTCTime structure
+ *
+ * @notapi
+ */
+void stm32_rtc_bcd2tm(struct tm *timp, RTCTime *timespec){
+ uint32_t tv_time = timespec->tv_time;
+ uint32_t tv_date = timespec->tv_date;
+
+#if CH_DBG_ENABLE_CHECKS
+ timp->tm_isdst = 0;
+ timp->tm_wday = 0;
+ timp->tm_mday = 0;
+ timp->tm_yday = 0;
+ timp->tm_mon = 0;
+ timp->tm_year = 0;
+ timp->tm_sec = 0;
+ timp->tm_min = 0;
+ timp->tm_hour = 0;
+#endif
+
+ timp->tm_isdst = -1;
+
+ timp->tm_wday = (tv_date & RTC_DR_WDU) >> RTC_DR_WDU_OFFSET;
+ if(timp->tm_wday == 7)
+ timp->tm_wday = 0;
+
+ timp->tm_mday = (tv_date & RTC_DR_DU) >> RTC_DR_DU_OFFSET;
+ timp->tm_mday += ((tv_date & RTC_DR_DT) >> RTC_DR_DT_OFFSET) * 10;
+
+ timp->tm_mon = (tv_date & RTC_DR_MU) >> RTC_DR_MU_OFFSET;
+ timp->tm_mon += ((tv_date & RTC_DR_MT) >> RTC_DR_MT_OFFSET) * 10;
+ timp->tm_mon -= 1;
+
+ timp->tm_year = (tv_date & RTC_DR_YU) >> RTC_DR_YU_OFFSET;
+ timp->tm_year += ((tv_date & RTC_DR_YT) >> RTC_DR_YT_OFFSET) * 10;
+ timp->tm_year += 2000 - 1900;
+
+ timp->tm_sec = (tv_time & RTC_TR_SU) >> RTC_TR_SU_OFFSET;
+ timp->tm_sec += ((tv_time & RTC_TR_ST) >> RTC_TR_ST_OFFSET) * 10;
+
+ timp->tm_min = (tv_time & RTC_TR_MNU) >> RTC_TR_MNU_OFFSET;
+ timp->tm_min += ((tv_time & RTC_TR_MNT) >> RTC_TR_MNT_OFFSET) * 10;
+
+ timp->tm_hour = (tv_time & RTC_TR_HU) >> RTC_TR_HU_OFFSET;
+ timp->tm_hour += ((tv_time & RTC_TR_HT) >> RTC_TR_HT_OFFSET) * 10;
+ timp->tm_hour += 12 * ((tv_time & RTC_TR_PM) >> RTC_TR_PM_OFFSET);
+}
+
+/**
+ * @brief Converts from canonicalized to STM32 BCD time format.
+ *
+ * @param[in] timp pointer to a @p tm structure defined in time.h
+ * @param[out] timespec pointer to a @p RTCTime structure
+ *
+ * @notapi
+ */
+void stm32_rtc_tm2bcd(struct tm *timp, RTCTime *timespec){
+ uint32_t v = 0;
+
+ timespec->tv_date = 0;
+ timespec->tv_time = 0;
+
+ v = timp->tm_year - 100;
+ timespec->tv_date |= ((v / 10) << RTC_DR_YT_OFFSET) & RTC_DR_YT;
+ timespec->tv_date |= (v % 10) << RTC_DR_YU_OFFSET;
+
+ if (timp->tm_wday == 0)
+ v = 7;
+ else
+ v = timp->tm_wday;
+ timespec->tv_date |= (v << RTC_DR_WDU_OFFSET) & RTC_DR_WDU;
+
+ v = timp->tm_mon + 1;
+ timespec->tv_date |= ((v / 10) << RTC_DR_MT_OFFSET) & RTC_DR_MT;
+ timespec->tv_date |= (v % 10) << RTC_DR_MU_OFFSET;
+
+ v = timp->tm_mday;
+ timespec->tv_date |= ((v / 10) << RTC_DR_DT_OFFSET) & RTC_DR_DT;
+ timespec->tv_date |= (v % 10) << RTC_DR_DU_OFFSET;
+
+ v = timp->tm_hour;
+ timespec->tv_time |= ((v / 10) << RTC_TR_HT_OFFSET) & RTC_TR_HT;
+ timespec->tv_time |= (v % 10) << RTC_TR_HU_OFFSET;
+
+ v = timp->tm_min;
+ timespec->tv_time |= ((v / 10) << RTC_TR_MNT_OFFSET) & RTC_TR_MNT;
+ timespec->tv_time |= (v % 10) << RTC_TR_MNU_OFFSET;
+
+ v = timp->tm_sec;
+ timespec->tv_time |= ((v / 10) << RTC_TR_ST_OFFSET) & RTC_TR_ST;
+ timespec->tv_time |= (v % 10) << RTC_TR_SU_OFFSET;
+}
+
+/**
+ * @brief Gets raw time from RTC and converts it to canonicalized format.
+ *
+ * @param[in] rtcp pointer to RTC driver structure
+ * @param[out] timp pointer to a @p tm structure defined in time.h
+ *
+ * @api
+ */
+void rtcGetTimeTm(RTCDriver *rtcp, struct tm *timp){
+ RTCTime timespec = {0,0,FALSE,0};
+ rtcGetTime(rtcp, &timespec);
+ stm32_rtc_bcd2tm(timp, &timespec);
+}
+
+/**
+ * @brief Sets RTC time.
+ *
+ * @param[in] rtcp pointer to RTC driver structure
+ * @param[out] timp pointer to a @p tm structure defined in time.h
+ *
+ * @api
+ */
+void rtcSetTimeTm(RTCDriver *rtcp, struct tm *timp){
+ RTCTime timespec = {0,0,FALSE,0};
+
+ stm32_rtc_tm2bcd(timp, &timespec);
+ rtcSetTime(rtcp, &timespec);
+}
+
+/**
+ * @brief Gets raw time from RTC and converts it to unix format.
+ *
+ * @param[in] rtcp pointer to RTC driver structure
+ *
+ * @return Unix time value in seconds.
+ *
+ * @api
+ */
+time_t rtcGetTimeUnixSec(RTCDriver *rtcp){
+ RTCTime timespec = {0,0,FALSE,0};
+ struct tm timp;
+
+ rtcGetTime(rtcp, &timespec);
+ stm32_rtc_bcd2tm(&timp, &timespec);
+
+ return mktime(&timp);
+}
+
+/**
+ * @brief Sets RTC time.
+ *
+ * @param[in] rtcp pointer to RTC driver structure
+ *
+ * @return Unix time value in seconds.
+ *
+ * @api
+ */
+void rtcSetTimeUnixSec(RTCDriver *rtcp, time_t tv_sec){
+ RTCTime timespec = {0,0,FALSE,0};
+ struct tm *timp;
+
+ timp = localtime(&tv_sec);
+ stm32_rtc_tm2bcd(timp, &timespec);
+ rtcSetTime(rtcp, &timespec);
+}
+
+/**
+ * @brief Gets raw time from RTC and converts it to unix format.
+ *
+ * @param[in] rtcp pointer to RTC driver structure
+ *
+ * @return Unix time value in microseconds.
+ *
+ * @api
+ */
+uint64_t rtcGetTimeUnixUsec(RTCDriver *rtcp){
+ uint64_t result = 0;
+
+ RTCTime timespec = {0,0,FALSE,0};
+ struct tm timp;
+
+ rtcGetTime(rtcp, &timespec);
+ stm32_rtc_bcd2tm(&timp, &timespec);
+
+ result = mktime(&timp) * 1000000;
+
+#if STM32_RTC_HAS_SUBSECONDS
+ return result + timespec.tv_msec * 1000;
+#else
+ return result;
+#endif
+}
+
+#else /* STM32_RTC_IS_CALENDAR */
+/**
+ * @brief Gets raw time from RTC and converts it to canonicalized format.
+ *
+ * @param[in] rtcp pointer to RTC driver structure
+ * @param[out] timp pointer to a @p tm structure defined in time.h
+ *
+ * @api
+ */
+void rtcGetTimeTm(RTCDriver *rtcp, struct tm *timp){
+ RTCTime timespec = {0,0,FALSE,0};
+
+ rtcGetTime(rtcp, &timespec);
+ if (timp != NULL) /* this comparison needed to avoid compiler warning */
+ timp = localtime((time_t *)&(timespec.tv_sec));
+}
+
+/**
+ * @brief Sets RTC time.
+ *
+ * @param[in] rtcp pointer to RTC driver structure
+ * @param[out] timp pointer to a @p tm structure defined in time.h
+ *
+ * @api
+ */
+void rtcSetTimeTm(RTCDriver *rtcp, struct tm *timp){
+ RTCTime timespec = {0,0,FALSE,0};
+
+ timespec.tv_sec = mktime(timp);
+ timespec.tv_msec = 0;
+ rtcSetTime(rtcp, &timespec);
+}
+
+/**
+ * @brief Gets raw time from RTC and converts it to unix format.
+ *
+ * @param[in] rtcp pointer to RTC driver structure
+ *
+ * @return Unix time value in seconds.
+ *
+ * @api
+ */
+time_t rtcGetTimeUnixSec(RTCDriver *rtcp){
+ RTCTime timespec = {0,0,FALSE,0};
+
+ rtcGetTime(rtcp, &timespec);
+ return timespec.tv_sec;
+}
+
+/**
+ * @brief Sets RTC time.
+ *
+ * @param[in] rtcp pointer to RTC driver structure
+ *
+ * @return Unix time value in seconds.
+ *
+ * @api
+ */
+void rtcSetTimeUnixSec(RTCDriver *rtcp, time_t tv_sec){
+ RTCTime timespec = {0,0,FALSE,0};
+
+ timespec.tv_sec = tv_sec;
+ timespec.tv_msec = 0;
+ rtcSetTime(rtcp, &timespec);
+}
+
+/**
+ * @brief Gets raw time from RTC and converts it to unix format.
+ *
+ * @param[in] rtcp pointer to RTC driver structure
+ *
+ * @return Unix time value in microseconds.
+ *
+ * @api
+ */
+uint64_t rtcGetTimeUnixUsec(RTCDriver *rtcp){
+ uint64_t result = 0;
+ RTCTime timespec = {0,0,FALSE,0};
+
+ rtcGetTime(rtcp, &timespec);
+ result = timespec.tv_sec * 1000000;
+
+#if STM32_RTC_HAS_SUBSECONDS
+ return result + timespec.tv_msec * 1000;
+#else
+ return result;
+#endif
+}
+#endif /* STM32_RTC_IS_CALENDAR */
+#endif /* (defined(STM32F4XX) || defined(STM32F2XX) || defined(STM32L1XX) || defined(STM32F1XX)) */
+
+/**
+ * @brief Get current time in format suitable for usage in FatFS.
+ *
+ * @param[in] timespec pointer to time value structure
+ * @return FAT time value.
+ *
+ * @api
+ */
+uint32_t rtcGetTimeFat(RTCDriver *rtcp){
+ uint32_t fattime = 0;
+ struct tm *timp = NULL;
+
+ rtcGetTimeTm(rtcp, timp);
+
+ fattime |= (timp->tm_sec / 2);
+ 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;
+}
diff --git a/os/various/chrtclib.h b/os/various/chrtclib.h
new file mode 100755
index 000000000..7831be83d
--- /dev/null
+++ b/os/various/chrtclib.h
@@ -0,0 +1,56 @@
+/*
+ ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010,
+ 2011,2012 Giovanni Di Sirio.
+
+ This file is part of ChibiOS/RT.
+
+ ChibiOS/RT is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ ChibiOS/RT is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+/*
+ Concepts and parts of this file have been contributed by Uladzimir Pylinsky
+ aka barthess.
+ */
+
+#ifndef CHRTCLIB_H_
+#define CHRTCLIB_H_
+
+#include <time.h>
+
+/**
+ * @file chrtclib.h
+ * @brief Various time conversion functionality.
+ *
+ * @addtogroup chrtclib
+ * @{
+ */
+
+/*===========================================================================*/
+/* External declarations. */
+/*===========================================================================*/
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+ uint32_t rtcGetTimeFat(RTCDriver *rtcp);
+ void rtcGetTimeTm(RTCDriver *rtcp, struct tm *timp);
+ void rtcSetTimeTm(RTCDriver *rtcp, struct tm *timp);
+ time_t rtcGetTimeUnixSec(RTCDriver *rtcp);
+ void rtcSetTimeUnixSec(RTCDriver *rtcp, time_t tv_sec);
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* CHRTCLIB_H_ */
+
+/** @} */
diff --git a/testhal/STM32F1xx/RTC/Makefile b/testhal/STM32F1xx/RTC/Makefile
index 9fc777248..3d28124f2 100644
--- a/testhal/STM32F1xx/RTC/Makefile
+++ b/testhal/STM32F1xx/RTC/Makefile
@@ -79,6 +79,7 @@ CSRC = $(PORTSRC) \
$(BOARDSRC) \
$(CHIBIOS)/os/various/evtimer.c \
$(CHIBIOS)/os/various/syscalls.c \
+ $(CHIBIOS)/os/various/chrtclib.c \
main.c \
# C++ sources that can be compiled in ARM or THUMB mode depending on the global
diff --git a/testhal/STM32F1xx/RTC/main.c b/testhal/STM32F1xx/RTC/main.c
index bd3faa493..380afdad3 100644
--- a/testhal/STM32F1xx/RTC/main.c
+++ b/testhal/STM32F1xx/RTC/main.c
@@ -21,6 +21,8 @@
#include "ch.h"
#include "hal.h"
+#include "chrtclib.h"
+
RTCTime timespec;
RTCAlarm alarmspec;
diff --git a/testhal/STM32F4xx/RTC/Makefile b/testhal/STM32F4xx/RTC/Makefile
index e60937914..82511a750 100644
--- a/testhal/STM32F4xx/RTC/Makefile
+++ b/testhal/STM32F4xx/RTC/Makefile
@@ -81,6 +81,7 @@ CSRC = $(PORTSRC) \
$(CHIBIOS)/os/various/syscalls.c \
$(CHIBIOS)/os/various/shell.c \
$(CHIBIOS)/os/various/chprintf.c \
+ $(CHIBIOS)/os/various/chrtclib.c \
main.c \
# C++ sources that can be compiled in ARM or THUMB mode depending on the global
diff --git a/testhal/STM32F4xx/RTC/main.c b/testhal/STM32F4xx/RTC/main.c
index 5bc51c7b6..ce3e7f13f 100644
--- a/testhal/STM32F4xx/RTC/main.c
+++ b/testhal/STM32F4xx/RTC/main.c
@@ -43,11 +43,11 @@ int tm_isdst daylight savings indicator (1 = yes, 0 = no, -1 = unknown)
#include "shell.h"
#include "chprintf.h"
+#include "chrtclib.h"
#if WAKEUP_TEST
static RTCWakeup wakeupspec;
#endif
-static RTCTime timespec = {0,0,FALSE,0};
static RTCAlarm alarmspec;
static time_t unix_time;
@@ -147,15 +147,14 @@ static void cmd_date(BaseChannel *chp, int argc, char *argv[]){
}
if ((argc == 1) && (strcmp(argv[0], "get") == 0)){
- rtcGetTime(&RTCD1, &timespec);
- stm32_rtc_bcd2tm(&timp, &timespec);
- unix_time = mktime(&timp);
+ unix_time = rtcGetTimeUnixSec(&RTCD1);
if (unix_time == -1){
chprintf(chp, "incorrect time in RTC cell\r\n");
}
else{
chprintf(chp, "%D%s",unix_time," - unix time\r\n");
+ rtcGetTimeTm(&RTCD1, &timp);
chprintf(chp, "%s%s",asctime(&timp)," - formatted time string\r\n");
}
return;
@@ -164,8 +163,7 @@ static void cmd_date(BaseChannel *chp, int argc, char *argv[]){
if ((argc == 2) && (strcmp(argv[0], "set") == 0)){
unix_time = atol(argv[1]);
if (unix_time > 0){
- stm32_rtc_tm2bcd((localtime(&unix_time)), &timespec);
- rtcSetTime(&RTCD1, &timespec);
+ rtcSetTimeUnixSec(&RTCD1, unix_time);
return;
}
else{
diff --git a/testhal/STM32F4xx/SDC/Makefile b/testhal/STM32F4xx/SDC/Makefile
index ed147a37e..cf4d7b333 100755
--- a/testhal/STM32F4xx/SDC/Makefile
+++ b/testhal/STM32F4xx/SDC/Makefile
@@ -86,6 +86,7 @@ CSRC = $(PORTSRC) \
$(CHIBIOS)/os/various/syscalls.c \
$(CHIBIOS)/os/various/shell.c \
$(CHIBIOS)/os/various/chprintf.c \
+ $(CHIBIOS)/os/various/chrtclib.c \
main.c
# C++ sources that can be compiled in ARM or THUMB mode depending on the global