foxBMS  1.2.1
The foxBMS Battery Management System API Documentation
mxm_1785x.h
Go to the documentation of this file.
1 /**
2  *
3  * @copyright © 2010 - 2021, Fraunhofer-Gesellschaft zur Foerderung der angewandten Forschung e.V.
4  * All rights reserved.
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright notice, this
12  * list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright notice,
15  * this list of conditions and the following disclaimer in the documentation
16  * and/or other materials provided with the distribution.
17  *
18  * 3. Neither the name of the copyright holder nor the names of its
19  * contributors may be used to endorse or promote products derived from
20  * this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
25  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  * We kindly request you to use one or more of the following phrases to refer to
34  * foxBMS in your hardware, software, documentation or advertising materials:
35  *
36  * - ″This product uses parts of foxBMS®″
37  * - ″This product includes parts of foxBMS®″
38  * - ″This product is derived from foxBMS®″
39  *
40  */
41 
42 /**
43  * @file mxm_1785x.h
44  * @author foxBMS Team
45  * @date 2019-01-15 (date of creation)
46  * @updated 2021-12-06 (date of last update)
47  * @ingroup DRIVERS
48  * @prefix MXM
49  *
50  * @brief Headers for the driver for the MAX17841B ASCI and MAX1785x monitoring chip
51  *
52  * @details def
53  *
54  */
55 
56 /**
57  * @brief Battery monitoring driver for MAX1785x battery monitoring ICs.
58  *
59  * This module supplies a driver for the Battery Monitoring ICs of the
60  * MAX1785x-family by Maxim Integrated.
61  *
62  * Entry point for the module is the function #MXM_Tick() in mxm_afe.c. It handles
63  * the measurement flow and the coordination of the underlying state-machines.
64  * Below this layer two state-machines are implemented.
65  *
66  * The state-machine in mxm_battery_management.c is executed with the
67  * #MXM_5XStateMachine()-function.
68  * This state-machine exposes commands of the Maxim Battery Management Protocol
69  * to the upper layers. Below, it translates these commands into state-changes
70  * for the underlying state-machine.
71  * This state-machine is implemented in mxm_17841b.c and executed with
72  * #MXM_41BStateMachine(). It handles the register- and buffer-transactions
73  * required for the MAX17841B communication interface (Maxim calls
74  * this chip ASCI).
75  *
76  */
77 
78 #ifndef FOXBMS__MXM_1785X_H_
79 #define FOXBMS__MXM_1785X_H_
80 
81 /*========== Includes =======================================================*/
82 #include "database_cfg.h"
83 #include "mxm_cfg.h"
84 
85 #include "mxm_17841b.h"
86 #include "mxm_1785x_tools.h"
87 #include "mxm_basic_defines.h"
88 #include "mxm_battery_management.h"
89 #include "mxm_crc8.h"
90 
91 /*========== Macros and Definitions =========================================*/
92 
93 /** threshold of iterations after which a diagnostic cycle is entered by the state-machine */
94 #define MXM_THRESHOLD_DIAGNOSTIC_AFTER_CYCLES (10u)
95 
96 /** length of voltage-read array */
97 #define MXM_VOLTAGE_READ_ARRAY_LENGTH (MXM_MAXIMUM_NR_OF_CELLS_PER_MODULE + 3u)
98 
99 /** number of cells that are in the LSB register of i.e. pin open diagnostic */
100 #define MXM_CELLS_IN_LSB (8u)
101 
102 /** unipolar full-scale reference value for cell voltage in mV */
103 #define MXM_REF_UNIPOLAR_CELL_mV (5000u)
104 
105 /*========== Extern Constant and Variable Declarations ======================*/
106 
107 /*========== Extern Function Prototypes =====================================*/
108 /**
109  * @brief Main state-machine implementation
110  * @details This state-machine handles the main state of the driver.
111  * It runs the self-check, initializes communication and then
112  * transitions into the operation state in which it
113  * executes the state-machine described in
114  * #MXM_StateMachineOperation().
115  * @param[in,out] pInstance used as both input and output (stores
116  * state-information, requests and intermediate
117  * values)
118  */
119 extern void MXM_StateMachine(MXM_MONITORING_INSTANCE_s *pInstance);
120 
121 /**
122  * @brief Initializes the state structs with default values
123  * @details This function is called through the startup of the driver in order
124  * to ensure proper default values.
125  * @param[out] pBalancingInstance instance of the balancing state struct that shall be initialized
126  * @param[out] pMonitoringInstance instance of the monitoring state struct that shall be initialized
127  */
128 extern void MXM_InitializeStateStruct(
129  MXM_BALANCING_STATE_s *pBalancingInstance,
130  MXM_MONITORING_INSTANCE_s *pMonitoringInstance);
131 
132 /**
133  * @brief Function that checks if the error counter can be reset
134  * @param[in,out] pInstance pointer to the state struct
135  */
137 
138 /**
139  * @brief Fill the balancing datastructure
140  * @details This function fills the data-structure that describes
141  * which balancing channels of the monitoring ICs should be
142  * activated.
143  * @param[in,out] pBalancingInstance pointer to the balancing state
144  * @return #STD_NOT_OK in case of invalid access
145  */
147 
148 /**
149  * @brief Handle the statemachine-transactions for a WRITEALL
150  * @details Before calling this function, update the command buffer of
151  * the state-variable. Then call this function and pass on the
152  * state-variable and the next state. The function will
153  * handle the communication with the lower state-machine and
154  * will transition into the next state, if the command has
155  * been sent successfully.
156  * @param[in,out] pInstance pointer to instance of the mxm
157  * monitoring state-machine
158  * @param[in] nextState state that should be entered upon successful
159  * completion
160  */
161 extern void MXM_HandleStateWriteall(
162  MXM_MONITORING_INSTANCE_s *pInstance,
164 
165 /**
166  * @brief Handle the statemachine-transactions for a READALL
167  * @details Call this function and pass on the state-variable, the
168  * register to be read and the next state. The function will
169  * handle the communication with the lower state-machine and
170  * will transition into the next state, if the command has
171  * been sent successfully. Moreover it will return true when
172  * transitioning. The return value has to be checked and used
173  * to execute additional code if necessary.
174  * @param[in,out] pInstance pointer to instance of the mxm
175  * monitoring state-machine
176  * @param[in] registerName register that should be read
177  * @param[in] nextState state that should be entered upon successful
178  * completion
179  * @return true when the state has been handled, false otherwise, use
180  * this to execute additional code when the message has been
181  * read.
182  */
184  MXM_MONITORING_INSTANCE_s *pInstance,
185  MXM_REG_NAME_e registerName,
187 
188 /**
189  * @brief Processes the retrieved information on openwire
190  * @details Parses through a retrieved RX buffer and writes into the
191  * database.
192  * @param[in,out] kpkInstance pointer to instance of the Maxim monitoring
193  * state-machine
194  * @return returns the return value of the database write function #DATA_Write_1_DataBlock()
195  */
196 extern STD_RETURN_TYPE_e MXM_ProcessOpenWire(const MXM_MONITORING_INSTANCE_s *const kpkInstance);
197 
198 /**
199  * @brief This error handler is used as a last resort and tries a reset of the complete driver
200  * @details A reset is done by setting the relevant flag in the state struct
201  * and waiting for the reset to occur (done by AFE on next tick).
202  * Before this last resort measure is taken, a error counter has to
203  * be counted up.
204  * @param[in,out] pInstance pointer to the state struct in order to write the reset flag
205  * @param[in] immediateReset if set to true, a reset will be requested independently of the error counter
206  */
207 extern void MXM_ErrorHandlerReset(MXM_MONITORING_INSTANCE_s *pInstance, bool immediateReset);
208 
209 /**
210  * @brief State-Machine implementation for operation state
211  * @details This state-machine contains the "program" with which the
212  * connected monitoring satellites are controlled. It is
213  * entered by #MXM_StateMachine() once the daisy-chain has
214  * been initialized and is in operation state.
215  * @param[in,out] pState used as both input and output (stores
216  * state-information, requests and intermediate values)
217  */
219 
220 /**
221  * @brief Encapsulation for reading voltages from a register
222  * @details This function encapsulates the request of state-changes and
223  * following conversion for the reading of an arbitrary
224  * measurement voltage of the daisy-chain. Its parameters are
225  * a variable for tracking the state of the underlying
226  * state-machines and the register address that has to be
227  * queried. It returns whether the action has been successful
228  * or not.
229  * In order to obtain all cell voltages this function has to
230  * be called for every relevant register address.
231  * @param[in,out] pState pointer to the state-machine struct
232  * @param[in] regAddress register address that shall be queried
233  * @return current state of the action:
234  * - MXM_MON_STATE_PASS upon completion
235  * - MXM_MON_STATE_PENDING as long as the action is
236  * ongoing
237  * - MXM_MON_STATE_FAIL if the function failed and could
238  * not recover on its own
239  */
242 
243 /**
244  * @brief Copies measured voltage data into the database.
245  * @details This function copies the acquired voltage data from
246  * #MXM_MONITORING_INSTANCE_s::localVoltages into the database-struct
247  * #mxm_tableCellVoltages and copies this struct into the database.
248  * This action is required due to different data layouts. This driver
249  * always stores its cell-voltages in an array with 14*32 = 448 cells
250  * in order to reduce the amount of different configurations and
251  * variants.
252  *
253  * This function maps these values into the database-struct which
254  * scales with the number of connected cells and monitoring ICs.
255  * @param[in] kpkInstance pointer to the #MXM_MONITORING_INSTANCE_s struct
256  * @return #STD_OK if the action was successful or #STD_NOT_OK otherwise
257  */
259 
260 /**
261  * @brief returns the model ID of the daisy chain
262  * @details this function is to be implemented by the driver in order to
263  * tell the rest of the driver which model is used. Currently, this
264  * implementation does not support mixed model daisy-chains.
265  * @returns model ID of daisy-chain
266  */
268 
269 /*========== Externalized Static Functions Prototypes (Unit Test) ===========*/
270 #ifdef UNITY_UNIT_TEST
271 extern STD_RETURN_TYPE_e TEST_MXM_ParseVoltageReadallTest(MXM_MONITORING_INSTANCE_s *pInstance);
272 extern STD_RETURN_TYPE_e TEST_MXM_ParseVoltagesIntoDB(MXM_MONITORING_INSTANCE_s *pInstance);
273 extern STD_RETURN_TYPE_e TEST_MXM_ParseVoltageReadall(
274  uint8_t *voltRxBuffer,
275  uint16_t voltRxBufferLength,
276  MXM_DATA_STORAGE_s *datastorage,
277  MXM_CONVERSION_TYPE_e conversionType);
278 #endif
279 
280 #endif /* FOXBMS__MXM_1785X_H_ */
Database configuration header.
enum STD_RETURN_TYPE STD_RETURN_TYPE_e
#define must_check_return
Allows functions to generate warnings in GCC for unused returns.
Definition: general.h:101
Headers for the driver for the MAX17841B ASCI and MAX1785x monitoring chip.
void MXM_CheckIfErrorCounterCanBeReset(MXM_MONITORING_INSTANCE_s *pInstance)
Function that checks if the error counter can be reset.
Definition: mxm_1785x.c:923
void MXM_ErrorHandlerReset(MXM_MONITORING_INSTANCE_s *pInstance, bool immediateReset)
This error handler is used as a last resort and tries a reset of the complete driver.
Definition: mxm_1785x.c:936
MXM_MODEL_ID_e MXM_GetModelIdOfDaisyChain(void)
returns the model ID of the daisy chain
Definition: mxm_17852.c:90
STD_RETURN_TYPE_e MXM_ProcessOpenWire(const MXM_MONITORING_INSTANCE_s *const kpkInstance)
Processes the retrieved information on openwire.
Definition: mxm_1785x.c:1006
void MXM_StateMachineOperation(MXM_MONITORING_INSTANCE_s *pState)
State-Machine implementation for operation state.
Definition: mxm_17852.c:94
bool must_check_return MXM_HandleStateReadall(MXM_MONITORING_INSTANCE_s *pInstance, MXM_REG_NAME_e registerName, MXM_STATEMACHINE_OPERATION_STATES_e nextState)
Handle the statemachine-transactions for a READALL.
Definition: mxm_1785x.c:975
STD_RETURN_TYPE_e MXM_ParseVoltagesIntoDB(const MXM_MONITORING_INSTANCE_s *const kpkInstance)
Copies measured voltage data into the database.
Definition: mxm_1785x.c:1109
STD_RETURN_TYPE_e MXM_ConstructBalancingBuffer(MXM_BALANCING_STATE_s *pBalancingInstance)
Fill the balancing datastructure.
Definition: mxm_1785x.c:1051
MXM_MONITORING_STATE_e must_check_return MXM_MonGetVoltages(MXM_MONITORING_INSTANCE_s *pState, MXM_REG_NAME_e regAddress)
Encapsulation for reading voltages from a register.
Definition: mxm_1785x.c:1275
void MXM_StateMachine(MXM_MONITORING_INSTANCE_s *pInstance)
Main state-machine implementation.
Definition: mxm_1785x.c:1314
void MXM_InitializeStateStruct(MXM_BALANCING_STATE_s *pBalancingInstance, MXM_MONITORING_INSTANCE_s *pMonitoringInstance)
Initializes the state structs with default values.
Definition: mxm_1785x.c:1197
void MXM_HandleStateWriteall(MXM_MONITORING_INSTANCE_s *pInstance, MXM_STATEMACHINE_OPERATION_STATES_e nextState)
Handle the statemachine-transactions for a WRITEALL.
Definition: mxm_1785x.c:947
This is a collection of helper functions for the MAX1785x ICs.
MXM_MONITORING_STATE_e
MXM_STATEMACHINE_OPERATION_STATES_e
Basic defines for the complete Maxim driver.
MXM_MODEL_ID_e
Type of monitoring device.
MXM_CONVERSION_TYPE_e
Headers for the driver for the MAX17841B ASCI and MAX1785x monitoring chip.
Header for the configuration for the Maxim monitoring chip.
Headers for the CRC8 calculation for Maxim Integrated Monitoring devices.
MXM_REG_NAME_e
MAX1785x register names.