Paparazzi UAS  v5.14.0_stable-0-g3f680d1
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  * Default autopilot thread stack size
58  */
59 #ifndef AP_THREAD_STACK_SIZE
60 #define AP_THREAD_STACK_SIZE 8192
61 #endif
62 
63 /*
64  * PPRZ/AP thread
65  */
66 static void thd_ap(void *arg);
67 static THD_WORKING_AREA(wa_thd_ap, AP_THREAD_STACK_SIZE);
68 static thread_t *apThdPtr = NULL;
69 
70 /*
71  * Default FBW thread stack size
72  */
73 #ifndef FBW_THREAD_STACK_SIZE
74 #define FBW_THREAD_STACK_SIZE 1024
75 #endif
76 
77 /*
78  * PPRZ/FBW thread
79  */
80 static void thd_fbw(void *arg);
81 static THD_WORKING_AREA(wa_thd_fbw, FBW_THREAD_STACK_SIZE);
82 static thread_t *fbwThdPtr = NULL;
83 
87 int main(void)
88 {
89  // Init
90  Fbw(init);
91 #if USE_HARD_FAULT_RECOVERY
92  // if recovering from hard fault, don't call AP init, only FBW
93  if (!recovering_from_hard_fault) {
94 #endif
95  Ap(init);
96 #if USE_HARD_FAULT_RECOVERY
97  } else {
98  // but we still need downlink to be initialized
99  downlink_init();
100  modules_datalink_init();
101  }
102 #endif
103 
104  chThdSleepMilliseconds(100);
105 
106  // Create threads
107  fbwThdPtr = chThdCreateStatic(wa_thd_fbw, sizeof(wa_thd_fbw), NORMALPRIO, thd_fbw, NULL);
108 #if USE_HARD_FAULT_RECOVERY
109  // if recovering from hard fault, don't start AP thread, only FBW
110  if (!recovering_from_hard_fault) {
111 #endif
112  apThdPtr = chThdCreateStatic(wa_thd_ap, sizeof(wa_thd_ap), NORMALPRIO, thd_ap, NULL);
113 #if USE_HARD_FAULT_RECOVERY
114  }
115 #endif
116 
117  // Main loop, do nothing
118  while (TRUE) {
119  chThdSleepMilliseconds(1000);
120  }
121  return 0;
122 }
123 
124 /*
125  * PPRZ/AP thread
126  *
127  * Call PPRZ AP periodic and event functions
128  */
129 static void thd_ap(void *arg)
130 {
131  (void) arg;
132  chRegSetThreadName("AP");
133 
134  while (!chThdShouldTerminateX()) {
136  Ap(event_task);
137  chThdSleepMicroseconds(500);
138  }
139 
140  chThdExit(0);
141 }
142 
143 /*
144  * PPRZ/FBW thread
145  *
146  * Call PPRZ FBW periodic and event functions
147  */
148 static void thd_fbw(void *arg)
149 {
150  (void) arg;
151  chRegSetThreadName("FBW");
152 
153  while (!chThdShouldTerminateX()) {
155  Fbw(event_task);
156  chThdSleepMicroseconds(500);
157  }
158 
159  chThdExit(0);
160 }
161 
162 /*
163  * Terminate autopilot threads
164  * Wait until proper stop
165  */
167 {
168  if (apThdPtr != NULL) {
169  chThdTerminate(apThdPtr);
170  chThdWait(apThdPtr);
171  apThdPtr = NULL;
172  }
173  if (fbwThdPtr != NULL) {
174  chThdTerminate(fbwThdPtr);
175  chThdWait(fbwThdPtr);
176  fbwThdPtr = NULL;
177  }
178 }
179 
static void thd_fbw(void *arg)
Definition: main_chibios.c:148
#define Fbw(f)
Definition: main_chibios.c:42
static THD_WORKING_AREA(wa_thd_ap, AP_THREAD_STACK_SIZE)
STATIC_INLINE void handle_periodic_tasks(void)
Definition: main_ap.c:202
#define FBW_THREAD_STACK_SIZE
Definition: main_chibios.c:74
#define Ap(f)
Definition: main_chibios.c:49
#define TRUE
Definition: std.h:4
#define AP_THREAD_STACK_SIZE
Definition: main_chibios.c:60
Architecture independent timing functions.
static void thd_ap(void *arg)
Definition: main_chibios.c:129
static thread_t * apThdPtr
Definition: main_chibios.c:68
void pprz_terminate_autopilot_threads(void)
Terminate all autopilot threads Wait until proper stop.
Definition: main_chibios.c:166
Arch independent mcu ( Micro Controller Unit ) utilities.
int main(void)
Main function.
Definition: main_chibios.c:87
static thread_t * fbwThdPtr
Definition: main_chibios.c:82
FBW ( FlyByWire ) process API.
AP ( AutoPilot ) process API.