foxBMS - Unit Tests  1.6.0
The foxBMS Unit Tests API Documentation
test_adi_ades1830.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_adi_ades1830.c
44  * @author foxBMS Team
45  * @date 2020-08-10 (date of creation)
46  * @updated 2023-10-12 (date of last update)
47  * @version v1.6.0
48  * @ingroup UNIT_TEST_IMPLEMENTATION
49  * @prefix TEST
50  *
51  * @brief Test of some module
52  *
53  */
54 
55 /*========== Includes =======================================================*/
56 #include "unity.h"
57 #include "Mockadi_ades1830_defs.h"
58 #include "Mockadi_ades183x_balancing.h"
59 #include "Mockadi_ades183x_defs.h"
60 #include "Mockadi_ades183x_diagnostic.h"
61 #include "Mockadi_ades183x_gpio_voltages.h"
62 #include "Mockadi_ades183x_helpers.h"
63 #include "Mockadi_ades183x_initialization.h"
64 #include "Mockadi_ades183x_pec.h"
65 #include "Mockadi_ades183x_temperatures.h"
66 #include "Mockadi_ades183x_voltages.h"
67 #include "Mockafe_plausibility.h"
68 #include "Mockdatabase.h"
69 #include "Mockdiag.h"
70 #include "Mockdma.h"
71 #include "Mockfassert.h"
72 #include "Mockftask.h"
73 #include "Mockinfinite-loop-helper.h"
74 #include "Mockio.h"
75 #include "Mockos.h"
76 #include "Mockpex.h"
77 #include "Mockqueue.h"
78 #include "Mockspi.h"
79 #include "Mockspi_cfg.h"
80 #include "Mocktask.h"
81 #include "Mocktsi.h"
82 
83 #include "adi_ades183x_cfg.h"
84 
85 #include "adi_ades183x.h"
86 #include "adi_ades183x_buffers.h" /* use the real command config */
87 #include "adi_ades183x_commands.h" /* use the real buffer configuration */
88 #include "spi_cfg-helper.h"
89 #include "test_assert_helper.h"
90 
91 #include <stdbool.h>
92 #include <stdint.h>
93 
94 /*========== Unit Testing Framework Directives ==============================*/
95 TEST_SOURCE_FILE("adi_ades1830_cfg.c")
96 TEST_SOURCE_FILE("adi_ades183x.c")
97 
98 TEST_INCLUDE_PATH("../../src/app/application/config")
99 TEST_INCLUDE_PATH("../../src/app/driver/afe/adi/ades1830")
100 TEST_INCLUDE_PATH("../../src/app/driver/afe/adi/common/ades183x")
101 TEST_INCLUDE_PATH("../../src/app/driver/afe/adi/common/ades183x/config")
102 TEST_INCLUDE_PATH("../../src/app/driver/afe/adi/common/ades183x/pec")
103 TEST_INCLUDE_PATH("../../src/app/driver/afe/api")
104 TEST_INCLUDE_PATH("../../src/app/driver/config")
105 TEST_INCLUDE_PATH("../../src/app/driver/dma")
106 TEST_INCLUDE_PATH("../../src/app/driver/io")
107 TEST_INCLUDE_PATH("../../src/app/driver/pex")
108 TEST_INCLUDE_PATH("../../src/app/driver/rtc")
109 TEST_INCLUDE_PATH("../../src/app/driver/spi")
110 TEST_INCLUDE_PATH("../../src/app/driver/ts/api")
111 TEST_INCLUDE_PATH("../../src/app/engine/database")
112 TEST_INCLUDE_PATH("../../src/app/engine/diag")
113 TEST_INCLUDE_PATH("../../src/app/task/config")
114 TEST_INCLUDE_PATH("../../src/app/task/ftask")
115 
116 /*========== Definitions and Implementations for Unit Test ==================*/
117 
118 /** SPI data configuration struct for ADI communication */
120  { /* struct is implemented in the TI HAL and uses uppercase true and false */
121  .CS_HOLD = TRUE, /* If true, HW chip select kept active between words */
122  .WDEL = FALSE, /* Activation of delay between words */
123  .DFSEL = SPI_FMT_0, /* Data word format selection */
125 };
126 
127 /**
128  * SPI interface configuration for ADI communication
129  * This is a list of structs because of multistring
130  */
132  {
133  .pConfig = &spi_kAdiDataConfig[0u],
134  .pNode = spiREG1,
135  .pGioPort = &(spiREG1->PC3),
136  .csPin = 2u,
137  .csType = SPI_CHIP_SELECT_HARDWARE,
138  },
139 };
140 
142 
143 /*========== Setup and Teardown =============================================*/
144 void setUp(void) {
145 }
146 
147 void tearDown(void) {
148 }
149 
150 /*========== Test Cases =====================================================*/
151 
153  /* Invalid pointer test */
155 
159 
163 }
164 
166  AFE_REQUEST_e testRequest = AFE_START_REQUEST;
167  STD_RETURN_TYPE_e queued = STD_NOT_OK;
168  OS_SendToBackOfQueue_ExpectAndReturn(ftsk_afeRequestQueue, (void *)&testRequest, ADI_QUEUE_TIMEOUT_MS, OS_SUCCESS);
169  queued = ADI_MakeRequest(testRequest);
170  TEST_ASSERT_EQUAL(STD_OK, queued);
171 
172  queued = STD_OK;
173  OS_SendToBackOfQueue_ExpectAndReturn(ftsk_afeRequestQueue, (void *)&testRequest, ADI_QUEUE_TIMEOUT_MS, OS_FAIL);
174  queued = ADI_MakeRequest(testRequest);
175  TEST_ASSERT_EQUAL(STD_NOT_OK, queued);
176 }
177 
179  /* 8 pins are needed, and they are aligned after PEX_PIN10 so we make the test code short with some loop */
180  const uint8_t nrOfPins = 8u;
181  for (uint8_t i = 0; i < nrOfPins; i++) {
182  PEX_SetPinDirectionOutput_Expect(PEX_PORT_EXPANDER3, PEX_PIN10 + i);
183  }
184  for (uint8_t i = 0; i < nrOfPins; i++) {
185  PEX_SetPin_Expect(PEX_PORT_EXPANDER3, PEX_PIN10 + i);
186  }
188 }
189 
191  /* Invalid pointer test */
193 }
195  FOREVER_ExpectAndReturn(0);
197 }
198 
200  /* 1. measurement not started */
201  /* 1.1. request could not be retrieved - just wait */
203  FOREVER_ExpectAndReturn(1);
204  ADI_Wait_Ignore();
205  OS_ReceiveFromQueue_IgnoreAndReturn(OS_FAIL);
207  TEST_ASSERT_FALSE(adi_stateBase.measurementStarted); /* start request has not been issued */
208 }
209 
211  /* 1. measurement not started */
212  /* 1.2. request retrieved, but not the start request - therefore wait */
214  FOREVER_ExpectAndReturn(1);
215  ADI_Wait_Ignore();
216  OS_ReceiveFromQueue_IgnoreAndReturn(OS_SUCCESS);
218  TEST_ASSERT_FALSE(adi_stateBase.measurementStarted); /* start request has not been issued */
219 }
220 
222  /* Invalid pointer test */
224 
225  /* Write measured data */
226  DATA_Write4DataBlocks_ExpectAndReturn(
231  STD_OK);
232  /* Leave some time for other tasks */
233  ADI_Wait_Ignore();
234  /* Read balancing orders */
235  DATA_Read1DataBlock_ExpectAndReturn(adi_stateBase.data.balancingControl, STD_OK);
237 }
238 
240  AFE_REQUEST_e request = AFE_NO_REQUEST;
241  AFE_REQUEST_e requestValue = AFE_START_REQUEST;
242 
243  /* Invalid pointer test */
246 
247  /* No request received */
248  OS_ReceiveFromQueue_IgnoreAndReturn(OS_FAIL);
249  ADI_Wait_Expect(1u);
251 
252  /* Request received, but not start */
253  requestValue = AFE_STOP_REQUEST;
254  OS_ReceiveFromQueue_ExpectAndReturn(ftsk_afeRequestQueue, &request, ADI_QUEUE_TIMEOUT_MS, OS_SUCCESS);
255  OS_ReceiveFromQueue_IgnoreArg_pvBuffer();
256  OS_ReceiveFromQueue_ReturnThruPtr_pvBuffer(&requestValue);
257  ADI_Wait_Expect(1u);
259  TEST_ASSERT_EQUAL(AFE_STOP_REQUEST, request);
260 
261  /* Request received and is start request */
262  requestValue = AFE_START_REQUEST;
263  OS_ReceiveFromQueue_ExpectAndReturn(ftsk_afeRequestQueue, &request, ADI_QUEUE_TIMEOUT_MS, OS_SUCCESS);
264  OS_ReceiveFromQueue_IgnoreArg_pvBuffer();
265  OS_ReceiveFromQueue_ReturnThruPtr_pvBuffer(&requestValue);
266  ADI_InitializeMeasurement_Expect(&adi_stateBase);
268  TEST_ASSERT_EQUAL(AFE_START_REQUEST, request);
269 }
270 
272  /* Invalid pointer test */
274 
275  /* Test for different balancing patterns */
276  /* Enum to run through all balancing tests */
277  typedef enum {
278  TEST_ADI_BALANCING_VALUE0,
279  TEST_ADI_BALANCING_VALUE1,
280  TEST_ADI_BALANCING_VALUE_E_MAX,
281  } TEST_ADI_BALANCING_e;
282  /* Test for 0xAA and 0x55 balancing patterns */
283  for (TEST_ADI_BALANCING_e i = TEST_ADI_BALANCING_VALUE0; i < TEST_ADI_BALANCING_VALUE_E_MAX; i++) {
284  for (uint8_t s = 0u; s < BS_NR_OF_STRINGS; s++) {
286  /* Mocks for unmute commands */
287  ADI_CopyCommandBits_Expect(adi_cmdUnmute, adi_command);
288  ADI_TransmitCommand_Expect(adi_command, &adi_stateBase);
289 
290  /* actual register configuration for the specific AFE */
291  ADI_DetermineBalancingRegisterConfiguration_Expect(&adi_stateBase);
292 
293  ADI_Wait_Ignore();
294 
295  /* Mocks for mute commands */
296  ADI_CopyCommandBits_Expect(adi_cmdMute, adi_command);
297  ADI_TransmitCommand_Expect(adi_command, &adi_stateBase);
299  }
300  }
301 }
302 
303 void testADI_GetRequest(void) {
304  AFE_REQUEST_e request = AFE_NO_REQUEST;
305  AFE_REQUEST_e requestValue = AFE_START_REQUEST;
306  /* Invalid pointer test */
308 
309  /* Request received from queue: function must return STD_OK */
310  OS_ReceiveFromQueue_IgnoreAndReturn(OS_SUCCESS);
311  TEST_ASSERT_EQUAL(STD_OK, TEST_ADI_GetRequest(&request));
312  /* No request received from queue: function must return STD_NOT_OK */
313  OS_ReceiveFromQueue_IgnoreAndReturn(OS_FAIL);
314  TEST_ASSERT_EQUAL(STD_NOT_OK, TEST_ADI_GetRequest(&request));
315 
316  /* Test setting value of request when a request is present in queue */
317  OS_ReceiveFromQueue_ExpectAndReturn(ftsk_afeRequestQueue, &request, ADI_QUEUE_TIMEOUT_MS, OS_SUCCESS);
318  OS_ReceiveFromQueue_IgnoreArg_pvBuffer();
319  OS_ReceiveFromQueue_ReturnThruPtr_pvBuffer(&requestValue);
320  TEST_ASSERT_EQUAL(AFE_NO_REQUEST, request);
321  TEST_ASSERT_EQUAL(STD_OK, TEST_ADI_GetRequest(&request));
322  TEST_ASSERT_EQUAL(AFE_START_REQUEST, request);
323 }
STD_RETURN_TYPE_e ADI_MakeRequest(AFE_REQUEST_e request)
Makes a request to the ADI driver.
Definition: adi_ades183x.c:342
void TEST_ADI_AccessToDatabase(ADI_STATE_s *adiState)
Definition: adi_ades183x.c:400
ADI_STATE_s adi_stateBase
Definition: adi_ades183x.c:116
void ADI_MeasurementCycle(ADI_STATE_s *adiState)
Implements the actual measurement sequence for the ADI driver.
Definition: adi_ades183x.c:364
bool TEST_ADI_ProcessMeasurementNotStartedState(ADI_STATE_s *adiState, AFE_REQUEST_e *request)
Definition: adi_ades183x.c:409
bool ADI_IsFirstMeasurementCycleFinished(ADI_STATE_s *adiState)
Gets the measurement initialization status.
Definition: adi_ades183x.c:354
STD_RETURN_TYPE_e TEST_ADI_GetRequest(AFE_REQUEST_e *request)
Definition: adi_ades183x.c:406
void TEST_ADI_BalanceControl(ADI_STATE_s *adiState)
Definition: adi_ades183x.c:403
void ADI_ActivateInterfaceBoard(void)
Sets the pins to activate the interface board.
Definition: adi_ades183x.c:322
Headers for the driver for the ADI analog front-end.
uint16_t adi_command[ADI_COMMAND_DEFINITION_LENGTH]
Header for the buffers used by the driver for the ADI analog front-end.
Header for the configuration for the ADI analog front-end.
const uint16_t adi_cmdUnmute[ADI_COMMAND_DEFINITION_LENGTH]
const uint16_t adi_cmdMute[ADI_COMMAND_DEFINITION_LENGTH]
Header file of some software.
#define ADI_QUEUE_TIMEOUT_MS
AFE_REQUEST_e
Definition: afe.h:69
@ AFE_STOP_REQUEST
Definition: afe.h:71
@ AFE_START_REQUEST
Definition: afe.h:70
@ AFE_NO_REQUEST
Definition: afe.h:72
#define BS_NR_OF_STRINGS
Number of parallel strings in the battery pack.
static DIAG_DIAGNOSIS_STATE_s diag
Definition: diag.c:71
STD_RETURN_TYPE_e
Definition: fstd_types.h:82
@ 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
@ OS_SUCCESS
Definition: os.h:86
@ OS_FAIL
Definition: os.h:87
#define PEX_PIN10
Definition: pex_cfg.h:90
#define PEX_PORT_EXPANDER3
Definition: pex_cfg.h:77
Headers for the configuration for the SPI module.
#define SPI_HARDWARE_CHIP_SELECT_2_ACTIVE
@ SPI_CHIP_SELECT_HARDWARE
Definition: spi_cfg.h:117
DATA_BLOCK_CELL_VOLTAGE_s * cellVoltageFiltered
DATA_BLOCK_BALANCING_CONTROL_s * balancingControl
DATA_BLOCK_CELL_TEMPERATURE_s * cellTemperature
DATA_BLOCK_CELL_VOLTAGE_s * cellVoltage
DATA_BLOCK_ALL_GPIO_VOLTAGES_s * allGpioVoltages
spiDAT1_t * pConfig
Definition: spi_cfg.h:126
void testADI_MeasurementCycleMeasurementNotStartedCase0(void)
void testADI_MeasurementCycleNoForever(void)
static spiDAT1_t spi_kAdiDataConfig[BS_NR_OF_STRINGS]
void testADI_GetRequest(void)
SPI_INTERFACE_CONFIG_s spi_adiInterface[BS_NR_OF_STRINGS]
void testADI_MakeRequest(void)
void testADI_ProcessMeasurementNotStartedState(void)
void setUp(void)
void tearDown(void)
void testADI_BalanceControl(void)
void testADI_AccessToDatabase(void)
void testADI_ActivateInterfaceBoard(void)
void testADI_MeasurementCycleMeasurementNotStartedCase1(void)
OS_QUEUE ftsk_afeRequestQueue
void testADI_MeasurementCycleAssertValidation(void)
void testADI_IsFirstMeasurementCycleFinished(void)
Helper for unit tests.
#define TEST_ASSERT_FAIL_ASSERT(_code_under_test)
assert whether assert macro has failed