aboutsummaryrefslogtreecommitdiffstats
path: root/hal/utils/include/utils.h
blob: 1cf26996906131b7dd25ed9b80a448dd1f3901a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/**
 * \file
 *
 * \brief Different macros.
 *
 * Copyright (c) 2014-2018 Microchip Technology Inc. and its subsidiaries.
 *
 * \asf_license_start
 *
 * \page License
 *
 * Subject to your compliance with these terms, you may use Microchip
 * software and any derivatives exclusively with Microchip products.
 * It is your responsibility to comply with third party license terms applicable
 * to your use of third party software (including open source software) that
 * may accompany Microchip software.
 *
 * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES,
 * WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE,
 * INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY,
 * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE
 * LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL
 * LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND WHATSOEVER RELATED TO THE
 * SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS BEEN ADVISED OF THE
 * POSSIBILITY OR THE DAMAGES ARE FORESEEABLE.  TO THE FULLEST EXTENT
 * ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY
 * RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY,
 * THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
 *
 * \asf_license_stop
 *
 */

#ifndef UTILS_H_INCLUDED
#define UTILS_H_INCLUDED

#ifdef __cplusplus
extern "C" {
#endif

/**
 * \addtogroup doc_driver_hal_utils_macro
 *
 * @{
 */

/**
 * \brief Retrieve pointer to parent structure
 */
#define CONTAINER_OF(ptr, type, field_name) ((type *)(((uint8_t *)ptr) - offsetof(type, field_name)))

/**
 * \brief Retrieve array size
 */
#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))

/**
 * \brief Emit the compiler pragma \a arg.
 *
 * \param[in] arg  The pragma directive as it would appear after \e \#pragma
 *             (i.e. not stringified).
 */
#define COMPILER_PRAGMA(arg) _Pragma(#arg)

/**
 * \def COMPILER_PACK_SET(alignment)
 * \brief Set maximum alignment for subsequent struct and union definitions to \a alignment.
 */
#define COMPILER_PACK_SET(alignment) COMPILER_PRAGMA(pack(alignment))

/**
 * \def COMPILER_PACK_RESET()
 * \brief Set default alignment for subsequent struct and union definitions.
 */
#define COMPILER_PACK_RESET() COMPILER_PRAGMA(pack())

/**
 * \brief Set aligned boundary.
 */
#if defined __GNUC__
#define COMPILER_ALIGNED(a) __attribute__((__aligned__(a)))
#elif defined __ICCARM__
#define COMPILER_ALIGNED(a) COMPILER_PRAGMA(data_alignment = a)
#elif defined __CC_ARM
#define COMPILER_ALIGNED(a) __attribute__((__aligned__(a)))
#endif

/**
 * \brief Flash located data macros
 */
#if defined __GNUC__
#define PROGMEM_DECLARE(type, name) const type name
#define PROGMEM_T const
#define PROGMEM_READ_BYTE(x) *((uint8_t *)(x))
#define PROGMEM_PTR_T const *
#define PROGMEM_STRING_T const uint8_t *
#elif defined __ICCARM__
#define PROGMEM_DECLARE(type, name) const type name
#define PROGMEM_T const
#define PROGMEM_READ_BYTE(x) *((uint8_t *)(x))
#define PROGMEM_PTR_T const *
#define PROGMEM_STRING_T const uint8_t *
#elif defined __CC_ARM
#define PROGMEM_DECLARE(type, name) const type name
#define PROGMEM_T const
#define PROGMEM_READ_BYTE(x) *((uint8_t *)(x))
#define PROGMEM_PTR_T const *
#define PROGMEM_STRING_T const uint8_t *
#endif

/**
 * \brief Optimization
 */
#if defined __GNUC__
#define OPTIMIZE_HIGH __attribute__((optimize(s)))
#elif defined __CC_ARM
#define OPTIMIZE_HIGH _Pragma("O3")
#elif defined __ICCARM__
#define OPTIMIZE_HIGH _Pragma("optimize=high")
#endif

/**
 * \brief RAM located function attribute
 */
#if defined(__CC_ARM) /* Keil ?Vision 4 */
#define RAMFUNC __attribute__((section(".ramfunc")))
#elif defined(__ICCARM__) /* IAR Ewarm 5.41+ */
#define RAMFUNC __ramfunc
#elif defined(__GNUC__) /* GCC CS3 2009q3-68 */
#define RAMFUNC __attribute__((section(".ramfunc")))
#endif

/**
 * \brief No-init section.
 * Place a data object or a function in a no-init section.
 */
#if defined(__CC_ARM)
#define NO_INIT(a) __attribute__((zero_init))
#elif defined(__ICCARM__)
#define NO_INIT(a) __no_init
#elif defined(__GNUC__)
#define NO_INIT(a) __attribute__((section(".no_init")))
#endif

/**
 * \brief Set user-defined section.
 * Place a data object or a function in a user-defined section.
 */
#if defined(__CC_ARM)
#define COMPILER_SECTION(a) __attribute__((__section__(a)))
#elif defined(__ICCARM__)
#define COMPILER_SECTION(a) COMPILER_PRAGMA(location = a)
#elif defined(__GNUC__)
#define COMPILER_SECTION(a) __attribute__((__section__(a)))
#endif

/**
 * \brief Define WEAK attribute.
 */
#if defined(__CC_ARM) /* Keil ?Vision 4 */
#define WEAK __attribute__((weak))
#elif defined(__ICCARM__) /* IAR Ewarm 5.41+ */
#define WEAK __weak
#elif defined(__GNUC__) /* GCC CS3 2009q3-68 */
#define WEAK __attribute__((weak))
#endif

/**
 * \brief Pointer to function
 */
typedef void (*FUNC_PTR)(void);

#define LE_BYTE0(a) ((uint8_t)(a))
#define LE_BYTE1(a) ((uint8_t)((a) >> 8))
#define LE_BYTE2(a) ((uint8_t)((a) >> 16))
#define LE_BYTE3(a) ((uint8_t)((a) >> 24))

#define LE_2_U16(p) ((p)[0] + ((p)[1] << 8))
#define LE_2_U32(p) ((p)[0] + ((p)[1] << 8) + ((p)[2] << 16) + ((p)[3] << 24))

/** \name Zero-Bit Counting
 *
 * Under GCC, __builtin_clz and __builtin_ctz behave like macros when
 * applied to constant expressions (values known at compile time), so they are
 * more optimized than the use of the corresponding assembly instructions and
 * they can be used as constant expressions e.g. to initialize objects having
 * static storage duration, and like the corresponding assembly instructions
 * when applied to non-constant expressions (values unknown at compile time), so
 * they are more optimized than an assembly periphrasis. Hence, clz and ctz
 * ensure a possible and optimized behavior for both constant and non-constant
 * expressions.
 *
 * @{ */

/** \brief Counts the leading zero bits of the given value considered as a 32-bit integer.
 *
 * \param[in] u Value of which to count the leading zero bits.
 *
 * \return The count of leading zero bits in \a u.
 */
#if (defined __GNUC__) || (defined __CC_ARM)
#define clz(u) __builtin_clz(u)
#else
#define clz(u)                                                                                                                                                                                                                                                                                                                                                                                     \
	(                                                                                                                                                                                                                                                                                                                                                                                              \
	    ((u) == 0)                                                                                                                                                                                                                                                                                                                                                                                 \
	        ? 32                                                                                                                                                                                                                                                                                                                                                                                   \
	        : ((u) & (1ul << 31))                                                                                                                                                                                                                                                                                                                                                                  \
	              ? 0                                                                                                                                                                                                                                                                                                                                                                              \
	              : ((u) & (1ul << 30))                                                                                                                                                                                                                                                                                                                                                            \
	                    ? 1                                                                                                                                                                                                                                                                                                                                                                        \
	                    : ((u) & (1ul << 29))                                                                                                                                                                                                                                                                                                                                                      \
	                          ? 2                                                                                                                                                                                                                                                                                                                                                                  \
	                          : ((u) & (1ul << 28))                                                                                                                                                                                                                                                                                                                                                \
	                                ? 3                                                                                                                                                                                                                                                                                                                                                            \
	                                : ((u) & (1ul << 27))                                                                                                                                                                                                                                                                                                                                          \
	                                      ? 4                                                                                                                                                                                                                                                                                                                                                      \
	                                      : ((u) & (1ul << 26))                                                                                                                                                                                                                                                                                                                                    \
	                                            ? 5                                                                                                                                                                                                                                                                                                                                                \
	                                            : ((u) & (1ul << 25))                                                                                                                                                                                                                                                                                                                              \
	                                                  ? 6                                                                                                                                                                                                                                                                                                                                          \
	                                                  : ((u) & (1ul << 24))                                                                                                                                                                                                                                                                                                                        \
	                                                        ? 7                                                                                                                                                                                                                                                                                                                                    \
	                                                        : ((u) & (1ul << 23))                                                                                                                                                                                                                                                                                                                  \
	                                                              ? 8                                                                                                                                                                                                                                                                                                                              \
	                                                              : ((u) & (1ul << 22))                                                                                                                                                                                                                                                                                                            \
	                                                                    ? 9                                                                                                                                                                                                                                                                                                                        \
	                                                                    : ((u) & (1ul << 21))                                                                                                                                                                                                                                                                                                      \
	                                                                          ? 10                                                                                                                                                                                                                                                                                                                 \
	                                                                          : ((u) & (1ul << 20))                                                                                                                                                                                                                                                                                                \
	                                                                                ? 11                                                                                                                                                                                                                                                                                                           \
	                                                                                : ((u) & (1ul << 19))                                                                                                                                                                                                                                                                                          \
	                                                                                      ? 12                                                                                                                                                                                                                                                                                                     \
	                                                                                      : ((u) & (1ul << 18))                                                                                                                                                                                                                                                                                    \
	                                                                                            ? 13                                                                                                                                                                                                                                                                                               \
	                                                                                            : ((u) & (1ul << 17)) ? 14                                                                                                                                                                                                                                                                         \
	                                                                                                                  : ((u) & (1ul << 16)) ? 15                                                                                                                                                                                                                                                   \
	                                                                                                                                        : ((u) & (1ul << 15)) ? 16                                                                                                                                                                                                                             \
	                                                                                                                                                              : ((u) & (1ul << 14)) ? 17                                                                                                                                                                                                       \
	                                                                                                                                                                                    : ((u) & (1ul << 13)) ? 18                                                                                                                                                                                 \
	                                                                                                                                                                                                          : ((u) & (1ul << 12)) ? 19                                                                                                                                                           \
	                                                                                                                                                                                                                                : ((u)                                                                                                                                                         \
	                                                                                                                                                                                                                                   & (1ul                                                                                                                                                      \
	                                                                                                                                                                                                                                      << 11))                                                                                                                                                  \
	                                                                                                                                                                                                                                      ? 20                                                                                                                                                     \
	                                                                                                                                                                                                                                      : ((u)                                                                                                                                                   \
	                                                                                                                                                                                                                                         & (1ul                                                                                                                                                \
	                                                                                                                                                                                                                                            << 10))                                                                                                                                            \
	                                                                                                                                                                                                                                            ? 21                                                                                                                                               \
	                                                                                                                                                                                                                                            : ((u)                                                                                                                                             \
	                                                                                                                                                                                                                                               & (1ul                                                                                                                                          \
	                                                                                                                                                                                                                                                  << 9))                                                                                                                                       \
	                                                                                                                                                                                                                                                  ? 22                                                                                                                                         \
	                                                                                                                                                                                                                                                  : ((u)                                                                                                                                       \
	                                                                                                                                                                                                                                                     & (1ul                                                                                                                                    \
	                                                                                                                                                                                                                                                        << 8))                                                                                                                                 \
	                                                                                                                                                                                                                                                        ? 23                                                                                                                                   \
	                                                                                                                                                                                                                                                        : ((u) & (1ul << 7)) ? 24                                                                                                              \
	                                                                                                                                                                                                                                                                             : ((u) & (1ul << 6)) ? 25                                                                                         \
	                                                                                                                                                                                                                                                                                                  : ((u)                                                                                       \
	                                                                                                                                                                                                                                                                                                     & (1ul                                                                                    \
	                                                                                                                                                                                                                                                                                                        << 5))                                                                                 \
	                                                                                                                                                                                                                                                                                                        ? 26                                                                                   \
	                                                                                                                                                                                                                                                                                                        : ((u) & (1ul << 4)) ? 27                                                              \
	                                                                                                                                                                                                                                                                                                                             : ((u) & (1ul << 3)) ? 28                                         \
	                                                                                                                                                                                                                                                                                                                                                  : ((u) & (1ul << 2)) ? 29                    \
	                                                                                                                                                                                                                                                                                                                                                                       : (                     \
	                                                                                                                                                                                                                                                                                                                                                                             (u) & (1ul << 1)) \
	                                                                                                                                                                                                                                                                                                                                                                             ? 30              \
	                                                                                                                                                                                                                                                                                                                                                                             : 31)
#endif

/** \brief Counts the trailing zero bits of the given value considered as a 32-bit integer.
 *
 * \param[in] u Value of which to count the trailing zero bits.
 *
 * \return The count of trailing zero bits in \a u.
 */
#if (defined __GNUC__) || (defined __CC_ARM)
#define ctz(u) __builtin_ctz(u)
#else
#define ctz(u)                                                                                                                                                                                                                                                                                                   \
	(                                                                                                                                                                                                                                                                                                            \
	    (u) & (1ul << 0)                                                                                                                                                                                                                                                                                         \
	        ? 0                                                                                                                                                                                                                                                                                                  \
	        : (u) & (1ul << 1)                                                                                                                                                                                                                                                                                   \
	              ? 1                                                                                                                                                                                                                                                                                            \
	              : (u) & (1ul << 2)                                                                                                                                                                                                                                                                             \
	                    ? 2                                                                                                                                                                                                                                                                                      \
	                    : (u) & (1ul << 3)                                                                                                                                                                                                                                                                       \
	                          ? 3                                                                                                                                                                                                                                                                                \
	                          : (u) & (1ul << 4)                                                                                                                                                                                                                                                                 \
	                                ? 4                                                                                                                                                                                                                                                                          \
	                                : (u) & (1ul << 5)                                                                                                                                                                                                                                                           \
	                                      ? 5                                                                                                                                                                                                                                                                    \
	                                      : (u) & (1ul << 6)                                                                                                                                                                                                                                                     \
	                                            ? 6                                                                                                                                                                                                                                                              \
	                                            : (u) & (1ul << 7)                                                                                                                                                                                                                                               \
	                                                  ? 7                                                                                                                                                                                                                                                        \
	                                                  : (u) & (1ul << 8)                                                                                                                                                                                                                                         \
	                                                        ? 8                                                                                                                                                                                                                                                  \
	                                                        : (u) & (1ul << 9)                                                                                                                                                                                                                                   \
	                                                              ? 9                                                                                                                                                                                                                                            \
	                                                              : (u) & (1ul << 10)                                                                                                                                                                                                                            \
	                                                                    ? 10                                                                                                                                                                                                                                     \
	                                                                    : (u) & (1ul << 11)                                                                                                                                                                                                                      \
	                                                                          ? 11                                                                                                                                                                                                                               \
	                                                                          : (u) & (1ul << 12)                                                                                                                                                                                                                \
	                                                                                ? 12                                                                                                                                                                                                                         \
	                                                                                : (u) & (1ul << 13)                                                                                                                                                                                                          \
	                                                                                      ? 13                                                                                                                                                                                                                   \
	                                                                                      : (u) & (1ul << 14)                                                                                                                                                                                                    \
	                                                                                            ? 14                                                                                                                                                                                                             \
	                                                                                            : (u) & (1ul << 15)                                                                                                                                                                                              \
	                                                                                                  ? 15                                                                                                                                                                                                       \
	                                                                                                  : (u) & (1ul << 16)                                                                                                                                                                                        \
	                                                                                                        ? 16                                                                                                                                                                                                 \
	                                                                                                        : (u) & (1ul << 17)                                                                                                                                                                                  \
	                                                                                                              ? 17                                                                                                                                                                                           \
	                                                                                                              : (u) & (1ul << 18)                                                                                                                                                                            \
	                                                                                                                    ? 18                                                                                                                                                                                     \
	                                                                                                                    : (u) & (1ul << 19) ? 19                                                                                                                                                                 \
	                                                                                                                                        : (u) & (1ul << 20) ? 20                                                                                                                                             \
	                                                                                                                                                            : (u) & (1ul << 21) ? 21                                                                                                                         \
	                                                                                                                                                                                : (u) & (1ul << 22) ? 22                                                                                                     \
	                                                                                                                                                                                                    : (u) & (1ul << 23) ? 23                                                                                 \
	                                                                                                                                                                                                                        : (u) & (1ul << 24) ? 24                                                             \
	                                                                                                                                                                                                                                            : (u) & (1ul << 25) ? 25                                         \
	                                                                                                                                                                                                                                                                : (u) & (1ul << 26) ? 26                     \
	                                                                                                                                                                                                                                                                                    : (u) & (1ul << 27) ? 27 \
	                                                                                                                                                                                                                                                                                                        : (u) & (1ul << 28) ? 28 : (u) & (1ul << 29) ? 29 : (u) & (1ul << 30) ? 30 : (u) & (1ul << 31) ? 31 : 32)
#endif
/** @} */

/**
 * \brief Counts the number of bits in a mask (no more than 32 bits)
 * \param[in] mask Mask of which to count the bits.
 */
#define size_of_mask(mask) (32 - clz(mask) - ctz(mask))

/**
 * \brief Retrieve the start position of bits mask (no more than 32 bits)
 * \param[in] mask Mask of which to retrieve the start position.
 */
#define pos_of_mask(mask) ctz(mask)

/**
 * \brief Return division result of a/b and round up the result to the closest
 *        number divisible by "b"
 */
#define round_up(a, b) (((a)-1) / (b) + 1)

/**
 * \brief Get the minimum of x and y
 */
#define min(x, y) ((x) > (y) ? (y) : (x))

/**
 * \brief Get the maximum of x and y
 */
#define max(x, y) ((x) > (y) ? (x) : (y))

/**@}*/

#ifdef __cplusplus
}
#endif
#endif /* UTILS_H_INCLUDED */