aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Aherne2013-02-26 21:22:19 -0800
committerMichael Aherne2013-02-26 21:22:58 -0800
commitdd756332457b900a88999f7ae7158513233061f6 (patch)
treeb3dd541dfafa1508d256257f7754264b5cfd6b05
parenta121769785c1cde6a5599ba3449021b673144d9c (diff)
Adding some useful functions for wakeup timer interrupts
-rw-r--r--include/libopencm3/stm32/common/rtc_common_bcd.h2
-rw-r--r--lib/stm32/common/rtc_common_bcd.c31
2 files changed, 33 insertions, 0 deletions
diff --git a/include/libopencm3/stm32/common/rtc_common_bcd.h b/include/libopencm3/stm32/common/rtc_common_bcd.h
index 6448468..69a46f5 100644
--- a/include/libopencm3/stm32/common/rtc_common_bcd.h
+++ b/include/libopencm3/stm32/common/rtc_common_bcd.h
@@ -305,6 +305,8 @@ void rtc_set_prescaler(u32 sync, u32 async);
void rtc_wait_for_synchro(void);
void rtc_lock(void);
void rtc_unlock(void);
+void rtc_set_wakeup_time(u16 wkup_time, u8 rtc_cr_wucksel);
+void rtc_clear_wakeup_flag(void);
END_DECLS
/**@}*/
diff --git a/lib/stm32/common/rtc_common_bcd.c b/lib/stm32/common/rtc_common_bcd.c
index c302ea2..cddfe9b 100644
--- a/lib/stm32/common/rtc_common_bcd.c
+++ b/lib/stm32/common/rtc_common_bcd.c
@@ -75,4 +75,35 @@ void rtc_lock(void) {
RTC_WPR = 0xff;
}
+/*---------------------------------------------------------------------------*/
+/** @brief Sets the wakeup time auto-reload value
+
+*/
+void rtc_set_wakeup_time(u16 wkup_time, u8 rtc_cr_wucksel) {
+// FTFM:
+// The following sequence is required to configure or change the wakeup timer
+// auto-reload value (WUT[15:0] in RTC_WUTR):
+// 1. Clear WUTE in RTC_CR to disable the wakeup timer.
+ RTC_CR &= ~RTC_CR_WUTE;
+// 2. Poll WUTWF until it is set in RTC_ISR to make sure the access to wakeup
+// auto-reload counter and to WUCKSEL[2:0] bits is allowed. It takes around 2
+// RTCCLK clock cycles (due to clock synchronization).
+ while (!((RTC_ISR) & (RTC_ISR_WUTWF))) { }
+// 3. Program the wakeup auto-reload value WUT[15:0], and the wakeup clock
+// selection (WUCKSEL[2:0] bits in RTC_CR).Set WUTE in RTC_CR to enable the
+// timer again. The wakeup timer restarts down-counting.
+ RTC_WUTR = wkup_time;
+ RTC_CR |= (rtc_cr_wucksel << RTC_CR_WUCLKSEL_SHIFT);
+ RTC_CR |= RTC_CR_WUTE;
+}
+
+/*---------------------------------------------------------------------------*/
+/** @brief Clears the wakeup flag
+
+ @details This function should be called first in the rtc_wkup_isr()
+*/
+void rtc_clear_wakeup_flag(void) {
+ RTC_ISR &= ~RTC_ISR_WUTF;
+}
+
/**@}*/