px-fwlib 0.10.0
Cross-platform embedded library and documentation for 8/16/32-bit microcontrollers generated with Doxygen 1.9.2
px_defs.h : Common definitions

Description

Definition of standard types, boolean, scope and utility macros.

File:

Macros

#define NULL   0
 NULL pointer. More...
 

Minimum and maximum of standard types

#define PX_U8_MIN   0x00
 Minimum of unsigned 8-bit value. More...
 
#define PX_U8_MAX   0xff
 Maximum of unsigned 8-bit value. More...
 
#define PX_U16_MIN   0x0000
 Minimum of unsigned 16-bit value. More...
 
#define PX_U16_MAX   0xffff
 Maximum of unsigned 16-bit value. More...
 
#define PX_U32_MIN   0x00000000
 Minimum of unsigned 32-bit value. More...
 
#define PX_U32_MAX   0xffffffff
 Maximum of unsigned 32-bit value. More...
 
#define PX_S8_MIN   (-0x80)
 Minimum of signed 8-bit value. More...
 
#define PX_S8_MAX   0x7f
 Maximum of signed 8-bit value. More...
 
#define PX_S16_MIN   (-0x8000)
 Minimum of signed 16-bit value. More...
 
#define PX_S16_MAX   0x7fff
 Maximum of signed 16-bit value. More...
 
#define PX_S32_MIN   (-0x80000000)
 Minimum of signed 32-bit value. More...
 
#define PX_S32_MAX   0x7fffffff
 Maximum of signed 32-bit value. More...
 

Type size and sign macros

Source: Catching Integer Overflows in C

#define PX_TYPE_IS_SIGNED(type)   ((type) - 1 < 1)
 Test if a type is signed or unsigned. More...
 
#define PX_MIN_OF_TYPE(type)   (PX_TYPE_IS_SIGNED(type) ? __PX_MIN_SIGNED(type) : (type)0)
 Return the minimum of a type. More...
 
#define PX_MAX_OF_TYPE(type)   ((type) ~PX_MIN_OF_TYPE(type))
 Return the maximum of a type. More...
 

Concatenation macros

#define _PX_CONCAT(x, y)   x ## y
 
#define PX_CONCAT(x, y)   _PX_CONCAT(x, y)
 Recursive token concatenation macro. More...
 

Stringify macros

#define _PX_STRINGIFY(x)   #x
 
#define PX_STRINGIFY(x)   _PX_STRINGIFY(x)
 Recursive stringify macro. More...
 

Bit manipulation macros useful for example to manipulate GPIO pins

#define PX_BIT_SET_HI(var, bit)   do { (var) |= (1ul << (bit)); } while(0)
 Set a bit (1) More...
 
#define PX_BIT_SET_LO(var, bit)   do { (var) &= ~(1ul << (bit)); } while(0)
 Clear a bit (0) More...
 
#define PX_BIT_TOGGLE(var, bit)   do { (var) ^= (1ul << (bit)); } while(0)
 Toggle a bit. More...
 
#define PX_BIT_IS_HI(var, bit)   (((var) & (1ul << (bit))) != 0)
 Test if a bit is set (1?) More...
 
#define PX_BIT_IS_LO(var, bit)   (((var) & (1ul << (bit))) == 0)
 Test if a bit is cleared (0?) More...
 
#define PX_WAIT_UNTIL_BIT_IS_HI(var, bit)   do { ; } while(PX_BIT_IS_LO((var), (bit)))
 Wait until a bit is set. More...
 
#define PX_WAIT_UNTIL_BIT_IS_LO(var, bit)   do { ; } while(PX_BIT_IS_HI((var), (bit)))
 Wait until a bit is cleared. More...
 

Bit mask macros

#define PX_BIT_MASK_SET_LO(var, bit_mask, shift)    do { (var) &= ~((bit_mask) << (shift)); } while(0)
 Clear bits (set bits to 0) More...
 
#define PX_BIT_MASK_SET_HI(var, bit_mask, shift)    do { (var) |= ((bit_mask) << (shift)); } while(0)
 Set bits (set bits to 1) More...
 
#define PX_BIT_MASK_SET_VAL(var, bit_mask, val, shift)    do { (var) = ((var) & (~((bit_mask) << (shift)))) | ((val) << (shift)); } while(0)
 Mask bits (set to 0) and then set to value. More...
 

