Skip to content
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
6 changes: 2 additions & 4 deletions Core/Inc/adi2950_interaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,16 @@ void read_vbat_regsisters(cell_asic_2950 ic, SPI_HandleTypeDef *hspi);
/**
* @brief Sets the state of a gpo pin.
* @param ic Pointer to the adbms2950 data structure.
* @param hspi Pointer to the SPI interface handle.
* @param gpo GPO to set
*/
void set_gpo(cell_asic_2950 ic, SPI_HandleTypeDef *hspi, GPO_2950 gpo);
void set_gpo(cell_asic_2950 ic, GPO_2950 gpo);

/**
* @brief Resets a gpo pin.
* @param ic Pointer to the adbms2950 data structure.
* @param hspi Pointer to the SPI interface handle.
* @param gpo GPO to reset
*/
void reset_gpo(cell_asic_2950 ic, SPI_HandleTypeDef *hspi, GPO_2950 gpo);
void reset_gpo(cell_asic_2950 ic, GPO_2950 gpo);

/**
* @brief Reads the voltage registers from the ADBMS2950.
Expand Down
12 changes: 1 addition & 11 deletions Core/Inc/hv_plate.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "stm32xx_hal.h"
#include "adi_bms_2950data.h"

#define SHUNT_RESISTANCE 0.05 / 1000 // 0.05 mOhms
#define HV_CTRL_GPO GPIO4_2950

/**
* @brief initializes the adbms2950
Expand Down Expand Up @@ -46,14 +46,4 @@ float get_ts_voltage(cell_asic_2950 *ic, SPI_HandleTypeDef *hspi);
*/
float get_shunt_temp(cell_asic_2950 *ic, SPI_HandleTypeDef *hspi);

/**
* @brief Sets the HV_CTRL GPO to the desired state to toggle precharge
*
* @param ic pointer to adbms data struct
* @param hspi pointer to spi handler
* @param state if true, pulls the GPO up, if false pulls it down
*/
void set_precharge_relay(cell_asic_2950 *ic, SPI_HandleTypeDef *hspi,
bool state);

#endif
32 changes: 32 additions & 0 deletions Core/Inc/precharge_routine.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "adi2950_interaction.h"
#include "timer.h"
#include "hv_plate.h"

typedef struct {
hv_plate_t *hv_plate;
GPO_2950 gpo;
float transition_ratio;
nertimer_t open_debounce_timer;
nertimer_t close_debounce_timer;
uint32_t debounce_time;
bool air_switch_closed;
} prechargeconfig_t;
Copy link
Contributor

Choose a reason for hiding this comment

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

Love the struct for this, could you maybe add some docs for what each field is for?


/**
* @brief Given the cell, SPI, AIR switch, initializes the pre-charge structure for the given BATT/TS voltage capacitor threshold ratio,
* and with the given debounce time to wait when the AIR opens before closing again.ADI1_delay_ms
* @param precharge_config is the empty configuration to configure.
* @param ic pointer to ADBMS2950 data struct
* @param threshold_ratio if batt volts > ts volts * threshold_ratio, close the AIR switch
* @param debounce_time is the time to wait after the AIR switch opens before closing again (to prevent immediate closes) in milliseconds.
* @return a precharge configuration for running the precharge thread.
*/
prechargeconfig_t *precharge_init(prechargeconfig_t *precharge_config,
hv_plate_t *hv_plate, float transition_ratio,
uint32_t debounce_time);
/**
* @brief Handles the precharge routine given the current BATT and TS voltages.
* @param precharge_config the prechsarge configuration struct.
* NOTE: Ment to be run in a Thread on a loop due to debounces
*/
void handle_precharge(prechargeconfig_t *precharge_config);
4 changes: 2 additions & 2 deletions Core/Src/adi2950_interaction.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ void read_vr_registers(cell_asic_2950 ic, SPI_HandleTypeDef *hspi)
adBmsReadData2950(TOTAL_IC_2950, &ic, RDV2C, GPV2, E_2950);
}

