LP-602 significant change to USB layer. force complete USB stack reset on replug...
[librepilot.git] / flight / pios / stm32f4xx / pios_rtc.c
blobac0f28f0abb0b1e0890cafdcd6651f3dd10591a9
1 /**
2 ******************************************************************************
3 * @addtogroup PIOS PIOS Core hardware abstraction layer
4 * @{
5 * @addtogroup PIOS_PWM PWM Input Functions
6 * @brief Code to measure with PWM input
7 * @{
9 * @file pios_pwm.c
10 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2012.
11 * @brief PWM Input functions (STM32 dependent)
12 * @see The GNU Public License (GPL) Version 3
14 *****************************************************************************/
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 3 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful, but
22 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
23 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24 * for more details.
26 * You should have received a copy of the GNU General Public License along
27 * with this program; if not, write to the Free Software Foundation, Inc.,
28 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 #include "pios.h"
33 #ifdef PIOS_INCLUDE_RTC
35 #include <pios_rtc_priv.h>
37 #ifndef PIOS_RTC_PRESCALER
38 #define PIOS_RTC_PRESCALER 100
39 #endif
41 struct rtc_callback_entry {
42 void (*fn)(uint32_t);
43 uint32_t data;
46 #define PIOS_RTC_MAX_CALLBACKS 3
47 struct rtc_callback_entry rtc_callback_list[PIOS_RTC_MAX_CALLBACKS];
48 static uint8_t rtc_callback_next = 0;
50 void PIOS_RTC_Init(const struct pios_rtc_cfg *cfg)
52 RCC_BackupResetCmd(ENABLE);
53 RCC_BackupResetCmd(DISABLE);
54 RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
55 PWR_BackupAccessCmd(ENABLE);
56 // Divide external 8 MHz clock to 1 MHz
57 RCC_RTCCLKConfig(cfg->clksrc);
58 RCC_RTCCLKCmd(ENABLE);
60 RTC_WakeUpCmd(DISABLE);
61 // Divide 1 Mhz clock by 16 -> 62.5 khz
62 RTC_WakeUpClockConfig(RTC_WakeUpClock_RTCCLK_Div16);
63 // Divide 62.5 khz by 200 to get 625 Hz
64 RTC_SetWakeUpCounter(cfg->prescaler); // cfg->prescaler);
65 RTC_WakeUpCmd(ENABLE);
67 /* Configure and enable the RTC Second interrupt */
68 EXTI_InitTypeDef ExtiInit = {
69 .EXTI_Line = EXTI_Line22, // matches above GPIO pin
70 .EXTI_Mode = EXTI_Mode_Interrupt,
71 .EXTI_Trigger = EXTI_Trigger_Rising,
72 .EXTI_LineCmd = ENABLE,
74 EXTI_Init(&ExtiInit);
75 NVIC_Init(&cfg->irq.init);
76 RTC_ITConfig(RTC_IT_WUT, ENABLE);
78 RTC_ClearFlag(RTC_FLAG_WUTF);
81 uint32_t PIOS_RTC_Counter()
83 return RTC_GetWakeUpCounter();
86 /* FIXME: This shouldn't use hard-coded clock rates, dividers or prescalers.
87 * Should get these from the cfg struct passed to init.
89 float PIOS_RTC_Rate()
91 return (float)(8e6f / 128.0f) / (1 + PIOS_RTC_PRESCALER);
94 float PIOS_RTC_MsPerTick()
96 return 1000.0f / PIOS_RTC_Rate();
99 /* TODO: This needs a mutex around rtc_callbacks[] */
100 bool PIOS_RTC_RegisterTickCallback(void (*fn)(uint32_t id), uint32_t data)
102 struct rtc_callback_entry *cb;
104 if (rtc_callback_next >= PIOS_RTC_MAX_CALLBACKS) {
105 return false;
108 cb = &rtc_callback_list[rtc_callback_next++];
110 cb->fn = fn;
111 cb->data = data;
112 return true;
115 void PIOS_RTC_irq_handler(void)
117 if (RTC_GetITStatus(RTC_IT_WUT)) {
118 /* Call all registered callbacks */
119 for (uint8_t i = 0; i < rtc_callback_next; i++) {
120 struct rtc_callback_entry *cb = &rtc_callback_list[i];
121 if (cb->fn) {
122 (cb->fn)(cb->data);
126 /* Clear the RTC Second interrupt */
127 RTC_ClearITPendingBit(RTC_IT_WUT);
130 if (EXTI_GetITStatus(EXTI_Line22) != RESET) {
131 EXTI_ClearITPendingBit(EXTI_Line22);
135 #endif /* PIOS_INCLUDE_RTC */
138 * @}
139 * @}