From 24e412446dc0cc2e95037a6afd5db6df9715fac8 Mon Sep 17 00:00:00 2001 From: Steins7 Date: Sun, 4 Aug 2024 20:04:09 +0200 Subject: [PATCH] Implement bkp's rtc alarm configuration Since the alarm must be set each time it is to be used, a separated function is a better fit --- drv/bkp.c | 25 ++++++++++++++++++++++--- drv/bkp.h | 15 ++++++++++++--- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/drv/bkp.c b/drv/bkp.c index c6b93a4..69630b9 100644 --- a/drv/bkp.c +++ b/drv/bkp.c @@ -33,7 +33,7 @@ static BkpRtcCallback rtc_callback; //--public functions------------------------------------------------------------ void bkp_configure_rtc(uint32_t period_ms, enum BkpRtcClockSrc clock_src, - enum BkpRtcIrq irq_mask, uint32_t alarm_tick, BkpRtcCallback callback) + enum BkpRtcIrq irq_mask, BkpRtcCallback callback) { rcc_enable(RCC_AHB_NONE, RCC_APB1_BKP, RCC_APB2_NONE); @@ -55,8 +55,6 @@ void bkp_configure_rtc(uint32_t period_ms, enum BkpRtcClockSrc clock_src, //configure core registers rtc_regs->PRLH.PRL = prescaler >> 16; rtc_regs->PRLL.PRL = prescaler; - rtc_regs->ALRH.RTC_ALR = alarm_tick >> 16; - rtc_regs->ALRL.RTC_ALR = alarm_tick; //apply irq config rtc_regs->CRH.word |= irq_mask & 0x7; @@ -73,6 +71,27 @@ void bkp_configure_rtc(uint32_t period_ms, enum BkpRtcClockSrc clock_src, } } +void bkp_set_rtc_alam(uint32_t offset) +{ + uint32_t alarm = bkp_read_rtc() + offset; + + //wait for last operation to finish + while (rtc_regs->CRL.RTOFF != 1) {} + + //enable core configuration + rtc_regs->CRL.CNF = 1; + + //write alarm value + rtc_regs->ALRH.RTC_ALR = alarm >> 16; + rtc_regs->ALRL.RTC_ALR = alarm; + + //disable/apply core configuration + rtc_regs->CRL.CNF = 0; + + //wait for last operation to finish + while (rtc_regs->CRL.RTOFF != 1) {} +} + uint32_t bkp_read_rtc(void) { //wait for core registers to be synchronized, immediate most of the time diff --git a/drv/bkp.h b/drv/bkp.h index b78994e..28da3f8 100644 --- a/drv/bkp.h +++ b/drv/bkp.h @@ -63,14 +63,23 @@ typedef void (*BkpRtcCallback)(enum BkpRtcIrq src); * Clocks must be enabled prior to calling this function * * Mulitple IRQs can be enabled and redirected to single callback. The alarm - * IRQ, triggered at the specified tick value can be rerouted to an exti line - * for wakeup from stanby, in wich case it doesn't need to be enabled here. + * IRQ, triggered at the tick value specified by bkp_set_rtc_alam() can be + * rerouted to an exti line for wakeup from stop and standby, in wich case it + * doesn't need to be enabled here. * * Ensure backup domain writes are enabled (see pwr_configure_bkp_write()) while * calling this function */ void bkp_configure_rtc(uint32_t period_ms, enum BkpRtcClockSrc clock_src, - enum BkpRtcIrq irq_mask, uint32_t alarm_tick, BkpRtcCallback callback); + enum BkpRtcIrq irq_mask, BkpRtcCallback callback); + +/** + * Configure the alarm to trigger after the specified time in ticks, which can + * be viewed as an offset to the current time value. This offset is only precise + * to the tick and thus if the tick changes right after this function is + * called, the observed offset will be close to 1 tick short + */ +void bkp_set_rtc_alam(uint32_t offset); /** * Returns the current counter value of the RTC