foxBMS - Unit Tests  1.2.1
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 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_1785x_tools.c
44  * @author foxBMS Team
45  * @date 2020-07-15 (date of creation)
46  * @updated 2021-12-06 (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 
78 /** test #MXM_ExtractValueFromRegister() with an ADC value */
80  const uint8_t lsb = 0xFCu;
81  const uint8_t msb = 0xFFu;
82  uint16_t value = 0x00u;
84  TEST_ASSERT_EQUAL(0x3FFFu, value);
85 }
86 
87 /** test #MXM_ExtractValueFromRegister() with a single bit in lsb */
89  const uint8_t lsb = 0x10u;
90  const uint8_t msb = 0x00u;
91  uint16_t value = 0x00u;
93  TEST_ASSERT_EQUAL(1u, value);
94 }
95 
96 /** test #MXM_ExtractValueFromRegister() without a bit in lsb */
98  const uint8_t lsb = 0x00u;
99  const uint8_t msb = 0x00u;
100  uint16_t value = 0x00u;
102  TEST_ASSERT_EQUAL(0u, value);
103 }
104 
105 /** test #MXM_ExtractValueFromRegister() with whole lsb */
107  const uint8_t lsb = 0xFFu;
108  const uint8_t msb = 0x00u;
109  uint16_t value = 0x00u;
110  MXM_ExtractValueFromRegister(lsb, msb, MXM_BM_LSB, &value);
111  TEST_ASSERT_EQUAL(0xFFu, value);
112 }
113 
114 /** test #MXM_ExtractValueFromRegister() with whole msb */
116  const uint8_t lsb = 0x00u;
117  const uint8_t msb = 0xFFu;
118  uint16_t value = 0x00u;
119  MXM_ExtractValueFromRegister(lsb, msb, MXM_BM_MSB, &value);
120  TEST_ASSERT_EQUAL(0xFFu, value);
121 }
122 
123 /** test #MXM_ExtractValueFromRegister() with a single bit in msb */
125  const uint8_t lsb = 0x00u;
126  const uint8_t msb = 0x40u;
127  uint16_t value = 0x00u;
129  TEST_ASSERT_EQUAL(1u, value);
130 }
131 
132 /** test #MXM_ExtractValueFromRegister() with a single bit in msb and with full byte */
134  const uint8_t lsb = 0xFFu;
135  const uint8_t msb = 0xFFu;
136  uint16_t value = 0x00u;
138  TEST_ASSERT_EQUAL(1u, value);
139 }
140 
141 /** test #MXM_ExtractValueFromRegister() without a bit in msb */
143  const uint8_t lsb = 0x00u;
144  const uint8_t msb = 0x00u;
145  uint16_t value = 0x00u;
147  TEST_ASSERT_EQUAL(0u, value);
148 }
149 
151  /* bogus conversion type */
152  uint8_t msb = 0;
153  uint8_t lsb = 0;
154  uint16_t voltage = 0;
155  TEST_ASSERT_FAIL_ASSERT(MXM_Convert(lsb, msb, &voltage, (MXM_CONVERSION_TYPE_e)42, 5000));
156 }
157 
159  /* full scale */
160  uint8_t msb = 0xFFu;
161  uint8_t lsb = 0xFCu;
162  uint16_t voltage = 0;
163  TEST_ASSERT_PASS_ASSERT(MXM_Convert(lsb, msb, &voltage, MXM_CONVERSION_UNIPOLAR, 5000));
164  TEST_ASSERT_EQUAL(5000u, voltage);
165 }
166 
168  /* low scale */
169  uint8_t msb = 0x00u;
170  uint8_t lsb = 0x00u;
171  uint16_t voltage = 0;
172  TEST_ASSERT_PASS_ASSERT(MXM_Convert(lsb, msb, &voltage, MXM_CONVERSION_UNIPOLAR, 5000));
173  TEST_ASSERT_EQUAL(0u, voltage);
174 }
175 
177  /* half scale */
178  uint8_t msb = 0x80u;
179  uint8_t lsb = 0x00u;
180  uint16_t voltage = 0;
181  TEST_ASSERT_PASS_ASSERT(MXM_Convert(lsb, msb, &voltage, MXM_CONVERSION_UNIPOLAR, 5000));
182  TEST_ASSERT_EQUAL(2500u, voltage);
183 }
184 
186  fas_storeAssertLoc_Ignore();
187  TEST_ASSERT_PASS_ASSERT(TEST_ASSERT_EQUAL(STD_OK, MXM_ConvertTest()));
188 }
189 
191  TEST_ASSERT_PASS_ASSERT(TEST_ASSERT_EQUAL(STD_OK, MXM_FirstSetBitTest()));
192 }
193 
195  uint8_t lsb = 255;
196  uint8_t msb = 255;
197  MXM_Unipolar14BitInto16Bit(0u, &lsb, &msb);
198  TEST_ASSERT_EQUAL(0u, lsb);
199  TEST_ASSERT_EQUAL(0u, msb);
200 }
201 
203  uint8_t lsb = 0;
204  uint8_t msb = 0;
205  MXM_Unipolar14BitInto16Bit(0x3FFFu, &lsb, &msb);
206  TEST_ASSERT_EQUAL(0xFCu, lsb);
207  TEST_ASSERT_EQUAL(0xFFu, msb);
208 }
209 
211  TEST_ASSERT_EQUAL(0u, MXM_VoltageIntoUnipolar14Bit(0, 5000));
212  TEST_ASSERT_EQUAL(0x3FFFu, MXM_VoltageIntoUnipolar14Bit(5000, 5000));
213  /* get the half-scale value by rightshifting */
214  TEST_ASSERT_EQUAL((0x3FFFu >> 1u), MXM_VoltageIntoUnipolar14Bit(2500, 5000));
215 
216  /* test with fullscale value of block-voltage */
217  TEST_ASSERT_EQUAL(0x3FFFu, MXM_VoltageIntoUnipolar14Bit(65000, 65000));
218  /* test with halfscale value of block-voltage */
219  TEST_ASSERT_EQUAL((0x3FFFu >> 1u), MXM_VoltageIntoUnipolar14Bit(32500, 65000));
220 }
221 
223  /* halfscale
224  lsb = 0xFCu;
225  msb = 0x7Fu;
226  2.5V
227  */
228  uint16_t value_14bit = MXM_VoltageIntoUnipolar14Bit(2500u, 5000u);
229  TEST_ASSERT_EQUAL((0x3FFF >> 1u), value_14bit);
230 
231  uint8_t lsb = 0;
232  uint8_t msb = 0;
233  MXM_Unipolar14BitInto16Bit(value_14bit, &lsb, &msb);
234  TEST_ASSERT_EQUAL(0xFCu, lsb);
235  TEST_ASSERT_EQUAL(0x7Fu, msb);
236 }
237 
239  /* known values from driver:
240  lsb = 0x98u;
241  msb = 0x19u;
242  0.5V
243  */
244  uint16_t value_14bit = MXM_VoltageIntoUnipolar14Bit(500u, 5000u);
245  TEST_ASSERT_EQUAL((0x1998u >> 2u), value_14bit);
246 
247  uint8_t lsb = 0;
248  uint8_t msb = 0;
249  MXM_Unipolar14BitInto16Bit(value_14bit, &lsb, &msb);
250  TEST_ASSERT_EQUAL(0x98u, lsb);
251  TEST_ASSERT_EQUAL(0x19u, msb);
252 }
253 
255  uint16_t voltage = 5000u;
256  uint16_t voltage_result = voltage;
257  uint8_t lsb = 0;
258  uint8_t msb = 0;
259 
260  uint16_t converted_voltage = MXM_VoltageIntoUnipolar14Bit(voltage, 5000);
261 
262  MXM_Unipolar14BitInto16Bit(converted_voltage, &lsb, &msb);
263 
264  TEST_ASSERT_PASS_ASSERT(MXM_Convert(lsb, msb, &voltage_result, MXM_CONVERSION_UNIPOLAR, 5000));
265 
266  TEST_ASSERT_EQUAL(voltage, voltage_result);
267 }
268 
270  uint16_t voltage = 0u;
271  uint16_t voltage_result = voltage;
272  uint8_t lsb = 0;
273  uint8_t msb = 0;
274 
275  uint16_t converted_voltage = MXM_VoltageIntoUnipolar14Bit(voltage, 5000);
276 
277  MXM_Unipolar14BitInto16Bit(converted_voltage, &lsb, &msb);
278 
279  TEST_ASSERT_PASS_ASSERT(MXM_Convert(lsb, msb, &voltage_result, MXM_CONVERSION_UNIPOLAR, 5000));
280 
281  TEST_ASSERT_EQUAL(voltage, voltage_result);
282 }
283 
285  uint16_t voltage = 4200u;
286  uint16_t voltage_result = voltage;
287  uint8_t lsb = 0;
288  uint8_t msb = 0;
289 
290  uint16_t converted_voltage = MXM_VoltageIntoUnipolar14Bit(voltage, 5000);
291  TEST_ASSERT_EQUAL(0x35C1u, converted_voltage);
292 
293  MXM_Unipolar14BitInto16Bit(converted_voltage, &lsb, &msb);
294  TEST_ASSERT_EQUAL(0x04u, lsb);
295  TEST_ASSERT_EQUAL(0xD7u, msb);
296 
297  TEST_ASSERT_PASS_ASSERT(MXM_Convert(lsb, msb, &voltage_result, MXM_CONVERSION_UNIPOLAR, 5000));
298 
299  TEST_ASSERT_EQUAL(voltage, voltage_result);
300 }
301 
302 /* WARNING: in order for the MXM_ConvertModuleToString tests to work as expected,
303  make sure that both test and source have been rebuilt (clear the build) after
304  changing system defines such as BS_NR_OF_MODULES */
305 
307  uint8_t stringNumber = 0u;
308  uint16_t moduleNumberInString = 0u;
309  TEST_ASSERT_FAIL_ASSERT(MXM_ConvertModuleToString(0u, NULL_PTR, &moduleNumberInString));
311 }
312 
314  uint8_t stringNumber = 0u;
315  uint16_t moduleNumberInString = 0u;
316  /* module index #MXM_MAXIMUM_NR_OF_MODULES should not be ok */
317  TEST_ASSERT_FAIL_ASSERT(MXM_ConvertModuleToString(MXM_MAXIMUM_NR_OF_MODULES, &stringNumber, &moduleNumberInString));
318 }
319 
321  uint8_t stringNumber = 0u;
322  uint16_t moduleNumberInString = 0u;
323  /* The system cannot have more modules than BS_NR_OF_STRINGS*BS_NR_OF_MODULES */
325  MXM_ConvertModuleToString((BS_NR_OF_STRINGS * BS_NR_OF_MODULES), &stringNumber, &moduleNumberInString));
326 }
327 
329  const uint8_t expectedStringNumber = 0u;
330  uint8_t stringNumber = 42u;
331  const uint16_t expectedModuleNumberInString = 0u;
332  uint16_t moduleNumberInString = 42u;
334  MXM_ConvertModuleToString((expectedStringNumber * BS_NR_OF_MODULES), &stringNumber, &moduleNumberInString));
335 
336  TEST_ASSERT_EQUAL(expectedStringNumber, stringNumber);
337  TEST_ASSERT_EQUAL(expectedModuleNumberInString, moduleNumberInString);
338 }
339 
341  /* skip this test if the BMS configuration does not fit to this test (we need a second string) */
342  if (BS_NR_OF_STRINGS < 2u) {
343  TEST_PASS_MESSAGE("This test is skipped due to the configuration of the BMS.");
344  }
345  const uint8_t expectedStringNumber = 1u;
346  uint8_t stringNumber = 0u;
347  const uint16_t expectedModuleNumberInString = 0u;
348  uint16_t moduleNumberInString = 42u;
350  MXM_ConvertModuleToString((expectedStringNumber * BS_NR_OF_MODULES), &stringNumber, &moduleNumberInString));
351 
352  TEST_ASSERT_EQUAL(expectedStringNumber, stringNumber);
353  TEST_ASSERT_EQUAL(expectedModuleNumberInString, moduleNumberInString);
354 }
355 
357  /* skip this test if the BMS configuration does not fit to this test (we need a second module) */
358  if (BS_NR_OF_MODULES < 2u) {
359  TEST_PASS_MESSAGE("This test is skipped due to the configuration of the BMS.");
360  }
361  const uint8_t expectedStringNumber = 0u;
362  uint8_t stringNumber = 42u;
363  const uint16_t expectedModuleNumberInString = 1u;
364  uint16_t moduleNumberInString = 42u;
366  ((expectedStringNumber * BS_NR_OF_MODULES) + expectedModuleNumberInString),
367  &stringNumber,
368  &moduleNumberInString));
369 
370  TEST_ASSERT_EQUAL(expectedStringNumber, stringNumber);
371  TEST_ASSERT_EQUAL(expectedModuleNumberInString, moduleNumberInString);
372 }
373 
375  /* skip this test if the BMS configuration does not fit to this test (we need a second module) */
376  if ((BS_NR_OF_MODULES < 2u) || (BS_NR_OF_STRINGS < 2u)) {
377  TEST_PASS_MESSAGE("This test is skipped due to the configuration of the BMS.");
378  }
379  const uint8_t expectedStringNumber = 1u;
380  uint8_t stringNumber = 42u;
381  const uint16_t expectedModuleNumberInString = 1u;
382  uint16_t moduleNumberInString = 42u;
384  (expectedModuleNumberInString + (expectedStringNumber * BS_NR_OF_MODULES)),
385  &stringNumber,
386  &moduleNumberInString));
387 
388  TEST_ASSERT_EQUAL(expectedStringNumber, stringNumber);
389  TEST_ASSERT_EQUAL(expectedModuleNumberInString, moduleNumberInString);
390 }
#define BS_NR_OF_STRINGS
#define BS_NR_OF_MODULES
number of modules in battery pack
@ STD_OK
Definition: fstd_types.h:81
#define NULL_PTR
Null pointer.
Definition: fstd_types.h:75
#define MXM_BM_LSB
Monitoring Register LSB.
#define MXM_REG_STATUS1_ALRTINTRFC
ALRTINTRFC bit in STATUS1.
#define MXM_BM_MSB
Monitoring Register MSB.
#define MXM_REG_STATUS1_ALRTRST
ALRTRST bit in STATUS1.
#define MXM_REG_ADC_14BIT_VALUE
Monitoring Register 14bit ADC value.
void MXM_Unipolar14BitInto16Bit(uint16_t inputValue, uint8_t *lsb, uint8_t *msb)
convert a unipolar 14bit-value and shifts it into the 16bit-format
void MXM_ConvertModuleToString(const uint16_t moduleNumber, uint8_t *pStringNumber, uint16_t *pModuleNumberInString)
Get the string and module number from a global module number.
STD_RETURN_TYPE_e must_check_return MXM_ConvertTest(void)
Test the MXM_Convert()-function.
STD_RETURN_TYPE_e must_check_return MXM_FirstSetBitTest(void)
Test MXM_FirstSetBit().
uint16_t MXM_VoltageIntoUnipolar14Bit(uint16_t voltage_mV, uint16_t fullscaleReference_mV)
convert a voltage value into a unipolar 14bit value
STD_RETURN_TYPE_e must_check_return MXM_ExtractValueFromRegisterTest(void)
Test MXM_ExtractValueFromRegister().
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.
void MXM_ExtractValueFromRegister(uint8_t lsb, uint8_t msb, MXM_REG_BM bitmask, uint16_t *pValue)
Extract a value from a single register.
This is a collection of helper functions for the MAX1785x ICs.
MXM_CONVERSION_TYPE_e
@ MXM_CONVERSION_UNIPOLAR
#define MXM_MAXIMUM_NR_OF_MODULES
Maximum number of modules.
Helper for unit tests.
#define TEST_ASSERT_PASS_ASSERT(_code_under_test)
assert whether assert macro has passed
#define TEST_ASSERT_FAIL_ASSERT(_code_under_test)
assert whether assert macro has failed
void testMXM_ConvertInvalidConversionType(void)
void testConversionExtractAndInsertRoundTripFullScale(void)
void testMXM_ExtractValueFromRegisterLsbCompletelySet(void)
void testMXM_VoltageIntoUnipolar14Bit(void)
void testMXM_FirstSetBitTest(void)
void testMXM_ConvertModuleToStringTooManyForSystem(void)
void testMXM_ConvertSelfCheck(void)
void testMXM_ConvertModuleToStringSecondInFirstString(void)
void testScaleAndConvert2500(void)
void testMXM_ConvertModuleToStringSecondInSecondString(void)
void testMXM_ConvertModuleToStringFirstInFirstString(void)
void testMXM_ExtractValueFromRegisterLsbBitUnset(void)
void testMXM_ConvertModuleToStringFirstInSecondString(void)
void testMXM_ConvertHalfScale(void)
void testMXM_ExtractValueFromRegisterADCValue(void)
void testMXM_Unipolar14BitInto16BitWithInput0(void)
void testMXM_ExtractValueFromRegisterMsbCompletelySet(void)
void setUp(void)
void tearDown(void)
void testMXM_ExtractValueFromRegisterMsbBitSet(void)
void testConversionExtractAndInsertRoundTripNull(void)
void testMXM_ExtractValueFromRegisterLsbBitSet(void)
void testConversionExtractAndInsertRoundTripSomeValue(void)
void testScaleAndConvert500(void)
void testMXM_ConvertLowScale(void)
void testMXM_ConvertFullScale(void)
void testMXM_ExtractValueFromRegisterMsbBitSetFullBytes(void)
void testMXM_ExtractValueFromRegisterTest(void)
void testMXM_ConvertModuleToStringNullPointer(void)
void testMXM_Unipolar14BitInto16BitWithInput3FFF(void)
void testMXM_ExtractValueFromRegisterMsbBitUnset(void)
void testMXM_ConvertModuleToStringTooManyForMaxim(void)