sarmfsw: SMFSW Toolbox (desinged for ARM / compatible with other 8b/16b/32b platforms) 4.0rc
SMFSW collection of miscellaneous functions \& macros (desinged for ARM / compatible with other 8b/16b/32b platforms)
Loading...
Searching...
No Matches
arm_macros.h File Reference

ARM common macros. More...

This graph shows which files directly or indirectly include this file:

Macros

#define Undefined   (-1)
 Undefined value.
 
#define Null   (0)
 Null Value.
 
#define pNull   ((void *) 0)
 Null pointer -> same as NULL in stdlib.h.
 
#define charNull   ('\0')
 Null Char.
 
#define True   true
 True alias for true
 
#define False   false
 False alias for false
 
#define TRUE   true
 TRUE alias for true
 
#define FALSE   false
 FALSE alias for false
 
#define UNUSED(X)
 Unused parameter X (to avoid gcc/g++ warnings)
 
#define UNUSED_RET   (void)
 Explicitly ignore function return (MISRA compliance)
 
#define _x_   UNUSED_RET
 Short alias for UNUSED_RET.
 
#define GET_BITS(v, b)
 Get bits from mask b of variable v.
 
#define SET_BITS(v, b)
 Set bits b of variable v.
 
#define CLR_BITS(v, b)
 Clear bits b of variable v.
 
#define INV_BITS(v, b)
 Invert bits b of variable v.
 
#define SET_BITS_VAL(v, m, s)
 Set variable v to set value s with mask m.
 
#define TEST_BITS_SET(v, b)
 Test variable v with bits b (all set)
 
#define TEST_BITS_CLR(v, b)
 Test variable v with bits b (all cleared)
 
#define TEST_BITS_VAL(v, b)
 Test variable v with bits b (all set)
 
#define TEST_MASK_VAL(v, m, r)
 Test variable v with bits mask m is equal to r.
 
#define LSHIFT(v, n)
 Shift v n bits left.
 
#define RSHIFT(v, n)
 Shift v n bits right.
 
#define LSHIFT8(v, n)
 Shift v n bits left (up to 7b)
 
#define RSHIFT8(v, n)
 Shift v n bits right (up to 7b)
 
#define LSHIFT16(v, n)
 Shift v n bits left (up to 15b)
 
#define RSHIFT16(v, n)
 Shift v n bits right (up to 15b)
 
#define LSHIFT32(v, n)
 Shift v n bits left (up to 31b)
 
#define RSHIFT32(v, n)
 Shift v n bits right (up to 31b)
 
#define LSHIFT64(v, n)
 Shift v n bits left (up to 63b)
 
#define RSHIFT64(v, n)
 Shift v n bits right (up to 63b)
 
#define MAKEWORD(lsb, msb)
 Make WORD from lsb and msb.
 
#define MAKELONG(lsw, msw)
 Make LONG from lsw and msw.
 
#define LOBYTE(w)
 Get BYTE LSB from WORD w.
 
#define HIBYTE(w)
 Get BYTE MSB from WORD w.
 
#define LOWORD(l)
 Get WORD LSW from LONG l.
 
#define HIWORD(l)
 Get WORD MSW from LONG l.
 
#define SWAP_TYPE(a, b, typ)
 Swap two variable type typ a & b.
 
#define SWAP_BYTE(a, b)
 Swap two BYTE a & b.
 
#define SWAP_WORD(a, b)
 Swap two WORD a & b.
 
#define SWAP_DWORD(a, b)
 Swap two DWORD a & b.
 
#define SWAP_LWORD(a, b)
 Swap two LWORD a & b.
 
#define SWAP_FLOAT(a, b)
 Swap two float a & b.
 
#define SWAP_DOUBLE(a, b)
 Swap two double a & b.
 
#define VAL_AT(addr, typ)
 Get the type typ content of address addr.
 
#define SZ_ARRAY(arr)
 Computes the number of array elements in arr.
 
#define SZ_OBJ(obj, typ)
 Computes the number of elements in obj of type typ.
 
#define SZ_TYP_MBR(typ, mbr)
 Computes the size of member mbr in struct type typ.
 
#define TYP_MBR(typ, mbr)
 Get member mbr from a struct type typ.
 
#define TYP_MBR_TYP(typ, mbr)
 Get typeof member mbr from a struct type typ.
 
#define OFFSET_OF(typ, mbr)
 Computes the offset member mbr from struct type typ.
 
#define ROOT_OF(ptr, typ, mbr)
 Computes the address of parent struct typ of ptr from member mbr.
 
#define binEval(exp)
 boolean evaluation of expression exp
 
