foxBMS - Unit Tests  1.3.0
The foxBMS Unit Tests API Documentation
test_mxm_1785x.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 test_mxm_1785x.c
44  * @author foxBMS Team
45  * @date 2020-07-02 (date of creation)
46  * @updated 2022-05-30 (date of last update)
47  * @version v1.3.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 /*========== Definitions and Implementations for Unit Test ==================*/
79 
81 
84  .operationSubstate = MXM_INIT_ENTRY,
85  .allowStartup = false,
86  .operationRequested = false,
87  .firstMeasurementDone = false,
88  .stopRequested = false,
89  .openwireRequested = false,
90  .undervoltageAlert = false,
91  .dcByte = MXM_DC_EMPTY,
92  .mxmVoltageCellCounter = 0,
93  .highest5xDevice = 0,
94  .requestStatus5x = MXM_5X_STATE_UNSENT,
95  .batteryCmdBuffer =
96  {
97  .regAddress = (MXM_REG_NAME_e)0x00,
98  .lsb = 0x00,
99  .msb = 0x00,
100  .deviceAddress = 0x00,
101  .blocksize = 0,
102  .model = MXM_MODEL_ID_MAX17852,
103  },
104  .resultSelfCheck = STD_NOT_OK,
105  .selfCheck =
106  {
107  .crc = STD_NOT_OK,
108  .conv = STD_NOT_OK,
109  .firstSetBit = STD_NOT_OK,
110  .extractValueFromRegister = STD_NOT_OK,
111  .parseVoltageReadall = STD_NOT_OK,
112  .addressSpaceChecker = STD_NOT_OK,
113  .fmeaStatusASCI = STD_NOT_OK,
114  },
115  .pOpenwire_table = &mxm_tableOpenWire,
116 };
117 
119 
120 /** dummy implementation for the Operation state machine */
122 }
123 
124 /** local variable that allows to change the reported model id on a per test basis*/
126 
127 /** dummy implementation for the model id */
129  return modelForTest;
130 }
131 
132 /*========== Setup and Teardown =============================================*/
133 void setUp(void) {
137 
139 
140  /* delete rx buffer */
141  for (uint16_t i = 0u; i < MXM_RX_BUFFER_LENGTH; i++) {
142  mxm_instance.rxBuffer[i] = 0u;
143  }
144 }
145 
146 void tearDown(void) {
147 }
148 
149 /*========== Test Cases =====================================================*/
152 }
153 
154 /** This test aims to provoke the assertion that checks if the battery system fits the internal configuration */
156  /* skip this test if the BMS configuration does not fit to this test
157  (we cannot run with more cells than Maxim supports) */
159  TEST_PASS_MESSAGE("This test is skipped due to the configuration of the BMS.");
160  }
163  MXM_BALANCING_STATE_s balancingInstance = {.pBalancingControl_table = &balancingTable};
165 }
166 
168  uint8_t voltRxBuffer = 0;
169  MXM_DATA_STORAGE_s datastorage = {0};
170 
172 
174 }
175 
178 }
179 
182 }
183 
185  TEST_ASSERT_FAIL_ASSERT(TEST_ASSERT_EQUAL(0u, MXM_MonGetVoltages(NULL_PTR, 0u)));
186 }
187 
190 }
191 
193  /* set request status to an invalid value */
196 }
197 
200  MXM_5XSetStateRequest_ExpectAndReturn(
205  STD_OK);
207 }
208 
212 }
213 
216  OS_GetTickCount_ExpectAndReturn(0u);
219 }
220 
225  TEST_ASSERT_EQUAL(42, mxm_instance.operationSubstate);
226 }
227 
229  TEST_ASSERT_FAIL_ASSERT(TEST_ASSERT_EQUAL(false, MXM_HandleStateReadall(NULL_PTR, 0u, 0u)));
230 }
231 
233  /* set request status to an invalid value */
235  TEST_ASSERT_FAIL_ASSERT(TEST_ASSERT_EQUAL(false, MXM_HandleStateReadall(&mxm_instance, 21u, 42u)));
236 }
237 
240  const uint8_t registerAddress = 21u;
241  /* recreate a local variant of the expected command buffer */
243  .regAddress = registerAddress,
244  .lsb = 0,
245  .msb = 0,
246  .model = modelForTest,
247  };
248  MXM_5XSetStateRequest_ExpectAndReturn(
250  bool functionReturnValue;
251  TEST_ASSERT_PASS_ASSERT(functionReturnValue = MXM_HandleStateReadall(&mxm_instance, registerAddress, 42u));
252  TEST_ASSERT_EQUAL(false, functionReturnValue);
253 }
254 
257  bool functionReturnValue;
258  TEST_ASSERT_PASS_ASSERT(functionReturnValue = MXM_HandleStateReadall(&mxm_instance, 21u, 42u));
259  TEST_ASSERT_EQUAL(false, functionReturnValue);
260 }
261 
264  OS_GetTickCount_ExpectAndReturn(0u);
265  bool functionReturnValue;
266  TEST_ASSERT_PASS_ASSERT(functionReturnValue = MXM_HandleStateReadall(&mxm_instance, 21u, 42u));
267  TEST_ASSERT_EQUAL(false, functionReturnValue);
269 }
270 
273  MXM_5XGetRXBuffer_ExpectAndReturn(mxm_instance.pInstance5X, mxm_instance.rxBuffer, MXM_RX_BUFFER_LENGTH, STD_OK);
274  MXM_5XGetLastDCByte_ExpectAndReturn(mxm_instance.pInstance5X, 84u);
275  bool functionReturnValue;
276  TEST_ASSERT_PASS_ASSERT(functionReturnValue = MXM_HandleStateReadall(&mxm_instance, 21u, 42u));
277  TEST_ASSERT_EQUAL(true, functionReturnValue);
279  TEST_ASSERT_EQUAL(42u, mxm_instance.operationSubstate);
280  TEST_ASSERT_EQUAL(84u, mxm_instance.dcByte);
281 }
282 
285 }
286 
288  /* with only one module, the entry of the LSB should be in the third
289  position of the RX buffer and the MSB in the fourth */
290  mxm_instance.rxBuffer[2] = 85; /* 0b01010101 */
291  mxm_instance.rxBuffer[3] = 170; /* 0b10101010 */
292  /* simulate 1 satellite */
293  MXM_5XGetNumberOfSatellites_ExpectAndReturn(mxm_instance.pInstance5X, 1);
294  /* don't care about the database call */
295  DATA_Write1DataBlock_IgnoreAndReturn(STD_OK);
297 
298  /* check for the injected pattern */
299  for (uint8_t i = 0u; i < 8; i = i + 2) {
300  TEST_ASSERT_EQUAL(1, mxm_instance.pOpenwire_table->openwire[0][i]);
301  }
302  for (uint8_t i = 1u; i < 8; i = i + 2) {
303  TEST_ASSERT_EQUAL(0, mxm_instance.pOpenwire_table->openwire[0][i]);
304  }
305  for (uint8_t i = 8u; i < MXM_MAXIMUM_NR_OF_CELLS_PER_MODULE; i = i + 2) {
306  TEST_ASSERT_EQUAL(0, mxm_instance.pOpenwire_table->openwire[0][i]);
307  }
308  for (uint8_t i = 9u; i < MXM_MAXIMUM_NR_OF_CELLS_PER_MODULE; i = i + 2) {
309  TEST_ASSERT_EQUAL(1, mxm_instance.pOpenwire_table->openwire[0][i]);
310  }
311 }
312 
315 }
#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:80
@ DATA_BLOCK_ID_OPEN_WIRE_BASE
Definition: database_cfg.h:84
@ STD_NOT_OK
Definition: fstd_types.h:83
@ STD_OK
Definition: fstd_types.h:82
#define NULL_PTR
Null pointer.
Definition: fstd_types.h:76
STD_RETURN_TYPE_e MXM_ProcessOpenWire(const MXM_MONITORING_INSTANCE_s *const kpkInstance)
Processes the retrieved information on openwire.
Definition: mxm_1785x.c:971
bool must_check_return MXM_HandleStateReadall(MXM_MONITORING_INSTANCE_s *pInstance, MXM_REG_NAME_e registerName, MXM_STATEMACHINE_OPERATION_STATES_e nextState)
Handle the statemachine-transactions for a READALL.
Definition: mxm_1785x.c:940
STD_RETURN_TYPE_e TEST_MXM_ParseVoltagesIntoDB(MXM_MONITORING_INSTANCE_s *pInstance)
Definition: mxm_1785x.c:1426
STD_RETURN_TYPE_e MXM_ConstructBalancingBuffer(MXM_BALANCING_STATE_s *pBalancingInstance)
Fill the balancing datastructure.
Definition: mxm_1785x.c:1016
MXM_MONITORING_STATE_e 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:1275
void MXM_StateMachine(MXM_MONITORING_INSTANCE_s *pInstance)
Main state-machine implementation.
Definition: mxm_1785x.c:1314
STD_RETURN_TYPE_e TEST_MXM_ParseVoltageReadallTest(MXM_MONITORING_INSTANCE_s *pInstance)
Definition: mxm_1785x.c:1423
void MXM_HandleStateWriteall(MXM_MONITORING_INSTANCE_s *pInstance, MXM_STATEMACHINE_OPERATION_STATES_e nextState)
Handle the statemachine-transactions for a WRITEALL.
Definition: mxm_1785x.c:912
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:1429
Headers for the driver for the MAX17841B ASCI and MAX1785x monitoring chip.
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:247
DATA_BLOCK_ID_e uniqueId
Definition: database_cfg.h:119
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:302
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:307
DATA_BLOCK_BALANCING_CONTROL_s *const pBalancingControl_table
MXM_5X_COMMAND_PAYLOAD_s batteryCmdBuffer
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
DATA_BLOCK_OPEN_WIRE_s *const pOpenwire_table
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)