foxBMS - Unit Tests  1.6.0
The foxBMS Unit Tests API Documentation
test_mxm_1785x.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 test_mxm_1785x.c
44  * @author foxBMS Team
45  * @date 2020-07-02 (date of creation)
46  * @updated 2023-10-12 (date of last update)
47  * @version v1.6.0
48  * @ingroup UNIT_TEST_IMPLEMENTATION
49  * @prefix MXM
50  *
51  * @brief Test for the Maxim 1785x driver.
52  *
53  * @details def
54  *
55  */
56 
57 /*========== Includes =======================================================*/
58 #include "unity.h"
59 #include "Mockafe_plausibility.h"
60 #include "Mockdatabase.h"
61 #include "Mockdiag.h"
62 #include "Mockfassert.h"
63 #include "Mockmxm_17841b.h"
64 #include "Mockmxm_41b_register_map.h"
65 #include "Mockmxm_battery_management.h"
66 #include "Mockmxm_cfg.h"
67 #include "Mockmxm_crc8.h"
68 #include "Mockmxm_registry.h"
69 #include "Mockos.h"
70 #include "Mocktsi.h"
71 
72 #include "database_cfg.h"
73 
74 #include "mxm_1785x.h"
75 #include "mxm_1785x_tools.h"
76 #include "test_assert_helper.h"
77 
78 #include <stdbool.h>
79 #include <stdint.h>
80 
81 /*========== Unit Testing Framework Directives ==============================*/
82 TEST_SOURCE_FILE("test_mxm_1785x")
83 
84 TEST_INCLUDE_PATH("../../src/app/driver/afe/api")
85 TEST_INCLUDE_PATH("../../src/app/driver/afe/maxim/common")
86 TEST_INCLUDE_PATH("../../src/app/driver/afe/maxim/common/config")
87 TEST_INCLUDE_PATH("../../src/app/driver/config")
88 TEST_INCLUDE_PATH("../../src/app/driver/spi")
89 TEST_INCLUDE_PATH("../../src/app/driver/ts/api")
90 TEST_INCLUDE_PATH("../../src/app/engine/diag")
91 
92 /*========== Definitions and Implementations for Unit Test ==================*/
93 
95 
98  .operationSubstate = MXM_INIT_ENTRY,
99  .allowStartup = false,
100  .operationRequested = false,
101  .firstMeasurementDone = false,
102  .stopRequested = false,
103  .openWireRequested = false,
104  .undervoltageAlert = false,
105  .dcByte = MXM_DC_EMPTY,
106  .mxmVoltageCellCounter = 0,
107  .highest5xDevice = 0,
108  .requestStatus5x = MXM_5X_STATE_UNSENT,
109  .batteryCmdBuffer =
110  {
111  .regAddress = (MXM_REG_NAME_e)0x00,
112  .lsb = 0x00,
113  .msb = 0x00,
114  .deviceAddress = 0x00,
115  .blocksize = 0,
116  .model = MXM_MODEL_ID_MAX17852,
117  },
118  .resultSelfCheck = STD_NOT_OK,
119  .selfCheck =
120  {
121  .crc = STD_NOT_OK,
122  .conv = STD_NOT_OK,
123  .firstSetBit = STD_NOT_OK,
124  .extractValueFromRegister = STD_NOT_OK,
125  .parseVoltageReadall = STD_NOT_OK,
126  .addressSpaceChecker = STD_NOT_OK,
127  .fmeaStatusASCI = STD_NOT_OK,
128  },
129  .pOpenWire_table = &mxm_tableOpenWire,
130 };
131 
133 
134 /** dummy implementation for the Operation state machine */
136 }
137 
138 /** local variable that allows to change the reported model id on a per test basis*/
140 
141 /** dummy implementation for the model id */
143  return modelForTest;
144 }
145 
146 /*========== Setup and Teardown =============================================*/
147 void setUp(void) {
151 
153 
154  /* delete rx buffer */
155  for (uint16_t i = 0u; i < MXM_RX_BUFFER_LENGTH; i++) {
156  mxm_instance.rxBuffer[i] = 0u;
157  }
158 }
159 
160 void tearDown(void) {
161 }
162 
163 /*========== Test Cases =====================================================*/
166 }
167 
168 /** This test aims to provoke the assertion that checks if the battery system fits the internal configuration */
170  /* skip this test if the BMS configuration does not fit to this test
171  (we cannot run with more cells than Maxim supports) */
173  TEST_PASS_MESSAGE("This test is skipped due to the configuration of the BMS.");
174  }
177  MXM_BALANCING_STATE_s balancingInstance = {.pBalancingControl_table = &balancingTable};
179 }
180 
182  uint8_t voltRxBuffer = 0;
183  MXM_DATA_STORAGE_s datastorage = {0};
184 
186 
188 }
189 
192 }
193 
196 }
197 
199  TEST_ASSERT_FAIL_ASSERT(TEST_ASSERT_EQUAL(0u, MXM_MonGetVoltages(NULL_PTR, 0u)));
200 }
201 
204 }
205 
207  /* set request status to an invalid value */
210 }
211 
214  MXM_5XSetStateRequest_ExpectAndReturn(
219  STD_OK);
221 }
222 
226 }
227 
230  OS_GetTickCount_ExpectAndReturn(0u);
233 }
234 
239  TEST_ASSERT_EQUAL(42, mxm_instance.operationSubstate);
240 }
241 
243  TEST_ASSERT_FAIL_ASSERT(TEST_ASSERT_EQUAL(false, MXM_HandleStateReadall(NULL_PTR, 0u, 0u)));
244 }
245 
247  /* set request status to an invalid value */
249  TEST_ASSERT_FAIL_ASSERT(TEST_ASSERT_EQUAL(false, MXM_HandleStateReadall(&mxm_instance, 21u, 42u)));
250 }
251 
254  const uint8_t registerAddress = 21u;
255  /* recreate a local variant of the expected command buffer */
257  .regAddress = registerAddress,
258  .lsb = 0,
259  .msb = 0,
260  .model = modelForTest,
261  };
262  MXM_5XSetStateRequest_ExpectAndReturn(
264  bool functionReturnValue;
265  TEST_ASSERT_PASS_ASSERT(functionReturnValue = MXM_HandleStateReadall(&mxm_instance, registerAddress, 42u));
266  TEST_ASSERT_EQUAL(false, functionReturnValue);
267 }
268 
271  bool functionReturnValue;
272  TEST_ASSERT_PASS_ASSERT(functionReturnValue = MXM_HandleStateReadall(&mxm_instance, 21u, 42u));
273  TEST_ASSERT_EQUAL(false, functionReturnValue);
274 }
275 
278  OS_GetTickCount_ExpectAndReturn(0u);
279  bool functionReturnValue;
280  TEST_ASSERT_PASS_ASSERT(functionReturnValue = MXM_HandleStateReadall(&mxm_instance, 21u, 42u));
281  TEST_ASSERT_EQUAL(false, functionReturnValue);
283 }
284 
287  MXM_5XGetRXBuffer_ExpectAndReturn(mxm_instance.pInstance5X, mxm_instance.rxBuffer, MXM_RX_BUFFER_LENGTH, STD_OK);
288  MXM_5XGetLastDCByte_ExpectAndReturn(mxm_instance.pInstance5X, 84u);
289  bool functionReturnValue;
290  TEST_ASSERT_PASS_ASSERT(functionReturnValue = MXM_HandleStateReadall(&mxm_instance, 21u, 42u));
291  TEST_ASSERT_EQUAL(true, functionReturnValue);
293  TEST_ASSERT_EQUAL(42u, mxm_instance.operationSubstate);
294  TEST_ASSERT_EQUAL(84u, mxm_instance.dcByte);
295 }
296 
299 }
300 
302  /* with only one module, the entry of the LSB should be in the third
303  position of the RX buffer and the MSB in the fourth */
304  mxm_instance.rxBuffer[2] = 85; /* 0b01010101 */
305  mxm_instance.rxBuffer[3] = 170; /* 0b10101010 */
306  /* simulate 1 satellite */
307  MXM_5XGetNumberOfSatellites_ExpectAndReturn(mxm_instance.pInstance5X, 1);
308  /* don't care about the database call */
309  DATA_Write1DataBlock_IgnoreAndReturn(STD_OK);
311 
312  /* check for the injected pattern */
313  for (uint8_t i = 0u; i < 8; i = i + 2) {
314  TEST_ASSERT_EQUAL(1, mxm_instance.pOpenWire_table->openWire[0][i]);
315  }
316  for (uint8_t i = 1u; i < 8; i = i + 2) {
317  TEST_ASSERT_EQUAL(0, mxm_instance.pOpenWire_table->openWire[0][i]);
318  }
319  for (uint8_t i = 8u; i < MXM_MAXIMUM_NR_OF_CELLS_PER_MODULE; i = i + 2) {
320  TEST_ASSERT_EQUAL(0, mxm_instance.pOpenWire_table->openWire[0][i]);
321  }
322  for (uint8_t i = 9u; i < MXM_MAXIMUM_NR_OF_CELLS_PER_MODULE; i = i + 2) {
323  TEST_ASSERT_EQUAL(1, mxm_instance.pOpenWire_table->openWire[0][i]);
324  }
325 }
326 
329 }
#define BS_NR_OF_CELL_BLOCKS_PER_MODULE
number of cells per module
Database configuration header.
@ DATA_BLOCK_ID_BALANCING_CONTROL
Definition: database_cfg.h:83
@ DATA_BLOCK_ID_OPEN_WIRE_BASE
Definition: database_cfg.h:103
static DIAG_DIAGNOSIS_STATE_s diag
Definition: diag.c:71
@ STD_NOT_OK
Definition: fstd_types.h:84
@ STD_OK
Definition: fstd_types.h:83
#define NULL_PTR
Null pointer.
Definition: fstd_types.h:77
bool GEN_MUST_CHECK_RETURN MXM_HandleStateReadall(MXM_MONITORING_INSTANCE_s *pInstance, MXM_REG_NAME_e registerName, MXM_STATEMACHINE_OPERATION_STATES_e nextState)
Handle the state machine-transactions for a READALL.
Definition: mxm_1785x.c:948
STD_RETURN_TYPE_e MXM_ProcessOpenWire(const MXM_MONITORING_INSTANCE_s *const kpkInstance)
Processes the retrieved information on open wire.
Definition: mxm_1785x.c:979
STD_RETURN_TYPE_e TEST_MXM_ParseVoltagesIntoDB(MXM_MONITORING_INSTANCE_s *pInstance)
Definition: mxm_1785x.c:1431
STD_RETURN_TYPE_e MXM_ConstructBalancingBuffer(MXM_BALANCING_STATE_s *pBalancingInstance)
Fill the balancing data structure.
Definition: mxm_1785x.c:1024
void MXM_StateMachine(MXM_MONITORING_INSTANCE_s *pInstance)
Main state-machine implementation.
Definition: mxm_1785x.c:1319
MXM_MONITORING_STATE_e GEN_MUST_CHECK_RETURN MXM_MonGetVoltages(MXM_MONITORING_INSTANCE_s *pState, MXM_REG_NAME_e regAddress)
Encapsulation for reading voltages from a register.
Definition: mxm_1785x.c:1280
STD_RETURN_TYPE_e TEST_MXM_ParseVoltageReadallTest(MXM_MONITORING_INSTANCE_s *pInstance)
Definition: mxm_1785x.c:1428
void MXM_HandleStateWriteall(MXM_MONITORING_INSTANCE_s *pInstance, MXM_STATEMACHINE_OPERATION_STATES_e nextState)
Handle the state machine-transactions for a WRITEALL.
Definition: mxm_1785x.c:920
STD_RETURN_TYPE_e TEST_MXM_ParseVoltageReadall(uint8_t *voltRxBuffer, uint16_t voltRxBufferLength, MXM_DATA_STORAGE_s *datastorage, MXM_CONVERSION_TYPE_e conversionType)
Definition: mxm_1785x.c:1434
Headers for the driver for the MAX17841B ASCI and MAX1785x analog front-end.
This is a collection of helper functions for the MAX1785x ICs.
@ MXM_INIT_ENTRY
@ MXM_STATEMACHINE_STATES_UNINITIALIZED
#define MXM_RX_BUFFER_LENGTH
Length of RX buffer.
MXM_MODEL_ID_e
Type of monitoring device.
@ MXM_MODEL_ID_MAX17852
#define MXM_MAXIMUM_NR_OF_CELLS_PER_MODULE
Maximum number of cells per module.
@ MXM_DC_EMPTY
@ MXM_STATEMACH_5X_READALL
@ MXM_STATEMACH_5X_WRITEALL
@ MXM_5X_STATE_UNSENT
@ MXM_5X_STATE_PROCESSED
@ MXM_5X_STATE_UNPROCESSED
@ MXM_5X_STATE_ERROR
MXM_REG_NAME_e
MAX1785x register names.
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:255
DATA_BLOCK_ID_e uniqueId
Definition: database_cfg.h:125
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:310
uint8_t openWire[BS_NR_OF_STRINGS][BS_NR_OF_MODULES_PER_STRING *(BS_NR_OF_CELL_BLOCKS_PER_MODULE+1u)]
Definition: database_cfg.h:315
DATA_BLOCK_BALANCING_CONTROL_s *const pBalancingControl_table
MXM_5X_COMMAND_PAYLOAD_s batteryCmdBuffer
DATA_BLOCK_OPEN_WIRE_s *const pOpenWire_table
MXM_STATEMACHINE_OPERATION_STATES_e operationSubstate
uint8_t rxBuffer[MXM_RX_BUFFER_LENGTH]
MXM_STATEMACHINE_STATES_e state
MXM_5X_INSTANCE_s *const pInstance5X
MXM_5X_STATE_REQUEST_STATUS_e requestStatus5x
Helper for unit tests.
#define TEST_ASSERT_PASS_ASSERT(_code_under_test)
assert whether assert macro has passed
#define TEST_ASSERT_FAIL_ASSERT(_code_under_test)
assert whether assert macro has failed
uint16_t commandBuffer[10]
void testMXM_ParseVoltageReadallTestNullPointer(void)
void testMXM_HandleStateWriteallInvalidRequestStatus(void)
void testMXM_HandleStateReadallError(void)
const bool mxm_allowSkippingPostInitSelfCheck
void testMXM_HandleStateReadallInvalidRequestStatus(void)
void testMXM_HandleStateReadallNullPointer(void)
void testMXM_StateMachineNullPointer(void)
void testMXM_ConstructBalancingBufferTest(void)
void testMXM_HandleStateWriteallError(void)
void testMXM_HandleStateWriteallEntry(void)
MXM_MODEL_ID_e MXM_GetModelIdOfDaisyChain(void)
returns the model ID of the daisy chain
void MXM_StateMachineOperation(MXM_MONITORING_INSTANCE_s *pState)
State-Machine implementation for operation state.
void testMXM_ParseVoltagesIntoDBNullPointer(void)
void testMXM_HandleStateReadallUnsent(void)
static MXM_MONITORING_INSTANCE_s mxm_instance
void testMXM_ParseVoltageReadallTest(void)
void testTEST_MXM_ParseVoltageReadallNullPointer(void)
void setUp(void)
void tearDown(void)
void testMXM_HandleStateReadallUnprocessed(void)
static MXM_MODEL_ID_e modelForTest
void testMXM_MonGetVoltagesNullPointer(void)
void testMXM_ProcessOpenWire1SatelliteAlternatingPattern(void)
void testMXM_HandleStateWriteallProcessed(void)
void testMXM_ProcessOpenWireNullPointer(void)
static DATA_BLOCK_OPEN_WIRE_s mxm_tableOpenWire
void testMXM_HandleStateReadallProcessed(void)
void testMXM_HandleStateWriteallUnprocessed(void)
void testMXM_HandleStateWriteallNullPointer(void)