SAE Teensy ECU
IIT SAE Microcontroller programming
Loading...
Searching...
No Matches
Faults.cpp
Go to the documentation of this file.
1
12// @cond
13
14#include "Faults.h"
15#include "Canbus.h"
16#include "FaultConfig.def"
17#include "Log.h"
18#include "Pins.h"
19
20namespace Fault {
21
22static LOG_TAG ID = "Fault Check";
23
24#define X(...) ,
25static const int HARD_CAN_COUNT = PP_NARG_MO(HARD_FAULT_ADD);
26static const int SOFT_CAN_COUNT = PP_NARG_MO(SOFT_FAULT_ADD);
27static const int CAN_COUNT = PP_NARG_MO(HARD_FAULT_ADD) + PP_NARG_MO(SOFT_FAULT_ADD); // Number of Can addresses we need to account for
28
29static const int HARD_PIN_COUNT = PP_NARG_MO(HARD_PIN_FAULTS);
30static const int SOFT_PIN_COUNT = PP_NARG_MO(SOFT_PIN_FAULTS);
31static const int PIN_COUNT = PP_NARG_MO(HARD_PIN_FAULTS) + PP_NARG_MO(SOFT_PIN_FAULTS); // Number of Pins we need to account for
32#undef X
33
34typedef struct CanFault {
35 uint32_t address;
36 Canbus::Buffer buffer;
37 uint64_t faultMask;
38 LOG_MSG *tags;
39 bool faulted = false;
40 union lastValue {
41 uint8_t arr[8];
42 uint64_t longlong;
43 } lastValue;
44
45 CanFault(const uint32_t address, const uint64_t faultMask) {
46 this->address = address;
47 this->buffer = address;
48 this->faultMask = faultMask;
49 }
50
51 bool check() {
52 uint64_t curr = buffer.getULong();
53 if (curr & faultMask) {
54 buffer.dump(lastValue.arr);
55#ifdef CONF_ECU_DEBUG
56 Log.w(ID, "Faulted CAN Address:", address);
57#endif
58 return faulted = true;
59 }
60 return faulted = false;
61 }
62
63 void clear() {
64 buffer.clear();
65 lastValue.longlong = 0;
66 faulted = false;
67 }
68
69 void log() {
70#define X(add, mask, id) \
71 if (address == add && lastValue.longlong & mask) { \
72 /*PRE_BUILD_IGNORE*/ Log.e(ID, id); \
73 }
74 // Ignore Pre_Build error
76#undef X
77 clear();
78 }
79} CanFault;
80
81typedef struct PinFault {
82 int lastValue = 0;
83 uint8_t GPIO_Pin;
84 bool faulted = false;
85
86 PinFault(const uint8_t GPIO_Pin) {
87 this->GPIO_Pin = GPIO_Pin;
88 }
89
90 void clear() {
91 lastValue = 0;
92 faulted = false;
93 }
94
95 bool check() {
96#define X(pin, comp, value, id) (GPIO_Pin == pin && Pins::getPinValue(pin) comp value) ||
98#ifdef CONF_ECU_DEBUG
99 Log.w(ID, "Faulted pin:", GPIO_Pin);
100#endif
101 return faulted = true;
102 }
103#undef X
104 return faulted = false;
105 }
106
107 void log() {
108#define X(pin, comp, value, id) \
109 if (GPIO_Pin == pin && this->faulted) { \
110 /*PRE_BUILD_IGNORE*/ Log.e(ID, id); \
111 }
114#undef X
115 clear();
116 }
117} PinFault;
118
119#define X(...) ,
120#if PP_NARG_MO(HARD_FAULT_ADD) > 0
121#undef X
122static CanFault hardCanFaults[HARD_CAN_COUNT] = {
123#define X(add, mask, tag) CanFault(add, mask),
125#undef X
126};
127#endif
128
129#define X(...) ,
130#if PP_NARG_MO(SOFT_FAULT_ADD) > 0
131#undef X
132static CanFault softCanFaults[SOFT_CAN_COUNT] = {
133#define X(add, mask, tag) CanFault(add, mask),
135#undef X
136};
137#endif
138
139#define X(...) ,
140#if PP_NARG_MO(HARD_PIN_FAULTS) > 0
141#undef X
142static PinFault hardPinFaults[HARD_PIN_COUNT] = {
143#define X(pin, comp, value, id) PinFault(pin),
145#undef X
146};
147#endif
148
149#define X(...) ,
150#if PP_NARG_MO(SOFT_PIN_FAULTS) > 0
151#undef X
152static PinFault softPinFaults[SOFT_PIN_COUNT] = {
153#define X(pin, comp, value, id) PinFault(pin),
155#undef X
156};
157#endif
158
159#define X(...) ,
160
161bool hardFault(void) {
162 bool faulted = false;
163
164#if PP_NARG_MO(HARD_FAULT_ADD) > 0
165 for (size_t i = 0; i < HARD_CAN_COUNT; i++) {
166 faulted |= hardCanFaults[i].check();
167 }
168#endif
169
170#if PP_NARG_MO(HARD_PIN_FAULTS) > 0
171 for (size_t i = 0; i < HARD_PIN_COUNT; i++) {
172 faulted |= hardPinFaults[i].check();
173 }
174#endif
175
176 return faulted;
177}
178
179bool softFault(void) {
180 bool faulted = false;
181
182#if PP_NARG_MO(SOFT_FAULT_ADD) > 0
183 for (size_t i = 0; i < SOFT_CAN_COUNT; i++) {
184 faulted |= softCanFaults[i].check();
185 }
186#endif
187
188#if PP_NARG_MO(SOFT_PIN_FAULTS) > 0
189 for (size_t i = 0; i < SOFT_PIN_COUNT; i++) {
190 faulted |= softPinFaults[i].check();
191 }
192#endif
193
194 return faulted;
195}
196
197void logFault(void) {
198#if PP_NARG_MO(HARD_FAULT_ADD) > 0
199 for (size_t i = 0; i < HARD_CAN_COUNT; i++) {
200 hardCanFaults[i].log();
201 }
202#endif
203#if PP_NARG_MO(SOFT_FAULT_ADD) > 0
204 for (size_t i = 0; i < SOFT_CAN_COUNT; i++) {
205 softCanFaults[i].log();
206 }
207#endif
208#if PP_NARG_MO(HARD_PIN_FAULTS) > 0
209 for (size_t i = 0; i < HARD_PIN_COUNT; i++) {
210 hardPinFaults[i].log();
211 }
212#endif
213#if PP_NARG_MO(SOFT_PIN_FAULTS) > 0
214 for (size_t i = 0; i < SOFT_PIN_COUNT; i++) {
215 softPinFaults[i].log();
216 }
217#endif
218}
219
220#undef X
221
222bool anyFault(void) {
223 return hardFault() || softFault();
224}
225
226} // namespace Fault
227 // @endcond
FlexCAN_T4 wrapper.
Configure fault checking.
#define SOFT_PIN_FAULTS
Defines Pins that should be checked for hard faults.
#define SOFT_FAULT_ADD
Defines CAN addresses that should be checked for soft faults.
#define HARD_FAULT_ADD
Defines CAN addresses that should be checked for hard faults.
#define HARD_PIN_FAULTS
Defines Pins that should be checked for hard faults.
#define CAN_FAULT_IDS
Defines address bit field identifiers.
Checks for defined faults from canbus addresses or pins.
Special logging functionality.
const char * LOG_MSG
Type definition for logging messages, only used internally.
Definition Log.h:52
const char * LOG_TAG
Type definition of logging tags This typedef is necessary to allow for easier manipulation of code by...
Definition Log.h:48
Logging::Log_t Log
The global logging object.
Update, set, and get predefined pin values.
Fault checking functionality.
Definition Faults.h:29
bool softFault(void)
Check if any non-serious fault has occurred.
bool hardFault(void)
Checks if any serious fault has occurred.
bool anyFault(void)
Checks both hardFault and softFault.
void logFault(void)
Interprets and logs the last fault that was checked using the Log library.
void w(LOG_TAG TAG, LOG_MSG message)
Log a string using a warning tag.