PER Firmware
Loading...
Searching...
No Matches
adbms6380.c File Reference

Low level BMS driver specific to ADBMS6380 chip. More...

#include "adbms6380.h"
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include "commands.h"
#include "phal_G4/gpio/gpio.h"
#include "phal_G4/spi/spi.h"
#include "rtos/rtos.h"
#include "utils/countof.h"
#include "pec.h"
#include "thermistor.h"
Include dependency graph for adbms6380.c:

Functions

static void set_cs_low (SPI_InitConfig_t *spi)
 
static void set_cs_high (SPI_InitConfig_t *spi)
 
static void wake_spi (SPI_InitConfig_t *spi, size_t pulse_count)
 
static void transfer_tx_spi (adbms6380_t *dev, SPI_InitConfig_t *spi)
 
static void transfer_tx (adbms6380_t *dev)
 
static uint16_t get_threshold_voltage_cfg (float threshold_voltage_v)
 
static int16_t extract_i16 (const uint8_t *data, size_t idx)
 
static float raw_to_v (int16_t raw)
 
static void encode_adcv (uint8_t output_cmd[ADBMS6380_COMMAND_RAW_SIZE], const adbms6380_adcv_config_t *cfg)
 
static void encode_adax (uint8_t output_cmd[ADBMS6380_COMMAND_RAW_SIZE], const adbms6380_adax_config_t *cfg)
 
static void prepare_command (strbuf_t *output_buffer, const uint8_t command[ADBMS6380_COMMAND_RAW_SIZE])
 
static void prepare_data_packet (strbuf_t *output_buffer, const uint8_t data[ADBMS6380_SINGLE_DATA_RAW_SIZE])
 
static void pack_cfg_rega (uint8_t output_cfg_rega[ADBMS6380_SINGLE_DATA_RAW_SIZE], const adbms6380_rega_config_t *cfg)
 
static void pack_cfg_regb (uint8_t output_cfg_regb[ADBMS6380_SINGLE_DATA_RAW_SIZE], const adbms6380_regb_config_t *cfg, const bool is_discharging[ADBMS6380_CELL_COUNT])
 
static bool check_data_pec (const uint8_t *rx_bytes, size_t rx_len)
 
static void read_spi_packets (adbms6380_t *dev, SPI_InitConfig_t *spi, uint8_t *rx_dest, size_t rx_length)
 
static bool read_response (adbms6380_t *dev, size_t rx_length_per_chip)
 
static bool read_data_with_retries (adbms6380_t *dev)
 
static void write_rega_spi (adbms6380_t *dev, SPI_InitConfig_t *spi)
 
static void discover_ccw_chip_count (adbms6380_t *dev)
 
void ADBMS6380_init (adbms6380_t *dev, SPI_InitConfig_t *spi_ccw, SPI_InitConfig_t *spi_cw)
 Initialize the ADBMS6380 device driver instance, SPI buffers, and CS lines.
 
void ADBMS6380_wake (adbms6380_t *dev)
 Wake the daisy-chained set of ADBMS6380 devices.
 
void ADBMS6380_write_rega (adbms6380_t *dev, const adbms6380_rega_config_t *cfg)
 Encode and write REGA configuration to all chips in the daisy chain.
 
bool ADBMS6380_verify_rega (adbms6380_t *dev)
 Discover the active CCW/CW split point, read back REGA from all chips, and compare with cached configuration.
 
void ADBMS6380_write_regb (adbms6380_t *dev, const adbms6380_regb_config_t *cfg)
 Encode and write REGB configuration (OV/UV thresholds + discharge bits) to all chips.
 
bool ADBMS6380_verify_regb (adbms6380_t *dev)
 Read back REGB from all chips and compare with cached configuration.
 
bool ADBMS6380_has_rega_mismatch (const adbms6380_t *dev)
 Check whether any chip in the chain has a REGA mismatch error.
 
bool ADBMS6380_has_regb_mismatch (const adbms6380_t *dev)
 Check whether any chip in the chain has a REGB mismatch error.
 
void ADBMS6380_start_adcv (adbms6380_t *dev, const adbms6380_adcv_config_t *cfg)
 Issue the ADCV command to start cell voltage conversions across all chips.
 
void ADBMS6380_start_adax (adbms6380_t *dev, const adbms6380_adax_config_t *cfg)
 Issue the ADAX command to start GPIO/aux voltage conversions across all chips.
 
void ADBMS6380_read_cell_voltages (adbms6380_t *dev)
 Read all cell voltages from each chip.
 
void ADBMS6380_read_gpio_voltages (adbms6380_t *dev)
 Read all GPIO/aux voltages from each chip.
 
