From: Gerhard Sittig Date: Mon, 18 Dec 2017 13:50:06 +0000 (+0100) Subject: scopes: improve C preprocessor macro robustness X-Git-Tag: sigrok-firmware-fx2lafw-0.1.6~11 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=fdaf46b9ac162543b9b23e5a0cf95127d6ea49cc;p=sigrok-firmware-fx2lafw.git scopes: improve C preprocessor macro robustness For pin and LED control related #define directives use C language compound statements. This lets call sites use the macros in intended ways (like regular function calls) without needing to worry about their implementation details, or suffering from unexpected behaviour. Strictly speaking the single statement variants don't require the wrapper, but I've added one for improved future maintenance. --- diff --git a/hw/hantek-6022be/fw.c b/hw/hantek-6022be/fw.c index e61a32e..ec88dac 100644 --- a/hw/hantek-6022be/fw.c +++ b/hw/hantek-6022be/fw.c @@ -30,11 +30,11 @@ #define SET_CALIBRATION_PULSE(x) -#define TOGGLE_CALIBRATION_PIN() PA7 = !PA7 +#define TOGGLE_CALIBRATION_PIN() do { PA7 = !PA7; } while (0) -#define LED_CLEAR() PC0 = 1; PC1 = 1; -#define LED_GREEN() PC0 = 1; PC1 = 0; -#define LED_RED() PC0 = 0; PC1 = 1; +#define LED_CLEAR() do { PC0 = 1; PC1 = 1; } while (0) +#define LED_GREEN() do { PC0 = 1; PC1 = 0; } while (0) +#define LED_RED() do { PC0 = 0; PC1 = 1; } while (0) #define TIMER2_VAL 500 diff --git a/hw/hantek-6022bl/fw.c b/hw/hantek-6022bl/fw.c index b2ac501..a05f4cb 100644 --- a/hw/hantek-6022bl/fw.c +++ b/hw/hantek-6022bl/fw.c @@ -24,17 +24,17 @@ #include #include -#define SET_ANALOG_MODE() PA7 = 1 +#define SET_ANALOG_MODE() do { PA7 = 1; } while (0) #define SET_COUPLING(x) #define SET_CALIBRATION_PULSE(x) -#define TOGGLE_CALIBRATION_PIN() PC2 = !PC2 +#define TOGGLE_CALIBRATION_PIN() do { PC2 = !PC2; } while (0) -#define LED_CLEAR() PC0 = 1; PC1 = 1; -#define LED_GREEN() PC0 = 1; PC1 = 0; -#define LED_RED() PC0 = 0; PC1 = 1; +#define LED_CLEAR() do { PC0 = 1; PC1 = 1; } while (0) +#define LED_GREEN() do { PC0 = 1; PC1 = 0; } while (0) +#define LED_RED() do { PC0 = 0; PC1 = 1; } while (0) #define TIMER2_VAL 500 diff --git a/hw/sainsmart-dds120/fw.c b/hw/sainsmart-dds120/fw.c index d67e7c9..64d88dd 100644 --- a/hw/sainsmart-dds120/fw.c +++ b/hw/sainsmart-dds120/fw.c @@ -24,14 +24,14 @@ #include #include -#define SET_ANALOG_MODE() PA7 = 1 +#define SET_ANALOG_MODE() do { PA7 = 1; } while (0) #define SET_COUPLING(x) set_coupling(x) #define SET_CALIBRATION_PULSE(x) set_calibration_pulse(x) /* Note: There's no PE2 as IOE is not bit-addressable (see TRM 15.2). */ -#define TOGGLE_CALIBRATION_PIN() IOE = IOE ^ 0x04 +#define TOGGLE_CALIBRATION_PIN() do { IOE = IOE ^ 0x04; } while (0) #define LED_CLEAR() NOP #define LED_GREEN() NOP