Paparazzi UAS  v6.2_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 #ifndef STM32_SYSCLK
43 #define STM32_SYSCLK STM32_SYS_CK
44 #endif
45 
46 /*
47  * Sys_tick handler thread
48  */
49 static void thd_sys_tick(void *arg);
50 static THD_WORKING_AREA(wa_thd_sys_tick, 1024);
51 static void sys_tick_handler(void);
52 
53 static uint32_t cpu_ticks = 0;
54 static uint32_t cpu_counter = 0;
55 
57 {
58 
60 
61  /* cpu ticks per desired sys_time timer step */
63 
64  // Create thread (PRIO should be higher than AP threads
65  chThdCreateStatic(wa_thd_sys_tick, sizeof(wa_thd_sys_tick),
66  NORMALPRIO + 2, thd_sys_tick, NULL);
67 
68 }
69 
76 {
77  chMtxLock(&sys_time_mtx);
78  uint32_t current = chSysGetRealtimeCounterX();
79  uint32_t t = sys_time.nb_sec * 1000000 +
80  TIME_I2US(sys_time.nb_sec_rem) +
81  RTC2US(STM32_SYSCLK, current - cpu_counter);
82  chMtxUnlock(&sys_time_mtx);
83  return t;
84 }
85 
87 {
88  chMtxLock(&sys_time_mtx);
89  uint32_t current = chSysGetRealtimeCounterX();
90  uint32_t t = sys_time.nb_sec * 1000 +
91  TIME_I2MS(sys_time.nb_sec_rem) +
92  RTC2MS(STM32_SYSCLK, current - cpu_counter);
93  chMtxUnlock(&sys_time_mtx);
94  return t;
95 }
96 
105 {
106  if (us < 1000) {
107  // for small time, use the polled version instead of thread sleep
108  chSysDisable();
109  chSysPolledDelayX(US2RTC(STM32_HCLK, us));
110  chSysEnable();
111  } else {
112  chThdSleepMicroseconds(us);
113  }
114 }
115 
117 {
118  chThdSleepMilliseconds(ms);
119 }
120 
122 {
123  chThdSleepSeconds(s);
124 }
125 
126 
127 /*
128  * Sys_tick thread
129  */
130 static __attribute__((noreturn)) void thd_sys_tick(void *arg)
131 {
132  (void) arg;
133  chRegSetThreadName("sys_tick_handler");
134 
135  while (TRUE) {
136  systime_t t = chVTGetSystemTime();
138  chThdSleepUntilWindowed(t, t + TIME_US2I(USEC_OF_SEC(1.f / (SYS_TIME_FREQUENCY))));
139  }
140 }
141 
142 static void sys_tick_handler(void)
143 {
144  chMtxLock(&sys_time_mtx);
145  /* current time in sys_ticks */
146  sys_time.nb_tick++;
147  /* max time is 2^32 / CH_CFG_ST_FREQUENCY, i.e. around 10 days at 10kHz */
148  cpu_ticks = chVTGetSystemTime();
149  /* store high resolution counter for accurate timing */
150  cpu_counter = chSysGetRealtimeCounterX();
151  /* increment secondes and remainder */
153 #ifdef SYS_TIME_LED
154  if (sec > sys_time.nb_sec) {
155  LED_TOGGLE(SYS_TIME_LED);
156  }
157 #endif
158  sys_time.nb_sec = sec;
160 
161  /* advance virtual timers */
162  for (unsigned int i = 0; i < SYS_TIME_NB_TIMER; i++) {
163  if (sys_time.timer[i].in_use &&
166  sys_time.timer[i].elapsed = TRUE;
167  /* call registered callbacks, WARNING: they will be executed in the sys_time thread! */
168  if (sys_time.timer[i].cb) {
169  sys_time.timer[i].cb(i);
170  }
171  }
172  }
173  chMtxUnlock(&sys_time_mtx);
174 }
175 
sys_time::cpu_ticks_per_sec
uint32_t cpu_ticks_per_sec
cpu ticks per second
Definition: sys_time.h:80
sys_time::timer
struct sys_time_timer timer[SYS_TIME_NB_TIMER]
Definition: sys_time.h:75
uint32_t
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
Definition: vl53l1_types.h:78
uint8_t
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.
Definition: vl53l1_types.h:98
sys_time_ssleep
void sys_time_ssleep(uint8_t s)
Definition: sys_time_arch.c:121
MUTEX_DECL
static MUTEX_DECL(sys_time_mtx)
cpu_ticks_of_sec
static uint32_t cpu_ticks_of_sec(float seconds)
Definition: sys_time.h:182
thd_sys_tick
static void thd_sys_tick(void *arg)
Definition: sys_time_arch.c:130
sys_time_timer::elapsed
volatile bool elapsed
Definition: sys_time.h:66
sys_time::resolution_cpu_ticks
uint32_t resolution_cpu_ticks
sys_time_timer resolution in cpu ticks
Definition: sys_time.h:79
sys_time_timer::duration
uint32_t duration
in SYS_TIME_TICKS
Definition: sys_time.h:68
sys_time_usleep
void sys_time_usleep(uint32_t us)
sys_time_usleep(uint32_t us)
Definition: sys_time_arch.c:104
THD_WORKING_AREA
static THD_WORKING_AREA(wa_thd_sys_tick, 1024)
s
static uint32_t s
Definition: light_scheduler.c:33
CH_CFG_ST_FREQUENCY
#define CH_CFG_ST_FREQUENCY
System tick frequency.
Definition: chconf.h:75
LED_TOGGLE
#define LED_TOGGLE(i)
Definition: led_hw.h:53
sys_time_msleep
void sys_time_msleep(uint16_t ms)
Definition: sys_time_arch.c:116
cpu_counter
static uint32_t cpu_counter
Definition: sys_time_arch.c:54
get_sys_time_usec
uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
Definition: sys_time_arch.c:75
USEC_OF_SEC
#define USEC_OF_SEC(sec)
Definition: sys_time.h:219
sys_time::nb_tick
volatile uint32_t nb_tick
SYS_TIME_TICKS since startup.
Definition: sys_time.h:74
sys_time_timer::in_use
bool in_use
Definition: sys_time.h:64
get_sys_time_msec
uint32_t get_sys_time_msec(void)
Get the time in milliseconds since startup.
Definition: sys_time_arch.c:86
cpu_ticks
static uint32_t cpu_ticks
Definition: sys_time_arch.c:53
led.h
arch independent LED (Light Emitting Diodes) API
f
uint16_t f
Camera baseline, in meters (i.e. horizontal distance between the two cameras of the stereo setup)
Definition: wedgebug.c:204
sys_time_timer::cb
sys_time_cb cb
Definition: sys_time.h:65
sys_time_arch_init
void sys_time_arch_init(void)
Definition: sys_time_arch.c:56
sys_time
Definition: sys_time.h:71
SYS_TIME_NB_TIMER
#define SYS_TIME_NB_TIMER
Definition: sys_time.h:44
sys_time::nb_sec_rem
volatile uint32_t nb_sec_rem
remainder of seconds since startup in CPU_TICKS
Definition: sys_time.h:73
sys_time_timer::end_time
uint32_t end_time
in SYS_TIME_TICKS
Definition: sys_time.h:67
STM32_SYSCLK
#define STM32_SYSCLK
Definition: sys_time_arch.c:43
SYS_TIME_FREQUENCY
#define SYS_TIME_FREQUENCY
(Default) sys_time timer frequency in Hz.
Definition: sys_time.h:56
sys_time::nb_sec
volatile uint32_t nb_sec
full seconds since startup
Definition: sys_time.h:72
TRUE
#define TRUE
Definition: std.h:4
uint16_t
unsigned short uint16_t
Typedef defining 16 bit unsigned short type.
Definition: vl53l1_types.h:88
sys_tick_handler
static void sys_tick_handler(void)
Definition: sys_time_arch.c:142
sys_time::resolution
float resolution
sys_time_timer resolution in seconds
Definition: sys_time.h:77