Paparazzi UAS  v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
sys_time_arch.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2013 AggieAir, A Remote Sensing Unmanned Aerial System for Scientific Applications
3  * Utah State University, http://aggieair.usu.edu/
4  *
5  * Michal Podhradsky (michal.podhradsky@aggiemail.usu.edu)
6  * Calvin Coopmans (c.r.coopmans@ieee.org)
7  *
8  * Copyright (C) 2015 Gautier Hattenberger <gautier.hattenberger@enac.fr>
9  *
10  * This file is part of paparazzi.
11  *
12  * paparazzi is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2, or (at your option)
15  * any later version.
16  *
17  * paparazzi is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with paparazzi; see the file COPYING. If not, write to
24  * the Free Software Foundation, 59 Temple Place - Suite 330,
25  * Boston, MA 02111-1307, USA.
26  */
27 
35 #include "mcu_periph/sys_time_arch.h"
36 #include BOARD_CONFIG
37 #include <ch.h>
38 #include "led.h"
39 
40 static MUTEX_DECL(sys_time_mtx);
41 
42 /*
43  * Sys_tick handler thread
44  */
45 static void thd_sys_tick(void *arg);
46 static THD_WORKING_AREA(wa_thd_sys_tick, 1024);
47 static void sys_tick_handler(void);
48 
49 static uint32_t cpu_ticks = 0;
50 static uint32_t cpu_counter = 0;
51 
53 {
54 
56 
57  /* cpu ticks per desired sys_time timer step */
59 
60  // Create thread (PRIO should be higher than AP threads
61  chThdCreateStatic(wa_thd_sys_tick, sizeof(wa_thd_sys_tick),
62  NORMALPRIO + 2, thd_sys_tick, NULL);
63 
64 }
65 
72 {
73  chMtxLock(&sys_time_mtx);
74  uint32_t current = chSysGetRealtimeCounterX();
75  uint32_t t = sys_time.nb_sec * 1000000 +
76  TIME_I2US(sys_time.nb_sec_rem) +
77  RTC2US(STM32_SYSCLK, current - cpu_counter);
78  chMtxUnlock(&sys_time_mtx);
79  return t;
80 }
81 
88 {
89  chMtxLock(&sys_time_mtx);
90  uint32_t current = chSysGetRealtimeCounterX();
91  uint32_t t = sys_time.nb_sec * 10000 +
92  TIME_I2US(sys_time.nb_sec_rem)/100 +
93  RTC2US(STM32_SYSCLK, current - cpu_counter)/100;
94  chMtxUnlock(&sys_time_mtx);
95  return t;
96 }
97 
99 {
100  chMtxLock(&sys_time_mtx);
101  uint32_t current = chSysGetRealtimeCounterX();
102  uint32_t t = sys_time.nb_sec * 1000 +
103  TIME_I2MS(sys_time.nb_sec_rem) +
104  RTC2MS(STM32_SYSCLK, current - cpu_counter);
105  chMtxUnlock(&sys_time_mtx);
106  return t;
107 }
108 
117 {
118  if (us < 1000) {
119  // for small time, use the polled version instead of thread sleep
120  chSysDisable();
121  chSysPolledDelayX(US2RTC(STM32_HCLK, us));
122  chSysEnable();
123  } else {
124  chThdSleepMicroseconds(us);
125  }
126 }
127 
129 {
130  chThdSleepMilliseconds(ms);
131 }
132 
134 {
135  chThdSleepSeconds(s);
136 }
137 
138 
139 /*
140  * Sys_tick thread
141  */
142 static __attribute__((noreturn)) void thd_sys_tick(void *arg)
143 {
144  (void) arg;
145  chRegSetThreadName("sys_tick_handler");
146 
147  while (TRUE) {
148  systime_t t = chVTGetSystemTime();
150  chThdSleepUntilWindowed(t, t + TIME_US2I(USEC_OF_SEC(1.f / (SYS_TIME_FREQUENCY))));
151  }
152 }
153 
154 static void sys_tick_handler(void)
155 {
156  chMtxLock(&sys_time_mtx);
157  /* current time in sys_ticks */
158  sys_time.nb_tick++;
159  /* max time is 2^32 / CH_CFG_ST_FREQUENCY, i.e. around 10 days at 10kHz */
160  cpu_ticks = chVTGetSystemTime();
161  /* store high resolution counter for accurate timing */
162  cpu_counter = chSysGetRealtimeCounterX();
163  /* increment secondes and remainder */
165 #ifdef SYS_TIME_LED
166  if (sec > sys_time.nb_sec) {
167  LED_TOGGLE(SYS_TIME_LED);
168  }
169 #endif
170  sys_time.nb_sec = sec;
172 
173  /* advance virtual timers */
174  for (unsigned int i = 0; i < SYS_TIME_NB_TIMER; i++) {
175  if (sys_time.timer[i].in_use &&
178  sys_time.timer[i].elapsed = TRUE;
179  /* call registered callbacks, WARNING: they will be executed in the sys_time thread! */
180  if (sys_time.timer[i].cb) {
181  sys_time.timer[i].cb(i);
182  }
183  }
184  }
185  chMtxUnlock(&sys_time_mtx);
186 }
187 
#define LED_TOGGLE(i)
Definition: led_hw.h:53
void sys_time_msleep(uint16_t ms)
static uint32_t cpu_ticks
Definition: sys_time_arch.c:49
static void sys_tick_handler(void)
static void thd_sys_tick(void *arg)
void sys_time_usleep(uint32_t us)
sys_time_usleep(uint32_t us)
static uint32_t cpu_counter
Definition: sys_time_arch.c:50
uint32_t get_sys_time_usec100(void)
Get the time in microseconds divided by 100 since startup.
Definition: sys_time_arch.c:87
uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
Definition: sys_time_arch.c:71
static THD_WORKING_AREA(wa_thd_sys_tick, 1024)
void sys_time_arch_init(void)
Definition: sys_time_arch.c:52
uint32_t get_sys_time_msec(void)
Get the time in milliseconds since startup.
Definition: sys_time_arch.c:98
void sys_time_ssleep(uint8_t s)
static MUTEX_DECL(sys_time_mtx)
#define CH_CFG_ST_FREQUENCY
System tick frequency.
Definition: chconf.h:75
arch independent LED (Light Emitting Diodes) API
static uint32_t s
#define TRUE
Definition: std.h:4
volatile uint32_t nb_tick
SYS_TIME_TICKS since startup.
Definition: sys_time.h:74
#define USEC_OF_SEC(sec)
Definition: sys_time.h:219
#define SYS_TIME_FREQUENCY
(Default) sys_time timer frequency in Hz.
Definition: sys_time.h:56
volatile uint32_t nb_sec
full seconds since startup
Definition: sys_time.h:72
uint32_t cpu_ticks_per_sec
cpu ticks per second
Definition: sys_time.h:80
uint32_t duration
in SYS_TIME_TICKS
Definition: sys_time.h:68
uint32_t resolution_cpu_ticks
sys_time_timer resolution in cpu ticks
Definition: sys_time.h:79
volatile uint32_t nb_sec_rem
remainder of seconds since startup in CPU_TICKS
Definition: sys_time.h:73
float resolution
sys_time_timer resolution in seconds
Definition: sys_time.h:77
sys_time_cb cb
Definition: sys_time.h:65
#define SYS_TIME_NB_TIMER
Definition: sys_time.h:44
uint32_t end_time
in SYS_TIME_TICKS
Definition: sys_time.h:67
static uint32_t cpu_ticks_of_sec(float seconds)
Definition: sys_time.h:182
struct sys_time_timer timer[SYS_TIME_NB_TIMER]
Definition: sys_time.h:75
volatile bool elapsed
Definition: sys_time.h:66
unsigned short uint16_t
Typedef defining 16 bit unsigned short type.
Definition: vl53l1_types.h:88
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
Definition: vl53l1_types.h:78
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.
Definition: vl53l1_types.h:98