Skip to content

Commit

Permalink
FWXV-219: pd current sense broadcast (#208)
Browse files Browse the repository at this point in the history
* pd current sense functions

* minor fix

* minor change
  • Loading branch information
devAdhiraj authored Sep 28, 2023
1 parent bab1374 commit 87e5665
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 30 deletions.
17 changes: 17 additions & 0 deletions projects/power_distribution_front/inc/output_current_sense.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include <stdbool.h>
#include <stdint.h>

#include "bts_load_switch.h"
#include "gpio.h"
#include "i2c.h"
#include "outputs.h"
#include "pca9555_gpio_expander.h"
#include "pin_defs.h"

// initializes sense pin and mux sel pins, must be called before adc_init()
StatusCode pd_sense_init(void);

// reads adc value of all pins in output group and stores the result in reading_out attr
StatusCode pd_sense_output_group(OutputGroup group);
49 changes: 19 additions & 30 deletions projects/power_distribution_front/inc/pin_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,21 +69,25 @@
#define FRONT_PIN_FRONT_LEFT_RIGHT_TURN_LIGHT_DSEL \
{ PD_PCA9555_I2C_ADDRESS_1, PCA9555_PIN_IO1_1 }

#define FRONT_MUX_SEL_CENTRE_CONSOLE_REAR_DISPLAY 0
#define FRONT_MUX_SEL_MAIN_PI_DRIVER_DISPLAY 1
#define FRONT_MUX_SEL_5V_SPARES 2
#define FRONT_MUX_SEL_SPARE_4 3 // on MCI's BTS7040
#define FRONT_MUX_SEL_LEFT_RIGHT_DISPLAY 4
#define FRONT_MUX_SEL_SPEAKER 5
#define FRONT_MUX_SEL_PEDAL_STEERING 6
#define FRONT_MUX_SEL_SPARE_2_3 7
#define FRONT_MUX_SEL_DAYTIME_RUNNING_LIGHTS 8
#define FRONT_MUX_SEL_FRONT_LEFT_RIGHT_TURN_LIGHT 9
#define FRONT_MUX_SEL_SPARE_1 11
#define FRONT_MUX_SEL_LEFT_RIGHT_CAMERA 12
#define FRONT_MUX_SEL_INFOTAINMENT_DISPLAY 13 // aka main display
#define FRONT_MUX_SEL_SPARE_5_6 14 // on rear fan 1 & 2's BTS7200
#define FRONT_MUX_SEL_UV_VBAT 15
// Current Sense MUX
#define NUM_MUX_SEL_PINS 4
#define NUM_MUX_INPUTS (1 << NUM_MUX_SEL_PINS) // 2^4 = 16

#define MUX_SEL_CENTRE_CONSOLE_REAR_DISPLAY 0
#define MUX_SEL_MAIN_PI_DRIVER_DISPLAY 1
#define MUX_SEL_5V_SPARES 2
#define MUX_SEL_MCI 3
#define MUX_SEL_LEFT_RIGHT_DISPLAY_STROBE_LIGHTS 4
#define MUX_SEL_SPEAKER_SOLAR 5
#define MUX_SEL_PEDAL_STEERING 6
#define MUX_SEL_SPARE_2_3 7
#define MUX_SEL_DAYTIME_RUNNING_LIGHTS_BRAKE_LIGHTS 8
#define MUX_SEL_FRONT_REAR_LEFT_RIGHT_TURN_LIGHT 9
#define MUX_SEL_SPARE_1 11
#define MUX_SEL_LEFT_RIGHT_REAR_CAMERA 12
#define MUX_SEL_INFOTAINMENT_DISPLAY_BMS 13 // aka main display
#define MUX_SEL_FAN_1_2 14
#define MUX_SEL_UV_VBAT 15

// Rear power distribution
// Enable pins
Expand Down Expand Up @@ -114,18 +118,3 @@

#define REAR_PIN_FAN_1_2_DSEL \
{ PD_PCA9555_I2C_ADDRESS_1, PCA9555_PIN_IO0_6 }

#define REAR_MUX_SEL_SPARE_9_10 0
#define REAR_MUX_SEL_SPARE_7_8 1 // on main pi/driver display BTS7200
#define REAR_MUX_SEL_5V_SPARES 2
#define REAR_MUX_SEL_MCI 3
#define REAR_MUX_SEL_STROBE_LIGHT 4
#define REAR_MUX_SEL_SOLAR_SENSE 5
#define REAR_MUX_SEL_SPARE_4_5 6 // on steering/pedal BTS7200
#define REAR_MUX_SEL_SPARE_2_3 7
#define REAR_MUX_SEL_BRAKE_LIGHT 8
#define REAR_MUX_SEL_REAR_LEFT_RIGHT_TURN_LIGHT 9
#define REAR_MUX_SEL_SPARE_1 11
#define REAR_MUX_SEL_REAR_CAMERA_SPARE_6 12
#define REAR_MUX_SEL_BMS 13
#define REAR_MUX_SEL_FAN_1_2 14
12 changes: 12 additions & 0 deletions projects/power_distribution_front/src/output_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,81 +85,93 @@ const BtsLoadSwitchOutput g_output_config[NUM_OUTPUTS] = {
.select_state = { .select_state_pca9555 = PCA9555_GPIO_STATE_HIGH },
.sense_pin = &(GpioAddress)PD_MUX_OUTPUT_PIN, // NOLINT
.resistor = 0,
.mux_val = MUX_SEL_CENTRE_CONSOLE_REAR_DISPLAY,
},
[FRONT_OUTPUT_PEDAL] = {
.enable_pin = &s_front_pin_pedal_en,
.select_pin = &s_front_pin_pedal_steering_dsel,
.select_state = { .select_state_pca9555 = PCA9555_GPIO_STATE_HIGH },
.sense_pin = &(GpioAddress)PD_MUX_OUTPUT_PIN, // NOLINT
.resistor = 0,
.mux_val = MUX_SEL_PEDAL_STEERING,
},
[FRONT_OUTPUT_STEERING] = {
.enable_pin = &s_front_pin_steering_en,
.select_pin = &s_front_pin_pedal_steering_dsel,
.select_state = { .select_state_pca9555 = PCA9555_GPIO_STATE_HIGH },
.sense_pin = &(GpioAddress)PD_MUX_OUTPUT_PIN, // NOLINT
.resistor = 0,
.mux_val = MUX_SEL_PEDAL_STEERING,
},
[FRONT_OUTPUT_LEFT_FRONT_TURN_LIGHT] = {
.enable_pin = &s_front_pin_front_left_turn_light_en,
.select_pin = &s_rear_pin_rear_left_right_turn_light_dsel,
.select_state = { .select_state_pca9555 = PCA9555_GPIO_STATE_HIGH },
.sense_pin = &(GpioAddress)PD_MUX_OUTPUT_PIN, // NOLINT
.resistor = 0,
.mux_val = MUX_SEL_FRONT_REAR_LEFT_RIGHT_TURN_LIGHT,
},
[FRONT_OUTPUT_RIGHT_FRONT_TURN_LIGHT] = {
.enable_pin = &s_front_pin_front_right_turn_light_en,
.select_pin = &s_rear_pin_rear_left_right_turn_light_dsel,
.select_state = { .select_state_pca9555 = PCA9555_GPIO_STATE_HIGH },
.sense_pin = &(GpioAddress)PD_MUX_OUTPUT_PIN, // NOLINT
.resistor = 0,
.mux_val = MUX_SEL_FRONT_REAR_LEFT_RIGHT_TURN_LIGHT,
},
[FRONT_OUTPUT_DAYTIME_RUNNING_LIGHTS] = {
.enable_pin = &s_front_pin_daytime_running_lights_en,
.select_pin = NULL,
.select_state = {0},
.sense_pin = &(GpioAddress)PD_MUX_OUTPUT_PIN, // NOLINT
.resistor = 0,
.mux_val = MUX_SEL_DAYTIME_RUNNING_LIGHTS_BRAKE_LIGHTS,
},
[FRONT_OUTPUT_CAMERA_DISPLAY] = {
.enable_pin = &s_front_pin_main_pi_camera_display_en,
.select_pin = NULL,
.select_state = {0},
.sense_pin = &(GpioAddress)PD_MUX_OUTPUT_PIN, // NOLINT
.resistor = 0,
.mux_val = MUX_SEL_INFOTAINMENT_DISPLAY_BMS,
},
[REAR_OUTPUT_BMS] = {
.enable_pin = &s_rear_pin_bms_en,
.select_pin = NULL,
.sense_pin = &(GpioAddress)PD_MUX_OUTPUT_PIN, // NOLINT
.resistor = 0,
.mux_val = MUX_SEL_INFOTAINMENT_DISPLAY_BMS,
},
[REAR_OUTPUT_MCI] = {
.enable_pin = &s_rear_pin_mci_en,
.select_pin = NULL,
.sense_pin = &(GpioAddress)PD_MUX_OUTPUT_PIN, // NOLINT
.resistor = 0,
.mux_val = MUX_SEL_MCI,
},
[REAR_OUTPUT_SOLAR_SENSE] = {
.enable_pin = &s_rear_pin_solar_sense_en,
.select_state = {0},
.select_pin = NULL,
.sense_pin = &(GpioAddress)PD_MUX_OUTPUT_PIN, // NOLINT
.resistor = 0,
.mux_val = MUX_SEL_SPEAKER_SOLAR,
},
[REAR_OUTPUT_BPS_STROBE_LIGHT] = {
.enable_pin = &s_rear_pin_strobe_light_en,
.select_pin = &s_rear_pin_strobe_light_dsel,
.select_state = { .select_state_pca9555 = PCA9555_GPIO_STATE_HIGH },
.sense_pin = &(GpioAddress)PD_MUX_OUTPUT_PIN, // NOLINT
.resistor = 0,
.mux_val = MUX_SEL_LEFT_RIGHT_DISPLAY_STROBE_LIGHTS,
},
[REAR_OUTPUT_FAN_1] = {
.enable_pin = &s_rear_pin_fan_1_en,
.select_pin = &s_rear_pin_fan_1_2_dsel,
.select_state = { .select_state_pca9555 = PCA9555_GPIO_STATE_HIGH },
.sense_pin = &(GpioAddress)PD_MUX_OUTPUT_PIN, // NOLINT
.resistor = 0,
.mux_val = MUX_SEL_FAN_1_2,
},
};

