Skip to content

Commit

Permalink
test push
Browse files Browse the repository at this point in the history
  • Loading branch information
Akashem06 committed Mar 20, 2024
1 parent 8e89c94 commit 8598c1c
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 19 deletions.
27 changes: 17 additions & 10 deletions libraries/ms-drivers/inc/ltc6811.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,17 @@ static_assert(sizeof(LtcAfeConfigRegisterData) == 6, "LtcAfeConfigRegisterData m

// COMM Register, refer to LTC6803 datasheet page 31, Table 15
typedef struct {
uint16_t icom0 : 4;
uint16_t d0 : 8;
uint16_t fcom0 : 4;
uint8_t icom0 : 4;
uint8_t d0 : 8;
uint8_t fcom0 : 4;

uint16_t icom1 : 4;
uint16_t d1 : 8;
uint16_t fcom1 : 4;
uint8_t icom1 : 4;
uint8_t d1 : 8;
uint8_t fcom1 : 4;

uint16_t icom2 : 4;
uint16_t d2 : 8;
uint16_t fcom2 : 4;
uint8_t icom2 : 4;
uint8_t d2 : 8;
uint8_t fcom2 : 4;
} _PACKED LtcAfeCommRegisterData;
static_assert(sizeof(LtcAfeCommRegisterData) == 6, "LtcAfeCommRegisterData must be 6 bytes");

Expand Down Expand Up @@ -116,7 +116,7 @@ typedef struct {
LtcAfeWriteDeviceConfigPacket devices[LTC_AFE_MAX_CELLS_PER_DEVICE];
} _PACKED LtcAfeWriteConfigPacket;
#define SIZEOF_LTC_AFE_WRITE_CONFIG_PACKET(devices) \
(LTC6811_CMD_SIZE + (devices) * sizeof(LtcAfeWriteConfigPacket))
(LTC6811_CMD_SIZE + (devices) * sizeof(LtcAfeWriteDeviceConfigPacket))

typedef union {
uint16_t voltages[3];
Expand Down Expand Up @@ -187,6 +187,10 @@ static_assert(sizeof(LtcAfeAuxRegisterGroupPacket) == 8,

#define LTC6811_STCOMM_RESERVED (1 << 0) | (1 << 1) | (1 << 5) | (1 << 8) | (1 << 9) | (1 << 10)

#define LTC6811_WRPWM_RESERVED (1 << 5)

#define LTC6811_RDPWM_RESERVED (1 << 5) | (1 << 2)

// command bits
// see Table 40 (p. 62)
#define LTC6811_GPIO1_PD_ON (0 << 3)
Expand Down Expand Up @@ -224,3 +228,6 @@ static_assert(sizeof(LtcAfeAuxRegisterGroupPacket) == 8,

#define LTC6811_FCOM_CSBM_LOW (0 << 0)
#define LTC6811_FCOM_CSBM_HIGH (1 << 3) | (1 << 0)

// see Table 17 (p. 38)
#define LTC6811_PWMC_DC_100 (0xF)
1 change: 0 additions & 1 deletion libraries/ms-drivers/inc/ltc_afe.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ typedef struct LtcAfeSettings {
typedef struct LtcAfeStorage {
// Only used for storage in the FSM so we store data for the correct cells
uint16_t aux_index;
uint16_t retry_count;
uint16_t device_cell;
uint32_t timer_start;

Expand Down
6 changes: 6 additions & 0 deletions libraries/ms-drivers/inc/ltc_afe_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
// represent whether we should monitor the cell input for the given device.
StatusCode ltc_afe_impl_init(LtcAfeStorage *afe, const LtcAfeSettings *settings);

// Write an LTC config based on the given storage settings
StatusCode ltc_afe_impl_write_config(LtcAfeStorage *afe);

// Triggers a conversion. Note that we need to wait for the conversions to complete before the
// readback will be valid.
StatusCode ltc_afe_impl_trigger_cell_conv(LtcAfeStorage *afe);
Expand All @@ -29,3 +32,6 @@ StatusCode ltc_afe_impl_read_aux(LtcAfeStorage *afe, uint8_t device_cell);
// Mark cell for discharging (takes effect after config is re-written)
// |cell| should be [0, LTC_AFE_MAX_CELLS)
StatusCode ltc_afe_impl_toggle_cell_discharge(LtcAfeStorage *afe, uint16_t cell, bool discharge);

// Sets the duty cycle to the same value for all cells on all afes
StatusCode ltc_afe_impl_set_discharge_pwm_cycle(LtcAfeStorage *afe, uint8_t duty_cycle);
42 changes: 38 additions & 4 deletions libraries/ms-drivers/src/ltc_afe_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ static uint16_t s_read_reg_cmd[NUM_LTC_AFE_REGISTERS] = {
[LTC_AFE_REGISTER_STATUS_A] = LTC6811_RDSTATA_RESERVED,
[LTC_AFE_REGISTER_STATUS_B] = LTC6811_RDSTATB_RESERVED,
[LTC_AFE_REGISTER_READ_COMM] = LTC6811_RDCOMM_RESERVED,
[LTC_AFE_REGISTER_START_COMM] = LTC6811_STCOMM_RESERVED
[LTC_AFE_REGISTER_START_COMM] = LTC6811_STCOMM_RESERVED,
};

static uint8_t s_voltage_reg[NUM_LTC_AFE_VOLTAGE_REGISTERS] = {
Expand All @@ -33,14 +33,15 @@ static uint8_t s_voltage_reg[NUM_LTC_AFE_VOLTAGE_REGISTERS] = {
[LTC_AFE_VOLTAGE_REGISTER_D] = LTC_AFE_REGISTER_CELL_VOLTAGE_D,
};


static void prv_wakeup_idle(LtcAfeStorage *afe) {
LtcAfeSettings *settings = &afe->settings;
// Wakeup method 2 - pair of long -1, +1 for each device
for (size_t i = 0; i < settings->num_devices; i++) {
gpio_set_state(&settings->cs, GPIO_STATE_LOW);
gpio_set_state(&settings->cs, GPIO_STATE_HIGH);
// Wait for 300us - greater than tWAKE, less than tIDLE
delay_ms(0.3);
// Wait for 1ms (should be 300us) - greater than tWAKE, less than tIDLE
delay_ms(1);
}
}

Expand Down Expand Up @@ -221,6 +222,25 @@ static void prv_calc_offsets(LtcAfeStorage *afe) {
}
}

// Sets the duty cycle to the same value for all cells on all afes
StatusCode ltc_afe_impl_set_discharge_pwm_cycle(LtcAfeStorage *afe, uint8_t duty_cycle) {
LtcAfeSettings *settings = &afe->settings;

uint8_t cmd[4 + (6 * 3)] = { 0 };
prv_build_cmd(LTC6811_WRPWM_RESERVED, cmd, 4);

// For every device, set all 6 PWM bytes to the same config
for (uint8_t curr_device = 0; curr_device < settings->num_devices; curr_device++) {
for (int cell_pwm = 0; cell_pwm < 6; cell_pwm++) {
cmd[(curr_device * 6) + cell_pwm] = duty_cycle;
}
}

size_t len = 4 + (6 * settings->num_devices);
prv_wakeup_idle(afe);
return spi_exchange(settings->spi_port, cmd, len, NULL, 0);
}

StatusCode ltc_afe_impl_init(LtcAfeStorage *afe, const LtcAfeSettings *settings) {
if (settings->num_devices > LTC_AFE_MAX_DEVICES ||
settings->num_cells > settings->num_devices * LTC_AFE_MAX_CELLS ||
Expand Down Expand Up @@ -248,7 +268,9 @@ StatusCode ltc_afe_impl_init(LtcAfeStorage *afe, const LtcAfeSettings *settings)
// Use GPIO1 as analog input, GPIO 3-5 for SPI
uint8_t gpio_bits =
LTC6811_GPIO1_PD_OFF | LTC6811_GPIO3_PD_OFF | LTC6811_GPIO4_PD_OFF | LTC6811_GPIO5_PD_OFF;
afe->timer_start = xTaskGetTickCount();

status_ok_or_return(ltc_afe_impl_set_discharge_pwm_cycle(afe, LTC6811_PWMC_DC_100));

return prv_write_config(afe, gpio_bits);
}

Expand Down Expand Up @@ -290,6 +312,11 @@ StatusCode ltc_afe_impl_read_cells(LtcAfeStorage *afe) {
uint16_t data_pec = crc15_calculate((uint8_t *)&voltage_register[device], 6);
if (received_pec != data_pec) {
// return early on failure
LOG_DEBUG("RECEIVED_PEC: %d\n\r", received_pec);
LOG_DEBUG("DATA_PEC: %d\n\r", data_pec);
LOG_DEBUG("Voltage: %d %d %d\n\r", voltage_register[device].reg.voltages[0],
voltage_register[device].reg.voltages[1],
voltage_register[device].reg.voltages[2]);
return status_code(STATUS_CODE_INTERNAL_ERROR);
}
}
Expand Down Expand Up @@ -343,3 +370,10 @@ StatusCode ltc_afe_impl_toggle_cell_discharge(LtcAfeStorage *afe, uint16_t cell,

return STATUS_CODE_OK;
}

// Just a wrapper setting the correct gpio bits
StatusCode ltc_afe_impl_write_config(LtcAfeStorage *afe) {
uint8_t gpio_bits =
LTC6811_GPIO1_PD_OFF | LTC6811_GPIO3_PD_OFF | LTC6811_GPIO4_PD_OFF | LTC6811_GPIO5_PD_OFF;
return prv_write_config(afe, gpio_bits);
}
8 changes: 6 additions & 2 deletions projects/bms_carrier/src/cell_sense.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ TASK(cell_sense_conversions, TASK_MIN_STACK_SIZE) {

// run conversions every 10 seconds
if (xTaskGetTickCount() - ltc_afe_storage->timer_start >= pdMS_TO_TICKS(10000)) {
LOG_DEBUG("RUNNING CONVERSIONS \n");
prv_cell_sense_conversions();
}

Expand Down Expand Up @@ -132,11 +133,13 @@ StatusCode cell_sense_run() {
for (size_t cell = 0; cell < (s_afe_settings.num_devices * s_afe_settings.num_cells); cell++) {
if (ltc_afe_storage->cell_voltages[ltc_afe_storage->cell_result_lookup[cell]] > min_voltage) {
ltc_afe_impl_toggle_cell_discharge(ltc_afe_storage, cell, true);
// LOG_DEBUG("Cell %d unbalanced %d MIN VOLTAGE: %d\n", cell,
// ltc_afe_storage->cell_voltages[ltc_afe_storage->cell_result_lookup[cell]], min_voltage);
LOG_DEBUG("Cell %d unbalanced %d MIN VOLTAGE: %d\n", cell,
ltc_afe_storage->cell_voltages[ltc_afe_storage->cell_result_lookup[cell]], min_voltage);
} else {
ltc_afe_impl_toggle_cell_discharge(ltc_afe_storage, cell, false);
}

ltc_afe_impl_write_config(ltc_afe_storage);
}
}

Expand Down Expand Up @@ -165,6 +168,7 @@ StatusCode cell_sense_init(LtcAfeStorage *afe_storage) {
ltc_afe_storage = afe_storage;
ltc_afe_init(ltc_afe_storage, &s_afe_settings);
delay_ms(10);
prv_cell_sense_conversions();
tasks_init_task(cell_sense_conversions, TASK_PRIORITY(2), NULL);
return STATUS_CODE_OK;
}
5 changes: 3 additions & 2 deletions projects/bms_carrier/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@ void run_fast_cycle() {}
void run_medium_cycle() {
run_can_rx_cycle();
wait_tasks(1);

cell_conversions();
// current_sense_run();
wait_tasks(1);
current_sense_run();
wait_tasks(1);

cell_sense_run();
Expand Down

0 comments on commit 8598c1c

Please sign in to comment.