88#ifndef _BMS_H_
99#define _BMS_H_
1010
11- #include <stdint.h>
1211#include <stddef.h>
12+ #include <stdint.h>
1313
14+ #include "adbms6380.h"
1415#include "common/phal/spi.h"
1516#include "common/strbuf/strbuf.h"
1617
17- #include "adbms6380.h"
18-
19-
20- #define ADBMS_MODULE_COUNT (8)
18+ #define ADBMS_MODULE_COUNT (8)
2119
2220// Max SPI TX is a command + all the data packets for all the modules
23- #define ADBMS_SPI_TX_BUFFER_SIZE (ADBMS6380_COMMAND_PKT_SIZE + (ADBMS_MODULE_COUNT * ADBMS6380_SINGLE_DATA_PKT_SIZE))
21+ #define ADBMS_SPI_TX_BUFFER_SIZE \
22+ (ADBMS6380_COMMAND_PKT_SIZE + (ADBMS_MODULE_COUNT * ADBMS6380_SINGLE_DATA_PKT_SIZE))
2423// MAX SPI RX is bounded by reading all cell voltages from all modules
2524#define ADBMS_SPI_RX_BUFFER_SIZE (ADBMS6380_RDCVALL_DATA_PKT_SIZE * ADBMS_MODULE_COUNT)
2625
27- #define ADBMS_REFON (true) // REGA
28- #define ADBMS_CTH (0b110) // REGA - 25.05 mV
26+ #define ADBMS_REFON (true) // REGA
27+ #define ADBMS_CTH (0b110) // REGA - 25.05 mV
2928#define ADBMS_OV_THRESHOLD (4.2f) // REGB - in volts
3029#define ADBMS_UV_THRESHOLD (3.0f) // REGB - in volts
3130#define ADBMS_RD (false) // ADCV
3433#define ADBMS_RSTF (true) // ADCV
3534#define ADBMS_OW (0b00) // ADCV/ADSV
3635
37-
3836/**
3937 * @brief ADBMS driver connection/operation state.
4038 */
4139typedef enum {
42- /** Driver is idle; not connected or awaiting re-connection. */
43- ADBMS_STATE_IDLE = 0 ,
44- /** Driver is connected; periodic reads/balancing are active. */
45- ADBMS_STATE_CONNECTED ,
40+ /** Driver is idle; not connected or awaiting re-connection. */
41+ ADBMS_STATE_IDLE = 0 ,
42+ /** Driver is connected; periodic reads/balancing are active. */
43+ ADBMS_STATE_CONNECTED ,
4644} ADBMS_state_t ;
4745
48-
4946/**
5047 * @brief Per-ADBMS module measurements, config, and error flags.
5148 */
5249typedef struct {
53- /** Latest cell voltage readings for this module (volts). */
54- float cell_voltages [ADBMS6380_CELL_COUNT ];
55- /** Minimum cell voltage within this module (volts). */
56- float min_voltage ;
57- /** Maximum cell voltage within this module (volts). */
58- float max_voltage ;
59- /** Average cell voltage within this module (volts). */
60- float avg_voltage ;
61- /** Sum of cell voltages within this module (volts). */
62- float sum_voltage ;
63- /** Latest thermistor readings mapped to GPIOs (degrees Celsius). */
64- float thermistors [ADBMS6380_GPIO_COUNT ];
65- //! TODO: do we also want min/max/avg for thermistors?
66- /** Per-cell discharge enable flags used for balancing. Set by BMS, not higher level logic. */
67- bool is_discharging [ADBMS6380_CELL_COUNT ];
68-
69- /** Cached REGA bytes written to the device. Used to compare against read-back data. */
70- uint8_t rega [ADBMS6380_SINGLE_DATA_RAW_SIZE ];
71- /** Cached REGB bytes written to the device. Used to compare against read-back data. */
72- uint8_t regb [ADBMS6380_SINGLE_DATA_RAW_SIZE ];
73-
74- /** Set if a read-back REGA does not match cached REGA. */
75- bool err_rega_mismatch ;
76- /** Set if a read-back REGB does not match cached REGB. */
77- bool err_regb_mismatch ;
50+ /** Latest cell voltage readings for this module (volts). */
51+ float cell_voltages [ADBMS6380_CELL_COUNT ];
52+ /** Minimum cell voltage within this module (volts). */
53+ float min_voltage ;
54+ /** Maximum cell voltage within this module (volts). */
55+ float max_voltage ;
56+ /** Average cell voltage within this module (volts). */
57+ float avg_voltage ;
58+ /** Sum of cell voltages within this module (volts). */
59+ float sum_voltage ;
60+ /** Latest thermistor readings mapped to GPIOs (degrees Celsius). */
61+ float thermistors [ADBMS6380_GPIO_COUNT ];
62+ //! TODO: do we also want min/max/avg for thermistors?
63+ /** Per-cell discharge enable flags used for balancing. Set by BMS, not higher level logic. */
64+ bool is_discharging [ADBMS6380_CELL_COUNT ];
65+
66+ /** Cached REGA bytes written to the device. Used to compare against read-back data. */
67+ uint8_t rega [ADBMS6380_SINGLE_DATA_RAW_SIZE ];
68+ /** Cached REGB bytes written to the device. Used to compare against read-back data. */
69+ uint8_t regb [ADBMS6380_SINGLE_DATA_RAW_SIZE ];
70+
71+ /** Set if a read-back REGA does not match cached REGA. */
72+ bool err_rega_mismatch ;
73+ /** Set if a read-back REGB does not match cached REGB. */
74+ bool err_regb_mismatch ;
7875} ADBMS_module_t ;
7976
8077/**
8178 * @brief Top-level ADBMS driver state, I/O buffers, and aggregated measurements.
8279 */
8380typedef struct {
84- /** Current driver state. */
85- ADBMS_state_t state ;
86-
87- /** Per-module state and measurements. */
88- ADBMS_module_t modules [ADBMS_MODULE_COUNT ];
89-
90- /** Minimum cell voltage across all modules (volts). */
91- float min_voltage ;
92- /** Maximum cell voltage across all modules (volts). */
93- float max_voltage ;
94- /** Average cell voltage across all modules (volts). */
95- float avg_voltage ;
96- /** Sum of cell voltages across all modules (volts). */
97- float sum_voltage ;
98-
99- /** True if cell discharge balancing is permitted by higher-level logic. */
100- bool is_discharge_enabled ;
101-
102- /** SPI instance used for ADBMS communication (CS control is manual by ADBMS6380 driver). */
103- SPI_InitConfig_t * spi ;
104- /** Scratch TX buffer wrapper for command/data packets. */
105- strbuf_t tx_strbuf ;
106- /** RX buffer for multi-module readouts. */
107- uint8_t rx_buf [ADBMS_SPI_RX_BUFFER_SIZE ];
108-
109- /** Set on any SPI transfer/read failure. Should be a terminal error. */
110- bool err_spi ;
111- /** Set when connect/initialization sequence fails. Retry logic up to higher-level logic. */
112- bool err_connect ;
113- /** Aggregated REGA mismatch flag across modules. */
114- bool err_rega_mismatch ;
115- /** Aggregated REGB mismatch flag across modules. */
116- bool err_regb_mismatch ;
81+ /** Current driver state. */
82+ ADBMS_state_t state ;
83+
84+ /** Per-module state and measurements. */
85+ ADBMS_module_t modules [ADBMS_MODULE_COUNT ];
86+
87+ /** Minimum cell voltage across all modules (volts). */
88+ float min_voltage ;
89+ /** Maximum cell voltage across all modules (volts). */
90+ float max_voltage ;
91+ /** Average cell voltage across all modules (volts). */
92+ float avg_voltage ;
93+ /** Sum of cell voltages across all modules (volts). */
94+ float sum_voltage ;
95+
96+ /** True if cell discharge balancing is permitted by higher-level logic. */
97+ bool is_discharge_enabled ;
98+
99+ /** SPI instance used for ADBMS communication (CS control is manual by ADBMS6380 driver). */
100+ SPI_InitConfig_t * spi ;
101+ /** Scratch TX buffer wrapper for command/data packets. */
102+ strbuf_t tx_strbuf ;
103+ /** RX buffer for multi-module readouts. */
104+ uint8_t rx_buf [ADBMS_SPI_RX_BUFFER_SIZE ];
105+
106+ /** Set on any SPI transfer/read failure. Should be a terminal error. */
107+ bool err_spi ;
108+ /** Set when connect/initialization sequence fails. Retry logic up to higher-level logic. */
109+ bool err_connect ;
110+ /** Aggregated REGA mismatch flag across modules. */
111+ bool err_rega_mismatch ;
112+ /** Aggregated REGB mismatch flag across modules. */
113+ bool err_regb_mismatch ;
117114} ADBMS_bms_t ;
118115
119-
120116/**
121117 * @brief Initialize the ADBMS driver instance and TX buffer.
122118 *
@@ -127,15 +123,15 @@ typedef struct {
127123 * @param spi SPI configuration used for ADBMS transactions.
128124 * @param tx_buf Backing buffer for TX command/data packets.
129125 */
130- void adbms_init (ADBMS_bms_t * bms , SPI_InitConfig_t * spi , uint8_t * tx_buf );
126+ void adbms_init (ADBMS_bms_t * bms , SPI_InitConfig_t * spi , uint8_t * tx_buf );
131127
132128/**
133129 * @brief Calculate and write REGA configuration to all modules.
134130 *
135131 * @param bms Pointer to driver state.
136132 * @return True on successful SPI transfer, false on failure.
137133 */
138- bool adbms_write_rega (ADBMS_bms_t * bms );
134+ bool adbms_write_rega (ADBMS_bms_t * bms );
139135/**
140136 * @brief Calculate and write REGB configuration to all modules.
141137 *
@@ -145,7 +141,7 @@ bool adbms_write_rega(ADBMS_bms_t* bms);
145141 * @param bms Pointer to driver state.
146142 * @return True on successful SPI transfer, false on failure.
147143 */
148- bool adbms_write_regb (ADBMS_bms_t * bms );
144+ bool adbms_write_regb (ADBMS_bms_t * bms );
149145/**
150146 * @brief Read back REGA and compare with cached configuration.
151147 *
@@ -155,7 +151,7 @@ bool adbms_write_regb(ADBMS_bms_t* bms);
155151 * @param bms Pointer to driver state.
156152 * @return False on SPI failure and when there is a mismatch; true otherwise.
157153 */
158- bool adbms_read_and_check_rega (ADBMS_bms_t * bms );
154+ bool adbms_read_and_check_rega (ADBMS_bms_t * bms );
159155/**
160156 * @brief Read back REGB and compare with cached configuration.
161157 *
@@ -165,7 +161,7 @@ bool adbms_read_and_check_rega(ADBMS_bms_t* bms);
165161 * @param bms Pointer to driver state.
166162 * @return False on SPI failure and when there is a mismatch; true otherwise.
167163 */
168- bool adbms_read_and_check_regb (ADBMS_bms_t * bms );
164+ bool adbms_read_and_check_regb (ADBMS_bms_t * bms );
169165
170166/**
171167 * @brief Perform the connect/bring-up sequence for all modules.
@@ -175,7 +171,7 @@ bool adbms_read_and_check_regb(ADBMS_bms_t* bms);
175171 *
176172 * @param bms Pointer to driver state.
177173 */
178- void adbms_connect (ADBMS_bms_t * bms );
174+ void adbms_connect (ADBMS_bms_t * bms );
179175
180176/**
181177 * @brief Read all cell voltages and update module/pack statistics.
@@ -185,13 +181,13 @@ void adbms_connect(ADBMS_bms_t* bms);
185181 *
186182 * @param bms Pointer to driver state.
187183 */
188- void adbms_read_cells (ADBMS_bms_t * bms );
184+ void adbms_read_cells (ADBMS_bms_t * bms );
189185/**
190186 * @brief Read all GPIO/thermistor voltages.
191187 *
192188 * @param bms Pointer to driver state.
193189 */
194- void adbms_read_therms (ADBMS_bms_t * bms );
190+ void adbms_read_therms (ADBMS_bms_t * bms );
195191
196192/**
197193 * @brief Compute per-cell discharge flags based on pack voltage spread.
@@ -204,7 +200,7 @@ void adbms_read_therms(ADBMS_bms_t* bms);
204200 * @param min_voltage Minimum pack voltage required to allow balancing.
205201 * @param min_delta Voltage delta above minimum to start discharging.
206202 */
207- void adbms_calculate_balance_cells (ADBMS_bms_t * bms , float min_voltage , float min_delta );
203+ void adbms_calculate_balance_cells (ADBMS_bms_t * bms , float min_voltage , float min_delta );
208204/**
209205 * @brief Compute discharge flags and write REGB to apply balancing.
210206 *
@@ -214,7 +210,7 @@ void adbms_calculate_balance_cells(ADBMS_bms_t* bms, float min_voltage, float mi
214210 * @param min_voltage Minimum pack voltage required to allow balancing.
215211 * @param min_delta Voltage delta above minimum to start discharging.
216212 */
217- void adbms_balance_and_update_regb (ADBMS_bms_t * bms , float min_voltage , float min_delta );
213+ void adbms_balance_and_update_regb (ADBMS_bms_t * bms , float min_voltage , float min_delta );
218214
219215/**
220216 * @brief Periodic service routine for connection, measurements, and balancing.
@@ -226,7 +222,6 @@ void adbms_balance_and_update_regb(ADBMS_bms_t* bms, float min_voltage, float mi
226222 * @param min_voltage_for_balance Minimum pack voltage required to allow balancing.
227223 * @param min_delta_for_balance Voltage delta above minimum to start discharging.
228224 */
229- void adbms_periodic (ADBMS_bms_t * bms , float min_voltage_for_balance , float min_delta_for_balance );
230-
225+ void adbms_periodic (ADBMS_bms_t * bms , float min_voltage_for_balance , float min_delta_for_balance );
231226
232227#endif // _BMS_H_
0 commit comments