Skip to contentSkip to conversation

staging: pi433: fix frequency deviation check

PATCH Superseded Paulo Miguel Almeida 3 participants 5 messages Jan 3, 2022 Staging

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 staging: pi433: fix frequency deviation check

rf69 datasheet states that frequency deviation must exceed 600 Hz but also that frequency deviation + (bitrate / 2) should be less than equal to 500 kHz to ensure proper modulation.

This patch validates that both conditions are met so RF intersymbol interference is "avoided" where possible.

---
 drivers/staging/pi433/rf69.c | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)
drivers/staging/pi433/rf69.c +16 -4
index e62e61ef4d27..cc22915fd489 100644
@@ -255,17 +255,29 @@ int rf69_set_deviation(struct spi_device *spi, u32 deviation)
 	int retval;
 	u64 f_reg;
 	u64 f_step;
+	u32 bit_rate_reg;
+	u32 bit_rate;
 	u8 msb;
 	u8 lsb;
 	u64 factor = 1000000; // to improve precision of calculation
 
-	// TODO: Dependency to bitrate
-	if (deviation < 600 || deviation > 500000) {
-		dev_dbg(&spi->dev, "set_deviation: illegal input param");
+	// calculate bit rate
+	bit_rate_reg = rf69_read_reg(spi, REG_BITRATE_MSB) << 8;
+	bit_rate_reg |= rf69_read_reg(spi, REG_BITRATE_LSB);
+	bit_rate = F_OSC / bit_rate_reg;
+
+	/*
+	 * frequency deviation must exceed 600 Hz and but not exceed
+	 * 500kHz when taking bitrate dependency into consideration
+	 * to ensure proper modulation
+	 */
+	if (deviation < 600 || (deviation + (bit_rate / 2)) > 500000) {
+		dev_dbg(&spi->dev,
+			"set_deviation: illegal input param: %u", deviation);
 		return -EINVAL;
 	}
 
-	// calculat f step
+	// calculate f step
 	f_step = F_OSC * factor;
 	do_div(f_step, 524288); //  524288 = 2^19
 
Signature · 1 line
2.25.4
#1 lore raw

Hello Paulo,

thanks for your patch.

Quoted 34 lines · Paulo Miguel Almeida

rf69 datasheet states that frequency deviation must exceed 600 Hz but also that frequency deviation + (bitrate / 2) should be less than equal to 500 kHz to ensure proper modulation.

This patch validates that both conditions are met so RF intersymbol interference is "avoided" where possible.

---
 drivers/staging/pi433/rf69.c | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)
drivers/staging/pi433/rf69.c +7 -3
index e62e61ef4d27..cc22915fd489 100644
@@ -255,17 +255,29 @@ int rf69_set_deviation(struct spi_device *spi, u32 deviation)
 	int retval;
 	u64 f_reg;
 	u64 f_step;
+	u32 bit_rate_reg;
+	u32 bit_rate;
 	u8 msb;
 	u8 lsb;
 	u64 factor = 1000000; // to improve precision of calculation
 
-	// TODO: Dependency to bitrate
-	if (deviation < 600 || deviation > 500000) {
-		dev_dbg(&spi->dev, "set_deviation: illegal input param");
+	// calculate bit rate
+	bit_rate_reg = rf69_read_reg(spi, REG_BITRATE_MSB) << 8;
+	bit_rate_reg |= rf69_read_reg(spi, REG_BITRATE_LSB);
+	bit_rate = F_OSC / bit_rate_reg;
+

drivers/staging/pi433/rf69.c int rf69_set_deviation(struct spi_device *spi, u32 deviation) · line 278

Quoted 10 lines
drivers/staging/pi433/rf69.c +8 -0
+	/*
+	 * frequency deviation must exceed 600 Hz and but not exceed
+	 * 500kHz when taking bitrate dependency into consideration
+	 * to ensure proper modulation
+	 */
+	if (deviation < 600 || (deviation + (bit_rate / 2)) > 500000) {
+		dev_dbg(&spi->dev,
+			"set_deviation: illegal input param: %u", deviation);
 		return -EINVAL;
 	}

this should go in a separate patch IMO

Quoted 10 lines
drivers/staging/pi433/rf69.c +1 -1
-	// calculat f step
+	// calculate f step
 	f_step = F_OSC * factor;
 	do_div(f_step, 524288); //  524288 = 2^19
 
Signature · 1 line
2.25.4

thank you,

fabio

maintainer
#2 lore raw
In reply to Paulo Miguel Almeida
Quoted 37 lines · Paulo Miguel Almeida

rf69 datasheet states that frequency deviation must exceed 600 Hz but also that frequency deviation + (bitrate / 2) should be less than equal to 500 kHz to ensure proper modulation.

This patch validates that both conditions are met so RF intersymbol interference is "avoided" where possible.

---
 drivers/staging/pi433/rf69.c | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)
