PebbleOS
Loading...
Searching...
No Matches
Macros
Bits and bit fields

Bit, mask and register field macros. More...

Macros

#define PBL_BIT(n)   (1U << (n))
 Unsigned 32-bit value with a single bit set.
 
#define PBL_BIT64(n)   (1ULL << (n))
 Unsigned 64-bit value with a single bit set.
 
#define PBL_BIT_MASK(n)   (PBL_BIT(n) - 1U)
 Unsigned 32-bit mask with the n least significant bits set.
 
#define PBL_BIT64_MASK(n)   (PBL_BIT64(n) - 1ULL)
 Unsigned 64-bit mask with the n least significant bits set.
 
#define PBL_GENMASK(h, l)   (((2U << ((h) - (l))) - 1U) << (l))
 Unsigned 32-bit mask with bits l to h set, both included.
 
#define PBL_GENMASK64(h, l)   (((2ULL << ((h) - (l))) - 1ULL) << (l))
 Unsigned 64-bit mask with bits l to h set, both included.
 
#define PBL_LSB_GET(value)   ((value) & -(value))
 Isolate the least significant set bit of a value.
 
#define PBL_FIELD_GET(mask, value)   (((value) & (mask)) / PBL_LSB_GET(mask))
 Extract a field from a register value.
 
#define PBL_FIELD_PREP(mask, value)   (((value) * PBL_LSB_GET(mask)) & (mask))
 Prepare a field value for a register.
 
#define PBL_WRITE_BIT(var, bit, set)
 Set or clear a bit in a variable.
 

Detailed Description

Bit, mask and register field macros.

A field is described by a contiguous mask, usually built with PBL_GENMASK(). PBL_FIELD_PREP() shifts a value into the field and PBL_FIELD_GET() extracts it, so the field position never has to be spelled out. With a constant mask both fold to a plain shift and mask.

#define CTRL1_ODR_MASK PBL_GENMASK(7, 4)
#define CTRL1_ODR_50HZ 0x4U
#define CTRL1_LP_EN PBL_BIT(0)
reg = PBL_FIELD_PREP(CTRL1_ODR_MASK, CTRL1_ODR_50HZ) | CTRL1_LP_EN;
odr = PBL_FIELD_GET(CTRL1_ODR_MASK, reg);
#define PBL_FIELD_GET(mask, value)
Extract a field from a register value.
Definition bits.h:85
#define PBL_FIELD_PREP(mask, value)
Prepare a field value for a register.
Definition bits.h:94

Macro Definition Documentation

◆ PBL_BIT

#define PBL_BIT (   n)    (1U << (n))

Unsigned 32-bit value with a single bit set.

Parameters
nBit position, 0 to 31.

◆ PBL_BIT64

#define PBL_BIT64 (   n)    (1ULL << (n))

Unsigned 64-bit value with a single bit set.

Parameters
nBit position, 0 to 63.

◆ PBL_BIT64_MASK

#define PBL_BIT64_MASK (   n)    (PBL_BIT64(n) - 1ULL)

Unsigned 64-bit mask with the n least significant bits set.

Parameters
nNumber of bits, 0 to 63.

◆ PBL_BIT_MASK

#define PBL_BIT_MASK (   n)    (PBL_BIT(n) - 1U)

Unsigned 32-bit mask with the n least significant bits set.

Parameters
nNumber of bits, 0 to 31.

◆ PBL_FIELD_GET

#define PBL_FIELD_GET (   mask,
  value 
)    (((value) & (mask)) / PBL_LSB_GET(mask))

Extract a field from a register value.

Parameters
maskContiguous, non-zero field mask, evaluated twice.
valueRegister value.
Returns
Field value, shifted down to bit 0.

◆ PBL_FIELD_PREP

#define PBL_FIELD_PREP (   mask,
  value 
)    (((value) * PBL_LSB_GET(mask)) & (mask))

Prepare a field value for a register.

Parameters
maskContiguous, non-zero field mask, evaluated twice.
valueField value; bits that do not fit the field are dropped.
Returns
value shifted into the field position and masked.

◆ PBL_GENMASK

#define PBL_GENMASK (   h,
  l 
)    (((2U << ((h) - (l))) - 1U) << (l))

Unsigned 32-bit mask with bits l to h set, both included.

Parameters
hMost significant bit of the mask, 0 to 31.
lLeast significant bit of the mask, at most h.

◆ PBL_GENMASK64

#define PBL_GENMASK64 (   h,
  l 
)    (((2ULL << ((h) - (l))) - 1ULL) << (l))

Unsigned 64-bit mask with bits l to h set, both included.

Parameters
hMost significant bit of the mask, 0 to 63.
lLeast significant bit of the mask, at most h.

◆ PBL_LSB_GET

#define PBL_LSB_GET (   value)    ((value) & -(value))

Isolate the least significant set bit of a value.

Parameters
valueUnsigned value, evaluated twice.
Returns
value with every bit cleared but its lowest set one, or 0 if value is 0.

◆ PBL_WRITE_BIT

#define PBL_WRITE_BIT (   var,
  bit,
  set 
)
Value:
((var) = \
(set) ? ((var) | ((__typeof__(var))1 << (bit))) : ((var) & ~((__typeof__(var))1 << (bit))))

Set or clear a bit in a variable.

The mask is built in the type of var, so other bits are preserved whatever its width.

Parameters
varUnsigned integer variable to update, evaluated more than once.
bitBit position, less than the width of var.
setSet the bit if true, clear it otherwise.