Saturday, December 21, 2013

Bit operation

於system driver中,時常遇到bit的操作...
比方說要將某bit設成1或0
或這要看看某些bit有無on/off
可以以以下方式操作
#define BIT0 0x00000001
#define BIT1 0x00000002
#define BIT2 0x00000004
#define BIT3 0x00000008
#define BIT4 0x00000010
#define BIT5 0x00000020
#define BIT6 0x00000040
#define BIT7 0x00000080

#define REG_BASE 0x80000000

typedef unsigned int u32;

#define REG_BIT_SET(reg, bit) \
   (*(volatile u32 *)(REG_BASE + reg) |= (bit))

#define REG_BIT_GET(reg, bit) \
   (*(volatile u32 *)(REG_BASE + reg) & (bit))

#define REG_BIT_CLR(reg, bit) \
   (*(volatile u32 *)(REG_BASE + reg) &= ~(bit))

No comments:

Post a Comment