2011-08-31 15:31:32 +00:00
|
|
|
/*
|
|
|
|
ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010,
|
|
|
|
2011 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/>.
|
|
|
|
*/
|
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__)
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* 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-12 18:26:26 +00:00
|
|
|
static void rtc_lld_apb1_sync(void) {
|
2011-10-01 08:04:14 +00:00
|
|
|
|
2012-01-12 18:26:26 +00:00
|
|
|
while ((RTC->CRL & RTC_CRL_RSF) == 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
|
|
|
|
* writing to PRL, CNT, ALR registers. CR registers can always
|
|
|
|
* be written.
|
2011-10-01 08:04:14 +00:00
|
|
|
*
|
|
|
|
* @notapi
|
|
|
|
*/
|
2012-01-11 20:07:19 +00:00
|
|
|
static void rtc_lld_acquire(void) {
|
2011-10-01 08:04:14 +00:00
|
|
|
|
2012-01-10 18:14:24 +00:00
|
|
|
while ((RTC->CRL & RTC_CRL_RTOFF) == 0)
|
2011-10-01 08:04:14 +00:00
|
|
|
;
|
2012-01-12 18:26:26 +00:00
|
|
|
RTC->CRL |= RTC_CRL_CNF;
|
2011-10-01 08:04:14 +00:00
|
|
|
}
|
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-12 18:36:16 +00:00
|
|
|
static void rtc_lld_release(void) {
|
2012-01-12 18:26:26 +00:00
|
|
|
|
|
|
|
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-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. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Enable access to registers and initialize RTC if BKP domain
|
|
|
|
* was previously reseted.
|
2011-09-21 17:42:30 +00:00
|
|
|
* @note: Cold start time of LSE oscillator on STM32 platform
|
|
|
|
* takes about 3 seconds.
|
|
|
|
*
|
2011-09-02 13:27:37 +00:00
|
|
|
* @notapi
|
2011-08-30 22:52:11 +00:00
|
|
|
*/
|
|
|
|
void rtc_lld_init(void){
|
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
|
|
|
|
2012-01-12 18:26:26 +00:00
|
|
|
/* Writes preload register only if its value is not equal to desired value.*/
|
2012-01-10 18:14:24 +00:00
|
|
|
if (STM32_RTCCLK != (((uint32_t)(RTC->PRLH)) << 16) +
|
|
|
|
((uint32_t)RTC->PRLL) + 1) {
|
2012-01-11 20:07:19 +00:00
|
|
|
rtc_lld_acquire();
|
2012-01-10 18:14:24 +00:00
|
|
|
RTC->PRLH = (uint16_t)((STM32_RTCCLK - 1) >> 16);
|
|
|
|
RTC->PRLL = (uint16_t)((STM32_RTCCLK - 1) & 0xFFFF);
|
2012-01-11 20:07:19 +00:00
|
|
|
rtc_lld_release();
|
2011-08-31 16:32:34 +00:00
|
|
|
}
|
|
|
|
|
2011-10-01 08:04:14 +00:00
|
|
|
/* All interrupts initially disabled.*/
|
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
|
|
|
|
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-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
|
|
|
}
|
|
|
|
}
|
2011-08-30 22:52:11 +00:00
|
|
|
|
|
|
|
#endif /* HAL_USE_RTC */
|
|
|
|
|
|
|
|
/** @} */
|