Byte extraction macros

#define PX_U8_HI4(data)   ((uint8_t)(((data) >> 4) & 0x0f))
 Extract the high 4 bits of a 8-bit value. More...
 
#define PX_U8_LO4(data)   ((uint8_t)((data) & 0x0f))
 Extract the low 4 bits of a 8-bit value. More...
 
#define PX_U16_HI8(data)   ((uint8_t)(((data) >> 8) & 0xff))
 Extract the high 8 bits of a 16-bit value (Most Significant Byte) More...
 
#define PX_U16_LO8(data)   ((uint8_t)((data) & 0xff))
 Extract the low 8 bits of a 16-bit value (Least Significant Byte) More...
 
#define PX_U32_HI8(data)   ((uint8_t)(((data) >> 24) & 0xff))
 Extract the high 8 bits (bits 31..24) of a 32-bit value. More...
 
#define PX_U32_MH8(data)   ((uint8_t)(((data) >> 16) & 0xff))
 Extract the middle high 8 bits (bits 23..16) of a 32-bit value. More...
 
#define PX_U32_ML8(data)   ((uint8_t)(((data) >> 8) & 0xff))
 Extract the middle low 8 bits (bits 15..8) of a 32-bit value. More...
 
#define PX_U32_LO8(data)   ((uint8_t)((data) & 0xff))
 Extract the low 8 bits (bits 7..0) of a 32-bit value. More...
 
#define PX_U32_HI16(data)   ((uint16_t)(((data) >> 16) & 0xffff))
 Extract the high 16 bits (bits 31..16) of a 32-bit value. More...
 
#define PX_U32_LO16(data)   ((uint16_t)((data) & 0xffff))
 Extract the low 16 bits (bits 15..0) of a 32-bit value. More...
 

Bit concatenation macros

#define PX_U8_CONCAT_U1(b7, b6, b5, b4, b3, b2, b1, b0)
 Concatenate 8 bits to form 8-bit value. More...
 

Byte concatenation macros

#define PX_U8_CONCAT_U4(hi4, lo4)
 Concatenate 2 x 4 bits to form 8-bit value. More...
 
#define PX_U16_CONCAT_U8(hi8, lo8)
 Concatenate 2 x 8 bits to form 16-bit value. More...
 
#define PX_U32_CONCAT_U8(hi8, mh8, ml8, lo8)
 Concatenate 4 x 8 bits to form 32-bit value. More...
 

General utility macros

#define PX_UDIV_ROUND(dividend, divisor)   (((dividend) + ((divisor) / 2)) / (divisor))
 Calculate unsigned division with rounding to nearest integer value. More...
 
#define PX_UDIV_ROUNDUP(dividend, divisor)   (((dividend) + ((divisor) - 1)) / (divisor))
 Calculate unsigned division with rounding to nearest integer value. More...
 
#define PX_SDIV_ROUND(dividend, divisor)   (((dividend < 0) ^ (divisor < 0)) ? (((dividend) - ((divisor) / 2)) / (divisor)) : (((dividend) + ((divisor) / 2)) / (divisor)))
 Calculate signed division with rounding to nearest integer value. More...
 
#define PX_SIZEOF_ARRAY(array)   (sizeof(array) / sizeof((array)[0]))
 Calculate the number of items in an array. More...
 
#define PX_VAL_IS_PWR_OF_TWO(value)   (((value) & ((value) - 1)) == 0)
 Is value a power of two (1, 2, 4, 8, 16, ...)? More...
 
#define PX_SWAP(type_t, i, j)   do { type_t _k = i; i = j; j = _k; } while (0)
 Swap the value of two variables. More...
 
#define PX_MAX(i, j)   (i > j ? i : j)
 Maximum of two values. More...
 
#define PX_MIN(i, j)   (i < j ? i : j)
 Minimum of two values. More...
 

Macro Definition Documentation

◆ NULL

#define NULL   0

NULL pointer.

Definition at line 49 of file px_defs.h.

◆ PX_U8_MIN

#define PX_U8_MIN   0x00

Minimum of unsigned 8-bit value.

Definition at line 82 of file px_defs.h.

◆ PX_U8_MAX

#define PX_U8_MAX   0xff

Maximum of unsigned 8-bit value.

