Paparazzi UAS  v5.12_stable-4-g9b43e9b
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  modules_datalink_init();
87  }
88 #endif
89 
90  chThdSleepMilliseconds(100);
91 
92  // Create threads
93  fbwThdPtr = chThdCreateStatic(wa_thd_fbw, sizeof(wa_thd_fbw), NORMALPRIO, thd_fbw, NULL);
94 #if USE_HARD_FAULT_RECOVERY
95  // if recovering from hard fault, don't start AP thread, only FBW
96  if (!recovering_from_hard_fault) {
97 #endif
98  apThdPtr = chThdCreateStatic(wa_thd_ap, sizeof(wa_thd_ap), NORMALPRIO, thd_ap, NULL);
99 #if USE_HARD_FAULT_RECOVERY
100  }
101 #endif
102 
103  // Main loop, do nothing
104  while (TRUE) {
105  chThdSleepMilliseconds(1000);
106  }
107  return 0;
108 }
109 
110 /*
111  * PPRZ/AP thread
112  *
113  * Call PPRZ AP periodic and event functions
114  */
115 static void thd_ap(void *arg)
116 {
117  (void) arg;
118  chRegSetThreadName("AP");
119 
120  while (!chThdShouldTerminateX()) {
122  Ap(event_task);
123  chThdSleepMicroseconds(500);
124  }
125 
126  chThdExit(0);
127 }
128 
129 /*
130  * PPRZ/FBW thread
131  *
132  * Call PPRZ FBW periodic and event functions
133  */
134 static void thd_fbw(void *arg)
135 {
136  (void) arg;
137  chRegSetThreadName("FBW");
138 
139  while (!chThdShouldTerminateX()) {
141  Fbw(event_task);
142  chThdSleepMicroseconds(500);
143  }
144 
145  chThdExit(0);
146 }
147 
148 /*
149  * Terminate autopilot threads
150  * Wait until proper stop
151  */
153 {
154  if (apThdPtr != NULL) {
155  chThdTerminate(apThdPtr);
156  chThdWait(apThdPtr);
157  apThdPtr = NULL;
158  }
159  if (fbwThdPtr != NULL) {
160  chThdTerminate(fbwThdPtr);
161  chThdWait(fbwThdPtr);
162  fbwThdPtr = NULL;
163  }
164 }
165 
static void thd_fbw(void *arg)
Definition: main_chibios.c:134
#define Fbw(f)
Definition: main_chibios.c:42
STATIC_INLINE void handle_periodic_tasks(void)
Definition: main_ap.c:200
#define Ap(f)
Definition: main_chibios.c:49
#define TRUE
Definition: std.h:4
Architecture independent timing functions.
static void thd_ap(void *arg)
Definition: main_chibios.c:115
static thread_t * apThdPtr
Definition: main_chibios.c:61
void pprz_terminate_autopilot_threads(void)
Terminate all autopilot threads Wait until proper stop.
Definition: main_chibios.c:152
Arch independent mcu ( Micro Controller Unit ) utilities.
int main(void)
Main function.
Definition: main_chibios.c:73
static thread_t * fbwThdPtr
Definition: main_chibios.c:68
static THD_WORKING_AREA(wa_thd_ap, 8192)
FBW ( FlyByWire ) process API.
AP ( AutoPilot ) process API.