Paparazzi UAS  v5.18.0_stable
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 
50 {
51 
53 
54  /* cpu ticks per desired sys_time timer step */
56 
57  // Create thread (PRIO should be higher than AP threads
58  chThdCreateStatic(wa_thd_sys_tick, sizeof(wa_thd_sys_tick),
59  NORMALPRIO+2, thd_sys_tick, NULL);
60 
61 }
62 
69 {
70  chMtxLock(&sys_time_mtx);
71  uint32_t t = sys_time.nb_sec * 1000000 +
73  usec_of_sys_time_ticks(chVTGetSystemTime() - sys_time.nb_tick);
74  chMtxUnlock(&sys_time_mtx);
75  return t;
76 }
77 
79 {
80  chMtxLock(&sys_time_mtx);
81  uint32_t t = sys_time.nb_sec * 1000 +
83  msec_of_sys_time_ticks(chVTGetSystemTime() - sys_time.nb_tick);
84  chMtxUnlock(&sys_time_mtx);
85  return t;
86 }
87 
96 {
97  if (us < 1000) {
98  // for small time, use the polled version instead of thread sleep
99  chSysDisable();
100  chSysPolledDelayX(US2RTC(STM32_HCLK, us));
101  chSysEnable();
102  } else {
103  uint64_t wait_st = ((uint64_t)us * CH_CFG_ST_FREQUENCY) / 1000000UL;
104  chThdSleep((systime_t)wait_st);
105  }
106 }
107 
109 {
110  uint64_t wait_st = ((uint64_t)ms * CH_CFG_ST_FREQUENCY) / 1000UL;
111  chThdSleep((systime_t)wait_st);
112 }
113 
115 {
116  chThdSleep(TIME_S2I(s));
117 }
118 
119 /*
120  * Sys_tick thread
121  */
122 static __attribute__((noreturn)) void thd_sys_tick(void *arg)
123 {
124  (void) arg;
125  chRegSetThreadName("sys_tick_handler");
126 
127  while (TRUE) {
129  chThdSleepMilliseconds(1);
130  }
131 }
132 
133 static void sys_tick_handler(void)
134 {
135  chMtxLock(&sys_time_mtx);
136  /* current time in sys_ticks */
137  sys_time.nb_tick = chVTGetSystemTime();
138  /* max time is 2^32 / CH_CFG_ST_FREQUENCY, i.e. around 10 days at 10kHz */
140 #ifdef SYS_TIME_LED
141  if (sec > sys_time.nb_sec) {
142  LED_TOGGLE(SYS_TIME_LED);
143  }
144 #endif
145  sys_time.nb_sec = sec;
147 
148  /* advance virtual timers */
149  for (unsigned int i = 0; i < SYS_TIME_NB_TIMER; i++) {
150  if (sys_time.timer[i].in_use &&
153  sys_time.timer[i].elapsed = TRUE;
154  /* call registered callbacks, WARNING: they will be executed in the sys_time thread! */
155  if (sys_time.timer[i].cb) {
156  sys_time.timer[i].cb(i);
157  }
158  }
159  }
160  chMtxUnlock(&sys_time_mtx);
161 }
162 
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
uint16_t
unsigned short uint16_t
Definition: types.h:16
sys_time_ssleep
void sys_time_ssleep(uint8_t s)
Definition: sys_time_arch.c:114
MUTEX_DECL
static MUTEX_DECL(sys_time_mtx)
thd_sys_tick
static void thd_sys_tick(void *arg)
Definition: sys_time_arch.c:122
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:95
THD_WORKING_AREA
static THD_WORKING_AREA(wa_thd_sys_tick, 1024)
s
static uint32_t s
Definition: light_scheduler.c:33
uint32_t
unsigned long uint32_t
Definition: types.h:18
CH_CFG_ST_FREQUENCY
#define CH_CFG_ST_FREQUENCY
System tick frequency.
Definition: chconf.h:55
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:108
get_sys_time_usec
uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
Definition: sys_time_arch.c:68
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:78
uint8_t
unsigned char uint8_t
Definition: types.h:14
led.h
arch independent LED (Light Emitting Diodes) API
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:49
sys_time
Definition: sys_time.h:71
SYS_TIME_NB_TIMER
#define SYS_TIME_NB_TIMER
Definition: sys_time.h:40
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
usec_of_sys_time_ticks
static uint32_t usec_of_sys_time_ticks(uint32_t ticks)
Definition: sys_time.h:163
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
sys_tick_handler
static void sys_tick_handler(void)
Definition: sys_time_arch.c:133
sys_time_ticks_of_sec
static uint32_t sys_time_ticks_of_sec(float seconds)
Definition: sys_time.h:138
msec_of_sys_time_ticks
static uint32_t msec_of_sys_time_ticks(uint32_t ticks)
Definition: sys_time.h:158
sys_time::resolution
float resolution
sys_time_timer resolution in seconds
Definition: sys_time.h:77
uint64_t
unsigned long long uint64_t
Definition: types.h:20