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