foxBMS-UnitTests  1.0.0
The foxBMS Unit Tests API Documentation
mxm_mic.c
Go to the documentation of this file.
1 /**
2  *
3  * @copyright © 2010 - 2021, Fraunhofer-Gesellschaft zur Foerderung der
4  * angewandten Forschung e.V. All rights reserved.
5  *
6  * BSD 3-Clause License
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  * 1. Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the copyright holder nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  *
30  * We kindly request you to use one or more of the following phrases to refer
31  * to foxBMS in your hardware, software, documentation or advertising
32  * materials:
33  *
34  * ″This product uses parts of foxBMS®″
35  *
36  * ″This product includes parts of foxBMS®″
37  *
38  * ″This product is derived from foxBMS®″
39  *
40  */
41 
42 /**
43  * @file mxm_mic.c
44  * @author foxBMS Team
45  * @date 2020-06-16 (date of creation)
46  * @updated 2020-06-18 (date of last update)
47  * @ingroup DRIVER
48  * @prefix MIC
49  *
50  * @brief Measurement IC driver implementation
51  */
52 
53 /*========== Includes =======================================================*/
54 #include "general.h"
55 
56 #include "mic.h"
57 #include "mxm_17841b.h"
58 #include "mxm_1785x.h"
59 #include "mxm_battery_management.h"
60 
61 /*========== Macros and Definitions =========================================*/
62 
63 /*========== Static Constant and Variable Definitions =======================*/
64 
65 /** state variable for the MAX17841B driver */
68  .substate = MXM_41B_ENTRY_SUBSTATE,
69  .aliveCounter = 0,
70  .extendMessageBytes = 0,
71  .waitCounter = 0,
72  .regRXIntEnable = 0x00u,
73  .regTXIntEnable = 0x00u,
74  .regRXStatus = 0x00u,
75  .regTXStatus = 0x00u,
76  .regConfig1 = 0x60u,
77  .regConfig2 = 0x10u,
78  .regConfig3 = 0x0Fu,
79  .spiRXBuffer = {0},
80  .spiTXBuffer = {0},
81 };
82 
83 /** state variable for the Battery Management protocol driver */
86  .substate = MXM_5X_ENTRY_SUBSTATE,
87  .commandPayload =
88  {
89  .regAddress = (MXM_REG_NAME_e)0x00u,
90  .lsb = 0x00u,
91  .msb = 0x00u,
92  },
93  .status41b = MXM_41B_STATE_UNSENT,
94  .numberOfSatellites = 0,
95  .numberOfSatellitesIsGood = STD_NOT_OK,
96  .lastDCByte = 0,
97 };
98 
99 /** state variable for the Maxim monitoring driver */
102  .operationSubstate = MXM_INIT_DEVCFG1,
103  .allowStartup = false,
104  .operationRequested = false,
105  .firstMeasurementDone = false,
106  .stopRequested = false,
107  .openwireRequested = false,
108  .undervoltageAlert = false,
109  .dcByte = MXM_DC_EMPTY,
110  .mxmVoltageCellCounter = 0,
111  .highest5xDevice = 0,
112  .requestStatus5x = MXM_5X_STATE_UNSENT,
113  .batteryCmdBuffer =
114  {
115  .regAddress = (MXM_REG_NAME_e)0x00,
116  .lsb = 0x00,
117  .msb = 0x00,
118  .deviceAddress = 0x00,
119  .blocksize = 0,
120  },
121  .resultSelfCheck = STD_NOT_OK,
122  .selfCheck =
123  {
124  .crc = STD_NOT_OK,
125  .conv = STD_NOT_OK,
126  .firstSetBit = STD_NOT_OK,
127  .extractValueFromRegister = STD_NOT_OK,
128  .parseVoltageReadall = STD_NOT_OK,
129  .addressSpaceChecker = STD_NOT_OK,
130  .fmeaStatusASCI = STD_NOT_OK,
131  },
132  .pInstance41B = &mxm_41b_state,
133  .pInstance5X = &mxm_5x_state,
134  .localVoltages = {0},
135 };
136 
137 /** counter variable for the number of all instances of this driver */
138 static int32_t mxm_numberOfInstances = 0;
139 
140 /*========== Extern Constant and Variable Definitions =======================*/
141 
142 /*========== Static Function Prototypes =====================================*/
143 
144 /**
145  * @brief Tick function
146  * @details This function is the central entry-point for this driver. It calls
147  * the state-machines via the functions #MXM_StateMachine(),
148  * #MXM_5XStateMachine() and #MXM_41BStateMachine().
149  *
150  * The state-machines are called in such a way that the main blocking
151  * point is the SPI interface. (That means the order is optimized so
152  * that every cycle a SPI command should be available for sending or
153  * receiving as this is the limiting ressource.)
154  */
155 static void MXM_Tick(void);
156 
157 /*========== Static Function Implementations ================================*/
158 
159 static void MXM_Tick() {
161  /* TODO let statemachines recover from hangups */
162  /* TODO best execution order of statemachines? */
163  /* DIAG_Handler(DIAG_SYSMON_LTC_ID, 0); */
177  /* MXM_41BStateMachine(); */
179 }
180 
181 /*========== Extern Function Implementations ================================*/
183  MXM_Tick();
184  return STD_OK;
185 }
186 
189  return STD_OK;
190 }
191 
193  STD_RETURN_TYPE_e retval = STD_OK;
194 
195  mxm_state.allowStartup = true;
197 
198  return retval;
199 }
200 
203 }
204 
205 extern STD_RETURN_TYPE_e MIC_RequestIoWrite(uint8_t string) {
206  return STD_NOT_OK;
207 }
208 
209 extern STD_RETURN_TYPE_e MIC_RequestIoRead(uint8_t string) {
210  return STD_NOT_OK;
211 }
212 
214  return STD_NOT_OK;
215 }
216 
218  return STD_NOT_OK;
219 }
220 
221 extern STD_RETURN_TYPE_e MIC_RequestEepromRead(uint8_t string) {
222  return STD_NOT_OK;
223 }
224 
226  return STD_NOT_OK;
227 }
228 
230  STD_RETURN_TYPE_e retval = STD_OK;
231 
233 
234  return retval;
235 }
236 
237 /*========== Externalized Static Function Implementations (Unit Test) =======*/
MIC_RequestOpenWireCheck
STD_RETURN_TYPE_e MIC_RequestOpenWireCheck(uint8_t string)
Makes the request to the MIC state machine to perform open-wire check.
Definition: mxm_mic.c:229
MIC_StartMeasurement
STD_RETURN_TYPE_e MIC_StartMeasurement(void)
Makes the initialization request to the MIC state machine.
Definition: mxm_mic.c:192
MXM_5X_ENTRY_SUBSTATE
@ MXM_5X_ENTRY_SUBSTATE
Definition: mxm_battery_management.h:167
MXM_5X_INSTANCE
5x statemachine structure
Definition: mxm_battery_management.h:251
general.h
TODO.
MXM_MONITORING_INSTANCE::pInstance5X
MXM_5X_INSTANCE_s * pInstance5X
Definition: mxm_1785x_tools.h:206
MIC_IsFirstMeasurementCycleFinished
bool MIC_IsFirstMeasurementCycleFinished(void)
Checks if the first MIC measurement cycle was made.
Definition: mxm_mic.c:201
MXM_41BStateMachine
void MXM_41BStateMachine(MXM_41B_INSTANCE_s *pInstance)
Execute state-machine for the MAX17841B.
Definition: mxm_17841b.c:428
MXM_MONITORING_INSTANCE
Definition: mxm_1785x_tools.h:188
MIC_RequestEepromWrite
STD_RETURN_TYPE_e MIC_RequestEepromWrite(uint8_t string)
Makes the request to the MIC state machine to write to the external EEPROM on slaves.
Definition: mxm_mic.c:225
mxm_17841b.h
Headers for the driver for the MAX17841B ASCI and MAX1785x monitoring chip.
STD_RETURN_TYPE_e
enum STD_RETURN_TYPE STD_RETURN_TYPE_e
MXM_DC_EMPTY
@ MXM_DC_EMPTY
Definition: mxm_basic_defines.h:115
MXM_41B_ENTRY_SUBSTATE
@ MXM_41B_ENTRY_SUBSTATE
Definition: mxm_17841b.h:98
mxm_numberOfInstances
static int32_t mxm_numberOfInstances
Definition: mxm_mic.c:138
MXM_STATEMACH_5X_UNINITIALIZED
@ MXM_STATEMACH_5X_UNINITIALIZED
Definition: mxm_battery_management.h:152
MIC_Init
STD_RETURN_TYPE_e MIC_Init(void)
Definition: mxm_mic.c:187
MIC_RequestIoRead
STD_RETURN_TYPE_e MIC_RequestIoRead(uint8_t string)
Makes the request to the MIC state machine to read from the IO port-expander.
Definition: mxm_mic.c:209
MXM_STATEMACH_41B_UNINITIALIZED
@ MXM_STATEMACH_41B_UNINITIALIZED
Definition: mxm_17841b.h:81
mxm_5x_state
static MXM_5X_INSTANCE_s mxm_5x_state
Definition: mxm_mic.c:84
MXM_MONITORING_INSTANCE::state
MXM_STATEMACHINE_STATES_e state
Definition: mxm_1785x_tools.h:189
MXM_MONITORING_INSTANCE::openwireRequested
bool openwireRequested
Definition: mxm_1785x_tools.h:195
mic.h
Measurement IC driver header.
STD_OK
@ STD_OK
Definition: fstd_types.h:72
mxm_41b_state
static MXM_41B_INSTANCE_s mxm_41b_state
Definition: mxm_mic.c:66
STD_NOT_OK
@ STD_NOT_OK
Definition: fstd_types.h:73
MXM_41B_INSTANCE::state
MXM_STATEMACH_41B_e state
Definition: mxm_17841b.h:150
MXM_5X_INSTANCE::state
MXM_STATEMACHINE_5X_e state
Definition: mxm_battery_management.h:252
MXM_41B_INSTANCE
Struct for the state-variable of state-machine.
Definition: mxm_17841b.h:149
MIC_RequestTemperatureRead
STD_RETURN_TYPE_e MIC_RequestTemperatureRead(uint8_t string)
Makes the request to the MIC state machine to read from the external temperature sensor on slaves.
Definition: mxm_mic.c:213
MIC_RequestBalancingFeedbackRead
STD_RETURN_TYPE_e MIC_RequestBalancingFeedbackRead(uint8_t string)
Makes the request to the MIC state machine to read balancing feedback from the slaves.
Definition: mxm_mic.c:217
MXM_StateMachine
void MXM_StateMachine(MXM_MONITORING_INSTANCE_s *pInstance)
Battery monitoring driver for MAX1785x battery monitoring ICs.
Definition: mxm_1785x.c:1824
MXM_MonitoringPinInit
void MXM_MonitoringPinInit(void)
Initialize the pins connected to the MAX17841B.
Definition: mxm_cfg.c:72
MXM_MONITORING_INSTANCE::allowStartup
bool allowStartup
Definition: mxm_1785x_tools.h:191
MXM_REG_NAME_e
enum MXM_REG_NAME MXM_REG_NAME_e
MAX1785x register names.
MXM_MONITORING_INSTANCE::operationRequested
bool operationRequested
Definition: mxm_1785x_tools.h:192
MIC_TriggerIc
STD_RETURN_TYPE_e MIC_TriggerIc(void)
Definition: mxm_mic.c:182
MXM_Tick
static void MXM_Tick(void)
Tick function.
Definition: mxm_mic.c:159
mxm_state
static MXM_MONITORING_INSTANCE_s mxm_state
Definition: mxm_mic.c:100
MXM_MONITORING_INSTANCE::firstMeasurementDone
bool firstMeasurementDone
Definition: mxm_1785x_tools.h:193
MIC_RequestEepromRead
STD_RETURN_TYPE_e MIC_RequestEepromRead(uint8_t string)
Makes the request to the MIC state machine to read from the external EEPROM on slaves.
Definition: mxm_mic.c:221
MIC_RequestIoWrite
STD_RETURN_TYPE_e MIC_RequestIoWrite(uint8_t string)
Makes the request to the MIC state machine to write to the IO port-expander.
Definition: mxm_mic.c:205
MXM_5X_STATE_UNSENT
@ MXM_5X_STATE_UNSENT
Definition: mxm_battery_management.h:230
MXM_MONITORING_INSTANCE::pInstance41B
MXM_41B_INSTANCE_s * pInstance41B
Definition: mxm_1785x_tools.h:205
MXM_INIT_DEVCFG1
@ MXM_INIT_DEVCFG1
Definition: mxm_1785x_tools.h:86
mxm_1785x.h
Headers for the driver for the MAX17841B ASCI and MAX1785x monitoring chip.
MXM_STATEMACHINE_STATES_UNINITIALIZED
@ MXM_STATEMACHINE_STATES_UNINITIALIZED
Definition: mxm_1785x_tools.h:75
MXM_5XStateMachine
void MXM_5XStateMachine(MXM_41B_INSTANCE_s *pInstance41b, MXM_5X_INSTANCE_s *pInstance5x)
Execute state-machine for Battery Management Protocol.
Definition: mxm_battery_management.c:412
MXM_41B_STATE_UNSENT
@ MXM_41B_STATE_UNSENT
Definition: mxm_17841b.h:124
mxm_battery_management.h
Headers for the driver for the MAX17841B ASCI and MAX1785x monitoring chip.