Source code for the Trusted Boot Module.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
tbm-mcu/source/platform/stm32f0/alarm.c

78 lines
1.5 KiB

#include <libopencm3/cm3/nvic.h>
#include <libopencm3/stm32/exti.h>
#include <libopencm3/stm32/rtc.h>
static inline uint32_t rtc_to_sec(uint32_t rtc)
{
uint32_t sec;
sec = (rtc & 0xf) + ((rtc >> 4) & 0xf) * 10;
sec += (((rtc >> 8) & 0xf) + ((rtc >> 12) & 0xf) * 10) * 60;
sec += (((rtc >> 16) & 0xf) + ((rtc >> 20) & 0xf) * 10) * 3600;
return sec;
}
static inline uint32_t sec_to_rtc(uint32_t sec)
{
uint32_t rtc;
uint32_t hour, min;
hour = sec / 3600;
min = (sec % 3600) / 60;
sec = sec % 60;
rtc = ((hour / 10) << 20) | ((hour % 10) << 16);
rtc |= ((min / 10) << 12) | ((min % 10) << 8);
rtc |= ((sec / 10) << 4) | (sec % 10);
return rtc;
}
void rtc_alarm_isr(void)
{
uint32_t alarm_sec;
exti_reset_request(EXTI17);
rtc_unlock();
/* Disable the alarm. */
RTC_CR &= ~RTC_CR_ALRAE;
while (!(RTC_ISR & RTC_ISR_ALRAWF));
RTC_ISR &= ~RTC_ISR_ALRAF;
/* Set the alarm time. */
alarm_sec = rtc_to_sec(RTC_TR) + 10;
RTC_ALRMAR = sec_to_rtc(alarm_sec);
RTC_ALRMAR |= 0xc0000000;
/* Enable the alarm. */
EXTI_IMR |= EXTI17;
exti_set_trigger(EXTI17, EXTI_TRIGGER_RISING);
RTC_CR |= RTC_CR_ALRAE;
rtc_lock();
}
int alarm_init(void)
{
uint32_t alarm_sec;
rtc_unlock();
/* Set the alarm time. */
alarm_sec = rtc_to_sec(RTC_TR) + 10;
RTC_ALRMAR = sec_to_rtc(alarm_sec);
RTC_ALRMAR |= 0xc0000000;
RTC_CR |= RTC_CR_ALRAIE | RTC_CR_BYPSHAD;
nvic_enable_irq(NVIC_RTC_IRQ);
EXTI_IMR |= EXTI17;
exti_set_trigger(EXTI17, EXTI_TRIGGER_RISING);
rtc_lock();
return 0;
}