diff --git a/board/boards/cuatro.h b/board/boards/cuatro.h index 90d0627517..ea463c3b24 100644 --- a/board/boards/cuatro.h +++ b/board/boards/cuatro.h @@ -80,7 +80,32 @@ static void cuatro_set_amp_enabled(bool enabled){ } static void cuatro_init(void) { - red_chiplet_init(); + 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); + + // Initialize harness + harness_init(); + + // Enable CAN transceivers + cuatro_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 + tres_set_can_mode(CAN_MODE_NORMAL); // init LEDs as open drain set_gpio_output_type(GPIOE, 2, OUTPUT_TYPE_OPEN_DRAIN); @@ -150,7 +175,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 +189,7 @@ 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, + .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/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..0de0beb94c 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,79 @@ 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 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); } @@ -39,7 +112,32 @@ 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(); + + // 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); + + // Initialize harness + harness_init(); + + // Enable CAN transceivers + tres_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 + tres_set_can_mode(CAN_MODE_NORMAL); // C2: SOM GPIO used as input (fan control at boot) set_gpio_mode(GPIOC, 2, MODE_INPUT); @@ -74,8 +172,22 @@ 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 +}; + 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 +198,10 @@ 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, + .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/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"