USB power mode + bootkick cleanup (#1051)

* remove tick

* remove more

* delete more

* board tick

* update health

* dos bootkicking

* cleanup
This commit is contained in:
Adeeb Shihadeh 2022-08-31 20:46:07 -07:00 committed by GitHub
parent f040d8b726
commit 13d64d4cc3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 59 additions and 173 deletions

View File

@ -57,26 +57,6 @@ void black_set_usb_load_switch(bool enabled) {
set_gpio_output(GPIOB, 1, !enabled);
}
void black_set_usb_power_mode(uint8_t mode) {
bool valid = false;
switch (mode) {
case USB_POWER_CLIENT:
black_set_usb_load_switch(false);
valid = true;
break;
case USB_POWER_CDP:
black_set_usb_load_switch(true);
valid = true;
break;
default:
puts("Invalid USB power mode\n");
break;
}
if (valid) {
usb_power_mode = mode;
}
}
void black_set_gps_mode(uint8_t mode) {
switch (mode) {
case GPS_DISABLED:
@ -162,9 +142,6 @@ void black_init(void) {
// Turn on USB load switch.
black_set_usb_load_switch(true);
// Set right power mode
black_set_usb_power_mode(USB_POWER_CDP);
// Initialize harness
harness_init();
@ -204,6 +181,7 @@ const harness_configuration black_harness_config = {
const board board_black = {
.board_type = "Black",
.board_tick = unused_board_tick,
.harness_config = &black_harness_config,
.has_gps = true,
.has_hw_gmlan = false,
@ -215,10 +193,8 @@ const board board_black = {
.enable_can_transceiver = black_enable_can_transceiver,
.enable_can_transceivers = black_enable_can_transceivers,
.set_led = black_set_led,
.set_usb_power_mode = black_set_usb_power_mode,
.set_gps_mode = black_set_gps_mode,
.set_can_mode = black_set_can_mode,
.usb_power_mode_tick = unused_usb_power_mode_tick,
.check_ignition = black_check_ignition,
.read_current = unused_read_current,
.set_fan_enabled = unused_set_fan_enabled,

View File

@ -3,10 +3,8 @@ typedef void (*board_init)(void);
typedef void (*board_enable_can_transceiver)(uint8_t transceiver, bool enabled);
typedef void (*board_enable_can_transceivers)(bool enabled);
typedef void (*board_set_led)(uint8_t color, bool enabled);
typedef void (*board_set_usb_power_mode)(uint8_t mode);
typedef void (*board_set_gps_mode)(uint8_t mode);
typedef void (*board_set_can_mode)(uint8_t mode);
typedef void (*board_usb_power_mode_tick)(uint32_t uptime);
typedef bool (*board_check_ignition)(void);
typedef uint32_t (*board_read_current)(void);
typedef void (*board_set_ir_power)(uint8_t percentage);
@ -14,6 +12,7 @@ typedef void (*board_set_fan_enabled)(bool enabled);
typedef void (*board_set_phone_power)(bool enabled);
typedef void (*board_set_clock_source_mode)(uint8_t mode);
typedef void (*board_set_siren)(bool enabled);
typedef void (*board_board_tick)(bool ignition, bool usb_enum, bool heartbeat_seen);
struct board {
const char *board_type;
@ -28,10 +27,8 @@ struct board {
board_enable_can_transceiver enable_can_transceiver;
board_enable_can_transceivers enable_can_transceivers;
board_set_led set_led;
board_set_usb_power_mode set_usb_power_mode;
board_set_gps_mode set_gps_mode;
board_set_can_mode set_can_mode;
board_usb_power_mode_tick usb_power_mode_tick;
board_check_ignition check_ignition;
board_read_current read_current;
board_set_ir_power set_ir_power;
@ -39,6 +36,7 @@ struct board {
board_set_phone_power set_phone_power;
board_set_clock_source_mode set_clock_source_mode;
board_set_siren set_siren;
board_board_tick board_tick;
};
// ******************* Definitions ********************
@ -73,6 +71,3 @@ struct board {
#define CAN_MODE_GMLAN_CAN2 1U
#define CAN_MODE_GMLAN_CAN3 2U
#define CAN_MODE_OBD_CAN2 3U
// ********************* Globals **********************
uint8_t usb_power_mode = USB_POWER_NONE;

View File

@ -53,23 +53,15 @@ void dos_set_bootkick(bool enabled){
set_gpio_output(GPIOC, 4, !enabled);
}
void dos_set_usb_power_mode(uint8_t mode) {
bool valid = false;
switch (mode) {
case USB_POWER_CLIENT:
dos_set_bootkick(false);
valid = true;
break;
case USB_POWER_CDP:
dos_set_bootkick(true);
valid = true;
break;
default:
puts("Invalid USB power mode\n");
break;
}
if (valid) {
usb_power_mode = mode;
void dos_board_tick(bool ignition, bool usb_enum, bool heartbeat_seen) {
if (ignition && !usb_enum) {
// enable bootkick if ignition seen
dos_set_bootkick(true);
} else if (heartbeat_seen) {
// disable once openpilot is up
dos_set_bootkick(false);
} else {
}
}
@ -173,6 +165,9 @@ void dos_init(void) {
dos_set_led(LED_GREEN, false);
dos_set_led(LED_BLUE, false);
// Bootkick
dos_set_bootkick(true);
// Set normal CAN mode
dos_set_can_mode(CAN_MODE_NORMAL);
@ -201,6 +196,7 @@ const harness_configuration dos_harness_config = {
const board board_dos = {
.board_type = "Dos",
.board_tick = dos_board_tick,
.harness_config = &dos_harness_config,
.has_gps = false,
.has_hw_gmlan = false,
@ -212,15 +208,13 @@ const board board_dos = {
.enable_can_transceiver = dos_enable_can_transceiver,
.enable_can_transceivers = dos_enable_can_transceivers,
.set_led = dos_set_led,
.set_usb_power_mode = dos_set_usb_power_mode,
.set_gps_mode = unused_set_gps_mode,
.set_can_mode = dos_set_can_mode,
.usb_power_mode_tick = unused_usb_power_mode_tick,
.check_ignition = dos_check_ignition,
.read_current = unused_read_current,
.set_fan_enabled = dos_set_fan_enabled,
.set_ir_power = dos_set_ir_power,
.set_phone_power = unused_set_phone_power,
.set_clock_source_mode = dos_set_clock_source_mode,
.set_siren = dos_set_siren
.set_siren = unused_set_siren
};

View File

@ -35,6 +35,7 @@ void grey_set_gps_mode(uint8_t mode) {
const board board_grey = {
.board_type = "Grey",
.board_tick = unused_board_tick,
.harness_config = &white_harness_config,
.has_gps = true,
.has_hw_gmlan = true,
@ -46,10 +47,8 @@ const board board_grey = {
.enable_can_transceiver = white_enable_can_transceiver,
.enable_can_transceivers = white_enable_can_transceivers,
.set_led = white_set_led,
.set_usb_power_mode = white_set_usb_power_mode,
.set_gps_mode = grey_set_gps_mode,
.set_can_mode = white_set_can_mode,
.usb_power_mode_tick = unused_usb_power_mode_tick,
.check_ignition = white_check_ignition,
.read_current = white_read_current,
.set_fan_enabled = unused_set_fan_enabled,

View File

@ -30,11 +30,6 @@ void pedal_set_led(uint8_t color, bool enabled) {
}
}
void pedal_set_usb_power_mode(uint8_t mode){
usb_power_mode = mode;
puts("Trying to set USB power mode on pedal. This is not supported.\n");
}
void pedal_set_gps_mode(uint8_t mode) {
UNUSED(mode);
puts("Trying to set ESP/GPS mode on pedal. This is not supported.\n");
@ -78,6 +73,7 @@ const harness_configuration pedal_harness_config = {
const board board_pedal = {
.board_type = "Pedal",
.board_tick = unused_board_tick,
.harness_config = &pedal_harness_config,
.has_gps = false,
.has_hw_gmlan = false,
@ -89,10 +85,8 @@ const board board_pedal = {
.enable_can_transceiver = pedal_enable_can_transceiver,
.enable_can_transceivers = pedal_enable_can_transceivers,
.set_led = pedal_set_led,
.set_usb_power_mode = pedal_set_usb_power_mode,
.set_gps_mode = pedal_set_gps_mode,
.set_can_mode = pedal_set_can_mode,
.usb_power_mode_tick = unused_usb_power_mode_tick,
.check_ignition = pedal_check_ignition,
.read_current = unused_read_current,
.set_fan_enabled = unused_set_fan_enabled,

View File

@ -53,25 +53,6 @@ void red_set_usb_load_switch(bool enabled) {
set_gpio_output(GPIOB, 14, !enabled);
}
void red_set_usb_power_mode(uint8_t mode) {
bool valid = false;
switch (mode) {
case USB_POWER_CLIENT:
red_set_usb_load_switch(false);
valid = true;
break;
case USB_POWER_CDP:
red_set_usb_load_switch(true);
valid = true;
break;
default:
break;
}
if (valid) {
usb_power_mode = mode;
}
}
void red_set_can_mode(uint8_t mode) {
switch (mode) {
case CAN_MODE_NORMAL:
@ -139,9 +120,6 @@ void red_init(void) {
// Turn on USB load switch.
red_set_usb_load_switch(true);
// Set right power mode
red_set_usb_power_mode(USB_POWER_CDP);
// Initialize harness
harness_init();
@ -181,6 +159,7 @@ const harness_configuration red_harness_config = {
const board board_red = {
.board_type = "Red",
.board_tick = unused_board_tick,
.harness_config = &red_harness_config,
.has_gps = false,
.has_hw_gmlan = false,
@ -192,10 +171,8 @@ const board board_red = {
.enable_can_transceiver = red_enable_can_transceiver,
.enable_can_transceivers = red_enable_can_transceivers,
.set_led = red_set_led,
.set_usb_power_mode = red_set_usb_power_mode,
.set_gps_mode = unused_set_gps_mode,
.set_can_mode = red_set_can_mode,
.usb_power_mode_tick = unused_usb_power_mode_tick,
.check_ignition = red_check_ignition,
.read_current = unused_read_current,
.set_fan_enabled = unused_set_fan_enabled,

View File

@ -56,7 +56,7 @@ void uno_set_gps_load_switch(bool enabled) {
}
void uno_set_bootkick(bool enabled){
if(enabled){
if (enabled) {
set_gpio_output(GPIOB, 14, false);
} else {
// We want the pin to be floating, not forced high!
@ -73,25 +73,6 @@ void uno_set_phone_power(bool enabled){
set_gpio_output(GPIOB, 4, enabled);
}
void uno_set_usb_power_mode(uint8_t mode) {
bool valid = false;
switch (mode) {
case USB_POWER_CLIENT:
valid = true;
break;
case USB_POWER_CDP:
uno_bootkick();
valid = true;
break;
default:
puts("Invalid USB power mode\n");
break;
}
if (valid) {
usb_power_mode = mode;
}
}
void uno_set_gps_mode(uint8_t mode) {
switch (mode) {
case GPS_DISABLED:
@ -145,9 +126,11 @@ void uno_set_can_mode(uint8_t mode){
}
}
void uno_usb_power_mode_tick(uint32_t uptime){
UNUSED(uptime);
if(bootkick_timer != 0U){
void uno_board_tick(bool ignition, bool usb_enum, bool heartbeat_seen) {
UNUSED(ignition);
UNUSED(usb_enum);
UNUSED(heartbeat_seen);
if (bootkick_timer != 0U) {
bootkick_timer--;
} else {
uno_set_bootkick(false);
@ -262,6 +245,7 @@ const harness_configuration uno_harness_config = {
const board board_uno = {
.board_type = "Uno",
.board_tick = uno_board_tick,
.harness_config = &uno_harness_config,
.has_gps = true,
.has_hw_gmlan = false,
@ -273,10 +257,8 @@ const board board_uno = {
.enable_can_transceiver = uno_enable_can_transceiver,
.enable_can_transceivers = uno_enable_can_transceivers,
.set_led = uno_set_led,
.set_usb_power_mode = uno_set_usb_power_mode,
.set_gps_mode = uno_set_gps_mode,
.set_can_mode = uno_set_can_mode,
.usb_power_mode_tick = uno_usb_power_mode_tick,
.check_ignition = uno_check_ignition,
.read_current = unused_read_current,
.set_fan_enabled = uno_set_fan_enabled,

View File

@ -2,10 +2,6 @@ void unused_set_gps_mode(uint8_t mode) {
UNUSED(mode);
}
void unused_usb_power_mode_tick(uint32_t uptime) {
UNUSED(uptime);
}
void unused_set_ir_power(uint8_t percentage) {
UNUSED(percentage);
}
@ -29,3 +25,9 @@ void unused_set_siren(bool enabled) {
uint32_t unused_read_current(void) {
return 0U;
}
void unused_board_tick(bool ignition, bool usb_enum, bool heartbeat_seen) {
UNUSED(ignition);
UNUSED(usb_enum);
UNUSED(heartbeat_seen);
}

View File

@ -43,7 +43,6 @@ void white_set_led(uint8_t color, bool enabled) {
}
void white_set_usb_power_mode(uint8_t mode){
bool valid_mode = true;
switch (mode) {
case USB_POWER_CLIENT:
// B2,A13: set client mode
@ -61,14 +60,9 @@ void white_set_usb_power_mode(uint8_t mode){
set_gpio_output(GPIOA, 13, 0);
break;
default:
valid_mode = false;
puts("Invalid usb power mode\n");
break;
}
if (valid_mode) {
usb_power_mode = mode;
}
}
void white_set_gps_mode(uint8_t mode) {
@ -243,6 +237,7 @@ const harness_configuration white_harness_config = {
const board board_white = {
.board_type = "White",
.board_tick = unused_board_tick,
.harness_config = &white_harness_config,
.has_gps = false,
.has_hw_gmlan = true,
@ -254,10 +249,8 @@ const board board_white = {
.enable_can_transceiver = white_enable_can_transceiver,
.enable_can_transceivers = white_enable_can_transceivers,
.set_led = white_set_led,
.set_usb_power_mode = white_set_usb_power_mode,
.set_gps_mode = white_set_gps_mode,
.set_can_mode = white_set_can_mode,
.usb_power_mode_tick = unused_usb_power_mode_tick,
.check_ignition = white_check_ignition,
.read_current = white_read_current,
.set_fan_enabled = unused_set_fan_enabled,

View File

@ -1,5 +1,7 @@
// When changing this struct, python/__init__.py needs to be kept up to date!
#define HEALTH_PACKET_VERSION 8
#define HEALTH_PACKET_VERSION 9
struct __attribute__((packed)) health_t {
uint32_t uptime_pkt;
uint32_t voltage_pkt;
@ -14,7 +16,6 @@ struct __attribute__((packed)) health_t {
uint8_t controls_allowed_pkt;
uint8_t gas_interceptor_detected_pkt;
uint8_t car_harness_status_pkt;
uint8_t usb_power_mode_pkt;
uint8_t safety_mode_pkt;
uint16_t safety_param_pkt;
uint8_t fault_status_pkt;

View File

@ -51,20 +51,6 @@ void debug_ring_callback(uart_ring *ring) {
if (rcv == 'x') {
NVIC_SystemReset();
}
// enable CDP mode
if (rcv == 'C') {
puts("switching USB to CDP mode\n");
current_board->set_usb_power_mode(USB_POWER_CDP);
}
if (rcv == 'c') {
puts("switching USB to client mode\n");
current_board->set_usb_power_mode(USB_POWER_CLIENT);
}
if (rcv == 'D') {
puts("switching USB to DCP mode\n");
current_board->set_usb_power_mode(USB_POWER_DCP);
}
}
}
@ -158,15 +144,13 @@ void tick_handler(void) {
// siren
current_board->set_siren((loop_counter & 1U) && (siren_enabled || (siren_countdown > 0U)));
// tick drivers
// tick drivers at 8Hz
fan_tick();
// decimated to 1Hz
if (loop_counter == 0U) {
can_live = pending_can_live;
current_board->usb_power_mode_tick(uptime_cnt);
//puth(usart1_dma); puts(" "); puth(DMA2_Stream5->M0AR); puts(" "); puth(DMA2_Stream5->NDTR); puts("\n");
// reset this every 16th pass
@ -188,6 +172,10 @@ void tick_handler(void) {
// unless we are in power saving mode
current_board->set_led(LED_BLUE, (uptime_cnt & 1U) && (power_save_status == POWER_SAVE_STATUS_ENABLED));
// tick drivers at 1Hz
const bool recent_heartbeat = heartbeat_counter == 0U;
current_board->board_tick(check_started(), usb_enumerated, recent_heartbeat);
// increase heartbeat counter and cap it at the uint32 limit
if (heartbeat_counter < __UINT32_MAX__) {
heartbeat_counter += 1U;
@ -254,11 +242,6 @@ void tick_handler(void) {
fan_set_power(0U);
}
}
// enter CDP mode when car starts to ensure we are charging a turned off EON
if (check_started() && ((usb_power_mode != USB_POWER_CDP) || !usb_enumerated)) {
current_board->set_usb_power_mode(USB_POWER_CDP);
}
}
// check registers
@ -289,7 +272,6 @@ void EXTI_IRQ_Handler(void) {
exti_irq_clear();
clock_init();
current_board->set_usb_power_mode(USB_POWER_CDP);
set_power_save_state(POWER_SAVE_STATUS_DISABLED);
deepsleep_allowed = false;
heartbeat_counter = 0U;
@ -399,19 +381,17 @@ int main(void) {
for (cnt=0;;cnt++) {
if (power_save_status == POWER_SAVE_STATUS_DISABLED) {
#ifdef DEBUG_FAULTS
if(fault_status == FAULT_STATUS_NONE){
if (fault_status == FAULT_STATUS_NONE) {
#endif
uint32_t div_mode = ((usb_power_mode == USB_POWER_DCP) ? 4U : 1U);
// useful for debugging, fade breaks = panda is overloaded
for(uint32_t fade = 0U; fade < MAX_LED_FADE; fade += div_mode){
for (uint32_t fade = 0U; fade < MAX_LED_FADE; fade += 1U) {
current_board->set_led(LED_RED, true);
delay(fade >> 4);
current_board->set_led(LED_RED, false);
delay((MAX_LED_FADE - fade) >> 4);
}
for(uint32_t fade = MAX_LED_FADE; fade > 0U; fade -= div_mode){
for (uint32_t fade = MAX_LED_FADE; fade > 0U; fade -= 1U) {
current_board->set_led(LED_RED, true);
delay(fade >> 4);
current_board->set_led(LED_RED, false);
@ -430,7 +410,6 @@ int main(void) {
if (deepsleep_allowed && !usb_enumerated && !check_started() && ignition_seen && (heartbeat_counter > 20U)) {
usb_soft_disconnect(true);
fan_set_power(0U);
current_board->set_usb_power_mode(USB_POWER_CLIENT);
NVIC_DisableIRQ(TICK_TIMER_IRQ);
delay(512000U);

View File

@ -25,7 +25,6 @@ int get_health_pkt(void *dat) {
health->can_fwd_errs_pkt = can_fwd_errs;
health->gmlan_send_errs_pkt = gmlan_send_errs;
health->car_harness_status_pkt = car_harness_status;
health->usb_power_mode_pkt = usb_power_mode;
health->safety_mode_pkt = (uint8_t)(current_safety_mode);
health->safety_param_pkt = current_safety_param;
health->alternative_experience_pkt = alternative_experience;
@ -459,10 +458,6 @@ int comms_control_handler(ControlPacket_t *req, uint8_t *resp) {
can_loopback = (req->param1 > 0U);
can_init_all();
break;
// **** 0xe6: set USB power
case 0xe6:
current_board->set_usb_power_mode(req->param1);
break;
// **** 0xe7: set power save state
case 0xe7:
set_power_save_state(req->param1);

View File

@ -174,8 +174,8 @@ class Panda:
HW_TYPE_RED_PANDA = b'\x07'
CAN_PACKET_VERSION = 2
HEALTH_PACKET_VERSION = 8
HEALTH_STRUCT = struct.Struct("<IIIIIIIIBBBBBBBHBBBHIfB")
HEALTH_PACKET_VERSION = 9
HEALTH_STRUCT = struct.Struct("<IIIIIIIIBBBBBBHBBBHIfB")
F2_DEVICES = (HW_TYPE_PEDAL, )
F4_DEVICES = (HW_TYPE_WHITE_PANDA, HW_TYPE_GREY_PANDA, HW_TYPE_BLACK_PANDA, HW_TYPE_UNO, HW_TYPE_DOS)
@ -448,16 +448,15 @@ class Panda:
"controls_allowed": a[10],
"gas_interceptor_detected": a[11],
"car_harness_status": a[12],
"usb_power_mode": a[13],
"safety_mode": a[14],
"safety_param": a[15],
"fault_status": a[16],
"power_save_enabled": a[17],
"heartbeat_lost": a[18],
"alternative_experience": a[19],
"blocked_msg_cnt": a[20],
"interrupt_load": a[21],
"fan_power": a[22],
"safety_mode": a[13],
"safety_param": a[14],
"fault_status": a[15],
"power_save_enabled": a[16],
"heartbeat_lost": a[17],
"alternative_experience": a[18],
"blocked_msg_cnt": a[19],
"interrupt_load": a[20],
"fan_power": a[21],
}
# ******************* control *******************

View File

@ -26,7 +26,7 @@ fi
printf "\nPANDA F4 CODE\n"
$CPPCHECK -DPANDA -DSTM32F4 -UPEDAL -DCAN3 -DUID_BASE \
--suppressions-list=suppressions.txt --suppress=*:*inc/* \
--suppressions-list=$DIR/suppressions.txt --suppress=*:*inc/* \
-I $PANDA_DIR/board/ --dump --enable=all --inline-suppr --force \
$PANDA_DIR/board/main.c 2>/tmp/misra/cppcheck_f4_output.txt
@ -39,7 +39,7 @@ misra_f4_output=$( cat /tmp/misra/misra_f4_output.txt | grep -v ": information:
printf "\nPANDA H7 CODE\n"
$CPPCHECK -DPANDA -DSTM32H7 -UPEDAL -DUID_BASE \
--suppressions-list=suppressions.txt --suppress=*:*inc/* \
--suppressions-list=$DIR/suppressions.txt --suppress=*:*inc/* \
-I $PANDA_DIR/board/ --dump --enable=all --inline-suppr --force \
$PANDA_DIR/board/main.c 2>/tmp/misra/cppcheck_h7_output.txt
@ -52,7 +52,7 @@ misra_h7_output=$( cat /tmp/misra/misra_h7_output.txt | grep -v ": information:
printf "\nPEDAL CODE\n"
$CPPCHECK -UPANDA -DSTM32F2 -DPEDAL -UCAN3 \
--suppressions-list=suppressions.txt --suppress=*:*inc/* \
--suppressions-list=$DIR/suppressions.txt --suppress=*:*inc/* \
-I $PANDA_DIR/board/ --dump --enable=all --inline-suppr --force \
$PANDA_DIR/board/pedal/main.c 2>/tmp/misra/cppcheck_pedal_output.txt