Paparazzi UAS  v6.0_unstable-135-g1952e6d-dirty
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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 
83 {
84  chMtxLock(&sys_time_mtx);
85  uint32_t current = chSysGetRealtimeCounterX();
86  uint32_t t = sys_time.nb_sec * 1000 +
87  TIME_I2MS(sys_time.nb_sec_rem) +
88  RTC2MS(STM32_SYSCLK, current - cpu_counter);
89  chMtxUnlock(&sys_time_mtx);
90  return t;
91 }
92 
101 {
102  if (us < 1000) {
103  // for small time, use the polled version instead of thread sleep
104  chSysDisable();
105  chSysPolledDelayX(US2RTC(STM32_HCLK, us));
106  chSysEnable();
107  } else {
108  chThdSleepMicroseconds(us);
109  }
110 }
111 
113 {
114  chThdSleepMilliseconds(ms);
115 }
116 
118 {
119  chThdSleepSeconds(s);
120 }
121 
122 
123 /*
124  * Sys_tick thread
125  */
126 static __attribute__((noreturn)) void thd_sys_tick(void *arg)
127 {
128  (void) arg;
129  chRegSetThreadName("sys_tick_handler");
130 
131  while (TRUE) {
132  systime_t t = chVTGetSystemTime();
134  chThdSleepUntilWindowed(t, t + TIME_US2I(USEC_OF_SEC(1.f/(SYS_TIME_FREQUENCY))));
135  }
136 }
137 
138 static void sys_tick_handler(void)
139 {
140  chMtxLock(&sys_time_mtx);
141  /* current time in sys_ticks */
142  sys_time.nb_tick++;
143  /* max time is 2^32 / CH_CFG_ST_FREQUENCY, i.e. around 10 days at 10kHz */
144  cpu_ticks = chVTGetSystemTime();
145  /* store high resolution counter for accurate timing */
146  cpu_counter = chSysGetRealtimeCounterX();
147  /* increment secondes and remainder */
149 #ifdef SYS_TIME_LED
150  if (sec > sys_time.nb_sec) {
151  LED_TOGGLE(SYS_TIME_LED);
152  }
153 #endif
154  sys_time.nb_sec = sec;
156 
157  /* advance virtual timers */
158  for (unsigned int i = 0; i < SYS_TIME_NB_TIMER; i++) {
159  if (sys_time.timer[i].in_use &&
162  sys_time.timer[i].elapsed = TRUE;
163  /* call registered callbacks, WARNING: they will be executed in the sys_time thread! */
164  if (sys_time.timer[i].cb) {
165  sys_time.timer[i].cb(i);
166  }
167  }
168  }
169  chMtxUnlock(&sys_time_mtx);
170 }
171 
static uint32_t s
float resolution
sys_time_timer resolution in seconds
Definition: sys_time.h:73
uint32_t get_sys_time_msec(void)
Get the time in milliseconds since startup.
Definition: sys_time_arch.c:82
uint32_t cpu_ticks_per_sec
cpu ticks per second
Definition: sys_time.h:76
void sys_time_ssleep(uint8_t s)
volatile uint32_t nb_tick
SYS_TIME_TICKS since startup.
Definition: sys_time.h:70
static void thd_sys_tick(void *arg)
void sys_time_usleep(uint32_t us)
sys_time_usleep(uint32_t us)
#define TRUE
Definition: std.h:4
static THD_WORKING_AREA(wa_thd_sys_tick, 1024)
static void sys_tick_handler(void)
volatile bool elapsed
Definition: sys_time.h:62
uint32_t end_time
in SYS_TIME_TICKS
Definition: sys_time.h:63
#define SYS_TIME_FREQUENCY
(Default) sys_time timer frequency in Hz.
Definition: sys_time.h:52
uint16_t f
Camera baseline, in meters (i.e. horizontal distance between the two cameras of the stereo setup) ...
Definition: wedgebug.c:204
static uint32_t cpu_ticks
Definition: sys_time_arch.c:49
volatile uint32_t nb_sec_rem
remainder of seconds since startup in CPU_TICKS
Definition: sys_time.h:69
#define LED_TOGGLE(i)
Definition: led_hw.h:53
#define USEC_OF_SEC(sec)
Definition: sys_time.h:215
struct sys_time_timer timer[SYS_TIME_NB_TIMER]
Definition: sys_time.h:71
void sys_time_msleep(uint16_t ms)
volatile uint32_t nb_sec
full seconds since startup
Definition: sys_time.h:68
static MUTEX_DECL(sys_time_mtx)
static uint32_t cpu_counter
Definition: sys_time_arch.c:50
sys_time_cb cb
Definition: sys_time.h:61
arch independent LED (Light Emitting Diodes) API
#define SYS_TIME_NB_TIMER
Definition: sys_time.h:40
#define CH_CFG_ST_FREQUENCY
System tick frequency.
Definition: chconf.h:55
uint32_t duration
in SYS_TIME_TICKS
Definition: sys_time.h:64
uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
Definition: sys_time_arch.c:71
static uint32_t cpu_ticks_of_sec(float seconds)
Definition: sys_time.h:178
void sys_time_arch_init(void)
Definition: sys_time_arch.c:52
unsigned short uint16_t
Typedef defining 16 bit unsigned short type.
Definition: vl53l1_types.h:88
uint32_t resolution_cpu_ticks
sys_time_timer resolution in cpu ticks
Definition: sys_time.h:75
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.
Definition: vl53l1_types.h:98
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
Definition: vl53l1_types.h:78