foxBMS - Unit Tests  1.2.1
The foxBMS Unit Tests API Documentation
mxm_afe.c
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_afe.c
44  * @author foxBMS Team
45  * @date 2020-06-16 (date of creation)
46  * @updated 2021-12-06 (date of last update)
47  * @ingroup DRIVER
48  * @prefix AFE
49  *
50  * @brief AFE driver implementation
51  */
52 
53 /*========== Includes =======================================================*/
54 #include "general.h"
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 /*========== Macros and Definitions =========================================*/
63 
64 /*========== Static Constant and Variable Definitions =======================*/
65 
66 /** state variable for the MAX17841B driver */
67 /* AXIVION Next Line Style Generic-InitializeAllVariables: state variable has an initializer function */
69 
70 /** state variable for the Battery Management protocol driver */
71 /* AXIVION Next Line Style Generic-InitializeAllVariables: state variable has an initializer function */
73 
74 /**
75  * @brief Local cell voltage data block
76  *
77  * This local instance stores the measured cell voltages. In contrast to
78  * #MXM_MONITORING_INSTANCE_s::localVoltages, the layout of this structure
79  * changes with the defined battery-system as it is described by the
80  * database_cfg.h. Mapping the values from
81  * #MXM_MONITORING_INSTANCE_s::localVoltages to #mxm_tableCellVoltages and copying
82  * these entries into the database is handled by #MXM_ParseVoltagesIntoDB().
83  */
85 
86 /**
87  * @brief Local cell temperature data block
88  * @details This local instance stores the measured cell temperatures.
89  */
92 
93 /**
94  * @brief Balancing control
95  * @details This variable stores information about which cells need balancing
96  */
98 
99 /** @brief Local data structure for openwire results. */
101 
102 /** balancing state variable */
105 };
106 
107 /** state variable for the Maxim monitoring driver */
110  .pInstance41B = &mxm_41bState,
111  .pInstance5X = &mxm_5xState,
112  .pCellVoltages_table = &mxm_tableCellVoltages,
113  .pCellTemperatures_table = &mxm_tableCellTemperatures,
114  .pOpenwire_table = &mxm_tableOpenWire,
115 };
116 
117 /*========== Extern Constant and Variable Definitions =======================*/
118 
119 /*========== Static Function Prototypes =====================================*/
120 
121 /**
122  * @brief Tick function
123  * @details This function is the central entry-point for this driver. It calls
124  * the state-machines via the functions #MXM_StateMachine(),
125  * #MXM_5XStateMachine() and #MXM_41BStateMachine().
126  *
127  * The state-machines are called in such a way that the main blocking
128  * point is the SPI interface. (That means the order is optimized so
129  * that every cycle a SPI command should be available for sending or
130  * receiving as this is the limiting ressource.)
131  */
132 static void MXM_Tick(void);
133 
134 /**
135  * @brief Initialize the state structs
136  * @details This function sets default values to the members of the state
137  * structs. It can be used to reset the driver to a default value.
138  */
139 static void MXM_SetStateStructDefaultValues(void);
140 
141 /*========== Static Function Implementations ================================*/
142 
143 static void MXM_Tick(void) {
144  /** counter variable for the number of all instances of this driver */
145  static int8_t mxm_numberOfInstances = 0;
146 
147  /* TODO once all states are covered in the instance structs, this reentry check can be removed */
148  FAS_ASSERT(mxm_numberOfInstances == 0);
149  mxm_numberOfInstances++;
164  FAS_ASSERT(mxm_numberOfInstances == 1); /* there should be exactly one instance running at the moment */
165  mxm_numberOfInstances--;
166 }
167 
172 }
173 
174 /*========== Extern Function Implementations ================================*/
176  if (mxm_state.resetNecessary == true) {
177  /* a reset has been requested */
178 
180  /* save allowStartup and operationRequested */
181  const bool allowStartup = mxm_state.allowStartup;
182  const bool operationRequested = mxm_state.operationRequested;
183  const bool firstMeasurementDone = mxm_state.firstMeasurementDone;
184 
185  (void)AFE_Initialize();
186 
187  /* restore relevant flags */
188  mxm_state.allowStartup = allowStartup;
189  /* if a first measurement has been done, operation has been requested before */
190  mxm_state.operationRequested = (operationRequested || firstMeasurementDone);
191 
193  }
194  MXM_Tick();
195  return STD_OK;
196 }
197 
201  return STD_OK;
202 }
203 
205  STD_RETURN_TYPE_e retval = STD_OK;
206 
208  mxm_state.allowStartup = true;
211 
212  return retval;
213 }
214 
217  const bool firstMeasurementDone = mxm_state.firstMeasurementDone;
219  return firstMeasurementDone;
220 }
221 
222 extern STD_RETURN_TYPE_e AFE_RequestIoWrite(uint8_t string) {
223  FAS_ASSERT(string < BS_NR_OF_STRINGS);
224  return STD_NOT_OK;
225 }
226 
227 extern STD_RETURN_TYPE_e AFE_RequestIoRead(uint8_t string) {
228  FAS_ASSERT(string < BS_NR_OF_STRINGS);
229  return STD_NOT_OK;
230 }
231 
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 
242 extern STD_RETURN_TYPE_e AFE_RequestEepromRead(uint8_t string) {
243  FAS_ASSERT(string < BS_NR_OF_STRINGS);
244  return STD_NOT_OK;
245 }
246 
248  FAS_ASSERT(string < BS_NR_OF_STRINGS);
249  return STD_NOT_OK;
250 }
251 
253  FAS_ASSERT(string < BS_NR_OF_STRINGS);
254  STD_RETURN_TYPE_e retval = STD_OK;
255 
257 
258  return retval;
259 }
260 
261 /*========== Externalized Static Function Implementations (Unit Test) =======*/
AFE driver header.
#define BS_NR_OF_STRINGS
@ DATA_BLOCK_ID_BALANCING_CONTROL
Definition: database_cfg.h:77
@ DATA_BLOCK_ID_CELL_TEMPERATURE_BASE
Definition: database_cfg.h:95
@ DATA_BLOCK_ID_OPEN_WIRE_BASE
Definition: database_cfg.h:81
@ DATA_BLOCK_ID_CELL_VOLTAGE_BASE
Definition: database_cfg.h:94
#define FAS_ASSERT(x)
Assertion macro that asserts that x is true.
Definition: fassert.h:235
@ STD_NOT_OK
Definition: fstd_types.h:82
@ STD_OK
Definition: fstd_types.h:81
enum STD_RETURN_TYPE STD_RETURN_TYPE_e
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:910
void MXM_41BInitializeStateStruct(MXM_41B_INSTANCE_s *pInstance)
Initializes the state struct with default values.
Definition: mxm_17841b.c:956
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_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
Headers for the driver for the MAX17841B ASCI and MAX1785x monitoring chip.
static void MXM_Tick(void)
Tick function.
Definition: mxm_afe.c:143
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:242
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:247
static DATA_BLOCK_CELL_VOLTAGE_s mxm_tableCellVoltages
Local cell voltage data block.
Definition: mxm_afe.c:84
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:227
STD_RETURN_TYPE_e AFE_TriggerIc(void)
Definition: mxm_afe.c:175
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:252
bool AFE_IsFirstMeasurementCycleFinished(void)
Checks if the first AFE measurement cycle was made.
Definition: mxm_afe.c:215
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:237
static DATA_BLOCK_BALANCING_CONTROL_s mxm_tableBalancingControl
Balancing control.
Definition: mxm_afe.c:97
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:232
static MXM_5X_INSTANCE_s mxm_5xState
Definition: mxm_afe.c:72
static MXM_41B_INSTANCE_s mxm_41bState
Definition: mxm_afe.c:68
static MXM_BALANCING_STATE_s mxm_balancingState
Definition: mxm_afe.c:103
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:222
static void MXM_SetStateStructDefaultValues(void)
Initialize the state structs.
Definition: mxm_afe.c:168
STD_RETURN_TYPE_e AFE_Initialize(void)
Definition: mxm_afe.c:198
static DATA_BLOCK_CELL_TEMPERATURE_s mxm_tableCellTemperatures
Local cell temperature data block.
Definition: mxm_afe.c:90
STD_RETURN_TYPE_e AFE_StartMeasurement(void)
Makes the initialization request to the AFE state machine.
Definition: mxm_afe.c:204
static DATA_BLOCK_OPEN_WIRE_s mxm_tableOpenWire
Local data structure for openwire results.
Definition: mxm_afe.c:100
static MXM_MONITORING_INSTANCE_s mxm_state
Definition: mxm_afe.c:108
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:73
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:125
void OS_EnterTaskCritical(void)
Enter Critical interface function for use in FreeRTOS-Tasks and FreeRTOS-ISR.
Definition: os_freertos.c:121
DATA_BLOCK_ID_e uniqueId
Definition: database_cfg.h:111
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:238
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:137
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:121
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:290
Struct for the state-variable of state-machine.
Definition: mxm_17841b.h:157
5x statemachine structure
DATA_BLOCK_BALANCING_CONTROL_s *const pBalancingControl_table
MXM_41B_INSTANCE_s *const pInstance41B
MXM_BALANCING_STATE_s *const pBalancingState
MXM_5X_INSTANCE_s *const pInstance5X