Definition at line 83 of file px_defs.h.

◆ PX_U16_MIN

#define PX_U16_MIN   0x0000

Minimum of unsigned 16-bit value.

Definition at line 84 of file px_defs.h.

◆ PX_U16_MAX

#define PX_U16_MAX   0xffff

Maximum of unsigned 16-bit value.

Definition at line 85 of file px_defs.h.

◆ PX_U32_MIN

#define PX_U32_MIN   0x00000000

Minimum of unsigned 32-bit value.

Definition at line 86 of file px_defs.h.

◆ PX_U32_MAX

#define PX_U32_MAX   0xffffffff

Maximum of unsigned 32-bit value.

Definition at line 87 of file px_defs.h.

◆ PX_S8_MIN

#define PX_S8_MIN   (-0x80)

Minimum of signed 8-bit value.

Definition at line 88 of file px_defs.h.

◆ PX_S8_MAX

#define PX_S8_MAX   0x7f

Maximum of signed 8-bit value.

Definition at line 89 of file px_defs.h.

◆ PX_S16_MIN

#define PX_S16_MIN   (-0x8000)

Minimum of signed 16-bit value.

Definition at line 90 of file px_defs.h.

◆ PX_S16_MAX

#define PX_S16_MAX   0x7fff

Maximum of signed 16-bit value.

Definition at line 91 of file px_defs.h.

◆ PX_S32_MIN

#define PX_S32_MIN   (-0x80000000)

Minimum of signed 32-bit value.

Definition at line 92 of file px_defs.h.

◆ PX_S32_MAX

#define PX_S32_MAX   0x7fffffff

Maximum of signed 32-bit value.

Definition at line 93 of file px_defs.h.

◆ PX_TYPE_IS_SIGNED

#define PX_TYPE_IS_SIGNED (   type)    ((type) - 1 < 1)

Test if a type is signed or unsigned.

Definition at line 109 of file px_defs.h.

◆ PX_MIN_OF_TYPE

#define PX_MIN_OF_TYPE (   type)    (PX_TYPE_IS_SIGNED(type) ? __PX_MIN_SIGNED(type) : (type)0)

Return the minimum of a type.

For an unsigned type it is zero. For a signed type this is the largest negative value

Definition at line 117 of file px_defs.h.

◆ PX_MAX_OF_TYPE

#define PX_MAX_OF_TYPE (   type)    ((type) ~PX_MIN_OF_TYPE(type))

Return the maximum of a type.

Definition at line 120 of file px_defs.h.

◆ PX_CONCAT

#define PX_CONCAT (   x,
 
)    _PX_CONCAT(x, y)

Recursive token concatenation macro.

Example:

#define XY 123456
#define TOKEN1 X
#define TOKEN2 Y
#define TOKEN1_2 PX_CONCAT(TOKEN1, TOKEN2)
int i = TOKEN1_2;

Preprocessor steps:

  1. TOKEN1_2 is replaced with PX_CONCAT(TOKEN1, TOKEN2)
  2. PX_CONCAT(TOKEN1, TOKEN2) is replaced with _PX_CONCAT(X, Y)
  3. _PX_CONCAT(X, Y) is replaced with XY
  4. XY is replaced with 123456

Compiler step:

  1. An integer variable called "i" is created and initialised with 123456

Definition at line 152 of file px_defs.h.

◆ PX_STRINGIFY

#define PX_STRINGIFY (   x)    _PX_STRINGIFY(x)

Recursive stringify macro.

Example:

#define VERSION_MAJOR 2
#define VERSION_MINOR 12
printf("Version " PX_STRINGIFY(VERSION_MAJOR) "." PX_STRINGIFY(VERSION_MINOR));
#define PX_STRINGIFY(x)
Recursive stringify macro.
Definition: px_defs.h:171

Definition at line 171 of file px_defs.h.

◆ PX_BIT_SET_HI

#define PX_BIT_SET_HI (   var,
  bit 
)    do { (var) |= (1ul << (bit)); } while(0)

Set a bit (1)

Definition at line 178 of file px_defs.h.

◆ PX_BIT_SET_LO

#define PX_BIT_SET_LO (   var,
  bit 
)    do { (var) &= ~(1ul << (bit)); } while(0)

Clear a bit (0)

Definition at line 181 of file px_defs.h.

