dRonin  adbada4
dRonin firmware
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Fault.c
Go to the documentation of this file.
1 
15 /*
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.
20  *
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.
25  *
26  * You should have received a copy of the GNU General Public License along
27  * with this program; if not, see <http://www.gnu.org/licenses/>
28  */
29 
30 // ****************
31 
32 #include "openpilot.h"
33 #include <stdbool.h>
34 #include "modulesettings.h"
35 #include "faultsettings.h"
36 #include "pios_thread.h"
37 
38 static bool module_enabled;
39 static uint8_t active_fault;
40 
41 static int32_t fault_initialize(void)
42 {
43 #ifdef MODULE_Fault_BUILTIN
44  module_enabled = true;
45 #else
46  uint8_t module_state[MODULESETTINGS_ADMINSTATE_NUMELEM];
47  ModuleSettingsAdminStateGet(module_state);
48  if (module_state[MODULESETTINGS_ADMINSTATE_FAULT] == MODULESETTINGS_ADMINSTATE_ENABLED) {
49  module_enabled = true;
50  } else {
51  module_enabled = false;
52  }
53 #endif
54 
55  /* Do this outside the module_enabled test so that it
56  * can be changed even when the module has been disabled.
57  * This is important so we can remove faults even when
58  * we've booted in BootFault recovery mode with all optional
59  * modules disabled.
60  */
61  if (FaultSettingsInitialize() == -1) {
62  module_enabled = false;
63  return -1;
64  }
65 
66  if (module_enabled) {
67  FaultSettingsActivateFaultGet(&active_fault);
68 
69  switch (active_fault) {
70  case FAULTSETTINGS_ACTIVATEFAULT_MODULEINITASSERT:
71  /* Simulate an assert during module init */
72  PIOS_Assert(0);
73  break;
74  case FAULTSETTINGS_ACTIVATEFAULT_INITOUTOFMEMORY:
75  /* Leak all available memory */
76  while (PIOS_malloc(10)) ;
77  break;
78  case FAULTSETTINGS_ACTIVATEFAULT_INITBUSERROR:
79  {
80  /* Force a bad access */
81  uint32_t * bad_ptr = (uint32_t *)0xFFFFFFFF;
82  *bad_ptr = 0xAA55AA55;
83  }
84  break;
85  }
86  }
87 
88  return 0;
89 }
90 
91 static void fault_task(void *parameters);
92 
93 static int32_t fault_start(void)
94 {
95  struct pios_thread *fault_task_handle;
96 
97  if (module_enabled) {
98  switch (active_fault) {
99  case FAULTSETTINGS_ACTIVATEFAULT_RUNAWAYTASK:
100  case FAULTSETTINGS_ACTIVATEFAULT_TASKOUTOFMEMORY:
102  (void) fault_task_handle;
103 
104  return 0;
105  break;
106  }
107  }
108  return -1;
109 }
111 
112 static void fault_task(void *parameters)
113 {
114  switch (active_fault) {
115  case FAULTSETTINGS_ACTIVATEFAULT_RUNAWAYTASK:
116  /* Consume all realtime, not letting the systemtask run */
117  while(1);
118  break;
119  case FAULTSETTINGS_ACTIVATEFAULT_TASKOUTOFMEMORY:
120  /* Leak all available memory and then sleep */
121  while (PIOS_malloc(10)) ;
122  while (1) {
123  PIOS_Thread_Sleep(1000);
124  }
125  break;
126  }
127 }
128 
static uint8_t active_fault
Definition: Fault.c:39
static void fault_task(void *parameters)
Definition: Fault.c:112
void * PIOS_malloc(size_t size)
Definition: pios_heap.c:125
#define PIOS_THREAD_STACK_SIZE_MIN
Definition: pios_thread.h:55
#define MODULE_INITCALL(ifn, sfn)
Definition: pios_initcall.h:67
struct pios_thread * PIOS_Thread_Create(void(*fp)(void *), const char *namep, size_t stack_bytes, void *argp, enum pios_thread_prio_e prio)
Definition: pios_thread.c:89
static int32_t fault_initialize(void)
Definition: Fault.c:41
void PIOS_Thread_Sleep(uint32_t time_ms)
Definition: pios_thread.c:229
Includes PiOS and core architecture components.
static int32_t fault_start(void)
Definition: Fault.c:93
static bool module_enabled
Definition: Fault.c:38
#define PIOS_Assert(test)
Definition: pios_debug.h:52