PER Firmware
Loading...
Searching...
No Matches
adbms.h File Reference

Primary logic and interface for ADBMS battery management system driver. More...

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include "adbms6380.h"
#include "common/phal/spi.h"
#include "common/strbuf/strbuf.h"

Go to the source code of this file.

Classes

struct  adbms_module_t
 Per-ADBMS module measurements, config, and error flags. More...
 
struct  adbms_bms_t
 Top-level ADBMS driver state, I/O buffers, measurements, and error flags. More...
 

Macros

#define ADBMS_MODULE_COUNT   (8)
 
#define ADBMS_PEC_FAIL_MAX_RETRIES   (3)
 
#define ADBMS_GPIO_R1   (10000.0f)
 
#define ADBMS_GPIO_VIN   (3.0f)
 
#define ADBMS_SPI_TX_BUFFER_SIZE    (ADBMS6380_COMMAND_PKT_SIZE + (ADBMS_MODULE_COUNT * ADBMS6380_SINGLE_DATA_PKT_SIZE))
 
#define ADBMS_SPI_RX_BUFFER_SIZE   (ADBMS6380_SINGLE_DATA_PKT_SIZE * ADBMS_MODULE_COUNT)
 
#define ADBMS_REGA_REFON   (true)
 
#define ADBMS_REGA_CTH   (0b110)
 
#define ADBMS_REGB_OV_THRESHOLD   (4.2f)
 
#define ADBMS_REGB_UV_THRESHOLD   (3.0f)
 
#define ADBMS_ADCV_RD   (false)
 
#define ADBMS_ADCV_CONT   (true)
 
#define ADBMS_ADCV_DCP   (false)
 
#define ADBMS_ADCV_RSTF   (true)
 
#define ADBMS_ADCV_OW   (0b00)
 
#define ADBMS_ADAX_OW   (false)
 
#define ADBMS_ADAX_PUP   (false)
 
#define ADBMS_ADAX_CH   (0b00000)
 

Enumerations

enum  adbms_state_t { ADBMS_STATE_IDLE = 0 , ADBMS_STATE_CONNECTED }
 ADBMS driver connection/operation state. More...
 

Functions

void adbms_init (adbms_bms_t *bms, SPI_InitConfig_t *spi, uint8_t *tx_buf)
 Initialize the ADBMS driver instance and TX buffer.
 
bool adbms_write_rega (adbms_bms_t *bms)
 Calculate and write REGA configuration to all modules.
 
bool adbms_write_regb (adbms_bms_t *bms)
 Calculate and write REGB configuration to all modules.
 
bool adbms_read_and_check_rega (adbms_bms_t *bms)
 Read back REGA and compare with cached configuration.
 
bool adbms_read_and_check_regb (adbms_bms_t *bms)
 Read back REGB and compare with cached configuration.
 
void adbms_connect (adbms_bms_t *bms)
 Perform the connect/bring-up sequence for all modules.
 
void adbms_read_cells (adbms_bms_t *bms)
 Read all cell voltages and update module/pack statistics.
 
void adbms_read_therms (adbms_bms_t *bms)
 Read all GPIO/thermistor values (voltage and calculate temperature) and update module/pack statistics.
 
void adbms_calculate_balance_cells (adbms_bms_t *bms, float min_voltage, float min_delta)
 Compute per-cell discharge flags based on pack voltage spread.
 
void adbms_balance_and_update_regb (adbms_bms_t *bms, float min_voltage, float min_delta)
 Compute discharge flags and write REGB to apply balancing.
 
void adbms_periodic (adbms_bms_t *bms, float min_voltage_for_balance, float min_delta_for_balance)
 Periodic service routine for connection, measurements, and balancing.
 

Detailed Description

Primary logic and interface for ADBMS battery management system driver.

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

Enumeration Type Documentation

◆ adbms_state_t

ADBMS driver connection/operation state.

Enumerator
ADBMS_STATE_IDLE 

Driver is idle; not connected or awaiting re-connection.

ADBMS_STATE_CONNECTED 

Driver is connected; periodic reads/balancing are active.

Function Documentation

