From d2ea9ad293df232bcd7dc808bb6e577e3d8b483d Mon Sep 17 00:00:00 2001 From: Adeeb Shihadeh Date: Wed, 8 Nov 2023 22:54:55 -0800 Subject: [PATCH] refactor bootkick (#1690) * bootkick refactor + reset * simplify * revert reset stuff * cleanup --------- Co-authored-by: Bruce Wayne --- board/boards/black.h | 2 +- board/boards/board_declarations.h | 9 +++++++-- board/boards/dos.h | 21 +++------------------ board/boards/grey.h | 2 +- board/boards/pedal.h | 2 +- board/boards/red.h | 2 +- board/boards/red_v2.h | 2 +- board/boards/tres.h | 31 ++++++++----------------------- board/boards/uno.h | 28 ++++------------------------ board/boards/unused_funcs.h | 8 ++------ board/boards/white.h | 2 +- board/drivers/bootkick.h | 22 ++++++++++++++++++++++ board/main.c | 26 +++----------------------- 13 files changed, 55 insertions(+), 102 deletions(-) create mode 100644 board/drivers/bootkick.h diff --git a/board/boards/black.h b/board/boards/black.h index 80b265394e..caeffc3174 100644 --- a/board/boards/black.h +++ b/board/boards/black.h @@ -164,7 +164,7 @@ const harness_configuration black_harness_config = { const board board_black = { .board_type = "Black", - .board_tick = unused_board_tick, + .set_bootkick = unused_set_bootkick, .harness_config = &black_harness_config, .has_hw_gmlan = false, .has_obd = true, diff --git a/board/boards/board_declarations.h b/board/boards/board_declarations.h index e8b1cdcffa..b691f7ca7a 100644 --- a/board/boards/board_declarations.h +++ b/board/boards/board_declarations.h @@ -1,4 +1,9 @@ // ******************** Prototypes ******************** +typedef enum { + BOOT_STANDBY, + BOOT_BOOTKICK, +} BootState; + typedef void (*board_init)(void); typedef void (*board_init_bootloader)(void); typedef void (*board_enable_can_transceiver)(uint8_t transceiver, bool enabled); @@ -11,7 +16,7 @@ typedef void (*board_set_ir_power)(uint8_t percentage); typedef void (*board_set_fan_enabled)(bool enabled); typedef void (*board_set_phone_power)(bool enabled); typedef void (*board_set_siren)(bool enabled); -typedef bool (*board_board_tick)(bool ignition, bool usb_enum, bool heartbeat_seen, bool harness_inserted); +typedef void (*board_set_bootkick)(BootState state); typedef bool (*board_read_som_gpio)(void); struct board { @@ -39,7 +44,7 @@ struct board { board_set_fan_enabled set_fan_enabled; board_set_phone_power set_phone_power; board_set_siren set_siren; - board_board_tick board_tick; + board_set_bootkick set_bootkick; board_read_som_gpio read_som_gpio; }; diff --git a/board/boards/dos.h b/board/boards/dos.h index 7e0a270036..bdbf1723eb 100644 --- a/board/boards/dos.h +++ b/board/boards/dos.h @@ -49,23 +49,8 @@ void dos_set_led(uint8_t color, bool enabled) { } } -void dos_set_bootkick(bool enabled){ - set_gpio_output(GPIOC, 4, !enabled); -} - -bool dos_board_tick(bool ignition, bool usb_enum, bool heartbeat_seen, bool harness_inserted) { - bool ret = false; - if ((ignition && !usb_enum) || harness_inserted) { - // enable bootkick if ignition seen or if plugged into a harness - ret = true; - dos_set_bootkick(true); - } else if (heartbeat_seen) { - // disable once openpilot is up - dos_set_bootkick(false); - } else { - - } - return ret; +void dos_set_bootkick(BootState state) { + set_gpio_output(GPIOC, 4, state != BOOT_BOOTKICK); } void dos_set_can_mode(uint8_t mode) { @@ -208,7 +193,6 @@ const harness_configuration dos_harness_config = { const board board_dos = { .board_type = "Dos", - .board_tick = dos_board_tick, .harness_config = &dos_harness_config, .has_hw_gmlan = false, .has_obd = true, @@ -236,5 +220,6 @@ const board board_dos = { .set_ir_power = dos_set_ir_power, .set_phone_power = unused_set_phone_power, .set_siren = dos_set_siren, + .set_bootkick = dos_set_bootkick, .read_som_gpio = dos_read_som_gpio }; diff --git a/board/boards/grey.h b/board/boards/grey.h index a74f894480..1aa2fdc3d7 100644 --- a/board/boards/grey.h +++ b/board/boards/grey.h @@ -6,7 +6,7 @@ const board board_grey = { .board_type = "Grey", - .board_tick = unused_board_tick, + .set_bootkick = unused_set_bootkick, .harness_config = &white_harness_config, .has_hw_gmlan = true, .has_obd = false, diff --git a/board/boards/pedal.h b/board/boards/pedal.h index f2ae367e5d..42dd8f7efe 100644 --- a/board/boards/pedal.h +++ b/board/boards/pedal.h @@ -68,7 +68,7 @@ const harness_configuration pedal_harness_config = { const board board_pedal = { .board_type = "Pedal", - .board_tick = unused_board_tick, + .set_bootkick = unused_set_bootkick, .harness_config = &pedal_harness_config, .has_hw_gmlan = false, .has_obd = false, diff --git a/board/boards/red.h b/board/boards/red.h index 987554698e..16f68161a7 100644 --- a/board/boards/red.h +++ b/board/boards/red.h @@ -172,7 +172,7 @@ const harness_configuration red_harness_config = { const board board_red = { .board_type = "Red", - .board_tick = unused_board_tick, + .set_bootkick = unused_set_bootkick, .harness_config = &red_harness_config, .has_hw_gmlan = false, .has_obd = true, diff --git a/board/boards/red_v2.h b/board/boards/red_v2.h index df663032b0..6554ef4e1e 100644 --- a/board/boards/red_v2.h +++ b/board/boards/red_v2.h @@ -12,7 +12,7 @@ void red_panda_v2_init(void) { const board board_red_v2 = { .board_type = "Red_v2", - .board_tick = unused_board_tick, + .set_bootkick = unused_set_bootkick, .harness_config = &red_chiplet_harness_config, .has_hw_gmlan = false, .has_obd = true, diff --git a/board/boards/tres.h b/board/boards/tres.h index c1f1f9ec22..ce937cd3e7 100644 --- a/board/boards/tres.h +++ b/board/boards/tres.h @@ -14,26 +14,9 @@ void tres_set_ir_power(uint8_t percentage){ pwm_set(TIM3, 4, percentage); } -void tres_set_bootkick(bool enabled){ - set_gpio_output(GPIOA, 0, !enabled); -} - -bool tres_ignition_prev = false; -bool tres_board_tick(bool ignition, bool usb_enum, bool heartbeat_seen, bool harness_inserted) { - UNUSED(usb_enum); - bool ret = false; - if ((ignition && !tres_ignition_prev) || harness_inserted) { - // enable bootkick on rising edge of ignition - ret = true; - tres_set_bootkick(true); - } else if (heartbeat_seen) { - // disable once openpilot is up - tres_set_bootkick(false); - } else { - - } - tres_ignition_prev = ignition; - return ret; +void tres_set_bootkick(BootState state) { + set_gpio_output(GPIOA, 0, state != BOOT_BOOTKICK); + set_gpio_output(GPIOC, 12, 1); } void tres_set_fan_enabled(bool enabled) { @@ -42,7 +25,7 @@ void tres_set_fan_enabled(bool enabled) { tres_update_fan_ir_power(); } -bool tres_read_som_gpio (void){ +bool tres_read_som_gpio (void) { return (get_gpio_input(GPIOC, 2) != 0); } @@ -58,7 +41,9 @@ void tres_init(void) { set_gpio_mode(GPIOC, 2, MODE_INPUT); set_gpio_pullup(GPIOC, 2, PULL_DOWN); - tres_set_bootkick(true); + // SOM bootkick + reset lines + set_gpio_mode(GPIOC, 12, MODE_OUTPUT); + tres_set_bootkick(BOOT_BOOTKICK); // SOM debugging UART gpio_uart7_init(); @@ -87,7 +72,6 @@ void tres_init(void) { const board board_tres = { .board_type = "Tres", - .board_tick = tres_board_tick, .harness_config = &red_chiplet_harness_config, .has_hw_gmlan = false, .has_obd = true, @@ -111,5 +95,6 @@ const board board_tres = { .set_ir_power = tres_set_ir_power, .set_phone_power = unused_set_phone_power, .set_siren = fake_siren_set, + .set_bootkick = tres_set_bootkick, .read_som_gpio = tres_read_som_gpio }; diff --git a/board/boards/uno.h b/board/boards/uno.h index 27c2e40297..6fe6177399 100644 --- a/board/boards/uno.h +++ b/board/boards/uno.h @@ -1,8 +1,6 @@ // ///////////// // // Uno + Harness // // ///////////// // -#define BOOTKICK_TIME 3U -uint8_t bootkick_timer = 0U; void uno_enable_can_transceiver(uint8_t transceiver, bool enabled) { switch (transceiver){ @@ -51,8 +49,8 @@ void uno_set_led(uint8_t color, bool enabled) { } } -void uno_set_bootkick(bool enabled){ - if (enabled) { +void uno_set_bootkick(BootState state) { + if (state == BOOT_BOOTKICK) { set_gpio_output(GPIOB, 14, false); } else { // We want the pin to be floating, not forced high! @@ -60,11 +58,6 @@ void uno_set_bootkick(bool enabled){ } } -void uno_bootkick(void) { - bootkick_timer = BOOTKICK_TIME; - uno_set_bootkick(true); -} - void uno_set_phone_power(bool enabled){ set_gpio_output(GPIOB, 4, enabled); } @@ -101,19 +94,6 @@ void uno_set_can_mode(uint8_t mode) { } } -bool uno_board_tick(bool ignition, bool usb_enum, bool heartbeat_seen, bool harness_inserted) { - UNUSED(ignition); - UNUSED(usb_enum); - UNUSED(heartbeat_seen); - UNUSED(harness_inserted); - if (bootkick_timer != 0U) { - bootkick_timer--; - } else { - uno_set_bootkick(false); - } - return false; -} - bool uno_check_ignition(void){ // ignition is checked through harness return harness_check_ignition(); @@ -198,7 +178,7 @@ void uno_init(void) { } // Bootkick phone - uno_bootkick(); + uno_set_bootkick(BOOT_BOOTKICK); } void uno_init_bootloader(void) { @@ -224,7 +204,6 @@ const harness_configuration uno_harness_config = { const board board_uno = { .board_type = "Uno", - .board_tick = uno_board_tick, .harness_config = &uno_harness_config, .has_hw_gmlan = false, .has_obd = true, @@ -248,5 +227,6 @@ const board board_uno = { .set_ir_power = uno_set_ir_power, .set_phone_power = uno_set_phone_power, .set_siren = unused_set_siren, + .set_bootkick = uno_set_bootkick, .read_som_gpio = unused_read_som_gpio }; diff --git a/board/boards/unused_funcs.h b/board/boards/unused_funcs.h index 24279d2bf9..edc83e3e05 100644 --- a/board/boards/unused_funcs.h +++ b/board/boards/unused_funcs.h @@ -21,12 +21,8 @@ uint32_t unused_read_current(void) { return 0U; } -bool unused_board_tick(bool ignition, bool usb_enum, bool heartbeat_seen, bool harness_inserted) { - UNUSED(ignition); - UNUSED(usb_enum); - UNUSED(heartbeat_seen); - UNUSED(harness_inserted); - return false; +void unused_set_bootkick(BootState state) { + UNUSED(state); } bool unused_read_som_gpio(void) { diff --git a/board/boards/white.h b/board/boards/white.h index f2dedf0107..4bd4381a51 100644 --- a/board/boards/white.h +++ b/board/boards/white.h @@ -223,7 +223,7 @@ const harness_configuration white_harness_config = { const board board_white = { .board_type = "White", - .board_tick = unused_board_tick, + .set_bootkick = unused_set_bootkick, .harness_config = &white_harness_config, .has_hw_gmlan = true, .has_obd = false, diff --git a/board/drivers/bootkick.h b/board/drivers/bootkick.h new file mode 100644 index 0000000000..b0377dd0e9 --- /dev/null +++ b/board/drivers/bootkick.h @@ -0,0 +1,22 @@ +bool bootkick_ign_prev = false; +BootState boot_state = BOOT_BOOTKICK; +uint8_t bootkick_harness_status_prev = HARNESS_STATUS_NC; + +void bootkick_tick(bool ignition, bool recent_heartbeat) { + 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 { + + } + + // update state + bootkick_ign_prev = ignition; + bootkick_harness_status_prev = harness.status; + current_board->set_bootkick(boot_state); +} diff --git a/board/main.c b/board/main.c index 3f8cec188a..0d692d0e91 100644 --- a/board/main.c +++ b/board/main.c @@ -6,6 +6,7 @@ #include "drivers/gmlan_alt.h" #include "drivers/kline_init.h" #include "drivers/simple_watchdog.h" +#include "drivers/bootkick.h" #include "early_init.h" #include "provision.h" @@ -145,9 +146,6 @@ void __attribute__ ((noinline)) enable_fpu(void) { // called at 8Hz uint8_t loop_counter = 0U; -uint8_t previous_harness_status = HARNESS_STATUS_NC; -uint32_t waiting_to_boot_count = 0; -bool waiting_to_boot = false; void tick_handler(void) { if (TICK_TIMER->SR != 0) { // siren @@ -185,27 +183,9 @@ void tick_handler(void) { current_board->set_led(LED_BLUE, (uptime_cnt & 1U) && (power_save_status == POWER_SAVE_STATUS_ENABLED)); const bool recent_heartbeat = heartbeat_counter == 0U; - const bool harness_inserted = (harness.status != previous_harness_status) && (harness.status != HARNESS_STATUS_NC); - const bool just_bootkicked = current_board->board_tick(check_started(), usb_enumerated, recent_heartbeat, harness_inserted); - previous_harness_status = harness.status; - - // log device boot time - const bool som_running = current_board->read_som_gpio(); - if (just_bootkicked && !som_running) { - print("bootkick\n"); - waiting_to_boot = true; - } - if (waiting_to_boot) { - if (som_running) { - print("device booted\n"); - waiting_to_boot = false; - } else if (waiting_to_boot_count == 45U) { - print("not booted after 45s\n"); - } else { - } - waiting_to_boot_count += 1U; - } + // 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__) {