#define nbinEval(exp)
 complemented boolean evaluation of expression exp
 
#define isNull(p)
 Test is p is null.
 
#define isNotNull(p)
 Test is p is not null.
 
#define isOdd(v)
 Test is v is odd.
 
#define isEven(v)
 Test is v is even.
 
#define min   MIN
 
#define MIN(a, b)
 Returns min value between a and b.
 
#define max   MAX
 
#define MAX(a, b)
 Returns max value between a and b.
 
#define min3   MIN3
 
#define MIN3(a, b, c)
 Returns min value between a, b and c.
 
#define max3   MAX3
 
#define MAX3(a, b, c)
 Returns max value between a, b and c.
 
#define clamp   CLAMP
 
#define CLAMP(v, mn, mx)
 Returns the value between mn and mx from v.
 
#define SCALE_VAL(v, from_min, from_max, to_min, to_max)
 Scale value v from range from_min:from_max to range to_min:to_max.
 
#define SCALE_VAL_T(typ, v, from_min, from_max, to_min, to_max)
 Scale typed typ value v from range from_min:from_max to range to_min:to_max.
 
#define BYTE_TO_PERC(b)
 Converts a BYTE b (0-255) to percent (0-100)
 
#define PERC_TO_BYTE(p)
 Converts a p percentage (0-100) to BYTE (0-255) with max checking.
 
#define M_1_3   (1.0 / 3.0)
 1/3 constant approximation
 
#define M_2_3   (2.0 / 3.0)
 2/3 constant approximation
 

Detailed Description

ARM common macros.

Author
SMFSW
Note
If used CPU only handles single shifts, you may define SINGLE_SHIFT_ONLY_OPCODE at project level (see LSHIFT & RSHIFT)
MISRA C:2012 Deviations
Header scope deviation has been granted for following rules:
Rule-1.2 - Advisory: Language extensions (misra-c2012-1.2)
Rule-20.5 - Advisory: #undef (misra-c2012-20.5)

Macro Definition Documentation

◆ _x_

#define _x_   UNUSED_RET

Short alias for UNUSED_RET.

◆ binEval

#define binEval ( exp)
Value:
((exp) ? true : false)

boolean evaluation of expression exp

◆ BYTE_TO_PERC

#define BYTE_TO_PERC ( b)
Value:
((BYTE) (((((b) >= 255U) ? 255U : (b)) * 100U) / 255U))
uint8_t BYTE
Unsigned Byte typedef (8bits)
Definition arm_typedefs.h:86

Converts a BYTE b (0-255) to percent (0-100)

Warning
This macro doesn't prevent from double evaluation; otherwise it couldn't be used as pre-processing initializer macro

◆ charNull

#define charNull   ('\0')

Null Char.

◆ CLAMP

#define CLAMP ( v,
mn,
mx )
Value:
({ __TYPEOF(v) _v = (v); \
__TYPEOF(mn) _mn = (mn); \
__TYPEOF(mx) _mx = (mx); \
(_v < _mn) ? _mn : ((_v > _mx) ? _mx : _v); })
#define __TYPEOF
typeof keyword alias (
Definition arm_keywords.h:115

Returns the value between mn and mx from v.

◆ clamp

#define clamp   CLAMP

clamp alias for CLAMP

◆ CLR_BITS

#define CLR_BITS ( v,
b )
Value:
((v) &= ~(b))

Clear bits b of variable v.

◆ FALSE

#define FALSE   false

FALSE alias for false

◆ False

#define False   false

False alias for false

◆ GET_BITS

#define GET_BITS ( v,
b )
Value:
((v) & (b))

Get bits from mask b of variable v.

◆ HIBYTE

#define HIBYTE ( w)
Value:
((BYTE) RSHIFT((WORD) (w), 8))
#define RSHIFT(v, n)
Shift v n bits right.
Definition arm_macros.h:130
uint16_t WORD
Unsigned Word typedef (16bits)
Definition arm_typedefs.h:87

Get BYTE MSB from WORD w.

◆ HIWORD

#define HIWORD ( l)
Value:
((WORD) RSHIFT((DWORD) (l), 16))
uint32_t DWORD
Unsigned dWord typedef (32bits)
Definition arm_typedefs.h:88

Get WORD MSW from LONG l.

◆ INV_BITS

#define INV_BITS ( v,
b )
Value:
((v) ^= (b))

Invert bits b of variable v.

◆ isEven

#define isEven ( v)
Value:
(((v) & 1) == 0)

Test is v is even.

