blob: 1cf26996906131b7dd25ed9b80a448dd1f3901a6 [file] [log] [blame]
Kévin Redon69b92d92019-01-24 16:39:20 +01001/**
2 * \file
3 *
4 * \brief Different macros.
5 *
6 * Copyright (c) 2014-2018 Microchip Technology Inc. and its subsidiaries.
7 *
8 * \asf_license_start
9 *
10 * \page License
11 *
12 * Subject to your compliance with these terms, you may use Microchip
13 * software and any derivatives exclusively with Microchip products.
14 * It is your responsibility to comply with third party license terms applicable
15 * to your use of third party software (including open source software) that
16 * may accompany Microchip software.
17 *
18 * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES,
19 * WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE,
20 * INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY,
21 * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE
22 * LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL
23 * LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND WHATSOEVER RELATED TO THE
24 * SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS BEEN ADVISED OF THE
25 * POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE FULLEST EXTENT
26 * ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY
27 * RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY,
28 * THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
29 *
30 * \asf_license_stop
31 *
32 */
33
34#ifndef UTILS_H_INCLUDED
35#define UTILS_H_INCLUDED
36
37#ifdef __cplusplus
38extern "C" {
39#endif
40
41/**
42 * \addtogroup doc_driver_hal_utils_macro
43 *
44 * @{
45 */
46
47/**
48 * \brief Retrieve pointer to parent structure
49 */
50#define CONTAINER_OF(ptr, type, field_name) ((type *)(((uint8_t *)ptr) - offsetof(type, field_name)))
51
52/**
53 * \brief Retrieve array size
54 */
55#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
56
57/**
58 * \brief Emit the compiler pragma \a arg.
59 *
60 * \param[in] arg The pragma directive as it would appear after \e \#pragma
61 * (i.e. not stringified).
62 */
63#define COMPILER_PRAGMA(arg) _Pragma(#arg)
64
65/**
66 * \def COMPILER_PACK_SET(alignment)
67 * \brief Set maximum alignment for subsequent struct and union definitions to \a alignment.
68 */
69#define COMPILER_PACK_SET(alignment) COMPILER_PRAGMA(pack(alignment))
70
71/**
72 * \def COMPILER_PACK_RESET()
73 * \brief Set default alignment for subsequent struct and union definitions.
74 */
75#define COMPILER_PACK_RESET() COMPILER_PRAGMA(pack())
76
77/**
78 * \brief Set aligned boundary.
79 */
80#if defined __GNUC__
81#define COMPILER_ALIGNED(a) __attribute__((__aligned__(a)))
82#elif defined __ICCARM__
83#define COMPILER_ALIGNED(a) COMPILER_PRAGMA(data_alignment = a)
84#elif defined __CC_ARM
85#define COMPILER_ALIGNED(a) __attribute__((__aligned__(a)))
86#endif
87
88/**
89 * \brief Flash located data macros
90 */
91#if defined __GNUC__
92#define PROGMEM_DECLARE(type, name) const type name
93#define PROGMEM_T const
94#define PROGMEM_READ_BYTE(x) *((uint8_t *)(x))
95#define PROGMEM_PTR_T const *
96#define PROGMEM_STRING_T const uint8_t *
97#elif defined __ICCARM__
98#define PROGMEM_DECLARE(type, name) const type name
99#define PROGMEM_T const
100#define PROGMEM_READ_BYTE(x) *((uint8_t *)(x))
101#define PROGMEM_PTR_T const *
102#define PROGMEM_STRING_T const uint8_t *
103#elif defined __CC_ARM
104#define PROGMEM_DECLARE(type, name) const type name
105#define PROGMEM_T const
106#define PROGMEM_READ_BYTE(x) *((uint8_t *)(x))
107#define PROGMEM_PTR_T const *
108#define PROGMEM_STRING_T const uint8_t *
109#endif
110
111/**
112 * \brief Optimization
113 */
114#if defined __GNUC__
115#define OPTIMIZE_HIGH __attribute__((optimize(s)))
116#elif defined __CC_ARM
117#define OPTIMIZE_HIGH _Pragma("O3")
118#elif defined __ICCARM__
119#define OPTIMIZE_HIGH _Pragma("optimize=high")
120#endif
121
122/**
123 * \brief RAM located function attribute
124 */
125#if defined(__CC_ARM) /* Keil ?Vision 4 */
126#define RAMFUNC __attribute__((section(".ramfunc")))
127#elif defined(__ICCARM__) /* IAR Ewarm 5.41+ */
128#define RAMFUNC __ramfunc
129#elif defined(__GNUC__) /* GCC CS3 2009q3-68 */
130#define RAMFUNC __attribute__((section(".ramfunc")))
131#endif
132
133/**
134 * \brief No-init section.
135 * Place a data object or a function in a no-init section.
136 */
137#if defined(__CC_ARM)
138#define NO_INIT(a) __attribute__((zero_init))
139#elif defined(__ICCARM__)
140#define NO_INIT(a) __no_init
141#elif defined(__GNUC__)
142#define NO_INIT(a) __attribute__((section(".no_init")))
143#endif
144
145/**
146 * \brief Set user-defined section.
147 * Place a data object or a function in a user-defined section.
148 */
149#if defined(__CC_ARM)
150#define COMPILER_SECTION(a) __attribute__((__section__(a)))
151#elif defined(__ICCARM__)
152#define COMPILER_SECTION(a) COMPILER_PRAGMA(location = a)
153#elif defined(__GNUC__)
154#define COMPILER_SECTION(a) __attribute__((__section__(a)))
155#endif
156
157/**
158 * \brief Define WEAK attribute.
159 */
160#if defined(__CC_ARM) /* Keil ?Vision 4 */
161#define WEAK __attribute__((weak))
162#elif defined(__ICCARM__) /* IAR Ewarm 5.41+ */
163#define WEAK __weak
164#elif defined(__GNUC__) /* GCC CS3 2009q3-68 */
165#define WEAK __attribute__((weak))
166#endif
167
168/**
169 * \brief Pointer to function
170 */
171typedef void (*FUNC_PTR)(void);
172
173#define LE_BYTE0(a) ((uint8_t)(a))
174#define LE_BYTE1(a) ((uint8_t)((a) >> 8))
175#define LE_BYTE2(a) ((uint8_t)((a) >> 16))
176#define LE_BYTE3(a) ((uint8_t)((a) >> 24))
177
178#define LE_2_U16(p) ((p)[0] + ((p)[1] << 8))
179#define LE_2_U32(p) ((p)[0] + ((p)[1] << 8) + ((p)[2] << 16) + ((p)[3] << 24))
180
181/** \name Zero-Bit Counting
182 *
183 * Under GCC, __builtin_clz and __builtin_ctz behave like macros when
184 * applied to constant expressions (values known at compile time), so they are
185 * more optimized than the use of the corresponding assembly instructions and
186 * they can be used as constant expressions e.g. to initialize objects having
187 * static storage duration, and like the corresponding assembly instructions
188 * when applied to non-constant expressions (values unknown at compile time), so
189 * they are more optimized than an assembly periphrasis. Hence, clz and ctz
190 * ensure a possible and optimized behavior for both constant and non-constant
191 * expressions.
192 *
193 * @{ */
194
195/** \brief Counts the leading zero bits of the given value considered as a 32-bit integer.
196 *
197 * \param[in] u Value of which to count the leading zero bits.
198 *
199 * \return The count of leading zero bits in \a u.
200 */
201#if (defined __GNUC__) || (defined __CC_ARM)
202#define clz(u) __builtin_clz(u)
203#else
204#define clz(u) \
205 ( \
206 ((u) == 0) \
207 ? 32 \
208 : ((u) & (1ul << 31)) \
209 ? 0 \
210 : ((u) & (1ul << 30)) \
211 ? 1 \
212 : ((u) & (1ul << 29)) \
213 ? 2 \
214 : ((u) & (1ul << 28)) \
215 ? 3 \
216 : ((u) & (1ul << 27)) \
217 ? 4 \
218 : ((u) & (1ul << 26)) \
219 ? 5 \
220 : ((u) & (1ul << 25)) \
221 ? 6 \
222 : ((u) & (1ul << 24)) \
223 ? 7 \
224 : ((u) & (1ul << 23)) \
225 ? 8 \
226 : ((u) & (1ul << 22)) \
227 ? 9 \
228 : ((u) & (1ul << 21)) \
229 ? 10 \
230 : ((u) & (1ul << 20)) \
231 ? 11 \
232 : ((u) & (1ul << 19)) \
233 ? 12 \
234 : ((u) & (1ul << 18)) \
235 ? 13 \
236 : ((u) & (1ul << 17)) ? 14 \
237 : ((u) & (1ul << 16)) ? 15 \
238 : ((u) & (1ul << 15)) ? 16 \
239 : ((u) & (1ul << 14)) ? 17 \
240 : ((u) & (1ul << 13)) ? 18 \
241 : ((u) & (1ul << 12)) ? 19 \
242 : ((u) \
243 & (1ul \
244 << 11)) \
245 ? 20 \
246 : ((u) \
247 & (1ul \
248 << 10)) \
249 ? 21 \
250 : ((u) \
251 & (1ul \
252 << 9)) \
253 ? 22 \
254 : ((u) \
255 & (1ul \
256 << 8)) \
257 ? 23 \
258 : ((u) & (1ul << 7)) ? 24 \
259 : ((u) & (1ul << 6)) ? 25 \
260 : ((u) \
261 & (1ul \
262 << 5)) \
263 ? 26 \
264 : ((u) & (1ul << 4)) ? 27 \
265 : ((u) & (1ul << 3)) ? 28 \
266 : ((u) & (1ul << 2)) ? 29 \
267 : ( \
268 (u) & (1ul << 1)) \
269 ? 30 \
270 : 31)
271#endif
272
273/** \brief Counts the trailing zero bits of the given value considered as a 32-bit integer.
274 *
275 * \param[in] u Value of which to count the trailing zero bits.
276 *
277 * \return The count of trailing zero bits in \a u.
278 */
279#if (defined __GNUC__) || (defined __CC_ARM)
280#define ctz(u) __builtin_ctz(u)
281#else
282#define ctz(u) \
283 ( \
284 (u) & (1ul << 0) \
285 ? 0 \
286 : (u) & (1ul << 1) \
287 ? 1 \
288 : (u) & (1ul << 2) \
289 ? 2 \
290 : (u) & (1ul << 3) \
291 ? 3 \
292 : (u) & (1ul << 4) \
293 ? 4 \
294 : (u) & (1ul << 5) \
295 ? 5 \
296 : (u) & (1ul << 6) \
297 ? 6 \
298 : (u) & (1ul << 7) \
299 ? 7 \
300 : (u) & (1ul << 8) \
301 ? 8 \
302 : (u) & (1ul << 9) \
303 ? 9 \
304 : (u) & (1ul << 10) \
305 ? 10 \
306 : (u) & (1ul << 11) \
307 ? 11 \
308 : (u) & (1ul << 12) \
309 ? 12 \
310 : (u) & (1ul << 13) \
311 ? 13 \
312 : (u) & (1ul << 14) \
313 ? 14 \
314 : (u) & (1ul << 15) \
315 ? 15 \
316 : (u) & (1ul << 16) \
317 ? 16 \
318 : (u) & (1ul << 17) \
319 ? 17 \
320 : (u) & (1ul << 18) \
321 ? 18 \
322 : (u) & (1ul << 19) ? 19 \
323 : (u) & (1ul << 20) ? 20 \
324 : (u) & (1ul << 21) ? 21 \
325 : (u) & (1ul << 22) ? 22 \
326 : (u) & (1ul << 23) ? 23 \
327 : (u) & (1ul << 24) ? 24 \
328 : (u) & (1ul << 25) ? 25 \
329 : (u) & (1ul << 26) ? 26 \
330 : (u) & (1ul << 27) ? 27 \
331 : (u) & (1ul << 28) ? 28 : (u) & (1ul << 29) ? 29 : (u) & (1ul << 30) ? 30 : (u) & (1ul << 31) ? 31 : 32)
332#endif
333/** @} */
334
335/**
336 * \brief Counts the number of bits in a mask (no more than 32 bits)
337 * \param[in] mask Mask of which to count the bits.
338 */
339#define size_of_mask(mask) (32 - clz(mask) - ctz(mask))
340
341/**
342 * \brief Retrieve the start position of bits mask (no more than 32 bits)
343 * \param[in] mask Mask of which to retrieve the start position.
344 */
345#define pos_of_mask(mask) ctz(mask)
346
347/**
348 * \brief Return division result of a/b and round up the result to the closest
349 * number divisible by "b"
350 */
351#define round_up(a, b) (((a)-1) / (b) + 1)
352
353/**
354 * \brief Get the minimum of x and y
355 */
356#define min(x, y) ((x) > (y) ? (y) : (x))
357
358/**
359 * \brief Get the maximum of x and y
360 */
361#define max(x, y) ((x) > (y) ? (x) : (y))
362
363/**@}*/
364
365#ifdef __cplusplus
366}
367#endif
368#endif /* UTILS_H_INCLUDED */