foxBMS - Unit Tests  1.4.0
The foxBMS Unit Tests API Documentation
general.h
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 general.h
44  * @author foxBMS Team
45  * @date 2019-09-24 (date of creation)
46  * @updated 2022-07-28 (date of last update)
47  * @version v1.4.0
48  * @ingroup GENERAL_CONF
49  * @prefix GEN
50  *
51  * @brief General macros and definitions for the whole platform.
52  *
53  */
54 
55 #ifndef FOXBMS__GENERAL_H_
56 #define FOXBMS__GENERAL_H_
57 
58 /*========== Includes =======================================================*/
59 #include "fassert.h"
60 #include "fstd_types.h"
61 #include "mcu.h"
62 
63 #include <stdbool.h>
64 #include <stdint.h>
65 
66 /*========== Macros and Definitions =========================================*/
67 
68 /**
69  * @brief sets a bit to 1u
70  * @param[in,out] register register on which to set the bit
71  * @param[in] bit number of the bit that should be set to 1u
72  */
73 #define GEN_SET_BIT(register, bit) ((register) |= (uint32)((uint32)1U << (bit)))
74 /**
75  * @brief clears a bit to 0u
76  * @param[in,out] register register on which to clear the bit
77  * @param[in] bit number of the bit that should be cleared to 0u
78  */
79 #define GEN_CLEAR_BIT(register, bit) ((register) &= ~(uint32)((uint32)1U << (bit)))
80 
81 /**
82  * @brief Allows functions to generate warnings in GCC for unused returns.
83  *
84  * This attribute allows to mark that a function return value must be used.
85  * The compiler will generate a warning when the return value of a function
86  * with this marker is not used in subsequent code.
87  */
88 #define GEN_MUST_CHECK_RETURN __attribute__((warn_unused_result))
89 
90 /** This attribute tells the compiler that the function should always be inlined */
91 #define GEN_ALWAYS_INLINE __attribute__((always_inline))
92 
93 /** allow implementations to be weak for unit tests */
94 #ifdef UNITY_UNIT_TEST
95 #define UNIT_TEST_WEAK_IMPL __attribute__((weak))
96 #else
97 #define UNIT_TEST_WEAK_IMPL
98 #endif
99 
100 /* assert that the basic datatypes in fstd_types.h are intact */
101 /* AXIVION Disable Style MisraC2012-10.4: These assertions have to check the actual values of the enums and defines. */
102 FAS_STATIC_ASSERT(false == 0, "false seems to have been modified.");
103 FAS_STATIC_ASSERT(true != false, "true seems to have been modified.");
104 FAS_STATIC_ASSERT(true == 1, "true seems to have been modified.");
105 
106 FAS_STATIC_ASSERT(STD_OK == 0, "STD_OK seems to have been modified.");
107 FAS_STATIC_ASSERT(STD_OK != STD_NOT_OK, "STD_OK or STD_NOT_OK seem to have been modified.");
108 FAS_STATIC_ASSERT(STD_NOT_OK == 1, "STD_NOT_OK seems to have been modified.");
109 /* AXIVION Enable Style MisraC2012-10.4: */
110 
111 /**
112  * Internal macros for the implementation of #GEN_REPEAT_U(). Do not use outside.
113  * @{
114  */
115 /* AXIVION Disable Style Generic-NoUnsafeMacro MisraC2012Directive-4.9: Due to the nature of these macros
116  it is impossible to wrap the REPEAT_Uxu(x) token in parentheses. With these
117  function-like Macros repeating a token is implemented. */
118 /* us lowercase 'u' suffix to emphasize the unsignedness of the 'GEN_REPEAT_U*u' macros. */
119 #define GEN_REPEAT_U1u(x) (x)
120 #define GEN_REPEAT_U2u(x) GEN_REPEAT_U1u(x), (x)
121 #define GEN_REPEAT_U3u(x) GEN_REPEAT_U2u(x), (x)
122 #define GEN_REPEAT_U4u(x) GEN_REPEAT_U3u(x), (x)
123 #define GEN_REPEAT_U5u(x) GEN_REPEAT_U4u(x), (x)
124 #define GEN_REPEAT_U6u(x) GEN_REPEAT_U5u(x), (x)
125 #define GEN_REPEAT_U7u(x) GEN_REPEAT_U6u(x), (x)
126 #define GEN_REPEAT_U8u(x) GEN_REPEAT_U7u(x), (x)
127 #define GEN_REPEAT_U9u(x) GEN_REPEAT_U8u(x), (x)
128 #define GEN_REPEAT_U10u(x) GEN_REPEAT_U9u(x), (x)
129 #define GEN_REPEAT_U11u(x) GEN_REPEAT_U10u(x), (x)
130 #define GEN_REPEAT_U12u(x) GEN_REPEAT_U11u(x), (x)
131 #define GEN_REPEAT_U13u(x) GEN_REPEAT_U12u(x), (x)
132 #define GEN_REPEAT_U14u(x) GEN_REPEAT_U13u(x), (x)
133 #define GEN_REPEAT_U15u(x) GEN_REPEAT_U14u(x), (x)
134 #define GEN_REPEAT_U16u(x) GEN_REPEAT_U15u(x), (x)
135 #define GEN_REPEAT_U17u(x) GEN_REPEAT_U16u(x), (x)
136 #define GEN_REPEAT_U18u(x) GEN_REPEAT_U17u(x), (x)
137 #define GEN_REPEAT_U19u(x) GEN_REPEAT_U18u(x), (x)
138 #define GEN_REPEAT_U20u(x) GEN_REPEAT_U19u(x), (x)
139 #define GEN_REPEAT_U21u(x) GEN_REPEAT_U20u(x), (x)
140 #define GEN_REPEAT_U22u(x) GEN_REPEAT_U21u(x), (x)
141 #define GEN_REPEAT_U23u(x) GEN_REPEAT_U22u(x), (x)
142 #define GEN_REPEAT_U24u(x) GEN_REPEAT_U23u(x), (x)
143 #define GEN_REPEAT_U25u(x) GEN_REPEAT_U24u(x), (x)
144 #define GEN_REPEAT_U26u(x) GEN_REPEAT_U25u(x), (x)
145 #define GEN_REPEAT_U27u(x) GEN_REPEAT_U26u(x), (x)
146 #define GEN_REPEAT_U28u(x) GEN_REPEAT_U27u(x), (x)
147 #define GEN_REPEAT_U29u(x) GEN_REPEAT_U28u(x), (x)
148 #define GEN_REPEAT_U30u(x) GEN_REPEAT_U29u(x), (x)
149 #define GEN_REPEAT_U31u(x) GEN_REPEAT_U30u(x), (x)
150 #define GEN_REPEAT_U32u(x) GEN_REPEAT_U31u(x), (x)
151 #define GEN_REPEAT_U33u(x) GEN_REPEAT_U32u(x), (x)
152 #define GEN_REPEAT_U34u(x) GEN_REPEAT_U33u(x), (x)
153 #define GEN_REPEAT_U35u(x) GEN_REPEAT_U34u(x), (x)
154 #define GEN_REPEAT_U36u(x) GEN_REPEAT_U35u(x), (x)
155 #define GEN_REPEAT_U37u(x) GEN_REPEAT_U36u(x), (x)
156 #define GEN_REPEAT_U38u(x) GEN_REPEAT_U37u(x), (x)
157 #define GEN_REPEAT_U39u(x) GEN_REPEAT_U38u(x), (x)
158 #define GEN_REPEAT_U40u(x) GEN_REPEAT_U39u(x), (x)
159 #define GEN_REPEAT_U41u(x) GEN_REPEAT_U40u(x), (x)
160 #define GEN_REPEAT_U42u(x) GEN_REPEAT_U41u(x), (x)
161 #define GEN_REPEAT_U43u(x) GEN_REPEAT_U42u(x), (x)
162 #define GEN_REPEAT_U44u(x) GEN_REPEAT_U43u(x), (x)
163 #define GEN_REPEAT_U45u(x) GEN_REPEAT_U44u(x), (x)
164 #define GEN_REPEAT_U46u(x) GEN_REPEAT_U45u(x), (x)
165 #define GEN_REPEAT_U47u(x) GEN_REPEAT_U46u(x), (x)
166 #define GEN_REPEAT_U48u(x) GEN_REPEAT_U47u(x), (x)
167 #define GEN_REPEAT_U49u(x) GEN_REPEAT_U48u(x), (x)
168 #define GEN_REPEAT_U50u(x) GEN_REPEAT_U49u(x), (x)
169 #define GEN_REPEAT_U51u(x) GEN_REPEAT_U50u(x), (x)
170 #define GEN_REPEAT_U52u(x) GEN_REPEAT_U51u(x), (x)
171 #define GEN_REPEAT_U53u(x) GEN_REPEAT_U52u(x), (x)
172 #define GEN_REPEAT_U54u(x) GEN_REPEAT_U53u(x), (x)
173 #define GEN_REPEAT_U55u(x) GEN_REPEAT_U54u(x), (x)
174 #define GEN_REPEAT_U56u(x) GEN_REPEAT_U55u(x), (x)
175 #define GEN_REPEAT_U57u(x) GEN_REPEAT_U56u(x), (x)
176 #define GEN_REPEAT_U58u(x) GEN_REPEAT_U57u(x), (x)
177 #define GEN_REPEAT_U59u(x) GEN_REPEAT_U58u(x), (x)
178 #define GEN_REPEAT_U60u(x) GEN_REPEAT_U59u(x), (x)
179 #define GEN_REPEAT_U61u(x) GEN_REPEAT_U60u(x), (x)
180 #define GEN_REPEAT_U62u(x) GEN_REPEAT_U61u(x), (x)
181 #define GEN_REPEAT_U63u(x) GEN_REPEAT_U62u(x), (x)
182 #define GEN_REPEAT_U64u(x) GEN_REPEAT_U63u(x), (x)
183 #define GEN_REPEAT_U65u(x) GEN_REPEAT_U64u(x), (x)
184 #define GEN_REPEAT_U66u(x) GEN_REPEAT_U65u(x), (x)
185 #define GEN_REPEAT_U67u(x) GEN_REPEAT_U66u(x), (x)
186 #define GEN_REPEAT_U68u(x) GEN_REPEAT_U67u(x), (x)
187 #define GEN_REPEAT_U69u(x) GEN_REPEAT_U68u(x), (x)
188 #define GEN_REPEAT_U70u(x) GEN_REPEAT_U69u(x), (x)
189 #define GEN_REPEAT_U71u(x) GEN_REPEAT_U70u(x), (x)
190 #define GEN_REPEAT_U72u(x) GEN_REPEAT_U71u(x), (x)
191 #define GEN_REPEAT_U73u(x) GEN_REPEAT_U72u(x), (x)
192 #define GEN_REPEAT_U74u(x) GEN_REPEAT_U73u(x), (x)
193 #define GEN_REPEAT_U75u(x) GEN_REPEAT_U74u(x), (x)
194 #define GEN_REPEAT_U76u(x) GEN_REPEAT_U75u(x), (x)
195 #define GEN_REPEAT_U77u(x) GEN_REPEAT_U76u(x), (x)
196 #define GEN_REPEAT_U78u(x) GEN_REPEAT_U77u(x), (x)
197 #define GEN_REPEAT_U79u(x) GEN_REPEAT_U78u(x), (x)
198 #define GEN_REPEAT_U80u(x) GEN_REPEAT_U79u(x), (x)
199 #define GEN_REPEAT_U81u(x) GEN_REPEAT_U80u(x), (x)
200 #define GEN_REPEAT_U82u(x) GEN_REPEAT_U81u(x), (x)
201 #define GEN_REPEAT_U83u(x) GEN_REPEAT_U82u(x), (x)
202 #define GEN_REPEAT_U84u(x) GEN_REPEAT_U83u(x), (x)
203 #define GEN_REPEAT_U85u(x) GEN_REPEAT_U84u(x), (x)
204 #define GEN_REPEAT_U86u(x) GEN_REPEAT_U85u(x), (x)
205 #define GEN_REPEAT_U87u(x) GEN_REPEAT_U86u(x), (x)
206 #define GEN_REPEAT_U88u(x) GEN_REPEAT_U87u(x), (x)
207 #define GEN_REPEAT_U89u(x) GEN_REPEAT_U88u(x), (x)
208 #define GEN_REPEAT_U90u(x) GEN_REPEAT_U89u(x), (x)
209 #define GEN_REPEAT_U91u(x) GEN_REPEAT_U90u(x), (x)
210 #define GEN_REPEAT_U92u(x) GEN_REPEAT_U91u(x), (x)
211 #define GEN_REPEAT_U93u(x) GEN_REPEAT_U92u(x), (x)
212 #define GEN_REPEAT_U94u(x) GEN_REPEAT_U93u(x), (x)
213 #define GEN_REPEAT_U95u(x) GEN_REPEAT_U94u(x), (x)
214 #define GEN_REPEAT_U96u(x) GEN_REPEAT_U95u(x), (x)
215 #define GEN_REPEAT_U97u(x) GEN_REPEAT_U96u(x), (x)
216 #define GEN_REPEAT_U98u(x) GEN_REPEAT_U97u(x), (x)
217 #define GEN_REPEAT_U99u(x) GEN_REPEAT_U98u(x), (x)
218 
219 /* AXIVION Disable Style MisraC2012-20.10: Usage allowed as long as remarks in documentation are honored. */
220 #define GEN_REPEAT_Ux(x, n) GEN_REPEAT_U##n(x)
221 /* AXIVION Enable Style Generic-NoUnsafeMacro MisraC2012Directive-4.9 MisraC2012-20.10: */
222 /**@}*/
223 
224 /** Maximum number of supported repetitions in #GEN_REPEAT_U(). Adapt if you change implementation.*/
225 #define GEN_REPEAT_MAXIMUM_REPETITIONS (99u)
226 
227 /**
228  * @brief Macro that helps to generate a series of literals (for array initializers).
229  * @details This macro generates a series of literals for array initializers.
230  * This can be used for initializing arrays to arbitrary non-null values
231  * when their size is defined with a macro. If the array shall be initialized
232  * to null the standard {0} should be used.
233  *
234  * @param x token that should be repeated, e.g. true
235  * @param n Times that it should be repeated (stripped of parenthesis with
236  * #GEN_STRIP() and described as unsigned integer literal) (maximum 16,
237  * #GEN_REPEAT_MAXIMUM_REPETITIONS, repetitions)
238  *
239  * Example usage:
240  \verbatim
241  #define ARRAY_SIZE (4u)
242  bool variable[ARRAY_SIZE] = {GEN_REPEAT_U(false, GEN_STRIP(ARRAY_SIZE))};
243  \endverbatim
244  * This will expand to:
245  \verbatim
246  bool variable[ARRAY_SIZE] = {false, false, false, false};
247  \endverbatim
248  */
249 /* AXIVION Next Codeline Style MisraC2012Directive-4.9: Function-like macro needed for this feature. */
250 #define GEN_REPEAT_U(x, n) GEN_REPEAT_Ux(x, n)
251 
252 /** Internal helper macros for #GEN_STRIP(). Do not use outside.
253  * @{
254  */
255 /* AXIVION Disable Style MisraC2012Directive-4.9 MisraC2012-20.7 Generic-NoUnsafeMacro CertC-PRE01: Function-like macro
256  needed for this feature. Parenthesis stripping is intended here. */
257 #define GEN_GET_ARGS(...) __VA_ARGS__
258 #define GEN_STRIP_PARENS(x) x
259 /**@}*/
260 /** Strips a token of its surrounding parenthesis. */
261 #define GEN_STRIP(x) GEN_STRIP_PARENS(GEN_GET_ARGS x)
262 /* AXIVION Enable Style MisraC2012Directive-4.9 MisraC2012-20.7 Generic-NoUnsafeMacro: */
263 
264 /** Defines the word size in bytes of the platform */
265 #if defined(__TI_COMPILER_VERSION__) && defined(__ARM_32BIT_STATE) && defined(__TMS470__)
266 #define GEN_BYTES_PER_WORD (4u)
267 #elif defined(UNITY_UNIT_TEST)
268 /* since this define only affects the task size, it can be safely set in unit
269  tests to the value that is used in the embedded platform */
270 #define GEN_BYTES_PER_WORD (4u)
271 #else
272 #warning "Unspecified platform default to 4 bytes per word."
273 #define GEN_BYTES_PER_WORD (4u)
274 #endif
275 
276 /*========== Extern Constant and Variable Declarations ======================*/
277 
278 /*========== Extern Function Prototypes =====================================*/
279 
280 /*========== Externalized Static Functions Prototypes (Unit Test) ===========*/
281 
282 #endif /* FOXBMS__GENERAL_H_ */
Assert macro implementation.
Definition of foxBMS standard types.
@ STD_NOT_OK
Definition: fstd_types.h:83
@ STD_OK
Definition: fstd_types.h:82
FAS_STATIC_ASSERT(false==0, "false seems to have been modified.")
Headers for the driver for the MCU module.