37 #ifndef UTILS_COMPILER_H 38 #define UTILS_COMPILER_H 50 #if (defined __ICCARM__) 51 # include <intrinsics.h> 61 #ifndef __ASSEMBLY__ // Not defined for assembling. 75 #define __inline__ inline 81 #define FUNC_PTR void * 86 #define UNUSED(v) (void)(v) 92 #define unused(v) do { (void)(v); } while(0) 98 #define barrier() __DMB() 106 #define COMPILER_PRAGMA(arg) _Pragma(#arg) 113 #define COMPILER_PACK_SET(alignment) COMPILER_PRAGMA(pack(alignment)) 120 #define COMPILER_PACK_RESET() COMPILER_PRAGMA(pack()) 126 #if (defined __GNUC__) || (defined __CC_ARM) 127 # define COMPILER_ALIGNED(a) __attribute__((__aligned__(a))) 128 #elif (defined __ICCARM__) 129 # define COMPILER_ALIGNED(a) COMPILER_PRAGMA(data_alignment = a) 135 #if (defined __GNUC__) || defined(__CC_ARM) 136 #define COMPILER_WORD_ALIGNED __attribute__((__aligned__(4))) 137 #elif (defined __ICCARM__) 138 #define COMPILER_WORD_ALIGNED COMPILER_PRAGMA(data_alignment = 4) 149 #if defined(__CC_ARM) 150 # define __always_inline __forceinline 151 #elif (defined __GNUC__) 152 #ifdef __always_inline 153 # undef __always_inline 155 # define __always_inline inline __attribute__((__always_inline__)) 156 #elif (defined __ICCARM__) 157 # define __always_inline _Pragma("inline=forced") 167 #if defined(__CC_ARM) 168 # define __no_inline __attribute__((noinline)) 169 #elif (defined __GNUC__) 170 # define __no_inline __attribute__((__noinline__)) 171 #elif (defined __ICCARM__) 172 # define __no_inline _Pragma("inline=never") 184 #if defined(_ASSERT_ENABLE_) 185 # if defined(TEST_SUITE_DEFINE_ASSERT_MACRO) 187 # include "unit_test/suite.h" 189 #undef TEST_SUITE_DEFINE_ASSERT_MACRO 190 # define Assert(expr) \ 192 if (!(expr)) while (true);\ 196 # define Assert(expr) ((void) 0) 200 #if defined ( __CC_ARM ) 201 # define WEAK __attribute__ ((weak)) 202 #elif defined ( __ICCARM__ ) 204 #elif defined ( __GNUC__ ) 205 # define WEAK __attribute__ ((weak)) 209 #if defined ( __CC_ARM ) 210 # define NO_INIT __attribute__((zero_init)) 211 #elif defined ( __ICCARM__ ) 212 # define NO_INIT __no_init 213 #elif defined ( __GNUC__ ) 214 # define NO_INIT __attribute__((section(".no_init"))) 218 #if defined ( __CC_ARM ) 219 # define RAMFUNC __attribute__ ((section(".ramfunc"))) 220 #elif defined ( __ICCARM__ ) 221 # define RAMFUNC __ramfunc 222 #elif defined ( __GNUC__ ) 223 # define RAMFUNC __attribute__ ((section(".ramfunc"))) 227 #if defined ( __CC_ARM ) 228 # define OPTIMIZE_HIGH _Pragma("O3") 229 #elif defined ( __ICCARM__ ) 230 # define OPTIMIZE_HIGH _Pragma("optimize=high") 231 #elif defined ( __GNUC__ ) 232 # define OPTIMIZE_HIGH __attribute__((optimize("s"))) 239 typedef unsigned char Bool;
242 #if !defined(__bool_true_false_are_defined) 415 #endif // #ifndef __ASSEMBLY__ 423 #if !defined(__bool_true_false_are_defined) 435 #ifndef __ASSEMBLY__ // not for assembling. 445 # define likely(exp) (exp) 453 # define unlikely(exp) (exp) 464 #if (defined __GNUC__) || (defined __CC_ARM) 465 # define is_constant(exp) __builtin_constant_p(exp) 467 # define is_constant(exp) (0) 483 #define Rd_bits( value, mask) ((value) & (mask)) 493 #define Wr_bits(lvalue, mask, bits) ((lvalue) = ((lvalue) & ~(mask)) |\ 503 #define Tst_bits( value, mask) (Rd_bits(value, mask) != 0) 512 #define Clr_bits(lvalue, mask) ((lvalue) &= ~(mask)) 521 #define Set_bits(lvalue, mask) ((lvalue) |= (mask)) 530 #define Tgl_bits(lvalue, mask) ((lvalue) ^= (mask)) 539 #define Rd_bitfield( value, mask) (Rd_bits( value, mask) >> ctz(mask)) 549 #define Wr_bitfield(lvalue, mask, bitfield) (Wr_bits(lvalue, mask, (U32)(bitfield) << ctz(mask))) 574 #if (defined __GNUC__) || (defined __CC_ARM) 575 # define clz(u) ((u) ? __builtin_clz(u) : 32) 576 #elif (defined __ICCARM__) 577 # define clz(u) ((u) ? __CLZ(u) : 32) 579 # define clz(u) (((u) == 0) ? 32 : \ 580 ((u) & (1ul << 31)) ? 0 : \ 581 ((u) & (1ul << 30)) ? 1 : \ 582 ((u) & (1ul << 29)) ? 2 : \ 583 ((u) & (1ul << 28)) ? 3 : \ 584 ((u) & (1ul << 27)) ? 4 : \ 585 ((u) & (1ul << 26)) ? 5 : \ 586 ((u) & (1ul << 25)) ? 6 : \ 587 ((u) & (1ul << 24)) ? 7 : \ 588 ((u) & (1ul << 23)) ? 8 : \ 589 ((u) & (1ul << 22)) ? 9 : \ 590 ((u) & (1ul << 21)) ? 10 : \ 591 ((u) & (1ul << 20)) ? 11 : \ 592 ((u) & (1ul << 19)) ? 12 : \ 593 ((u) & (1ul << 18)) ? 13 : \ 594 ((u) & (1ul << 17)) ? 14 : \ 595 ((u) & (1ul << 16)) ? 15 : \ 596 ((u) & (1ul << 15)) ? 16 : \ 597 ((u) & (1ul << 14)) ? 17 : \ 598 ((u) & (1ul << 13)) ? 18 : \ 599 ((u) & (1ul << 12)) ? 19 : \ 600 ((u) & (1ul << 11)) ? 20 : \ 601 ((u) & (1ul << 10)) ? 21 : \ 602 ((u) & (1ul << 9)) ? 22 : \ 603 ((u) & (1ul << 8)) ? 23 : \ 604 ((u) & (1ul << 7)) ? 24 : \ 605 ((u) & (1ul << 6)) ? 25 : \ 606 ((u) & (1ul << 5)) ? 26 : \ 607 ((u) & (1ul << 4)) ? 27 : \ 608 ((u) & (1ul << 3)) ? 28 : \ 609 ((u) & (1ul << 2)) ? 29 : \ 610 ((u) & (1ul << 1)) ? 30 : \ 620 #if (defined __GNUC__) || (defined __CC_ARM) 621 # define ctz(u) ((u) ? __builtin_ctz(u) : 32) 623 # define ctz(u) ((u) & (1ul << 0) ? 0 : \ 624 (u) & (1ul << 1) ? 1 : \ 625 (u) & (1ul << 2) ? 2 : \ 626 (u) & (1ul << 3) ? 3 : \ 627 (u) & (1ul << 4) ? 4 : \ 628 (u) & (1ul << 5) ? 5 : \ 629 (u) & (1ul << 6) ? 6 : \ 630 (u) & (1ul << 7) ? 7 : \ 631 (u) & (1ul << 8) ? 8 : \ 632 (u) & (1ul << 9) ? 9 : \ 633 (u) & (1ul << 10) ? 10 : \ 634 (u) & (1ul << 11) ? 11 : \ 635 (u) & (1ul << 12) ? 12 : \ 636 (u) & (1ul << 13) ? 13 : \ 637 (u) & (1ul << 14) ? 14 : \ 638 (u) & (1ul << 15) ? 15 : \ 639 (u) & (1ul << 16) ? 16 : \ 640 (u) & (1ul << 17) ? 17 : \ 641 (u) & (1ul << 18) ? 18 : \ 642 (u) & (1ul << 19) ? 19 : \ 643 (u) & (1ul << 20) ? 20 : \ 644 (u) & (1ul << 21) ? 21 : \ 645 (u) & (1ul << 22) ? 22 : \ 646 (u) & (1ul << 23) ? 23 : \ 647 (u) & (1ul << 24) ? 24 : \ 648 (u) & (1ul << 25) ? 25 : \ 649 (u) & (1ul << 26) ? 26 : \ 650 (u) & (1ul << 27) ? 27 : \ 651 (u) & (1ul << 28) ? 28 : \ 652 (u) & (1ul << 29) ? 29 : \ 653 (u) & (1ul << 30) ? 30 : \ 654 (u) & (1ul << 31) ? 31 : \ 671 #define bit_reverse8(u8) ((U8)(bit_reverse32((U8)(u8)) >> 24)) 679 #define bit_reverse16(u16) ((U16)(bit_reverse32((U16)(u16)) >> 16)) 687 #define bit_reverse32(u32) __RBIT(u32) 695 #define bit_reverse64(u64) ((U64)(((U64)bit_reverse32((U64)(u64) >> 32)) |\ 696 ((U64)bit_reverse32((U64)(u64)) << 32))) 712 #define Test_align(val, n ) (!Tst_bits( val, (n) - 1 ) ) 721 #define Get_align( val, n ) ( Rd_bits( val, (n) - 1 ) ) 731 #define Set_align(lval, n, alg) ( Wr_bits(lval, (n) - 1, alg) ) 740 #define Align_up( val, n ) (((val) + ((n) - 1)) & ~((n) - 1)) 749 #define Align_down(val, n ) ( (val) & ~((n) - 1)) 775 #define Abs(a) (((a) < 0 ) ? -(a) : (a)) 786 #define Min(a, b) (((a) < (b)) ? (a) : (b)) 797 #define Max(a, b) (((a) > (b)) ? (a) : (b)) 799 #if !defined(__cplusplus) 812 #define min(a, b) Min(a, b) 823 #define max(a, b) Max(a, b) 841 #define Long_call(addr) ((*(void (*)(void))(addr))()) 847 #define MSB(u16) (((U8 *)&(u16))[1]) 849 #define LSB(u16) (((U8 *)&(u16))[0]) 851 #define MSH(u32) (((U16 *)&(u32))[1]) 852 #define LSH(u32) (((U16 *)&(u32))[0]) 853 #define MSB0W(u32) (((U8 *)&(u32))[3]) 854 #define MSB1W(u32) (((U8 *)&(u32))[2]) 855 #define MSB2W(u32) (((U8 *)&(u32))[1]) 856 #define MSB3W(u32) (((U8 *)&(u32))[0]) 857 #define LSB3W(u32) MSB0W(u32) 858 #define LSB2W(u32) MSB1W(u32) 859 #define LSB1W(u32) MSB2W(u32) 860 #define LSB0W(u32) MSB3W(u32) 862 #define MSW(u64) (((U32 *)&(u64))[1]) 863 #define LSW(u64) (((U32 *)&(u64))[0]) 864 #define MSH0(u64) (((U16 *)&(u64))[3]) 865 #define MSH1(u64) (((U16 *)&(u64))[2]) 866 #define MSH2(u64) (((U16 *)&(u64))[1]) 867 #define MSH3(u64) (((U16 *)&(u64))[0]) 868 #define LSH3(u64) MSH0(u64) 869 #define LSH2(u64) MSH1(u64) 870 #define LSH1(u64) MSH2(u64) 871 #define LSH0(u64) MSH3(u64) 872 #define MSB0D(u64) (((U8 *)&(u64))[7]) 873 #define MSB1D(u64) (((U8 *)&(u64))[6]) 874 #define MSB2D(u64) (((U8 *)&(u64))[5]) 875 #define MSB3D(u64) (((U8 *)&(u64))[4]) 876 #define MSB4D(u64) (((U8 *)&(u64))[3]) 877 #define MSB5D(u64) (((U8 *)&(u64))[2]) 878 #define MSB6D(u64) (((U8 *)&(u64))[1]) 879 #define MSB7D(u64) (((U8 *)&(u64))[0]) 880 #define LSB7D(u64) MSB0D(u64) 881 #define LSB6D(u64) MSB1D(u64) 882 #define LSB5D(u64) MSB2D(u64) 883 #define LSB4D(u64) MSB3D(u64) 884 #define LSB3D(u64) MSB4D(u64) 885 #define LSB2D(u64) MSB5D(u64) 886 #define LSB1D(u64) MSB6D(u64) 887 #define LSB0D(u64) MSB7D(u64) 889 #define BE16(x) swap16(x) 892 #define le16_to_cpu(x) (x) 893 #define cpu_to_le16(x) (x) 894 #define LE16_TO_CPU(x) (x) 895 #define CPU_TO_LE16(x) (x) 897 #define be16_to_cpu(x) swap16(x) 898 #define cpu_to_be16(x) swap16(x) 899 #define BE16_TO_CPU(x) swap16(x) 900 #define CPU_TO_BE16(x) swap16(x) 902 #define le32_to_cpu(x) (x) 903 #define cpu_to_le32(x) (x) 904 #define LE32_TO_CPU(x) (x) 905 #define CPU_TO_LE32(x) (x) 907 #define be32_to_cpu(x) swap32(x) 908 #define cpu_to_be32(x) swap32(x) 909 #define BE32_TO_CPU(x) swap32(x) 910 #define CPU_TO_BE32(x) swap32(x) 934 #define Swap16(u16) ((U16)(((U16)(u16) >> 8) |\ 945 #define Swap32(u32) ((U32)(((U32)Swap16((U32)(u32) >> 16)) |\ 946 ((U32)Swap16((U32)(u32)) << 16))) 956 #define Swap64(u64) ((U64)(((U64)Swap32((U64)(u64) >> 32)) |\ 957 ((U64)Swap32((U64)(u64)) << 32))) 967 #define swap16(u16) Swap16(u16) 977 #if (defined __GNUC__) 978 # define swap32(u32) ((U32)__builtin_bswap32((U32)(u32))) 980 # define swap32(u32) Swap32(u32) 991 #if (defined __GNUC__) 992 # define swap64(u64) ((U64)__builtin_bswap64((U64)(u64))) 994 # define swap64(u64) ((U64)(((U64)swap32((U64)(u64) >> 32)) |\ 995 ((U64)swap32((U64)(u64)) << 32))) 1005 #define _GLOBEXT_ extern 1006 #define _CONST_TYPE_ const 1007 #define _MEM_TYPE_SLOW_ 1008 #define _MEM_TYPE_MEDFAST_ 1009 #define _MEM_TYPE_FAST_ 1013 #define memcmp_ram2ram memcmp 1014 #define memcmp_code2ram memcmp 1015 #define memcpy_ram2ram memcpy 1016 #define memcpy_code2ram memcpy 1018 #define LSB0(u32) LSB0W(u32) 1019 #define LSB1(u32) LSB1W(u32) 1020 #define LSB2(u32) LSB2W(u32) 1021 #define LSB3(u32) LSB3W(u32) 1022 #define MSB3(u32) MSB3W(u32) 1023 #define MSB2(u32) MSB2W(u32) 1024 #define MSB1(u32) MSB1W(u32) 1025 #define MSB0(u32) MSB0W(u32) 1038 #define div_ceil(a, b) (((a) + (b) - 1) / (b)) 1040 #endif // #ifndef __ASSEMBLY__ 1043 #if defined(__ICCARM__) 1044 #define SHORTENUM __packed 1045 #elif defined(__GNUC__) 1046 #define SHORTENUM __attribute__((packed)) 1050 #if defined(__ICCARM__) 1051 #define nop() __no_operation() 1052 #elif defined(__GNUC__) 1053 #define nop() (__NOP()) 1056 #define FLASH_DECLARE(x) const x 1057 #define FLASH_EXTERN(x) extern const x 1058 #define PGM_READ_BYTE(x) *(x) 1059 #define PGM_READ_WORD(x) *(x) 1060 #define PGM_READ_DWORD(x) *(x) 1061 #define MEMCPY_ENDIAN memcpy 1062 #define PGM_READ_BLOCK(dst, src, len) memcpy((dst), (src), (len)) 1065 #define CMD_ID_OCTET (0) 1068 #define CPU_ENDIAN_TO_LE16(x) (x) 1069 #define CPU_ENDIAN_TO_LE32(x) (x) 1070 #define CPU_ENDIAN_TO_LE64(x) (x) 1073 #define LE16_TO_CPU_ENDIAN(x) (x) 1074 #define LE32_TO_CPU_ENDIAN(x) (x) 1075 #define LE64_TO_CPU_ENDIAN(x) (x) 1078 #define CLE16_TO_CPU_ENDIAN(x) (x) 1079 #define CLE32_TO_CPU_ENDIAN(x) (x) 1080 #define CLE64_TO_CPU_ENDIAN(x) (x) 1083 #define CCPU_ENDIAN_TO_LE16(x) (x) 1084 #define CCPU_ENDIAN_TO_LE32(x) (x) 1085 #define CCPU_ENDIAN_TO_LE64(x) (x) 1087 #define ADDR_COPY_DST_SRC_16(dst, src) ((dst) = (src)) 1088 #define ADDR_COPY_DST_SRC_64(dst, src) ((dst) = (src)) 1099 uint8_t val_index = 0;
1101 while (val_index < 8)
1103 data[val_index++] = value & 0xFF;
1117 data[0] = value & 0xFF;
1118 data[1] = (value >> 8) & 0xFF;
1124 data[0] = value & 0xFF;
1125 data[1] = (value >> 8) & 0xFF;
1131 data[0] = value & 0xFF;
1132 data[1] = (value >> 8) & 0xFF;
1145 return (data[0] | ((uint16_t)data[1] << 8));
1157 for (index = 0; index < 4; index++)
1159 long_addr.u8[index] = *data++;
1161 return long_addr.u32;
1182 for (val_index = 0; val_index < 8; val_index++)
1184 long_addr.u8[val_index] = *data++;
1187 return long_addr.u64;
int8_t S8
8-bit signed integer.
const volatile U64 * u64ptr
uint64_t U64
64-bit unsigned integer.
Union of pointers to constant 64-, 32-, 16- and 8-bit unsigned integers.
Union of pointers to constant volatile 64-, 32-, 16- and 8-bit unsigned integers. ...
const volatile S16 * s16ptr
uint16_t U16
16-bit unsigned integer.
GeneratorWrapper< T > value(T &&value)
const volatile S32 * s32ptr
const volatile S8 * s8ptr
double F64
64-bit floating-point number.
const volatile U8 * u8ptr
const volatile U8 * u8ptr
static uint16_t convert_byte_array_to_16_bit(uint8_t *data)
const volatile U64 * u64ptr
int32_t S32
32-bit signed integer.
Structure of pointers to volatile 64-, 32-, 16- and 8-bit unsigned integers.
const volatile U16 * u16ptr
Atmel part identification macros.
float F32
32-bit floating-point number.
const volatile U16 * u16ptr
const volatile S8 * s8ptr
uint32_t U32
32-bit unsigned integer.
const volatile S32 * s32ptr
const volatile S64 * s64ptr
Structure of pointers to 64-, 32-, 16- and 8-bit unsigned integers.
static void convert_16_bit_to_byte_array(uint16_t value, uint8_t *data)
Converts a 16-Bit value into a 2 Byte array.
static void convert_64_bit_to_byte_array(uint64_t value, uint8_t *data)
Converts a 64-Bit value into a 8 Byte array.
Union of pointers to 64-, 32-, 16- and 8-bit unsigned integers.
Global interrupt management for 8- and 32-bit AVR.
USBInterfaceDescriptor data
Union of pointers to volatile 64-, 32-, 16- and 8-bit unsigned integers.
static uint32_t convert_byte_array_to_32_bit(uint8_t *data)
unsigned char bool
Boolean.
const volatile U32 * u32ptr
static void convert_16_bit_to_byte_address(uint16_t value, uint8_t *data)
uint8_t U8
8-bit unsigned integer.
static uint64_t convert_byte_array_to_64_bit(uint8_t *data)
Converts a 8 Byte array into a 64-Bit value.
int16_t S16
16-bit signed integer.
U8 Byte
8-bit unsigned integer.
Structure of pointers to constant 64-, 32-, 16- and 8-bit unsigned integers.
static void convert_spec_16_bit_to_byte_array(uint16_t value, uint8_t *data)
Structure of pointers to constant volatile 64-, 32-, 16- and 8-bit unsigned integers.
bool Status_bool_t
Boolean status.
const volatile U32 * u32ptr
const volatile S64 * s64ptr
unsigned char Bool
Boolean.
const volatile S16 * s16ptr
int64_t S64
64-bit signed integer.