macro.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #ifndef _MACRO_H
  2. #define _MACRO_H
  3. #define UINT_MAX 0xffffffff
  4. #define BIT(n) (1ul << (n))
  5. #define AT(x) __attribute__((section(#x)))
  6. #define ALIGNED(n) __attribute__((aligned(n)))
  7. #define DMA_ADR(x) ((u32)x)
  8. #define ALWAYS_INLINE __attribute__((always_inline)) inline
  9. #define NO_INLINE __attribute__((noinline))
  10. #define WEAK __attribute__((weak))
  11. #define PACKED __attribute__((packed))
  12. #define FIQ __attribute__((fiq("machine")))
  13. #define WDT_CLR() WDTCON = 0xa
  14. #define WDT_EN() WDTCON = 0x110
  15. #define WDT_DIS() WDTCON = 0xaa0
  16. #define WDT_RST() WDTCON = 0xa000110; while (1)
  17. #define WDT_RST_DELAY() WDTCON = 0xa100110; while (1)
  18. #define BYTE0(n) ((unsigned char)(n))
  19. #define BYTE1(n) ((unsigned char)((n)>>8))
  20. #define BYTE2(n) ((unsigned char)((n)>>16))
  21. #define BYTE3(n) ((unsigned char)((n)>>24))
  22. #define GET_LE16(ptr) (u16)(*(u16*)(u8*)(ptr))
  23. #define GET_LE32(ptr) (u32)(*(u32*)(u8*)(ptr))
  24. #define PUT_LE16(ptr, val) *(u16*)(u8*)(ptr) = (u16)(val)
  25. #define PUT_LE32(ptr, val) *(u32*)(u8*)(ptr) = (u32)(val)
  26. #define GET_BE16(ptr) get_be16(ptr)
  27. #define GET_BE32(ptr) get_be32(ptr)
  28. #define PUT_BE16(ptr, val) put_be16(ptr, val)
  29. #define PUT_BE32(ptr, val) put_be32(ptr, val)
  30. #define GLOBAL_INT_DISABLE() uint32_t cpu_ie = PICCON&BIT(0); PICCONCLR = BIT(0); asm volatile("":::"memory")
  31. #define GLOBAL_INT_RESTORE() asm volatile("":::"memory"); PICCON |= cpu_ie
  32. #define CONST_CAT(x, y) x ## y
  33. #define SET_MACRO(x, y) CONST_CAT(x, y)
  34. ///编译阶段检测结构体的大小是否等于特定值
  35. #define SIZE_OF_TYPE_EQUAL_TO(type, size) \
  36. static inline char size_of_##type##_equal_to_##size() \
  37. { \
  38. char __dummy1[sizeof(type) - size]; \
  39. char __dummy2[size - sizeof(type)]; \
  40. return __dummy1[0] + __dummy2[0]; \
  41. }
  42. ///编译阶段检测结构体成员在结构体的偏移是否等于特定值
  43. #define OFFSET_OF_TYPE_EQUAL_TO(type, member, size) \
  44. static inline char OFFSET_of_##member##_in_##type##_equal_to_##size() \
  45. { \
  46. char __dummy1[offsetof(type,member) - size]; \
  47. char __dummy2[size - offsetof(type,member)]; \
  48. return __dummy1[0] + __dummy2[0]; \
  49. }
  50. #endif // _MACRO_H