update credits
[librepilot.git] / flight / pios / stm32f10x / pios_rtc.c
blobdbf89be6a1cf3a9b398cc59aa334358983112927
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) 2010.
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_APB1PeriphClockCmd(RCC_APB1Periph_BKP | RCC_APB1Periph_PWR,
53 ENABLE);
54 PWR_BackupAccessCmd(ENABLE);
56 RCC_RTCCLKConfig(cfg->clksrc);
57 RCC_RTCCLKCmd(ENABLE);
58 RTC_WaitForLastTask();
59 RTC_WaitForSynchro();
60 RTC_WaitForLastTask();
62 /* Configure and enable the RTC Second interrupt */
63 NVIC_Init(&cfg->irq.init);
64 RTC_ITConfig(RTC_IT_SEC, ENABLE);
65 RTC_WaitForLastTask();
67 RTC_SetPrescaler(cfg->prescaler);
68 RTC_WaitForLastTask();
69 RTC_SetCounter(0);
70 RTC_WaitForLastTask();
73 uint32_t PIOS_RTC_Counter()
75 return RTC_GetCounter();
78 /* FIXME: This shouldn't use hard-coded clock rates, dividers or prescalers.
79 * Should get these from the cfg struct passed to init.
81 float PIOS_RTC_Rate()
83 return (float)(8e6f / 128.0f) / (1 + PIOS_RTC_PRESCALER);
86 float PIOS_RTC_MsPerTick()
88 return 1000.0f / PIOS_RTC_Rate();
91 /* TODO: This needs a mutex around rtc_callbacks[] */
92 bool PIOS_RTC_RegisterTickCallback(void (*fn)(uint32_t id), uint32_t data)
94 struct rtc_callback_entry *cb;
96 if (rtc_callback_next >= PIOS_RTC_MAX_CALLBACKS) {
97 return false;
100 cb = &rtc_callback_list[rtc_callback_next++];
102 cb->fn = fn;
103 cb->data = data;
104 return true;
107 void PIOS_RTC_irq_handler(void)
109 if (RTC_GetITStatus(RTC_IT_SEC)) {
110 /* Call all registered callbacks */
111 for (uint8_t i = 0; i < rtc_callback_next; i++) {
112 struct rtc_callback_entry *cb = &rtc_callback_list[i];
113 if (cb->fn) {
114 (cb->fn)(cb->data);
118 /* Wait until last write operation on RTC registers has finished */
119 RTC_WaitForLastTask();
120 /* Clear the RTC Second interrupt */
121 RTC_ClearITPendingBit(RTC_IT_SEC);
125 #endif /* PIOS_INCLUDE_RTC */
128 * @}
129 * @}