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

Pd rev2 fixes #258

Merged
merged 9 commits into from
Feb 3, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 2 additions & 0 deletions libraries/codegen/boards/pedal.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
watchdog: 0
centre_console:
watchdog: 0
power_distribution:
watchdog: 0
signals:
throttle_output:
length: 32
Expand Down
4 changes: 2 additions & 2 deletions libraries/ms-common/inc/fsm.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
#include "status.h"
#include "tasks.h"

#define FSM_PRIORITY 2
#define FSM_PRIORITY 1
// TODO(mitchellostler): make user defined
#define FSM_TASK_STACK TASK_STACK_1024
#define FSM_TIMEOUT_MS 1000
Expand Down Expand Up @@ -110,7 +110,7 @@ typedef struct Fsm {
Fsm *name##_fsm = &((Fsm){ \
.num_states = num_fsm_states, \
}); \
TASK(name, TASK_STACK_512) { \
TASK(name, TASK_STACK_256) { \
_fsm_task(context); \
}

Expand Down
1 change: 1 addition & 0 deletions projects/power_distribution/inc/outputs.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ typedef enum {
OUTPUT_GROUP_LIGHTS_LEFT_TURN,
OUTPUT_GROUP_LIGHTS_RIGHT_TURN,
OUTPUT_GROUP_LIGHTS_HAZARD,
OUTPUT_GROUP_BRAKE,
// Power States
OUTPUT_GROUP_POWER_OFF,
OUTPUT_GROUP_POWER_ON,
Expand Down
96 changes: 47 additions & 49 deletions projects/power_distribution/inc/pin_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,31 @@

// PCA9555 and I2C definitions
#define PD_I2C_PORT I2C_PORT_1
#define PD_PCA9555_I2C_ADDRESS_0 0x24
#define PD_PCA9555_I2C_ADDRESS_1 0x26
#define PD_PCA9555_I2C_ADDRESS_0 0x20
#define PD_PCA9555_I2C_ADDRESS_1 0x21
#define PD_I2C_SDA \
{ GPIO_PORT_B, 9 }
#define PD_I2C_SCL \
{ GPIO_PORT_B, 8 }
#define PCA9555_IO_INT1 \
{ GPIO_PORT_B, 14 }
{ GPIO_PORT_A, 9 }
#define PCA9555_IO_INT2 \
{ GPIO_PORT_B, 13 }
{ GPIO_PORT_A, 10 }

// Power, fan, and mux pin definitions
#define PD_5V_REG_MONITOR_PIN \
{ GPIO_PORT_B, 1 }

#define PD_MUX_OUTPUT_PIN \
{ GPIO_PORT_A, 7 }
#define PD_MUX_SEL1_PIN \
{ GPIO_PORT_A, 6 }
#define PD_MUX_SEL1_PIN \
{ GPIO_PORT_B, 15 }
#define PD_MUX_SEL2_PIN \
{ GPIO_PORT_A, 5 }
{ GPIO_PORT_B, 14 }
#define PD_MUX_SEL3_PIN \
{ GPIO_PORT_A, 4 }
{ GPIO_PORT_B, 13 }
#define PD_MUX_SEL4_PIN \
{ GPIO_PORT_A, 3 }
{ GPIO_PORT_B, 12 }

#define PD_SMBALERT_PIN \
{ GPIO_PORT_B, 10 }
Expand All @@ -41,94 +41,94 @@
{ GPIO_PORT_A, 8 }

#define SOLAR_SENSE_1_EN \
{ PD_PCA9555_I2C_ADDRESS_0, PCA9555_PIN_IO0_0 }
{ PD_PCA9555_I2C_ADDRESS_1, PCA9555_PIN_IO0_5 }

#define SOLAR_SENSE_2_EN \
{ PD_PCA9555_I2C_ADDRESS_1, PCA9555_PIN_IO0_1 }
{ PD_PCA9555_I2C_ADDRESS_1, PCA9555_PIN_IO0_7 }

#define SOLAR_SENSE_1_2_DSEL \
{ PD_PCA9555_I2C_ADDRESS_0, PCA9555_PIN_IO0_2 }
{ PD_PCA9555_I2C_ADDRESS_1, PCA9555_PIN_IO0_6 }

#define PEDAL_EN \
{ PD_PCA9555_I2C_ADDRESS_0, PCA9555_PIN_IO0_3 }
{ PD_PCA9555_I2C_ADDRESS_1, PCA9555_PIN_IO0_2 }

#define STEERING_EN \
{ PD_PCA9555_I2C_ADDRESS_0, PCA9555_PIN_IO0_4 }
{ PD_PCA9555_I2C_ADDRESS_1, PCA9555_PIN_IO0_4 }

#define PEDAL_STEERING_DSEL \
{ PD_PCA9555_I2C_ADDRESS_0, PCA9555_PIN_IO0_5 }
{ PD_PCA9555_I2C_ADDRESS_1, PCA9555_PIN_IO0_3 }

#define LEFT_TURN_EN \
{ PD_PCA9555_I2C_ADDRESS_0, PCA9555_PIN_IO0_6 }
{ PD_PCA9555_I2C_ADDRESS_1, PCA9555_PIN_IO1_0 }

#define RIGHT_TURN_EN \
{ PD_PCA9555_I2C_ADDRESS_0, PCA9555_PIN_IO0_7 }
{ PD_PCA9555_I2C_ADDRESS_1, PCA9555_PIN_IO0_1 }

#define LEFT_RIGHT_TURN_DSEL \
{ PD_PCA9555_I2C_ADDRESS_0, PCA9555_PIN_IO1_0 }
{ PD_PCA9555_I2C_ADDRESS_1, PCA9555_PIN_IO0_0 }

#define DRL_LIGHT_EN \
{ PD_PCA9555_I2C_ADDRESS_0, PCA9555_PIN_IO1_1 }
{ PD_PCA9555_I2C_ADDRESS_1, PCA9555_PIN_IO1_4 }

#define BRAKE_LIGHT_EN \
{ PD_PCA9555_I2C_ADDRESS_0, PCA9555_PIN_IO1_2 }
{ PD_PCA9555_I2C_ADDRESS_1, PCA9555_PIN_IO1_6 }

#define DRL_BRAKE_LIGHT_DSEL \
{ PD_PCA9555_I2C_ADDRESS_0, PCA9555_PIN_IO1_3 }
{ PD_PCA9555_I2C_ADDRESS_1, PCA9555_PIN_IO1_5 }

#define CENTER_CONSOLE_EN \
{ PD_PCA9555_I2C_ADDRESS_0, PCA9555_PIN_IO1_4 }
{ PD_PCA9555_I2C_ADDRESS_1, PCA9555_PIN_IO1_1 }

#define BMS_DCDC_EN \
{ PD_PCA9555_I2C_ADDRESS_0, PCA9555_PIN_IO1_5 }
{ PD_PCA9555_I2C_ADDRESS_1, PCA9555_PIN_IO1_3 }

#define CENTER_CONSOLE_BMS_DCDC_DSEL \
{ PD_PCA9555_I2C_ADDRESS_0, PCA9555_PIN_IO1_6 }
{ PD_PCA9555_I2C_ADDRESS_1, PCA9555_PIN_IO1_2 }

#define MCI_EN \
{ PD_PCA9555_I2C_ADDRESS_0, PCA9555_PIN_IO0_0 }
{ PD_PCA9555_I2C_ADDRESS_0, PCA9555_PIN_IO1_4 }

#define BPS_LIGHT_EN \
{ PD_PCA9555_I2C_ADDRESS_0, PCA9555_PIN_IO0_1 }
{ PD_PCA9555_I2C_ADDRESS_0, PCA9555_PIN_IO1_6 }

#define MCI_BPS_LIGHT_DSEL \
{ PD_PCA9555_I2C_ADDRESS_0, PCA9555_PIN_IO0_2 }
{ PD_PCA9555_I2C_ADDRESS_0, PCA9555_PIN_IO1_5 }

#define SPARE_12V_1_EN \
{ PD_PCA9555_I2C_ADDRESS_0, PCA9555_PIN_IO0_3 }
{ PD_PCA9555_I2C_ADDRESS_0, PCA9555_PIN_IO1_3 }

#define SPARE_12V_2_EN \
{ PD_PCA9555_I2C_ADDRESS_0, PCA9555_PIN_IO0_4 }
{ PD_PCA9555_I2C_ADDRESS_0, PCA9555_PIN_IO1_1 }

#define SPARE_12V_1_2_DSEL \
{ PD_PCA9555_I2C_ADDRESS_0, PCA9555_PIN_IO0_5 }
{ PD_PCA9555_I2C_ADDRESS_0, PCA9555_PIN_IO1_2 }

#define BMS_AUX_EN \
{ PD_PCA9555_I2C_ADDRESS_0, PCA9555_PIN_IO0_6 }
{ PD_PCA9555_I2C_ADDRESS_0, PCA9555_PIN_IO0_0 }

#define DRIVER_FAN_EN \
{ PD_PCA9555_I2C_ADDRESS_0, PCA9555_PIN_IO0_7 }
{ PD_PCA9555_I2C_ADDRESS_0, PCA9555_PIN_IO0_2 }

#define BMS_AUX_DRIVER_FAN_DSEL \
{ PD_PCA9555_I2C_ADDRESS_0, PCA9555_PIN_IO1_0 }
{ PD_PCA9555_I2C_ADDRESS_0, PCA9555_PIN_IO0_1 }

#define REAR_CAM_LCD_EN \
{ PD_PCA9555_I2C_ADDRESS_0, PCA9555_PIN_IO1_1 }
{ PD_PCA9555_I2C_ADDRESS_0, PCA9555_PIN_IO0_6 }

#define SPARE_5V_DCDC_EN \
{ PD_PCA9555_I2C_ADDRESS_0, PCA9555_PIN_IO1_2 }
{ PD_PCA9555_I2C_ADDRESS_0, PCA9555_PIN_IO1_0 }

#define REAR_CAM_LCD_SPARE_5V_DCDC_DSEL \
{ PD_PCA9555_I2C_ADDRESS_0, PCA9555_PIN_IO1_3 }
{ PD_PCA9555_I2C_ADDRESS_0, PCA9555_PIN_IO0_7 }

#define TELEMETRY_EN \
{ PD_PCA9555_I2C_ADDRESS_0, PCA9555_PIN_IO1_4 }
{ PD_PCA9555_I2C_ADDRESS_0, PCA9555_PIN_IO0_3 }

#define SPARE_5V_AUX_EN \
{ PD_PCA9555_I2C_ADDRESS_0, PCA9555_PIN_IO1_5 }
{ PD_PCA9555_I2C_ADDRESS_0, PCA9555_PIN_IO0_5 }

#define TELEMETRY_SPARE_5V_AUX_DSEL \
{ PD_PCA9555_I2C_ADDRESS_0, PCA9555_PIN_IO1_6 }
{ PD_PCA9555_I2C_ADDRESS_0, PCA9555_PIN_IO0_4 }

// Select Pins

Expand All @@ -154,13 +154,11 @@

// Fault pin addresses
// GPIO
#define AUX_FAULT_GPIO_1 \
{ .port = GPIO_PORT_B, .pin = 3 }
#define AUX_FAULT_GPIO_2 \
{ .port = GPIO_PORT_B, .pin = 4 }
#define DCDC_FAULT_GPIO_1 \
{ .port = GPIO_PORT_A, .pin = 15 }
#define DCDC_FAULT_GPIO_2 \
{ .port = GPIO_PORT_B, .pin = 5 }
#define DCDC_FAULT_GPIO_3 \
{ .port = GPIO_PORT_A, .pin = 6 }
#define AUX_VALID1 \
{ .port = GPIO_PORT_B, .pin = 10 }
#define AUX_VALID2 \
{ .port = GPIO_PORT_B, .pin = 2 }
#define DCDC_VALID1 \
{ .port = GPIO_PORT_B, .pin = 1 }
#define DCDC_VALID2 \
{ .port = GPIO_PORT_B, .pin = 0 }
4 changes: 2 additions & 2 deletions projects/power_distribution/inc/power_seq_fsm.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
#include "task.h"

// TODO: figure out actual values for timeout
#define BMS_RESPONSE_TIMEOUT_MS 10000
#define MCI_RESPONSE_TIMEOUT_MS 12000
#define BMS_RESPONSE_TIMEOUT_MS 3000
#define MCI_RESPONSE_TIMEOUT_MS 300

#define NUM_POWER_STATES 6

Expand Down
10 changes: 10 additions & 0 deletions projects/power_distribution/src/lights_fsm.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
#include "outputs.h"
#include "power_distribution_getters.h"

#define brake_lights() \
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a huge fan of this syntax. I think it would be better to just make it a short prv_ function

if (get_pedal_output_brake_output()) \
pd_set_output_group(OUTPUT_GROUP_BRAKE, OUTPUT_STATE_ON); \
else \
pd_set_output_group(OUTPUT_GROUP_BRAKE, OUTPUT_STATE_OFF);

// Placeholder GPIO Address, will be updated
GpioAddress RIGHT_LIGHT_ADDR = { .port = GPIO_PORT_B, .pin = 5 };
GpioAddress LEFT_LIGHT_ADDR = { .port = GPIO_PORT_A, .pin = 15 };
Expand Down Expand Up @@ -55,6 +61,7 @@ static void prv_lights_signal_blinker(SoftTimerId id) {
}

static void prv_init_state_input(Fsm *fsm, void *context) {
brake_lights();
// can transition to LEFT, RIGHT, HAZARD
EELightType light_event = get_steering_info_input_lights();
HazardStatus hazard_status = get_cc_power_control_hazard_enabled();
Expand All @@ -75,6 +82,7 @@ static void prv_init_state_output(void *context) {
}

static void prv_left_signal_input(Fsm *fsm, void *context) {
brake_lights();
// can transition to INIT, RIGHT, HAZARD
EELightType light_event = get_steering_info_input_lights();
HazardStatus hazard_status = get_cc_power_control_hazard_enabled();
Expand All @@ -99,6 +107,7 @@ static void prv_left_signal_output(void *context) {
}

static void prv_right_signal_input(Fsm *fsm, void *context) {
brake_lights();
// can transition to INIT, LEFT, HAZARD
EELightType light_event = get_steering_info_input_lights();
HazardStatus hazard_status = get_cc_power_control_hazard_enabled();
Expand All @@ -123,6 +132,7 @@ static void prv_right_signal_output(void *context) {
}

static void prv_hazard_input(Fsm *fsm, void *context) {
brake_lights();
// can transition to INIT, BPS_FAULT
EELightType light_event = get_steering_info_input_lights();
HazardStatus hazard_status = get_cc_power_control_hazard_enabled();
Expand Down
4 changes: 2 additions & 2 deletions projects/power_distribution/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ I2CSettings i2c_settings = {
};

void pre_loop_init() {
pca9555_gpio_init(I2C_PORT_1);
pd_output_init();
pd_sense_init();
adc_init();
Expand Down Expand Up @@ -58,9 +59,8 @@ int main() {
log_init();
interrupt_init();
gpio_init();
i2c_init(I2C_PORT_1, &i2c_settings);
pca9555_gpio_init(I2C_PORT_1);
can_init(&s_can_storage, &can_settings);
i2c_init(I2C_PORT_1, &i2c_settings);
// set_master_cycle_time(250); // Give it enough time to run an entire medium cycle
// set_medium_cycle_count(2); // adjust medium cycle count to run once per 500ms
init_power_seq();
Expand Down
7 changes: 7 additions & 0 deletions projects/power_distribution/src/output_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,8 @@ static OutputGroupDef s_output_group_hazards = {
.outputs = { LEFT_TURN, RIGHT_TURN },
};

static OutputGroupDef s_output_group_brake = { .num_outputs = 1, .outputs = { BRAKE_LIGHT } };

static OutputGroupDef s_output_group_power_off = { .num_outputs = 2,
.outputs = { CENTER_CONSOLE, BMS_DCDC } };

Expand All @@ -335,4 +337,9 @@ const OutputGroupDef *g_output_group_map[NUM_OUTPUT_GROUPS] = {
[OUTPUT_GROUP_LIGHTS_LEFT_TURN] = &s_output_group_left_signal,
[OUTPUT_GROUP_LIGHTS_RIGHT_TURN] = &s_output_group_right_signal,
[OUTPUT_GROUP_LIGHTS_HAZARD] = &s_output_group_hazards,
[OUTPUT_GROUP_BRAKE] = &s_output_group_brake,
[OUTPUT_GROUP_POWER_OFF] = &s_output_group_power_off,
[OUTPUT_GROUP_POWER_ON] = &s_output_group_power_on,
[OUTPUT_GROUP_POWER_DRIVE] = &s_output_group_power_drive,
[OUTPUT_GROUP_POWER_FAULT] = &s_output_group_power_fault,
};
34 changes: 15 additions & 19 deletions projects/power_distribution/src/pd_fault.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
#include "adc.h"
#include "exported_enums.h"
#include "gpio.h"
#include "log.h"
#include "pin_defs.h"
#include "power_distribution_setters.h"

static uint8_t s_fault_bitset = 0;

// Fault pin address definitions
static const GpioAddress aux_fault_gpio_1 = AUX_FAULT_GPIO_1;
static const GpioAddress aux_fault_gpio_2 = AUX_FAULT_GPIO_2;
static const GpioAddress dcdc_fault_gpio_1 = DCDC_FAULT_GPIO_1;
static const GpioAddress dcdc_fault_gpio_2 = DCDC_FAULT_GPIO_2;
static const GpioAddress dcdc_fault_gpio_3 = DCDC_FAULT_GPIO_3;
static const GpioAddress aux_valid1 = AUX_VALID1;
static const GpioAddress aux_valid2 = AUX_VALID2;
static const GpioAddress dcdc_valid1 = DCDC_VALID1;
static const GpioAddress dcdc_valid2 = DCDC_VALID2;

static void prv_set_fault_bit(uint8_t mask, bool condition) {
if (condition) {
Expand All @@ -24,27 +24,23 @@ static void prv_set_fault_bit(uint8_t mask, bool condition) {
}

static bool prv_check_aux_fault(void) {
GpioState aux_gpio_1_state;
GpioState aux_gpio_2_state;
if (!status_ok(gpio_get_state(&aux_fault_gpio_1, &aux_gpio_1_state)) ||
!status_ok(gpio_get_state(&aux_fault_gpio_2, &aux_gpio_2_state))) {
GpioState aux_valid1_state;
GpioState aux_valid2_state;
if (!status_ok(gpio_get_state(&aux_valid1, &aux_valid1_state)) ||
!status_ok(gpio_get_state(&aux_valid2, &aux_valid2_state))) {
return true;
}
return (aux_gpio_1_state == GPIO_STATE_LOW || aux_gpio_2_state == GPIO_STATE_LOW);
return (aux_valid1_state == GPIO_STATE_HIGH || aux_valid2_state == GPIO_STATE_HIGH);
}

static bool prv_check_dcdc_fault(void) {
GpioState dcdc_gpio_1_state;
GpioState dcdc_gpio_2_state;
GpioState dcdc_gpio_3_state;
if (!status_ok(gpio_get_state(&dcdc_fault_gpio_1, &dcdc_gpio_1_state)) ||
!status_ok(gpio_get_state(&dcdc_fault_gpio_2, &dcdc_gpio_2_state)) ||
!status_ok(gpio_get_state(&dcdc_fault_gpio_3, &dcdc_gpio_3_state))) {
GpioState dcdc_valid1_state;
GpioState dcdc_valid2_state;
if (!status_ok(gpio_get_state(&dcdc_valid1, &dcdc_valid1_state)) ||
!status_ok(gpio_get_state(&dcdc_valid2, &dcdc_valid2_state))) {
return true;
}

return (dcdc_gpio_1_state == GPIO_STATE_LOW || dcdc_gpio_2_state == GPIO_STATE_LOW ||
dcdc_gpio_3_state == GPIO_STATE_HIGH);
return (dcdc_valid1_state == GPIO_STATE_HIGH || dcdc_valid2_state == GPIO_STATE_HIGH);
}

uint8_t check_pd_fault(void) {
Expand Down
Loading
Loading