Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor and cleanup board init #2094

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 9 additions & 64 deletions board/boards/black.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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) {
Expand All @@ -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 = {
Expand All @@ -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,
Expand Down
2 changes: 0 additions & 2 deletions board/boards/board_declarations.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand Down
8 changes: 2 additions & 6 deletions board/boards/cuatro.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
77 changes: 9 additions & 68 deletions board/boards/dos.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);

Expand All @@ -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();
}
Expand All @@ -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 = {
Expand All @@ -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,
Expand Down
1 change: 0 additions & 1 deletion board/boards/grey.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
79 changes: 9 additions & 70 deletions board/boards/red.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
Expand All @@ -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 = {
Expand All @@ -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 = {
Expand All @@ -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,
Expand Down
Loading
Loading