dRonin  adbada4
dRonin firmware
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
pios_rtc.c
Go to the documentation of this file.
1 
15 /*
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.
20  *
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.
25  *
26  * You should have received a copy of the GNU General Public License along
27  * with this program; if not, see <http://www.gnu.org/licenses/>
28  */
29 
30 /* Project Includes */
31 #include "pios.h"
32 
33 #if defined(PIOS_INCLUDE_RTC)
34 #include <pios_rtc_priv.h>
35 
36 #ifndef PIOS_RTC_PRESCALER
37 #define PIOS_RTC_PRESCALER 100
38 #endif
39 
40 struct rtc_callback_entry {
41  void (*fn)(uintptr_t);
42  uintptr_t data;
43 };
44 
45 #define PIOS_RTC_MAX_CALLBACKS 3
47 static uint8_t rtc_callback_next = 0;
48 
49 void PIOS_RTC_Init(const struct pios_rtc_cfg * cfg)
50 {
51  RCC_APB1PeriphClockCmd(RCC_APB1Periph_BKP | RCC_APB1Periph_PWR,
52  ENABLE);
53  PWR_BackupAccessCmd(ENABLE);
54 
55  RCC_RTCCLKConfig(cfg->clksrc);
56  RCC_RTCCLKCmd(ENABLE);
57  RTC_WaitForLastTask();
58  RTC_WaitForSynchro();
59  RTC_WaitForLastTask();
60 
61  /* Configure and enable the RTC Second interrupt */
62  NVIC_Init((NVIC_InitTypeDef*)&cfg->irq.init);
63  RTC_ITConfig( RTC_IT_SEC, ENABLE );
64  RTC_WaitForLastTask();
65 
66  RTC_SetPrescaler(cfg->prescaler);
67  RTC_WaitForLastTask();
68  RTC_SetCounter(0);
69  RTC_WaitForLastTask();
70 }
71 
72 uint32_t PIOS_RTC_Counter()
73 {
74  return RTC_GetCounter();
75 }
76 
77 /* FIXME: This shouldn't use hard-coded clock rates, dividers or prescalers.
78  * Should get these from the cfg struct passed to init.
79  */
80 float PIOS_RTC_Rate()
81 {
82  return (float) (8e6 / 128) / (1 + PIOS_RTC_PRESCALER);
83 }
84 
85 float PIOS_RTC_MsPerTick()
86 {
87  return 1000.0f / PIOS_RTC_Rate();
88 }
89 
90 /* TODO: This needs a mutex around rtc_callbacks[] */
91 bool PIOS_RTC_RegisterTickCallback(void (*fn)(uintptr_t id), uintptr_t data)
92 {
93  struct rtc_callback_entry * cb;
94  if (rtc_callback_next >= PIOS_RTC_MAX_CALLBACKS) {
95  return false;
96  }
97 
98  cb = &rtc_callback_list[rtc_callback_next++];
99 
100  cb->fn = fn;
101  cb->data = data;
102  return true;
103 }
104 
105 void PIOS_RTC_irq_handler (void)
106 {
107  if (RTC_GetITStatus(RTC_IT_SEC))
108  {
109  /* Call all registered callbacks */
110  for (uint8_t i = 0; i < rtc_callback_next; i++) {
111  struct rtc_callback_entry * cb = &rtc_callback_list[i];
112  if (cb->fn) {
113  (cb->fn)(cb->data);
114  }
115  }
116 
117  /* Wait until last write operation on RTC registers has finished */
118  RTC_WaitForLastTask();
119  /* Clear the RTC Second interrupt */
120  RTC_ClearITPendingBit(RTC_IT_SEC);
121  }
122 }
123 #endif
124 
Main PiOS header to include all the compiled in PiOS options.
uint32_t PIOS_RTC_Counter()
Definition: pios_delay.c:41
bool PIOS_RTC_RegisterTickCallback(void(*fn)(uintptr_t id), uintptr_t data)
Definition: pios_delay.c:185
uint8_t data[XFER_BYTES_PER_PACKET]
Definition: bl_messages.h:129
static struct rtc_callback_entry rtc_callback_list[PIOS_RTC_MAX_CALLBACKS]
Definition: pios_delay.c:46
static struct flyingpicmd_cfg_fa cfg
Definition: main.c:49
float PIOS_RTC_MsPerTick()
Definition: pios_delay.c:180
float PIOS_RTC_Rate()
Definition: pios_delay.c:175
uintptr_t data
Definition: pios_delay.c:43
void(* fn)(uintptr_t)
Definition: pios_delay.c:42
uint8_t i
Definition: msp_messages.h:97
NVIC_InitTypeDef init
Definition: pios_stm32.h:36
uint32_t prescaler
Definition: pios_rtc_priv.h:38
void PIOS_RTC_Init(const struct pios_rtc_cfg *cfg)
#define PIOS_RTC_MAX_CALLBACKS
Definition: pios_delay.c:39
struct stm32_irq irq
Definition: pios_rtc_priv.h:39
void PIOS_RTC_irq_handler(void)
uint32_t clksrc
Definition: pios_rtc_priv.h:37