From c3b0612e71b54ee6c16b3f75a39a8e045117e7a9 Mon Sep 17 00:00:00 2001 From: Robbe Derks Date: Mon, 2 Dec 2024 21:37:38 -0800 Subject: [PATCH] global set_can_mode --- board/boards/black.h | 44 +++++------------------ board/boards/board_declarations.h | 2 -- board/boards/cuatro.h | 1 - board/boards/dos.h | 43 +++++----------------- board/boards/grey.h | 1 - board/boards/red.h | 53 +++++----------------------- board/boards/tres.h | 53 +++++----------------------- board/boards/uno.h | 43 +++++----------------- board/boards/white.h | 37 +++++++++---------- board/drivers/harness.h | 33 +++++++++++++++++ board/drivers/harness_declarations.h | 12 +++++++ board/main.c | 11 +++--- board/main_comms.h | 4 +-- board/stm32f4/stm32f4_config.h | 1 + board/stm32h7/stm32h7_config.h | 1 + 15 files changed, 115 insertions(+), 224 deletions(-) diff --git a/board/boards/black.h b/board/boards/black.h index fd23ed4897..3403a0353f 100644 --- a/board/boards/black.h +++ b/board/boards/black.h @@ -57,39 +57,6 @@ static void black_set_usb_load_switch(bool enabled) { set_gpio_output(GPIOB, 1, !enabled); } -static void black_set_can_mode(uint8_t mode) { - black_enable_can_transceiver(2U, false); - black_enable_can_transceiver(4U, false); - switch (mode) { - case CAN_MODE_NORMAL: - case CAN_MODE_OBD_CAN2: - if ((bool)(mode == CAN_MODE_NORMAL) != (bool)(harness.status == HARNESS_STATUS_FLIPPED)) { - // B12,B13: disable OBD mode - set_gpio_mode(GPIOB, 12, MODE_INPUT); - set_gpio_mode(GPIOB, 13, MODE_INPUT); - - // B5,B6: normal CAN2 mode - set_gpio_alternate(GPIOB, 5, GPIO_AF9_CAN2); - set_gpio_alternate(GPIOB, 6, GPIO_AF9_CAN2); - black_enable_can_transceiver(2U, true); - - } else { - // B5,B6: disable normal CAN2 mode - set_gpio_mode(GPIOB, 5, MODE_INPUT); - set_gpio_mode(GPIOB, 6, MODE_INPUT); - - // B12,B13: OBD mode - set_gpio_alternate(GPIOB, 12, GPIO_AF9_CAN2); - set_gpio_alternate(GPIOB, 13, GPIO_AF9_CAN2); - black_enable_can_transceiver(4U, true); - } - break; - default: - print("Tried to set unsupported CAN mode: "); puth(mode); print("\n"); - break; - } -} - static bool black_check_ignition(void){ // ignition is checked through harness return harness_check_ignition(); @@ -127,7 +94,15 @@ static harness_configuration black_harness_config = { .pin_relay_SBU1 = 10, .pin_relay_SBU2 = 11, .adc_channel_SBU1 = 10, - .adc_channel_SBU2 = 13 + .adc_channel_SBU2 = 13, + .GPIO_CAN2_RX_NORMAL = GPIOB, + .GPIO_CAN2_TX_NORMAL = GPIOB, + .GPIO_CAN2_RX_FLIPPED = GPIOB, + .GPIO_CAN2_TX_FLIPPED = GPIOB, + .pin_CAN2_RX_NORMAL = 12, + .pin_CAN2_TX_NORMAL = 13, + .pin_CAN2_RX_FLIPPED = 5, + .pin_CAN2_TX_FLIPPED = 6 }; board board_black = { @@ -146,7 +121,6 @@ board board_black = { .enable_can_transceiver = black_enable_can_transceiver, .enable_can_transceivers = black_enable_can_transceivers, .set_led = black_set_led, - .set_can_mode = black_set_can_mode, .check_ignition = black_check_ignition, .read_voltage_mV = white_read_voltage_mV, .read_current_mA = unused_read_current, diff --git a/board/boards/board_declarations.h b/board/boards/board_declarations.h index 61e9ce2d8e..99a1f21d64 100644 --- a/board/boards/board_declarations.h +++ b/board/boards/board_declarations.h @@ -15,7 +15,6 @@ typedef void (*board_init_bootloader)(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_can_mode)(uint8_t mode); typedef bool (*board_check_ignition)(void); typedef uint32_t (*board_read_voltage_mV)(void); typedef uint32_t (*board_read_current_mA)(void); @@ -41,7 +40,6 @@ struct board { board_enable_can_transceiver enable_can_transceiver; board_enable_can_transceivers enable_can_transceivers; board_set_led set_led; - board_set_can_mode set_can_mode; board_check_ignition check_ignition; board_read_voltage_mV read_voltage_mV; board_read_current_mA read_current_mA; diff --git a/board/boards/cuatro.h b/board/boards/cuatro.h index cda262df0e..e514ea295b 100644 --- a/board/boards/cuatro.h +++ b/board/boards/cuatro.h @@ -161,7 +161,6 @@ board board_cuatro = { .enable_can_transceiver = cuatro_enable_can_transceiver, .enable_can_transceivers = cuatro_enable_can_transceivers, .set_led = cuatro_set_led, - .set_can_mode = tres_set_can_mode, .check_ignition = red_check_ignition, .read_voltage_mV = cuatro_read_voltage_mV, .read_current_mA = cuatro_read_current_mA, diff --git a/board/boards/dos.h b/board/boards/dos.h index 12ac363ac9..bda207aec1 100644 --- a/board/boards/dos.h +++ b/board/boards/dos.h @@ -57,38 +57,6 @@ static void dos_set_bootkick(BootState state) { set_gpio_output(GPIOC, 4, state != BOOT_BOOTKICK); } -static void dos_set_can_mode(uint8_t mode) { - dos_enable_can_transceiver(2U, false); - dos_enable_can_transceiver(4U, false); - switch (mode) { - case CAN_MODE_NORMAL: - case CAN_MODE_OBD_CAN2: - if ((bool)(mode == CAN_MODE_NORMAL) != (bool)(harness.status == HARNESS_STATUS_FLIPPED)) { - // B12,B13: disable OBD mode - set_gpio_mode(GPIOB, 12, MODE_INPUT); - set_gpio_mode(GPIOB, 13, MODE_INPUT); - - // B5,B6: normal CAN2 mode - set_gpio_alternate(GPIOB, 5, GPIO_AF9_CAN2); - set_gpio_alternate(GPIOB, 6, GPIO_AF9_CAN2); - dos_enable_can_transceiver(2U, true); - } else { - // B5,B6: disable normal CAN2 mode - set_gpio_mode(GPIOB, 5, MODE_INPUT); - set_gpio_mode(GPIOB, 6, MODE_INPUT); - - // B12,B13: OBD mode - set_gpio_alternate(GPIOB, 12, GPIO_AF9_CAN2); - set_gpio_alternate(GPIOB, 13, GPIO_AF9_CAN2); - dos_enable_can_transceiver(4U, true); - } - break; - default: - print("Tried to set unsupported CAN mode: "); puth(mode); print("\n"); - break; - } -} - static bool dos_check_ignition(void){ // ignition is checked through harness return harness_check_ignition(); @@ -147,7 +115,15 @@ static harness_configuration dos_harness_config = { .pin_relay_SBU1 = 10, .pin_relay_SBU2 = 11, .adc_channel_SBU1 = 10, - .adc_channel_SBU2 = 13 + .adc_channel_SBU2 = 13, + .GPIO_CAN2_RX_NORMAL = GPIOB, + .GPIO_CAN2_TX_NORMAL = GPIOB, + .GPIO_CAN2_RX_FLIPPED = GPIOB, + .GPIO_CAN2_TX_FLIPPED = GPIOB, + .pin_CAN2_RX_NORMAL = 12, + .pin_CAN2_TX_NORMAL = 13, + .pin_CAN2_RX_FLIPPED = 5, + .pin_CAN2_TX_FLIPPED = 6 }; board board_dos = { @@ -169,7 +145,6 @@ board board_dos = { .enable_can_transceiver = dos_enable_can_transceiver, .enable_can_transceivers = dos_enable_can_transceivers, .set_led = dos_set_led, - .set_can_mode = dos_set_can_mode, .check_ignition = dos_check_ignition, .read_voltage_mV = white_read_voltage_mV, .read_current_mA = unused_read_current, diff --git a/board/boards/grey.h b/board/boards/grey.h index b3e40bc6fe..cfd98481ab 100644 --- a/board/boards/grey.h +++ b/board/boards/grey.h @@ -24,7 +24,6 @@ board board_grey = { .enable_can_transceiver = white_enable_can_transceiver, .enable_can_transceivers = white_enable_can_transceivers, .set_led = white_set_led, - .set_can_mode = white_set_can_mode, .check_ignition = white_check_ignition, .read_voltage_mV = white_read_voltage_mV, .read_current_mA = white_read_current_mA, diff --git a/board/boards/red.h b/board/boards/red.h index 1893442a31..a6b04fe6b4 100644 --- a/board/boards/red.h +++ b/board/boards/red.h @@ -53,48 +53,6 @@ static void red_set_led(uint8_t color, bool enabled) { } } -static void red_set_can_mode(uint8_t mode) { - red_enable_can_transceiver(2U, false); - red_enable_can_transceiver(4U, false); - switch (mode) { - case CAN_MODE_NORMAL: - case CAN_MODE_OBD_CAN2: - if ((bool)(mode == CAN_MODE_NORMAL) != (bool)(harness.status == HARNESS_STATUS_FLIPPED)) { - // B12,B13: disable normal mode - set_gpio_pullup(GPIOB, 12, PULL_NONE); - set_gpio_mode(GPIOB, 12, MODE_ANALOG); - - set_gpio_pullup(GPIOB, 13, PULL_NONE); - set_gpio_mode(GPIOB, 13, MODE_ANALOG); - - // B5,B6: FDCAN2 mode - set_gpio_pullup(GPIOB, 5, PULL_NONE); - set_gpio_alternate(GPIOB, 5, GPIO_AF9_FDCAN2); - - set_gpio_pullup(GPIOB, 6, PULL_NONE); - set_gpio_alternate(GPIOB, 6, GPIO_AF9_FDCAN2); - red_enable_can_transceiver(2U, true); - } else { - // B5,B6: disable normal mode - set_gpio_pullup(GPIOB, 5, PULL_NONE); - set_gpio_mode(GPIOB, 5, MODE_ANALOG); - - set_gpio_pullup(GPIOB, 6, PULL_NONE); - set_gpio_mode(GPIOB, 6, MODE_ANALOG); - // B12,B13: FDCAN2 mode - set_gpio_pullup(GPIOB, 12, PULL_NONE); - set_gpio_alternate(GPIOB, 12, GPIO_AF9_FDCAN2); - - set_gpio_pullup(GPIOB, 13, PULL_NONE); - set_gpio_alternate(GPIOB, 13, GPIO_AF9_FDCAN2); - red_enable_can_transceiver(4U, true); - } - break; - default: - break; - } -} - static bool red_check_ignition(void) { // ignition is checked through harness return harness_check_ignition(); @@ -142,7 +100,15 @@ static harness_configuration red_harness_config = { .pin_relay_SBU1 = 10, .pin_relay_SBU2 = 11, .adc_channel_SBU1 = 4, //ADC12_INP4 - .adc_channel_SBU2 = 17 //ADC1_INP17 + .adc_channel_SBU2 = 17, //ADC1_INP17 + .GPIO_CAN2_RX_NORMAL = GPIOB, + .GPIO_CAN2_TX_NORMAL = GPIOB, + .GPIO_CAN2_RX_FLIPPED = GPIOB, + .GPIO_CAN2_TX_FLIPPED = GPIOB, + .pin_CAN2_RX_NORMAL = 12, + .pin_CAN2_TX_NORMAL = 13, + .pin_CAN2_RX_FLIPPED = 5, + .pin_CAN2_TX_FLIPPED = 6 }; board board_red = { @@ -161,7 +127,6 @@ board board_red = { .enable_can_transceiver = red_enable_can_transceiver, .enable_can_transceivers = red_enable_can_transceivers, .set_led = red_set_led, - .set_can_mode = red_set_can_mode, .check_ignition = red_check_ignition, .read_voltage_mV = red_read_voltage_mV, .read_current_mA = unused_read_current, diff --git a/board/boards/tres.h b/board/boards/tres.h index ab26e4c564..3919b8e52c 100644 --- a/board/boards/tres.h +++ b/board/boards/tres.h @@ -60,48 +60,6 @@ static void tres_enable_can_transceivers(bool enabled) { } } -static void tres_set_can_mode(uint8_t mode) { - current_board->enable_can_transceiver(2U, false); - current_board->enable_can_transceiver(4U, false); - switch (mode) { - case CAN_MODE_NORMAL: - case CAN_MODE_OBD_CAN2: - if ((bool)(mode == CAN_MODE_NORMAL) != (bool)(harness.status == HARNESS_STATUS_FLIPPED)) { - // B12,B13: disable normal mode - set_gpio_pullup(GPIOB, 12, PULL_NONE); - set_gpio_mode(GPIOB, 12, MODE_ANALOG); - - set_gpio_pullup(GPIOB, 13, PULL_NONE); - set_gpio_mode(GPIOB, 13, MODE_ANALOG); - - // B5,B6: FDCAN2 mode - set_gpio_pullup(GPIOB, 5, PULL_NONE); - set_gpio_alternate(GPIOB, 5, GPIO_AF9_FDCAN2); - - set_gpio_pullup(GPIOB, 6, PULL_NONE); - set_gpio_alternate(GPIOB, 6, GPIO_AF9_FDCAN2); - current_board->enable_can_transceiver(2U, true); - } else { - // B5,B6: disable normal mode - set_gpio_pullup(GPIOB, 5, PULL_NONE); - set_gpio_mode(GPIOB, 5, MODE_ANALOG); - - set_gpio_pullup(GPIOB, 6, PULL_NONE); - set_gpio_mode(GPIOB, 6, MODE_ANALOG); - // B12,B13: FDCAN2 mode - set_gpio_pullup(GPIOB, 12, PULL_NONE); - set_gpio_alternate(GPIOB, 12, GPIO_AF9_FDCAN2); - - set_gpio_pullup(GPIOB, 13, PULL_NONE); - set_gpio_alternate(GPIOB, 13, GPIO_AF9_FDCAN2); - current_board->enable_can_transceiver(4U, true); - } - break; - default: - break; - } -} - static bool tres_read_som_gpio (void) { return (get_gpio_input(GPIOC, 2) != 0); } @@ -155,7 +113,15 @@ static harness_configuration tres_harness_config = { .pin_relay_SBU1 = 8, .pin_relay_SBU2 = 3, .adc_channel_SBU1 = 4, // ADC12_INP4 - .adc_channel_SBU2 = 17 // ADC1_INP17 + .adc_channel_SBU2 = 17, // ADC1_INP17 + .GPIO_CAN2_RX_NORMAL = GPIOB, + .GPIO_CAN2_TX_NORMAL = GPIOB, + .GPIO_CAN2_RX_FLIPPED = GPIOB, + .GPIO_CAN2_TX_FLIPPED = GPIOB, + .pin_CAN2_RX_NORMAL = 12, + .pin_CAN2_TX_NORMAL = 13, + .pin_CAN2_RX_FLIPPED = 5, + .pin_CAN2_TX_FLIPPED = 6 }; board board_tres = { @@ -173,7 +139,6 @@ board board_tres = { .enable_can_transceiver = tres_enable_can_transceiver, .enable_can_transceivers = tres_enable_can_transceivers, .set_led = red_set_led, - .set_can_mode = tres_set_can_mode, .check_ignition = red_check_ignition, .read_voltage_mV = red_read_voltage_mV, .read_current_mA = unused_read_current, diff --git a/board/boards/uno.h b/board/boards/uno.h index da9a2529a3..d033db7648 100644 --- a/board/boards/uno.h +++ b/board/boards/uno.h @@ -62,38 +62,6 @@ static void uno_set_bootkick(BootState state) { } } -static void uno_set_can_mode(uint8_t mode) { - uno_enable_can_transceiver(2U, false); - uno_enable_can_transceiver(4U, false); - switch (mode) { - case CAN_MODE_NORMAL: - case CAN_MODE_OBD_CAN2: - if ((bool)(mode == CAN_MODE_NORMAL) != (bool)(harness.status == HARNESS_STATUS_FLIPPED)) { - // B12,B13: disable OBD mode - set_gpio_mode(GPIOB, 12, MODE_INPUT); - set_gpio_mode(GPIOB, 13, MODE_INPUT); - - // B5,B6: normal CAN2 mode - set_gpio_alternate(GPIOB, 5, GPIO_AF9_CAN2); - set_gpio_alternate(GPIOB, 6, GPIO_AF9_CAN2); - uno_enable_can_transceiver(2U, true); - } else { - // B5,B6: disable normal CAN2 mode - set_gpio_mode(GPIOB, 5, MODE_INPUT); - set_gpio_mode(GPIOB, 6, MODE_INPUT); - - // B12,B13: OBD mode - set_gpio_alternate(GPIOB, 12, GPIO_AF9_CAN2); - set_gpio_alternate(GPIOB, 13, GPIO_AF9_CAN2); - uno_enable_can_transceiver(4U, true); - } - break; - default: - print("Tried to set unsupported CAN mode: "); puth(mode); print("\n"); - break; - } -} - static bool uno_check_ignition(void){ // ignition is checked through harness return harness_check_ignition(); @@ -163,7 +131,15 @@ static harness_configuration uno_harness_config = { .pin_relay_SBU1 = 10, .pin_relay_SBU2 = 11, .adc_channel_SBU1 = 10, - .adc_channel_SBU2 = 13 + .adc_channel_SBU2 = 13, + .GPIO_CAN2_RX_NORMAL = GPIOB, + .GPIO_CAN2_TX_NORMAL = GPIOB, + .GPIO_CAN2_RX_FLIPPED = GPIOB, + .GPIO_CAN2_TX_FLIPPED = GPIOB, + .pin_CAN2_RX_NORMAL = 12, + .pin_CAN2_TX_NORMAL = 13, + .pin_CAN2_RX_FLIPPED = 5, + .pin_CAN2_TX_FLIPPED = 6 }; board board_uno = { @@ -181,7 +157,6 @@ board board_uno = { .enable_can_transceiver = uno_enable_can_transceiver, .enable_can_transceivers = uno_enable_can_transceivers, .set_led = uno_set_led, - .set_can_mode = uno_set_can_mode, .check_ignition = uno_check_ignition, .read_voltage_mV = white_read_voltage_mV, .read_current_mA = unused_read_current, diff --git a/board/boards/white.h b/board/boards/white.h index 44bbf832f4..795d8ad946 100644 --- a/board/boards/white.h +++ b/board/boards/white.h @@ -69,26 +69,6 @@ static void white_set_usb_power_mode(uint8_t mode){ } } -static void white_set_can_mode(uint8_t mode){ - if (mode == CAN_MODE_NORMAL) { - // B12,B13: disable GMLAN mode - set_gpio_mode(GPIOB, 12, MODE_INPUT); - set_gpio_mode(GPIOB, 13, MODE_INPUT); - - // B3,B4: disable GMLAN mode - set_gpio_mode(GPIOB, 3, MODE_INPUT); - set_gpio_mode(GPIOB, 4, MODE_INPUT); - - // B5,B6: normal CAN2 mode - set_gpio_alternate(GPIOB, 5, GPIO_AF9_CAN2); - set_gpio_alternate(GPIOB, 6, GPIO_AF9_CAN2); - - // A8,A15: normal CAN3 mode - set_gpio_alternate(GPIOA, 8, GPIO_AF11_CAN3); - set_gpio_alternate(GPIOA, 15, GPIO_AF11_CAN3); - } -} - static uint32_t white_read_voltage_mV(void){ return adc_get_mV(12) * 11U; } @@ -152,6 +132,22 @@ static void white_grey_init(void) { set_gpio_alternate(GPIOC, 11, GPIO_AF7_USART3); set_gpio_pullup(GPIOC, 11, PULL_UP); + // B12,B13: disable GMLAN mode + set_gpio_mode(GPIOB, 12, MODE_INPUT); + set_gpio_mode(GPIOB, 13, MODE_INPUT); + + // B3,B4: disable GMLAN mode + set_gpio_mode(GPIOB, 3, MODE_INPUT); + set_gpio_mode(GPIOB, 4, MODE_INPUT); + + // B5,B6: normal CAN2 mode + set_gpio_alternate(GPIOB, 5, GPIO_AF9_CAN2); + set_gpio_alternate(GPIOB, 6, GPIO_AF9_CAN2); + + // A8,A15: normal CAN3 mode + set_gpio_alternate(GPIOA, 8, GPIO_AF11_CAN3); + set_gpio_alternate(GPIOA, 15, GPIO_AF11_CAN3); + // Init usb power mode // Init in CDP mode only if panda is powered by 12V. // Otherwise a PC would not be able to flash a standalone panda @@ -192,7 +188,6 @@ board board_white = { .enable_can_transceiver = white_enable_can_transceiver, .enable_can_transceivers = white_enable_can_transceivers, .set_led = white_set_led, - .set_can_mode = white_set_can_mode, .check_ignition = white_check_ignition, .read_voltage_mV = white_read_voltage_mV, .read_current_mA = white_read_current_mA, diff --git a/board/drivers/harness.h b/board/drivers/harness.h index efe319d0d3..44c54da738 100644 --- a/board/drivers/harness.h +++ b/board/drivers/harness.h @@ -32,6 +32,37 @@ void set_intercept_relay(bool intercept, bool ignition_relay) { } } +void set_can_mode(uint8_t mode) { + current_board->enable_can_transceiver(2U, false); + current_board->enable_can_transceiver(4U, false); + switch (mode) { + case CAN_MODE_NORMAL: + case CAN_MODE_OBD_CAN2: + if ((bool)(mode == CAN_MODE_NORMAL) != (bool)(harness.status == HARNESS_STATUS_FLIPPED)) { + // normal pins + set_gpio_mode(current_board->harness_config->GPIO_CAN2_RX_NORMAL, current_board->harness_config->pin_CAN2_RX_NORMAL, MODE_ANALOG); + set_gpio_mode(current_board->harness_config->GPIO_CAN2_TX_NORMAL, current_board->harness_config->pin_CAN2_TX_NORMAL, MODE_ANALOG); + + // flipped pins + set_gpio_mode(current_board->harness_config->GPIO_CAN2_RX_FLIPPED, current_board->harness_config->pin_CAN2_RX_FLIPPED, GPIO_CAN2_AF); + set_gpio_mode(current_board->harness_config->GPIO_CAN2_TX_FLIPPED, current_board->harness_config->pin_CAN2_TX_FLIPPED, GPIO_CAN2_AF); + current_board->enable_can_transceiver(2U, true); + } else { + // normal pins + set_gpio_mode(current_board->harness_config->GPIO_CAN2_RX_FLIPPED, current_board->harness_config->pin_CAN2_RX_FLIPPED, MODE_ANALOG); + set_gpio_mode(current_board->harness_config->GPIO_CAN2_TX_FLIPPED, current_board->harness_config->pin_CAN2_TX_FLIPPED, MODE_ANALOG); + + // flipped pins + set_gpio_mode(current_board->harness_config->GPIO_CAN2_RX_NORMAL, current_board->harness_config->pin_CAN2_RX_NORMAL, GPIO_CAN2_AF); + set_gpio_mode(current_board->harness_config->GPIO_CAN2_TX_NORMAL, current_board->harness_config->pin_CAN2_TX_NORMAL, GPIO_CAN2_AF); + current_board->enable_can_transceiver(4U, true); + } + break; + default: + break; + } +} + bool harness_check_ignition(void) { bool ret = false; @@ -94,6 +125,8 @@ void harness_tick(void) { } void harness_init(void) { + set_can_mode(CAN_MODE_NORMAL); + // init OBD_SBUx_RELAY set_gpio_output_type(current_board->harness_config->GPIO_relay_SBU1, current_board->harness_config->pin_relay_SBU1, OUTPUT_TYPE_OPEN_DRAIN); set_gpio_output_type(current_board->harness_config->GPIO_relay_SBU2, current_board->harness_config->pin_relay_SBU2, OUTPUT_TYPE_OPEN_DRAIN); diff --git a/board/drivers/harness_declarations.h b/board/drivers/harness_declarations.h index 953941f621..0f2828a2a3 100644 --- a/board/drivers/harness_declarations.h +++ b/board/drivers/harness_declarations.h @@ -15,6 +15,8 @@ extern struct harness_t harness; struct harness_configuration { const bool has_harness; + + // harness control GPIO_TypeDef * const GPIO_SBU1; GPIO_TypeDef * const GPIO_SBU2; GPIO_TypeDef * const GPIO_relay_SBU1; @@ -25,6 +27,16 @@ struct harness_configuration { const uint8_t pin_relay_SBU2; const uint8_t adc_channel_SBU1; const uint8_t adc_channel_SBU2; + + // CAN control + GPIO_TypeDef * const GPIO_CAN2_RX_NORMAL; + GPIO_TypeDef * const GPIO_CAN2_TX_NORMAL; + GPIO_TypeDef * const GPIO_CAN2_RX_FLIPPED; + GPIO_TypeDef * const GPIO_CAN2_TX_FLIPPED; + const uint8_t pin_CAN2_RX_NORMAL; + const uint8_t pin_CAN2_TX_NORMAL; + const uint8_t pin_CAN2_RX_FLIPPED; + const uint8_t pin_CAN2_TX_FLIPPED; }; // The ignition relay is only used for testing purposes diff --git a/board/main.c b/board/main.c index 1c86ebe30f..edfa36c07f 100644 --- a/board/main.c +++ b/board/main.c @@ -77,14 +77,14 @@ void set_safety_mode(uint16_t mode, uint16_t param) { case SAFETY_SILENT: set_intercept_relay(false, false); if (current_board->has_obd) { - current_board->set_can_mode(CAN_MODE_NORMAL); + set_can_mode(CAN_MODE_NORMAL); } can_silent = ALL_CAN_SILENT; break; case SAFETY_NOOUTPUT: set_intercept_relay(false, false); if (current_board->has_obd) { - current_board->set_can_mode(CAN_MODE_NORMAL); + set_can_mode(CAN_MODE_NORMAL); } can_silent = ALL_CAN_LIVE; break; @@ -97,9 +97,9 @@ void set_safety_mode(uint16_t mode, uint16_t param) { // 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); + set_can_mode(CAN_MODE_OBD_CAN2); } else { - current_board->set_can_mode(CAN_MODE_NORMAL); + set_can_mode(CAN_MODE_NORMAL); } } can_silent = ALL_CAN_LIVE; @@ -109,7 +109,7 @@ void set_safety_mode(uint16_t mode, uint16_t param) { heartbeat_counter = 0U; heartbeat_lost = false; if (current_board->has_obd) { - current_board->set_can_mode(CAN_MODE_NORMAL); + set_can_mode(CAN_MODE_NORMAL); } can_silent = ALL_CAN_LIVE; break; @@ -322,7 +322,6 @@ int main(void) { // init board current_board->init(); - current_board->set_can_mode(CAN_MODE_NORMAL); if (current_board->harness_config->has_harness) { harness_init(); } diff --git a/board/main_comms.h b/board/main_comms.h index e366b19a09..88b0af65bf 100644 --- a/board/main_comms.h +++ b/board/main_comms.h @@ -215,10 +215,10 @@ int comms_control_handler(ControlPacket_t *req, uint8_t *resp) { if (current_board->has_obd) { if (req->param1 == 1U) { // Enable OBD CAN - current_board->set_can_mode(CAN_MODE_OBD_CAN2); + set_can_mode(CAN_MODE_OBD_CAN2); } else { // Disable OBD CAN - current_board->set_can_mode(CAN_MODE_NORMAL); + set_can_mode(CAN_MODE_NORMAL); } } break; diff --git a/board/stm32f4/stm32f4_config.h b/board/stm32f4/stm32f4_config.h index eae9222a9d..726e8198b2 100644 --- a/board/stm32f4/stm32f4_config.h +++ b/board/stm32f4/stm32f4_config.h @@ -12,6 +12,7 @@ // Around (1Mbps / 8 bits/byte / 12 bytes per message) #define CAN_INTERRUPT_RATE 12000U +#define GPIO_CAN2_AF GPIO_AF9_CAN2 #define MAX_LED_FADE 8192U diff --git a/board/stm32h7/stm32h7_config.h b/board/stm32h7/stm32h7_config.h index bfc12e8c3a..e60b8da34f 100644 --- a/board/stm32h7/stm32h7_config.h +++ b/board/stm32h7/stm32h7_config.h @@ -24,6 +24,7 @@ separate IRQs for RX and TX. 1 / ((29 bits / 0.5Mbps) + (23 bits / 2Mbps)) = 14388Hz */ #define CAN_INTERRUPT_RATE 16000U +#define GPIO_CAN2_AF GPIO_AF9_FDCAN2 #define MAX_LED_FADE 10240U