Skip to content

Commit

Permalink
refactor out red chiplet
Browse files Browse the repository at this point in the history
  • Loading branch information
robbederks committed Nov 29, 2024
1 parent c7cc2de commit e7abbf2
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 161 deletions.
31 changes: 28 additions & 3 deletions board/boards/cuatro.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
151 changes: 0 additions & 151 deletions board/boards/red_chiplet.h

This file was deleted.

124 changes: 118 additions & 6 deletions board/boards/tres.h
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand All @@ -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);
}
Expand All @@ -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);
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
1 change: 0 additions & 1 deletion board/stm32h7/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down

0 comments on commit e7abbf2

Please sign in to comment.