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 
35 #include <pios_thread.h>
36 
37 struct rtc_callback_entry {
38  void (*fn)(uintptr_t);
39  uintptr_t data;
40 };
41 
42 #define PIOS_RTC_MAX_CALLBACKS 10
44 static volatile uint32_t rtc_counter;
45 static int rtc_callback_next;
46 
47 uint32_t PIOS_RTC_Counter()
48 {
49  return rtc_counter;
50 }
51 
52 float PIOS_RTC_Rate()
53 {
54  return 500;
55 }
56 
57 float PIOS_RTC_MsPerTick()
58 {
59  return 1000.0f / PIOS_RTC_Rate();
60 }
61 
62 bool PIOS_RTC_RegisterTickCallback(void (*fn)(uintptr_t id), uintptr_t data)
63 {
64  struct rtc_callback_entry * cb;
65  if (rtc_callback_next >= PIOS_RTC_MAX_CALLBACKS) {
66  return false;
67  }
68 
69  cb = &rtc_callback_list[rtc_callback_next++];
70 
71  cb->fn = fn;
72  cb->data = data;
73 
74  return true;
75 }
76 
77 static void rtc_do(void)
78 {
79  rtc_counter++;
80 
81  /* Call all registered callbacks */
82  for (uint8_t i = 0; i < rtc_callback_next; i++) {
83  struct rtc_callback_entry * cb = &rtc_callback_list[i];
84  if (cb->fn) {
85  (cb->fn)(cb->data);
86  }
87  }
88 }
89 
90 static void PIOS_RTC_Task(void *unused)
91 {
92  (void) unused;
93 
94  uint32_t now = PIOS_Thread_Systime();
95 
96  while (true) {
97  PIOS_Thread_Sleep_Until(&now, 1000 / PIOS_RTC_Rate());
98 
99  rtc_do();
100  }
101 }
102 
103 void PIOS_RTC_Init()
104 {
105  struct pios_thread *rtc_task = PIOS_Thread_Create(PIOS_RTC_Task,
106  "pios_rtc_task",
109 
110  PIOS_Assert(rtc_task);
111 }
112 
113 #endif /* PIOS_INCLUDE_RTC */
114 
115 
uint32_t PIOS_Thread_Systime(void)
Definition: pios_thread.c:212
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
float PIOS_RTC_MsPerTick()
Definition: pios_delay.c:180
float PIOS_RTC_Rate()
Definition: pios_delay.c:175
#define PIOS_THREAD_STACK_SIZE_MIN
Definition: pios_thread.h:55
uintptr_t data
Definition: pios_delay.c:43
void(* fn)(uintptr_t)
Definition: pios_delay.c:42
struct pios_thread * PIOS_Thread_Create(void(*fp)(void *), const char *namep, size_t stack_bytes, void *argp, enum pios_thread_prio_e prio)
Definition: pios_thread.c:89
uint8_t i
Definition: msp_messages.h:97
void PIOS_RTC_Init(const struct pios_rtc_cfg *cfg)
void PIOS_Thread_Sleep_Until(uint32_t *previous_ms, uint32_t increment_ms)
Definition: pios_thread.c:255
#define PIOS_RTC_MAX_CALLBACKS
Definition: pios_delay.c:39
uint8_t unused[4]
Definition: bl_messages.h:70
#define PIOS_Assert(test)
Definition: pios_debug.h:52