foxBMS - Unit Tests  1.4.1
The foxBMS Unit Tests API Documentation
test_spi.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_spi.c
44  * @author foxBMS Team
45  * @date 2020-04-01 (date of creation)
46  * @updated 2022-10-27 (date of last update)
47  * @version v1.4.1
48  * @ingroup UNIT_TEST_IMPLEMENTATION
49  * @prefix TEST
50  *
51  * @brief Tests for the spi module
52  *
53  */
54 
55 /*========== Includes =======================================================*/
56 #include "unity.h"
57 #include "MockHL_spi.h"
58 #include "MockHL_sys_dma.h"
59 #include "Mockio.h"
60 #include "Mockmcu.h"
61 #include "Mockos.h"
62 
63 #include "dma_cfg.h"
64 #include "spi_cfg.h"
65 
66 #include "spi.h"
67 #include "test_assert_helper.h"
68 
69 /*========== Definitions and Implementations for Unit Test ==================*/
70 
71 long FSYS_RaisePrivilege(void) {
72  return 0;
73 }
74 
75 /** mock for testing with an SPI handle */
76 spiBASE_t spiMockHandle = {0};
77 
78 spi_config_reg_t spiMockConfigRegister = {0};
79 
80 /*========== Setup and Teardown =============================================*/
81 void setUp(void) {
82  /* make sure PC0 of config register is clean */
83  spiMockConfigRegister.CONFIG_PC0 = 0;
84 }
85 
86 void tearDown(void) {
87 }
88 
89 /*========== Test Cases =====================================================*/
90 /** simple API test that function guards against null pointer */
93 }
94 
95 /** test intended function of SPI_SetFunctional() for setting a bit */
97  /** fake a config register that is null and inject into function */
98  spiMockConfigRegister.CONFIG_PC0 = 0;
99  spi1GetConfigValue_Expect(NULL_PTR, CurrentValue);
100  spi1GetConfigValue_IgnoreArg_config_reg();
101  spi1GetConfigValue_ReturnThruPtr_config_reg(&spiMockConfigRegister);
102 
103  /* the function should call spiSetFunctional with a 1 at bit 10 */
104  spiSetFunctional_Expect(spiREG1, ((uint32_t)1u << 10u));
105 
106  SPI_SetFunctional(spiREG1, 10, true);
107 }
108 
109 /** test intended function of SPI_SetFunctional() for clearing a bit */
111  /** fake a config register that is UINT32_MAX and inject into function */
112  spiMockConfigRegister.CONFIG_PC0 = UINT32_MAX;
113  spi1GetConfigValue_Expect(NULL_PTR, CurrentValue);
114  spi1GetConfigValue_IgnoreArg_config_reg();
115  spi1GetConfigValue_ReturnThruPtr_config_reg(&spiMockConfigRegister);
116 
117  /* the function should call spiSetFunctional with a 0 at bit 10 */
118  spiSetFunctional_Expect(spiREG1, ~((uint32_t)1u << 10u));
119 
120  SPI_SetFunctional(spiREG1, 10, false);
121 }
122 
123 /** test usage of right API functions for SPI1 */
125  /* this test will fail if another function than the intended function is
126  called */
127  spi1GetConfigValue_Ignore();
128  spiSetFunctional_Ignore();
129  SPI_SetFunctional(spiREG1, 0, false);
130 }
131 
132 /** test usage of right API functions for SPI2 */
134  /* this test will fail if another function than the intended function is
135  called */
136  spi2GetConfigValue_Ignore();
137  spiSetFunctional_Ignore();
138  SPI_SetFunctional(spiREG2, 0, false);
139 }
140 
141 /** test usage of right API functions for SPI3 */
143  /* this test will fail if another function than the intended function is
144  called */
145  spi3GetConfigValue_Ignore();
146  spiSetFunctional_Ignore();
147  SPI_SetFunctional(spiREG3, 0, false);
148 }
149 
150 /** test usage of right API functions for SPI4 */
152  /* this test will fail if another function than the intended function is
153  called */
154  spi4GetConfigValue_Ignore();
155  spiSetFunctional_Ignore();
156  SPI_SetFunctional(spiREG4, 0, false);
157 }
158 
159 /** test usage of right API functions for SPI5 */
161  /* this test will fail if another function than the intended function is
162  called */
163  spi5GetConfigValue_Ignore();
164  spiSetFunctional_Ignore();
165  SPI_SetFunctional(spiREG5, 0, false);
166 }
167 
168 /** test invalid input to SPI_CheckInterfaceAvailable */
171 }
172 
173 /** test all return codes from HAL with SPI_CheckInterfaceAvailable */
175  SpiTxStatus_ExpectAndReturn(spiREG1, SPI_READY);
176  TEST_ASSERT_EQUAL(STD_OK, SPI_CheckInterfaceAvailable(spiREG1));
177 
178  SpiTxStatus_ExpectAndReturn(spiREG1, SPI_PENDING);
179  TEST_ASSERT_EQUAL(STD_NOT_OK, SPI_CheckInterfaceAvailable(spiREG1));
180 
181  SpiTxStatus_ExpectAndReturn(spiREG1, SPI_COMPLETED);
182  TEST_ASSERT_EQUAL(STD_OK, SPI_CheckInterfaceAvailable(spiREG1));
183 }
Headers for the configuration for the DMA module.
@ 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
void SPI_SetFunctional(spiBASE_t *pNode, enum spiPinSelect bit, bool hardwareControlled)
Sets the functional of a SPI pin.
Definition: spi.c:356
STD_RETURN_TYPE_e SPI_CheckInterfaceAvailable(spiBASE_t *pNode)
Returns STD_OK if the SPI interface can be used again.
Definition: spi.c:473
Headers for the driver for the SPI module.
Headers for the configuration for the SPI module.
Helper for unit tests.
#define TEST_ASSERT_FAIL_ASSERT(_code_under_test)
assert whether assert macro has failed
spiBASE_t spiMockHandle
Definition: test_spi.c:76
void testSPI_CheckInterfaceAvailableInvalidInput(void)
Definition: test_spi.c:169
void testSPI_SetFunctionalRightApiSpi5(void)
Definition: test_spi.c:160
void testSPI_CheckInterfaceAvailable(void)
Definition: test_spi.c:174
void testSPI_SetFunctionalRightApiSpi2(void)
Definition: test_spi.c:133
void testSPI_SetFunctionalTestIntendedFunctionClear(void)
Definition: test_spi.c:110
void testSPI_SetFunctionalTestIntendedFunctionSet(void)
Definition: test_spi.c:96
void testSPI_SetFunctionalRightApiSpi4(void)
Definition: test_spi.c:151
void testSPI_SetFunctionalNullPointer(void)
Definition: test_spi.c:91
void setUp(void)
Definition: test_spi.c:81
void tearDown(void)
Definition: test_spi.c:86
void testSPI_SetFunctionalRightApiSpi3(void)
Definition: test_spi.c:142
spi_config_reg_t spiMockConfigRegister
Definition: test_spi.c:78
long FSYS_RaisePrivilege(void)
Raise privilege.
Definition: test_spi.c:71
void testSPI_SetFunctionalRightApiSpi1(void)
Definition: test_spi.c:124