Paparazzi UAS  v6.1.0_stable
Paparazzi is a free software Unmanned Aircraft System.
main_chibios.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2013-2015 Gautier Hattenberger, Alexandre Bustico
3  *
4  * This file is part of paparazzi.
5  *
6  * paparazzi is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * paparazzi is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with paparazzi; see the file COPYING. If not, write to
18  * the Free Software Foundation, 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21 
26 #include "mcu_periph/sys_time.h"
27 #include "mcu.h"
28 #include <ch.h>
29 
30 #ifndef SYS_TIME_FREQUENCY
31 #error SYS_TIME_FREQUENCY should be defined in Makefile.chibios or airframe.xml and be equal to CH_CFG_ST_FREQUENCY
32 #elif CH_CFG_ST_FREQUENCY < (2 * PERIODIC_FREQUENCY) && SYS_TIME_FREQUENCY < (2 * PERIODIC_FREQUENCY)
33 #error CH_CFG_ST_FREQUENCY and SYS_TIME_FREQUENCY should be >= 2 x PERIODIC_FREQUENCY
34 #endif
35 
37 
38 #ifndef THD_WORKING_AREA_MAIN
39 #define THD_WORKING_AREA_MAIN 8192
40 #endif
41 
42 /*
43  * PPRZ/AP thread
44  * Also include FBW on single MCU
45  */
46 static void thd_ap(void *arg);
48 static thread_t *apThdPtr = NULL;
49 
53 int main(void)
54 {
55  // Init
56  main_init();
57 
58  chThdSleepMilliseconds(100);
59 
60  // Create threads
61  apThdPtr = chThdCreateStatic(wa_thd_ap, sizeof(wa_thd_ap), NORMALPRIO, thd_ap, NULL);
62 
63  // Main loop, do nothing
64  chThdSleep(TIME_INFINITE);
65  return 0;
66 }
67 
68 /*
69  * PPRZ/AP thread
70  *
71  * Call PPRZ AP periodic and event functions
72  */
73 static void thd_ap(void *arg)
74 {
75  (void) arg;
76  chRegSetThreadName("AP");
77 
78  while (!chThdShouldTerminateX()) {
79  systime_t t = chVTGetSystemTime();
81  main_event();
82  // The sleep time is computed to have a polling interval of
83  // 1e6 / CH_CFG_ST_FREQUENCY. If time is passed, thanks to the
84  // "Windowed" sleep function, the execution is not blocked until
85  // a complet roll-over.
86  chThdSleepUntilWindowed(t, t + TIME_US2I(1000000 / CH_CFG_ST_FREQUENCY));
87  }
88 
89  chThdExit(0);
90 }
91 
92 /*
93  * Terminate autopilot threads
94  * Wait until proper stop
95  */
97 {
98  if (apThdPtr != NULL) {
99  chThdTerminate(apThdPtr);
100  chThdWait(apThdPtr);
101  apThdPtr = NULL;
102  }
103 }
104 
THD_WORKING_AREA_MAIN
#define THD_WORKING_AREA_MAIN
Definition: main_chibios.c:39
main_event
static void main_event(void)
Definition: uart_tunnel.c:85
main
int main(void)
Main function.
Definition: main_chibios.c:85
CH_CFG_ST_FREQUENCY
#define CH_CFG_ST_FREQUENCY
System tick frequency.
Definition: chconf.h:55
pprz_terminate_autopilot_threads
void pprz_terminate_autopilot_threads(void)
Terminate all autopilot threads Wait until proper stop.
Definition: main_chibios.c:172
THD_WORKING_AREA
THD_WORKING_AREA(wa_thd_ap, THD_WORKING_AREA_MAIN)
thd_ap
static void thd_ap(void *arg)
Definition: main_chibios.c:73
mcu.h
Arch independent mcu ( Micro Controller Unit ) utilities.
sys_time.h
Architecture independent timing functions.
main_init
static void main_init(void)
Definition: demo_ahrs_actuators.c:87
apThdPtr
static thread_t * apThdPtr
Definition: main_chibios.c:48
main_ap.h
handle_periodic_tasks
STATIC_INLINE void handle_periodic_tasks(void)
Definition: main_ap.c:128