◆ PX_BIT_TOGGLE

#define PX_BIT_TOGGLE (   var,
  bit 
)    do { (var) ^= (1ul << (bit)); } while(0)

Toggle a bit.

Definition at line 184 of file px_defs.h.

◆ PX_BIT_IS_HI

#define PX_BIT_IS_HI (   var,
  bit 
)    (((var) & (1ul << (bit))) != 0)

Test if a bit is set (1?)

Definition at line 187 of file px_defs.h.

◆ PX_BIT_IS_LO

#define PX_BIT_IS_LO (   var,
  bit 
)    (((var) & (1ul << (bit))) == 0)

Test if a bit is cleared (0?)

Definition at line 190 of file px_defs.h.

◆ PX_WAIT_UNTIL_BIT_IS_HI

#define PX_WAIT_UNTIL_BIT_IS_HI (   var,
  bit 
)    do { ; } while(PX_BIT_IS_LO((var), (bit)))

Wait until a bit is set.

Definition at line 193 of file px_defs.h.

◆ PX_WAIT_UNTIL_BIT_IS_LO

#define PX_WAIT_UNTIL_BIT_IS_LO (   var,
  bit 
)    do { ; } while(PX_BIT_IS_HI((var), (bit)))

Wait until a bit is cleared.

Definition at line 196 of file px_defs.h.

◆ PX_BIT_MASK_SET_LO

#define PX_BIT_MASK_SET_LO (   var,
  bit_mask,
  shift 
)     do { (var) &= ~((bit_mask) << (shift)); } while(0)

Clear bits (set bits to 0)

Definition at line 203 of file px_defs.h.

◆ PX_BIT_MASK_SET_HI

#define PX_BIT_MASK_SET_HI (   var,
  bit_mask,
  shift 
)     do { (var) |= ((bit_mask) << (shift)); } while(0)

Set bits (set bits to 1)

Definition at line 207 of file px_defs.h.

◆ PX_BIT_MASK_SET_VAL

#define PX_BIT_MASK_SET_VAL (   var,
  bit_mask,
  val,
  shift 
)     do { (var) = ((var) & (~((bit_mask) << (shift)))) | ((val) << (shift)); } while(0)

Mask bits (set to 0) and then set to value.

Definition at line 211 of file px_defs.h.

◆ PX_U8_HI4

#define PX_U8_HI4 (   data)    ((uint8_t)(((data) >> 4) & 0x0f))

Extract the high 4 bits of a 8-bit value.

Definition at line 219 of file px_defs.h.

◆ PX_U8_LO4

#define PX_U8_LO4 (   data)    ((uint8_t)((data) & 0x0f))

Extract the low 4 bits of a 8-bit value.

Definition at line 222 of file px_defs.h.

◆ PX_U16_HI8

#define PX_U16_HI8 (   data)    ((uint8_t)(((data) >> 8) & 0xff))

Extract the high 8 bits of a 16-bit value (Most Significant Byte)

Definition at line 225 of file px_defs.h.

◆ PX_U16_LO8

#define PX_U16_LO8 (   data)    ((uint8_t)((data) & 0xff))

Extract the low 8 bits of a 16-bit value (Least Significant Byte)

Definition at line 228 of file px_defs.h.

◆ PX_U32_HI8

#define PX_U32_HI8 (   data)    ((uint8_t)(((data) >> 24) & 0xff))

Extract the high 8 bits (bits 31..24) of a 32-bit value.

Definition at line 231 of file px_defs.h.

◆ PX_U32_MH8

#define PX_U32_MH8 (   data)    ((uint8_t)(((data) >> 16) & 0xff))

Extract the middle high 8 bits (bits 23..16) of a 32-bit value.

Definition at line 234 of file px_defs.h.

◆ PX_U32_ML8

#define PX_U32_ML8 (   data)    ((uint8_t)(((data) >> 8) & 0xff))

Extract the middle low 8 bits (bits 15..8) of a 32-bit value.

Definition at line 237 of file px_defs.h.

◆ PX_U32_LO8

#define PX_U32_LO8 (   data)    ((uint8_t)((data) & 0xff))

Extract the low 8 bits (bits 7..0) of a 32-bit value.

Definition at line 240 of file px_defs.h.

◆ PX_U32_HI16