Expand Down
63 changes: 63 additions & 0 deletions projects/power_distribution_front/src/output_current_sense.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include "output_current_sense.h"

#include "bts_load_switch.h"
#include "status.h"

static const GpioAddress sense_mux_pin = PD_MUX_OUTPUT_PIN;
static const GpioAddress mux_select_pins[NUM_MUX_SEL_PINS] = {
PD_MUX_SEL1_PIN,
PD_MUX_SEL2_PIN,
PD_MUX_SEL3_PIN,
PD_MUX_SEL4_PIN,
};

StatusCode pd_sense_init(void) {
// init mux output for adc
status_ok_or_return(gpio_init_pin(&sense_mux_pin, GPIO_ANALOG, GPIO_STATE_LOW));
status_ok_or_return(adc_add_channel(sense_mux_pin));

// init mux sel pins
for (uint8_t i = 0; i < NUM_MUX_SEL_PINS; ++i) {
status_ok_or_return(gpio_init_pin(&mux_select_pins[i], GPIO_OUTPUT_PUSH_PULL, GPIO_STATE_LOW));
}

return STATUS_CODE_OK;
}

static StatusCode prv_set_sense_mux(uint8_t mux_val) {
if (mux_val >= NUM_MUX_INPUTS) {
return STATUS_CODE_INVALID_ARGS;
}
for (uint8_t i = 0; i < NUM_MUX_SEL_PINS; ++i) {
status_ok_or_return(gpio_set_state(&mux_select_pins[i], mux_val & (1 << i)));
}
return STATUS_CODE_OK;
}

StatusCode pd_sense_output_group(OutputGroup group) {
if (group >= NUM_OUTPUT_GROUPS) {
return STATUS_CODE_INVALID_ARGS;
}

if (group == OUTPUT_GROUP_ALL) {
for (uint8_t out = 0; out < NUM_OUTPUTS; out++) {
status_ok_or_return(prv_set_sense_mux(g_output_config[out].mux_val));
status_ok_or_return(
bts_output_get_current(&g_output_config[out], &(g_output_config[out].reading_out)));
}
} else {
// Get specific group, iterate through set of pins
OutputGroupDef *grp = g_output_group_map[group];
if (!grp) {
return STATUS_CODE_UNINITIALIZED;
}

for (uint8_t out = 0; out < grp->num_outputs; out++) {
Output output = grp->outputs[out];
status_ok_or_return(prv_set_sense_mux(g_output_config[out].mux_val));
status_ok_or_return(
bts_output_get_current(&g_output_config[out], &(g_output_config[out].reading_out)));
}
}
return STATUS_CODE_OK;
}

0 comments on commit 87e5665

Please sign in to comment.