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_BackupResetCmd(ENABLE);
52  RCC_BackupResetCmd(DISABLE);
53  RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
54  PWR_BackupAccessCmd(ENABLE);
55  // Divide external 8 MHz clock to 1 MHz
56  RCC_RTCCLKConfig(cfg->clksrc);
57  RCC_RTCCLKCmd(ENABLE);
58 
59  RTC_WakeUpCmd(DISABLE);
60  // Divide 1 Mhz clock by 16 -> 62.5 khz
61  RTC_WakeUpClockConfig(RTC_WakeUpClock_RTCCLK_Div16);
62  // Divide 62.5 khz by 200 to get 625 Hz
63  RTC_SetWakeUpCounter(cfg->prescaler); //cfg->prescaler);
64  RTC_WakeUpCmd(ENABLE);
65 
66  /* Configure and enable the RTC Second interrupt */
67  EXTI_InitTypeDef ExtiInit = {
68  .EXTI_Line = EXTI_Line22, // matches above GPIO pin
69  .EXTI_Mode = EXTI_Mode_Interrupt,
70  .EXTI_Trigger = EXTI_Trigger_Rising,
71  .EXTI_LineCmd = ENABLE,
72  };
73  EXTI_Init(&ExtiInit);
74  NVIC_Init((NVIC_InitTypeDef*)&cfg->irq.init);
75  RTC_ITConfig(RTC_IT_WUT, ENABLE);
76 
77  RTC_ClearFlag(RTC_FLAG_WUTF);
78 }
79 
80 uint32_t PIOS_RTC_Counter()
81 {
82  return RTC_GetWakeUpCounter();
83 }
84 
85 /* FIXME: This shouldn't use hard-coded clock rates, dividers or prescalers.
86  * Should get these from the cfg struct passed to init.
87  */
88 float PIOS_RTC_Rate()
89 {
90  return (float) (8e6 / 128) / (1 + PIOS_RTC_PRESCALER);
91 }
92 
93 float PIOS_RTC_MsPerTick()
94 {
95  return 1000.0f / PIOS_RTC_Rate();
96 }
97 
98 /* TODO: This needs a mutex around rtc_callbacks[] */
99 bool PIOS_RTC_RegisterTickCallback(void (*fn)(uintptr_t id), uintptr_t data)
100 {
101  struct rtc_callback_entry * cb;
102  if (rtc_callback_next >= PIOS_RTC_MAX_CALLBACKS) {
103  return false;
104  }
105 
106  cb = &rtc_callback_list[rtc_callback_next++];
107 
108  cb->fn = fn;
109  cb->data = data;
110  return true;
111 }
112 
113 void PIOS_RTC_irq_handler (void)
114 {
116 
117  if (RTC_GetITStatus(RTC_IT_WUT))
118  {
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);
124  }
125  }
126 
127  /* Clear the RTC Second interrupt */
128  RTC_ClearITPendingBit(RTC_IT_WUT);
129  }
130 
131  if (EXTI_GetITStatus(EXTI_Line22) != RESET)
132  EXTI_ClearITPendingBit(EXTI_Line22);
133 
135 }
136 #endif
137 
Main PiOS header to include all the compiled in PiOS options.
uint32_t PIOS_RTC_Counter()
#define PIOS_IRQ_Epilogue()
Definition: pios_irq.h:46
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)
#define PIOS_IRQ_Prologue()
Definition: pios_irq.h:45
uint32_t clksrc
Definition: pios_rtc_priv.h:37