void set_gpo(cell_asic_2950 ic, SPI_HandleTypeDef *hspi, GPO_2950 gpo)
void set_gpo(cell_asic_2950 ic, GPO_2950 gpo)
{
switch (gpo) {
case GPO1_2950:
Expand Down Expand Up @@ -84,7 +84,7 @@ void set_gpo(cell_asic_2950 ic, SPI_HandleTypeDef *hspi, GPO_2950 gpo)
adBmsReadData2950(TOTAL_IC_2950, &ic, RDCFGA2950, Config2950, A_2950);
}

void reset_gpo(cell_asic_2950 ic, SPI_HandleTypeDef *hspi, GPO_2950 gpo)
void reset_gpo(cell_asic_2950 ic, GPO_2950 gpo)
{
switch (gpo) {
case GPO1_2950:
Expand Down
12 changes: 1 addition & 11 deletions Core/Src/hv_plate.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "adi2950_interaction.h"
#include "hv_plate.h"

#define HV_CTRL_GPO GPIO4_2950
#define SHUNT_RESISTANCE 0.05 / 1000 // 0.05 mOhms

static float get_current_conversion(uint32_t data)
{
Expand Down Expand Up @@ -126,13 +126,3 @@ float get_shunt_temp(cell_asic_2950 *ic, SPI_HandleTypeDef *hspi)
2;
return avg_volts;
}

void set_precharge_relay(cell_asic_2950 *ic, SPI_HandleTypeDef *hspi,
bool state)
{
if (state) {
set_gpo(*ic, hspi, HV_CTRL_GPO);
} else {
reset_gpo(*ic, hspi, HV_CTRL_GPO);
}
}
58 changes: 58 additions & 0 deletions Core/Src/precharge_routine.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include "precharge_routine.h"
#include <assert.h>
#include "debounce.h"

static void close_relay(void *args)
{
prechargeconfig_t *precharge_config = (prechargeconfig_t *)args;
set_precharge_relay(precharge_config->hv_plate->ic, true);
precharge_config->air_switch_closed = true;
}

static void open_relay(void *args)
{
prechargeconfig_t *precharge_config = (prechargeconfig_t *)args;
set_precharge_relay(precharge_config->hv_plate->ic, false);
precharge_config->air_switch_closed = false;
}

static void set_precharge_relay(cell_asic_2950 *ic, bool state)
{
if (state) {
set_gpo(*ic, HV_CTRL_GPO);
} else {
reset_gpo(*ic, HV_CTRL_GPO);
}
}

prechargeconfig_t *precharge_init(prechargeconfig_t *precharge_config,
hv_plate_t *hv_plate, float transition_ratio,
uint32_t debounce_time)
{
assert(precharge_config != NULL);
assert(hv_plate != NULL);
assert(transition_ratio > 0 && transition_ratio < 1);

precharge_config->transition_ratio = transition_ratio;
precharge_config->open_debounce_timer =
(nertimer_t){ 0, 0, false, false };
precharge_config->close_debounce_timer =
(nertimer_t){ 0, 0, false, false };
precharge_config->debounce_time = debounce_time;
precharge_config->air_switch_closed = false;
}

void handle_precharge(prechargeconfig_t *precharge_config)
{
hv_plate_t *hv_plate = precharge_config->hv_plate;
bool should_precharge = // TODO: mutex hv plate data
hv_plate->ts_volts * precharge_config->transition_ratio >=
hv_plate->batt_volts;

debounce(should_precharge, &precharge_config->open_debounce_timer,
precharge_config->debounce_time, close_relay,
precharge_config);

debounce(!should_precharge, &precharge_config->close_debounce_timer,
precharge_config->debounce_time, open_relay, precharge_config);
}
15 changes: 15 additions & 0 deletions Core/Src/shep_tasks.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "hv_plate.h"
#include "compute.h"
#include "cell_temp_sanitizer.h"
#include "precharge_routine.h"

void vDefaultTask(ULONG thread_input)
{
Expand Down Expand Up @@ -242,6 +243,20 @@ void vSanitizer(ULONG thread_input)
}
}

void vPrecharge(ULONG args)
{
hv_plate_t *hv_plate = (hv_plate_t *)args;

prechargeconfig_t precharge_config;
precharge_init(&precharge_config, hv_plate, 0.9f,
50 /* ms debounce time */);

for (;;) {
handle_precharge(&precharge_config);
tx_thread_sleep(MS_TO_TICKS(50)); // TODO; fix thread timing
}
}

void vBMSAlgorithms(ULONG thread_input)
{
for (;;) {
Expand Down