Re: [PATCH 6/8 v2] staging: pi433: use defines for shifting register values

From: Marcus Wolf
Date: Wed Dec 13 2017 - 12:15:24 EST




Am 13.12.2017 um 18:55 schrieb Valentin Vidic:
Avoid shifting by magic numbers and use defines instead.

Signed-off-by: Valentin Vidic <Valentin.Vidic@xxxxxxxxx>
---
v2: - drop change for SHIFT_DATAMODUL_MODULATION_TYPE
- move shifting to the header file

drivers/staging/pi433/rf69.c | 16 ++++++++--------
drivers/staging/pi433/rf69_registers.h | 8 ++++++++
2 files changed, 16 insertions(+), 8 deletions(-)

diff --git a/drivers/staging/pi433/rf69.c b/drivers/staging/pi433/rf69.c
index f77ecd60f43a..ce259b4f0b0e 100644
--- a/drivers/staging/pi433/rf69.c
+++ b/drivers/staging/pi433/rf69.c
@@ -340,14 +340,14 @@ enum lnaGain rf69_get_lna_gain(struct spi_device *spi)
currentValue = rf69_read_reg(spi, REG_LNA);
- switch (currentValue & MASK_LNA_CURRENT_GAIN >> 3) { // improvement: change 3 to define
- case LNA_GAIN_AUTO: return automatic;
- case LNA_GAIN_MAX: return max;
- case LNA_GAIN_MAX_MINUS_6: return maxMinus6;
- case LNA_GAIN_MAX_MINUS_12: return maxMinus12;
- case LNA_GAIN_MAX_MINUS_24: return maxMinus24;
- case LNA_GAIN_MAX_MINUS_36: return maxMinus36;
- case LNA_GAIN_MAX_MINUS_48: return maxMinus48;
+ switch (currentValue & MASK_LNA_CURRENT_GAIN) {
+ case LNA_GAIN_AUTO_SHIFTED: return automatic;
+ case LNA_GAIN_MAX_SHIFTED: return max;
+ case LNA_GAIN_MAX_MINUS_6_SHIFTED: return maxMinus6;
+ case LNA_GAIN_MAX_MINUS_12_SHIFTED: return maxMinus12;
+ case LNA_GAIN_MAX_MINUS_24_SHIFTED: return maxMinus24;
+ case LNA_GAIN_MAX_MINUS_36_SHIFTED: return maxMinus36;
+ case LNA_GAIN_MAX_MINUS_48_SHIFTED: return maxMinus48;
default: return undefined;
}
}
diff --git a/drivers/staging/pi433/rf69_registers.h b/drivers/staging/pi433/rf69_registers.h
index d23b8b668ef5..6329bbf9e499 100644
--- a/drivers/staging/pi433/rf69_registers.h
+++ b/drivers/staging/pi433/rf69_registers.h
@@ -237,6 +237,7 @@
#define MASK_LNA_ZIN 0x80
#define MASK_LNA_CURRENT_GAIN 0x38
#define MASK_LNA_GAIN 0x07
+#define SHIFT_LNA_CURRENT_GAIN 3
#define LNA_GAIN_AUTO 0x00 /* default */
#define LNA_GAIN_MAX 0x01
@@ -246,6 +247,13 @@
#define LNA_GAIN_MAX_MINUS_36 0x05
#define LNA_GAIN_MAX_MINUS_48 0x06
+#define LNA_GAIN_AUTO_SHIFTED (LNA_GAIN_AUTO << SHIFT_LNA_CURRENT_GAIN)
+#define LNA_GAIN_MAX_SHIFTED (LNA_GAIN_MAX << SHIFT_LNA_CURRENT_GAIN)
+#define LNA_GAIN_MAX_MINUS_6_SHIFTED (LNA_GAIN_MAX_MINUS_6 << SHIFT_LNA_CURRENT_GAIN)
+#define LNA_GAIN_MAX_MINUS_12_SHIFTED (LNA_GAIN_MAX_MINUS_12 << SHIFT_LNA_CURRENT_GAIN)
+#define LNA_GAIN_MAX_MINUS_24_SHIFTED (LNA_GAIN_MAX_MINUS_24 << SHIFT_LNA_CURRENT_GAIN)
+#define LNA_GAIN_MAX_MINUS_36_SHIFTED (LNA_GAIN_MAX_MINUS_36 << SHIFT_LNA_CURRENT_GAIN)
+#define LNA_GAIN_MAX_MINUS_48_SHIFTED (LNA_GAIN_MAX_MINUS_48 << SHIFT_LNA_CURRENT_GAIN)
/* RegRxBw (0x19) and RegAfcBw (0x1A) */
#define MASK_BW_DCC_FREQ 0xE0


Hi Valentin,

nice :-)

Name is a bit strange, since it's not really shifted. If you have a look at the datasheet (RegLNA, page 68), there are two sections "by chance" both using the max, -6, -12 ...
But auto is not needed for current gain (was already bad in my old implementation!), since the current gain will always report a real value, never auto.
So maybe it is a little(!) better not to derive the "current" defines from the "select" defines and use names like LNA_GAIN_MAX_SELECT and LNA_GAIN_MAX_CURRENT.

Never the less - I like patch 6/8 v2 a lot more, than the original one :-)

Thank you very much for your help,

Marcus