Paparazzi UAS  v5.10_stable-5-g83a0da5-dirty
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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 SYS_TIME_FREQUENCY != CH_CFG_ST_FREQUENCY
33 #error SYS_TIME_FREQUENCY should be equal to CH_CFG_ST_FREQUENCY
34 #elif CH_CFG_ST_FREQUENCY < (2 * PERIODIC_FREQUENCY)
35 #error CH_CFG_ST_FREQUENCY and SYS_TIME_FREQUENCY should be >= 2 x PERIODIC_FREQUENCY
36 #endif
37 
38 #ifdef FBW
40 #define Fbw(f) f ## _fbw()
41 #else
42 #define Fbw(f)
43 #endif
44 
45 #ifdef AP
47 #define Ap(f) f ## _ap()
48 #else
49 #define Ap(f)
50 #endif
51 
52 #if USE_HARD_FAULT_RECOVERY
54 #endif
55 
56 /*
57  * PPRZ/AP thread
58  */
59 static void thd_ap(void *arg);
60 static THD_WORKING_AREA(wa_thd_ap, 8192);
61 static thread_t *apThdPtr = NULL;
62 
63 /*
64  * PPRZ/FBW thread
65  */
66 static void thd_fbw(void *arg);
67 static THD_WORKING_AREA(wa_thd_fbw, 1024);
68 static thread_t *fbwThdPtr = NULL;
69 
73 int main(void)
74 {
75  // Init
76  Fbw(init);
77 #if USE_HARD_FAULT_RECOVERY
78  // if recovering from hard fault, don't call AP init, only FBW
79  if (!recovering_from_hard_fault) {
80 #endif
81  Ap(init);
82 #if USE_HARD_FAULT_RECOVERY
83  } else {
84  // but we still need downlink to be initialized
85  downlink_init();
86  }
87 #endif
88 
89  chThdSleepMilliseconds(100);
90 
91  // Create threads
92  fbwThdPtr = chThdCreateStatic(wa_thd_fbw, sizeof(wa_thd_fbw), NORMALPRIO, thd_fbw, NULL);
93 #if USE_HARD_FAULT_RECOVERY
94  // if recovering from hard fault, don't start AP thread, only FBW
95  if (!recovering_from_hard_fault) {
96 #endif
97  apThdPtr = chThdCreateStatic(wa_thd_ap, sizeof(wa_thd_ap), NORMALPRIO, thd_ap, NULL);
98 #if USE_HARD_FAULT_RECOVERY
99  }
100 #endif
101 
102  // Main loop, do nothing
103  while (TRUE) {
104  chThdSleepMilliseconds(1000);
105  }
106  return 0;
107 }
108 
109 /*
110  * PPRZ/AP thread
111  *
112  * Call PPRZ AP periodic and event functions
113  */
114 static void thd_ap(void *arg)
115 {
116  (void) arg;
117  chRegSetThreadName("AP");
118 
119  while (!chThdShouldTerminateX()) {
121  Ap(event_task);
122  chThdSleepMicroseconds(500);
123  }
124 
125  chThdExit(0);
126 }
127 
128 /*
129  * PPRZ/FBW thread
130  *
131  * Call PPRZ FBW periodic and event functions
132  */
133 static void thd_fbw(void *arg)
134 {
135  (void) arg;
136  chRegSetThreadName("FBW");
137 
138  while (!chThdShouldTerminateX()) {
140  Fbw(event_task);
141  chThdSleepMicroseconds(500);
142  }
143 
144  chThdExit(0);
145 }
146 
147 /*
148  * Terminate autopilot threads
149  * Wait until proper stop
150  */
152 {
153  if (apThdPtr != NULL) {
154  chThdTerminate(apThdPtr);
155  chThdWait(apThdPtr);
156  apThdPtr = NULL;
157  }
158  if (fbwThdPtr != NULL) {
159  chThdTerminate(fbwThdPtr);
160  chThdWait(fbwThdPtr);
161  fbwThdPtr = NULL;
162  }
163 }
164 
static thread_t * apThdPtr
Definition: main_chibios.c:61
static void thd_ap(void *arg)
Definition: main_chibios.c:114
AP ( AutoPilot ) process API.
int main(void)
Main function.
Definition: main_chibios.c:73
STATIC_INLINE void handle_periodic_tasks(void)
Definition: main.c:243
#define TRUE
Definition: std.h:4
Architecture independent timing functions.
static thread_t * fbwThdPtr
Definition: main_chibios.c:68
Arch independent mcu ( Micro Controller Unit ) utilities.
#define Ap(f)
Definition: main_chibios.c:49
FBW ( FlyByWire ) process API.
void pprz_terminate_autopilot_threads(void)
Terminate all autopilot threads Wait until proper stop.
Definition: main_chibios.c:151
#define Fbw(f)
Definition: main_chibios.c:42
static void thd_fbw(void *arg)
Definition: main_chibios.c:133
static THD_WORKING_AREA(wa_thd_ap, 8192)