MISRA C:2012 Deviations
Local legitimate use derogation authorized for:
Rule-10.4 - Required: same essential type category (misra-c2012-10.4)
Justification: signed or unsigned use will produce the same result.

◆ isNotNull

#define isNotNull ( p)
Value:
((p) != pNull)
#define pNull
Null pointer -> same as NULL in stdlib.h.
Definition arm_macros.h:25

Test is p is not null.

◆ isNull

#define isNull ( p)
Value:
((p) == pNull)

Test is p is null.

◆ isOdd

#define isOdd ( v)
Value:
(((v) & 1) == 1)

Test is v is odd.

MISRA C:2012 Deviations
Local legitimate use derogation authorized for:
Rule-10.4 - Required: same essential type category (misra-c2012-10.4)
Justification: signed or unsigned use will produce the same result.

◆ LOBYTE

#define LOBYTE ( w)
Value:
((BYTE) (w))

Get BYTE LSB from WORD w.

◆ LOWORD

#define LOWORD ( l)
Value:
((WORD) (l))

Get WORD LSW from LONG l.

◆ LSHIFT

#define LSHIFT ( v,
n )
Value:
((v) << (n))

Shift v n bits left.

◆ LSHIFT16

#define LSHIFT16 ( v,
n )
Value:
((WORD) ((WORD) (v) << (n)))

Shift v n bits left (up to 15b)

◆ LSHIFT32

#define LSHIFT32 ( v,
n )
Value:
((DWORD) ((DWORD) (v) << (n)))

Shift v n bits left (up to 31b)

◆ LSHIFT64

#define LSHIFT64 ( v,
n )
Value:
((LWORD) ((LWORD) (v) << (n)))
uint64_t LWORD
Unsigned lWord typedef (64bits)
Definition arm_typedefs.h:89

Shift v n bits left (up to 63b)

◆ LSHIFT8

#define LSHIFT8 ( v,
n )
Value:
((BYTE) ((BYTE) (v) << (n)))

Shift v n bits left (up to 7b)

◆ M_1_3

#define M_1_3   (1.0 / 3.0)

1/3 constant approximation

◆ M_2_3

#define M_2_3   (2.0 / 3.0)

2/3 constant approximation

◆ MAKELONG

#define MAKELONG ( lsw,
msw )
Value:
((DWORD) (((WORD) (lsw)) | LSHIFT(((DWORD) (msw)), 16)))
#define LSHIFT(v, n)
Shift v n bits left.
Definition arm_macros.h:129

Make LONG from lsw and msw.

◆ MAKEWORD

#define MAKEWORD ( lsb,
msb )
Value:
((WORD) (((BYTE) (lsb)) | LSHIFT(((WORD) (msb)), 8)))

Make WORD from lsb and msb.

◆ MAX

#define MAX ( a,
b )
Value:
({ __TYPEOF(a) _a = (a); \
__TYPEOF(b) _b = (b); \
(_a >= _b) ? _a : _b; })

Returns max value between a and b.

◆ max

#define max   MAX

max alias for MAX

◆ MAX3

#define MAX3 ( a,
b,
c )
Value:
({ __TYPEOF(a) _a = (a); \
__TYPEOF(b) _b = (b); \
__TYPEOF(c) _c = (c); \
(_b >= _c) ? ((_a >= _b) ? _a : _b) : ((_a >= _c) ? _a : _c); })

Returns max value between a, b and c.

◆ max3

#define max3   MAX3

max3 alias for MAX3

◆ MIN

#define MIN ( a,
b )
Value:
({ __TYPEOF(a) _a = (a); \
__TYPEOF(b) _b = (b); \
(_a <= _b) ? _a : _b; })

Returns min value between a and b.

◆ min

#define min   MIN

min alias for MIN

◆ MIN3

#define MIN3 ( a,
b,
c )
Value:
({ __TYPEOF(a) _a = (a); \
__TYPEOF(b) _b = (b); \
__TYPEOF(c) _c = (c); \
(_b <= _c) ? ((_a <= _b) ? _a : _b) : ((_a <= _c) ? _a : _c); })

Returns min value between a, b and c.

◆ min3

#define min3   MIN3

min3 alias for MIN3

◆ nbinEval

#define nbinEval ( exp)
Value:
(!binEval(exp))
#define binEval(exp)
boolean evaluation of expression exp
Definition arm_macros.h:243

complemented boolean evaluation of expression exp

◆ Null

#define Null   (0)

Null Value.

◆ OFFSET_OF

#define OFFSET_OF ( typ,
mbr )
Value:
((size_t) &(((typ *)0)->mbr))

