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