Merged in f5soh/librepilot/update_credits (pull request #529)
[librepilot.git] / flight / modules / Fault / Fault.c
blob7eb7801bf1c60a01acc072ae8c6859e7013836e8
1 /**
2 ******************************************************************************
3 * @addtogroup OpenPilotModules OpenPilot Modules
4 * @{
5 * @addtogroup FaultModule Fault Module
6 * @brief Insert various fault conditions for testing
7 * @{
9 * @file Fault.c
10 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
11 * @brief Fault module, inserts faults for testing
12 * @see The GNU Public License (GPL) Version 3
14 *****************************************************************************/
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 3 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful, but
22 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
23 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24 * for more details.
26 * You should have received a copy of the GNU General Public License along
27 * with this program; if not, write to the Free Software Foundation, Inc.,
28 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 // ****************
33 #include "openpilot.h"
34 #include <stdbool.h>
35 #include "hwsettings.h"
36 #include "faultsettings.h"
38 static bool module_enabled;
39 static uint8_t active_fault;
41 static int32_t fault_initialize(void)
43 #ifdef MODULE_FAULT_BUILTIN
44 module_enabled = true;
45 #else
46 uint8_t optionalModules[HWSETTINGS_OPTIONALMODULES_NUMELEM];
48 HwSettingsOptionalModulesGet(optionalModules);
50 if (optionalModules[HWSETTINGS_OPTIONALMODULES_FAULT] == HWSETTINGS_OPTIONALMODULES_ENABLED) {
51 module_enabled = true;
52 } else {
53 module_enabled = false;
55 #endif
57 /* Do this outside the module_enabled test so that it
58 * can be changed even when the module has been disabled.
59 * This is important so we can remove faults even when
60 * we've booted in BootFault recovery mode with all optional
61 * modules disabled.
64 if (module_enabled) {
65 FaultSettingsActivateFaultGet(&active_fault);
67 switch (active_fault) {
68 case FAULTSETTINGS_ACTIVATEFAULT_MODULEINITASSERT:
69 /* Simulate an assert during module init */
70 PIOS_Assert(0);
71 break;
72 case FAULTSETTINGS_ACTIVATEFAULT_INITOUTOFMEMORY:
73 /* Leak all available memory */
74 while (pios_malloc(10)) {
77 break;
78 case FAULTSETTINGS_ACTIVATEFAULT_INITBUSERROR:
80 /* Force a bad access */
81 uint32_t *bad_ptr = (uint32_t *)0xFFFFFFFF;
82 *bad_ptr = 0xAA55AA55;
84 break;
88 return 0;
91 static void fault_task(void *parameters);
93 static int32_t fault_start(void)
95 xTaskHandle fault_task_handle;
97 if (module_enabled) {
98 switch (active_fault) {
99 case FAULTSETTINGS_ACTIVATEFAULT_RUNAWAYTASK:
100 case FAULTSETTINGS_ACTIVATEFAULT_TASKOUTOFMEMORY:
101 xTaskCreate(fault_task,
102 (signed char *)"Fault",
103 configMINIMAL_STACK_SIZE,
104 NULL,
105 configMAX_PRIORITIES - 1,
106 &fault_task_handle);
107 return 0;
109 break;
112 return -1;
114 MODULE_INITCALL(fault_initialize, fault_start);
116 static void fault_task(__attribute__((unused)) void *parameters)
118 switch (active_fault) {
119 case FAULTSETTINGS_ACTIVATEFAULT_RUNAWAYTASK:
120 /* Consume all realtime, not letting the systemtask run */
121 while (1) {
124 break;
125 case FAULTSETTINGS_ACTIVATEFAULT_TASKOUTOFMEMORY:
126 /* Leak all available memory and then sleep */
127 while (pios_malloc(10)) {
130 while (1) {
131 vTaskDelay(1000);
133 break;
138 * @}
139 * @}