static float gpio_v_to_r2 (float v_out)
 
void ADBMS6380_read_temperatures (adbms6380_t *dev, const adbms6380_adax_config_t *cfg)
 Read all GPIO voltages, start the next ADAX conversion, and convert to cell and onboard temperatures.
 

Variables

static constexpr size_t ADBMS6380_PEC_FAIL_MAX_RETRIES = 3U
 
static constexpr uint32_t ADBMS6380_WAKE_DELAY_MS = 1U
 
static constexpr uint32_t ADBMS6380_PORT_REVERSE_DELAY_MS = 2U
 
static constexpr float ADBMS6380_GPIO_R1_OHM = 10'000.0f
 
static constexpr float ADBMS6380_GPIO_VIN_V = 3.0f
 
static constexpr float ADBMS6380_DIVIDER_EPSILON_V = 0.00001f
 
static constexpr float ADBMS6380_VOLTAGE_OFFSET_V = 1.5f
 
static constexpr float ADBMS6380_ADC_LSB_V = 0.000150f
 
static constexpr float ADBMS6380_THRESHOLD_SCALE = 16.0f
 
static constexpr int32_t ADBMS6380_RAW_OFFSET_CODES = 10'000
 
static constexpr uint8_t ADBMS6380_THRESHOLD_BITS = 12U
 
static constexpr uint16_t ADBMS6380_THRESHOLD_MASK = 0x0FFFU
 
static const uint8_t *const RDCV_CMDS []
 
static const uint8_t *const RDAUX_CMDS []
 

Detailed Description

Low level BMS driver specific to ADBMS6380 chip.

Author
Millan Kumar (kumar.nosp@m.798@.nosp@m.purdu.nosp@m.e.ed.nosp@m.u)

Function Documentation

◆ ADBMS6380_has_rega_mismatch()

bool ADBMS6380_has_rega_mismatch ( const adbms6380_t * dev)

Check whether any chip in the chain has a REGA mismatch error.

Parameters
devNon-null pointer to device state.
Returns
true if any chip has adbms6380_chip_t::err_rega_mismatch set.
Here is the caller graph for this function:

◆ ADBMS6380_has_regb_mismatch()

bool ADBMS6380_has_regb_mismatch ( const adbms6380_t * dev)

Check whether any chip in the chain has a REGB mismatch error.

Parameters
devNon-null pointer to device state.
Returns
true if any chip has adbms6380_chip_t::err_regb_mismatch set.
Here is the caller graph for this function:

◆ ADBMS6380_init()

void ADBMS6380_init ( adbms6380_t * dev,
SPI_InitConfig_t * spi_ccw,
SPI_InitConfig_t * spi_cw )

Initialize the ADBMS6380 device driver instance, SPI buffers, and CS lines.

Deasserts both CS lines high, initializes the internal TX strbuf, and clears discharge and error flags across all chips.

Parameters
devNon-null pointer to device state to initialize.
spi_ccwNon-null CCW SPI configuration.
spi_cwNon-null CW SPI configuration.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ADBMS6380_read_cell_voltages()

void ADBMS6380_read_cell_voltages ( adbms6380_t * dev)

Read all cell voltages from each chip.

Converts raw values to volts and populates each chip's cell_voltages array. If any command fails with a PEC error after retries, the successfully read cell voltages are still updated and the corresponding flag in err_cell_voltage_pecs is set.

Parameters
devNon-null pointer to device state.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ADBMS6380_read_gpio_voltages()

void ADBMS6380_read_gpio_voltages ( adbms6380_t * dev)

Read all GPIO/aux voltages from each chip.

Converts raw values to volts and populates each chip's gpio_voltages array. If any command fails with a PEC error after retries, the successfully read GPIO voltages are still updated and the corresponding flag in err_gpio_voltage_pecs is set.

Parameters
devNon-null pointer to device state.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ADBMS6380_read_temperatures()

void ADBMS6380_read_temperatures ( adbms6380_t * dev,
const adbms6380_adax_config_t * cfg )

Read all GPIO voltages, start the next ADAX conversion, and convert to cell and onboard temperatures.

Populates each chip's gpio_voltages, cell_temps, and board_temp fields.

Parameters
devNon-null pointer to device state.
cfgNon-null pointer to ADAX command parameters.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ADBMS6380_start_adax()

void ADBMS6380_start_adax ( adbms6380_t * dev,
const adbms6380_adax_config_t * cfg )

Issue the ADAX command to start GPIO/aux voltage conversions across all chips.

Parameters
devNon-null pointer to device state.
cfgNon-null pointer to ADAX command parameters.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ADBMS6380_start_adcv()

