Paparazzi UAS  v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
main_chibios.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2013-2021 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, see
18  * <http://www.gnu.org/licenses/>.
19  *
20  */
21 
30 #include "mcu_periph/sys_time.h"
31 #include "mcu.h"
32 #include <ch.h>
33 
34 #ifndef SYS_TIME_FREQUENCY
35 #error SYS_TIME_FREQUENCY should be defined in Makefile.chibios or airframe.xml and be equal to CH_CFG_ST_FREQUENCY
36 #elif CH_CFG_ST_FREQUENCY < (2 * PERIODIC_FREQUENCY) || SYS_TIME_FREQUENCY < (2 * PERIODIC_FREQUENCY)
37 #error CH_CFG_ST_FREQUENCY and SYS_TIME_FREQUENCY should be >= 2 x PERIODIC_FREQUENCY
38 #endif
39 
40 #if (defined AP) && (defined FBW)
41 #error "AP and FBW can't be defined at the same time"
42 #endif
43 #if (!defined AP) && (!defined FBW)
44 #error "AP or FBW should be defined"
45 #endif
46 
47 #ifdef FBW
48 #include "main_fbw.h"
49 #define Call(f) main_fbw_ ## f
50 #endif
51 
52 #ifdef AP
53 #include "main_ap.h"
54 #define Call(f) main_ap_ ## f
55 #endif
56 
57 #include "generated/modules.h"
58 
59 #ifndef THD_WORKING_AREA_MAIN
60 #define THD_WORKING_AREA_MAIN 8192
61 #endif
62 
63 /*
64  * PPRZ/AP thread
65  */
66 static void thd_ap(void *arg);
68 static thread_t *apThdPtr = NULL;
69 
70 #if USE_HARD_FAULT_RECOVERY
71 #include "main_recovery.h"
72 
73 #ifndef THD_WORKING_AREA_RECOVERY
74 #define THD_WORKING_AREA_RECOVERY 1024
75 #endif
76 
77 static void thd_recovery(void *arg);
78 THD_WORKING_AREA(wa_thd_recovery, THD_WORKING_AREA_RECOVERY);
79 static thread_t *recoveryThdPtr = NULL;
80 #endif
81 
85 int main(void)
86 {
87  // Init
88 
89  // mcu modules init in all cases
90  modules_mcu_init();
91 
92 #if USE_HARD_FAULT_RECOVERY
93  // if recovering from hard fault, don't call AP init, only FBW
94  if (!recovering_from_hard_fault) {
95 #endif
96  Call(init());
97  chThdSleepMilliseconds(100);
98 
99  // Create threads
100  apThdPtr = chThdCreateStatic(wa_thd_ap, sizeof(wa_thd_ap), NORMALPRIO, thd_ap, NULL);
101 
102 #if USE_HARD_FAULT_RECOVERY
103  } else {
105  chThdSleepMilliseconds(100);
106 
107  // Create threads
108  recoveryThdPtr = chThdCreateStatic(wa_thd_recovery, sizeof(wa_thd_recovery), NORMALPRIO, thd_recovery, NULL);
109  }
110 #endif
111 
112  // Main loop, do nothing
113  chThdSleep(TIME_INFINITE);
114  return 0;
115 }
116 
117 /*
118  * PPRZ/AP thread
119  *
120  * Call PPRZ AP periodic and event functions
121  */
122 static void thd_ap(void *arg)
123 {
124  (void) arg;
125  chRegSetThreadName("AP");
126 
127  while (!chThdShouldTerminateX()) {
128  systime_t t = chVTGetSystemTime();
129  Call(periodic());
130  Call(event());
131  // The sleep time is computed to have a polling interval of
132  // 1e6 / CH_CFG_ST_FREQUENCY. If time is passed, thanks to the
133  // "Windowed" sleep function, the execution is not blocked until
134  // a complet roll-over.
135  chThdSleepUntilWindowed(t, t + TIME_US2I(1000000 / CH_CFG_ST_FREQUENCY));
136  }
137 
138  chThdExit(0);
139 }
140 
141 #if USE_HARD_FAULT_RECOVERY
142 /*
143  * PPRZ/RECOVERY thread
144  *
145  * Call PPRZ minimal AP after MCU hard fault
146  */
147 static void thd_recovery(void *arg)
148 {
149  (void) arg;
150  chRegSetThreadName("RECOVERY");
151 
152  while (!chThdShouldTerminateX()) {
153  systime_t t = chVTGetSystemTime();
156  // The sleep time is computed to have a polling interval of
157  // 1e6 / CH_CFG_ST_FREQUENCY. If time is passed, thanks to the
158  // "Windowed" sleep function, the execution is not blocked until
159  // a complet roll-over.
160  chThdSleepUntilWindowed(t, t + TIME_US2I(1000000 / CH_CFG_ST_FREQUENCY));
161  }
162 
163  chThdExit(0);
164 }
165 #endif
166 
167 /*
168  * Terminate autopilot threads
169  * Wait until proper stop
170  */
172 {
173  if (apThdPtr != NULL) {
174  chThdTerminate(apThdPtr);
175  chThdWait(apThdPtr);
176  apThdPtr = NULL;
177  }
178 #if USE_HARD_FAULT_RECOVERY
179  if (recoveryThdPtr != NULL) {
180  chThdTerminate(recoveryThdPtr);
181  chThdWait(recoveryThdPtr);
182  recoveryThdPtr = NULL;
183  }
184 #endif
185 }
186 
#define CH_CFG_ST_FREQUENCY
System tick frequency.
Definition: chconf.h:75
Autopilot main loop.
static thread_t * apThdPtr
Definition: main_chibios.c:68
static void thd_ap(void *arg)
Definition: main_chibios.c:122
#define THD_WORKING_AREA_MAIN
Definition: main_chibios.c:60
int main(void)
Main function.
Definition: main_chibios.c:85
THD_WORKING_AREA(wa_thd_ap, THD_WORKING_AREA_MAIN)
void pprz_terminate_autopilot_threads(void)
Terminate all autopilot threads Wait until proper stop.
Definition: main_chibios.c:171
Fly By Wire:
void main_recovery_periodic(void)
void main_recovery_event(void)
void main_recovery_init(void)
Definition: main_recovery.c:89
Recovery mode: run manual mode in case of hardfault Based on legacy FBW.
Arch independent mcu ( Micro Controller Unit ) utilities.
bool init
Definition: nav_gls.c:57
Architecture independent timing functions.