15#include "common/phal/spi.h"
19#define ADBMS6380_CELL_COUNT (16)
21#define ADBMS6380_GPIO_COUNT (10)
24#define ADBMS6380_PEC_SIZE (2)
26#define ADBMS6380_COMMAND_RAW_SIZE (2)
28#define ADBMS6380_COMMAND_PKT_SIZE (ADBMS6380_COMMAND_RAW_SIZE + ADBMS6380_PEC_SIZE)
30#define ADBMS6380_SINGLE_DATA_RAW_SIZE (6)
32#define ADBMS6380_SINGLE_DATA_PKT_SIZE (ADBMS6380_SINGLE_DATA_RAW_SIZE + ADBMS6380_PEC_SIZE)
36#define ADBMS6380_RDCV_CMD_COUNT (6)
40#define ADBMS6380_RDAUX_CMD_COUNT (4)
47#define ADBMS6380_WAKE_DELAY_MS (1)
56 ADBMS6380_READ_SUCCESS = 0,
57 ADBMS6380_READ_PEC_FAILURE,
58 ADBMS6380_READ_SPI_FAILURE,
117void adbms6380_adcv(uint8_t output_cmd[ADBMS6380_COMMAND_RAW_SIZE],
131void adbms6380_adsv(uint8_t output_cmd[ADBMS6380_COMMAND_RAW_SIZE],
144void adbms6380_adax(uint8_t output_cmd[ADBMS6380_COMMAND_RAW_SIZE],
bool ow,
bool pup, uint8_t ch);
153 const uint8_t command[ADBMS6380_COMMAND_RAW_SIZE]);
161 const uint8_t data[ADBMS6380_SINGLE_DATA_RAW_SIZE]);
186 float overvoltage_threshold,
187 float undervoltage_threshold,
188 const bool is_discharging[ADBMS6380_CELL_COUNT]);
216 const uint8_t cmd_buffer[ADBMS6380_COMMAND_PKT_SIZE],
218 size_t rx_length_per_module);
233 const uint8_t cmd_buffer[ADBMS6380_COMMAND_PKT_SIZE],
253 const uint8_t cmd_buffer[ADBMS6380_COMMAND_PKT_SIZE],
279 float **cell_voltages,
280 int16_t **cell_voltages_raw,
281 bool pec_error_flags[ADBMS6380_RDCV_CMD_COUNT],
283 size_t max_retries_on_pec_failure);
306 float **gpio_voltages,
307 bool pec_error_flags[ADBMS6380_RDAUX_CMD_COUNT],
309 size_t max_retries_on_pec_failure);
void adbms6380_set_cs_low(SPI_InitConfig_t *spi)
Drive the ADBMS CS line low.
Definition adbms6380.c:21
float adbms6380_raw_to_v(int16_t raw)
Convert raw voltage measurement from the ADBMS6380 to volts.
Definition adbms6380.c:51
bool adbms6380_read_cell_voltages(SPI_InitConfig_t *spi, strbuf_t *cmd_buffer, uint8_t *rx_buffer, float **cell_voltages, int16_t **cell_voltages_raw, bool pec_error_flags[ADBMS6380_RDCV_CMD_COUNT], size_t module_count, size_t max_retries_on_pec_failure)
Read all cell voltages from each module.
Definition adbms6380.c:223
int16_t adbms6380_extract_i16(uint8_t *data, int idx)
Extract a little-endian 16-bit signed value from a byte buffer.
Definition adbms6380.c:47
adbms6380_read_result_t
Result of a read operation from the ADBMS6380.
Definition adbms6380.h:55
bool adbms6380_read_gpio_voltages(SPI_InitConfig_t *spi, strbuf_t *cmd_buffer, uint8_t *rx_buffer, float **gpio_voltages, bool pec_error_flags[ADBMS6380_RDAUX_CMD_COUNT], size_t module_count, size_t max_retries_on_pec_failure)
Read all GPIO/aux voltages from each module.
Definition adbms6380.c:281
uint16_t adbms6380_get_threshold_voltage_cfg(float threshold_voltage)
Convert a voltage threshold (V) into 12-bit REG_B threshold encoding.
Definition adbms6380.c:38
void adbms6380_calculate_cfg_regb(uint8_t output_cfg_regb[ADBMS6380_SINGLE_DATA_RAW_SIZE], float overvoltage_threshold, float undervoltage_threshold, const bool is_discharging[ADBMS6380_CELL_COUNT])
Calculate the REG_B configuration register value.
Definition adbms6380.c:117
void adbms6380_adax(uint8_t output_cmd[ADBMS6380_COMMAND_RAW_SIZE], bool ow, bool pup, uint8_t ch)
Build ADAX (aux) command bytes.
Definition adbms6380.c:76
adbms6380_read_result_t adbms6380_read_data(SPI_InitConfig_t *spi, size_t module_count, const uint8_t cmd_buffer[ADBMS6380_COMMAND_PKT_SIZE], uint8_t *rx_buffer)
Read a single-data-packet response per module.
Definition adbms6380.c:215
void adbms6380_prepare_command(strbuf_t *output_buffer, const uint8_t command[ADBMS6380_COMMAND_RAW_SIZE])
Append a command and its PEC to an output buffer.
Definition adbms6380.c:82
void adbms6380_prepare_data_packet(strbuf_t *output_buffer, const uint8_t data[ADBMS6380_SINGLE_DATA_RAW_SIZE])
Append a data payload and its PEC to an output buffer.
Definition adbms6380.c:90
void adbms6380_adsv(uint8_t output_cmd[ADBMS6380_COMMAND_RAW_SIZE], bool cont, bool dcp, uint8_t ow)
Build ADSV command bytes.
Definition adbms6380.c:67
void adbms6380_wake(SPI_InitConfig_t *spi, size_t module_count)
Wake a daisy-chained set of ADBMS devices.
Definition adbms6380.c:29
void adbms6380_adcv(uint8_t output_cmd[ADBMS6380_COMMAND_RAW_SIZE], bool rd, bool cont, bool dcp, bool rstf, uint8_t ow)
Build ADCV command bytes.
Definition adbms6380.c:55
adbms6380_read_result_t adbms6380_read(SPI_InitConfig_t *spi, size_t module_count, const uint8_t cmd_buffer[ADBMS6380_COMMAND_PKT_SIZE], uint8_t *rx_buffer, size_t rx_length_per_module)
Read a fixed-length response from all modules after a command.
Definition adbms6380.c:157
adbms6380_read_result_t adbms6380_read_data_with_retries(SPI_InitConfig_t *spi, size_t max_retries, size_t module_count, const uint8_t cmd_buffer[ADBMS6380_COMMAND_PKT_SIZE], uint8_t *rx_buffer)
Read a single-data-packet response per module and retries on PEC failure.
Definition adbms6380.c:189
bool adbms6380_check_data_pec(const uint8_t *rx_bytes, size_t rx_len)
Verify the PEC of received data.
Definition adbms6380.c:146
void adbms6380_calculate_cfg_rega(uint8_t output_cfg_rega[ADBMS6380_SINGLE_DATA_RAW_SIZE], bool refon, uint8_t cth)
Calculate the REG_A configuration register value.
Definition adbms6380.c:98
void adbms6380_set_cs_high(SPI_InitConfig_t *spi)
Drive the ADBMS CS line to high.
Definition adbms6380.c:25
Fixed-size string builder. Useful for building large CMD strings before transmission.
Configuration entry for SPI initilization.
Definition spi.h:26