void ADBMS6380_start_adcv ( adbms6380_t * dev,
const adbms6380_adcv_config_t * cfg )

Issue the ADCV command to start cell voltage conversions across all chips.

Parameters
devNon-null pointer to device state.
cfgNon-null pointer to ADCV command parameters.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ADBMS6380_verify_rega()

bool ADBMS6380_verify_rega ( adbms6380_t * dev)

Discover the active CCW/CW split point, read back REGA from all chips, and compare with cached configuration.

Sets per-chip mismatch or PEC flags when the read-back fails or does not match the cached REGA data.

Parameters
devNon-null pointer to device state.
Returns
false on PEC error or when there is a mismatch; true otherwise.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ADBMS6380_verify_regb()

bool ADBMS6380_verify_regb ( adbms6380_t * dev)

Read back REGB from all chips and compare with cached configuration.

Sets per-chip mismatch or PEC flags when the read-back fails or does not match the cached REGB data.

Parameters
devNon-null pointer to device state.
Returns
false on PEC error or when there is a mismatch; true otherwise.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ADBMS6380_wake()

void ADBMS6380_wake ( adbms6380_t * dev)

Wake the daisy-chained set of ADBMS6380 devices.

Toggles CS with blocking delay to bring each chip out of sleep/idle. If the harness break is already known, wakes only the reachable chips in each direction.

Parameters
devNon-null pointer to device state.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ADBMS6380_write_rega()

void ADBMS6380_write_rega ( adbms6380_t * dev,
const adbms6380_rega_config_t * cfg )

Encode and write REGA configuration to all chips in the daisy chain.

Parameters
devNon-null pointer to device state.
cfgNon-null pointer to REGA configuration parameters.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ADBMS6380_write_regb()

void ADBMS6380_write_regb ( adbms6380_t * dev,
const adbms6380_regb_config_t * cfg )

Encode and write REGB configuration (OV/UV thresholds + discharge bits) to all chips.

Parameters
devNon-null pointer to device state.
cfgNon-null pointer to REGB configuration parameters.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ check_data_pec()

static bool check_data_pec ( const uint8_t * rx_bytes,
size_t rx_len )
static
Here is the call graph for this function:
Here is the caller graph for this function:

◆ discover_ccw_chip_count()

static void discover_ccw_chip_count ( adbms6380_t * dev)
static
Here is the call graph for this function:
Here is the caller graph for this function:

◆ encode_adax()

static void encode_adax ( uint8_t output_cmd[ADBMS6380_COMMAND_RAW_SIZE],
const adbms6380_adax_config_t * cfg )
static
Here is the caller graph for this function:

◆ encode_adcv()

static void encode_adcv ( uint8_t output_cmd[ADBMS6380_COMMAND_RAW_SIZE],
const adbms6380_adcv_config_t * cfg )
static
Here is the caller graph for this function:

◆ extract_i16()

static int16_t extract_i16 ( const uint8_t * data,
size_t idx )
static
Here is the caller graph for this function:

◆ get_threshold_voltage_cfg()

static uint16_t get_threshold_voltage_cfg ( float threshold_voltage_v)
static
Here is the caller graph for this function:

◆ gpio_v_to_r2()

static float gpio_v_to_r2 ( float v_out)
static
Here is the caller graph for this function:

◆ pack_cfg_rega()

static void pack_cfg_rega ( uint8_t output_cfg_rega[ADBMS6380_SINGLE_DATA_RAW_SIZE],
const adbms6380_rega_config_t * cfg )
static
Here is the caller graph for this function:

◆ pack_cfg_regb()

static void pack_cfg_regb ( uint8_t output_cfg_regb[ADBMS6380_SINGLE_DATA_RAW_SIZE],
const adbms6380_regb_config_t * cfg,
const bool is_discharging[ADBMS6380_CELL_COUNT] )
static
Here is the call graph for this function:
Here is the caller graph for this function:

◆ prepare_command()

static void prepare_command ( strbuf_t * output_buffer,
const uint8_t command[ADBMS6380_COMMAND_RAW_SIZE] )
static
Here is the call graph for this function:
Here is the caller graph for this function:

◆ prepare_data_packet()

static void prepare_data_packet ( strbuf_t * output_buffer,
const uint8_t data[ADBMS6380_SINGLE_DATA_RAW_SIZE] )
static
Here is the call graph for this function:
Here is the caller graph for this function:

◆ raw_to_v()

static float raw_to_v ( int16_t raw)
static
Here is the caller graph for this function:

◆ read_data_with_retries()

static bool read_data_with_retries ( adbms6380_t * dev)
static
Here is the call graph for this function:
Here is the caller graph for this function:

