fix RSSI reading

This commit is contained in:
liquidraver
2026-03-19 11:38:45 +01:00
parent 57369b5f9e
commit 6f0fa3db85
@@ -1,5 +1,5 @@
diff --git a/drivers/lora/native/sx126x/sx126x.c b/drivers/lora/native/sx126x/sx126x.c
index 7b0b67f89d5..7154eb074ce 100644
index 7b0b67f89d5..49b0a563138 100644
--- a/drivers/lora/native/sx126x/sx126x.c
+++ b/drivers/lora/native/sx126x/sx126x.c
@@ -9,10 +9,19 @@
@@ -149,7 +149,7 @@ index 7b0b67f89d5..7154eb074ce 100644
}
static int sx126x_get_rx_buffer_status(const struct device *dev,
@@ -295,12 +350,18 @@ static int sx126x_get_packet_status(const struct device *dev,
@@ -295,7 +350,7 @@ static int sx126x_get_packet_status(const struct device *dev,
uint8_t buf[3];
int ret;
@@ -157,20 +157,8 @@ index 7b0b67f89d5..7154eb074ce 100644
+ ret = sx126x_hal_read_cmd(dev, SX126X_CMD_GET_PACKET_STATUS, buf, 3);
if (ret == 0) {
/* RSSI is -value/2 dBm */
- *rssi = -((int16_t)buf[0] >> 1);
+ int16_t pkt_rssi = -((int16_t)buf[0] >> 1);
/* SNR is value/4 dB (signed) */
*snr = ((int8_t)buf[1]) >> 2;
+ /* SignalRssiPkt (byte 2) = LoRa signal RSSI excluding noise.
+ * When SNR < 0 the packet RSSI is dominated by noise —
+ * use the signal-only estimate for a more accurate reading. */
+ int16_t sig_rssi = -((int16_t)buf[2] >> 1);
+
+ *rssi = (*snr < 0 && sig_rssi > pkt_rssi) ? sig_rssi : pkt_rssi;
}
return ret;
@@ -366,6 +427,21 @@ static int sx126x_chip_init(const struct device *dev)
*rssi = -((int16_t)buf[0] >> 1);
@@ -366,6 +421,21 @@ static int sx126x_chip_init(const struct device *dev)
return ret;
}
@@ -192,7 +180,7 @@ index 7b0b67f89d5..7154eb074ce 100644
/* Set packet type to LoRa */
ret = sx126x_set_packet_type(dev, SX126X_PACKET_TYPE_LORA);
if (ret < 0) {
@@ -397,7 +473,7 @@ static void sx126x_dio1_callback(const struct device *dev)
@@ -397,7 +467,7 @@ static void sx126x_dio1_callback(const struct device *dev)
{
struct sx126x_data *data = dev->data;
@@ -201,7 +189,7 @@ index 7b0b67f89d5..7154eb074ce 100644
}
static void sx126x_set_rf_path(const struct device *dev, bool enable, bool tx)
@@ -405,11 +481,107 @@ static void sx126x_set_rf_path(const struct device *dev, bool enable, bool tx)
@@ -405,11 +475,107 @@ static void sx126x_set_rf_path(const struct device *dev, bool enable, bool tx)
const struct sx126x_hal_config *config = dev->config;
sx126x_hal_set_antenna_enable(dev, enable);
@@ -310,7 +298,7 @@ index 7b0b67f89d5..7154eb074ce 100644
static int sx126x_set_sleep(const struct device *dev)
{
struct sx126x_data *data = dev->data;
@@ -521,19 +693,23 @@ static void sx126x_handle_irq_rx_done(const struct device *dev, uint16_t irq_sta
@@ -521,19 +687,23 @@ static void sx126x_handle_irq_rx_done(const struct device *dev, uint16_t irq_sta
/* Handle async callback or signal sync receiver */
if (data->rx_cb != NULL) {
@@ -343,7 +331,7 @@ index 7b0b67f89d5..7154eb074ce 100644
} else {
/* Sync mode */
sx126x_set_sleep(dev);
@@ -653,6 +829,29 @@ static int sx126x_lora_config(const struct device *dev,
@@ -653,6 +823,29 @@ static int sx126x_lora_config(const struct device *dev,
goto out;
}
@@ -373,7 +361,7 @@ index 7b0b67f89d5..7154eb074ce 100644
/* Set sync word */
ret = sx126x_set_sync_word(dev, config->public_network);
if (ret < 0) {
@@ -732,6 +931,29 @@ static int sx126x_lora_send_async(const struct device *dev,
@@ -732,6 +925,29 @@ static int sx126x_lora_send_async(const struct device *dev,
/* Enable antenna and set TX path */
sx126x_set_rf_path(dev, true, true);
@@ -403,7 +391,7 @@ index 7b0b67f89d5..7154eb074ce 100644
/* Start transmission with 10 second timeout */
ret = sx126x_set_tx(dev, 10000);
if (ret < 0) {
@@ -1038,6 +1260,126 @@ static int sx126x_lora_test_cw(const struct device *dev, uint32_t frequency,
@@ -1038,6 +1254,126 @@ static int sx126x_lora_test_cw(const struct device *dev, uint32_t frequency,
return 0;
}
@@ -530,7 +518,7 @@ index 7b0b67f89d5..7154eb074ce 100644
static DEVICE_API(lora, sx126x_lora_api) = {
.config = sx126x_lora_config,
.send = sx126x_lora_send,
@@ -1063,6 +1400,14 @@ static int sx126x_init(const struct device *dev)
@@ -1063,6 +1399,14 @@ static int sx126x_init(const struct device *dev)
data->dev = dev;
atomic_set(&data->state, SX126X_STATE_IDLE);
data->config_valid = false;