explicit state machine

This commit is contained in:
Shane Smiskol 2023-11-09 16:59:30 -08:00
parent a46b842e0c
commit 7f5974d3d4
1 changed files with 31 additions and 32 deletions

View File

@ -8,18 +8,8 @@ bool bootkick_reset_triggered = false;
uint16_t bootkick_last_serial_ptr = 0;
void bootkick_tick(bool ignition, bool recent_heartbeat) {
BootState boot_state_prev = boot_state;
const bool harness_inserted = (harness.status != bootkick_harness_status_prev) && (harness.status != HARNESS_STATUS_NC);
if ((ignition && !bootkick_ign_prev) || harness_inserted) {
// bootkick on rising edge of ignition or harness insertion
boot_state = BOOT_BOOTKICK;
} else if (recent_heartbeat) {
// disable bootkick once openpilot is up
boot_state = BOOT_STANDBY;
} else {
}
/*
Ensure SOM boots in case it goes into QDL mode. Reset behavior:
@ -28,40 +18,49 @@ void bootkick_tick(bool ignition, bool recent_heartbeat) {
* only try once per panda boot, since openpilot will reset panda on startup
* once BOOT_RESET is triggered, it stays until countdown is finished
*/
if (!bootkick_reset_triggered && (boot_state == BOOT_BOOTKICK) && (boot_state_prev == BOOT_STANDBY)) {
waiting_to_boot_countdown = 45U;
}
if (waiting_to_boot_countdown > 0U) {
bool serial_activity = uart_ring_som_debug.w_ptr_tx != bootkick_last_serial_ptr;
if (serial_activity || current_board->read_som_gpio() || (boot_state != BOOT_BOOTKICK)) {
if (boot_state == BOOT_STANDBY) {
if ((ignition && !bootkick_ign_prev) || harness_inserted) {
// bootkick on rising edge of ignition or harness insertion,
// start countdown if we haven't previously tried bootkick reset
boot_state = BOOT_BOOTKICK;
if (!bootkick_reset_triggered) {
waiting_to_boot_countdown = 45U;
}
} else if (boot_state == BOOT_BOOTKICK) {
if (recent_heartbeat) {
// disable bootkick once openpilot is up
waiting_to_boot_countdown = 0U;
boot_state = BOOT_STANDBY;
} else {
// try a reset
if (waiting_to_boot_countdown == 1U) {
boot_reset_countdown = 5U;
if (waiting_to_boot_countdown > 0U) {
waiting_to_boot_countdown--;
bool serial_activity = uart_ring_som_debug.w_ptr_tx != bootkick_last_serial_ptr;
if (serial_activity || current_board->read_som_gpio()) {
waiting_to_boot_countdown = 0U;
} else {
// try a reset
if (waiting_to_boot_countdown == 0U) {
boot_reset_countdown = 5U;
boot_state = BOOT_RESET;
}
}
}
}
}
// handle reset state
if (boot_reset_countdown > 0U) {
boot_state = BOOT_RESET;
} else if (boot_state == BOOT_RESET) {
boot_reset_countdown--;
bootkick_reset_triggered = true;
} else {
if (boot_state == BOOT_RESET) {
boot_state = BOOT_BOOTKICK;
if (boot_reset_countdown == 0U) {
boot_state = BOOT_STANDBY;
}
} else {
}
// update state
bootkick_ign_prev = ignition;
bootkick_harness_status_prev = harness.status;
bootkick_last_serial_ptr = uart_ring_som_debug.w_ptr_tx;
if (waiting_to_boot_countdown > 0U) {
waiting_to_boot_countdown--;
}
if (boot_reset_countdown > 0U) {
boot_reset_countdown--;
}
current_board->set_bootkick(boot_state);
}