我试图指定一些定义来设置和获取寄存器中的位,但它们似乎不起作用。我认为它与添加基数和偏移量有关。当我在单独的函数中指定它们时,它们确实有效但我不想在每次设置寄存器时执行函数调用。
#ifndef COMMON_UTILS_H_
#define COMMON_UTILS_H_
#include <inttypes.h>
#include <stdbool.h>
typedef volatile unsigned int * address;
/* Get the adresse of a register */
#define GET_REG(base, offset) ((address)(base+offset));
/* Get the value of a given register */
#define GET_REG_BIT(base, offset, pos) *((address)base+offset) & (1 << pos);
/* Set bit of given register */
#define SET_REG_BIT(base, offset, pos) *((address)(base+offset)) |= (1 << pos);
/* Clear bit of given register */
#define CLEAR_REG_BIT(base, offset, pos) *((address)(base+offset)) &= ~(1 << pos);
/* Poll register until bit is set */
#define POLL_REG_SET(base, offset, pos) while(*((address)base+offset) & (1 << pos) != 1){};
/* Clear register */
#define CLEAR_REG(base, offset) *((address)base+offset) = 0x0;
/* Get int-value of a specivied range of bits - lsb starting at 0 */
#define GET_BIT_RANGE(x,msb,lsb) (x >> lsb) & ~(~0 << (msb-lsb+1))