drivers/staging/pi433/rf69.c +10 -3
index e62e61ef4d27..cc22915fd489 100644
@@ -255,17 +255,29 @@ int rf69_set_deviation(struct spi_device *spi, u32 deviation)
 	int retval;
 	u64 f_reg;
 	u64 f_step;
+	u32 bit_rate_reg;
+	u32 bit_rate;
 	u8 msb;
 	u8 lsb;
 	u64 factor = 1000000; // to improve precision of calculation
 
-	// TODO: Dependency to bitrate
-	if (deviation < 600 || deviation > 500000) {
-		dev_dbg(&spi->dev, "set_deviation: illegal input param");
+	// calculate bit rate
+	bit_rate_reg = rf69_read_reg(spi, REG_BITRATE_MSB) << 8;
+	bit_rate_reg |= rf69_read_reg(spi, REG_BITRATE_LSB);
+	bit_rate = F_OSC / bit_rate_reg;
+
+	/*
+	 * frequency deviation must exceed 600 Hz and but not exceed
+	 * 500kHz when taking bitrate dependency into consideration

drivers/staging/pi433/rf69.c int rf69_set_deviation(struct spi_device *spi, u32 deviation) · line 280

Quoted 10 lines
drivers/staging/pi433/rf69.c +6 -1
+	 * to ensure proper modulation
+	 */
+	if (deviation < 600 || (deviation + (bit_rate / 2)) > 500000) {
+		dev_dbg(&spi->dev,
+			"set_deviation: illegal input param: %u", deviation);
 		return -EINVAL;
 	}
 
-	// calculat f step
+	// calculate f step

This change should be a separate patch.

thanks,

greg k-h

#3 lore raw

PATCH v2 1/1 staging: pi433: fix frequency deviation check

rf69 datasheet states that frequency deviation must exceed 600 Hz but also that frequency deviation + (bitrate / 2) should be less than equal to 500 kHz to ensure proper modulation.

This patch validates that both conditions are met so RF intersymbol interference is less likely to happen due to misconfiguration of the uC

---
v2: remove typo correction from this patch - Req Greg k-h, Fabio A.
v1: https://lore.kernel.org/lkml/[email protected]/

---
 drivers/staging/pi433/rf69.c | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)
drivers/staging/pi433/rf69.c +15 -3
index e62e61ef4d27..d64df072d8e8 100644
@@ -255,13 +255,25 @@ int rf69_set_deviation(struct spi_device *spi, u32 deviation)
 	int retval;
 	u64 f_reg;
 	u64 f_step;
+	u32 bit_rate_reg;
+	u32 bit_rate;
 	u8 msb;
 	u8 lsb;
 	u64 factor = 1000000; // to improve precision of calculation
 
-	// TODO: Dependency to bitrate
-	if (deviation < 600 || deviation > 500000) {
-		dev_dbg(&spi->dev, "set_deviation: illegal input param");
+	// calculate bit rate
+	bit_rate_reg = rf69_read_reg(spi, REG_BITRATE_MSB) << 8;
+	bit_rate_reg |= rf69_read_reg(spi, REG_BITRATE_LSB);
+	bit_rate = F_OSC / bit_rate_reg;
+
+	/*
+	 * frequency deviation must exceed 600 Hz but not exceed
+	 * 500kHz when taking bitrate dependency into consideration
+	 * to ensure proper modulation
+	 */
+	if (deviation < 600 || (deviation + (bit_rate / 2)) > 500000) {
+		dev_dbg(&spi->dev,
+			"set_deviation: illegal input param: %u", deviation);
 		return -EINVAL;
 	}
 
Signature · 1 line
2.25.4
#4 lore raw
In reply to Greg KH
Quoted 6 lines · Greg KH

drivers/staging/pi433/rf69.c int rf69_set_deviation(struct spi_device *spi, u32 deviation) · line 280

Quoted 2 lines · Paulo Miguel Almeida
drivers/staging/pi433/rf69.c +1 -1
-	// calculat f step
+	// calculate f step

This change should be a separate patch.

Thanks Fabio and Greg for reviewing my patch. I just sent a v2 of this patch with that line untouched.

thanks,

Paulo A.