Files
timely-reference/firmware/Bsp/Drivers/RtcDriver.cpp

460 lines
12 KiB
C++

/*
* Copyright (C) 2019 Max Regan
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <new>
#include "Bsp/Drivers/RtcDriver.h"
#include "Bsp/macros.h"
namespace BSP {
using BSP::ReturnCode;
using BSP::time_t;
RtcDriver::RtcSystemTimer RtcDriver::m_sys_timer;
ReturnCode RtcDriver::init()
{
m_alarm_count = 0;
m_wakeup_count = 0;
init_hw();
return ReturnCode::OK;
}
void RtcDriver::enable_rtc_write()
{
/*<! Disable write protection */
RTC->WPR = 0xCA;
RTC->WPR = 0x53;
}
void RtcDriver::disable_rtc_write()
{
/*<! Enable write protection */
RTC->WPR = 0x00;
}
void RtcDriver::enable_periodic_alarm()
{
SET(RTC->ALRMAR, RTC_ALRMAR_MSK4 | RTC_ALRMAR_MSK3 | RTC_ALRMAR_MSK2 | RTC_ALRMAR_MSK1);
// Only calculate alarms when second rolls over
SET_TO(RTC->ALRMASSR, RTC_ALRMASSR_MASKSS, RTC_ALRMASSR_MASKSS);
CLR(RTC->CR, RTC_CR_ALRAE | RTC_CR_ALRAIE);
#if defined(STM32L0XX)
SET(EXTI->IMR, EXTI_IMR_IM17);
SET(EXTI->RTSR, EXTI_RTSR_RT17);
#elif defined(STM32L4XX)
SET(EXTI->IMR1, EXTI_IMR1_IM18);
SET(EXTI->RTSR1, EXTI_RTSR1_RT18);
NVIC_EnableIRQ(RTC_Alarm_IRQn);
NVIC_SetPriority(RTC_Alarm_IRQn, 0);
SET(RTC->SCR, RTC_SCR_CALRAF);
#else
#error "Unsupported family"
#endif
SET(RTC->CR, RTC_CR_ALRAE);
SET(RTC->CR, RTC_CR_ALRAIE);
}
ReturnCode RtcDriver::init_hw()
{
#if defined(STM32L0XX)
uint32_t temp = RCC->CSR;
SET(RCC->CSR, RCC_CSR_RTCRST);
SET(RCC->APB1ENR, RCC_APB1ENR_PWREN);
SET(PWR->CR, PWR_CR_DBP);
/*<! Set RTC input clock to the LSE (low-speed external 32.768kHz) clock */
if (!(RCC->CSR & RCC_CSR_LSERDY)) {
// TODO: Does this help?
SET(temp, RCC_CSR_LSEON);
}
SET_TO(temp, RCC_CSR_RTCSEL, RCC_CSR_RTCSEL_0);
SET(temp, RCC_CSR_RTCEN);
RCC->CSR = temp;
while (!(RCC->CSR & RCC_CSR_LSERDY)) {}
enable_rtc_write();
RTC->ISR = RTC_ISR_INIT;
while (!(RTC->ISR & RTC_ISR_INITF)) {}
#elif defined(STM32L4XX)
uint32_t temp = RCC->CSR;
// Reset the backup domain (includes the RTC)
SET(RCC->BDCR, RCC_BDCR_BDRST);
CLR(RCC->BDCR, RCC_BDCR_BDRST);
SET(RCC->APB1ENR1, RCC_APB1ENR1_PWREN);
SET(PWR->CR1, PWR_CR1_DBP);
SET(temp, RCC_BDCR_LSEON);
// while (!(RCC->BDCR & RCC_BDCR_LSERDY)) {}
SET_TO(temp, RCC_BDCR_RTCSEL, RCC_BDCR_RTCSEL_0);
SET(temp, RCC_BDCR_RTCEN);
RCC->BDCR = temp;
while (!(RCC->BDCR & RCC_BDCR_LSERDY)) {}
enable_rtc_write();
RTC->ICSR = RTC_ICSR_INIT;
while (!(RTC->ICSR & RTC_ICSR_INITF)) {}
#else
#error "Unsupported device type"
#endif
/*<! Set the Clock Prescalers (32.768kHz / 1 / 32768 = 1Hz */
/*<! Set the Async prescaler to the Maximum (divide the clock by 128) */
SET_TO(RTC->PRER, RTC_PRER_PREDIV_A, 0);
/*<! Set the Syncronous scaler so the RTC updates at 1Hz */
SET_TO(RTC->PRER, RTC_PRER_PREDIV_S, (LSE_CLOCK_FREQ - 1));
/*<! Load initial date and time */
// 24-Hour format
CLR(RTC->CR, RTC_CR_FMT);
uint32_t time = 0;
SET(time, RTC_TR_PM);
SET_TO(time, RTC_TR_HT, 1 << RTC_TR_HT_Pos);
SET_TO(time, RTC_TR_HU, 0 << RTC_TR_HU_Pos);
SET_TO(time, RTC_TR_MNT, 0 << RTC_TR_MNT_Pos);
SET_TO(time, RTC_TR_MNU, 9 << RTC_TR_MNU_Pos);
SET_TO(time, RTC_TR_ST, 3 << RTC_TR_ST_Pos);
SET_TO(time, RTC_TR_SU, 6 << RTC_TR_SU_Pos);
RTC->TR = time;
#if defined(STM32L0XX)
CLR(RTC->ISR, RTC_ISR_INIT);
SET(EXTI->IMR, EXTI_IMR_IM20);
SET(EXTI->RTSR, EXTI_RTSR_RT20);
// Enable Wakeup irq, we may/will use them later
SET(RTC->CR, RTC_CR_WUTIE);
NVIC_EnableIRQ(RTC_IRQn);
NVIC_SetPriority(RTC_IRQn, 1);
enable_periodic_alarm();
// Clear the interrupt in the RTC
CLR(RTC->ISR, RTC_ISR_WUTF);
#elif defined(STM32L4XX)
CLR(RTC->ICSR, RTC_ICSR_INIT);
SET(EXTI->IMR1, EXTI_IMR1_IM20);
SET(EXTI->RTSR1, EXTI_RTSR1_RT20);
// Enable Wakeup irq, we may/will use them later
SET(RTC->CR, RTC_CR_WUTIE);
NVIC_EnableIRQ(RTC_WKUP_IRQn);
NVIC_SetPriority(RTC_WKUP_IRQn, 0);
enable_periodic_alarm();
// Clear the interrupt in the RTC
SET(RTC->SCR, RTC_SCR_CWUTF);
#else
#error "Unsupported device type"
#endif
// Disable the wakeup timer. This can be leftover from an old firmware/reset
CLR(RTC->CR, RTC_CR_WUTE);
disable_rtc_write();
return ReturnCode::OK;
}
ReturnCode RtcDriver::get_time(BSP::WallClockTime &wall_time)
{
/*<! The value of TR in the shadow register is locked when SSR is
read (by the system timer), until the date register is read. We're
not using the date here, but we do need to clear the stale value. */
(void) RTC->DR;
// TODO: reread TR + PM for consistency
uint32_t time = RTC->TR;
unsigned int hours = 0, minutes = 0, seconds = 0;
hours += 10 * STM32_GET_FIELD(time, RTC_TR_HT);
hours += STM32_GET_FIELD(time, RTC_TR_HU);
minutes += 10 * STM32_GET_FIELD(time, RTC_TR_MNT);
minutes += STM32_GET_FIELD(time, RTC_TR_MNU);
seconds += 10 * STM32_GET_FIELD(time, RTC_TR_ST);
seconds += STM32_GET_FIELD(time, RTC_TR_SU);
new (&wall_time) BSP::WallClockTime(hours, minutes, seconds);
return ReturnCode::OK;
}
ReturnCode RtcDriver::set_time(const BSP::WallClockTime &wall_time)
{
enable_rtc_write();
#if defined(STM32L0XX)
RTC->ISR = RTC_ISR_INIT;
while (!(RTC->ISR & RTC_ISR_INITF)) {}
#elif defined(STM32L4XX)
RTC->ICSR = RTC_ICSR_INIT;
while (!(RTC->ICSR & RTC_ICSR_INITF)) {}
#else
#error "Unsupported device type"
#endif
/*<! Load initial date and time */
uint32_t time = 0;
SET_TO(time, RTC_TR_HT, wall_time.get_hours_24_tens() << RTC_TR_HT_Pos);
SET_TO(time, RTC_TR_HU, wall_time.get_hours_24_ones() << RTC_TR_HU_Pos);
SET_TO(time, RTC_TR_MNT, wall_time.get_minutes_tens() << RTC_TR_MNT_Pos);
SET_TO(time, RTC_TR_MNU, wall_time.get_minutes_ones() << RTC_TR_MNU_Pos);
SET_TO(time, RTC_TR_ST, wall_time.get_seconds_tens() << RTC_TR_ST_Pos);
SET_TO(time, RTC_TR_SU, wall_time.get_seconds_ones() << RTC_TR_SU_Pos);
RTC->TR = time;
#if defined(STM32L0XX)
CLR(RTC->ISR, RTC_ISR_INIT);
while (!(RTC->ISR & RTC_ISR_INITF)) {} // FIXME: this is probably inverted
while (!(RTC->ISR & RTC_ISR_RSF)) {}
#elif defined(STM32L4XX)
CLR(RTC->ICSR, RTC_ICSR_INIT);
while ((RTC->ICSR & RTC_ICSR_INITF)) {}
while (!(RTC->ICSR & RTC_ICSR_RSF)) {}
#else
#error "Unsupported device type"
#endif
disable_rtc_write();
return ReturnCode::OK;
}
ReturnCode RtcDriver::set_wakeup_in(BSP::time_t wakeup_delay)
{
/*<! 2^64 / (1000000 * 32768) / 60 / 60 / 24 / 365 = ~17.85 This
value will only overflow for wakeup_delays > 17.85 years, so
this is fine. */
uint64_t delay_cycles = BSP::Time::to_micros(wakeup_delay) * LSE_CLOCK_FREQ /
BSP::Time::MICROS_PER_SEC;
enable_rtc_write();
/*<! If there is an ongoing wakeup, disable it */
if (RTC->CR & RTC_CR_WUTE) {
CLR(RTC->CR, RTC_CR_WUTE);
#if defined(STM32L0XX)
while (!(RTC->ISR & RTC_ISR_WUTWF)) {}
#elif defined(STM32L4XX)
while (!(RTC->ICSR & RTC_ICSR_WUTWF)) {}
#else
#error "Unsupported device type"
#endif
}
uint32_t wucksel = 0;
if (delay_cycles == 0) {
return ReturnCode::FAIL;
} else if (delay_cycles < 0x20000) {
delay_cycles /= 2;
wucksel = 3;
} else if (delay_cycles <= 0x40000) {
delay_cycles /= 4;
wucksel = 2;
} else if (delay_cycles <= 0x80000) {
delay_cycles /= 8;
wucksel = 1;
} else if (delay_cycles <= 0x100000) {
delay_cycles /= 16;
wucksel = 0;
} else {
wucksel = 4;
delay_cycles /= 32768;
}
SET_TO(RTC->WUTR, RTC_WUTR_WUT, delay_cycles - 1);
SET_TO(RTC->CR, RTC_CR_WUCKSEL, wucksel << RTC_CR_WUCKSEL_Pos);
// Clear any pending wakeup flags
#if defined(STM32L0XX)
CLR(RTC->ISR, RTC_ISR_WUTF);
#elif defined(STM32L4XX)
SET(RTC->SCR, RTC_SCR_CWUTF);
#else
#error "Unsupported family"
#endif
// Finally, enable the timer
SET(RTC->CR, RTC_CR_WUTE);
disable_rtc_write();
return ReturnCode::OK;
}
BSP::time_t RtcDriver::RtcSystemTimer::get_time()
{
uint32_t new_secs, old_secs, ssr;
uint64_t new_timer_ticks, new_millis;
enable_rtc_write();
#if defined(STM32L0XX)
CLR(RTC->ISR, RTC_ISR_RSF);
disable_rtc_write();
while (!(RTC->ISR & RTC_ISR_RSF)) {}
#elif defined(STM32L4XX)
CLR(RTC->ICSR, RTC_ICSR_RSF);
disable_rtc_write();
while (!(RTC->ICSR & RTC_ICSR_RSF)) {}
#else
#error "Unsupported family"
#endif
do {
__disable_irq();
old_secs = m_seconds;
ssr = RTC->SSR & 0xFFFF;
new_secs = m_seconds;
__enable_irq();
} while (new_secs != old_secs);
new_timer_ticks = (uint64_t) new_secs * LSE_CLOCK_FREQ;
/** SSR is a countdown register */
new_millis = (new_timer_ticks + LSE_CLOCK_FREQ - 1 - ssr) * BSP::Time::MILLIS_PER_SEC / LSE_CLOCK_FREQ;
return BSP::Time::millis(new_millis);
}
void RtcDriver::RtcSystemTimer::increment_seconds()
{
m_seconds++;
}
void RtcDriver::increment_seconds()
{
m_sys_timer.increment_seconds();
}
uint32_t RtcDriver::m_wakeup_count = 0;
uint32_t RtcDriver::m_alarm_count = 0;
void RtcDriver::increment_wakeup_count()
{
m_wakeup_count++;
}
void RtcDriver::increment_alarm_count()
{
m_alarm_count++;
}
uint32_t RtcDriver::get_wakeup_count() {
return m_wakeup_count;
}
uint32_t RtcDriver::get_alarm_count() {
return m_alarm_count;
}
#if defined(STM32L0XX)
extern "C" void RTC_IRQHandler()
{
// Clear the wakeup and alarm irq in the EXTI
SET(EXTI->PR, EXTI_PR_PIF20 | EXTI_PR_PIF17);
if (RTC->ISR & RTC_ISR_ALRAF) {
RtcDriver::increment_seconds();
RtcDriver::increment_alarm_count();
CLR(RTC->ISR, RTC_ISR_ALRAF);
}
if (RTC->ISR & RTC_ISR_WUTF) {
RtcDriver::increment_wakeup_count();
// Clear the interrupt in the RTC
CLR(RTC->ISR, RTC_ISR_WUTF);
// Disable the Wakeup timer (its periodic, but we use it as a
// one-shot timer
CLR(RTC->CR, RTC_CR_WUTE);
}
}
#elif defined(STM32L4XX)
static void irq_handler() {
if (RTC->SR & RTC_ICSR_WUTWF) {
SET(EXTI->PR1, EXTI_PR1_PIF20);
RtcDriver::increment_wakeup_count();
// Clear the interrupt in the RTC
SET(RTC->SCR, RTC_SCR_CWUTF);
// Disable the Wakeup timer (its periodic, but we use it as a
// one-shot timer
CLR(RTC->CR, RTC_CR_WUTE);
}
if (RTC->SR & RTC_SR_ALRAF) {
SET(EXTI->PR1, EXTI_PR1_PIF18);
RtcDriver::increment_alarm_count();
RtcDriver::increment_seconds();
SET(RTC->SCR, RTC_SCR_CALRAF);
}
}
extern "C" void RTC_WKUP_IRQHandler()
{
irq_handler();
}
extern "C" void RTC_ALARM_IRQHandler()
{
irq_handler();
}
#else
#error "Unsupported device type"
#endif
}