foxBMS-UnitTests  1.0.0
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
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 test_mxm_1785x.c
44  * @author foxBMS Team
45  * @date 2020-07-02 (date of creation)
46  * @updated 2020-09-10 (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 "Mockdatabase.h"
59 #include "Mockfassert.h"
60 #include "Mockmxm_17841b.h"
61 #include "Mockmxm_41b_register_map.h"
62 #include "Mockmxm_battery_management.h"
63 #include "Mockmxm_cfg.h"
64 #include "Mockmxm_crc8.h"
65 #include "Mockmxm_registry.h"
66 #include "Mockos.h"
67 #include "Mocktsi.h"
68 
69 #include "database_cfg.h"
70 
71 #include "mxm_1785x.h"
72 #include "mxm_1785x_tools.h"
73 #include "test_assert_helper.h"
74 
75 /*========== Definitions and Implementations for Unit Test ==================*/
76 
79  .operationSubstate = MXM_INIT_DEVCFG1,
80  .allowStartup = false,
81  .operationRequested = false,
82  .firstMeasurementDone = false,
83  .stopRequested = false,
84  .openwireRequested = false,
85  .undervoltageAlert = false,
86  .dcByte = MXM_DC_EMPTY,
87  .mxmVoltageCellCounter = 0,
88  .highest5xDevice = 0,
89  .requestStatus5x = MXM_5X_STATE_UNSENT,
90  .batteryCmdBuffer =
91  {
92  .regAddress = (MXM_REG_NAME_e)0x00,
93  .lsb = 0x00,
94  .msb = 0x00,
95  .deviceAddress = 0x00,
96  .blocksize = 0,
97  },
98  .resultSelfCheck = STD_NOT_OK,
99  .selfCheck =
100  {
101  .crc = STD_NOT_OK,
102  .conv = STD_NOT_OK,
103  .firstSetBit = STD_NOT_OK,
104  .extractValueFromRegister = STD_NOT_OK,
105  .parseVoltageReadall = STD_NOT_OK,
106  .addressSpaceChecker = STD_NOT_OK,
107  .fmeaStatusASCI = STD_NOT_OK,
108  },
109 };
110 
112 
113 /*========== Setup and Teardown =============================================*/
114 void setUp(void) {
118 
119  /* delete rx buffer */
120  for (uint16_t i = 0u; i < MXM_RX_BUFFER_LENGTH; i++) {
121  mxm_instance.rxBuffer[i] = 0u;
122  }
123 }
124 
125 void tearDown(void) {
126 }
127 
128 /*========== Test Cases =====================================================*/
131 }
132 
134  uint8_t volt_rx_buffer = 0;
135  MXM_DATA_STORAGE_s datastorage = {0};
136 
138 
140 }
141 
144 }
145 
148 }
149 
152 }
153 
156 }
157 
159  /* set request status to an invalid value */
162 }
163 
166  MXM_5XSetStateRequest_ExpectAndReturn(
171  STD_OK);
173 }
174 
178 }
179 
184 }
185 
190  TEST_ASSERT_EQUAL(42, mxm_instance.operationSubstate);
191 }
192 
195 }
196 
198  /* set request status to an invalid value */
201 }
202 
205  const uint8_t registerAddress = 21u;
206  /* recreate a local variant of the expected command buffer */
208  .regAddress = registerAddress,
209  .lsb = 0,
210  .msb = 0,
211  };
212  MXM_5XSetStateRequest_ExpectAndReturn(
214  bool functionReturnValue;
215  TEST_ASSERT_PASS_ASSERT(functionReturnValue = TEST_MXM_HandleStateReadall(&mxm_instance, registerAddress, 42u));
216  TEST_ASSERT_EQUAL(false, functionReturnValue);
217 }
218 
221  bool functionReturnValue;
222  TEST_ASSERT_PASS_ASSERT(functionReturnValue = TEST_MXM_HandleStateReadall(&mxm_instance, 21u, 42u));
223  TEST_ASSERT_EQUAL(false, functionReturnValue);
224 }
225 
228  bool functionReturnValue;
229  TEST_ASSERT_PASS_ASSERT(functionReturnValue = TEST_MXM_HandleStateReadall(&mxm_instance, 21u, 42u));
230  TEST_ASSERT_EQUAL(false, functionReturnValue);
232 }
233 
236  MXM_5XGetRXBuffer_ExpectAndReturn(mxm_instance.pInstance5X, mxm_instance.rxBuffer, MXM_RX_BUFFER_LENGTH, STD_OK);
237  MXM_5XGetLastDCByte_ExpectAndReturn(mxm_instance.pInstance5X, 84u);
238  bool functionReturnValue;
239  TEST_ASSERT_PASS_ASSERT(functionReturnValue = TEST_MXM_HandleStateReadall(&mxm_instance, 21u, 42u));
240  TEST_ASSERT_EQUAL(true, functionReturnValue);
242  TEST_ASSERT_EQUAL(42u, mxm_instance.operationSubstate);
243  TEST_ASSERT_EQUAL(84u, mxm_instance.dcByte);
244 }
245 
249 }
250 
252  /* with only one module, the entry of the LSB should be in the third
253  position of the RX buffer and the MSB in the fourth */
254  mxm_instance.rxBuffer[2] = 85; /* 0b01010101 */
255  mxm_instance.rxBuffer[3] = 170; /* 0b10101010 */
256  /* simulate 1 satellite */
257  MXM_5XGetNumberOfSatellites_ExpectAndReturn(mxm_instance.pInstance5X, 1);
258  /* don't care about the database call */
259  DATA_Write_1_DataBlock_IgnoreAndReturn(STD_OK);
261 
262  /* check for the injected pattern */
263  for (uint8_t i = 0u; i < 8; i = i + 2) {
264  TEST_ASSERT_EQUAL(1, mxm_openwire.openwire[0][i]);
265  }
266  for (uint8_t i = 1u; i < 8; i = i + 2) {
267  TEST_ASSERT_EQUAL(0, mxm_openwire.openwire[0][i]);
268  }
269  for (uint8_t i = 8u; i < MXM_MAXIMUM_NR_OF_CELLS_PER_MODULE; i = i + 2) {
270  TEST_ASSERT_EQUAL(0, mxm_openwire.openwire[0][i]);
271  }
272  for (uint8_t i = 9u; i < MXM_MAXIMUM_NR_OF_CELLS_PER_MODULE; i = i + 2) {
273  TEST_ASSERT_EQUAL(1, mxm_openwire.openwire[0][i]);
274  }
275 }
276 
279 }
MXM_MAXIMUM_NR_OF_CELLS_PER_MODULE
#define MXM_MAXIMUM_NR_OF_CELLS_PER_MODULE
Maximum number of cells per module.
Definition: mxm_basic_defines.h:81
testTEST_MXM_ParseVoltageReadallNullPointer
void testTEST_MXM_ParseVoltageReadallNullPointer(void)
Definition: test_mxm_1785x.c:133
testMXM_HandleStateReadallError
void testMXM_HandleStateReadallError(void)
Definition: test_mxm_1785x.c:226
testMXM_ProcessOpenWireNullPointer
void testMXM_ProcessOpenWireNullPointer(void)
Definition: test_mxm_1785x.c:246
MXM_DATA_STORAGE
Definition: mxm_basic_defines.h:146
DATA_BLOCK_OPENWIRE
Definition: database_cfg.h:296
MXM_MONITORING_INSTANCE::pInstance5X
MXM_5X_INSTANCE_s * pInstance5X
Definition: mxm_1785x_tools.h:206
TEST_MXM_HandleStateReadall
bool TEST_MXM_HandleStateReadall(MXM_MONITORING_INSTANCE_s *pInstance, MXM_REG_NAME_e registerName, MXM_STATEMACHINE_OPERATION_STATES_e nextState)
Definition: mxm_1785x.c:1936
commandBuffer
uint16_t commandBuffer[10]
Definition: test_mxm_17841b.c:84
MXM_MONITORING_INSTANCE
Definition: mxm_1785x_tools.h:188
MXM_MONITORING_INSTANCE::dcByte
MXM_DC_BYTE_e dcByte
Definition: mxm_1785x_tools.h:198
DATA_BLOCK_ID_OPEN_WIRE_BASE
@ DATA_BLOCK_ID_OPEN_WIRE_BASE
Definition: database_cfg.h:81
MXM_DC_EMPTY
@ MXM_DC_EMPTY
Definition: mxm_basic_defines.h:115
testMXM_HandleStateWriteallProcessed
void testMXM_HandleStateWriteallProcessed(void)
Definition: test_mxm_1785x.c:186
testMXM_ProcessOpenWire1SatelliteAlternatingPattern
void testMXM_ProcessOpenWire1SatelliteAlternatingPattern(void)
Definition: test_mxm_1785x.c:251
testMXM_HandleStateReadallInvalidRequestStatus
void testMXM_HandleStateReadallInvalidRequestStatus(void)
Definition: test_mxm_1785x.c:197
MXM_MONITORING_INSTANCE::batteryCmdBuffer
MXM_5X_COMMAND_PAYLOAD_s batteryCmdBuffer
Definition: mxm_1785x_tools.h:202
TEST_MXM_ParseVoltageReadallTest
STD_RETURN_TYPE_e TEST_MXM_ParseVoltageReadallTest(MXM_MONITORING_INSTANCE_s *pInstance)
Definition: mxm_1785x.c:1920
testMXM_HandleStateWriteallNullPointer
void testMXM_HandleStateWriteallNullPointer(void)
Definition: test_mxm_1785x.c:154
MXM_5X_STATE_ERROR
@ MXM_5X_STATE_ERROR
Definition: mxm_battery_management.h:234
MXM_MONITORING_INSTANCE::rxBuffer
uint8_t rxBuffer[MXM_RX_BUFFER_LENGTH]
Definition: mxm_1785x_tools.h:223
test_assert_helper.h
Helper for unit tests.
testMXM_ParseVoltageReadallTestNullPointer
void testMXM_ParseVoltageReadallTestNullPointer(void)
Definition: test_mxm_1785x.c:142
tearDown
void tearDown(void)
Definition: test_mxm_1785x.c:125
MXM_5X_STATE_UNPROCESSED
@ MXM_5X_STATE_UNPROCESSED
Definition: mxm_battery_management.h:231
MXM_STATEMACH_5X_WRITEALL
@ MXM_STATEMACH_5X_WRITEALL
Definition: mxm_battery_management.h:156
mxm_1785x_tools.h
This is a collection of helper functions for the MAX1785x ICs.
testTEST_MXM_MonGetVoltagesNullPointer
void testTEST_MXM_MonGetVoltagesNullPointer(void)
Definition: test_mxm_1785x.c:150
testMXM_StateMachineNullPointer
void testMXM_StateMachineNullPointer(void)
Definition: test_mxm_1785x.c:277
testMXM_HandleStateReadallNullPointer
void testMXM_HandleStateReadallNullPointer(void)
Definition: test_mxm_1785x.c:193
MXM_MONITORING_INSTANCE::state
MXM_STATEMACHINE_STATES_e state
Definition: mxm_1785x_tools.h:189
MXM_RX_BUFFER_LENGTH
#define MXM_RX_BUFFER_LENGTH
Length of RX buffer.
Definition: mxm_1785x_tools.h:185
mxm_openwire
static DATA_BLOCK_OPEN_WIRE_s mxm_openwire
Definition: test_mxm_1785x.c:111
STD_OK
@ STD_OK
Definition: fstd_types.h:72
TEST_MXM_ParseVoltagesIntoDB
STD_RETURN_TYPE_e TEST_MXM_ParseVoltagesIntoDB(MXM_MONITORING_INSTANCE_s *pInstance)
Definition: mxm_1785x.c:1923
testMXM_HandleStateWriteallUnprocessed
void testMXM_HandleStateWriteallUnprocessed(void)
Definition: test_mxm_1785x.c:175
STD_NOT_OK
@ STD_NOT_OK
Definition: fstd_types.h:73
setUp
void setUp(void)
Definition: test_mxm_1785x.c:114
testMXM_HandleStateWriteallEntry
void testMXM_HandleStateWriteallEntry(void)
Definition: test_mxm_1785x.c:164
MXM_StateMachine
void MXM_StateMachine(MXM_MONITORING_INSTANCE_s *pInstance)
Battery monitoring driver for MAX1785x battery monitoring ICs.
Definition: mxm_1785x.c:1824
TEST_MXM_ProcessOpenWire
void TEST_MXM_ProcessOpenWire(MXM_MONITORING_INSTANCE_s *pInstance, DATA_BLOCK_OPEN_WIRE_s *pDataOpenWire)
Definition: mxm_1785x.c:1949
TEST_MXM_HandleStateWriteall
void TEST_MXM_HandleStateWriteall(MXM_MONITORING_INSTANCE_s *pInstance, MXM_STATEMACHINE_OPERATION_STATES_e nextState)
Definition: mxm_1785x.c:1931
MXM_REG_NAME_e
enum MXM_REG_NAME MXM_REG_NAME_e
MAX1785x register names.
MXM_MONITORING_INSTANCE::operationSubstate
MXM_STATEMACHINE_OPERATION_STATES_e operationSubstate
Definition: mxm_1785x_tools.h:190
mxm_instance
static MXM_MONITORING_INSTANCE_s mxm_instance
Definition: test_mxm_1785x.c:77
DATA_BLOCK_OPENWIRE::openwire
uint8_t openwire[BS_NR_OF_STRINGS][BS_NR_OF_MODULES *(BS_NR_OF_CELLS_PER_MODULE+1)]
Definition: database_cfg.h:304
testMXM_HandleStateWriteallError
void testMXM_HandleStateWriteallError(void)
Definition: test_mxm_1785x.c:180
MXM_STATEMACH_5X_READALL
@ MXM_STATEMACH_5X_READALL
Definition: mxm_battery_management.h:158
NULL_PTR
#define NULL_PTR
Null pointer.
Definition: fstd_types.h:66
TEST_MXM_ParseVoltageReadall
STD_RETURN_TYPE_e TEST_MXM_ParseVoltageReadall(uint8_t *volt_rx_buffer, uint16_t volt_rx_buffer_len, MXM_DATA_STORAGE_s *datastorage, MXM_CONVERSION_TYPE_e conversionType)
Definition: mxm_1785x.c:1942
testMXM_HandleStateReadallProcessed
void testMXM_HandleStateReadallProcessed(void)
Definition: test_mxm_1785x.c:234
MXM_5X_STATE_UNSENT
@ MXM_5X_STATE_UNSENT
Definition: mxm_battery_management.h:230
testMXM_ParseVoltageReadallTest
void testMXM_ParseVoltageReadallTest(void)
Definition: test_mxm_1785x.c:129
MXM_5X_STATE_PROCESSED
@ MXM_5X_STATE_PROCESSED
Definition: mxm_battery_management.h:233
MXM_5X_COMMAND_PAYLOAD_s
Payload command.
Definition: mxm_battery_management.h:240
MXM_MONITORING_INSTANCE::requestStatus5x
MXM_5X_STATE_REQUEST_STATUS_e requestStatus5x
Definition: mxm_1785x_tools.h:201
TEST_MXM_MonGetVoltages
MXM_MONINTORING_STATE_e TEST_MXM_MonGetVoltages(MXM_MONITORING_INSTANCE_s *pInstance, MXM_REG_NAME_e regAddress)
Definition: mxm_1785x.c:1926
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.
testMXM_HandleStateReadallUnsent
void testMXM_HandleStateReadallUnsent(void)
Definition: test_mxm_1785x.c:203
MXM_STATEMACHINE_STATES_UNINITIALIZED
@ MXM_STATEMACHINE_STATES_UNINITIALIZED
Definition: mxm_1785x_tools.h:75
testMXM_HandleStateWriteallInvalidRequestStatus
void testMXM_HandleStateWriteallInvalidRequestStatus(void)
Definition: test_mxm_1785x.c:158
DATA_BLOCK_OPENWIRE::header
DATA_BLOCK_HEADER_s header
Definition: database_cfg.h:300
testMXM_ParseVoltagesIntoDBNullPointer
void testMXM_ParseVoltagesIntoDBNullPointer(void)
Definition: test_mxm_1785x.c:146
TEST_ASSERT_FAIL_ASSERT
#define TEST_ASSERT_FAIL_ASSERT(_code_under_test)
assert whether assert macro has failed
Definition: test_assert_helper.h:70
testMXM_HandleStateReadallUnprocessed
void testMXM_HandleStateReadallUnprocessed(void)
Definition: test_mxm_1785x.c:219
database_cfg.h
Database configuration header.
TEST_ASSERT_PASS_ASSERT
#define TEST_ASSERT_PASS_ASSERT(_code_under_test)
assert whether assert macro has passed
Definition: test_assert_helper.h:88
DATA_BLOCKHEADER::uniqueId
DATA_BLOCK_ID_e uniqueId
Definition: database_cfg.h:109