foxBMS  1.6.0
The foxBMS Battery Management System API Documentation
mxm_afe.c
Go to the documentation of this file.
1 /**
2  *
3  * @copyright © 2010 - 2023, 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_afe.c
44  * @author foxBMS Team
45  * @date 2020-06-16 (date of creation)
46  * @updated 2023-10-12 (date of last update)
47  * @version v1.6.0
48  * @ingroup DRIVER
49  * @prefix AFE
50  *
51  * @brief AFE driver implementation
52  */
53 
54 /*========== Includes =======================================================*/
55 
56 #include "afe.h"
57 #include "mxm_17841b.h"
58 #include "mxm_1785x.h"
59 #include "mxm_battery_management.h"
60 #include "os.h"
61 
62 #include <stdbool.h>
63 #include <stdint.h>
64 
65 /*========== Macros and Definitions =========================================*/
66 
67 /*========== Static Constant and Variable Definitions =======================*/
68 
69 /** state variable for the MAX17841B driver */
70 /* AXIVION Next Codeline Style Generic-InitializeAllVariables: state variable has an initializer function */
72 
73 /** state variable for the Battery Management protocol driver */
74 /* AXIVION Next Codeline Style Generic-InitializeAllVariables: state variable has an initializer function */
76 
77 /**
78  * @brief Local cell voltage data block
79  *
80  * This local instance stores the measured cell voltages. In contrast to
81  * #MXM_MONITORING_INSTANCE_s::localVoltages, the layout of this structure
82  * changes with the defined battery-system as it is described by the
83  * database_cfg.h. Mapping the values from
84  * #MXM_MONITORING_INSTANCE_s::localVoltages to #mxm_tableCellVoltages and copying
85  * these entries into the database is handled by #MXM_ParseVoltagesIntoDB().
86  */
88 
89 /**
90  * @brief Local cell temperature data block
91  * @details This local instance stores the measured cell temperatures.
92  */
95 
96 /**
97  * @brief Balancing control
98  * @details This variable stores information about which cells need balancing
99  */
101 
102 /** @brief Local data structure for openWire results. */
104 
105 /** balancing state variable */
108 };
109 
110 /** state variable for the Maxim monitoring driver */
113  .pInstance41B = &mxm_41bState,
114  .pInstance5X = &mxm_5xState,
115  .pCellVoltages_table = &mxm_tableCellVoltages,
116  .pCellTemperatures_table = &mxm_tableCellTemperatures,
117  .pOpenWire_table = &mxm_tableOpenWire,
118  .selfCheck.crc = STD_NOT_OK,
119  .selfCheck.conv = STD_NOT_OK,
120  .selfCheck.firstSetBit = STD_NOT_OK,
121  .selfCheck.extractValueFromRegister = STD_NOT_OK,
122  .selfCheck.parseVoltageReadall = STD_NOT_OK,
123  .selfCheck.addressSpaceChecker = STD_NOT_OK,
124 };
125 
126 /*========== Extern Constant and Variable Definitions =======================*/
127 
128 /*========== Static Function Prototypes =====================================*/
129 
130 /**
131  * @brief Tick function
132  * @details This function is the central entry-point for this driver. It calls
133  * the state-machines via the functions #MXM_StateMachine(),
134  * #MXM_5XStateMachine() and #MXM_41BStateMachine().
135  *
136  * The state-machines are called in such a way that the main blocking
137  * point is the SPI interface. (That means the order is optimized so
138  * that every cycle a SPI command should be available for sending or
139  * receiving as this is the limiting resource.)
140  */
141 static void MXM_Tick(void);
142 
143 /**
144  * @brief Initialize the state structs
145  * @details This function sets default values to the members of the state
146  * structs. It can be used to reset the driver to a default value.
147  */
148 static void MXM_SetStateStructDefaultValues(void);
149 
150 /*========== Static Function Implementations ================================*/
151 
152 static void MXM_Tick(void) {
157  /* execute battery management state machine once (low cost and prepares for next cycle) */
159 
160  /* check if init is stuck */
163  (mxm_state.resetNecessary == false)) {
164  /* check if we spend too long in initialization and try to resolve with a reset */
167  }
168  }
169 }
170 
175 }
176 
177 /*========== Extern Function Implementations ================================*/
179  if (mxm_state.resetNecessary == true) {
180  /* a reset has been requested */
181 
183  /* save allowStartup and operationRequested */
184  const bool allowStartup = mxm_state.allowStartup;
185  const bool operationRequested = mxm_state.operationRequested;
186  const bool firstMeasurementDone = mxm_state.firstMeasurementDone;
188 
189  (void)AFE_Initialize();
190 
191  /* restore relevant flags */
192  mxm_state.allowStartup = allowStartup;
193  /* if a first measurement has been done, operation has been requested before */
194  mxm_state.operationRequested = (operationRequested || firstMeasurementDone);
195  } else {
196  MXM_Tick();
197  }
198 
199  return STD_OK;
200 }
201 
205  /* call pre init self check so that we can do these costly tests before the main cycle begins (results are stored) */
207  return STD_OK;
208 }
209 
211  STD_RETURN_TYPE_e retval = STD_OK;
212 
214  mxm_state.allowStartup = true;
217 
218  return retval;
219 }
220 
223  const bool firstMeasurementDone = mxm_state.firstMeasurementDone;
225  return firstMeasurementDone;
226 }
227 
228 extern STD_RETURN_TYPE_e AFE_RequestIoWrite(uint8_t string) {
229  FAS_ASSERT(string < BS_NR_OF_STRINGS);
230  return STD_NOT_OK;
231 }
232 
233 extern STD_RETURN_TYPE_e AFE_RequestIoRead(uint8_t string) {
234  FAS_ASSERT(string < BS_NR_OF_STRINGS);
235  return STD_NOT_OK;
236 }
237 
239  FAS_ASSERT(string < BS_NR_OF_STRINGS);
240  return STD_NOT_OK;
241 }
242 
244  FAS_ASSERT(string < BS_NR_OF_STRINGS);
245  return STD_NOT_OK;
246 }
247 
248 extern STD_RETURN_TYPE_e AFE_RequestEepromRead(uint8_t string) {
249  FAS_ASSERT(string < BS_NR_OF_STRINGS);
250  return STD_NOT_OK;
251 }
252 
254  FAS_ASSERT(string < BS_NR_OF_STRINGS);
255  return STD_NOT_OK;
256 }
257 
259  FAS_ASSERT(string < BS_NR_OF_STRINGS);
260  STD_RETURN_TYPE_e retval = STD_OK;
261 
263 
264  return retval;
265 }
266 
267 /*========== Externalized Static Function Implementations (Unit Test) =======*/
268 #ifdef UNITY_UNIT_TEST
269 #endif
AFE driver header.
#define BS_NR_OF_STRINGS
Number of parallel strings in the battery pack.
@ DATA_BLOCK_ID_BALANCING_CONTROL
Definition: database_cfg.h:83
@ DATA_BLOCK_ID_CELL_TEMPERATURE_BASE
Definition: database_cfg.h:87
@ DATA_BLOCK_ID_OPEN_WIRE_BASE
Definition: database_cfg.h:103
@ DATA_BLOCK_ID_CELL_VOLTAGE_BASE
Definition: database_cfg.h:90
#define FAS_ASSERT(x)
Assertion macro that asserts that x is true.
Definition: fassert.h:255
STD_RETURN_TYPE_e
Definition: fstd_types.h:82
@ STD_NOT_OK
Definition: fstd_types.h:84
@ STD_OK
Definition: fstd_types.h:83
void MXM_41BStateMachine(MXM_41B_INSTANCE_s *pInstance)
Execute state-machine for the MAX17841B.
Definition: mxm_17841b.c:928
void MXM_41BInitializeStateStruct(MXM_41B_INSTANCE_s *pInstance)
Initializes the state struct with default values.
Definition: mxm_17841b.c:974
Headers for the driver for the MAX17841B ASCI and MAX1785x analog front-end.
void MXM_CheckIfErrorCounterCanBeReset(MXM_MONITORING_INSTANCE_s *pInstance)
Function that checks if the error counter can be reset.
Definition: mxm_1785x.c:896
STD_RETURN_TYPE_e MXM_PreInitSelfCheck(MXM_MONITORING_INSTANCE_s *pState)
Execute all preinit selfchecks.
Definition: mxm_1785x.c:1242
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:909
void MXM_StateMachine(MXM_MONITORING_INSTANCE_s *pInstance)
Main state-machine implementation.
Definition: mxm_1785x.c:1319
void MXM_InitializeStateStruct(MXM_BALANCING_STATE_s *pBalancingInstance, MXM_MONITORING_INSTANCE_s *pMonitoringInstance)
Initializes the state structs with default values.
Definition: mxm_1785x.c:1169
Headers for the driver for the MAX17841B ASCI and MAX1785x analog front-end.
@ MXM_STATEMACHINE_STATES_OPERATION
@ MXM_STATEMACHINE_STATES_UNINITIALIZED
@ MXM_STATEMACHINE_STATES_IDLE
static void MXM_Tick(void)
Tick function.
Definition: mxm_afe.c:152
STD_RETURN_TYPE_e AFE_RequestEepromRead(uint8_t string)
Makes the request to the AFE state machine to read from the external EEPROM on slaves.
Definition: mxm_afe.c:248
STD_RETURN_TYPE_e AFE_RequestEepromWrite(uint8_t string)
Makes the request to the AFE state machine to write to the external EEPROM on slaves.
Definition: mxm_afe.c:253
static DATA_BLOCK_CELL_VOLTAGE_s mxm_tableCellVoltages
Local cell voltage data block.
Definition: mxm_afe.c:87
STD_RETURN_TYPE_e AFE_RequestIoRead(uint8_t string)
Makes the request to the AFE state machine to read from the IO port-expander.
Definition: mxm_afe.c:233
STD_RETURN_TYPE_e AFE_TriggerIc(void)
Definition: mxm_afe.c:178
STD_RETURN_TYPE_e AFE_RequestOpenWireCheck(uint8_t string)
Makes the request to the AFE state machine to perform open-wire check.
Definition: mxm_afe.c:258
bool AFE_IsFirstMeasurementCycleFinished(void)
Checks if the first AFE measurement cycle was made.
Definition: mxm_afe.c:221
STD_RETURN_TYPE_e AFE_RequestBalancingFeedbackRead(uint8_t string)
Makes the request to the AFE state machine to read balancing feedback from the slaves.
Definition: mxm_afe.c:243
static DATA_BLOCK_BALANCING_CONTROL_s mxm_tableBalancingControl
Balancing control.
Definition: mxm_afe.c:100
STD_RETURN_TYPE_e AFE_RequestTemperatureRead(uint8_t string)
Makes the request to the AFE state machine to read from the external temperature sensor on slaves.
Definition: mxm_afe.c:238
static MXM_5X_INSTANCE_s mxm_5xState
Definition: mxm_afe.c:75
static MXM_41B_INSTANCE_s mxm_41bState
Definition: mxm_afe.c:71
static MXM_BALANCING_STATE_s mxm_balancingState
Definition: mxm_afe.c:106
STD_RETURN_TYPE_e AFE_RequestIoWrite(uint8_t string)
Makes the request to the AFE state machine to write to the IO port-expander.
Definition: mxm_afe.c:228
static void MXM_SetStateStructDefaultValues(void)
Initialize the state structs.
Definition: mxm_afe.c:171
STD_RETURN_TYPE_e AFE_Initialize(void)
Definition: mxm_afe.c:202
static DATA_BLOCK_CELL_TEMPERATURE_s mxm_tableCellTemperatures
Local cell temperature data block.
Definition: mxm_afe.c:93
STD_RETURN_TYPE_e AFE_StartMeasurement(void)
Makes the initialization request to the AFE state machine.
Definition: mxm_afe.c:210
static DATA_BLOCK_OPEN_WIRE_s mxm_tableOpenWire
Local data structure for openWire results.
Definition: mxm_afe.c:103
static MXM_MONITORING_INSTANCE_s mxm_state
Definition: mxm_afe.c:111
void MXM_5X_InitializeStateStruct(MXM_5X_INSTANCE_s *pInstance)
Initializes the state struct with default values.
void MXM_5XStateMachine(MXM_41B_INSTANCE_s *pInstance41b, MXM_5X_INSTANCE_s *pInstance5x)
Execute state-machine for Battery Management Protocol.
Headers for the driver for the MAX17841B ASCI and MAX1785x analog front-end.
void MXM_InitializeMonitoringPins(void)
Initialize the pins connected to the MAX17841B.
Definition: mxm_cfg.c:77
#define MXM_MAXIMUM_INIT_TIME_ms
Definition: mxm_cfg.h:115
bool OS_CheckTimeHasPassed(uint32_t oldTimeStamp_ms, uint32_t timeToPass_ms)
This function checks if timeToPass has passed since the last timestamp to now.
Definition: os.c:150
Declaration of the OS wrapper interface.
void OS_ExitTaskCritical(void)
Exit Critical interface function for use in FreeRTOS-Tasks and FreeRTOS-ISR.
Definition: os_freertos.c:154
void OS_EnterTaskCritical(void)
Enter Critical interface function for use in FreeRTOS-Tasks and FreeRTOS-ISR.
Definition: os_freertos.c:150
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:255
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:153
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:135
DATA_BLOCK_ID_e uniqueId
Definition: database_cfg.h:125
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:310
Struct for the state-variable of state-machine.
Definition: mxm_17841b.h:161
5x state machine structure
DATA_BLOCK_BALANCING_CONTROL_s *const pBalancingControl_table
MXM_41B_INSTANCE_s *const pInstance41B
MXM_BALANCING_STATE_s *const pBalancingState
MXM_STATEMACHINE_STATES_e state
MXM_5X_INSTANCE_s *const pInstance5X