|
PER Firmware
|
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 "common/freertos/freertos.h"#include "common/phal/gpio.h"#include "common/phal/spi.h"#include "pec.h"Functions | |
| void | adbms6380_set_cs_low (SPI_InitConfig_t *spi) |
| Drive the ADBMS CS line low. | |
| void | adbms6380_set_cs_high (SPI_InitConfig_t *spi) |
| Drive the ADBMS CS line to high. | |
| void | adbms6380_wake (SPI_InitConfig_t *spi, size_t module_count) |
| Wake a daisy-chained set of ADBMS devices. | |
| uint16_t | adbms6380_get_threshold_voltage_cfg (float threshold_voltage) |
| Convert a voltage threshold (V) into 12-bit REG_B threshold encoding. | |
| int16_t | adbms6380_extract_i16 (uint8_t *data, int idx) |
| Extract a little-endian 16-bit signed value from a byte buffer. | |
| float | adbms6380_raw_to_v (int16_t raw) |
| Convert raw voltage measurement from the ADBMS6380 to volts. | |
| 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. | |
| void | adbms6380_adsv (uint8_t output_cmd[ADBMS6380_COMMAND_RAW_SIZE], bool cont, bool dcp, uint8_t ow) |
| Build ADSV command bytes. | |
| void | adbms6380_adax (uint8_t output_cmd[ADBMS6380_COMMAND_RAW_SIZE], bool ow, bool pup, uint8_t ch) |
| Build ADAX (aux) command bytes. | |
| 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. | |
| 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. | |
| 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. | |
| 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. | |
| bool | adbms6380_check_data_pec (const uint8_t *rx_bytes, size_t rx_len) |
| Verify the PEC of received data. | |
| 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. | |
| 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. | |
| 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. | |
| 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. | |
| 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. | |
Low level BMS driver specific to ADBMS6380 chip.
| void adbms6380_adax | ( | uint8_t | output_cmd[ADBMS6380_COMMAND_RAW_SIZE], |
| bool | ow, | ||
| bool | pup, | ||
| uint8_t | ch ) |
Build ADAX (aux) command bytes.
| output_cmd | Output buffer for 2-byte command. |
| ow | Open-wire detection enable. |
| pup | Pull-up enable for open-wire detection. |
| ch | Channel selection (5-bit). 0 for all. |
| 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.
| output_cmd | Output buffer for 2-byte command. |
| rd | Redundant mode. |
| cont | Continuous conversion enable. |
| dcp | Discharge permitted during conversion (only affects PWM balancing, not DCC). |
| rstf | Reset filter. |
| ow | Open-wire setting (2-bit). |
| void adbms6380_adsv | ( | uint8_t | output_cmd[ADBMS6380_COMMAND_RAW_SIZE], |
| bool | cont, | ||
| bool | dcp, | ||
| uint8_t | ow ) |
Build ADSV command bytes.
| output_cmd | Output buffer for 2-byte command. |
| cont | Continuous conversion enable. |
| dcp | Discharge permitted during conversion (only affects PWM balancing, not DCC). |
| ow | Open-wire setting (2-bit). |
| 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.
Populates GPIO pull-downs, reference behavior, and comparator threshold.
| output_cfg_rega | Output buffer for REG_A configuration (6 bytes). |
| refon | 1 = reference remains powered up until watchdog timeout. |
| cth | C-ADC vs. S-ADC comparison voltage threshold. |
| 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.
Encodes VOV/VUV thresholds and discharge control bits.
| output_cfg_regb | Output buffer for REG_B configuration (6 bytes). |
| overvoltage_threshold | Overvoltage threshold in volts. |
| undervoltage_threshold | Undervoltage threshold in volts. |
| is_discharging | Per-cell discharge enable flags. |
| bool adbms6380_check_data_pec | ( | const uint8_t * | rx_bytes, |
| size_t | rx_len ) |
Verify the PEC of received data.
| rx_bytes | Pointer to received bytes from a module. PEC10 must follow the raw data bytes. |
| rx_len | Length of the recieved data from a module in bytes (including PEC, so the raw data is actually rx_len - ADBMS6380_PEC_SIZE). |
| int16_t adbms6380_extract_i16 | ( | uint8_t * | data, |
| int | idx ) |
Extract a little-endian 16-bit signed value from a byte buffer.
| data | Pointer to byte buffer containing 16-bit words. |
| idx | Index of the 16-bit word to extract. |
| uint16_t adbms6380_get_threshold_voltage_cfg | ( | float | threshold_voltage | ) |
Convert a voltage threshold (V) into 12-bit REG_B threshold encoding.
| threshold_voltage | Threshold voltage in volts. |
| 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.
| output_buffer | Buffer to append into. |
| command | 2-byte command payload. |
| 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.
| output_buffer | Buffer to append into. |
| data | 6-byte data payload. |
| float adbms6380_raw_to_v | ( | int16_t | raw | ) |
Convert raw voltage measurement from the ADBMS6380 to volts.
| raw | Raw code from ADBMS. |
| 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.
Sends the command to the daisy chain, then reads rx_length bytes into rx_buffer. Data is returned in module order (0..N-1).
| spi | SPI configuration used for the transfer. |
| module_count | Number of modules in the daisy chain. |
| cmd_buffer | Command buffer including PEC. |
| rx_buffer | Output buffer for received bytes. |
| rx_length_per_module | Number of bytes expected from each module (including PEC). |
| 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.
Issues the RDCVALL command, converts raw values to volts, and fills the provided per-module cell voltage arrays. If any of the commands fail with a PEC error (even with the retries), the successfully read cell voltages will still be updated but the corresponding PEC error flag will be set.
| spi | SPI configuration used for the transfer. |
| cmd_buffer | Scratch buffer for command + PEC. |
| rx_buffer | RX buffer for raw bytes. |
| cell_voltages | Per-module arrays of cell voltages to populate. |
| cell_voltages_raw | Per-module arrays of raw cell voltage codes to populate. |
| pec_error_flags | Output array of flags indicating which command (if any) had PEC failures after retries are exhausted. |
| module_count | Number of modules in the daisy chain. |
| max_retries_on_pec_failure | Number of attempts to retry a read if a PEC failure is detected before giving up. |
| 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.
Convenience wrapper around adbms6380_read() with ADBMS6380_SINGLE_DATA_PKT_SIZE as the expected response length per module.
| spi | SPI configuration used for the transfer. |
| module_count | Number of modules in the daisy chain. |
| cmd_buffer | Command buffer including PEC. |
| rx_buffer | Output buffer for received bytes. |
| 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.
Convenience wrapper around adbms6380_read_data() that retries the read up to a fixed number of attempts if a PEC failure is detected.
| spi | SPI configuration used for the transfer. |
| max_retries | Maximum number of read attempts if PEC failures occur. |
| module_count | Number of modules in the daisy chain. |
| cmd_buffer | Command buffer including PEC. |
| rx_buffer | Output buffer for received bytes. |
| 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.
Issues RDAUXA/B/C/D commands, converts raw values to volts, and fills the provided per-module GPIO voltage arrays. If any of the commands fail with a PEC error (even with the retries), the successfully read GPIO voltages will still be updated but the corresponding PEC error flag will be set.
| spi | SPI configuration used for the transfer. |
| cmd_buffer | Scratch buffer for command + PEC. |
| rx_buffer | RX buffer for raw bytes. |
| gpio_voltages | Per-module arrays of GPIO voltages to populate. |
| pec_error_flags | Output array of flags indicating which command (if any) had PEC failures after retries are exhausted. |
| module_count | Number of modules in the daisy chain. |
| max_retries_on_pec_failure | Number of attempts to retry a read if a PEC failure is detected before giving up. |
| void adbms6380_set_cs_high | ( | SPI_InitConfig_t * | spi | ) |
Drive the ADBMS CS line to high.
| spi | SPI configuration containing the CS GPIO. |
| void adbms6380_set_cs_low | ( | SPI_InitConfig_t * | spi | ) |
Drive the ADBMS CS line low.
| spi | SPI configuration containing the CS GPIO. |
| void adbms6380_wake | ( | SPI_InitConfig_t * | spi, |
| size_t | module_count ) |
Wake a daisy-chained set of ADBMS devices.
Toggles CS with blocking delays to bring each module out of sleep.
| spi | SPI configuration containing the CS GPIO. |
| module_count | Number of modules in the daisy chain. |