Computes the offset member mbr from struct type typ.

MISRA C:2012 Deviations
Local deviation has been granted for following rules:
Rule-11.4 - Advisory: pointer to integer conversion (misra-c2012-11.4)
Local legitimate use derogation authorized for:
Rule-20.7 - Required: Enclosed macro parameters expansion (misra-c2012-20.7)
Justification: typ parameter would be cast instead of type.
Justification: mbr parameter shall be a structure member; will in any way raise an error in case not.

◆ PERC_TO_BYTE

#define PERC_TO_BYTE ( p)
Value:
((BYTE) (((((p) >= 100U) ? 100U : (p)) * 255U) / 100U))

Converts a p percentage (0-100) to BYTE (0-255) with max checking.

Warning
This macro doesn't prevent from double evaluation; otherwise it couldn't be used as pre-processing initializer macro

◆ pNull

#define pNull   ((void *) 0)

Null pointer -> same as NULL in stdlib.h.

◆ ROOT_OF

#define ROOT_OF ( ptr,
typ,
mbr )
Value:
((typ *) (((uint8_t *) (ptr)) - OFFSET_OF(typ, mbr)))
#define OFFSET_OF(typ, mbr)
Computes the offset member mbr from struct type typ.
Definition arm_macros.h:233

Computes the address of parent struct typ of ptr from member mbr.

MISRA C:2012 Deviations
Local legitimate use derogation authorized for:
Rule-20.7 - Required: Enclosed macro parameters expansion (misra-c2012-20.7)
Justification: mbr parameter shall be a structure member; will in any way raise an error in case not.

◆ RSHIFT

#define RSHIFT ( v,
n )
Value:
((v) >> (n))

Shift v n bits right.

◆ RSHIFT16

#define RSHIFT16 ( v,
n )
Value:
((WORD) ((WORD) (v) >> (n)))

Shift v n bits right (up to 15b)

◆ RSHIFT32

#define RSHIFT32 ( v,
n )
Value:
((DWORD) ((DWORD) (v) >> (n)))

Shift v n bits right (up to 31b)

◆ RSHIFT64

#define RSHIFT64 ( v,
n )
Value:
((LWORD) ((LWORD) (v) >> (n)))

Shift v n bits right (up to 63b)

◆ RSHIFT8

#define RSHIFT8 ( v,
n )
Value:
((BYTE) ((BYTE) (v) >> (n)))

Shift v n bits right (up to 7b)

◆ SCALE_VAL

#define SCALE_VAL ( v,
from_min,
from_max,
to_min,
to_max )
Value:
(((((v) - (from_min)) * ((to_max) - (to_min))) / \
((from_max) - (from_min))) + (to_min))

Scale value v from range from_min:from_max to range to_min:to_max.

Warning
SCALE_VAL does not check types and is limited to MCU register size computation, for larger scales, use SCALE_VAL_T or scaleValue instead

◆ SCALE_VAL_T

#define SCALE_VAL_T ( typ,
v,
from_min,
from_max,
to_min,
to_max )
Value:
(((((typ) (v) - (typ) (from_min)) * ((typ) (to_max) - (typ) (to_min))) / \
((typ) (from_max) - (typ) (from_min))) + (typ) (to_min))

Scale typed typ value v from range from_min:from_max to range to_min:to_max.

◆ SET_BITS

#define SET_BITS ( v,
b )
Value:
((v) |= (b))

Set bits b of variable v.

◆ SET_BITS_VAL

#define SET_BITS_VAL ( v,
m,
s )
Value:
((v) = (((v) & ~(m)) | ((s) & (m))))

Set variable v to set value s with mask m.

◆ SWAP_BYTE

#define SWAP_BYTE ( a,
b )
Value:
#define SWAP_TYPE(a, b, typ)
Swap two variable type typ a & b.
Definition arm_macros.h:169

Swap two BYTE a & b.

◆ SWAP_DOUBLE

#define SWAP_DOUBLE ( a,
b )
Value:
SWAP_TYPE(a, b, double)

Swap two double a & b.

◆ SWAP_DWORD

#define SWAP_DWORD ( a,
b )
Value:

Swap two DWORD a & b.

◆ SWAP_FLOAT

#define SWAP_FLOAT ( a,
b )
Value:
SWAP_TYPE(a, b, float)

Swap two float a & b.

◆ SWAP_LWORD

#define SWAP_LWORD ( a,
b )
Value:

Swap two LWORD a & b.

◆ SWAP_TYPE

#define SWAP_TYPE ( a,
b,
typ )
Value:
({ typ c = a; a = b; b = c; })

