forked from commaai/panda
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
408 lines (331 loc) · 11.4 KB
/
main.c
File metadata and controls
408 lines (331 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
// ********************* Includes *********************
#include "config.h"
#include "drivers/led.h"
#include "drivers/pwm.h"
#include "drivers/usb.h"
#include "drivers/simple_watchdog.h"
#include "drivers/bootkick.h"
#include "early_init.h"
#include "provision.h"
#include "safety/safety.h"
#include "health.h"
#include "drivers/can_common_panda.h"
#ifdef STM32H7
#include "drivers/fdcan.h"
#else
#include "drivers/bxcan.h"
#endif
#include "power_saving.h"
#include "obj/gitversion.h"
#include "can_comms.h"
#include "main_comms.h"
// ********************* Serial debugging *********************
static bool check_started(void) {
bool started = current_board->check_ignition() || ignition_can;
return started;
}
void debug_ring_callback(uart_ring *ring) {
char rcv;
while (get_char(ring, &rcv)) {
(void)put_char(ring, rcv); // misra-c2012-17.7: cast to void is ok: debug function
// only allow bootloader entry on debug builds
#ifdef ALLOW_DEBUG
// jump to DFU flash
if (rcv == 'z') {
enter_bootloader_mode = ENTER_BOOTLOADER_MAGIC;
NVIC_SystemReset();
}
#endif
// normal reset
if (rcv == 'x') {
NVIC_SystemReset();
}
}
}
// ****************************** safety mode ******************************
// this is the only way to leave silent mode
void set_safety_mode(uint16_t mode, uint16_t param) {
uint16_t mode_copy = mode;
int err = set_safety_hooks(mode_copy, param);
if (err == -1) {
print("Error: safety set mode failed. Falling back to SILENT\n");
mode_copy = SAFETY_SILENT;
err = set_safety_hooks(mode_copy, 0U);
// TERMINAL ERROR: we can't continue if SILENT safety mode isn't succesfully set
assert_fatal(err == 0, "Error: Failed setting SILENT mode. Hanging\n");
}
safety_tx_blocked = 0;
safety_rx_invalid = 0;
switch (mode_copy) {
case SAFETY_SILENT:
set_intercept_relay(false, false);
if (current_board->harness_config->has_harness) {
current_board->set_can_mode(CAN_MODE_NORMAL);
}
can_silent = ALL_CAN_SILENT;
break;
case SAFETY_NOOUTPUT:
set_intercept_relay(false, false);
if (current_board->harness_config->has_harness) {
current_board->set_can_mode(CAN_MODE_NORMAL);
}
can_silent = ALL_CAN_LIVE;
break;
case SAFETY_ELM327:
set_intercept_relay(false, false);
heartbeat_counter = 0U;
heartbeat_lost = false;
if (current_board->harness_config->has_harness) {
// Clear any pending messages in the can core (i.e. sending while comma power is unplugged)
// TODO: rewrite using hardware queues rather than fifo to cancel specific messages
can_clear_send(CANIF_FROM_CAN_NUM(1), 1);
if (param == 0U) {
current_board->set_can_mode(CAN_MODE_OBD_CAN2);
} else {
current_board->set_can_mode(CAN_MODE_NORMAL);
}
}
can_silent = ALL_CAN_LIVE;
break;
default:
set_intercept_relay(true, false);
heartbeat_counter = 0U;
heartbeat_lost = false;
if (current_board->harness_config->has_harness) {
current_board->set_can_mode(CAN_MODE_NORMAL);
}
can_silent = ALL_CAN_LIVE;
break;
}
can_init_all();
}
bool is_car_safety_mode(uint16_t mode) {
return (mode != SAFETY_SILENT) &&
(mode != SAFETY_NOOUTPUT) &&
(mode != SAFETY_ALLOUTPUT) &&
(mode != SAFETY_ELM327);
}
// ***************************** main code *****************************
// cppcheck-suppress unusedFunction ; used in headers not included in cppcheck
// cppcheck-suppress misra-c2012-8.4
void __initialize_hardware_early(void) {
early_initialization();
}
static void __attribute__ ((noinline)) enable_fpu(void) {
// enable the FPU
SCB->CPACR |= ((3UL << (10U * 2U)) | (3UL << (11U * 2U)));
}
// go into SILENT when heartbeat isn't received for this amount of seconds.
#define HEARTBEAT_IGNITION_CNT_ON 5U
#define HEARTBEAT_IGNITION_CNT_OFF 2U
// called at 8Hz
static void tick_handler(void) {
static uint32_t siren_countdown = 0; // siren plays while countdown > 0
static uint32_t controls_allowed_countdown = 0;
static uint8_t prev_harness_status = HARNESS_STATUS_NC;
static uint8_t loop_counter = 0U;
if (TICK_TIMER->SR != 0U) {
// siren
current_board->set_siren((loop_counter & 1U) && (siren_enabled || (siren_countdown > 0U)));
// tick drivers at 8Hz
fan_tick();
harness_tick();
simple_watchdog_kick();
sound_tick();
// re-init everything that uses harness status
if (harness.status != prev_harness_status) {
prev_harness_status = harness.status;
can_set_orientation(harness.status == HARNESS_STATUS_FLIPPED);
// re-init everything that uses harness status
can_init_all();
set_safety_mode(current_safety_mode, current_safety_param);
set_power_save_state(power_save_status);
}
// decimated to 1Hz
if (loop_counter == 0U) {
can_live = pending_can_live;
//puth(usart1_dma); print(" "); puth(DMA2_Stream5->M0AR); print(" "); puth(DMA2_Stream5->NDTR); print("\n");
// reset this every 16th pass
if ((uptime_cnt & 0xFU) == 0U) {
pending_can_live = 0;
}
#ifdef DEBUG
print("** blink ");
print("rx:"); puth4(can_rx_q.r_ptr); print("-"); puth4(can_rx_q.w_ptr); print(" ");
print("tx1:"); puth4(can_tx1_q.r_ptr); print("-"); puth4(can_tx1_q.w_ptr); print(" ");
print("tx2:"); puth4(can_tx2_q.r_ptr); print("-"); puth4(can_tx2_q.w_ptr); print(" ");
print("tx3:"); puth4(can_tx3_q.r_ptr); print("-"); puth4(can_tx3_q.w_ptr); print("\n");
#endif
// set green LED to be controls allowed
led_set(LED_GREEN, controls_allowed | green_led_enabled);
// turn off the blue LED, turned on by CAN
// unless we are in power saving mode
led_set(LED_BLUE, (uptime_cnt & 1U) && (power_save_status == POWER_SAVE_STATUS_ENABLED));
const bool recent_heartbeat = heartbeat_counter == 0U;
// tick drivers at 1Hz
bootkick_tick(check_started(), recent_heartbeat);
// increase heartbeat counter and cap it at the uint32 limit
if (heartbeat_counter < UINT32_MAX) {
heartbeat_counter += 1U;
}
// disabling heartbeat not allowed while in safety mode
if (is_car_safety_mode(current_safety_mode)) {
heartbeat_disabled = false;
}
if (siren_countdown > 0U) {
siren_countdown -= 1U;
}
if (controls_allowed || heartbeat_engaged) {
controls_allowed_countdown = 5U;
} else if (controls_allowed_countdown > 0U) {
controls_allowed_countdown -= 1U;
} else {
}
// exit controls allowed if unused by openpilot for a few seconds
if (controls_allowed && !heartbeat_engaged) {
heartbeat_engaged_mismatches += 1U;
if (heartbeat_engaged_mismatches >= 3U) {
controls_allowed = false;
}
} else {
heartbeat_engaged_mismatches = 0U;
}
if (!heartbeat_disabled) {
// if the heartbeat has been gone for a while, go to SILENT safety mode and enter power save
if (heartbeat_counter >= (check_started() ? HEARTBEAT_IGNITION_CNT_ON : HEARTBEAT_IGNITION_CNT_OFF)) {
print("device hasn't sent a heartbeat for 0x");
puth(heartbeat_counter);
print(" seconds. Safety is set to SILENT mode.\n");
if (controls_allowed_countdown > 0U) {
siren_countdown = 3U;
controls_allowed_countdown = 0U;
}
// set flag to indicate the heartbeat was lost
if (is_car_safety_mode(current_safety_mode)) {
heartbeat_lost = true;
}
// clear heartbeat engaged state
heartbeat_engaged = false;
if (current_safety_mode != SAFETY_SILENT) {
set_safety_mode(SAFETY_SILENT, 0U);
}
if (power_save_status != POWER_SAVE_STATUS_ENABLED) {
set_power_save_state(POWER_SAVE_STATUS_ENABLED);
}
// Also disable IR when the heartbeat goes missing
current_board->set_ir_power(0U);
// Run fan when device is up but not talking to us.
// The bootloader enables the SOM GPIO on boot.
fan_set_power(current_board->read_som_gpio() ? 30U : 0U);
}
}
// check registers
check_registers();
// set ignition_can to false after 2s of no CAN seen
if (ignition_can_cnt > 2U) {
ignition_can = false;
}
// on to the next one
uptime_cnt += 1U;
safety_mode_cnt += 1U;
ignition_can_cnt += 1U;
// synchronous safety check
safety_tick(¤t_safety_config);
}
loop_counter++;
loop_counter %= 8U;
}
TICK_TIMER->SR = 0;
}
int main(void) {
// Init interrupt table
init_interrupts(true);
// shouldn't have interrupts here, but just in case
disable_interrupts();
// init early devices
clock_init();
peripherals_init();
detect_board_type();
led_init();
// red+green leds enabled until succesful USB/SPI init, as a debug indicator
led_set(LED_RED, true);
led_set(LED_GREEN, true);
adc_init();
// print hello
print("\n\n\n************************ MAIN START ************************\n");
// check for non-supported board types
assert_fatal(hw_type != HW_TYPE_UNKNOWN, "Unsupported board type");
print("Config:\n");
print(" Board type: 0x"); puth(hw_type); print("\n");
// init board
current_board->init();
current_board->set_can_mode(CAN_MODE_NORMAL);
if (current_board->harness_config->has_harness) {
harness_init();
}
// panda has an FPU, let's use it!
enable_fpu();
microsecond_timer_init();
current_board->set_siren(false);
if (current_board->fan_max_rpm > 0U) {
fan_init();
}
// init to SILENT and can silent
set_safety_mode(SAFETY_SILENT, 0U);
// enable CAN TXs
enable_can_transceivers(true);
// init watchdog for heartbeat loop, fed at 8Hz
simple_watchdog_init(FAULT_HEARTBEAT_LOOP_WATCHDOG, (3U * 1000000U / 8U));
// 8Hz timer
REGISTER_INTERRUPT(TICK_TIMER_IRQ, tick_handler, 10U, FAULT_INTERRUPT_RATE_TICK)
tick_timer_init();
#ifdef DEBUG
print("DEBUG ENABLED\n");
#endif
// enable USB (right before interrupts or enum can fail!)
usb_init();
#ifdef ENABLE_SPI
if (current_board->has_spi) {
gpio_spi_init();
spi_init();
}
#endif
led_set(LED_RED, false);
led_set(LED_GREEN, false);
led_set(LED_BLUE, false);
print("**** INTERRUPTS ON ****\n");
enable_interrupts();
// LED should keep on blinking all the time
while (true) {
if (power_save_status == POWER_SAVE_STATUS_DISABLED) {
#ifdef DEBUG_FAULTS
if (fault_status == FAULT_STATUS_NONE) {
#endif
// useful for debugging, fade breaks = panda is overloaded
for (uint32_t fade = 0U; fade < MAX_LED_FADE; fade += 1U) {
led_set(LED_RED, true);
delay(fade >> 4);
led_set(LED_RED, false);
delay((MAX_LED_FADE - fade) >> 4);
}
for (uint32_t fade = MAX_LED_FADE; fade > 0U; fade -= 1U) {
led_set(LED_RED, true);
delay(fade >> 4);
led_set(LED_RED, false);
delay((MAX_LED_FADE - fade) >> 4);
}
#ifdef DEBUG_FAULTS
} else {
led_set(LED_RED, 1);
delay(512000U);
led_set(LED_RED, 0);
delay(512000U);
}
#endif
} else {
__WFI();
SCB->SCR &= ~SCB_SCR_SLEEPDEEP_Msk;
}
}
return 0;
}