2011-08-31 15:31:32 +00:00
|
|
|
/*
|
|
|
|
ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010,
|
2013-02-02 10:58:09 +00:00
|
|
|
2011,2012,2013 Giovanni Di Sirio.
|
2011-08-31 15:31:32 +00:00
|
|
|
|
|
|
|
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/>.
|
|
|
|
*/
|
2011-12-29 18:07:02 +00:00
|
|
|
/*
|
|
|
|
Concepts and parts of this file have been contributed by Uladzimir Pylinsky
|
|
|
|
aka barthess.
|
|
|
|
*/
|
2011-08-31 15:31:32 +00:00
|
|
|
|
2011-08-30 22:52:11 +00:00
|
|
|
/**
|
2011-09-25 10:56:39 +00:00
|
|
|
* @file STM32/RTCv1/rtc_lld.c
|
2011-08-30 22:52:11 +00:00
|
|
|
* @brief STM32 RTC subsystem low level driver header.
|
|
|
|
*
|
|
|
|
* @addtogroup RTC
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ch.h"
|
|
|
|
#include "hal.h"
|
|
|
|
|
|
|
|
#if HAL_USE_RTC || defined(__DOXYGEN__)
|
|
|
|
|
2012-12-25 08:20:13 +00:00
|
|
|
/*===========================================================================*/
|
|
|
|
/* Driver local definitions. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
2011-08-30 22:52:11 +00:00
|
|
|
/*===========================================================================*/
|
|
|
|
/* Driver exported variables. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
2011-10-01 08:04:14 +00:00
|
|
|
/**
|
|
|
|
* @brief RTC driver identifier.
|
|
|
|
*/
|
|
|
|
RTCDriver RTCD1;
|
2011-08-30 22:52:11 +00:00
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Driver local variables. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Driver local functions. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/**
|
2012-01-12 18:26:26 +00:00
|
|
|
* @brief Wait for synchronization of RTC registers with APB1 bus.
|
|
|
|
* @details This function must be invoked before trying to read RTC registers
|
|
|
|
* in the backup domain: DIV, CNT, ALR. CR registers can always
|
|
|
|
* be read.
|
2011-09-02 13:27:37 +00:00
|
|
|
*
|
|
|
|
* @notapi
|
2011-08-30 22:52:11 +00:00
|
|
|
*/
|
2012-01-13 20:38:02 +00:00
|
|
|
#define rtc_lld_apb1_sync() {while ((RTC->CRL & RTC_CRL_RSF) == 0);}
|
2011-10-01 08:04:14 +00:00
|
|
|
|
2012-01-13 20:38:02 +00:00
|
|
|
/**
|
|
|
|
* @brief Wait for for previous write operation complete.
|
|
|
|
* @details This function must be invoked before writing to any RTC registers
|
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
|
|
|
#define rtc_lld_wait_write() {while ((RTC->CRL & RTC_CRL_RTOFF) == 0);}
|
2012-01-12 08:25:50 +00:00
|
|
|
|
|
|
|
/**
|
2012-01-12 18:26:26 +00:00
|
|
|
* @brief Acquires write access to RTC registers.
|
|
|
|
* @details Before writing to the backup domain RTC registers the previous
|
|
|
|
* write operation must be completed. Use this function before
|
2012-01-13 20:38:02 +00:00
|
|
|
* writing to PRL, CNT, ALR registers.
|
2011-10-01 08:04:14 +00:00
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
2012-01-13 20:38:02 +00:00
|
|
|
#define rtc_lld_acquire() {rtc_lld_wait_write(); RTC->CRL |= RTC_CRL_CNF;}
|
2011-08-30 22:52:11 +00:00
|
|
|
|
2012-01-11 20:07:19 +00:00
|
|
|
/**
|
2012-01-12 18:26:26 +00:00
|
|
|
* @brief Releases write access to RTC registers.
|
2012-01-11 20:07:19 +00:00
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
2012-01-13 20:38:02 +00:00
|
|
|
#define rtc_lld_release() {RTC->CRL &= ~RTC_CRL_CNF;}
|
2012-01-11 20:07:19 +00:00
|
|
|
|
2011-08-30 22:52:11 +00:00
|
|
|
/*===========================================================================*/
|
|
|
|
/* Driver interrupt handlers. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief RTC interrupt handler.
|
2011-10-01 08:04:14 +00:00
|
|
|
*
|
2011-08-30 22:52:11 +00:00
|
|
|
* @isr
|
|
|
|
*/
|
|
|
|
CH_IRQ_HANDLER(RTC_IRQHandler) {
|
2012-01-12 18:26:26 +00:00
|
|
|
uint16_t flags;
|
2011-10-01 08:04:14 +00:00
|
|
|
|
2011-08-30 22:52:11 +00:00
|
|
|
CH_IRQ_PROLOGUE();
|
2011-10-01 08:04:14 +00:00
|
|
|
|
2012-01-14 12:23:42 +00:00
|
|
|
/* This wait works only when AHB1 bus was previously powered off by any
|
|
|
|
reason (standby, reset, etc). In other cases it does nothing.*/
|
|
|
|
rtc_lld_apb1_sync();
|
|
|
|
|
2012-01-12 18:26:26 +00:00
|
|
|
/* Mask of all enabled and pending sources.*/
|
|
|
|
flags = RTC->CRH & RTC->CRL;
|
|
|
|
RTC->CRL &= ~(RTC_CRL_SECF | RTC_CRL_ALRF | RTC_CRL_OWF);
|
|
|
|
|
|
|
|
if (flags & RTC_CRL_SECF)
|
|
|
|
RTCD1.callback(&RTCD1, RTC_EVENT_SECOND);
|
|
|
|
|
|
|
|
if (flags & RTC_CRL_ALRF)
|
|
|
|
RTCD1.callback(&RTCD1, RTC_EVENT_ALARM);
|
|
|
|
|
|
|
|
if (flags & RTC_CRL_OWF)
|
|
|
|
RTCD1.callback(&RTCD1, RTC_EVENT_OVERFLOW);
|
2011-10-01 08:04:14 +00:00
|
|
|
|
2011-08-30 22:52:11 +00:00
|
|
|
CH_IRQ_EPILOGUE();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Driver exported functions. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/**
|
2012-12-08 18:24:20 +00:00
|
|
|
* @brief Load value of RTCCLK to prescaler registers.
|
|
|
|
* @note The pre-scaler must not be set on every reset as RTC clock
|
|
|
|
* counts are lost when it is set.
|
|
|
|
* @note This function designed to be called from
|
|
|
|
* hal_lld_backup_domain_init(). Because there is only place
|
|
|
|
* where possible to detect BKP domain reset event reliably.
|
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
|
|
|
void rtc_lld_set_prescaler(void){
|
|
|
|
rtc_lld_acquire();
|
|
|
|
RTC->PRLH = (uint16_t)((STM32_RTCCLK - 1) >> 16) & 0x000F;
|
|
|
|
RTC->PRLL = (uint16_t)(((STM32_RTCCLK - 1)) & 0xFFFF);
|
|
|
|
rtc_lld_release();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Initialize RTC.
|
2011-09-21 17:42:30 +00:00
|
|
|
*
|
2011-09-02 13:27:37 +00:00
|
|
|
* @notapi
|
2011-08-30 22:52:11 +00:00
|
|
|
*/
|
2012-12-08 17:14:28 +00:00
|
|
|
void rtc_lld_init(void){
|
|
|
|
|
|
|
|
/* RSF bit must be cleared by software after an APB1 reset or an APB1 clock
|
2013-02-02 10:58:09 +00:00
|
|
|
stop. Otherwise its value will not be actual. */
|
2012-12-08 17:14:28 +00:00
|
|
|
RTC->CRL &= ~RTC_CRL_RSF;
|
2011-10-01 08:04:14 +00:00
|
|
|
|
2012-01-12 18:26:26 +00:00
|
|
|
/* Required because access to PRL.*/
|
|
|
|
rtc_lld_apb1_sync();
|
2012-01-12 07:09:24 +00:00
|
|
|
|
2011-10-01 08:04:14 +00:00
|
|
|
/* All interrupts initially disabled.*/
|
2012-01-13 20:38:02 +00:00
|
|
|
rtc_lld_wait_write();
|
2012-01-12 18:26:26 +00:00
|
|
|
RTC->CRH = 0;
|
2012-01-12 07:09:24 +00:00
|
|
|
|
2011-10-01 08:04:14 +00:00
|
|
|
/* Callback initially disabled.*/
|
2012-01-07 07:06:49 +00:00
|
|
|
RTCD1.callback = NULL;
|
2012-01-12 18:26:26 +00:00
|
|
|
|
|
|
|
/* IRQ vector permanently assigned to this driver.*/
|
|
|
|
nvicEnableVector(RTC_IRQn, CORTEX_PRIORITY_MASK(STM32_RTC_IRQ_PRIORITY));
|
2011-08-30 22:52:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-10-01 08:04:14 +00:00
|
|
|
* @brief Set current time.
|
|
|
|
* @note Fractional part will be silently ignored. There is no possibility
|
|
|
|
* to change it on STM32F1xx platform.
|
2011-08-30 22:52:11 +00:00
|
|
|
*
|
2011-10-01 08:04:14 +00:00
|
|
|
* @param[in] rtcp pointer to RTC driver structure
|
|
|
|
* @param[in] timespec pointer to a @p RTCTime structure
|
2011-09-02 13:27:37 +00:00
|
|
|
*
|
|
|
|
* @notapi
|
2011-08-30 22:52:11 +00:00
|
|
|
*/
|
2011-10-01 08:04:14 +00:00
|
|
|
void rtc_lld_set_time(RTCDriver *rtcp, const RTCTime *timespec) {
|
2011-08-30 22:52:11 +00:00
|
|
|
|
2011-10-01 08:04:14 +00:00
|
|
|
(void)rtcp;
|
2011-08-30 22:52:11 +00:00
|
|
|
|
2012-01-11 20:07:19 +00:00
|
|
|
rtc_lld_acquire();
|
2011-10-01 08:04:14 +00:00
|
|
|
RTC->CNTH = (uint16_t)(timespec->tv_sec >> 16);
|
|
|
|
RTC->CNTL = (uint16_t)(timespec->tv_sec & 0xFFFF);
|
2012-01-11 20:07:19 +00:00
|
|
|
rtc_lld_release();
|
2011-08-30 22:52:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-10-01 08:04:14 +00:00
|
|
|
* @brief Get current time.
|
2011-08-30 22:52:11 +00:00
|
|
|
*
|
2011-10-01 08:04:14 +00:00
|
|
|
* @param[in] rtcp pointer to RTC driver structure
|
2011-12-10 19:41:46 +00:00
|
|
|
* @param[in] timespec pointer to a @p RTCTime structure
|
2011-09-02 13:27:37 +00:00
|
|
|
*
|
|
|
|
* @notapi
|
2011-08-30 22:52:11 +00:00
|
|
|
*/
|
2011-10-01 08:04:14 +00:00
|
|
|
void rtc_lld_get_time(RTCDriver *rtcp, RTCTime *timespec) {
|
|
|
|
(void)rtcp;
|
2011-08-30 22:52:11 +00:00
|
|
|
|
2011-12-10 19:41:46 +00:00
|
|
|
uint32_t time_frac;
|
|
|
|
|
2012-01-12 18:26:26 +00:00
|
|
|
/* Required because access to CNT and DIV.*/
|
|
|
|
rtc_lld_apb1_sync();
|
|
|
|
|
|
|
|
/* Loops until two consecutive read returning the same value.*/
|
2012-01-10 18:14:24 +00:00
|
|
|
do {
|
|
|
|
timespec->tv_sec = ((uint32_t)(RTC->CNTH) << 16) + RTC->CNTL;
|
|
|
|
time_frac = (((uint32_t)RTC->DIVH) << 16) + (uint32_t)RTC->DIVL;
|
|
|
|
} while ((timespec->tv_sec) != (((uint32_t)(RTC->CNTH) << 16) + RTC->CNTL));
|
2011-12-10 19:41:46 +00:00
|
|
|
|
2012-01-10 18:14:24 +00:00
|
|
|
timespec->tv_msec = (uint16_t)(((STM32_RTCCLK - 1 - time_frac) * 1000) /
|
|
|
|
STM32_RTCCLK);
|
2011-08-30 22:52:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-10-01 08:04:14 +00:00
|
|
|
* @brief Set alarm time.
|
|
|
|
*
|
|
|
|
* @note Default value after BKP domain reset is 0xFFFFFFFF
|
2011-09-02 13:27:37 +00:00
|
|
|
*
|
2011-10-01 08:04:14 +00:00
|
|
|
* @param[in] rtcp pointer to RTC driver structure
|
|
|
|
* @param[in] alarm alarm identifier
|
|
|
|
* @param[in] alarmspec pointer to a @p RTCAlarm structure
|
2011-09-02 13:27:37 +00:00
|
|
|
*
|
|
|
|
* @notapi
|
2011-08-30 22:52:11 +00:00
|
|
|
*/
|
2011-10-01 08:04:14 +00:00
|
|
|
void rtc_lld_set_alarm(RTCDriver *rtcp,
|
|
|
|
rtcalarm_t alarm,
|
|
|
|
const RTCAlarm *alarmspec) {
|
2011-09-25 10:03:59 +00:00
|
|
|
|
2011-10-01 08:04:14 +00:00
|
|
|
(void)rtcp;
|
|
|
|
(void)alarm;
|
|
|
|
|
2012-01-11 20:07:19 +00:00
|
|
|
rtc_lld_acquire();
|
2011-10-01 08:04:14 +00:00
|
|
|
if (alarmspec != NULL) {
|
|
|
|
RTC->ALRH = (uint16_t)(alarmspec->tv_sec >> 16);
|
|
|
|
RTC->ALRL = (uint16_t)(alarmspec->tv_sec & 0xFFFF);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
RTC->ALRH = 0;
|
|
|
|
RTC->ALRL = 0;
|
|
|
|
}
|
2012-01-11 20:07:19 +00:00
|
|
|
rtc_lld_release();
|
2011-08-30 22:52:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-10-01 08:04:14 +00:00
|
|
|
* @brief Get current alarm.
|
|
|
|
* @note If an alarm has not been set then the returned alarm specification
|
|
|
|
* is not meaningful.
|
2011-09-25 10:03:59 +00:00
|
|
|
*
|
2011-10-01 08:04:14 +00:00
|
|
|
* @note Default value after BKP domain reset is 0xFFFFFFFF.
|
2011-09-25 10:03:59 +00:00
|
|
|
*
|
2011-12-16 14:42:23 +00:00
|
|
|
* @param[in] rtcp pointer to RTC driver structure
|
|
|
|
* @param[in] alarm alarm identifier
|
2011-10-01 08:04:14 +00:00
|
|
|
* @param[out] alarmspec pointer to a @p RTCAlarm structure
|
2011-09-02 13:27:37 +00:00
|
|
|
*
|
|
|
|
* @notapi
|
2011-08-30 22:52:11 +00:00
|
|
|
*/
|
2011-10-01 08:04:14 +00:00
|
|
|
void rtc_lld_get_alarm(RTCDriver *rtcp,
|
|
|
|
rtcalarm_t alarm,
|
|
|
|
RTCAlarm *alarmspec) {
|
2011-08-30 22:52:11 +00:00
|
|
|
|
2011-10-01 08:04:14 +00:00
|
|
|
(void)rtcp;
|
|
|
|
(void)alarm;
|
2011-09-01 18:09:40 +00:00
|
|
|
|
2012-01-12 18:26:26 +00:00
|
|
|
/* Required because access to ALR.*/
|
|
|
|
rtc_lld_apb1_sync();
|
|
|
|
|
2011-10-01 08:04:14 +00:00
|
|
|
alarmspec->tv_sec = ((RTC->ALRH << 16) + RTC->ALRL);
|
2011-08-30 22:52:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-10-01 08:04:14 +00:00
|
|
|
* @brief Enables or disables RTC callbacks.
|
|
|
|
* @details This function enables or disables callbacks, use a @p NULL pointer
|
|
|
|
* in order to disable a callback.
|
2011-09-25 10:03:59 +00:00
|
|
|
*
|
2011-10-01 08:04:14 +00:00
|
|
|
* @param[in] rtcp pointer to RTC driver structure
|
|
|
|
* @param[in] callback callback function pointer or @p NULL
|
2011-09-02 13:27:37 +00:00
|
|
|
*
|
|
|
|
* @notapi
|
2011-08-30 22:52:11 +00:00
|
|
|
*/
|
2011-12-16 14:42:23 +00:00
|
|
|
void rtc_lld_set_callback(RTCDriver *rtcp, rtccb_t callback) {
|
|
|
|
|
|
|
|
if (callback != NULL) {
|
2012-01-12 18:26:26 +00:00
|
|
|
|
|
|
|
/* IRQ sources enabled only after setting up the callback.*/
|
2012-01-07 07:06:49 +00:00
|
|
|
rtcp->callback = callback;
|
2011-08-30 22:52:11 +00:00
|
|
|
|
2012-01-13 20:38:02 +00:00
|
|
|
rtc_lld_wait_write();
|
2011-10-01 08:04:14 +00:00
|
|
|
RTC->CRL &= ~(RTC_CRL_OWF | RTC_CRL_ALRF | RTC_CRL_SECF);
|
2012-01-12 18:26:26 +00:00
|
|
|
RTC->CRH = RTC_CRH_OWIE | RTC_CRH_ALRIE | RTC_CRH_SECIE;
|
2011-10-01 08:04:14 +00:00
|
|
|
}
|
|
|
|
else {
|
2012-01-13 20:38:02 +00:00
|
|
|
rtc_lld_wait_write();
|
2012-01-12 18:26:26 +00:00
|
|
|
RTC->CRH = 0;
|
|
|
|
|
|
|
|
/* Callback set to NULL only after disabling the IRQ sources.*/
|
|
|
|
rtcp->callback = NULL;
|
2011-10-01 08:04:14 +00:00
|
|
|
}
|
|
|
|
}
|
2012-09-21 09:26:41 +00:00
|
|
|
|
2012-09-21 11:59:21 +00:00
|
|
|
#include "chrtclib.h"
|
|
|
|
|
2012-09-21 09:26:41 +00:00
|
|
|
/**
|
|
|
|
* @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;
|
|
|
|
}
|
2011-08-30 22:52:11 +00:00
|
|
|
#endif /* HAL_USE_RTC */
|
|
|
|
|
|
|
|
/** @} */
|