lawrencejiabin
3/12/2020 - 10:45 AM

## 常用宏定义

``````#if CPU_ENDIAN == LITTLE_ENDIAN
#define LD_WORD(ptr)        (u16)(*(u16*)(u8*)(ptr))
#define LD_DWORD(ptr)        (u32)(*(u32*)(u8*)(ptr))
#define ST_WORD(ptr,val)    *(u16*)(u8*)(ptr)=(u16)(val)
#define ST_DWORD(ptr,val)    *(u32*)(u8*)(ptr)=(u32)(val)
#else
//#define ntohl(x) (x)
//#define ntoh(x) (x)
//#define NTOH(x) (x) = ntoh(x)
//#define NTOHL(x) (x) = ntohl(x)
#endif

#define FALSE    	0
#define TRUE    	1
#define false    	0
#define true    	1
#define NULL    	(void *)0

#define     BIT(n)              (1UL << (n))
#define     BitSET(REG,POS)     ((REG) |= (1L << (POS)))
#define     BitCLR(REG,POS)     ((REG) &= (~(1L<< (POS))))
#define     BitXOR(REG,POS)     ((REG) ^= (~(1L << (POS))))
#define     BitCHK_1(REG,POS)   (((REG) & (1L << (POS))) == (1L << (POS)))
#define     BitCHK_0(REG,POS)   (((REG) & (1L << (POS))) == 0x00)
#define     testBit(REG,POS)    ((REG) & (1L << (POS)))

#define     clrBit(x,y)         (x) &= ~(1L << (y))
#define     setBit(x,y)         (x) |= (1L << (y))

#define ALIGN_4BYTE(size)   ((size+3)&0xfffffffc)

#if CPU_ENDIAN == BIG_ENDIAN
#define __cpu_u16(lo, hi)  ((lo)|((hi)<<8))
#elif CPU_ENDIAN == LITTLE_ENDIAN
#define __cpu_u16(lo, hi)  ((hi)|((lo)<<8))
#endif

#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) > (b) ? (a) : (b))

#define ARRAY_SIZE(array)  (sizeof(array)/sizeof(array[0]))

#define SFR(sfr, start, len, dat) \
(sfr = (sfr & ~((~(0xffffffff << (len))) << (start))) | \
(((dat) & (~(0xffffffff << (len)))) << (start)))

#define offsetof(type, memb) \
((unsigned long)(&((type *)0)->memb))

#define container_of(ptr, type, memb) \
((type *)((char *)(ptr) - offsetof(type, memb)))

#define READ_BIG_U16(a)   ((*((u8*)(a)) <<8)  + *((u8*)(a)+1))
#define READ_BIG_U32(a)   ((*((u8*)(a)) <<24) + (*((u8*)(a)+1)<<16) + (*((u8*)(a)+2)<<8) + *((u8*)(a)+3))