#ifndef UTILS_COMPILER_H_INCLUDED
# define UTILS_COMPILER_H_INCLUDED
# if (defined __ICCARM__)
# include <intrinsics.h>
# endif
# include <stddef.h>
# ifndef __ASSEMBLY__
# include <stdio.h>
# include <stdbool.h>
# include <stdint.h>
# include <stdlib.h>
# define UNUSED(v) (void)(v)
# ifdef __GNUC__
# define barrier() asm volatile("" ::: "memory")
# else
# define barrier() asm("")
# endif
# define COMPILER_PRAGMA(arg) _Pragma(# arg)
# define COMPILER_PACK_SET(alignment) COMPILER_PRAGMA(pack(alignment))
# define COMPILER_PACK_RESET() COMPILER_PRAGMA(pack())
# if (defined __GNUC__) || (defined __CC_ARM)
# define COMPILER_ALIGNED(a) __attribute__((__aligned__(a)))
# elif (defined __ICCARM__)
# define COMPILER_ALIGNED(a) COMPILER_PRAGMA(data_alignment = a)
# endif
# if (defined __GNUC__) || defined(__CC_ARM)
# define COMPILER_WORD_ALIGNED __attribute__((__aligned__(4)))
# elif (defined __ICCARM__)
# define COMPILER_WORD_ALIGNED COMPILER_PRAGMA(data_alignment = 4)
# endif
# if !defined(__always_inline)
# if defined(__CC_ARM)
# define __always_inline __forceinline
# elif (defined __GNUC__)
# define __always_inline __attribute__((__always_inline__))
# elif (defined __ICCARM__)
# define __always_inline _Pragma("inline=forced")
# endif
# endif
# if defined(__CC_ARM)
# define __no_inline __attribute__((noinline))
# elif (defined __GNUC__)
# define __no_inline __attribute__((noinline))
# elif (defined __ICCARM__)
# define __no_inline _Pragma("inline=never")
# endif
# if defined(_ASSERT_ENABLE_)
# if defined(TEST_SUITE_DEFINE_ASSERT_MACRO)
# include "unit_test/suite.h"
# else
# undef TEST_SUITE_DEFINE_ASSERT_MACRO
# define Assert(expr) \
{ \
if (!(expr)) asm("BKPT #0"); \
}
# endif
# else
# define Assert(expr) ((void)0)
# endif
# if defined(__CC_ARM)
# define WEAK __attribute__((weak))
# elif defined(__ICCARM__)
# define WEAK __weak
# elif defined(__GNUC__)
# define WEAK __attribute__((weak))
# endif
# if defined(__CC_ARM)
# define NO_INIT __attribute__((zero_init))
# elif defined(__ICCARM__)
# define NO_INIT __no_init
# elif defined(__GNUC__)
# define NO_INIT __attribute__((section(".no_init")))
# endif
# ifndef __cplusplus
# if !defined(__bool_true_false_are_defined)
typedef unsigned char bool;
# endif
# endif
typedef uint16_t le16_t;
typedef uint16_t be16_t;
typedef uint32_t le32_t;
typedef uint32_t be32_t;
typedef uint32_t iram_size_t;
typedef union {
int16_t s16;
uint16_t u16;
int8_t s8[2];
uint8_t u8[2];
} Union16;
typedef union {
int32_t s32;
uint32_t u32;
int16_t s16[2];
uint16_t u16[2];
int8_t s8[4];
uint8_t u8[4];
} Union32;
typedef union {
int64_t s64;
uint64_t u64;
int32_t s32[2];
uint32_t u32[2];
int16_t s16[4];
uint16_t u16[4];
int8_t s8[8];
uint8_t u8[8];
} Union64;
typedef union {
int64_t * s64ptr;
uint64_t *u64ptr;
int32_t * s32ptr;
uint32_t *u32ptr;
int16_t * s16ptr;
uint16_t *u16ptr;
int8_t * s8ptr;
uint8_t * u8ptr;
} UnionPtr;
typedef union {
volatile int64_t * s64ptr;
volatile uint64_t *u64ptr;
volatile int32_t * s32ptr;
volatile uint32_t *u32ptr;
volatile int16_t * s16ptr;
volatile uint16_t *u16ptr;
volatile int8_t * s8ptr;
volatile uint8_t * u8ptr;
} UnionVPtr;
typedef union {
const int64_t * s64ptr;
const uint64_t *u64ptr;
const int32_t * s32ptr;
const uint32_t *u32ptr;
const int16_t * s16ptr;
const uint16_t *u16ptr;
const int8_t * s8ptr;
const uint8_t * u8ptr;
} UnionCPtr;
typedef union {
const volatile int64_t * s64ptr;
const volatile uint64_t *u64ptr;
const volatile int32_t * s32ptr;
const volatile uint32_t *u32ptr;
const volatile int16_t * s16ptr;
const volatile uint16_t *u16ptr;
const volatile int8_t * s8ptr;
const volatile uint8_t * u8ptr;
} UnionCVPtr;
typedef struct {
int64_t * s64ptr;
uint64_t *u64ptr;
int32_t * s32ptr;
uint32_t *u32ptr;
int16_t * s16ptr;
uint16_t *u16ptr;
int8_t * s8ptr;
uint8_t * u8ptr;
} StructPtr;
typedef struct {
volatile int64_t * s64ptr;
volatile uint64_t *u64ptr;
volatile int32_t * s32ptr;
volatile uint32_t *u32ptr;
volatile int16_t * s16ptr;
volatile uint16_t *u16ptr;
volatile int8_t * s8ptr;
volatile uint8_t * u8ptr;
} StructVPtr;
typedef struct {
const int64_t * s64ptr;
const uint64_t *u64ptr;
const int32_t * s32ptr;
const uint32_t *u32ptr;
const int16_t * s16ptr;
const uint16_t *u16ptr;
const int8_t * s8ptr;
const uint8_t * u8ptr;
} StructCPtr;
typedef struct {
const volatile int64_t * s64ptr;
const volatile uint64_t *u64ptr;
const volatile int32_t * s32ptr;
const volatile uint32_t *u32ptr;
const volatile int16_t * s16ptr;
const volatile uint16_t *u16ptr;
const volatile int8_t * s8ptr;
const volatile uint8_t * u8ptr;
} StructCVPtr;
# endif
# ifndef __cplusplus
# if !defined(__bool_true_false_are_defined)
# define false 0
# define true 1
# endif
# endif
# ifndef __ASSEMBLY__
# if !defined(likely) || defined(__DOXYGEN__)
# define likely(exp) (exp)
# endif
# if !defined(unlikely) || defined(__DOXYGEN__)
# define unlikely(exp) (exp)
# endif
# if (defined __GNUC__) || (defined __CC_ARM)
# define is_constant(exp) __builtin_constant_p(exp)
# else
# define is_constant(exp) (0)
# endif
# define Rd_bits(value, mask) ((value) & (mask))
# define Wr_bits(lvalue, mask, bits) ((lvalue) = ((lvalue) & ~(mask)) | ((bits) & (mask)))
# define Tst_bits(value, mask) (Rd_bits(value, mask) != 0)
# define Clr_bits(lvalue, mask) ((lvalue) &= ~(mask))
# define Set_bits(lvalue, mask) ((lvalue) |= (mask))
# define Tgl_bits(lvalue, mask) ((lvalue) ^= (mask))
# define Rd_bitfield(value, mask) (Rd_bits(value, mask) >> ctz(mask))
# define Wr_bitfield(lvalue, mask, bitfield) (Wr_bits(lvalue, mask, (uint32_t)(bitfield) << ctz(mask)))
# if (defined __GNUC__) || (defined __CC_ARM)
# define clz(u) ((u) ? __builtin_clz(u) : 32)
# 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
# if (defined __GNUC__) || (defined __CC_ARM)
# define ctz(u) ((u) ? __builtin_ctz(u) : 32)
# 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
# define bit_reverse8(u8) ((U8)(bit_reverse32((U8)(u8)) >> 24))
# define bit_reverse16(u16) ((uint16_t)(bit_reverse32((uint16_t)(u16)) >> 16))
# define bit_reverse32(u32) __RBIT(u32)
# define bit_reverse64(u64) ((uint64_t)(((uint64_t)bit_reverse32((uint64_t)(u64) >> 32)) | ((uint64_t)bit_reverse32((uint64_t)(u64)) << 32)))
# define Test_align(val, n) (!Tst_bits(val, (n)-1))
# define Get_align(val, n) (Rd_bits(val, (n)-1))
# define Set_align(lval, n, alg) (Wr_bits(lval, (n)-1, alg))
# define Align_up(val, n) (((val) + ((n)-1)) & ~((n)-1))
# define Align_down(val, n) ((val) & ~((n)-1))
# define Abs(a) (((a) < 0) ? -(a) : (a))
# ifndef __cplusplus
# define Min(a, b) (((a) < (b)) ? (a) : (b))
# define Max(a, b) (((a) > (b)) ? (a) : (b))
# define min(a, b) Min(a, b)
# define max(a, b) Max(a, b)
# endif
# define Long_call(addr) ((*(void (*)(void))(addr))())
# define BE16(x) swap16(x)
# define LE16(x) (x)
# define le16_to_cpu(x) (x)
# define cpu_to_le16(x) (x)
# define LE16_TO_CPU(x) (x)
# define CPU_TO_LE16(x) (x)
# define be16_to_cpu(x) swap16(x)
# define cpu_to_be16(x) swap16(x)
# define BE16_TO_CPU(x) swap16(x)
# define CPU_TO_BE16(x) swap16(x)
# define le32_to_cpu(x) (x)
# define cpu_to_le32(x) (x)
# define LE32_TO_CPU(x) (x)
# define CPU_TO_LE32(x) (x)
# define be32_to_cpu(x) swap32(x)
# define cpu_to_be32(x) swap32(x)
# define BE32_TO_CPU(x) swap32(x)
# define CPU_TO_BE32(x) swap32(x)
# define Swap16(u16) ((uint16_t)(((uint16_t)(u16) >> 8) | ((uint16_t)(u16) << 8)))
# define Swap32(u32) ((uint32_t)(((uint32_t)Swap16((uint32_t)(u32) >> 16)) | ((uint32_t)Swap16((uint32_t)(u32)) << 16)))
# define Swap64(u64) ((uint64_t)(((uint64_t)Swap32((uint64_t)(u64) >> 32)) | ((uint64_t)Swap32((uint64_t)(u64)) << 32)))
# define swap16(u16) Swap16(u16)
# if (defined __GNUC__)
# define swap32(u32) ((uint32_t)__builtin_bswap32((uint32_t)(u32)))
# else
# define swap32(u32) Swap32(u32)
# endif
# if (defined __GNUC__)
# define swap64(u64) ((uint64_t)__builtin_bswap64((uint64_t)(u64)))
# else
# define swap64(u64) ((uint64_t)(((uint64_t)swap32((uint64_t)(u64) >> 32)) | ((uint64_t)swap32((uint64_t)(u64)) << 32)))
# endif
# define _GLOBEXT_ extern
# define _CONST_TYPE_ const
# define _MEM_TYPE_SLOW_
# define _MEM_TYPE_MEDFAST_
# define _MEM_TYPE_FAST_
# define memcmp_ram2ram memcmp
# define memcmp_code2ram memcmp
# define memcpy_ram2ram memcpy
# define memcpy_code2ram memcpy
# define div_ceil(a, b) (((a) + (b)-1) / (b))
# endif
# ifdef __ICCARM__
# define __asm__ asm
# define __inline__ inline
# define __volatile__
# endif
# define FUNC_PTR void *
# define unused(v) \
do { \
(void)(v); \
} while (0)
# if defined(__CC_ARM)
# define RAMFUNC __attribute__((section(".ramfunc")))
# elif defined(__ICCARM__)
# define RAMFUNC __ramfunc
# elif defined(__GNUC__)
# define RAMFUNC __attribute__((section(".ramfunc")))
# endif
# if defined(__CC_ARM)
# define OPTIMIZE_HIGH _Pragma("O3")
# elif defined(__ICCARM__)
# define OPTIMIZE_HIGH _Pragma("optimize=high")
# elif defined(__GNUC__)
# define OPTIMIZE_HIGH __attribute__((optimize("s")))
# endif
typedef int8_t S8; typedef uint8_t U8; typedef int16_t S16; typedef uint16_t U16; typedef int32_t S32; typedef uint32_t U32; typedef int64_t S64; typedef uint64_t U64; typedef float F32; typedef double F64;
# define MSB(u16) (((U8 *)&(u16))[1])
# define LSB(u16) (((U8 *)&(u16))[0])
# define MSH(u32) (((U16 *)&(u32))[1])
# define LSH(u32) (((U16 *)&(u32))[0])
# define MSB0W(u32) (((U8 *)&(u32))[3])
# define MSB1W(u32) (((U8 *)&(u32))[2])
# define MSB2W(u32) (((U8 *)&(u32))[1])
# define MSB3W(u32) (((U8 *)&(u32))[0])
# define LSB3W(u32) MSB0W(u32)
# define LSB2W(u32) MSB1W(u32)
# define LSB1W(u32) MSB2W(u32)
# define LSB0W(u32) MSB3W(u32)
# define MSW(u64) (((U32 *)&(u64))[1])
# define LSW(u64) (((U32 *)&(u64))[0])
# define MSH0(u64) (((U16 *)&(u64))[3])
# define MSH1(u64) (((U16 *)&(u64))[2])
# define MSH2(u64) (((U16 *)&(u64))[1])
# define MSH3(u64) (((U16 *)&(u64))[0])
# define LSH3(u64) MSH0(u64)
# define LSH2(u64) MSH1(u64)
# define LSH1(u64) MSH2(u64)
# define LSH0(u64) MSH3(u64)
# define MSB0D(u64) (((U8 *)&(u64))[7])
# define MSB1D(u64) (((U8 *)&(u64))[6])
# define MSB2D(u64) (((U8 *)&(u64))[5])
# define MSB3D(u64) (((U8 *)&(u64))[4])
# define MSB4D(u64) (((U8 *)&(u64))[3])
# define MSB5D(u64) (((U8 *)&(u64))[2])
# define MSB6D(u64) (((U8 *)&(u64))[1])
# define MSB7D(u64) (((U8 *)&(u64))[0])
# define LSB7D(u64) MSB0D(u64)
# define LSB6D(u64) MSB1D(u64)
# define LSB5D(u64) MSB2D(u64)
# define LSB4D(u64) MSB3D(u64)
# define LSB3D(u64) MSB4D(u64)
# define LSB2D(u64) MSB5D(u64)
# define LSB1D(u64) MSB6D(u64)
# define LSB0D(u64) MSB7D(u64)
# define LSB0(u32) LSB0W(u32)
# define LSB1(u32) LSB1W(u32)
# define LSB2(u32) LSB2W(u32)
# define LSB3(u32) LSB3W(u32)
# define MSB3(u32) MSB3W(u32)
# define MSB2(u32) MSB2W(u32)
# define MSB1(u32) MSB1W(u32)
# define MSB0(u32) MSB0W(u32)
# if defined(__ICCARM__)
# define SHORTENUM __packed
# elif defined(__GNUC__)
# define SHORTENUM __attribute__((packed))
# endif
# if defined(__ICCARM__)
# define nop() __no_operation()
# elif defined(__GNUC__)
# define nop() (__NOP())
# endif
# define FLASH_DECLARE(x) const x
# define FLASH_EXTERN(x) extern const x
# define PGM_READ_BYTE(x) *(x)
# define PGM_READ_WORD(x) *(x)
# define MEMCPY_ENDIAN memcpy
# define PGM_READ_BLOCK(dst, src, len) memcpy((dst), (src), (len))
# define CMD_ID_OCTET (0)
# define CPU_ENDIAN_TO_LE16(x) (x)
# define CPU_ENDIAN_TO_LE32(x) (x)
# define CPU_ENDIAN_TO_LE64(x) (x)
# define LE16_TO_CPU_ENDIAN(x) (x)
# define LE32_TO_CPU_ENDIAN(x) (x)
# define LE64_TO_CPU_ENDIAN(x) (x)
# define CLE16_TO_CPU_ENDIAN(x) (x)
# define CLE32_TO_CPU_ENDIAN(x) (x)
# define CLE64_TO_CPU_ENDIAN(x) (x)
# define CCPU_ENDIAN_TO_LE16(x) (x)
# define CCPU_ENDIAN_TO_LE32(x) (x)
# define CCPU_ENDIAN_TO_LE64(x) (x)
# define ADDR_COPY_DST_SRC_16(dst, src) ((dst) = (src))
# define ADDR_COPY_DST_SRC_64(dst, src) ((dst) = (src))
static inline void convert_64_bit_to_byte_array(uint64_t value, uint8_t *data) {
uint8_t index = 0;
while (index < 8) {
data[index++] = value & 0xFF;
value = value >> 8;
}
}
static inline void convert_16_bit_to_byte_array(uint16_t value, uint8_t *data) {
data[0] = value & 0xFF;
data[1] = (value >> 8) & 0xFF;
}
static inline void convert_spec_16_bit_to_byte_array(uint16_t value, uint8_t *data) {
data[0] = value & 0xFF;
data[1] = (value >> 8) & 0xFF;
}
static inline void convert_16_bit_to_byte_address(uint16_t value, uint8_t *data) {
data[0] = value & 0xFF;
data[1] = (value >> 8) & 0xFF;
}
static inline uint16_t convert_byte_array_to_16_bit(uint8_t *data) { return (data[0] | ((uint16_t)data[1] << 8)); }
static inline uint32_t convert_byte_array_to_32_bit(uint8_t *data) {
union {
uint32_t u32;
uint8_t u8[4];
} long_addr;
uint8_t index;
for (index = 0; index < 4; index++) {
long_addr.u8[index] = *data++;
}
return long_addr.u32;
}
static inline uint64_t convert_byte_array_to_64_bit(uint8_t *data) {
union {
uint64_t u64;
uint8_t u8[8];
} long_addr;
uint8_t index;
for (index = 0; index < 8; index++) {
long_addr.u8[index] = *data++;
}
return long_addr.u64;
}
#endif