foxBMS - Unit Tests  1.6.0
The foxBMS Unit Tests API Documentation
test_adi_ades1830_pec.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_pec.c
44  * @author foxBMS Team
45  * @date 2022-11-04 (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 ADI PEC computations
52  *
53  */
54 
55 /*========== Includes =======================================================*/
56 #include "unity.h"
57 
58 #include "adi_ades183x_pec.h"
59 #include "fstd_types.h"
60 #include "test_assert_helper.h"
61 
62 #include <stdbool.h>
63 #include <stdint.h>
64 
65 /*========== Unit Testing Framework Directives ==============================*/
66 TEST_SOURCE_FILE("adi_ades183x_pec.c")
67 
68 TEST_INCLUDE_PATH("../../src/app/driver/afe/adi/ades1830")
69 TEST_INCLUDE_PATH("../../src/app/driver/afe/adi/common/ades183x")
70 TEST_INCLUDE_PATH("../../src/app/driver/afe/adi/common/ades183x/config")
71 TEST_INCLUDE_PATH("../../src/app/driver/afe/adi/common/ades183x/pec")
72 TEST_INCLUDE_PATH("../../src/app/driver/afe/api")
73 TEST_INCLUDE_PATH("../../src/app/driver/config")
74 TEST_INCLUDE_PATH("../../src/app/driver/spi")
75 
76 /*========== Definitions and Implementations for Unit Test ==================*/
77 
78 /* Test for PEC15 computation on command */
79 uint8_t pec15Byte2Test0[2u] = {0x12u, 0x34u};
80 uint8_t pec15Byte2Test1[2u] = {0xBEu, 0xEFu};
81 uint8_t pec15Byte2Test2[2u] = {0x00u, 0x01u};
82 uint8_t pec15Byte2Test3[2u] = {0x07u, 0x23u};
83 uint8_t pec15Byte2Test4[2u] = {0x03u, 0x60u};
84 /* Test for PEC15 computation on 6 bytes of data */
85 uint8_t pec15Byte6Test0[6u] = {0x12u, 0x34u, 0x56u, 0x78u, 0x9Au, 0xBCu};
86 uint8_t pec15Byte6Test1[6u] = {0xFFu, 0xFFu, 0xFFu, 0xFFu, 0xFFu, 0xFFu};
87 uint8_t pec15Byte6Test2[6u] = {0xBEu, 0xEFu, 0xBEu, 0xEFu, 0xBEu, 0xEFu};
88 uint8_t pec15Byte6Test3[6u] = {0xA0u, 0xB0u, 0xC0u, 0xD0u, 0xE0u, 0xF0u};
89 uint8_t pec15Byte6Test4[6u] = {0x01u, 0x2Eu, 0x9Bu, 0x33u, 0xACu, 0x55u};
90 
91 /* Test for PEC10 computation on 6 bytes of data + 6 bits of command counter */
92 uint8_t pec10Byte7Test0[7u] = {0x12u, 0x34u, 0x56u, 0x78u, 0x9Au, 0xBCu, 0xDEu};
93 uint8_t pec10Byte7Test1[7u] = {0x12u, 0x34u, 0x56u, 0x78u, 0x9Au, 0xBCu, 0x00u};
94 uint8_t pec10Byte7Test2[7u] = {0xFFu, 0xFFu, 0xFFu, 0xFFu, 0xFFu, 0xFFu, 0xFFu};
95 uint8_t pec10Byte7Test3[7u] = {0xFFu, 0xFFu, 0xFFu, 0xFFu, 0xFFu, 0xFFu, 0x00u};
96 uint8_t pec10Byte7Test4[7u] = {0xBEu, 0xEFu, 0xBEu, 0xEFu, 0xBEu, 0xEFu, 0x35u};
97 uint8_t pec10Byte7Test5[7u] = {0xBEu, 0xEFu, 0xBEu, 0xEFu, 0xBEu, 0xEFu, 0x00u};
98 /* Test for PEC10 if command counter bits are handled correctly */
99 uint8_t pec10Byte7Test6[7u] = {0x12u, 0x34u, 0x56u, 0x78u, 0x9Au, 0xBCu, 0x00u};
100 uint8_t pec10Byte7Test7[7u] = {0x12u, 0x34u, 0x56u, 0x78u, 0x9Au, 0xBCu, 0xFFu};
101 
102 /*========== Setup and Teardown =============================================*/
103 void setUp(void) {
104 }
105 
106 void tearDown(void) {
107 }
108 
109 /*========== Test Cases =====================================================*/
110 
111 void testAdiPec15(void) {
112  TEST_ASSERT_EQUAL(0x1DD4u, ADI_Pec15(2u, pec15Byte2Test0));
113  TEST_ASSERT_EQUAL(0xFD2Cu, ADI_Pec15(2u, pec15Byte2Test1));
114  TEST_ASSERT_EQUAL(0x3D6Eu, ADI_Pec15(2u, pec15Byte2Test2));
115  TEST_ASSERT_EQUAL(0xB9E4u, ADI_Pec15(2u, pec15Byte2Test3));
116  TEST_ASSERT_EQUAL(0xF46Cu, ADI_Pec15(2u, pec15Byte2Test4));
117 
118  TEST_ASSERT_EQUAL(0xBC24u, ADI_Pec15(6u, pec15Byte6Test0));
119  TEST_ASSERT_EQUAL(0x664Cu, ADI_Pec15(6u, pec15Byte6Test1));
120  TEST_ASSERT_EQUAL(0x845Au, ADI_Pec15(6u, pec15Byte6Test2));
121  TEST_ASSERT_EQUAL(0x792Cu, ADI_Pec15(6u, pec15Byte6Test3));
122  TEST_ASSERT_EQUAL(0xD522u, ADI_Pec15(6u, pec15Byte6Test4));
123 }
124 
125 void testAdiPec10(void) {
126  TEST_ASSERT_EQUAL(0x1CCu, ADI_Pec10(6u, pec10Byte7Test0, false));
127  TEST_ASSERT_EQUAL(0x53u, ADI_Pec10(6u, pec10Byte7Test0, true));
128  TEST_ASSERT_EQUAL(0x1CCu, ADI_Pec10(6u, pec10Byte7Test1, false));
129  TEST_ASSERT_EQUAL(0x1CCu, ADI_Pec10(6u, pec10Byte7Test1, true));
130 
131  TEST_ASSERT_EQUAL(0x196u, ADI_Pec10(6u, pec10Byte7Test2, false));
132  TEST_ASSERT_EQUAL(0xFEu, ADI_Pec10(6u, pec10Byte7Test2, true));
133  TEST_ASSERT_EQUAL(0x196u, ADI_Pec10(6u, pec10Byte7Test3, false));
134  TEST_ASSERT_EQUAL(0x196u, ADI_Pec10(6u, pec10Byte7Test3, true));
135 
136  TEST_ASSERT_EQUAL(0x3F3u, ADI_Pec10(6u, pec10Byte7Test4, false));
137  TEST_ASSERT_EQUAL(0x1B7u, ADI_Pec10(6u, pec10Byte7Test4, true));
138  TEST_ASSERT_EQUAL(0x3F3u, ADI_Pec10(6u, pec10Byte7Test5, false));
139  TEST_ASSERT_EQUAL(0x3F3u, ADI_Pec10(6u, pec10Byte7Test5, true));
140 
141  /* pec10Test6: command counter bits set to 0 and taken into account because the receive */
142  /* parameter is set to true */
143  /* pec10Test7: command counter bits not set to 0 in data but must be ignored and replaced by 0 */
144  /* because the receive parameter is set to false */
145  /* So both function calls must return the same result */
146  TEST_ASSERT_EQUAL(ADI_Pec10(6u, pec10Byte7Test6, true), ADI_Pec10(6u, pec10Byte7Test7, false));
147 }
148 
150  /* invalid data length */
152  /* invalid data */
154 }
155 
157  /* invalid data length */
159  /* invalid data */
161 }
uint16_t ADI_Pec10(uint8_t length, uint8_t *data, bool receive)
calculates the PEC10 K
uint16_t ADI_Pec15(uint8_t length, uint8_t *data)
calculates the PEC15
Headers for the PEC computations.
Definition of foxBMS standard types.
#define NULL_PTR
Null pointer.
Definition: fstd_types.h:77
uint8_t pec15Byte2Test4[2u]
void testAdiPec15Asserts(void)
uint8_t pec10Byte7Test4[7u]
void testAdiPec10Asserts(void)
uint8_t pec10Byte7Test7[7u]
uint8_t pec15Byte2Test2[2u]
void testAdiPec15(void)
uint8_t pec15Byte2Test3[2u]
uint8_t pec15Byte6Test3[6u]
void setUp(void)
void tearDown(void)
uint8_t pec15Byte6Test0[6u]
uint8_t pec10Byte7Test3[7u]
uint8_t pec15Byte6Test2[6u]
uint8_t pec10Byte7Test1[7u]
uint8_t pec10Byte7Test0[7u]
uint8_t pec15Byte2Test1[2u]
uint8_t pec15Byte6Test4[6u]
uint8_t pec15Byte2Test0[2u]
uint8_t pec10Byte7Test5[7u]
void testAdiPec10(void)
uint8_t pec10Byte7Test6[7u]
uint8_t pec15Byte6Test1[6u]
uint8_t pec10Byte7Test2[7u]
Helper for unit tests.
#define TEST_ASSERT_FAIL_ASSERT(_code_under_test)
assert whether assert macro has failed