◆ read_response()

static bool read_response ( adbms6380_t * dev,
size_t rx_length_per_chip )
static
Here is the call graph for this function:
Here is the caller graph for this function:

◆ read_spi_packets()

static void read_spi_packets ( adbms6380_t * dev,
SPI_InitConfig_t * spi,
uint8_t * rx_dest,
size_t rx_length )
static
Here is the call graph for this function:
Here is the caller graph for this function:

◆ set_cs_high()

static void set_cs_high ( SPI_InitConfig_t * spi)
static
Here is the call graph for this function:
Here is the caller graph for this function:

◆ set_cs_low()

static void set_cs_low ( SPI_InitConfig_t * spi)
static
Here is the call graph for this function:
Here is the caller graph for this function:

◆ transfer_tx()

static void transfer_tx ( adbms6380_t * dev)
static
Here is the call graph for this function:
Here is the caller graph for this function:

◆ transfer_tx_spi()

static void transfer_tx_spi ( adbms6380_t * dev,
SPI_InitConfig_t * spi )
static
Here is the call graph for this function:
Here is the caller graph for this function:

◆ wake_spi()

static void wake_spi ( SPI_InitConfig_t * spi,
size_t pulse_count )
static
Here is the call graph for this function:
Here is the caller graph for this function:

◆ write_rega_spi()

static void write_rega_spi ( adbms6380_t * dev,
SPI_InitConfig_t * spi )
static
Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ ADBMS6380_ADC_LSB_V

float ADBMS6380_ADC_LSB_V = 0.000150f
staticconstexpr

◆ ADBMS6380_DIVIDER_EPSILON_V

float ADBMS6380_DIVIDER_EPSILON_V = 0.00001f
staticconstexpr

◆ ADBMS6380_GPIO_R1_OHM

float ADBMS6380_GPIO_R1_OHM = 10'000.0f
staticconstexpr

◆ ADBMS6380_GPIO_VIN_V

float ADBMS6380_GPIO_VIN_V = 3.0f
staticconstexpr

◆ ADBMS6380_PEC_FAIL_MAX_RETRIES

size_t ADBMS6380_PEC_FAIL_MAX_RETRIES = 3U
staticconstexpr

◆ ADBMS6380_PORT_REVERSE_DELAY_MS

uint32_t ADBMS6380_PORT_REVERSE_DELAY_MS = 2U
staticconstexpr

◆ ADBMS6380_RAW_OFFSET_CODES

int32_t ADBMS6380_RAW_OFFSET_CODES = 10'000
staticconstexpr

◆ ADBMS6380_THRESHOLD_BITS

uint8_t ADBMS6380_THRESHOLD_BITS = 12U
staticconstexpr

◆ ADBMS6380_THRESHOLD_MASK

uint16_t ADBMS6380_THRESHOLD_MASK = 0x0FFFU
staticconstexpr

◆ ADBMS6380_THRESHOLD_SCALE

float ADBMS6380_THRESHOLD_SCALE = 16.0f
staticconstexpr

◆ ADBMS6380_VOLTAGE_OFFSET_V

float ADBMS6380_VOLTAGE_OFFSET_V = 1.5f
staticconstexpr

◆ ADBMS6380_WAKE_DELAY_MS

uint32_t ADBMS6380_WAKE_DELAY_MS = 1U
staticconstexpr

◆ RDAUX_CMDS

const uint8_t* const RDAUX_CMDS[]
static
Initial value:
= {
}
static constexpr uint8_t ADBMS6380_CMD_RDAUXD[2]
Definition commands.h:68
static constexpr uint8_t ADBMS6380_CMD_RDAUXA[2]
Definition commands.h:65
static constexpr uint8_t ADBMS6380_CMD_RDAUXC[2]
Definition commands.h:67
static constexpr uint8_t ADBMS6380_CMD_RDAUXB[2]
Definition commands.h:66

◆ RDCV_CMDS

const uint8_t* const RDCV_CMDS[]
static
Initial value:
= {
}
static constexpr uint8_t ADBMS6380_CMD_RDCVF[2]
Definition commands.h:27
static constexpr uint8_t ADBMS6380_CMD_RDCVA[2]
Definition commands.h:22
static constexpr uint8_t ADBMS6380_CMD_RDCVB[2]
Definition commands.h:23
static constexpr uint8_t ADBMS6380_CMD_RDCVD[2]
Definition commands.h:25
static constexpr uint8_t ADBMS6380_CMD_RDCVC[2]
Definition commands.h:24
static constexpr uint8_t ADBMS6380_CMD_RDCVE[2]
Definition commands.h:26