foxBMS  1.2.1
The foxBMS Battery Management System API Documentation
mxm_battery_management.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_battery_management.h
44  * @author foxBMS Team
45  * @date 2019-01-14 (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
51  * MAX1785x monitoring chip
52  *
53  * @details def
54  *
55  */
56 
57 #ifndef FOXBMS__MXM_BATTERY_MANAGEMENT_H_
58 #define FOXBMS__MXM_BATTERY_MANAGEMENT_H_
59 
60 /*========== Includes =======================================================*/
61 #include "mxm_cfg.h"
62 
63 #include "mxm_17841b.h"
64 #include "mxm_basic_defines.h"
65 #include "mxm_crc8.h"
66 #include "mxm_register_map.h"
67 
68 /*========== Macros and Definitions =========================================*/
69 
70 /** length of the command buffer */
71 #define COMMAND_BUFFER_LENGTH (10u)
72 
73 /** length of the rx buffer */
74 #define MXM_5X_RX_BUFFER_LEN 100u
75 
76 /** seed for helloall, should be 0x00u except for special edge cases */
77 #define HELLOALL_START_SEED 0x00u
78 /** seed for data check, should be 0x00u */
79 #define DATA_CHECK_BYTE_SEED 0x00u
80 
81 /**
82  * @brief Type for Battery Management Protocol commands.
83  * @details This type describes the hex-numbers of all Maxim Battery
84  * Management Protocol commands. For details see the
85  * data sheet of MAX1785x devices.
86  */
88 
89 /**
90  * @brief HELLOALL message
91  */
92 #define BATTERY_MANAGEMENT_HELLOALL ((MXM_BATTERY_MANAGEMENT_COMMAND_t)0x57u)
93 
94 /**
95  * @brief ALERTPACKET message
96  */
97 #define BATTERY_MANAGEMENT_ALERTPACKET ((MXM_BATTERY_MANAGEMENT_COMMAND_t)0x21u)
98 
99 /**
100  * @brief WRITEDEVICE message (write single register of a single device)
101  */
102 #define BATTERY_MANAGEMENT_WRITEDEVICE ((MXM_BATTERY_MANAGEMENT_COMMAND_t)0x04u)
103 
104 /**
105  * @brief WRITEALL message (write single register of all daisy-chain devices)
106  */
107 #define BATTERY_MANAGEMENT_WRITEALL ((MXM_BATTERY_MANAGEMENT_COMMAND_t)0x02u)
108 
109 /**
110  * @brief READDEVICE message (read single register of a single device)
111  */
112 #define BATTERY_MANAGEMENT_READDEVICE ((MXM_BATTERY_MANAGEMENT_COMMAND_t)0x05u)
113 
114 /**
115  * @brief READALL message (read single register of all daisy-chain devices)
116  */
117 #define BATTERY_MANAGEMENT_READALL ((MXM_BATTERY_MANAGEMENT_COMMAND_t)0x03u)
118 
119 /**
120  * @brief READBLOCK message (read block of registers of a single device)
121  */
122 #define BATTERY_MANAGEMENT_READBLOCK ((MXM_BATTERY_MANAGEMENT_COMMAND_t)0x06u)
123 
124 /**
125  * @brief DOWNHOST message (make the downhost writing)
126  * @details This feature is only useable on downhost line.
127  */
128 #define BATTERY_MANAGEMENT_DOWNHOST ((MXM_BATTERY_MANAGEMENT_COMMAND_t)0x09u)
129 
130 /**
131  * @brief UPHOST message (make the uphost writing, only useable on uphost line)
132  */
133 #define BATTERY_MANAGEMENT_UPHOST ((MXM_BATTERY_MANAGEMENT_COMMAND_t)0x08u)
134 
135 /**
136  * @brief Battery Management Protocol lengths of TX buffer
137  * @details Reference values for the lengths of the respective TX buffers
138  * for Battery Management Protocol messages.
139  */
140 
141 /**
142  * @brief length of a READALL command without stuffing bytes
143  */
144 #define BATTERY_MANAGEMENT_TX_LENGTH_READALL ((uint8_t)4u)
145 
146 /**
147  * @brief States of the Battery Management Protocol state-machine
148  */
149 typedef enum {
150  MXM_STATEMACH_5X_UNINITIALIZED, /*!< Uninitialized state that the state-machine starts in */
151  MXM_STATEMACH_5X_INIT, /*!< Initialization state */
152  MXM_STATEMACH_5X_41B_FMEA_CHECK, /*!< Requests a FMEA-check from the 41B-state-machine */
153  MXM_STATEMACH_5X_IDLE, /*!< Idle state, transitions are possible in this state */
154  MXM_STATEMACH_5X_WRITEALL, /*!< WRITEALL state, sends a WRITEALL to the daisy-chain */
155  MXM_STATEMACH_5X_WRITE_DEVICE, /*!< WRITEDEVICE state, sends a WRITEDEVICE to a specific address */
156  MXM_STATEMACH_5X_READALL, /*!< READALL state, sends a READALL to the daisy-chain */
157  MXM_STATEMACH_5X_MAXSTATE, /*!< Highest state */
159 
160 /**
161  * @brief Sub-states of the Battery Management Protocol state-machine
162  */
163 typedef enum {
164  MXM_5X_INIT_41B_INIT, /*!< initialization of the MAX17841 bridge IC */
165  MXM_5X_INIT_41B_GET_VERSION, /*!< retrieves the version of the bridge IC */
166  /** waits for the slave devices to be reset (due to disabled preambles) */
168  /** substate for the initialization, enable RX interrupt flags */
170  /** substate for the wake-up routine, clear the receive buffer in the
171  * bridge IC */
173  /** substate for the wake-up routine, enable the sending of preambles in
174  * order to wake up the daisy-chain devices */
176  MXM_5X_INIT_ENABLE_KEEP_ALIVE, /*!< enables keep alive mode (which will take over after en preambles is off) */
177  /** substate for the wake-up routine, wait for the daisy-chain devices to
178  * return an answer, resulting in status RX busy of the bridge IC */
180  /** substate for the wake-up routine, disable the sending of preambles */
182  /** substate for the wake-up routine, wait for the RX transmission to
183  * finish */
185  /** substate for the wake-up routine, clear the transmit buffer */
187  /** substate for the wake-up routine, clear the receive buffer */
189  /** substate for the wake-up routine, sending helloall to the daisy-chain */
191  /** substate for the wake-up routine, verifying the result of helloall */
193  /** substate for requesting an FMEA check on the bridge IC */
195  /** substate for the verification of the FMEA check done in
196  * #MXM_5X_41B_FMEA_REQUEST */
198  /** substate for the initiation of a uart transaction for the WRITEALL
199  * command */
201  /** substate for the verifying the buffer of a WRITEALL transaction */
203  /** substate for the initiation of a uart transaction for the WRITEALL
204  * command */
206  /** substate for checking the received CRC in a WRITEDEVICE transaction */
208  /** substate for the initiation of a uart transaction for the READALL
209  * command */
211  /** substate for checking the received CRC in a READALL transaction */
213  /** substate for retrieving the data check byte from a READALL transaction */
215  /** default value indicating no selected substate; has to be last substate in enum */
218 
219 /**
220  * @brief Request status of Battery Management Protocol states.
221  * @details This enum describes the states that a state in the
222  * #MXM_5XStateMachine() can have.
223  *
224  * Alternating bit patterns are used for better distinction
225  * and to make sure that there is no accidental overlap.
226  */
227 typedef enum {
228  MXM_5X_STATE_UNSENT = 0x56, /*!< The request has not been sent to the state-machine yet. */
230  0x67, /*!< The request has been received by the state-machine, but not been processed yet. */
231  MXM_5X_STATE_PROCESSED = 0x9A, /*!< The request has been process successfully */
232  MXM_5X_STATE_ERROR = 0xAB, /*!< An error has occurred during processing of the request. */
234 
235 /**
236  * @brief Payload command
237  */
238 typedef struct {
239  MXM_REG_NAME_e regAddress; /*!< register address that shall be written */
240  uint8_t lsb; /*!< least significant bit */
241  uint8_t msb; /*!< most significant bit */
242  uint8_t blocksize; /*!< blocksize for the READBLOCK command */
243  uint8_t deviceAddress; /*!< device address for commands that address specific devices */
244  MXM_MODEL_ID_e model; /*!< model of device that shall be addressed */
246 
247 /**
248  * @brief 5x statemachine structure
249  **/
250 typedef struct {
251  MXM_STATEMACHINE_5X_e state; /*!< state of Driver State Machine */
252  MXM_5X_SUBSTATES_e substate; /*!< substate of current Driver State */
253  MXM_5X_COMMAND_PAYLOAD_s commandPayload; /*!< command payload of the Battery Management Protocol */
254  MXM_5X_STATE_REQUEST_STATUS_e *processed; /*!< status-indicator of the underlying state-machine */
255  MXM_41B_STATE_REQUEST_STATUS_e status41b; /*!< for tracking of 41B state-machine */
256  /**
257  * @brief Number of satellites
258  * @details This variable tracks the number of monitoring ICs that have been
259  * found during the enumeration of all connected monitoring ICs.
260  * Therefore this variable is initialized with 0. This value will be
261  * compared with the expected number of monitoring ICs which is set in
262  * #BS_NR_OF_MODULES.
263  */
265  /**
266  * @brief Number of monitoring ICs matches the expected number.
267  * @details This status variable indicates whether the number of found
268  * monitoring ICs in #MXM_5X_INSTANCE_s::numberOfSatellites
269  * matches with the configured number of monitoring ICs in
270  * #BS_NR_OF_MODULES. This variable is updated during execution of
271  * the substate
272  * #MXM_5X_INIT_WAKE_UP_SATELLITE_DEVICES_HELLOALL_VERIFY_MSG_AND_COUNT
273  * in the state #MXM_STATEMACH_5X_INIT in #MXM_5XStateMachine().
274  *
275  * In case the numbers are the same, the variable will read #STD_OK,
276  * otherwise it will be set to #STD_NOT_OK.
277  */
279  /**
280  * @brief Tracks the last received DC byte.
281  */
282  uint8_t lastDCByte;
283  uint8_t errorCounter; /*!< counts the number of errors that the underlying state-machine has encountered. */
284  uint32_t resetWaitTimestamp; /*!< timestamp of the moment when waiting for the devices to be reset has started */
285  /**
286  * @brief Length of Command Buffer
287  * @details Length of the array #MXM_5X_INSTANCE_s::commandBuffer.
288  */
290  /**
291  * @brief Command Buffer
292  * @details This variable contains a buffer for Battery Management Protocol
293  * commands. The content is constructed by calling functions like
294  * #MXM_5XConstructCommandBufferHelloall() and similar.
295  *
296  * Afterwards this buffer can be passed on to the lower state-machine
297  * as payload. The length of this buffer is described in
298  * #MXM_5X_INSTANCE_s::commandBufferCurrentLength.
299  */
300  uint16_t commandBuffer[COMMAND_BUFFER_LENGTH];
301  uint16_t rxBuffer[MXM_5X_RX_BUFFER_LEN]; /*!< array containing the buffer for received data */
303 
304 /*========== Extern Constant and Variable Declarations ======================*/
305 
306 /*========== Extern Function Prototypes =====================================*/
307 
308 /**
309  * @brief Execute state-machine for Battery Management Protocol.
310  * @details This function executes the state-machine that exposes an interface
311  * for the Maxim Battery Management Protocol for monitoring ICs
312  * of the MAX1785x family.
313  */
314 extern void MXM_5XStateMachine(MXM_41B_INSTANCE_s *pInstance41b, MXM_5X_INSTANCE_s *pInstance5x);
315 
316 /**
317  * @brief Returns the last received DC byte.
318  * @param[in] kpkInstance pointer to the instance struct
319  */
320 extern MXM_DC_BYTE_e MXM_5XGetLastDCByte(const MXM_5X_INSTANCE_s *const kpkInstance);
321 
322 /**
323  * @brief Get the value of #MXM_5X_INSTANCE_s::numberOfSatellitesIsGood
324  * @param[in] kpkInstance pointer to the instance struct
325  *
326  * @return #MXM_5X_INSTANCE_s::numberOfSatellitesIsGood
327  */
329 
330 /**
331  * @brief Copy RX buffer into variable.
332  * @details This function copies the RX buffer which is locally available in
333  * the #MXM_5XStateMachine() into a supplied array pointer.
334  * It will make sure that both the length of the local as also the
335  * supplied length of the RX buffer are not violated.
336  * In case of the supplied RX buffer being longer than the locally
337  * available one, the remaining entries of the buffer will be
338  * filled with 0.
339  * @param[in,out] kpkInstance pointer to the state-struct
340  * @param[in] rxBuffer array-pointer to a RX buffer that shall be filled
341  * @param[in] rxBufferLength length of the supplied array
342  * @return #STD_NOT_OK for rxBuffer being a #NULL_PTR or rxBufferLength
343  * having length 0
344  */
346  const MXM_5X_INSTANCE_s *const kpkInstance,
347  uint8_t *rxBuffer,
348  uint16_t rxBufferLength);
349 
350 /**
351  * @brief Get number of satellites
352  * @details Getter-function for the number of satellites
353  * (variable #MXM_5X_INSTANCE_s::numberOfSatellites).
354  * @param[in] kpkInstance pointer to the state struct
355  * @return value of #MXM_5X_INSTANCE_s::numberOfSatellites
356  */
357 extern uint8_t MXM_5XGetNumberOfSatellites(const MXM_5X_INSTANCE_s *const kpkInstance);
358 
359 /**
360  * @brief Set state request for the Battery Management Statemachine
361  * @details This function sets the state requests for the #MXM_5XStateMachine().
362  * @param[in,out] pInstance5x pointer to the 5x state
363  * @param[in] state State that is requested
364  * @param[in] commandPayload additional payload that is handled by the state
365  * @param[in,out] processed pointer to the status of the request
366  * @return #STD_OK if the request has been successfully placed,
367  * #STD_NOT_OK if not
368  */
370  MXM_5X_INSTANCE_s *pInstance5x,
371  MXM_STATEMACHINE_5X_e state,
372  MXM_5X_COMMAND_PAYLOAD_s commandPayload,
373  MXM_5X_STATE_REQUEST_STATUS_e *processed);
374 
375 /**
376  * @brief runs a selfcheck for the address space check
377  * @details Runs a selfcheck for the function which is checking if a register
378  * address is inside the user accessible address space of the
379  * monitoring IC.
380  * @return #STD_OK if the selfcheck has been successful
381  */
383 
384 /**
385  * @brief Initializes the state struct with default values
386  * @details This function is called through the startup of the driver in order
387  * to ensure proper default values.
388  * @param[out] pInstance instance of the state struct that shall be initialized
389  */
390 extern void MXM_5X_InitializeStateStruct(MXM_5X_INSTANCE_s *pInstance);
391 
392 /*========== Externalized Static Functions Prototypes (Unit Test) ===========*/
393 
394 #endif /* FOXBMS__MXM_BATTERY_MANAGEMENT_H_ */
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.
MXM_41B_STATE_REQUEST_STATUS_e
Request status of MAX17841B states.
Definition: mxm_17841b.h:130
Basic defines for the complete Maxim driver.
MXM_MODEL_ID_e
Type of monitoring device.
MXM_DC_BYTE_e
MXM_STATEMACHINE_5X_e
States of the Battery Management Protocol state-machine.
@ MXM_STATEMACH_5X_INIT
@ MXM_STATEMACH_5X_IDLE
@ MXM_STATEMACH_5X_READALL
@ MXM_STATEMACH_5X_WRITEALL
@ MXM_STATEMACH_5X_WRITE_DEVICE
@ MXM_STATEMACH_5X_UNINITIALIZED
@ MXM_STATEMACH_5X_MAXSTATE
@ MXM_STATEMACH_5X_41B_FMEA_CHECK
#define MXM_5X_RX_BUFFER_LEN
void MXM_5X_InitializeStateStruct(MXM_5X_INSTANCE_s *pInstance)
Initializes the state struct with default values.
MXM_5X_STATE_REQUEST_STATUS_e
Request status of Battery Management Protocol states.
@ MXM_5X_STATE_UNSENT
@ MXM_5X_STATE_PROCESSED
@ MXM_5X_STATE_UNPROCESSED
@ MXM_5X_STATE_ERROR
STD_RETURN_TYPE_e must_check_return MXM_5XUserAccessibleAddressSpaceCheckerSelfCheck(void)
runs a selfcheck for the address space check
void MXM_5XStateMachine(MXM_41B_INSTANCE_s *pInstance41b, MXM_5X_INSTANCE_s *pInstance5x)
Execute state-machine for Battery Management Protocol.
MXM_5X_SUBSTATES_e
Sub-states of the Battery Management Protocol state-machine.
@ MXM_5X_WRITE_DEVICE_CHECK_CRC
@ MXM_5X_INIT_WAKE_UP_SATELLITE_DEVICES_CLEAR_RECEIVE_BUFFER
@ MXM_5X_WRITEALL_UART_TRANSACTION
@ MXM_5X_WRITEALL_UART_VERIFY_BUFFER
@ MXM_5X_INIT_WAKE_UP_SATELLITE_DEVICES_HELLOALL
@ MXM_5X_READALL_UART_TRANSACTION
@ MXM_5X_READALL_CHECK_CRC
@ MXM_5X_INIT_WAKE_UP_SATELLITE_DEVICES_EN_PREAMBLES
@ MXM_5X_INIT_41B_GET_VERSION
@ MXM_5X_READALL_GET_DC
@ MXM_5X_41B_FMEA_REQUEST
@ MXM_5X_INIT_ENABLE_RX_INTERRUPT_FLAGS
@ MXM_5X_WRITE_DEVICE_UART_TRANSACTION
@ MXM_5X_INIT_WAKE_UP_SATELLITE_DEVICES_CLEAR_RECEIVE_BUFFER_2
@ MXM_5X_INIT_WAKE_UP_SATELLITE_DEVICES_HELLOALL_VERIFY_MSG_AND_COUNT
@ MXM_5X_INIT_WAKE_UP_SATELLITE_DEVICES_WAIT_FOR_RX_STATUS_BUSY
@ MXM_5X_INIT_41B_INIT
@ MXM_5X_ENTRY_SUBSTATE
@ MXM_5X_INIT_WAKE_UP_SATELLITE_DEVICES_WAIT_FOR_RX_STATUS_EMPTY
@ MXM_5X_INIT_WAIT_FOR_RESET
@ MXM_5X_INIT_WAKE_UP_SATELLITE_DEVICES_CLEAR_TRANSMIT_BUFFER
@ MXM_5X_INIT_ENABLE_KEEP_ALIVE
@ MXM_5X_41B_FMEA_CHECK
@ MXM_5X_INIT_WAKE_UP_SATELLITE_DEVICES_DIS_PREAMBLES
uint8_t MXM_BATTERY_MANAGEMENT_COMMAND_t
Type for Battery Management Protocol commands.
STD_RETURN_TYPE_e MXM_5XGetRXBuffer(const MXM_5X_INSTANCE_s *const kpkInstance, uint8_t *rxBuffer, uint16_t rxBufferLength)
Copy RX buffer into variable.
STD_RETURN_TYPE_e MXM_5XSetStateRequest(MXM_5X_INSTANCE_s *pInstance5x, MXM_STATEMACHINE_5X_e state, MXM_5X_COMMAND_PAYLOAD_s commandPayload, MXM_5X_STATE_REQUEST_STATUS_e *processed)
Set state request for the Battery Management Statemachine.
STD_RETURN_TYPE_e MXM_5XGetNumberOfSatellitesGood(const MXM_5X_INSTANCE_s *const kpkInstance)
Get the value of MXM_5X_INSTANCE_s::numberOfSatellitesIsGood.
MXM_DC_BYTE_e MXM_5XGetLastDCByte(const MXM_5X_INSTANCE_s *const kpkInstance)
Returns the last received DC byte.
#define COMMAND_BUFFER_LENGTH
uint8_t MXM_5XGetNumberOfSatellites(const MXM_5X_INSTANCE_s *const kpkInstance)
Get number of satellites.
Header for the configuration for the Maxim monitoring chip.
Headers for the CRC8 calculation for Maxim Integrated Monitoring devices.
Register map of the MAX1785x monitoring IC.
MXM_REG_NAME_e
MAX1785x register names.
Struct for the state-variable of state-machine.
Definition: mxm_17841b.h:157
5x statemachine structure
uint8_t lastDCByte
Tracks the last received DC byte.
MXM_41B_STATE_REQUEST_STATUS_e status41b
STD_RETURN_TYPE_e numberOfSatellitesIsGood
Number of monitoring ICs matches the expected number.
uint8_t commandBufferCurrentLength
Length of Command Buffer.
MXM_STATEMACHINE_5X_e state
MXM_5X_COMMAND_PAYLOAD_s commandPayload
uint8_t numberOfSatellites
Number of satellites.
MXM_5X_SUBSTATES_e substate
MXM_5X_STATE_REQUEST_STATUS_e * processed