#define PX_U32_HI16 (   data)    ((uint16_t)(((data) >> 16) & 0xffff))

Extract the high 16 bits (bits 31..16) of a 32-bit value.

Definition at line 243 of file px_defs.h.

◆ PX_U32_LO16

#define PX_U32_LO16 (   data)    ((uint16_t)((data) & 0xffff))

Extract the low 16 bits (bits 15..0) of a 32-bit value.

Definition at line 246 of file px_defs.h.

◆ PX_U8_CONCAT_U1

#define PX_U8_CONCAT_U1 (   b7,
  b6,
  b5,
  b4,
  b3,
  b2,
  b1,
  b0 
)
Value:
( (((b7) & 0x01) << 7) | (((b6) & 0x01) << 6) | (((b5) & 0x01) << 5) | (((b4) & 0x01) << 4) \
| (((b3) & 0x01) << 3) | (((b2) & 0x01) << 2) | (((b1) & 0x01) << 1) | (((b0) & 0x01) << 0) )

Concatenate 8 bits to form 8-bit value.

Definition at line 253 of file px_defs.h.

◆ PX_U8_CONCAT_U4

#define PX_U8_CONCAT_U4 (   hi4,
  lo4 
)
Value:
( (((uint8_t)((hi4) & 0x0f)) << 4) \
| ((uint8_t)((lo4) & 0x0f)) )

Concatenate 2 x 4 bits to form 8-bit value.

Definition at line 263 of file px_defs.h.

◆ PX_U16_CONCAT_U8

#define PX_U16_CONCAT_U8 (   hi8,
  lo8 
)
Value:
( (((uint16_t)((hi8) & 0xff)) << 8) \
| ((uint16_t)((lo8) & 0xff)) )

Concatenate 2 x 8 bits to form 16-bit value.

Definition at line 268 of file px_defs.h.

◆ PX_U32_CONCAT_U8

#define PX_U32_CONCAT_U8 (   hi8,
  mh8,
  ml8,
  lo8 
)
Value:
( (((uint32_t)((hi8) & 0xff)) << 24) \
| (((uint32_t)((mh8) & 0xff)) << 16) \
| (((uint32_t)((ml8) & 0xff)) << 8 ) \
| ((uint32_t)((lo8) & 0xff)) )

Concatenate 4 x 8 bits to form 32-bit value.

Definition at line 273 of file px_defs.h.

◆ PX_UDIV_ROUND

#define PX_UDIV_ROUND (   dividend,
  divisor 
)    (((dividend) + ((divisor) / 2)) / (divisor))

Calculate unsigned division with rounding to nearest integer value.

Definition at line 284 of file px_defs.h.

◆ PX_UDIV_ROUNDUP

#define PX_UDIV_ROUNDUP (   dividend,
  divisor 
)    (((dividend) + ((divisor) - 1)) / (divisor))

Calculate unsigned division with rounding to nearest integer value.

Definition at line 287 of file px_defs.h.

◆ PX_SDIV_ROUND

#define PX_SDIV_ROUND (   dividend,
  divisor 
)    (((dividend < 0) ^ (divisor < 0)) ? (((dividend) - ((divisor) / 2)) / (divisor)) : (((dividend) + ((divisor) / 2)) / (divisor)))

Calculate signed division with rounding to nearest integer value.

Definition at line 290 of file px_defs.h.

◆ PX_SIZEOF_ARRAY

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

Calculate the number of items in an array.

Definition at line 293 of file px_defs.h.

◆ PX_VAL_IS_PWR_OF_TWO

#define PX_VAL_IS_PWR_OF_TWO (   value)    (((value) & ((value) - 1)) == 0)

Is value a power of two (1, 2, 4, 8, 16, ...)?

Definition at line 296 of file px_defs.h.

◆ PX_SWAP

#define PX_SWAP (   type_t,
  i,
 
)    do { type_t _k = i; i = j; j = _k; } while (0)

Swap the value of two variables.

Definition at line 299 of file px_defs.h.

◆ PX_MAX

#define PX_MAX (   i,
 
)    (i > j ? i : j)

Maximum of two values.

Definition at line 302 of file px_defs.h.

◆ PX_MIN

#define PX_MIN (   i,
 
)    (i < j ? i : j)

Minimum of two values.

Definition at line 305 of file px_defs.h.