foxBMS - Unit Tests  1.1.0
The foxBMS Unit Tests API Documentation
test_mxm_17841b.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_17841b.c
44  * @author foxBMS Team
45  * @date 2020-06-22 (date of creation)
46  * @updated 2021-06-16 (date of last update)
47  * @ingroup UNIT_TEST_IMPLEMENTATION
48  * @prefix MXM
49  *
50  * @brief Test for the Maxim MAX17841B driver.
51  *
52  * @details def
53  *
54  */
55 
56 /*========== Includes =======================================================*/
57 #include "unity.h"
58 #include "Mockfassert.h"
59 #include "Mockmxm_cfg.h"
60 
61 #include "mxm_17841b.h"
62 #include "mxm_bitextract.h"
63 #include "test_assert_helper.h"
64 
65 /*========== Definitions and Implementations for Unit Test ==================*/
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  .hwModel = 0,
80  .hwMaskRevision = 0,
81  .spiRXBuffer = {0},
82  .spiTXBuffer = {0},
83 };
84 
85 uint16_t commandBuffer[10] = {0};
86 uint16_t rxBuffer[100] = {0};
87 
89 
90 /*========== Setup and Teardown =============================================*/
91 void setUp(void) {
99  mxm_41b_state.regRXStatus = 0x00u;
100  mxm_41b_state.regTXStatus = 0x00u;
101  mxm_41b_state.regConfig1 = 0x60u;
102  mxm_41b_state.regConfig2 = 0x10u;
103  mxm_41b_state.regConfig3 = 0x0Fu;
106 
107  commandBuffer[0] = 0;
108  commandBuffer[1] = 0;
109  commandBuffer[2] = 0;
110  commandBuffer[3] = 0;
111  commandBuffer[4] = 0;
112  commandBuffer[5] = 0;
113  commandBuffer[6] = 0;
114  commandBuffer[7] = 0;
115  commandBuffer[8] = 0;
116  commandBuffer[9] = 0;
117 
119 }
120 
121 void tearDown(void) {
122 }
123 
124 /*========== Test Cases =====================================================*/
125 /* tests for function MXM_41BSetStateRequest */
126 
128  TEST_ASSERT_EQUAL(
129  STD_NOT_OK,
132  TEST_ASSERT_EQUAL(
133  STD_NOT_OK,
136  TEST_ASSERT_EQUAL(
137  STD_NOT_OK,
140  TEST_ASSERT_EQUAL(
141  STD_OK,
143 }
144 
146  /* start in Idle state */
148 
149  TEST_ASSERT_EQUAL(
150  STD_OK,
153 
154  /* try additional transition (even though not in idle state anymore) */
155  TEST_ASSERT_EQUAL(
156  STD_NOT_OK,
159 }
160 
162  TEST_ASSERT_EQUAL(
163  STD_OK,
165 }
166 
168  TEST_ASSERT_EQUAL(
169  STD_NOT_OK,
171 
172  TEST_ASSERT_EQUAL(
173  STD_NOT_OK,
175 
176  TEST_ASSERT_EQUAL(
177  STD_OK,
179 }
180 
182  TEST_ASSERT_EQUAL(
183  STD_NOT_OK,
185 
186  TEST_ASSERT_EQUAL(
187  STD_NOT_OK,
189 
190  TEST_ASSERT_EQUAL(
191  STD_OK,
193 }
194 
196  TEST_ASSERT_EQUAL(
197  STD_NOT_OK,
199 }
200 
201 /* end of tests for function MXM_41BSetStateRequest */
202 
203 /* tests for function MXM_41BWriteRegisterFunction */
205  TEST_ASSERT_EQUAL(STD_NOT_OK, MXM_41BWriteRegisterFunction(&mxm_41b_state, 0xFF, 0));
206 }
208  /* standard state should be only fourth bit set */
209  TEST_ASSERT_EQUAL(0x10u, mxm_41b_state.regConfig2);
210  TEST_ASSERT_EQUAL(
212  TEST_ASSERT_EQUAL(0x10u, mxm_41b_state.regConfig2);
213 
214  /* after writing register, fifth bit should be set in addition */
215  TEST_ASSERT_EQUAL(
217  TEST_ASSERT_EQUAL((0x10u | (1u << 5u)), mxm_41b_state.regConfig2);
218 
219  /* standard state should be third, second, first and zero bit set */
220  TEST_ASSERT_EQUAL(0xFu, mxm_41b_state.regConfig3);
221 
222  /* 0us --> 0 */
223  TEST_ASSERT_EQUAL(
224  STD_OK,
226  TEST_ASSERT_EQUAL(0x0u, mxm_41b_state.regConfig3);
227 
228  /* 10us --> 1 */
229  TEST_ASSERT_EQUAL(
230  STD_OK,
232  TEST_ASSERT_EQUAL(0x1u, mxm_41b_state.regConfig3);
233 
234  /* 640us --> 0x7 */
235  TEST_ASSERT_EQUAL(
236  STD_OK,
238  TEST_ASSERT_EQUAL(0x7u, mxm_41b_state.regConfig3);
239 
240  /* INF DLY --> 0xF */
241  TEST_ASSERT_EQUAL(
242  STD_OK,
244  TEST_ASSERT_EQUAL(0xFu, mxm_41b_state.regConfig3);
245 
246  /* standard state should be zero */
247  TEST_ASSERT_EQUAL(0x0u, mxm_41b_state.regRXIntEnable);
248 
249  /* rx error int on --> 0x80 */
250  TEST_ASSERT_EQUAL(
252  TEST_ASSERT_EQUAL(0x80u, mxm_41b_state.regRXIntEnable);
253 
254  /* rx error int off --> 0 */
255  TEST_ASSERT_EQUAL(
257  TEST_ASSERT_EQUAL(0x0u, mxm_41b_state.regRXIntEnable);
258 
259  /* standard state should be zero */
260  TEST_ASSERT_EQUAL(0x0u, mxm_41b_state.regRXIntEnable);
261 
262  /* rx overflow int on --> 0x8 */
263  TEST_ASSERT_EQUAL(
265  TEST_ASSERT_EQUAL(0x8u, mxm_41b_state.regRXIntEnable);
266 
267  /* rx overflow int off --> 0 */
268  TEST_ASSERT_EQUAL(
270  TEST_ASSERT_EQUAL(0x0u, mxm_41b_state.regRXIntEnable);
271 }
272 /* end of tests for function MXM_41BWriteRegisterFunction */
273 
274 /* tests for function MXM_41BReadRegisterFunction */
276  MXM_41B_REG_BIT_VALUE result = {0};
277  TEST_ASSERT_EQUAL(STD_NOT_OK, MXM_41BReadRegisterFunction(&mxm_41b_state, 0xFF, &result));
280 }
282  MXM_41B_REG_BIT_VALUE result = 42;
283  /* set register */
285 
286  /* read all functions in this register */
287  TEST_ASSERT_EQUAL(
289  TEST_ASSERT_EQUAL(MXM_41B_REG_FALSE, result);
290 
291  TEST_ASSERT_EQUAL(
293  TEST_ASSERT_EQUAL(MXM_41B_REG_FALSE, result);
294 
295  /* set register with bit RX_EMPTY */
297 
298  /* read all functions in this register */
299  TEST_ASSERT_EQUAL(
301  TEST_ASSERT_EQUAL(MXM_41B_REG_FALSE, result);
302 
303  TEST_ASSERT_EQUAL(
305  TEST_ASSERT_EQUAL(MXM_41B_REG_TRUE, result);
306 
307  /* set register with bit RX_BUSY */
308  mxm_41b_state.regRXStatus = 0x20u;
309 
310  /* read all functions in this register */
311  TEST_ASSERT_EQUAL(
313  TEST_ASSERT_EQUAL(MXM_41B_REG_TRUE, result);
314 
315  TEST_ASSERT_EQUAL(
317  TEST_ASSERT_EQUAL(MXM_41B_REG_FALSE, result);
318 
319  /* set register to zero */
321 
322  /* read all functions in this register */
324  TEST_ASSERT_EQUAL(MXM_41B_REG_FALSE, result);
325 
326  /* set register with TX_PREAMBLES */
327  mxm_41b_state.regConfig2 = 0x20u;
328 
329  /* read all functions in this register */
331  TEST_ASSERT_EQUAL(MXM_41B_REG_TRUE, result);
332 }
333 /* end of tests for function MXM_41BReadRegisterFunction */
334 
335 /* tests for the state-machine */
340 }
341 
343  /* force state-machine in idle state */
345 
346  TEST_ASSERT_EQUAL(MXM_STATEMACH_41B_IDLE, mxm_41b_state.state);
348  TEST_ASSERT_EQUAL(MXM_STATEMACH_41B_IDLE, mxm_41b_state.state);
349 }
350 
351 void testStateCheckFMEA(void) {
352  /* force state-machine in check_fmea state */
354 
356 
357  /* create a buffer with the assumed output of the ASCI and inject into SPI read */
358  uint16_t rxBuffer[10] = {0x13u, 0x00u};
359  MXM_ReceiveData_ExpectAndReturn(mxm_41b_state.spiTXBuffer, mxm_41b_state.spiRXBuffer, 2, STD_OK);
360  MXM_ReceiveData_ReturnArrayThruPtr_rxBuffer(rxBuffer, 2);
363 
364  /* check if the values are parsed correctly */
365  MXM_GetSPIStateReady_ExpectAndReturn(STD_OK);
367  TEST_ASSERT_EQUAL(MXM_STATEMACH_41B_IDLE, mxm_41b_state.state);
368  TEST_ASSERT_EQUAL(MXM_41B_STATE_PROCESSED, status41b);
369 }
370 
372  /* force state-machine in check_fmea state */
374 
376 
377  /* create a buffer with the assumed output of the ASCI and inject into SPI read */
378  /* simulate an GNDL Alert in FMEA register */
379  uint16_t rxBuffer[10] = {0x13u, 0x01u};
380  MXM_ReceiveData_ExpectAndReturn(mxm_41b_state.spiTXBuffer, mxm_41b_state.spiRXBuffer, 2, STD_OK);
381  MXM_ReceiveData_ReturnArrayThruPtr_rxBuffer(rxBuffer, 2);
384 
385  /* check if the values are parsed correctly */
386  MXM_GetSPIStateReady_ExpectAndReturn(STD_OK);
388  TEST_ASSERT_EQUAL(MXM_STATEMACH_41B_IDLE, mxm_41b_state.state);
389  TEST_ASSERT_EQUAL(MXM_41B_STATE_ERROR, status41b);
390 }
391 
393  /* force state-machine in get_version state */
395 
397 
398  /* create a buffer with the assumed output of the ASCI and inject into SPI read */
399  uint16_t rxBuffer[10] = {0x15u, 0x84u, 0x12u};
400  MXM_ReceiveData_ExpectAndReturn(mxm_41b_state.spiTXBuffer, mxm_41b_state.spiRXBuffer, 3, STD_OK);
401  MXM_ReceiveData_ReturnArrayThruPtr_rxBuffer(rxBuffer, 3);
404 
405  /* check if the values are parsed correctly */
406  MXM_GetSPIStateReady_ExpectAndReturn(STD_OK);
408  TEST_ASSERT_EQUAL(0x841u, mxm_41b_state.hwModel);
409  TEST_ASSERT_EQUAL(0x2u, mxm_41b_state.hwMaskRevision);
410 }
411 
413  /* force state-machine in clear_receive_buffer state */
415 
417 
418  /* create a buffer with the assumed input to the ASCI */
419  uint16_t tx_buffer[10] = {0xE0u};
420  MXM_SendData_ExpectAndReturn(tx_buffer, 1, STD_OK);
422 
423  /* check if the write was successful */
424  TEST_ASSERT_EQUAL(MXM_STATEMACH_41B_IDLE, mxm_41b_state.state);
425  TEST_ASSERT_EQUAL(MXM_41B_STATE_PROCESSED, status41b);
426 }
427 
429  /* force state-machine in clear_receive_buffer state */
431 
433 
434  /* create a buffer with the assumed input to the ASCI */
435  uint16_t tx_buffer[10] = {0xE0u};
436  /* simulate a failure of writing */
437  MXM_SendData_ExpectAndReturn(tx_buffer, 1, STD_NOT_OK);
439 
440  /* check if the write was successful */
442  TEST_ASSERT_EQUAL(MXM_41B_STATE_ERROR, status41b);
443 }
444 
446  /* force state-machine in clear_transmit_buffer state */
448 
450 
451  /* create a buffer with the assumed input to the ASCI */
452  uint16_t tx_buffer[10] = {0x20u};
453  MXM_SendData_ExpectAndReturn(tx_buffer, 1, STD_OK);
455 
456  /* check if the write was successful */
457  TEST_ASSERT_EQUAL(MXM_STATEMACH_41B_IDLE, mxm_41b_state.state);
458  TEST_ASSERT_EQUAL(MXM_41B_STATE_PROCESSED, status41b);
459 }
460 
462  /* force state-machine in clear_transmit_buffer state */
464 
466 
467  /* create a buffer with the assumed input to the ASCI */
468  uint16_t tx_buffer[10] = {0x20u};
469  /* simulate a failure of writing */
470  MXM_SendData_ExpectAndReturn(tx_buffer, 1, STD_NOT_OK);
472 
473  /* check if the write was successful */
475  TEST_ASSERT_EQUAL(MXM_41B_STATE_ERROR, status41b);
476 }
477 
479  /* force state-machine in MXM_41B_UART_WAIT_FOR_RX_STATUS_CHANGE_READ_AND_READ_BACK_RCV_BUF */
482  /* prepare RX buffer with not received RX_Stop_Status bit */
483  mxm_41b_state.spiRXBuffer[1] = 0;
484 
485  /* transition */
487 
489 }
490 
492  /* force state-machine in MXM_41B_UART_WAIT_FOR_RX_STATUS_CHANGE_READ_AND_READ_BACK_RCV_BUF */
495  /* prepare RX buffer with received RX_Stop_Status bit */
496  mxm_41b_state.spiRXBuffer[1] |= (0x01u << 1u);
497 
498  /* transition */
499  MXM_ReceiveData_IgnoreAndReturn(STD_OK);
501 
503 }
504 
505 /* end tests for the state-machine */
@ STD_NOT_OK
Definition: fstd_types.h:73
@ STD_OK
Definition: fstd_types.h:72
#define NULL_PTR
Null pointer.
Definition: fstd_types.h:66
void MXM_41BStateMachine(MXM_41B_INSTANCE_s *pInstance)
Execute state-machine for the MAX17841B.
Definition: mxm_17841b.c:429
STD_RETURN_TYPE_e MXM_41BWriteRegisterFunction(MXM_41B_INSTANCE_s *pInstance, MXM_41B_REG_FUNCTION_e registerFunction, MXM_41B_REG_BIT_VALUE value)
Write a register function.
Definition: mxm_17841b.c:347
STD_RETURN_TYPE_e MXM_41BSetStateRequest(MXM_41B_INSTANCE_s *pInstance, MXM_STATEMACH_41B_e state, uint16_t *pPayload, uint16_t payloadLength, uint8_t extendMessageBytes, uint16_t *pRxBuffer, uint16_t rxBufferLength, MXM_41B_STATE_REQUEST_STATUS_e *processed)
Set state transition for MAX17841B-state-machine.
Definition: mxm_17841b.c:291
STD_RETURN_TYPE_e MXM_41BReadRegisterFunction(const MXM_41B_INSTANCE_s *const kpkInstance, MXM_41B_REG_FUNCTION_e registerFunction, MXM_41B_REG_BIT_VALUE *pValue)
Read the value of a register function.
Definition: mxm_17841b.c:391
Headers for the driver for the MAX17841B ASCI and MAX1785x monitoring chip.
enum MXM_41B_STATE_REQUEST_STATUS MXM_41B_STATE_REQUEST_STATUS_e
Request status of MAX17841B states.
@ MXM_41B_STATE_ERROR
Definition: mxm_17841b.h:127
@ MXM_41B_STATE_UNPROCESSED
Definition: mxm_17841b.h:125
@ MXM_41B_STATE_PROCESSED
Definition: mxm_17841b.h:126
@ MXM_41B_REG_FUNCTION_RX_BUSY_STATUS
Definition: mxm_17841b.h:134
@ MXM_41B_REG_FUNCTION_RX_EMPTY_STATUS
Definition: mxm_17841b.h:136
@ MXM_41B_REG_FUNCTION_TX_PREAMBLES
Definition: mxm_17841b.h:137
@ MXM_41B_REG_FUNCTION_RX_ERROR_INT
Definition: mxm_17841b.h:139
@ MXM_41B_REG_FUNCTION_KEEP_ALIVE
Definition: mxm_17841b.h:138
@ MXM_41B_REG_FUNCTION_RX_OVERFLOW_INT
Definition: mxm_17841b.h:140
@ MXM_STATEMACH_41B_MAXSTATE
Definition: mxm_17841b.h:91
@ MXM_STATEMACH_41B_GET_VERSION
Definition: mxm_17841b.h:85
@ MXM_STATEMACH_41B_CLEAR_RECEIVE_BUFFER
Definition: mxm_17841b.h:89
@ MXM_STATEMACH_41B_CLEAR_TRANSMIT_BUFFER
Definition: mxm_17841b.h:90
@ MXM_STATEMACH_41B_UNINITIALIZED
Definition: mxm_17841b.h:81
@ MXM_STATEMACH_41B_INIT
Definition: mxm_17841b.h:82
@ MXM_STATEMACH_41B_CHECK_FMEA
Definition: mxm_17841b.h:84
@ MXM_STATEMACH_41B_IDLE
Definition: mxm_17841b.h:83
@ MXM_STATEMACH_41B_UART_TRANSACTION
Definition: mxm_17841b.h:88
@ MXM_41B_UART_WAIT_FOR_RX_STATUS_CHANGE_WRITE
Definition: mxm_17841b.h:110
@ MXM_41B_UART_READ_BACK_RECEIVE_BUFFER_SAVE
Definition: mxm_17841b.h:112
@ MXM_41B_ENTRY_SUBSTATE
Definition: mxm_17841b.h:98
@ MXM_41B_UART_WAIT_FOR_RX_STATUS_CHANGE_READ_AND_READ_BACK_RCV_BUF
Definition: mxm_17841b.h:111
Bit extraction function for MXM_17841b.
#define MXM_41B_REG_FALSE
uint8_t MXM_41B_REG_BIT_VALUE
Bit-values for registers.
#define MXM_41B_REG_KEEP_ALIVE_0US
#define MXM_41B_REG_TRUE
#define MXM_41B_REG_KEEP_ALIVE_10US
#define MXM_41B_REG_KEEP_ALIVE_640US
#define MXM_41B_REG_KEEP_ALIVE_INF_DLY
Struct for the state-variable of state-machine.
Definition: mxm_17841b.h:149
uint8_t regConfig2
Definition: mxm_17841b.h:165
uint16_t aliveCounter
Definition: mxm_17841b.h:157
uint16_t spiRXBuffer[100]
Definition: mxm_17841b.h:169
uint8_t waitCounter
Definition: mxm_17841b.h:159
uint16_t hwModel
Definition: mxm_17841b.h:167
uint8_t regConfig3
Definition: mxm_17841b.h:166
uint8_t regConfig1
Definition: mxm_17841b.h:164
MXM_STATEMACH_41B_e state
Definition: mxm_17841b.h:150
MXM_41B_SUBSTATES_e substate
Definition: mxm_17841b.h:151
uint8_t extendMessageBytes
Definition: mxm_17841b.h:158
uint16_t spiTXBuffer[MXM_SPI_TX_BUFFER_LENGTH]
Definition: mxm_17841b.h:170
uint8_t regTXIntEnable
Definition: mxm_17841b.h:161
uint8_t hwMaskRevision
Definition: mxm_17841b.h:168
uint8_t regTXStatus
Definition: mxm_17841b.h:163
uint8_t regRXStatus
Definition: mxm_17841b.h:162
uint8_t regRXIntEnable
Definition: mxm_17841b.h:160
Helper for unit tests.
#define TEST_ASSERT_FAIL_ASSERT(_code_under_test)
assert whether assert macro has failed
void testStateGetVersion(void)
uint16_t rxBuffer[100]
void testStateClearTransmitBuffer(void)
void testWriteRegisterFunctionWithLegalValues(void)
void testStateRequestInconsistentInputPayload(void)
void testStateClearTransmitBufferFailedSPIWrite(void)
void testTransitionsOnlyAllowedInIdleAfterInit(void)
uint16_t commandBuffer[10]
void testWriteRegisterFunctionWithIllegalValues(void)
MXM_41B_STATE_REQUEST_STATUS_e status41b
void testStateClearReceiveBufferFailedSPIWrite(void)
void testStateRequestConsistentInput(void)
void setUp(void)
void tearDown(void)
void testStateStayInIdle(void)
static MXM_41B_INSTANCE_s mxm_41b_state
void testStateUARTWaitForRXStatusChangeFail(void)
void testStateRequestInconsistentInputRXBuffer(void)
void testStateCheckFMEAGNDLAlert(void)
void testStateStayInUninitialized(void)
void testReadRegisterFunctionWithIllegalValues(void)
void testStateClearReceiveBuffer(void)
void testStateCheckFMEA(void)
void testReadRegisterFunctionWithLegalValues(void)
void testOnlyAllowedTransitionFromUnitializedIsInit(void)
void testStateUARTWaitForRXStatusChangeSuccess(void)
void testStateRequestInconsistentInputProcessed(void)