foxBMS  1.3.0
The foxBMS Battery Management System API Documentation
mxm_afe.c
Go to the documentation of this file.
1 /**
2  *
3  * @copyright © 2010 - 2022, 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 2022-05-30 (date of last update)
47  * @version v1.3.0
48  * @ingroup DRIVER
49  * @prefix AFE
50  *
51  * @brief AFE driver implementation
52  */
53 
54 /*========== Includes =======================================================*/
55 #include "general.h"
56 
57 #include "afe.h"
58 #include "mxm_17841b.h"
59 #include "mxm_1785x.h"
60 #include "mxm_battery_management.h"
61 #include "os.h"
62 
63 /*========== Macros and Definitions =========================================*/
64 
65 /*========== Static Constant and Variable Definitions =======================*/
66 
67 /** state variable for the MAX17841B driver */
68 /* AXIVION Next Line Style Generic-InitializeAllVariables: state variable has an initializer function */
70 
71 /** state variable for the Battery Management protocol driver */
72 /* AXIVION Next Line Style Generic-InitializeAllVariables: state variable has an initializer function */
74 
75 /**
76  * @brief Local cell voltage data block
77  *
78  * This local instance stores the measured cell voltages. In contrast to
79  * #MXM_MONITORING_INSTANCE_s::localVoltages, the layout of this structure
80  * changes with the defined battery-system as it is described by the
81  * database_cfg.h. Mapping the values from
82  * #MXM_MONITORING_INSTANCE_s::localVoltages to #mxm_tableCellVoltages and copying
83  * these entries into the database is handled by #MXM_ParseVoltagesIntoDB().
84  */
86 
87 /**
88  * @brief Local cell temperature data block
89  * @details This local instance stores the measured cell temperatures.
90  */
93 
94 /**
95  * @brief Balancing control
96  * @details This variable stores information about which cells need balancing
97  */
99 
100 /** @brief Local data structure for openwire results. */
102 
103 /** balancing state variable */
106 };
107 
108 /** state variable for the Maxim monitoring driver */
111  .pInstance41B = &mxm_41bState,
112  .pInstance5X = &mxm_5xState,
113  .pCellVoltages_table = &mxm_tableCellVoltages,
114  .pCellTemperatures_table = &mxm_tableCellTemperatures,
115  .pOpenwire_table = &mxm_tableOpenWire,
116  .selfCheck.crc = STD_NOT_OK,
117  .selfCheck.conv = STD_NOT_OK,
118  .selfCheck.firstSetBit = STD_NOT_OK,
119  .selfCheck.extractValueFromRegister = STD_NOT_OK,
120  .selfCheck.parseVoltageReadall = STD_NOT_OK,
121  .selfCheck.addressSpaceChecker = STD_NOT_OK,
122 };
123 
124 /*========== Extern Constant and Variable Definitions =======================*/
125 
126 /*========== Static Function Prototypes =====================================*/
127 
128 /**
129  * @brief Tick function
130  * @details This function is the central entry-point for this driver. It calls
131  * the state-machines via the functions #MXM_StateMachine(),
132  * #MXM_5XStateMachine() and #MXM_41BStateMachine().
133  *
134  * The state-machines are called in such a way that the main blocking
135  * point is the SPI interface. (That means the order is optimized so
136  * that every cycle a SPI command should be available for sending or
137  * receiving as this is the limiting ressource.)
138  */
139 static void MXM_Tick(void);
140 
141 /**
142  * @brief Initialize the state structs
143  * @details This function sets default values to the members of the state
144  * structs. It can be used to reset the driver to a default value.
145  */
146 static void MXM_SetStateStructDefaultValues(void);
147 
148 /*========== Static Function Implementations ================================*/
149 
150 static void MXM_Tick(void) {
155  /* execute battery management state machine once (low cost and prepares for next cycle) */
157 
158  /* check if init is stuck */
161  (mxm_state.resetNecessary == false)) {
162  /* check if we spend too long in initialization and try to resolve with a reset */
165  }
166  }
167 }
168 
173 }
174 
175 /*========== Extern Function Implementations ================================*/
177  if (mxm_state.resetNecessary == true) {
178  /* a reset has been requested */
179 
181  /* save allowStartup and operationRequested */
182  const bool allowStartup = mxm_state.allowStartup;
183  const bool operationRequested = mxm_state.operationRequested;
184  const bool firstMeasurementDone = mxm_state.firstMeasurementDone;
186 
187  (void)AFE_Initialize();
188 
189  /* restore relevant flags */
190  mxm_state.allowStartup = allowStartup;
191  /* if a first measurement has been done, operation has been requested before */
192  mxm_state.operationRequested = (operationRequested || firstMeasurementDone);
193 
194  } else {
195  MXM_Tick();
196  }
197 
198  return STD_OK;
199 }
200 
204  /* call pre init self check so that we can do these costly tests before the main cycle begins (results are stored) */
206  return STD_OK;
207 }
208 
210  STD_RETURN_TYPE_e retval = STD_OK;
211 
213  mxm_state.allowStartup = true;
216 
217  return retval;
218 }
219 
222  const bool firstMeasurementDone = mxm_state.firstMeasurementDone;
224  return firstMeasurementDone;
225 }
226 
227 extern STD_RETURN_TYPE_e AFE_RequestIoWrite(uint8_t string) {
228  FAS_ASSERT(string < BS_NR_OF_STRINGS);
229  return STD_NOT_OK;
230 }
231 
232 extern STD_RETURN_TYPE_e AFE_RequestIoRead(uint8_t string) {
233  FAS_ASSERT(string < BS_NR_OF_STRINGS);
234  return STD_NOT_OK;
235 }
236 
238  FAS_ASSERT(string < BS_NR_OF_STRINGS);
239  return STD_NOT_OK;
240 }
241 
243  FAS_ASSERT(string < BS_NR_OF_STRINGS);
244  return STD_NOT_OK;
245 }
246 
247 extern STD_RETURN_TYPE_e AFE_RequestEepromRead(uint8_t string) {
248  FAS_ASSERT(string < BS_NR_OF_STRINGS);
249  return STD_NOT_OK;
250 }
251 
253  FAS_ASSERT(string < BS_NR_OF_STRINGS);
254  return STD_NOT_OK;
255 }
256 
258  FAS_ASSERT(string < BS_NR_OF_STRINGS);
259  STD_RETURN_TYPE_e retval = STD_OK;
260 
262 
263  return retval;
264 }
265 
266 /*========== Externalized Static Function Implementations (Unit Test) =======*/
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:80
@ DATA_BLOCK_ID_CELL_TEMPERATURE_BASE
Definition: database_cfg.h:98
@ DATA_BLOCK_ID_OPEN_WIRE_BASE
Definition: database_cfg.h:84
@ DATA_BLOCK_ID_CELL_VOLTAGE_BASE
Definition: database_cfg.h:97
#define FAS_ASSERT(x)
Assertion macro that asserts that x is true.
Definition: fassert.h:241
STD_RETURN_TYPE_e
Definition: fstd_types.h:81
@ STD_NOT_OK
Definition: fstd_types.h:83
@ STD_OK
Definition: fstd_types.h:82
General macros and definitions for the whole platform.
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 monitoring chip.
void MXM_CheckIfErrorCounterCanBeReset(MXM_MONITORING_INSTANCE_s *pInstance)
Function that checks if the error counter can be reset.
Definition: mxm_1785x.c:888
STD_RETURN_TYPE_e MXM_PreInitSelfCheck(MXM_MONITORING_INSTANCE_s *pState)
Execute all preinit selfchecks.
Definition: mxm_1785x.c:1237
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:901
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:1164
Headers for the driver for the MAX17841B ASCI and MAX1785x monitoring chip.
@ MXM_STATEMACHINE_STATES_OPERATION
@ MXM_STATEMACHINE_STATES_UNINITIALIZED
@ MXM_STATEMACHINE_STATES_IDLE
static void MXM_Tick(void)
Tick function.
Definition: mxm_afe.c:150
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:247
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:252
static DATA_BLOCK_CELL_VOLTAGE_s mxm_tableCellVoltages
Local cell voltage data block.
Definition: mxm_afe.c:85
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:232
STD_RETURN_TYPE_e AFE_TriggerIc(void)
Definition: mxm_afe.c:176
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:257
bool AFE_IsFirstMeasurementCycleFinished(void)
Checks if the first AFE measurement cycle was made.
Definition: mxm_afe.c:220
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:242
static DATA_BLOCK_BALANCING_CONTROL_s mxm_tableBalancingControl
Balancing control.
Definition: mxm_afe.c:98
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:237
static MXM_5X_INSTANCE_s mxm_5xState
Definition: mxm_afe.c:73
static MXM_41B_INSTANCE_s mxm_41bState
Definition: mxm_afe.c:69
static MXM_BALANCING_STATE_s mxm_balancingState
Definition: mxm_afe.c:104
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:227
static void MXM_SetStateStructDefaultValues(void)
Initialize the state structs.
Definition: mxm_afe.c:169
STD_RETURN_TYPE_e AFE_Initialize(void)
Definition: mxm_afe.c:201
static DATA_BLOCK_CELL_TEMPERATURE_s mxm_tableCellTemperatures
Local cell temperature data block.
Definition: mxm_afe.c:91
STD_RETURN_TYPE_e AFE_StartMeasurement(void)
Makes the initialization request to the AFE state machine.
Definition: mxm_afe.c:209
static DATA_BLOCK_OPEN_WIRE_s mxm_tableOpenWire
Local data structure for openwire results.
Definition: mxm_afe.c:101
static MXM_MONITORING_INSTANCE_s mxm_state
Definition: mxm_afe.c:109
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 monitoring chip.
void MXM_InitializeMonitoringPins(void)
Initialize the pins connected to the MAX17841B.
Definition: mxm_cfg.c:75
#define MXM_MAXIMUM_INIT_TIME_ms
Definition: mxm_cfg.h:108
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:144
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:135
void OS_EnterTaskCritical(void)
Enter Critical interface function for use in FreeRTOS-Tasks and FreeRTOS-ISR.
Definition: os_freertos.c:131
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:247
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:146
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:129
DATA_BLOCK_ID_e uniqueId
Definition: database_cfg.h:119
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:302
Struct for the state-variable of state-machine.
Definition: mxm_17841b.h:158
5x statemachine 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