Swap two variable type typ a & b.

MISRA C:2012 Deviations
Local legitimate use derogation authorized for:
Rule-20.7 - Required: Enclosed macro parameters expansion (misra-c2012-20.7)
Justification: typ parameter would be cast instead of type declaration (thus raising error); other parameters shall be variable names, raising error otherwise is intended.

◆ SWAP_WORD

#define SWAP_WORD ( a,
b )
Value:

Swap two WORD a & b.

◆ SZ_ARRAY

#define SZ_ARRAY ( arr)
Value:
((size_t) (sizeof(arr) / sizeof(arr[0])))

Computes the number of array elements in arr.

MISRA C:2012 Deviations
local derogation authorized for:
Rule-20.7 - Required: Enclosed macro parameters expansion (misra-c2012-20.7)
Justification: Parenthesis doesn't make sense as parameter arr shall be an instance of type; raising error otherwise is intended.

◆ SZ_OBJ

#define SZ_OBJ ( obj,
typ )
Value:
((size_t) (sizeof(obj) / sizeof(typ)))

Computes the number of elements in obj of type typ.

MISRA C:2012 Deviations
Local legitimate use derogation authorized for:
Rule-20.7 - Required: Enclosed macro parameters expansion (misra-c2012-20.7)
Justification: obj parameter can be a type instead of an istance of type.
Justification: typ parameter would be cast instead of type.

◆ SZ_TYP_MBR

#define SZ_TYP_MBR ( typ,
mbr )
Value:
((size_t) sizeof(((typ *)0)->mbr))

Computes the size of member mbr in struct type typ.

MISRA C:2012 Deviations
Local deviation has been granted for following rules:
Rule-11.4 - Advisory: pointer to integer conversion (misra-c2012-11.4)
Local legitimate use derogation authorized for:
Rule-20.7 - Required: Enclosed macro parameters expansion (misra-c2012-20.7)
Justification: typ parameter would be cast instead of type.
Justification: mbr parameter shall be a structure member; will in any way raise an error in case not.

◆ TEST_BITS_CLR

#define TEST_BITS_CLR ( v,
b )
Value:
(((v) & (b)) == 0)

Test variable v with bits b (all cleared)

◆ TEST_BITS_SET

#define TEST_BITS_SET ( v,
b )
Value:
(((v) & (b)) == (b))

Test variable v with bits b (all set)

◆ TEST_BITS_VAL

#define TEST_BITS_VAL ( v,
b )
Value:
TEST_BITS_SET((v), (b))
#define TEST_BITS_SET(v, b)
Test variable v with bits b (all set)
Definition arm_macros.h:59

Test variable v with bits b (all set)

◆ TEST_MASK_VAL

#define TEST_MASK_VAL ( v,
m,
r )
Value:
(((v) & (m)) == (r))

Test variable v with bits mask m is equal to r.

◆ TRUE

#define TRUE   true

TRUE alias for true

◆ True

#define True   true

True alias for true

◆ TYP_MBR

#define TYP_MBR ( typ,
mbr )
Value:
(((typ *)0)->mbr)

Get member mbr from a struct type typ.

MISRA C:2012 Deviations
Local legitimate use derogation authorized for:
Rule-20.7 - Required: Enclosed macro parameters expansion (misra-c2012-20.7)
Justification: typ parameter would be cast instead of type.
Justification: mbr parameter shall be a structure member; will in any way raise an error in case not.

◆ TYP_MBR_TYP

#define TYP_MBR_TYP ( typ,
mbr )
Value:
typeof(TYP_MBR(typ, mbr))
#define TYP_MBR(typ, mbr)
Get member mbr from a struct type typ.
Definition arm_macros.h:217

Get typeof member mbr from a struct type typ.

Warning
use with caution, C language typeof extension works well with native types, yet can be the cause of big (experienced) issues with more complex ones
Note
typeof C language extension should only be used to get a compiler native known type

◆ Undefined

#define Undefined   (-1)

Undefined value.

◆ UNUSED

#define UNUSED ( X)
Value:
(void) (X)

Unused parameter X (to avoid gcc/g++ warnings)

◆ UNUSED_RET

#define UNUSED_RET   (void)

Explicitly ignore function return (MISRA compliance)

◆ VAL_AT

#define VAL_AT ( addr,
typ )
Value:
(*(typ *) (addr))

Get the type typ content of address addr.

MISRA C:2012 Deviations
Local legitimate use derogation authorized for:
Rule-20.7 - Required: Enclosed macro parameters expansion (misra-c2012-20.7)
Justification: typ parameter would be cast instead of type.