◆ adbms_balance_and_update_regb()

void adbms_balance_and_update_regb ( adbms_bms_t * bms,
float min_voltage,
float min_delta )

Compute discharge flags and write REGB to apply balancing.

Calls adbms_calculate_balance_cells(), writes REGB, and verifies it.

Parameters
bmsPointer to driver state.
min_voltageMinimum pack voltage required to allow balancing.
min_deltaVoltage delta above minimum to start discharging.

◆ adbms_calculate_balance_cells()

void adbms_calculate_balance_cells ( adbms_bms_t * bms,
float min_voltage,
float min_delta )

Compute per-cell discharge flags based on pack voltage spread.

If balancing is disabled, clears all discharge flags. If the pack minimum voltage is below min_voltage, no balancing occurs. Otherwise, discharges cells above (min + delta).

Parameters
bmsPointer to driver state.
min_voltageMinimum pack voltage required to allow balancing.
min_deltaVoltage delta above minimum to start discharging.

◆ adbms_connect()

void adbms_connect ( adbms_bms_t * bms)

Perform the connect/bring-up sequence for all modules.

Writes REGA/REGB, verifies read-back, and starts ADCV conversions.

Parameters
bmsPointer to driver state.

◆ adbms_init()

void adbms_init ( adbms_bms_t * bms,
SPI_InitConfig_t * spi,
uint8_t * tx_buf )

Initialize the ADBMS driver instance and TX buffer.

Sets the driver state to idle, clears discharge flags (and discharged enabled), and sets the provided SPI config and TX buffer into the internal strbuf.

Parameters
bmsPointer to driver state to initialize.
spiSPI configuration used for ADBMS transactions.
tx_bufBacking buffer for TX command/data packets.

◆ adbms_periodic()

void adbms_periodic ( adbms_bms_t * bms,
float min_voltage_for_balance,
float min_delta_for_balance )

Periodic service routine for connection, measurements, and balancing.

If idle, attempts to connect. When connected, reads cells and thermistors, then updates balancing + REGB.

Parameters
bmsPointer to driver state.
min_voltage_for_balanceMinimum pack voltage required to allow balancing.
min_delta_for_balanceVoltage delta above minimum to start discharging.

◆ adbms_read_and_check_rega()

bool adbms_read_and_check_rega ( adbms_bms_t * bms)

Read back REGA and compare with cached configuration.

Sets per-module and aggregated mismatch flags when the read-back does not match the cached REGA data.

Parameters
bmsPointer to driver state.
Returns
False on SPI failure, PEC error, or when there is a mismatch; true otherwise.

◆ adbms_read_and_check_regb()

bool adbms_read_and_check_regb ( adbms_bms_t * bms)

Read back REGB and compare with cached configuration.

Sets per-module and aggregated mismatch flags when the read-back does not match the cached REGB data.

Parameters
bmsPointer to driver state.
Returns
False on SPI failure, PEC error, or when there is a mismatch; true otherwise.

◆ adbms_read_cells()

void adbms_read_cells ( adbms_bms_t * bms)

Read all cell voltages and update module/pack statistics.

Updates per-module min/max/avg/sum and aggregated min/max/avg/sum for the full pack.

Parameters
bmsPointer to driver state.

◆ adbms_read_therms()

void adbms_read_therms ( adbms_bms_t * bms)

Read all GPIO/thermistor values (voltage and calculate temperature) and update module/pack statistics.

Updates per-module min/max/avg for thermistor temperatures and aggregated min/max/avg for the full pack.

Parameters
bmsPointer to driver state.

◆ adbms_write_rega()

bool adbms_write_rega ( adbms_bms_t * bms)

Calculate and write REGA configuration to all modules.

Parameters
bmsPointer to driver state.
Returns
True on successful SPI transfer, false on failure.

◆ adbms_write_regb()

bool adbms_write_regb ( adbms_bms_t * bms)

Calculate and write REGB configuration to all modules.

Computes REGB with OV/UV thresholds and discharge flags, then writes the configuration to all modules.

Parameters
bmsPointer to driver state.
Returns
True on successful SPI transfer, false on failure.