foxBMS - Unit Tests  1.2.1
The foxBMS Unit Tests API Documentation
test_mxm_1785x.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 test_mxm_1785x.c
44  * @author foxBMS Team
45  * @date 2020-07-02 (date of creation)
46  * @updated 2021-12-06 (date of last update)
47  * @ingroup UNIT_TEST_IMPLEMENTATION
48  * @prefix MXM
49  *
50  * @brief Test for the Maxim 1785x driver.
51  *
52  * @details def
53  *
54  */
55 
56 /*========== Includes =======================================================*/
57 #include "unity.h"
58 #include "Mockafe_plausibility.h"
59 #include "Mockdatabase.h"
60 #include "Mockfassert.h"
61 #include "Mockmxm_17841b.h"
62 #include "Mockmxm_41b_register_map.h"
63 #include "Mockmxm_battery_management.h"
64 #include "Mockmxm_cfg.h"
65 #include "Mockmxm_crc8.h"
66 #include "Mockmxm_registry.h"
67 #include "Mockos.h"
68 #include "Mocktsi.h"
69 
70 #include "database_cfg.h"
71 
72 #include "mxm_1785x.h"
73 #include "mxm_1785x_tools.h"
74 #include "test_assert_helper.h"
75 
76 /*========== Definitions and Implementations for Unit Test ==================*/
77 
79 
82  .operationSubstate = MXM_INIT_ENTRY,
83  .allowStartup = false,
84  .operationRequested = false,
85  .firstMeasurementDone = false,
86  .stopRequested = false,
87  .openwireRequested = false,
88  .undervoltageAlert = false,
89  .dcByte = MXM_DC_EMPTY,
90  .mxmVoltageCellCounter = 0,
91  .highest5xDevice = 0,
92  .requestStatus5x = MXM_5X_STATE_UNSENT,
93  .batteryCmdBuffer =
94  {
95  .regAddress = (MXM_REG_NAME_e)0x00,
96  .lsb = 0x00,
97  .msb = 0x00,
98  .deviceAddress = 0x00,
99  .blocksize = 0,
100  .model = MXM_MODEL_ID_MAX17852,
101  },
102  .resultSelfCheck = STD_NOT_OK,
103  .selfCheck =
104  {
105  .crc = STD_NOT_OK,
106  .conv = STD_NOT_OK,
107  .firstSetBit = STD_NOT_OK,
108  .extractValueFromRegister = STD_NOT_OK,
109  .parseVoltageReadall = STD_NOT_OK,
110  .addressSpaceChecker = STD_NOT_OK,
111  .fmeaStatusASCI = STD_NOT_OK,
112  },
113  .pOpenwire_table = &mxm_tableOpenWire,
114 };
115 
117 
118 /** dummy implementation for the Operation state machine */
120 }
121 
122 /** local variable that allows to change the reported model id on a per test basis*/
124 
125 /** dummy implementation for the model id */
127  return modelForTest;
128 }
129 
130 /*========== Setup and Teardown =============================================*/
131 void setUp(void) {
135 
137 
138  /* delete rx buffer */
139  for (uint16_t i = 0u; i < MXM_RX_BUFFER_LENGTH; i++) {
140  mxm_instance.rxBuffer[i] = 0u;
141  }
142 }
143 
144 void tearDown(void) {
145 }
146 
147 /*========== Test Cases =====================================================*/
150 }
151 
153  uint8_t voltRxBuffer = 0;
154  MXM_DATA_STORAGE_s datastorage = {0};
155 
157 
159 }
160 
163 }
164 
167 }
168 
170  TEST_ASSERT_FAIL_ASSERT(TEST_ASSERT_EQUAL(0u, MXM_MonGetVoltages(NULL_PTR, 0u)));
171 }
172 
175 }
176 
178  /* set request status to an invalid value */
181 }
182 
185  MXM_5XSetStateRequest_ExpectAndReturn(
190  STD_OK);
192 }
193 
197 }
198 
201  OS_GetTickCount_ExpectAndReturn(0u);
204 }
205 
210  TEST_ASSERT_EQUAL(42, mxm_instance.operationSubstate);
211 }
212 
214  TEST_ASSERT_FAIL_ASSERT(TEST_ASSERT_EQUAL(false, MXM_HandleStateReadall(NULL_PTR, 0u, 0u)));
215 }
216 
218  /* set request status to an invalid value */
220  TEST_ASSERT_FAIL_ASSERT(TEST_ASSERT_EQUAL(false, MXM_HandleStateReadall(&mxm_instance, 21u, 42u)));
221 }
222 
225  const uint8_t registerAddress = 21u;
226  /* recreate a local variant of the expected command buffer */
228  .regAddress = registerAddress,
229  .lsb = 0,
230  .msb = 0,
231  .model = modelForTest,
232  };
233  MXM_5XSetStateRequest_ExpectAndReturn(
235  bool functionReturnValue;
236  TEST_ASSERT_PASS_ASSERT(functionReturnValue = MXM_HandleStateReadall(&mxm_instance, registerAddress, 42u));
237  TEST_ASSERT_EQUAL(false, functionReturnValue);
238 }
239 
242  bool functionReturnValue;
243  TEST_ASSERT_PASS_ASSERT(functionReturnValue = MXM_HandleStateReadall(&mxm_instance, 21u, 42u));
244  TEST_ASSERT_EQUAL(false, functionReturnValue);
245 }
246 
249  OS_GetTickCount_ExpectAndReturn(0u);
250  bool functionReturnValue;
251  TEST_ASSERT_PASS_ASSERT(functionReturnValue = MXM_HandleStateReadall(&mxm_instance, 21u, 42u));
252  TEST_ASSERT_EQUAL(false, functionReturnValue);
254 }
255 
258  MXM_5XGetRXBuffer_ExpectAndReturn(mxm_instance.pInstance5X, mxm_instance.rxBuffer, MXM_RX_BUFFER_LENGTH, STD_OK);
259  MXM_5XGetLastDCByte_ExpectAndReturn(mxm_instance.pInstance5X, 84u);
260  bool functionReturnValue;
261  TEST_ASSERT_PASS_ASSERT(functionReturnValue = MXM_HandleStateReadall(&mxm_instance, 21u, 42u));
262  TEST_ASSERT_EQUAL(true, functionReturnValue);
264  TEST_ASSERT_EQUAL(42u, mxm_instance.operationSubstate);
265  TEST_ASSERT_EQUAL(84u, mxm_instance.dcByte);
266 }
267 
270 }
271 
273  /* with only one module, the entry of the LSB should be in the third
274  position of the RX buffer and the MSB in the fourth */
275  mxm_instance.rxBuffer[2] = 85; /* 0b01010101 */
276  mxm_instance.rxBuffer[3] = 170; /* 0b10101010 */
277  /* simulate 1 satellite */
278  MXM_5XGetNumberOfSatellites_ExpectAndReturn(mxm_instance.pInstance5X, 1);
279  /* don't care about the database call */
280  DATA_Write_1_DataBlock_IgnoreAndReturn(STD_OK);
282 
283  /* check for the injected pattern */
284  for (uint8_t i = 0u; i < 8; i = i + 2) {
285  TEST_ASSERT_EQUAL(1, mxm_instance.pOpenwire_table->openwire[0][i]);
286  }
287  for (uint8_t i = 1u; i < 8; i = i + 2) {
288  TEST_ASSERT_EQUAL(0, mxm_instance.pOpenwire_table->openwire[0][i]);
289  }
290  for (uint8_t i = 8u; i < MXM_MAXIMUM_NR_OF_CELLS_PER_MODULE; i = i + 2) {
291  TEST_ASSERT_EQUAL(0, mxm_instance.pOpenwire_table->openwire[0][i]);
292  }
293  for (uint8_t i = 9u; i < MXM_MAXIMUM_NR_OF_CELLS_PER_MODULE; i = i + 2) {
294  TEST_ASSERT_EQUAL(1, mxm_instance.pOpenwire_table->openwire[0][i]);
295  }
296 }
297 
300 }
Database configuration header.
@ DATA_BLOCK_ID_OPEN_WIRE_BASE
Definition: database_cfg.h:81
@ STD_NOT_OK
Definition: fstd_types.h:82
@ STD_OK
Definition: fstd_types.h:81
#define NULL_PTR
Null pointer.
Definition: fstd_types.h:75
STD_RETURN_TYPE_e MXM_ProcessOpenWire(const MXM_MONITORING_INSTANCE_s *const kpkInstance)
Processes the retrieved information on openwire.
Definition: mxm_1785x.c:1006
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:975
STD_RETURN_TYPE_e TEST_MXM_ParseVoltagesIntoDB(MXM_MONITORING_INSTANCE_s *pInstance)
Definition: mxm_1785x.c:1424
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:1421
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:947
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:1427
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_ID_e uniqueId
Definition: database_cfg.h:111
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:290
uint8_t openwire[BS_NR_OF_STRINGS][BS_NR_OF_MODULES *(BS_NR_OF_CELLS_PER_MODULE+1u)]
Definition: database_cfg.h:294
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_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)