From b76bf99d7cd8cd82f3f8f8b329abff289d3e0ef4 Mon Sep 17 00:00:00 2001 From: Alex Stewart Date: Tue, 25 Aug 2015 17:07:06 -0700 Subject: [PATCH] Remove unnecessary AND for VAL2FIELD macro Added VAL2FIELD_M / SET_FIELD_M for rare cases when there might actually be some need for it. --- core/include/common_macros.h | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/core/include/common_macros.h b/core/include/common_macros.h index 669203a..b0c7a08 100644 --- a/core/include/common_macros.h +++ b/core/include/common_macros.h @@ -20,12 +20,21 @@ * and shift) constants. Used primarily with ESP8266 register access. */ -#define VAL2FIELD(fieldname, value) (((value) & fieldname##_M) << fieldname##_S) +#define VAL2FIELD(fieldname, value) ((value) << fieldname##_S) #define FIELD2VAL(fieldname, regbits) (((regbits) >> fieldname##_S) & fieldname##_M) #define FIELD_MASK(fieldname) (fieldname##_M << fieldname##_S) #define SET_FIELD(regbits, fieldname, value) (((regbits) & ~FIELD_MASK(fieldname)) | VAL2FIELD(fieldname, value)) +/* VAL2FIELD/SET_FIELD do not normally check to make sure that the passed value + * will fit in the specified field (without clobbering other bits). This makes + * them faster and is usually fine. If you do need to make sure that the value + * will not overflow the field, use VAL2FIELD_M or SET_FIELD_M (which will + * first mask the supplied value to only the allowed number of bits) instead. + */ +#define VAL2FIELD_M(fieldname, value) (((value) & fieldname##_M) << fieldname##_S) +#define SET_FIELD_M(regbits, fieldname, value) (((regbits) & ~FIELD_MASK(fieldname)) | VAL2FIELD_M(fieldname, value)) + /* Use this macro to store constant values in IROM flash instead of having them loaded into rodata (which resides in DRAM)