update credits
[librepilot.git] / flight / pios / stm32f30x / pios_rtc.c
blob559fde9956b30b5ad14f9c9afa784efa417cbe80
1 /**
2 ******************************************************************************
3 * @addtogroup PIOS PIOS Core hardware abstraction layer
4 * @{
5 * @addtogroup PIOS_RTC RTC Functions
6 * @brief Code to manage initialization and interrups of RTC peripheral
7 * @{
9 * @file pios_rtc.c
10 * @author The LibrePilot Project, http://www.librepilot.org Copyright (C) 2017.
11 * The OpenPilot Team, http://www.openpilot.org Copyright (C) 2012.
12 * @brief RTC Functions (STM32 dependent)
13 * @see The GNU Public License (GPL) Version 3
15 *****************************************************************************/
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 3 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful, but
23 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
24 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
25 * for more details.
27 * You should have received a copy of the GNU General Public License along
28 * with this program; if not, write to the Free Software Foundation, Inc.,
29 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 #include "pios.h"
34 #ifdef PIOS_INCLUDE_RTC
36 #include <pios_rtc_priv.h>
38 #ifndef PIOS_RTC_PRESCALER
39 #define PIOS_RTC_PRESCALER 100
40 #endif
42 struct rtc_callback_entry {
43 void (*fn)(uint32_t);
44 uint32_t data;
47 #define PIOS_RTC_MAX_CALLBACKS 3
48 struct rtc_callback_entry rtc_callback_list[PIOS_RTC_MAX_CALLBACKS];
49 static uint8_t rtc_callback_next = 0;
51 void PIOS_RTC_Init(const struct pios_rtc_cfg *cfg)
53 RCC_BackupResetCmd(ENABLE);
54 RCC_BackupResetCmd(DISABLE);
55 RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
56 PWR_BackupAccessCmd(ENABLE);
57 // Divide external 8 MHz clock to 1 MHz
58 RCC_RTCCLKConfig(cfg->clksrc);
59 RCC_RTCCLKCmd(ENABLE);
61 RTC_WakeUpCmd(DISABLE);
62 // Divide 1 Mhz clock by 16 -> 62.5 khz
63 RTC_WakeUpClockConfig(RTC_WakeUpClock_RTCCLK_Div16);
64 // Divide 62.5 khz by 200 to get 625 Hz
65 RTC_SetWakeUpCounter(cfg->prescaler); // cfg->prescaler);
66 RTC_WakeUpCmd(ENABLE);
68 /* Configure and enable the RTC Second interrupt */
69 EXTI_InitTypeDef ExtiInit = {
70 .EXTI_Line = EXTI_Line20, // matches above GPIO pin
71 .EXTI_Mode = EXTI_Mode_Interrupt,
72 .EXTI_Trigger = EXTI_Trigger_Rising,
73 .EXTI_LineCmd = ENABLE,
75 EXTI_Init((EXTI_InitTypeDef *)&ExtiInit);
76 NVIC_Init((NVIC_InitTypeDef *)&cfg->irq.init);
77 RTC_ITConfig(RTC_IT_WUT, ENABLE);
79 RTC_ClearFlag(RTC_FLAG_WUTF);
82 uint32_t PIOS_RTC_Counter()
84 return RTC_GetWakeUpCounter();
87 /* FIXME: This shouldn't use hard-coded clock rates, dividers or prescalers.
88 * Should get these from the cfg struct passed to init.
90 float PIOS_RTC_Rate()
92 return (float)(8e6f / 128.0f) / (1 + PIOS_RTC_PRESCALER);
95 float PIOS_RTC_MsPerTick()
97 return 1000.0f / PIOS_RTC_Rate();
100 /* TODO: This needs a mutex around rtc_callbacks[] */
101 bool PIOS_RTC_RegisterTickCallback(void (*fn)(uint32_t id), uint32_t data)
103 struct rtc_callback_entry *cb;
105 if (rtc_callback_next >= PIOS_RTC_MAX_CALLBACKS) {
106 return false;
109 cb = &rtc_callback_list[rtc_callback_next++];
111 cb->fn = fn;
112 cb->data = data;
113 return true;
116 void PIOS_RTC_irq_handler(void)
118 if (RTC_GetITStatus(RTC_IT_WUT)) {
119 /* Call all registered callbacks */
120 for (uint8_t i = 0; i < rtc_callback_next; i++) {
121 struct rtc_callback_entry *cb = &rtc_callback_list[i];
122 if (cb->fn) {
123 (cb->fn)(cb->data);
127 /* Clear the RTC Second interrupt */
128 RTC_ClearITPendingBit(RTC_IT_WUT);
131 if (EXTI_GetITStatus(EXTI_Line20) != RESET) {
132 EXTI_ClearITPendingBit(EXTI_Line20);
136 #endif /* PIOS_INCLUDE_RTC */
139 * @}
140 * @}