diff --git a/board/boards/black.h b/board/boards/black.h index 133c11fb2d..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(); @@ -102,41 +69,12 @@ static void black_init(void) { set_gpio_alternate(GPIOA, 8, GPIO_AF11_CAN3); set_gpio_alternate(GPIOA, 15, GPIO_AF11_CAN3); - // C0: OBD_SBU1 (orientation detection) - // C3: OBD_SBU2 (orientation detection) - set_gpio_mode(GPIOC, 0, MODE_ANALOG); - set_gpio_mode(GPIOC, 3, MODE_ANALOG); - // GPS OFF set_gpio_output(GPIOC, 5, 0); set_gpio_output(GPIOC, 12, 0); - // C10: OBD_SBU1_RELAY (harness relay driving output) - // C11: OBD_SBU2_RELAY (harness relay driving output) - set_gpio_mode(GPIOC, 10, MODE_OUTPUT); - set_gpio_mode(GPIOC, 11, MODE_OUTPUT); - set_gpio_output_type(GPIOC, 10, OUTPUT_TYPE_OPEN_DRAIN); - set_gpio_output_type(GPIOC, 11, OUTPUT_TYPE_OPEN_DRAIN); - set_gpio_output(GPIOC, 10, 1); - set_gpio_output(GPIOC, 11, 1); - // Turn on USB load switch. black_set_usb_load_switch(true); - - // Initialize harness - harness_init(); - - - // Enable CAN transceivers - black_enable_can_transceivers(true); - - // Disable LEDs - black_set_led(LED_RED, false); - black_set_led(LED_GREEN, false); - black_set_led(LED_BLUE, false); - - // Set normal CAN mode - black_set_can_mode(CAN_MODE_NORMAL); } static void black_init_bootloader(void) { @@ -156,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 = { @@ -175,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 90d0627517..e514ea295b 100644 --- a/board/boards/cuatro.h +++ b/board/boards/cuatro.h @@ -80,7 +80,7 @@ static void cuatro_set_amp_enabled(bool enabled){ } static void cuatro_init(void) { - red_chiplet_init(); + common_init_gpio(); // init LEDs as open drain set_gpio_output_type(GPIOE, 2, OUTPUT_TYPE_OPEN_DRAIN); @@ -118,9 +118,6 @@ static void cuatro_init(void) { gpio_uart7_init(); uart_init(&uart_ring_som_debug, 115200); - // SPI init - gpio_spi_init(); - // fan setup set_gpio_alternate(GPIOC, 8, GPIO_AF2_TIM3); register_set_bits(&(GPIOC->OTYPER), GPIO_OTYPER_OT8); // open drain @@ -150,7 +147,7 @@ static void cuatro_init(void) { } board board_cuatro = { - .harness_config = &red_chiplet_harness_config, + .harness_config = &tres_harness_config, .has_obd = true, .has_spi = true, .has_canfd = true, @@ -164,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 = red_chiplet_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 d96cee4ca4..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(); @@ -117,25 +85,6 @@ static void dos_init(void) { set_gpio_alternate(GPIOA, 8, GPIO_AF11_CAN3); set_gpio_alternate(GPIOA, 15, GPIO_AF11_CAN3); - // C0: OBD_SBU1 (orientation detection) - // C3: OBD_SBU2 (orientation detection) - set_gpio_mode(GPIOC, 0, MODE_ANALOG); - set_gpio_mode(GPIOC, 3, MODE_ANALOG); - - // C10: OBD_SBU1_RELAY (harness relay driving output) - // C11: OBD_SBU2_RELAY (harness relay driving output) - set_gpio_mode(GPIOC, 10, MODE_OUTPUT); - set_gpio_mode(GPIOC, 11, MODE_OUTPUT); - set_gpio_output_type(GPIOC, 10, OUTPUT_TYPE_OPEN_DRAIN); - set_gpio_output_type(GPIOC, 11, OUTPUT_TYPE_OPEN_DRAIN); - set_gpio_output(GPIOC, 10, 1); - set_gpio_output(GPIOC, 11, 1); - -#ifdef ENABLE_SPI - // SPI init - gpio_spi_init(); -#endif - // C8: FAN PWM aka TIM3_CH3 set_gpio_alternate(GPIOC, 8, GPIO_AF2_TIM3); @@ -148,24 +97,9 @@ static void dos_init(void) { pwm_init(TIM4, 2); dos_set_ir_power(0U); - // Initialize harness - harness_init(); - - - // Enable CAN transceivers - dos_enable_can_transceivers(true); - - // Disable LEDs - dos_set_led(LED_RED, false); - 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); - // Init clock source (camera strobe) using PWM clock_source_init(); } @@ -181,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 = { @@ -203,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 f1d8d9c44b..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(); @@ -107,17 +65,6 @@ static uint32_t red_read_voltage_mV(void){ static void red_init(void) { common_init_gpio(); - //C10,C11 : OBD_SBU1_RELAY, OBD_SBU2_RELAY - set_gpio_output_type(GPIOC, 10, OUTPUT_TYPE_OPEN_DRAIN); - set_gpio_pullup(GPIOC, 10, PULL_NONE); - set_gpio_mode(GPIOC, 10, MODE_OUTPUT); - set_gpio_output(GPIOC, 10, 1); - - set_gpio_output_type(GPIOC, 11, OUTPUT_TYPE_OPEN_DRAIN); - set_gpio_pullup(GPIOC, 11, PULL_NONE); - set_gpio_mode(GPIOC, 11, MODE_OUTPUT); - set_gpio_output(GPIOC, 11, 1); - // G11,B3,D7,B4: transceiver enable set_gpio_pullup(GPIOG, 11, PULL_NONE); set_gpio_mode(GPIOG, 11, MODE_OUTPUT); @@ -140,21 +87,6 @@ static void red_init(void) { set_gpio_pullup(GPIOB, 14, PULL_UP); set_gpio_mode(GPIOB, 14, MODE_OUTPUT); set_gpio_output(GPIOB, 14, 1); - - // Initialize harness - harness_init(); - - - // Enable CAN transceivers - red_enable_can_transceivers(true); - - // Disable LEDs - red_set_led(LED_RED, false); - red_set_led(LED_GREEN, false); - red_set_led(LED_BLUE, false); - - // Set normal CAN mode - red_set_can_mode(CAN_MODE_NORMAL); } static harness_configuration red_harness_config = { @@ -168,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 = { @@ -187,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/red_chiplet.h b/board/boards/red_chiplet.h deleted file mode 100644 index e5d1aa097f..0000000000 --- a/board/boards/red_chiplet.h +++ /dev/null @@ -1,151 +0,0 @@ -#pragma once - -#include "board_declarations.h" - -// ///////////////////////////////////// // -// Red Panda chiplet (STM32H7) + Harness // -// ///////////////////////////////////// // - -// Most hardware functionality is similar to red panda - -static void red_chiplet_enable_can_transceiver(uint8_t transceiver, bool enabled) { - switch (transceiver) { - case 1U: - set_gpio_output(GPIOG, 11, !enabled); - break; - case 2U: - set_gpio_output(GPIOB, 10, !enabled); - break; - case 3U: - set_gpio_output(GPIOD, 7, !enabled); - break; - case 4U: - set_gpio_output(GPIOB, 11, !enabled); - break; - default: - break; - } -} - -static void red_chiplet_enable_can_transceivers(bool enabled) { - uint8_t main_bus = (harness.status == HARNESS_STATUS_FLIPPED) ? 3U : 1U; - for (uint8_t i=1U; i<=4U; i++) { - // Leave main CAN always on for CAN-based ignition detection - if (i == main_bus) { - red_chiplet_enable_can_transceiver(i, true); - } else { - red_chiplet_enable_can_transceiver(i, enabled); - } - } -} - -static void red_chiplet_set_can_mode(uint8_t mode) { - red_chiplet_enable_can_transceiver(2U, false); - red_chiplet_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_chiplet_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_chiplet_enable_can_transceiver(4U, true); - } - break; - default: - break; - } -} - -static void red_chiplet_set_fan_or_usb_load_switch(bool enabled) { - set_gpio_output(GPIOD, 3, enabled); -} - -static void red_chiplet_init(void) { - common_init_gpio(); - - // A8, A3: OBD_SBU1_RELAY, OBD_SBU2_RELAY - set_gpio_output_type(GPIOA, 8, OUTPUT_TYPE_OPEN_DRAIN); - set_gpio_pullup(GPIOA, 8, PULL_NONE); - set_gpio_output(GPIOA, 8, 1); - set_gpio_mode(GPIOA, 8, MODE_OUTPUT); - - set_gpio_output_type(GPIOA, 3, OUTPUT_TYPE_OPEN_DRAIN); - set_gpio_pullup(GPIOA, 3, PULL_NONE); - set_gpio_output(GPIOA, 3, 1); - set_gpio_mode(GPIOA, 3, MODE_OUTPUT); - - // G11,B10,D7,B11: transceiver enable - set_gpio_pullup(GPIOG, 11, PULL_NONE); - set_gpio_mode(GPIOG, 11, MODE_OUTPUT); - - set_gpio_pullup(GPIOB, 10, PULL_NONE); - set_gpio_mode(GPIOB, 10, MODE_OUTPUT); - - set_gpio_pullup(GPIOD, 7, PULL_NONE); - set_gpio_mode(GPIOD, 7, MODE_OUTPUT); - - set_gpio_pullup(GPIOB, 11, PULL_NONE); - set_gpio_mode(GPIOB, 11, MODE_OUTPUT); - - // D3: usb load switch - set_gpio_pullup(GPIOD, 3, PULL_NONE); - set_gpio_mode(GPIOD, 3, MODE_OUTPUT); - - // B0: 5VOUT_S - set_gpio_pullup(GPIOB, 0, PULL_NONE); - set_gpio_mode(GPIOB, 0, MODE_ANALOG); - - // Initialize harness - harness_init(); - - - // Enable CAN transceivers - red_chiplet_enable_can_transceivers(true); - - // Disable LEDs - red_set_led(LED_RED, false); - red_set_led(LED_GREEN, false); - red_set_led(LED_BLUE, false); - - // Set normal CAN mode - red_chiplet_set_can_mode(CAN_MODE_NORMAL); -} - -static harness_configuration red_chiplet_harness_config = { - .has_harness = true, - .GPIO_SBU1 = GPIOC, - .GPIO_SBU2 = GPIOA, - .GPIO_relay_SBU1 = GPIOA, - .GPIO_relay_SBU2 = GPIOA, - .pin_SBU1 = 4, - .pin_SBU2 = 1, - .pin_relay_SBU1 = 8, - .pin_relay_SBU2 = 3, - .adc_channel_SBU1 = 4, // ADC12_INP4 - .adc_channel_SBU2 = 17 // ADC1_INP17 -}; diff --git a/board/boards/tres.h b/board/boards/tres.h index f0602ae60b..3919b8e52c 100644 --- a/board/boards/tres.h +++ b/board/boards/tres.h @@ -9,7 +9,7 @@ static bool tres_ir_enabled; static bool tres_fan_enabled; static void tres_update_fan_ir_power(void) { - red_chiplet_set_fan_or_usb_load_switch(tres_ir_enabled || tres_fan_enabled); + set_gpio_output(GPIOD, 3, tres_ir_enabled || tres_fan_enabled); } static void tres_set_ir_power(uint8_t percentage){ @@ -29,6 +29,37 @@ static void tres_set_fan_enabled(bool enabled) { tres_update_fan_ir_power(); } +static void tres_enable_can_transceiver(uint8_t transceiver, bool enabled) { + switch (transceiver) { + case 1U: + set_gpio_output(GPIOG, 11, !enabled); + break; + case 2U: + set_gpio_output(GPIOB, 10, !enabled); + break; + case 3U: + set_gpio_output(GPIOD, 7, !enabled); + break; + case 4U: + set_gpio_output(GPIOB, 11, !enabled); + break; + default: + break; + } +} + +static void tres_enable_can_transceivers(bool enabled) { + uint8_t main_bus = (harness.status == HARNESS_STATUS_FLIPPED) ? 3U : 1U; + for (uint8_t i=1U; i<=4U; i++) { + // Leave main CAN always on for CAN-based ignition detection + if (i == main_bus) { + tres_enable_can_transceiver(i, true); + } else { + tres_enable_can_transceiver(i, enabled); + } + } +} + static bool tres_read_som_gpio (void) { return (get_gpio_input(GPIOC, 2) != 0); } @@ -39,7 +70,7 @@ static void tres_init(void) { register_set_bits(&(PWR->CR3), PWR_CR3_USB33DEN); while ((PWR->CR3 & PWR_CR3_USB33RDY) == 0U); - red_chiplet_init(); + common_init_gpio(); // C2: SOM GPIO used as input (fan control at boot) set_gpio_mode(GPIOC, 2, MODE_INPUT); @@ -54,9 +85,6 @@ static void tres_init(void) { gpio_uart7_init(); uart_init(&uart_ring_som_debug, 115200); - // SPI init - gpio_spi_init(); - // fan setup set_gpio_alternate(GPIOC, 8, GPIO_AF2_TIM3); @@ -74,8 +102,30 @@ static void tres_init(void) { clock_source_init(); } +static harness_configuration tres_harness_config = { + .has_harness = true, + .GPIO_SBU1 = GPIOC, + .GPIO_SBU2 = GPIOA, + .GPIO_relay_SBU1 = GPIOA, + .GPIO_relay_SBU2 = GPIOA, + .pin_SBU1 = 4, + .pin_SBU2 = 1, + .pin_relay_SBU1 = 8, + .pin_relay_SBU2 = 3, + .adc_channel_SBU1 = 4, // ADC12_INP4 + .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 = { - .harness_config = &red_chiplet_harness_config, + .harness_config = &tres_harness_config, .has_obd = true, .has_spi = true, .has_canfd = true, @@ -86,10 +136,9 @@ board board_tres = { .fan_enable_cooldown_time = 3U, .init = tres_init, .init_bootloader = unused_init_bootloader, - .enable_can_transceiver = red_chiplet_enable_can_transceiver, - .enable_can_transceivers = red_chiplet_enable_can_transceivers, + .enable_can_transceiver = tres_enable_can_transceiver, + .enable_can_transceivers = tres_enable_can_transceivers, .set_led = red_set_led, - .set_can_mode = red_chiplet_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 d1728f1f9f..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(); @@ -118,25 +86,11 @@ static void uno_init(void) { set_gpio_alternate(GPIOA, 8, GPIO_AF11_CAN3); set_gpio_alternate(GPIOA, 15, GPIO_AF11_CAN3); - // C0: OBD_SBU1 (orientation detection) - // C3: OBD_SBU2 (orientation detection) - set_gpio_mode(GPIOC, 0, MODE_ANALOG); - set_gpio_mode(GPIOC, 3, MODE_ANALOG); - // GPS off set_gpio_output(GPIOB, 1, 0); set_gpio_output(GPIOC, 5, 0); set_gpio_output(GPIOC, 12, 0); - // C10: OBD_SBU1_RELAY (harness relay driving output) - // C11: OBD_SBU2_RELAY (harness relay driving output) - set_gpio_mode(GPIOC, 10, MODE_OUTPUT); - set_gpio_mode(GPIOC, 11, MODE_OUTPUT); - set_gpio_output_type(GPIOC, 10, OUTPUT_TYPE_OPEN_DRAIN); - set_gpio_output_type(GPIOC, 11, OUTPUT_TYPE_OPEN_DRAIN); - set_gpio_output(GPIOC, 10, 1); - set_gpio_output(GPIOC, 11, 1); - // C8: FAN PWM aka TIM3_CH3 set_gpio_alternate(GPIOC, 8, GPIO_AF2_TIM3); @@ -148,21 +102,6 @@ static void uno_init(void) { pwm_init(TIM4, 2); uno_set_ir_power(0U); - // Initialize harness - harness_init(); - - - // Enable CAN transceivers - uno_enable_can_transceivers(true); - - // Disable LEDs - uno_set_led(LED_RED, false); - uno_set_led(LED_GREEN, false); - uno_set_led(LED_BLUE, false); - - // Set normal CAN mode - uno_set_can_mode(CAN_MODE_NORMAL); - // Switch to phone usb mode if harness connection is powered by less than 7V if(white_read_voltage_mV() < 7000U){ uno_set_usb_switch(true); @@ -192,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 = { @@ -210,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 c4a2559353..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,17 +132,21 @@ 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); - // Enable CAN transceivers - white_enable_can_transceivers(true); + // B3,B4: disable GMLAN mode + set_gpio_mode(GPIOB, 3, MODE_INPUT); + set_gpio_mode(GPIOB, 4, MODE_INPUT); - // Disable LEDs - white_set_led(LED_RED, false); - white_set_led(LED_GREEN, false); - white_set_led(LED_BLUE, false); + // B5,B6: normal CAN2 mode + set_gpio_alternate(GPIOB, 5, GPIO_AF9_CAN2); + set_gpio_alternate(GPIOB, 6, GPIO_AF9_CAN2); - // Set normal CAN mode - white_set_can_mode(CAN_MODE_NORMAL); + // 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. @@ -204,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 cfb6965c03..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,14 @@ 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); + set_gpio_output(current_board->harness_config->GPIO_relay_SBU1, current_board->harness_config->pin_relay_SBU1, 1); + set_gpio_output(current_board->harness_config->GPIO_relay_SBU2, current_board->harness_config->pin_relay_SBU2, 1); + // try to detect orientation harness.status = harness_detect_orientation(); if (harness.status != HARNESS_STATUS_NC) { 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 aaea95d089..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,6 +322,9 @@ int main(void) { // init board current_board->init(); + if (current_board->harness_config->has_harness) { + harness_init(); + } // panda has an FPU, let's use it! enable_fpu(); @@ -354,12 +357,14 @@ int main(void) { #ifdef ENABLE_SPI if (current_board->has_spi) { + gpio_spi_init(); spi_init(); } #endif current_board->set_led(LED_RED, false); current_board->set_led(LED_GREEN, false); + current_board->set_led(LED_BLUE, false); print("**** INTERRUPTS ON ****\n"); enable_interrupts(); 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/board.h b/board/stm32h7/board.h index ea4812245e..143e431c74 100644 --- a/board/stm32h7/board.h +++ b/board/stm32h7/board.h @@ -15,7 +15,6 @@ #include "stm32h7/sound.h" #include "drivers/clock_source.h" #include "boards/red.h" -#include "boards/red_chiplet.h" #include "boards/tres.h" #include "boards/cuatro.h" diff --git a/board/stm32h7/peripherals.h b/board/stm32h7/peripherals.h index c6874b8ac3..222f3c0dc3 100644 --- a/board/stm32h7/peripherals.h +++ b/board/stm32h7/peripherals.h @@ -44,13 +44,6 @@ void common_init_gpio(void) { set_gpio_mode(GPIOE, 4, MODE_OUTPUT); set_gpio_output_type(GPIOE, 4, OUTPUT_TYPE_OPEN_DRAIN); - //C4,A1: OBD_SBU1, OBD_SBU2 - set_gpio_pullup(GPIOC, 4, PULL_NONE); - set_gpio_mode(GPIOC, 4, MODE_ANALOG); - - set_gpio_pullup(GPIOA, 1, PULL_NONE); - set_gpio_mode(GPIOA, 1, MODE_ANALOG); - //F11: VOLT_S set_gpio_pullup(GPIOF, 11, PULL_NONE); set_gpio_mode(GPIOF, 11, MODE_ANALOG); 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