foxBMS-UnitTests  1.0.0
The foxBMS Unit Tests API Documentation
test_mxm_1785x_tools.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_tools.c
44  * @author foxBMS Team
45  * @date 2020-07-15 (date of creation)
46  * @updated 2020-07-15 (date of last update)
47  * @ingroup UNIT_TEST_IMPLEMENTATION
48  * @prefix MXM
49  *
50  * @brief Test for the Maxim 1785x driver tools.
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_1785x_tools.h"
62 #include "test_assert_helper.h"
63 
64 /*========== Definitions and Implementations for Unit Test ==================*/
65 
66 /*========== Setup and Teardown =============================================*/
67 void setUp(void) {
68 }
69 
70 void tearDown(void) {
71 }
72 
73 /*========== Test Cases =====================================================*/
76 }
77 
79  /* bogus conversion type */
80  uint8_t msb = 0;
81  uint8_t lsb = 0;
82  uint16_t voltage = 0;
83  TEST_ASSERT_FAIL_ASSERT(MXM_Convert(lsb, msb, &voltage, (MXM_CONVERSION_TYPE_e)42, 5000));
84 }
85 
87  /* full scale */
88  uint8_t msb = 0xFFu;
89  uint8_t lsb = 0xFCu;
90  uint16_t voltage = 0;
92  TEST_ASSERT_EQUAL(5000u, voltage);
93 }
94 
96  /* low scale */
97  uint8_t msb = 0x00u;
98  uint8_t lsb = 0x00u;
99  uint16_t voltage = 0;
100  TEST_ASSERT_PASS_ASSERT(MXM_Convert(lsb, msb, &voltage, MXM_CONVERSION_UNIPOLAR, 5000));
101  TEST_ASSERT_EQUAL(0u, voltage);
102 }
103 
105  /* half scale */
106  uint8_t msb = 0x80u;
107  uint8_t lsb = 0x00u;
108  uint16_t voltage = 0;
109  TEST_ASSERT_PASS_ASSERT(MXM_Convert(lsb, msb, &voltage, MXM_CONVERSION_UNIPOLAR, 5000));
110  TEST_ASSERT_EQUAL(2500u, voltage);
111 }
112 
114  fas_storeAssertLoc_Ignore();
115  TEST_ASSERT_PASS_ASSERT(TEST_ASSERT_EQUAL(STD_OK, MXM_ConvertTest()));
116 }
117 
119  TEST_ASSERT_PASS_ASSERT(TEST_ASSERT_EQUAL(STD_OK, MXM_FirstSetBitTest()));
120 }
121 
123  uint8_t lsb = 255;
124  uint8_t msb = 255;
125  MXM_Unipolar14BitInto16Bit(0u, &lsb, &msb);
126  TEST_ASSERT_EQUAL(0u, lsb);
127  TEST_ASSERT_EQUAL(0u, msb);
128 }
129 
131  uint8_t lsb = 0;
132  uint8_t msb = 0;
133  MXM_Unipolar14BitInto16Bit(0x3FFFu, &lsb, &msb);
134  TEST_ASSERT_EQUAL(0xFCu, lsb);
135  TEST_ASSERT_EQUAL(0xFFu, msb);
136 }
137 
139  TEST_ASSERT_EQUAL(0u, MXM_VoltageIntoUnipolar14Bit(0, 5000));
140  TEST_ASSERT_EQUAL(0x3FFFu, MXM_VoltageIntoUnipolar14Bit(5000, 5000));
141  /* get the half-scale value by rightshifting */
142  TEST_ASSERT_EQUAL((0x3FFFu >> 1u), MXM_VoltageIntoUnipolar14Bit(2500, 5000));
143 
144  /* test with fullscale value of block-voltage */
145  TEST_ASSERT_EQUAL(0x3FFFu, MXM_VoltageIntoUnipolar14Bit(65000, 65000));
146  /* test with halfscale value of block-voltage */
147  TEST_ASSERT_EQUAL((0x3FFFu >> 1u), MXM_VoltageIntoUnipolar14Bit(32500, 65000));
148 }
149 
151  /* halfscale
152  lsb = 0xFCu;
153  msb = 0x7Fu;
154  2.5V
155  */
156  uint16_t value_14bit = MXM_VoltageIntoUnipolar14Bit(2500u, 5000u);
157  TEST_ASSERT_EQUAL((0x3FFF >> 1u), value_14bit);
158 
159  uint8_t lsb = 0;
160  uint8_t msb = 0;
161  MXM_Unipolar14BitInto16Bit(value_14bit, &lsb, &msb);
162  TEST_ASSERT_EQUAL(0xFCu, lsb);
163  TEST_ASSERT_EQUAL(0x7Fu, msb);
164 }
165 
167  /* known values from driver:
168  lsb = 0x98u;
169  msb = 0x19u;
170  0.5V
171  */
172  uint16_t value_14bit = MXM_VoltageIntoUnipolar14Bit(500u, 5000u);
173  TEST_ASSERT_EQUAL((0x1998u >> 2u), value_14bit);
174 
175  uint8_t lsb = 0;
176  uint8_t msb = 0;
177  MXM_Unipolar14BitInto16Bit(value_14bit, &lsb, &msb);
178  TEST_ASSERT_EQUAL(0x98u, lsb);
179  TEST_ASSERT_EQUAL(0x19u, msb);
180 }
181 
183  uint16_t voltage = 5000u;
184  uint16_t voltage_result = voltage;
185  uint8_t lsb = 0;
186  uint8_t msb = 0;
187 
188  uint16_t converted_voltage = MXM_VoltageIntoUnipolar14Bit(voltage, 5000);
189 
190  MXM_Unipolar14BitInto16Bit(converted_voltage, &lsb, &msb);
191 
192  TEST_ASSERT_PASS_ASSERT(MXM_Convert(lsb, msb, &voltage_result, MXM_CONVERSION_UNIPOLAR, 5000));
193 
194  TEST_ASSERT_EQUAL(voltage, voltage_result);
195 }
196 
198  uint16_t voltage = 0u;
199  uint16_t voltage_result = voltage;
200  uint8_t lsb = 0;
201  uint8_t msb = 0;
202 
203  uint16_t converted_voltage = MXM_VoltageIntoUnipolar14Bit(voltage, 5000);
204 
205  MXM_Unipolar14BitInto16Bit(converted_voltage, &lsb, &msb);
206 
207  TEST_ASSERT_PASS_ASSERT(MXM_Convert(lsb, msb, &voltage_result, MXM_CONVERSION_UNIPOLAR, 5000));
208 
209  TEST_ASSERT_EQUAL(voltage, voltage_result);
210 }
211 
213  uint16_t voltage = 4200u;
214  uint16_t voltage_result = voltage;
215  uint8_t lsb = 0;
216  uint8_t msb = 0;
217 
218  uint16_t converted_voltage = MXM_VoltageIntoUnipolar14Bit(voltage, 5000);
219  TEST_ASSERT_EQUAL(0x35C1u, converted_voltage);
220 
221  MXM_Unipolar14BitInto16Bit(converted_voltage, &lsb, &msb);
222  TEST_ASSERT_EQUAL(0x04u, lsb);
223  TEST_ASSERT_EQUAL(0xD7u, msb);
224 
225  TEST_ASSERT_PASS_ASSERT(MXM_Convert(lsb, msb, &voltage_result, MXM_CONVERSION_UNIPOLAR, 5000));
226 
227  TEST_ASSERT_EQUAL(voltage, voltage_result);
228 }
229 
230 /* WARNING: in order for the MXM_ConvertModuleToString tests to work as expected,
231  make sure that both test and source have been rebuilt (clear the build) after
232  changing system defines such as BS_NR_OF_MODULES */
233 
235  uint8_t stringNumber = 0u;
236  uint16_t moduleNumberInString = 0u;
237  TEST_ASSERT_FAIL_ASSERT(MXM_ConvertModuleToString(0u, NULL_PTR, &moduleNumberInString));
239 }
240 
242  uint8_t stringNumber = 0u;
243  uint16_t moduleNumberInString = 0u;
244  /* module index #MXM_MAXIMUM_NR_OF_MODULES should not be ok */
245  TEST_ASSERT_FAIL_ASSERT(MXM_ConvertModuleToString(MXM_MAXIMUM_NR_OF_MODULES, &stringNumber, &moduleNumberInString));
246 }
247 
249  uint8_t stringNumber = 0u;
250  uint16_t moduleNumberInString = 0u;
251  /* The system cannot have more modules than BS_NR_OF_STRINGS*BS_NR_OF_MODULES */
253  MXM_ConvertModuleToString((BS_NR_OF_STRINGS * BS_NR_OF_MODULES), &stringNumber, &moduleNumberInString));
254 }
255 
257  const uint8_t expectedStringNumber = 0u;
258  uint8_t stringNumber = 42u;
259  const uint16_t expectedModuleNumberInString = 0u;
260  uint16_t moduleNumberInString = 42u;
262  MXM_ConvertModuleToString((expectedStringNumber * BS_NR_OF_MODULES), &stringNumber, &moduleNumberInString));
263 
264  TEST_ASSERT_EQUAL(expectedStringNumber, stringNumber);
265  TEST_ASSERT_EQUAL(expectedModuleNumberInString, moduleNumberInString);
266 }
267 
269  /* skip this test if the BMS configuration does not fit to this test (we need a second string) */
270  if (BS_NR_OF_STRINGS < 2u) {
271  TEST_PASS_MESSAGE("This test is skipped due to the configuration of the BMS.");
272  }
273  const uint8_t expectedStringNumber = 1u;
274  uint8_t stringNumber = 0u;
275  const uint16_t expectedModuleNumberInString = 0u;
276  uint16_t moduleNumberInString = 42u;
278  MXM_ConvertModuleToString((expectedStringNumber * BS_NR_OF_MODULES), &stringNumber, &moduleNumberInString));
279 
280  TEST_ASSERT_EQUAL(expectedStringNumber, stringNumber);
281  TEST_ASSERT_EQUAL(expectedModuleNumberInString, moduleNumberInString);
282 }
283 
285  /* skip this test if the BMS configuration does not fit to this test (we need a second module) */
286  if (BS_NR_OF_MODULES < 2u) {
287  TEST_PASS_MESSAGE("This test is skipped due to the configuration of the BMS.");
288  }
289  const uint8_t expectedStringNumber = 0u;
290  uint8_t stringNumber = 42u;
291  const uint16_t expectedModuleNumberInString = 1u;
292  uint16_t moduleNumberInString = 42u;
294  ((expectedStringNumber * BS_NR_OF_MODULES) + expectedModuleNumberInString),
295  &stringNumber,
296  &moduleNumberInString));
297 
298  TEST_ASSERT_EQUAL(expectedStringNumber, stringNumber);
299  TEST_ASSERT_EQUAL(expectedModuleNumberInString, moduleNumberInString);
300 }
301 
303  /* skip this test if the BMS configuration does not fit to this test (we need a second module) */
304  if ((BS_NR_OF_MODULES < 2u) || (BS_NR_OF_STRINGS < 2u)) {
305  TEST_PASS_MESSAGE("This test is skipped due to the configuration of the BMS.");
306  }
307  const uint8_t expectedStringNumber = 1u;
308  uint8_t stringNumber = 42u;
309  const uint16_t expectedModuleNumberInString = 1u;
310  uint16_t moduleNumberInString = 42u;
312  (expectedModuleNumberInString + (expectedStringNumber * BS_NR_OF_MODULES)),
313  &stringNumber,
314  &moduleNumberInString));
315 
316  TEST_ASSERT_EQUAL(expectedStringNumber, stringNumber);
317  TEST_ASSERT_EQUAL(expectedModuleNumberInString, moduleNumberInString);
318 }
testMXM_ConvertFullScale
void testMXM_ConvertFullScale(void)
Definition: test_mxm_1785x_tools.c:86
testMXM_ExtractValueFromRegisterTest
void testMXM_ExtractValueFromRegisterTest(void)
Definition: test_mxm_1785x_tools.c:74
MXM_ExtractValueFromRegisterTest
STD_RETURN_TYPE_e must_check_return MXM_ExtractValueFromRegisterTest()
Test MXM_ExtractValueFromRegister().
Definition: mxm_1785x_tools.c:176
testMXM_ConvertModuleToStringFirstInFirstString
void testMXM_ConvertModuleToStringFirstInFirstString(void)
Definition: test_mxm_1785x_tools.c:256
testScaleAndConvert2500
void testScaleAndConvert2500(void)
Definition: test_mxm_1785x_tools.c:150
MXM_CONVERSION_TYPE_e
enum MXM_CONVERSION_TYPE MXM_CONVERSION_TYPE_e
testMXM_ConvertModuleToStringFirstInSecondString
void testMXM_ConvertModuleToStringFirstInSecondString(void)
Definition: test_mxm_1785x_tools.c:268
BS_NR_OF_MODULES
#define BS_NR_OF_MODULES
number of modules in battery pack
Definition: battery_system_cfg.h:96
testMXM_ConvertModuleToStringTooManyForMaxim
void testMXM_ConvertModuleToStringTooManyForMaxim(void)
Definition: test_mxm_1785x_tools.c:241
MXM_ConvertModuleToString
void MXM_ConvertModuleToString(const uint16_t moduleNumber, uint8_t *pStringNumber, uint16_t *pModuleNumberInString)
Get the string and module number from a global module number.
Definition: mxm_1785x_tools.c:224
setUp
void setUp(void)
Definition: test_mxm_1785x_tools.c:67
MXM_Convert
void MXM_Convert(uint8_t lsb, uint8_t msb, uint16_t *pTarget, MXM_CONVERSION_TYPE_e convType, uint32_t fullScaleReference_mV)
Convert a measurement value to a voltage value.
Definition: mxm_1785x_tools.c:98
testMXM_VoltageIntoUnipolar14Bit
void testMXM_VoltageIntoUnipolar14Bit(void)
Definition: test_mxm_1785x_tools.c:138
testMXM_ConvertModuleToStringTooManyForSystem
void testMXM_ConvertModuleToStringTooManyForSystem(void)
Definition: test_mxm_1785x_tools.c:248
MXM_ConvertTest
STD_RETURN_TYPE_e must_check_return MXM_ConvertTest()
Test the MXM_Convert()-function.
Definition: mxm_1785x_tools.c:127
test_assert_helper.h
Helper for unit tests.
tearDown
void tearDown(void)
Definition: test_mxm_1785x_tools.c:70
testMXM_ConvertHalfScale
void testMXM_ConvertHalfScale(void)
Definition: test_mxm_1785x_tools.c:104
testMXM_ConvertSelfCheck
void testMXM_ConvertSelfCheck(void)
Definition: test_mxm_1785x_tools.c:113
testMXM_ConvertModuleToStringNullPointer
void testMXM_ConvertModuleToStringNullPointer(void)
Definition: test_mxm_1785x_tools.c:234
MXM_MAXIMUM_NR_OF_MODULES
#define MXM_MAXIMUM_NR_OF_MODULES
Maximum number of modules.
Definition: mxm_basic_defines.h:73
MXM_FirstSetBitTest
STD_RETURN_TYPE_e must_check_return MXM_FirstSetBitTest()
Test MXM_FirstSetBit().
Definition: mxm_1785x_tools.c:85
mxm_1785x_tools.h
This is a collection of helper functions for the MAX1785x ICs.
MXM_CONVERSION_UNIPOLAR
@ MXM_CONVERSION_UNIPOLAR
Definition: mxm_basic_defines.h:131
testMXM_ConvertLowScale
void testMXM_ConvertLowScale(void)
Definition: test_mxm_1785x_tools.c:95
STD_OK
@ STD_OK
Definition: fstd_types.h:72
testConversionExtractAndInsertRoundTripNull
void testConversionExtractAndInsertRoundTripNull(void)
Definition: test_mxm_1785x_tools.c:197
testMXM_ConvertModuleToStringSecondInSecondString
void testMXM_ConvertModuleToStringSecondInSecondString(void)
Definition: test_mxm_1785x_tools.c:302
testMXM_ConvertModuleToStringSecondInFirstString
void testMXM_ConvertModuleToStringSecondInFirstString(void)
Definition: test_mxm_1785x_tools.c:284
testConversionExtractAndInsertRoundTripSomeValue
void testConversionExtractAndInsertRoundTripSomeValue(void)
Definition: test_mxm_1785x_tools.c:212
testScaleAndConvert500
void testScaleAndConvert500(void)
Definition: test_mxm_1785x_tools.c:166
testMXM_FirstSetBitTest
void testMXM_FirstSetBitTest(void)
Definition: test_mxm_1785x_tools.c:118
MXM_VoltageIntoUnipolar14Bit
uint16_t MXM_VoltageIntoUnipolar14Bit(uint16_t voltage_mV, uint16_t fullscaleReference_mV)
convert a voltage value into a unipolar 14bit value
Definition: mxm_1785x_tools.c:216
MXM_Unipolar14BitInto16Bit
void MXM_Unipolar14BitInto16Bit(uint16_t inputValue, uint8_t *lsb, uint8_t *msb)
convert a unipolar 14bit-value and shifts it into the 16bit-format
Definition: mxm_1785x_tools.c:205
NULL_PTR
#define NULL_PTR
Null pointer.
Definition: fstd_types.h:66
testMXM_Unipolar14BitInto16BitWithInput3FFF
void testMXM_Unipolar14BitInto16BitWithInput3FFF(void)
Definition: test_mxm_1785x_tools.c:130
testMXM_Unipolar14BitInto16BitWithInput0
void testMXM_Unipolar14BitInto16BitWithInput0(void)
Definition: test_mxm_1785x_tools.c:122
testConversionExtractAndInsertRoundTripFullScale
void testConversionExtractAndInsertRoundTripFullScale(void)
Definition: test_mxm_1785x_tools.c:182
TEST_ASSERT_FAIL_ASSERT
#define TEST_ASSERT_FAIL_ASSERT(_code_under_test)
assert whether assert macro has failed
Definition: test_assert_helper.h:70
BS_NR_OF_STRINGS
#define BS_NR_OF_STRINGS
Definition: battery_system_cfg.h:89
testMXM_ConvertInvalidConversionType
void testMXM_ConvertInvalidConversionType(void)
Definition: test_mxm_1785x_tools.c:78
TEST_ASSERT_PASS_ASSERT
#define TEST_ASSERT_PASS_ASSERT(_code_under_test)
assert whether assert macro has passed
Definition: test_assert_helper.h:88