1 /** 2 * \file 3 * 4 * \brief Commonly used includes, types and macros. 5 * 6 * Copyright (c) 2010-2015 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 * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a> 45 */ 46 47 #ifndef UTILS_COMPILER_H 48 #define UTILS_COMPILER_H 49 50 /** 51 * \defgroup group_sam_utils Compiler abstraction layer and code utilities 52 * 53 * Compiler abstraction layer and code utilities for AT91SAM. 54 * This module provides various abstraction layers and utilities to make code compatible between different compilers. 55 * 56 * \{ 57 */ 58 #include <stddef.h> 59 60 #if (defined __ICCARM__) 61 # include <intrinsics.h> 62 #endif 63 64 #include <parts.h> 65 #include "preprocessor.h" 66 67 #include <io.h> 68 69 //_____ D E C L A R A T I O N S ____________________________________________ 70 71 #ifndef __ASSEMBLY__ // Not defined for assembling. 72 73 #include <stdio.h> 74 #include <stdbool.h> 75 #include <stdint.h> 76 #include <stdlib.h> 77 78 #ifdef __ICCARM__ 79 /*! \name Compiler Keywords 80 * 81 * Port of some keywords from GCC to IAR Embedded Workbench. 82 */ 83 //! @{ 84 #define __asm__ asm 85 #define __inline__ inline 86 #define __volatile__ 87 //! @} 88 89 #endif 90 91 #define FUNC_PTR void * 92 /** 93 * \def UNUSED 94 * \brief Marking \a v as a unused parameter or value. 95 */ 96 #define UNUSED(v) (void)(v) 97 98 /** 99 * \def unused 100 * \brief Marking \a v as a unused parameter or value. 101 */ 102 #define unused(v) do { (void)(v); } while(0) 103 104 /** 105 * \def barrier 106 * \brief Memory barrier 107 */ 108 #define barrier() __DMB() 109 110 /** 111 * \brief Emit the compiler pragma \a arg. 112 * 113 * \param arg The pragma directive as it would appear after \e \#pragma 114 * (i.e. not stringified). 115 */ 116 #define COMPILER_PRAGMA(arg) _Pragma(#arg) 117 118 /** 119 * \def COMPILER_PACK_SET(alignment) 120 * \brief Set maximum alignment for subsequent struct and union 121 * definitions to \a alignment. 122 */ 123 #define COMPILER_PACK_SET(alignment) COMPILER_PRAGMA(pack(alignment)) 124 125 /** 126 * \def COMPILER_PACK_RESET() 127 * \brief Set default alignment for subsequent struct and union 128 * definitions. 129 */ 130 #define COMPILER_PACK_RESET() COMPILER_PRAGMA(pack()) 131 132 133 /** 134 * \brief Set aligned boundary. 135 */ 136 #if (defined __GNUC__) || (defined __CC_ARM) 137 # define COMPILER_ALIGNED(a) __attribute__((__aligned__(a))) 138 #elif (defined __ICCARM__) 139 # define COMPILER_ALIGNED(a) COMPILER_PRAGMA(data_alignment = a) 140 #endif 141 142 /** 143 * \brief Set word-aligned boundary. 144 */ 145 #if (defined __GNUC__) || defined(__CC_ARM) 146 #define COMPILER_WORD_ALIGNED __attribute__((__aligned__(4))) 147 #elif (defined __ICCARM__) 148 #define COMPILER_WORD_ALIGNED COMPILER_PRAGMA(data_alignment = 4) 149 #endif 150 151 /** 152 * \def __always_inline 153 * \brief The function should always be inlined. 154 * 155 * This annotation instructs the compiler to ignore its inlining 156 * heuristics and inline the function no matter how big it thinks it 157 * becomes. 158 */ 159 160 // HACK BK: avoid warning 161 #ifdef __always_inline 162 # undef __always_inline 163 #endif 164 165 #if defined(__CC_ARM) 166 # define __always_inline __forceinline 167 #elif (defined __GNUC__) 168 # define __always_inline inline __attribute__((__always_inline__)) 169 #elif (defined __ICCARM__) 170 # define __always_inline _Pragma("inline=forced") 171 #endif 172 173 /** 174 * \def __no_inline 175 * \brief The function should not be inlined. 176 * 177 * This annotation instructs the compiler to ignore its inlining 178 * heuristics and not inline the function. 179 */ 180 #if defined(__CC_ARM) 181 # define __no_inline __attribute__((noinline)) 182 #elif (defined __GNUC__) 183 # define __no_inline __attribute__((__noinline__)) 184 #elif (defined __ICCARM__) 185 # define __no_inline _Pragma("inline=never") 186 #endif 187 188 /*! \brief This macro is used to test fatal errors. 189 * 190 * The macro tests if the expression is false. If it is, a fatal error is 191 * detected and the application hangs up. If TEST_SUITE_DEFINE_ASSERT_MACRO 192 * is defined, a unit test version of the macro is used, to allow execution 193 * of further tests after a false expression. 194 * 195 * \param expr Expression to evaluate and supposed to be nonzero. 196 */ 197 #if defined(_ASSERT_ENABLE_) 198 # if defined(TEST_SUITE_DEFINE_ASSERT_MACRO) 199 // Assert() is defined in unit_test/suite.h 200 # include "unit_test/suite.h" 201 # else 202 #undef TEST_SUITE_DEFINE_ASSERT_MACRO 203 # define Assert(expr) \ 204 {\ 205 if (!(expr)) while (true);\ 206 } 207 # endif 208 #else 209 # define Assert(expr) ((void) 0) 210 #endif 211 212 /* Define WEAK attribute */ 213 #if defined ( __CC_ARM ) /* Keil µVision 4 */ 214 # define WEAK __attribute__ ((weak)) 215 #elif defined ( __ICCARM__ ) /* IAR Ewarm 5.41+ */ 216 # define WEAK __weak 217 #elif defined ( __GNUC__ ) /* GCC CS3 2009q3-68 */ 218 # define WEAK __attribute__ ((weak)) 219 #endif 220 221 /* Define NO_INIT attribute */ 222 #if defined ( __CC_ARM ) 223 # define NO_INIT __attribute__((zero_init)) 224 #elif defined ( __ICCARM__ ) 225 # define NO_INIT __no_init 226 #elif defined ( __GNUC__ ) 227 # define NO_INIT __attribute__((section(".no_init"))) 228 #endif 229 230 /* Define RAMFUNC attribute */ 231 #if defined ( __CC_ARM ) /* Keil µVision 4 */ 232 # define RAMFUNC __attribute__ ((section(".ramfunc"))) 233 #elif defined ( __ICCARM__ ) /* IAR Ewarm 5.41+ */ 234 # define RAMFUNC __ramfunc 235 #elif defined ( __GNUC__ ) /* GCC CS3 2009q3-68 */ 236 # define RAMFUNC __attribute__ ((section(".ramfunc"))) 237 #endif 238 239 /* Define OPTIMIZE_HIGH attribute */ 240 #if defined ( __CC_ARM ) /* Keil µVision 4 */ 241 # define OPTIMIZE_HIGH _Pragma("O3") 242 #elif defined ( __ICCARM__ ) /* IAR Ewarm 5.41+ */ 243 # define OPTIMIZE_HIGH _Pragma("optimize=high") 244 #elif defined ( __GNUC__ ) /* GCC CS3 2009q3-68 */ 245 # define OPTIMIZE_HIGH __attribute__((optimize(s))) 246 #endif 247 248 #include "interrupt.h" 249 250 /*! \name Usual Types 251 */ 252 //! @{ 253 typedef unsigned char Bool; //!< Boolean. 254 #ifndef __cplusplus 255 #if !defined(__bool_true_false_are_defined) 256 typedef unsigned char bool; //!< Boolean. 257 #endif 258 #endif 259 typedef int8_t S8 ; //!< 8-bit signed integer. 260 typedef uint8_t U8 ; //!< 8-bit unsigned integer. 261 typedef int16_t S16; //!< 16-bit signed integer. 262 typedef uint16_t U16; //!< 16-bit unsigned integer. 263 typedef uint16_t le16_t; 264 typedef uint16_t be16_t; 265 typedef int32_t S32; //!< 32-bit signed integer. 266 typedef uint32_t U32; //!< 32-bit unsigned integer. 267 typedef uint32_t le32_t; 268 typedef uint32_t be32_t; 269 typedef int64_t S64; //!< 64-bit signed integer. 270 typedef uint64_t U64; //!< 64-bit unsigned integer. 271 typedef float F32; //!< 32-bit floating-point number. 272 typedef double F64; //!< 64-bit floating-point number. 273 typedef uint32_t iram_size_t; 274 //! @} 275 276 277 /*! \name Status Types 278 */ 279 //! @{ 280 typedef bool Status_bool_t; //!< Boolean status. 281 typedef U8 Status_t; //!< 8-bit-coded status. 282 //! @} 283 284 285 /*! \name Aliasing Aggregate Types 286 */ 287 //! @{ 288 289 //! 16-bit union. 290 typedef union 291 { 292 S16 s16 ; 293 U16 u16 ; 294 S8 s8 [2]; 295 U8 u8 [2]; 296 } Union16; 297 298 //! 32-bit union. 299 typedef union 300 { 301 S32 s32 ; 302 U32 u32 ; 303 S16 s16[2]; 304 U16 u16[2]; 305 S8 s8 [4]; 306 U8 u8 [4]; 307 } Union32; 308 309 //! 64-bit union. 310 typedef union 311 { 312 S64 s64 ; 313 U64 u64 ; 314 S32 s32[2]; 315 U32 u32[2]; 316 S16 s16[4]; 317 U16 u16[4]; 318 S8 s8 [8]; 319 U8 u8 [8]; 320 } Union64; 321 322 //! Union of pointers to 64-, 32-, 16- and 8-bit unsigned integers. 323 typedef union 324 { 325 S64 *s64ptr; 326 U64 *u64ptr; 327 S32 *s32ptr; 328 U32 *u32ptr; 329 S16 *s16ptr; 330 U16 *u16ptr; 331 S8 *s8ptr ; 332 U8 *u8ptr ; 333 } UnionPtr; 334 335 //! Union of pointers to volatile 64-, 32-, 16- and 8-bit unsigned integers. 336 typedef union 337 { 338 volatile S64 *s64ptr; 339 volatile U64 *u64ptr; 340 volatile S32 *s32ptr; 341 volatile U32 *u32ptr; 342 volatile S16 *s16ptr; 343 volatile U16 *u16ptr; 344 volatile S8 *s8ptr ; 345 volatile U8 *u8ptr ; 346 } UnionVPtr; 347 348 //! Union of pointers to constant 64-, 32-, 16- and 8-bit unsigned integers. 349 typedef union 350 { 351 const S64 *s64ptr; 352 const U64 *u64ptr; 353 const S32 *s32ptr; 354 const U32 *u32ptr; 355 const S16 *s16ptr; 356 const U16 *u16ptr; 357 const S8 *s8ptr ; 358 const U8 *u8ptr ; 359 } UnionCPtr; 360 361 //! Union of pointers to constant volatile 64-, 32-, 16- and 8-bit unsigned integers. 362 typedef union 363 { 364 const volatile S64 *s64ptr; 365 const volatile U64 *u64ptr; 366 const volatile S32 *s32ptr; 367 const volatile U32 *u32ptr; 368 const volatile S16 *s16ptr; 369 const volatile U16 *u16ptr; 370 const volatile S8 *s8ptr ; 371 const volatile U8 *u8ptr ; 372 } UnionCVPtr; 373 374 //! Structure of pointers to 64-, 32-, 16- and 8-bit unsigned integers. 375 typedef struct 376 { 377 S64 *s64ptr; 378 U64 *u64ptr; 379 S32 *s32ptr; 380 U32 *u32ptr; 381 S16 *s16ptr; 382 U16 *u16ptr; 383 S8 *s8ptr ; 384 U8 *u8ptr ; 385 } StructPtr; 386 387 //! Structure of pointers to volatile 64-, 32-, 16- and 8-bit unsigned integers. 388 typedef struct 389 { 390 volatile S64 *s64ptr; 391 volatile U64 *u64ptr; 392 volatile S32 *s32ptr; 393 volatile U32 *u32ptr; 394 volatile S16 *s16ptr; 395 volatile U16 *u16ptr; 396 volatile S8 *s8ptr ; 397 volatile U8 *u8ptr ; 398 } StructVPtr; 399 400 //! Structure of pointers to constant 64-, 32-, 16- and 8-bit unsigned integers. 401 typedef struct 402 { 403 const S64 *s64ptr; 404 const U64 *u64ptr; 405 const S32 *s32ptr; 406 const U32 *u32ptr; 407 const S16 *s16ptr; 408 const U16 *u16ptr; 409 const S8 *s8ptr ; 410 const U8 *u8ptr ; 411 } StructCPtr; 412 413 //! Structure of pointers to constant volatile 64-, 32-, 16- and 8-bit unsigned integers. 414 typedef struct 415 { 416 const volatile S64 *s64ptr; 417 const volatile U64 *u64ptr; 418 const volatile S32 *s32ptr; 419 const volatile U32 *u32ptr; 420 const volatile S16 *s16ptr; 421 const volatile U16 *u16ptr; 422 const volatile S8 *s8ptr ; 423 const volatile U8 *u8ptr ; 424 } StructCVPtr; 425 426 //! @} 427 428 #endif // #ifndef __ASSEMBLY__ 429 430 /*! \name Usual Constants 431 */ 432 //! @{ 433 #define DISABLE 0 434 #define ENABLE 1 435 #ifndef __cplusplus 436 #if !defined(__bool_true_false_are_defined) 437 #define false 0 438 #define true 1 439 #endif 440 #endif 441 #define PASS 0 442 #define FAIL 1 443 #define LOW 0 444 #define HIGH 1 445 //! @} 446 447 448 #ifndef __ASSEMBLY__ // not for assembling. 449 450 //! \name Optimization Control 451 //@{ 452 453 /** 454 * \def likely(exp) 455 * \brief The expression \a exp is likely to be true 456 */ 457 #ifndef likely 458 # define likely(exp) (exp) 459 #endif 460 461 /** 462 * \def unlikely(exp) 463 * \brief The expression \a exp is unlikely to be true 464 */ 465 #ifndef unlikely 466 # define unlikely(exp) (exp) 467 #endif 468 469 /** 470 * \def is_constant(exp) 471 * \brief Determine if an expression evaluates to a constant value. 472 * 473 * \param exp Any expression 474 * 475 * \return true if \a exp is constant, false otherwise. 476 */ 477 #if (defined __GNUC__) || (defined __CC_ARM) 478 # define is_constant(exp) __builtin_constant_p(exp) 479 #else 480 # define is_constant(exp) (0) 481 #endif 482 483 //! @} 484 485 /*! \name Bit-Field Handling 486 */ 487 //! @{ 488 489 /*! \brief Reads the bits of a value specified by a given bit-mask. 490 * 491 * \param value Value to read bits from. 492 * \param mask Bit-mask indicating bits to read. 493 * 494 * \return Read bits. 495 */ 496 #define Rd_bits( value, mask) ((value) & (mask)) 497 498 /*! \brief Writes the bits of a C lvalue specified by a given bit-mask. 499 * 500 * \param lvalue C lvalue to write bits to. 501 * \param mask Bit-mask indicating bits to write. 502 * \param bits Bits to write. 503 * 504 * \return Resulting value with written bits. 505 */ 506 #define Wr_bits(lvalue, mask, bits) ((lvalue) = ((lvalue) & ~(mask)) |\ 507 ((bits ) & (mask))) 508 509 /*! \brief Tests the bits of a value specified by a given bit-mask. 510 * 511 * \param value Value of which to test bits. 512 * \param mask Bit-mask indicating bits to test. 513 * 514 * \return \c 1 if at least one of the tested bits is set, else \c 0. 515 */ 516 #define Tst_bits( value, mask) (Rd_bits(value, mask) != 0) 517 518 /*! \brief Clears the bits of a C lvalue specified by a given bit-mask. 519 * 520 * \param lvalue C lvalue of which to clear bits. 521 * \param mask Bit-mask indicating bits to clear. 522 * 523 * \return Resulting value with cleared bits. 524 */ 525 #define Clr_bits(lvalue, mask) ((lvalue) &= ~(mask)) 526 527 /*! \brief Sets the bits of a C lvalue specified by a given bit-mask. 528 * 529 * \param lvalue C lvalue of which to set bits. 530 * \param mask Bit-mask indicating bits to set. 531 * 532 * \return Resulting value with set bits. 533 */ 534 #define Set_bits(lvalue, mask) ((lvalue) |= (mask)) 535 536 /*! \brief Toggles the bits of a C lvalue specified by a given bit-mask. 537 * 538 * \param lvalue C lvalue of which to toggle bits. 539 * \param mask Bit-mask indicating bits to toggle. 540 * 541 * \return Resulting value with toggled bits. 542 */ 543 #define Tgl_bits(lvalue, mask) ((lvalue) ^= (mask)) 544 545 /*! \brief Reads the bit-field of a value specified by a given bit-mask. 546 * 547 * \param value Value to read a bit-field from. 548 * \param mask Bit-mask indicating the bit-field to read. 549 * 550 * \return Read bit-field. 551 */ 552 #define Rd_bitfield( value, mask) (Rd_bits( value, mask) >> ctz(mask)) 553 554 /*! \brief Writes the bit-field of a C lvalue specified by a given bit-mask. 555 * 556 * \param lvalue C lvalue to write a bit-field to. 557 * \param mask Bit-mask indicating the bit-field to write. 558 * \param bitfield Bit-field to write. 559 * 560 * \return Resulting value with written bit-field. 561 */ 562 #define Wr_bitfield(lvalue, mask, bitfield) (Wr_bits(lvalue, mask, (U32)(bitfield) << ctz(mask))) 563 564 //! @} 565 566 567 /*! \name Zero-Bit Counting 568 * 569 * Under GCC, __builtin_clz and __builtin_ctz behave like macros when 570 * applied to constant expressions (values known at compile time), so they are 571 * more optimized than the use of the corresponding assembly instructions and 572 * they can be used as constant expressions e.g. to initialize objects having 573 * static storage duration, and like the corresponding assembly instructions 574 * when applied to non-constant expressions (values unknown at compile time), so 575 * they are more optimized than an assembly periphrasis. Hence, clz and ctz 576 * ensure a possible and optimized behavior for both constant and non-constant 577 * expressions. 578 */ 579 //! @{ 580 581 /*! \brief Counts the leading zero bits of the given value considered as a 32-bit integer. 582 * 583 * \param u Value of which to count the leading zero bits. 584 * 585 * \return The count of leading zero bits in \a u. 586 */ 587 #if (defined __GNUC__) || (defined __CC_ARM) 588 # define clz(u) __builtin_clz(u) 589 #elif (defined __ICCARM__) 590 # define clz(u) __CLZ(u) 591 #else 592 # define clz(u) (((u) == 0) ? 32 : \ 593 ((u) & (1ul << 31)) ? 0 : \ 594 ((u) & (1ul << 30)) ? 1 : \ 595 ((u) & (1ul << 29)) ? 2 : \ 596 ((u) & (1ul << 28)) ? 3 : \ 597 ((u) & (1ul << 27)) ? 4 : \ 598 ((u) & (1ul << 26)) ? 5 : \ 599 ((u) & (1ul << 25)) ? 6 : \ 600 ((u) & (1ul << 24)) ? 7 : \ 601 ((u) & (1ul << 23)) ? 8 : \ 602 ((u) & (1ul << 22)) ? 9 : \ 603 ((u) & (1ul << 21)) ? 10 : \ 604 ((u) & (1ul << 20)) ? 11 : \ 605 ((u) & (1ul << 19)) ? 12 : \ 606 ((u) & (1ul << 18)) ? 13 : \ 607 ((u) & (1ul << 17)) ? 14 : \ 608 ((u) & (1ul << 16)) ? 15 : \ 609 ((u) & (1ul << 15)) ? 16 : \ 610 ((u) & (1ul << 14)) ? 17 : \ 611 ((u) & (1ul << 13)) ? 18 : \ 612 ((u) & (1ul << 12)) ? 19 : \ 613 ((u) & (1ul << 11)) ? 20 : \ 614 ((u) & (1ul << 10)) ? 21 : \ 615 ((u) & (1ul << 9)) ? 22 : \ 616 ((u) & (1ul << 8)) ? 23 : \ 617 ((u) & (1ul << 7)) ? 24 : \ 618 ((u) & (1ul << 6)) ? 25 : \ 619 ((u) & (1ul << 5)) ? 26 : \ 620 ((u) & (1ul << 4)) ? 27 : \ 621 ((u) & (1ul << 3)) ? 28 : \ 622 ((u) & (1ul << 2)) ? 29 : \ 623 ((u) & (1ul << 1)) ? 30 : \ 624 31) 625 #endif 626 627 /*! \brief Counts the trailing zero bits of the given value considered as a 32-bit integer. 628 * 629 * \param u Value of which to count the trailing zero bits. 630 * 631 * \return The count of trailing zero bits in \a u. 632 */ 633 #if (defined __GNUC__) || (defined __CC_ARM) 634 # define ctz(u) __builtin_ctz(u) 635 #else 636 # define ctz(u) ((u) & (1ul << 0) ? 0 : \ 637 (u) & (1ul << 1) ? 1 : \ 638 (u) & (1ul << 2) ? 2 : \ 639 (u) & (1ul << 3) ? 3 : \ 640 (u) & (1ul << 4) ? 4 : \ 641 (u) & (1ul << 5) ? 5 : \ 642 (u) & (1ul << 6) ? 6 : \ 643 (u) & (1ul << 7) ? 7 : \ 644 (u) & (1ul << 8) ? 8 : \ 645 (u) & (1ul << 9) ? 9 : \ 646 (u) & (1ul << 10) ? 10 : \ 647 (u) & (1ul << 11) ? 11 : \ 648 (u) & (1ul << 12) ? 12 : \ 649 (u) & (1ul << 13) ? 13 : \ 650 (u) & (1ul << 14) ? 14 : \ 651 (u) & (1ul << 15) ? 15 : \ 652 (u) & (1ul << 16) ? 16 : \ 653 (u) & (1ul << 17) ? 17 : \ 654 (u) & (1ul << 18) ? 18 : \ 655 (u) & (1ul << 19) ? 19 : \ 656 (u) & (1ul << 20) ? 20 : \ 657 (u) & (1ul << 21) ? 21 : \ 658 (u) & (1ul << 22) ? 22 : \ 659 (u) & (1ul << 23) ? 23 : \ 660 (u) & (1ul << 24) ? 24 : \ 661 (u) & (1ul << 25) ? 25 : \ 662 (u) & (1ul << 26) ? 26 : \ 663 (u) & (1ul << 27) ? 27 : \ 664 (u) & (1ul << 28) ? 28 : \ 665 (u) & (1ul << 29) ? 29 : \ 666 (u) & (1ul << 30) ? 30 : \ 667 (u) & (1ul << 31) ? 31 : \ 668 32) 669 #endif 670 671 //! @} 672 673 674 /*! \name Bit Reversing 675 */ 676 //! @{ 677 678 /*! \brief Reverses the bits of \a u8. 679 * 680 * \param u8 U8 of which to reverse the bits. 681 * 682 * \return Value resulting from \a u8 with reversed bits. 683 */ 684 #define bit_reverse8(u8) ((U8)(bit_reverse32((U8)(u8)) >> 24)) 685 686 /*! \brief Reverses the bits of \a u16. 687 * 688 * \param u16 U16 of which to reverse the bits. 689 * 690 * \return Value resulting from \a u16 with reversed bits. 691 */ 692 #define bit_reverse16(u16) ((U16)(bit_reverse32((U16)(u16)) >> 16)) 693 694 /*! \brief Reverses the bits of \a u32. 695 * 696 * \param u32 U32 of which to reverse the bits. 697 * 698 * \return Value resulting from \a u32 with reversed bits. 699 */ 700 #define bit_reverse32(u32) __RBIT(u32) 701 702 /*! \brief Reverses the bits of \a u64. 703 * 704 * \param u64 U64 of which to reverse the bits. 705 * 706 * \return Value resulting from \a u64 with reversed bits. 707 */ 708 #define bit_reverse64(u64) ((U64)(((U64)bit_reverse32((U64)(u64) >> 32)) |\ 709 ((U64)bit_reverse32((U64)(u64)) << 32))) 710 711 //! @} 712 713 714 /*! \name Alignment 715 */ 716 //! @{ 717 718 /*! \brief Tests alignment of the number \a val with the \a n boundary. 719 * 720 * \param val Input value. 721 * \param n Boundary. 722 * 723 * \return \c 1 if the number \a val is aligned with the \a n boundary, else \c 0. 724 */ 725 #define Test_align(val, n ) (!Tst_bits( val, (n) - 1 ) ) 726 727 /*! \brief Gets alignment of the number \a val with respect to the \a n boundary. 728 * 729 * \param val Input value. 730 * \param n Boundary. 731 * 732 * \return Alignment of the number \a val with respect to the \a n boundary. 733 */ 734 #define Get_align( val, n ) ( Rd_bits( val, (n) - 1 ) ) 735 736 /*! \brief Sets alignment of the lvalue number \a lval to \a alg with respect to the \a n boundary. 737 * 738 * \param lval Input/output lvalue. 739 * \param n Boundary. 740 * \param alg Alignment. 741 * 742 * \return New value of \a lval resulting from its alignment set to \a alg with respect to the \a n boundary. 743 */ 744 #define Set_align(lval, n, alg) ( Wr_bits(lval, (n) - 1, alg) ) 745 746 /*! \brief Aligns the number \a val with the upper \a n boundary. 747 * 748 * \param val Input value. 749 * \param n Boundary. 750 * 751 * \return Value resulting from the number \a val aligned with the upper \a n boundary. 752 */ 753 #define Align_up( val, n ) (((val) + ((n) - 1)) & ~((n) - 1)) 754 755 /*! \brief Aligns the number \a val with the lower \a n boundary. 756 * 757 * \param val Input value. 758 * \param n Boundary. 759 * 760 * \return Value resulting from the number \a val aligned with the lower \a n boundary. 761 */ 762 #define Align_down(val, n ) ( (val) & ~((n) - 1)) 763 764 //! @} 765 766 767 /*! \name Mathematics 768 * 769 * The same considerations as for clz and ctz apply here but GCC does not 770 * provide built-in functions to access the assembly instructions abs, min and 771 * max and it does not produce them by itself in most cases, so two sets of 772 * macros are defined here: 773 * - Abs, Min and Max to apply to constant expressions (values known at 774 * compile time); 775 * - abs, min and max to apply to non-constant expressions (values unknown at 776 * compile time), abs is found in stdlib.h. 777 */ 778 //! @{ 779 780 /*! \brief Takes the absolute value of \a a. 781 * 782 * \param a Input value. 783 * 784 * \return Absolute value of \a a. 785 * 786 * \note More optimized if only used with values known at compile time. 787 */ 788 #define Abs(a) (((a) < 0 ) ? -(a) : (a)) 789 790 /*! \brief Takes the minimal value of \a a and \a b. 791 * 792 * \param a Input value. 793 * \param b Input value. 794 * 795 * \return Minimal value of \a a and \a b. 796 * 797 * \note More optimized if only used with values known at compile time. 798 */ 799 #define Min(a, b) (((a) < (b)) ? (a) : (b)) 800 801 /*! \brief Takes the maximal value of \a a and \a b. 802 * 803 * \param a Input value. 804 * \param b Input value. 805 * 806 * \return Maximal value of \a a and \a b. 807 * 808 * \note More optimized if only used with values known at compile time. 809 */ 810 #define Max(a, b) (((a) > (b)) ? (a) : (b)) 811 812 // abs() is already defined by stdlib.h 813 814 /*! \brief Takes the minimal value of \a a and \a b. 815 * 816 * \param a Input value. 817 * \param b Input value. 818 * 819 * \return Minimal value of \a a and \a b. 820 * 821 * \note More optimized if only used with values unknown at compile time. 822 */ 823 #define min(a, b) Min(a, b) 824 825 /*! \brief Takes the maximal value of \a a and \a b. 826 * 827 * \param a Input value. 828 * \param b Input value. 829 * 830 * \return Maximal value of \a a and \a b. 831 * 832 * \note More optimized if only used with values unknown at compile time. 833 */ 834 #define max(a, b) Max(a, b) 835 836 //! @} 837 838 839 /*! \brief Calls the routine at address \a addr. 840 * 841 * It generates a long call opcode. 842 * 843 * For example, `Long_call(0x80000000)' generates a software reset on a UC3 if 844 * it is invoked from the CPU supervisor mode. 845 * 846 * \param addr Address of the routine to call. 847 * 848 * \note It may be used as a long jump opcode in some special cases. 849 */ 850 #define Long_call(addr) ((*(void (*)(void))(addr))()) 851 852 853 /*! \name MCU Endianism Handling 854 * ARM is MCU little endianism. 855 */ 856 //! @{ 857 #define MSB(u16) (((U8 *)&(u16))[1]) //!< Most significant byte of \a u16. 858 #define LSB(u16) (((U8 *)&(u16))[0]) //!< Least significant byte of \a u16. 859 860 #define MSH(u32) (((U16 *)&(u32))[1]) //!< Most significant half-word of \a u32. 861 #define LSH(u32) (((U16 *)&(u32))[0]) //!< Least significant half-word of \a u32. 862 #define MSB0W(u32) (((U8 *)&(u32))[3]) //!< Most significant byte of 1st rank of \a u32. 863 #define MSB1W(u32) (((U8 *)&(u32))[2]) //!< Most significant byte of 2nd rank of \a u32. 864 #define MSB2W(u32) (((U8 *)&(u32))[1]) //!< Most significant byte of 3rd rank of \a u32. 865 #define MSB3W(u32) (((U8 *)&(u32))[0]) //!< Most significant byte of 4th rank of \a u32. 866 #define LSB3W(u32) MSB0W(u32) //!< Least significant byte of 4th rank of \a u32. 867 #define LSB2W(u32) MSB1W(u32) //!< Least significant byte of 3rd rank of \a u32. 868 #define LSB1W(u32) MSB2W(u32) //!< Least significant byte of 2nd rank of \a u32. 869 #define LSB0W(u32) MSB3W(u32) //!< Least significant byte of 1st rank of \a u32. 870 871 #define MSW(u64) (((U32 *)&(u64))[1]) //!< Most significant word of \a u64. 872 #define LSW(u64) (((U32 *)&(u64))[0]) //!< Least significant word of \a u64. 873 #define MSH0(u64) (((U16 *)&(u64))[3]) //!< Most significant half-word of 1st rank of \a u64. 874 #define MSH1(u64) (((U16 *)&(u64))[2]) //!< Most significant half-word of 2nd rank of \a u64. 875 #define MSH2(u64) (((U16 *)&(u64))[1]) //!< Most significant half-word of 3rd rank of \a u64. 876 #define MSH3(u64) (((U16 *)&(u64))[0]) //!< Most significant half-word of 4th rank of \a u64. 877 #define LSH3(u64) MSH0(u64) //!< Least significant half-word of 4th rank of \a u64. 878 #define LSH2(u64) MSH1(u64) //!< Least significant half-word of 3rd rank of \a u64. 879 #define LSH1(u64) MSH2(u64) //!< Least significant half-word of 2nd rank of \a u64. 880 #define LSH0(u64) MSH3(u64) //!< Least significant half-word of 1st rank of \a u64. 881 #define MSB0D(u64) (((U8 *)&(u64))[7]) //!< Most significant byte of 1st rank of \a u64. 882 #define MSB1D(u64) (((U8 *)&(u64))[6]) //!< Most significant byte of 2nd rank of \a u64. 883 #define MSB2D(u64) (((U8 *)&(u64))[5]) //!< Most significant byte of 3rd rank of \a u64. 884 #define MSB3D(u64) (((U8 *)&(u64))[4]) //!< Most significant byte of 4th rank of \a u64. 885 #define MSB4D(u64) (((U8 *)&(u64))[3]) //!< Most significant byte of 5th rank of \a u64. 886 #define MSB5D(u64) (((U8 *)&(u64))[2]) //!< Most significant byte of 6th rank of \a u64. 887 #define MSB6D(u64) (((U8 *)&(u64))[1]) //!< Most significant byte of 7th rank of \a u64. 888 #define MSB7D(u64) (((U8 *)&(u64))[0]) //!< Most significant byte of 8th rank of \a u64. 889 #define LSB7D(u64) MSB0D(u64) //!< Least significant byte of 8th rank of \a u64. 890 #define LSB6D(u64) MSB1D(u64) //!< Least significant byte of 7th rank of \a u64. 891 #define LSB5D(u64) MSB2D(u64) //!< Least significant byte of 6th rank of \a u64. 892 #define LSB4D(u64) MSB3D(u64) //!< Least significant byte of 5th rank of \a u64. 893 #define LSB3D(u64) MSB4D(u64) //!< Least significant byte of 4th rank of \a u64. 894 #define LSB2D(u64) MSB5D(u64) //!< Least significant byte of 3rd rank of \a u64. 895 #define LSB1D(u64) MSB6D(u64) //!< Least significant byte of 2nd rank of \a u64. 896 #define LSB0D(u64) MSB7D(u64) //!< Least significant byte of 1st rank of \a u64. 897 898 #define BE16(x) Swap16(x) 899 #define LE16(x) (x) 900 901 #define le16_to_cpu(x) (x) 902 #define cpu_to_le16(x) (x) 903 #define LE16_TO_CPU(x) (x) 904 #define CPU_TO_LE16(x) (x) 905 906 #define be16_to_cpu(x) Swap16(x) 907 #define cpu_to_be16(x) Swap16(x) 908 #define BE16_TO_CPU(x) Swap16(x) 909 #define CPU_TO_BE16(x) Swap16(x) 910 911 #define le32_to_cpu(x) (x) 912 #define cpu_to_le32(x) (x) 913 #define LE32_TO_CPU(x) (x) 914 #define CPU_TO_LE32(x) (x) 915 916 #define be32_to_cpu(x) swap32(x) 917 #define cpu_to_be32(x) swap32(x) 918 #define BE32_TO_CPU(x) swap32(x) 919 #define CPU_TO_BE32(x) swap32(x) 920 //! @} 921 922 923 /*! \name Endianism Conversion 924 * 925 * The same considerations as for clz and ctz apply here but GCC's 926 * __builtin_bswap_32 and __builtin_bswap_64 do not behave like macros when 927 * applied to constant expressions, so two sets of macros are defined here: 928 * - Swap16, Swap32 and Swap64 to apply to constant expressions (values known 929 * at compile time); 930 * - swap16, swap32 and swap64 to apply to non-constant expressions (values 931 * unknown at compile time). 932 */ 933 //! @{ 934 935 /*! \brief Toggles the endianism of \a u16 (by swapping its bytes). 936 * 937 * \param u16 U16 of which to toggle the endianism. 938 * 939 * \return Value resulting from \a u16 with toggled endianism. 940 * 941 * \note More optimized if only used with values known at compile time. 942 */ 943 #define Swap16(u16) ((U16)(((U16)(u16) >> 8) |\ 944 ((U16)(u16) << 8))) 945 946 /*! \brief Toggles the endianism of \a u32 (by swapping its bytes). 947 * 948 * \param u32 U32 of which to toggle the endianism. 949 * 950 * \return Value resulting from \a u32 with toggled endianism. 951 * 952 * \note More optimized if only used with values known at compile time. 953 */ 954 #define Swap32(u32) ((U32)(((U32)Swap16((U32)(u32) >> 16)) |\ 955 ((U32)Swap16((U32)(u32)) << 16))) 956 957 /*! \brief Toggles the endianism of \a u64 (by swapping its bytes). 958 * 959 * \param u64 U64 of which to toggle the endianism. 960 * 961 * \return Value resulting from \a u64 with toggled endianism. 962 * 963 * \note More optimized if only used with values known at compile time. 964 */ 965 #define Swap64(u64) ((U64)(((U64)Swap32((U64)(u64) >> 32)) |\ 966 ((U64)Swap32((U64)(u64)) << 32))) 967 968 /*! \brief Toggles the endianism of \a u16 (by swapping its bytes). 969 * 970 * \param u16 U16 of which to toggle the endianism. 971 * 972 * \return Value resulting from \a u16 with toggled endianism. 973 * 974 * \note More optimized if only used with values unknown at compile time. 975 */ 976 #define swap16(u16) Swap16(u16) 977 978 /*! \brief Toggles the endianism of \a u32 (by swapping its bytes). 979 * 980 * \param u32 U32 of which to toggle the endianism. 981 * 982 * \return Value resulting from \a u32 with toggled endianism. 983 * 984 * \note More optimized if only used with values unknown at compile time. 985 */ 986 #if (defined __GNUC__) 987 # define swap32(u32) ((U32)__builtin_bswap32((U32)(u32))) 988 #else 989 # define swap32(u32) Swap32(u32) 990 #endif 991 992 /*! \brief Toggles the endianism of \a u64 (by swapping its bytes). 993 * 994 * \param u64 U64 of which to toggle the endianism. 995 * 996 * \return Value resulting from \a u64 with toggled endianism. 997 * 998 * \note More optimized if only used with values unknown at compile time. 999 */ 1000 #if (defined __GNUC__) 1001 # define swap64(u64) ((U64)__builtin_bswap64((U64)(u64))) 1002 #else 1003 # define swap64(u64) ((U64)(((U64)swap32((U64)(u64) >> 32)) |\ 1004 ((U64)swap32((U64)(u64)) << 32))) 1005 #endif 1006 1007 //! @} 1008 1009 1010 /*! \name Target Abstraction 1011 */ 1012 //! @{ 1013 1014 #define _GLOBEXT_ extern //!< extern storage-class specifier. 1015 #define _CONST_TYPE_ const //!< const type qualifier. 1016 #define _MEM_TYPE_SLOW_ //!< Slow memory type. 1017 #define _MEM_TYPE_MEDFAST_ //!< Fairly fast memory type. 1018 #define _MEM_TYPE_FAST_ //!< Fast memory type. 1019 1020 typedef U8 Byte; //!< 8-bit unsigned integer. 1021 1022 #define memcmp_ram2ram memcmp //!< Target-specific memcmp of RAM to RAM. 1023 #define memcmp_code2ram memcmp //!< Target-specific memcmp of RAM to NVRAM. 1024 #define memcpy_ram2ram memcpy //!< Target-specific memcpy from RAM to RAM. 1025 #define memcpy_code2ram memcpy //!< Target-specific memcpy from NVRAM to RAM. 1026 1027 #define LSB0(u32) LSB0W(u32) //!< Least significant byte of 1st rank of \a u32. 1028 #define LSB1(u32) LSB1W(u32) //!< Least significant byte of 2nd rank of \a u32. 1029 #define LSB2(u32) LSB2W(u32) //!< Least significant byte of 3rd rank of \a u32. 1030 #define LSB3(u32) LSB3W(u32) //!< Least significant byte of 4th rank of \a u32. 1031 #define MSB3(u32) MSB3W(u32) //!< Most significant byte of 4th rank of \a u32. 1032 #define MSB2(u32) MSB2W(u32) //!< Most significant byte of 3rd rank of \a u32. 1033 #define MSB1(u32) MSB1W(u32) //!< Most significant byte of 2nd rank of \a u32. 1034 #define MSB0(u32) MSB0W(u32) //!< Most significant byte of 1st rank of \a u32. 1035 1036 //! @} 1037 1038 /** 1039 * \brief Calculate \f$ \left\lceil \frac{a}{b} \right\rceil \f$ using 1040 * integer arithmetic. 1041 * 1042 * \param a An integer 1043 * \param b Another integer 1044 * 1045 * \return (\a a / \a b) rounded up to the nearest integer. 1046 */ 1047 #define div_ceil(a, b) (((a) + (b) - 1) / (b)) 1048 1049 #endif // #ifndef __ASSEMBLY__ 1050 1051 1052 #if defined(__ICCARM__) 1053 #define SHORTENUM __packed 1054 #elif defined(__GNUC__) 1055 #define SHORTENUM __attribute__((packed)) 1056 #endif 1057 1058 /* No operation */ 1059 #if defined(__ICCARM__) 1060 #define nop() __no_operation() 1061 #elif defined(__GNUC__) 1062 #define nop() (__NOP()) 1063 #endif 1064 1065 #define FLASH_DECLARE(x) const x 1066 #define FLASH_EXTERN(x) extern const x 1067 #define PGM_READ_BYTE(x) *(x) 1068 #define PGM_READ_WORD(x) *(x) 1069 #define PGM_READ_DWORD(x) *(x) 1070 #define MEMCPY_ENDIAN memcpy 1071 #define PGM_READ_BLOCK(dst, src, len) memcpy((dst), (src), (len)) 1072 1073 /*Defines the Flash Storage for the request and response of MAC*/ 1074 #define CMD_ID_OCTET (0) 1075 1076 /* Converting of values from CPU endian to little endian. */ 1077 #define CPU_ENDIAN_TO_LE16(x) (x) 1078 #define CPU_ENDIAN_TO_LE32(x) (x) 1079 #define CPU_ENDIAN_TO_LE64(x) (x) 1080 1081 /* Converting of values from little endian to CPU endian. */ 1082 #define LE16_TO_CPU_ENDIAN(x) (x) 1083 #define LE32_TO_CPU_ENDIAN(x) (x) 1084 #define LE64_TO_CPU_ENDIAN(x) (x) 1085 1086 /* Converting of constants from little endian to CPU endian. */ 1087 #define CLE16_TO_CPU_ENDIAN(x) (x) 1088 #define CLE32_TO_CPU_ENDIAN(x) (x) 1089 #define CLE64_TO_CPU_ENDIAN(x) (x) 1090 1091 /* Converting of constants from CPU endian to little endian. */ 1092 #define CCPU_ENDIAN_TO_LE16(x) (x) 1093 #define CCPU_ENDIAN_TO_LE32(x) (x) 1094 #define CCPU_ENDIAN_TO_LE64(x) (x) 1095 1096 #define ADDR_COPY_DST_SRC_16(dst, src) ((dst) = (src)) 1097 #define ADDR_COPY_DST_SRC_64(dst, src) ((dst) = (src)) 1098 1099 /** 1100 * @brief Converts a 64-Bit value into a 8 Byte array 1101 * 1102 * @param[in] value 64-Bit value 1103 * @param[out] data Pointer to the 8 Byte array to be updated with 64-Bit value 1104 * @ingroup apiPalApi 1105 */ 1106 static inline void convert_64_bit_to_byte_array(uint64_t value, uint8_t *data) 1107 { 1108 uint8_t val_index = 0; 1109 1110 while (val_index < 8) 1111 { 1112 data[val_index++] = value & 0xFF; 1113 value = value >> 8; 1114 } 1115 } 1116 1117 /** 1118 * @brief Converts a 16-Bit value into a 2 Byte array 1119 * 1120 * @param[in] value 16-Bit value 1121 * @param[out] data Pointer to the 2 Byte array to be updated with 16-Bit value 1122 * @ingroup apiPalApi 1123 */ 1124 static inline void convert_16_bit_to_byte_array(uint16_t value, uint8_t *data) 1125 { 1126 data[0] = value & 0xFF; 1127 data[1] = (value >> 8) & 0xFF; 1128 } 1129 1130 /* Converts a 16-Bit value into a 2 Byte array */ 1131 static inline void convert_spec_16_bit_to_byte_array(uint16_t value, uint8_t *data) 1132 { 1133 data[0] = value & 0xFF; 1134 data[1] = (value >> 8) & 0xFF; 1135 } 1136 1137 /* Converts a 16-Bit value into a 2 Byte array */ 1138 static inline void convert_16_bit_to_byte_address(uint16_t value, uint8_t *data) 1139 { 1140 data[0] = value & 0xFF; 1141 data[1] = (value >> 8) & 0xFF; 1142 } 1143 1144 /* 1145 * @brief Converts a 2 Byte array into a 16-Bit value 1146 * 1147 * @param data Specifies the pointer to the 2 Byte array 1148 * 1149 * @return 16-Bit value 1150 * @ingroup apiPalApi 1151 */ 1152 static inline uint16_t convert_byte_array_to_16_bit(uint8_t *data) 1153 { 1154 return (data[0] | ((uint16_t)data[1] << 8)); 1155 } 1156 1157 /* Converts a 8 Byte array into a 32-Bit value */ 1158 static inline uint32_t convert_byte_array_to_32_bit(uint8_t *data) 1159 { 1160 union 1161 { 1162 uint32_t u32; 1163 uint8_t u8[8]; 1164 }long_addr; 1165 uint8_t index; 1166 for (index = 0; index < 4; index++) 1167 { 1168 long_addr.u8[index] = *data++; 1169 } 1170 return long_addr.u32; 1171 } 1172 1173 /** 1174 * @brief Converts a 8 Byte array into a 64-Bit value 1175 * 1176 * @param data Specifies the pointer to the 8 Byte array 1177 * 1178 * @return 64-Bit value 1179 * @ingroup apiPalApi 1180 */ 1181 static inline uint64_t convert_byte_array_to_64_bit(uint8_t *data) 1182 { 1183 union 1184 { 1185 uint64_t u64; 1186 uint8_t u8[8]; 1187 } long_addr; 1188 1189 uint8_t val_index; 1190 1191 for (val_index = 0; val_index < 8; val_index++) 1192 { 1193 long_addr.u8[val_index] = *data++; 1194 } 1195 1196 return long_addr.u64; 1197 } 1198 /** 1199 * \} 1200 */ 1201 1202 #endif /* UTILS_COMPILER_H */ 1203