Skip to content

Commit

Permalink
refactor bootkick (commaai#1690)
Browse files Browse the repository at this point in the history
* bootkick refactor + reset

* simplify

* revert reset stuff

* cleanup

---------

Co-authored-by: Bruce Wayne <[email protected]>
  • Loading branch information
adeebshihadeh and Bruce Wayne authored Nov 9, 2023
1 parent cd0f0bd commit d2ea9ad
Show file tree
Hide file tree
Showing 13 changed files with 55 additions and 102 deletions.
2 changes: 1 addition & 1 deletion board/boards/black.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 7 additions & 2 deletions board/boards/board_declarations.h
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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 {
Expand Down Expand Up @@ -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;
};

Expand Down
21 changes: 3 additions & 18 deletions board/boards/dos.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
};
2 changes: 1 addition & 1 deletion board/boards/grey.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion board/boards/pedal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion board/boards/red.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion board/boards/red_v2.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
31 changes: 8 additions & 23 deletions board/boards/tres.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
}

Expand All @@ -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();
Expand Down Expand Up @@ -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,
Expand All @@ -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
};
28 changes: 4 additions & 24 deletions board/boards/uno.h
Original file line number Diff line number Diff line change
@@ -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){
Expand Down Expand Up @@ -51,20 +49,15 @@ 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!
set_gpio_mode(GPIOB, 14, MODE_INPUT);
}
}

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);
}
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -198,7 +178,7 @@ void uno_init(void) {
}

// Bootkick phone
uno_bootkick();
uno_set_bootkick(BOOT_BOOTKICK);
}

void uno_init_bootloader(void) {
Expand All @@ -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,
Expand All @@ -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
};
8 changes: 2 additions & 6 deletions board/boards/unused_funcs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion board/boards/white.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
22 changes: 22 additions & 0 deletions board/drivers/bootkick.h
Original file line number Diff line number Diff line change
@@ -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);
}
26 changes: 3 additions & 23 deletions board/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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__) {
Expand Down

0 comments on commit d2ea9ad

Please sign in to comment.