PER Firmware
Loading...
Searching...
No Matches
adbms6380.h
Go to the documentation of this file.
1
8#ifndef _ADBMS6380_H_
9#define _ADBMS6380_H_
10
11#include <stdbool.h>
12#include <stddef.h>
13#include <stdint.h>
14
15#include "common/phal/spi.h"
17
18// Number of cells per ADBMS6380 device.
19#define ADBMS6380_CELL_COUNT (16)
20// Number of GPIO/aux channels per ADBMS6380 device.
21#define ADBMS6380_GPIO_COUNT (10)
22
23// PEC size in bytes for ADBMS6380 commands and data packets. Is 2 bytes for both PEC10 (data) and PEC15 (commands).
24#define ADBMS6380_PEC_SIZE (2)
25// Raw command size (bytes) for one module without PEC.
26#define ADBMS6380_COMMAND_RAW_SIZE (2)
27// Command packet size (bytes) for one module including PEC.
28#define ADBMS6380_COMMAND_PKT_SIZE (ADBMS6380_COMMAND_RAW_SIZE + ADBMS6380_PEC_SIZE)
29// Single data payload size (bytes) for one module without PEC.
30#define ADBMS6380_SINGLE_DATA_RAW_SIZE (6)
31// Single data packet size (bytes) for one module including PEC.
32#define ADBMS6380_SINGLE_DATA_PKT_SIZE (ADBMS6380_SINGLE_DATA_RAW_SIZE + ADBMS6380_PEC_SIZE)
33
34// Number of commands to read all 16 cell voltages (5 for 3 cells each, 1 for the last cell)
35// {RDCVA, RDCVB, RDCVC, RDCVD, RDCVE, RDCVF}
36#define ADBMS6380_RDCV_CMD_COUNT (6)
37
38// Number of commands to read all 10 GPIO voltages (3 for 3 GPIOs each, 1 for the last GPIO)
39// {RDAUXA, RDAUXB, RDAUXC, RDAUXD}
40#define ADBMS6380_RDAUX_CMD_COUNT (4)
41
47#define ADBMS6380_WAKE_DELAY_MS (1)
48
55typedef enum {
56 ADBMS6380_READ_SUCCESS = 0,
57 ADBMS6380_READ_PEC_FAILURE,
58 ADBMS6380_READ_SPI_FAILURE,
60
73
82void adbms6380_wake(SPI_InitConfig_t *spi, size_t module_count);
83
90uint16_t adbms6380_get_threshold_voltage_cfg(float threshold_voltage);
98int16_t adbms6380_extract_i16(uint8_t *data, int idx);
105float adbms6380_raw_to_v(int16_t raw);
106
117void adbms6380_adcv(uint8_t output_cmd[ADBMS6380_COMMAND_RAW_SIZE],
118 bool rd,
119 bool cont,
120 bool dcp,
121 bool rstf,
122 uint8_t ow);
131void adbms6380_adsv(uint8_t output_cmd[ADBMS6380_COMMAND_RAW_SIZE],
132 bool cont,
133 bool dcp,
134 uint8_t ow);
135
144void adbms6380_adax(uint8_t output_cmd[ADBMS6380_COMMAND_RAW_SIZE], bool ow, bool pup, uint8_t ch);
145
152void adbms6380_prepare_command(strbuf_t *output_buffer,
153 const uint8_t command[ADBMS6380_COMMAND_RAW_SIZE]);
160void adbms6380_prepare_data_packet(strbuf_t *output_buffer,
161 const uint8_t data[ADBMS6380_SINGLE_DATA_RAW_SIZE]);
162
172void adbms6380_calculate_cfg_rega(uint8_t output_cfg_rega[ADBMS6380_SINGLE_DATA_RAW_SIZE],
173 bool refon,
174 uint8_t cth);
185void adbms6380_calculate_cfg_regb(uint8_t output_cfg_regb[ADBMS6380_SINGLE_DATA_RAW_SIZE],
186 float overvoltage_threshold,
187 float undervoltage_threshold,
188 const bool is_discharging[ADBMS6380_CELL_COUNT]);
189
199bool adbms6380_check_data_pec(const uint8_t *rx_bytes, size_t rx_len);
200
215 size_t module_count,
216 const uint8_t cmd_buffer[ADBMS6380_COMMAND_PKT_SIZE],
217 uint8_t *rx_buffer,
218 size_t rx_length_per_module);
232 size_t module_count,
233 const uint8_t cmd_buffer[ADBMS6380_COMMAND_PKT_SIZE],
234 uint8_t *rx_buffer);
235
251 size_t max_retries,
252 size_t module_count,
253 const uint8_t cmd_buffer[ADBMS6380_COMMAND_PKT_SIZE],
254 uint8_t *rx_buffer);
255
277 strbuf_t *cmd_buffer,
278 uint8_t *rx_buffer,
279 float **cell_voltages,
280 int16_t **cell_voltages_raw,
281 bool pec_error_flags[ADBMS6380_RDCV_CMD_COUNT],
282 size_t module_count,
283 size_t max_retries_on_pec_failure);
304 strbuf_t *cmd_buffer,
305 uint8_t *rx_buffer,
306 float **gpio_voltages,
307 bool pec_error_flags[ADBMS6380_RDAUX_CMD_COUNT],
308 size_t module_count,
309 size_t max_retries_on_pec_failure);
310
311// Other adbms6380 related function declarations can go here
312
313#endif // _ADBMS6380_H_
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
Definition strbuf.h:15