]> git.tnoah.ca Git - lora-radio.git/commitdiff
Attach packet RSSI and SNR to packet queue entry structs. Disable ISR spinlocks on...
authorMark Qvist <mark@unsigned.io>
Fri, 11 Oct 2024 14:29:17 +0000 (16:29 +0200)
committerMark Qvist <mark@unsigned.io>
Fri, 11 Oct 2024 14:29:17 +0000 (16:29 +0200)
RNode_Firmware.ino
sx126x.cpp
sx126x.h
sx127x.cpp
sx127x.h
sx128x.cpp
sx128x.h

index a8ab5532f475ef215ca0f01d0d0c27cecbb85947..2064abe0c1ec1d43e6451bc5c95bf3cd8d67ead9 100644 (file)
@@ -45,6 +45,8 @@ volatile bool serial_buffering = false;
   #define MODEM_QUEUE_SIZE 4
   typedef struct {
           size_t len;
+          int rssi;
+          int snr_raw;
           uint8_t data[];
   } modem_packet_t;
   static xQueueHandle modem_packet_queue = NULL;
@@ -241,34 +243,31 @@ void lora_receive() {
   }
 }
 
-#if MCU_VARIANT == MCU_ESP32
-  portMUX_TYPE update_lock = portMUX_INITIALIZER_UNLOCKED;
-#endif
-
 inline void kiss_write_packet() {
   serial_write(FEND);
   serial_write(CMD_DATA);
+  
   for (uint16_t i = 0; i < read_len; i++) {
-    #if MCU_VARIANT == MCU_ESP32
-      portENTER_CRITICAL(&update_lock);
-    #elif MCU_VARIANT == MCU_NRF52
+    #if MCU_VARIANT == MCU_NRF52
       portENTER_CRITICAL();
-    #endif
-    uint8_t byte = pbuf[i];
-    #if MCU_VARIANT == MCU_ESP32
-      portEXIT_CRITICAL(&update_lock);
-    #elif MCU_VARIANT == MCU_NRF52
+      uint8_t byte = pbuf[i];
       portEXIT_CRITICAL();
+    #else
+      uint8_t byte = pbuf[i];
     #endif
+
     if (byte == FEND) { serial_write(FESC); byte = TFEND; }
     if (byte == FESC) { serial_write(FESC); byte = TFESC; }
     serial_write(byte);
   }
+
   serial_write(FEND);
   read_len = 0;
+
   #if MCU_VARIANT == MCU_ESP32 || MCU_VARIANT == MCU_NRF52
     packet_ready = false;
   #endif
+
   #if MCU_VARIANT == MCU_ESP32
     #if HAS_BLE
       bt_flush();
@@ -277,17 +276,24 @@ inline void kiss_write_packet() {
 }
 
 inline void getPacketData(uint16_t len) {
-  BaseType_t int_mask = taskENTER_CRITICAL_FROM_ISR();
-  while (len-- && read_len < MTU) {
-    pbuf[read_len++] = LoRa->read();
-  }
-  taskEXIT_CRITICAL_FROM_ISR(int_mask);
+  #if MCU_VARIANT != MCU_NRF52
+    while (len-- && read_len < MTU) {
+      pbuf[read_len++] = LoRa->read();
+    }  
+  #else
+    BaseType_t int_mask = taskENTER_CRITICAL_FROM_ISR();
+    while (len-- && read_len < MTU) {
+      pbuf[read_len++] = LoRa->read();
+    }
+    taskEXIT_CRITICAL_FROM_ISR(int_mask);
+  #endif
 }
 
 void ISR_VECT receive_callback(int packet_size) {
   #if MCU_VARIANT == MCU_ESP32 || MCU_VARIANT == MCU_NRF52
-  BaseType_t int_mask;
+    BaseType_t int_mask;
   #endif
+
   if (!promisc) {
     // The standard operating mode allows large
     // packets with a payload up to 500 bytes,
@@ -302,13 +308,12 @@ void ISR_VECT receive_callback(int packet_size) {
       // This is the first part of a split
       // packet, so we set the seq variable
       // and add the data to the buffer
-      #if MCU_VARIANT == MCU_ESP32 || MCU_VARIANT == MCU_NRF52
-      int_mask = taskENTER_CRITICAL_FROM_ISR();
-      #endif
-      read_len = 0;
-      #if MCU_VARIANT == MCU_ESP32 || MCU_VARIANT == MCU_NRF52
-      taskEXIT_CRITICAL_FROM_ISR(int_mask);
+      #if MCU_VARIANT == MCU_NRF52
+        int_mask = taskENTER_CRITICAL_FROM_ISR(); read_len = 0; taskEXIT_CRITICAL_FROM_ISR(int_mask);
+      #else
+        read_len = 0;
       #endif
+      
       seq = sequence;
 
       #if MCU_VARIANT != MCU_ESP32 && MCU_VARIANT != MCU_NRF52
@@ -322,15 +327,12 @@ void ISR_VECT receive_callback(int packet_size) {
       // This is the second part of a split
       // packet, so we add it to the buffer
       // and set the ready flag.
-      
-
       #if MCU_VARIANT != MCU_ESP32 && MCU_VARIANT != MCU_NRF52
         last_rssi = (last_rssi+LoRa->packetRssi())/2;
         last_snr_raw = (last_snr_raw+LoRa->packetSnrRaw())/2;
       #endif
 
       getPacketData(packet_size);
-
       seq = SEQ_UNSET;
       ready = true;
 
@@ -339,12 +341,10 @@ void ISR_VECT receive_callback(int packet_size) {
       // same sequence id, so we must assume
       // that we are seeing the first part of
       // a new split packet.
-      #if MCU_VARIANT == MCU_ESP32 || MCU_VARIANT == MCU_NRF52
-      int_mask = taskENTER_CRITICAL_FROM_ISR();
-      #endif
-      read_len = 0;
-      #if MCU_VARIANT == MCU_ESP32 || MCU_VARIANT == MCU_NRF52
-      taskEXIT_CRITICAL_FROM_ISR(int_mask);
+      #if MCU_VARIANT == MCU_NRF52
+        int_mask = taskENTER_CRITICAL_FROM_ISR(); read_len = 0; taskEXIT_CRITICAL_FROM_ISR(int_mask);
+      #else
+        read_len = 0;
       #endif
       seq = sequence;
 
@@ -363,12 +363,10 @@ void ISR_VECT receive_callback(int packet_size) {
       if (seq != SEQ_UNSET) {
         // If we already had part of a split
         // packet in the buffer, we clear it.
-        #if MCU_VARIANT == MCU_ESP32 || MCU_VARIANT == MCU_NRF52
-        int_mask = taskENTER_CRITICAL_FROM_ISR();
-        #endif
-        read_len = 0;
-        #if MCU_VARIANT == MCU_ESP32 || MCU_VARIANT == MCU_NRF52
-        taskEXIT_CRITICAL_FROM_ISR(int_mask);
+        #if MCU_VARIANT == MCU_NRF52
+          int_mask = taskENTER_CRITICAL_FROM_ISR(); read_len = 0; taskEXIT_CRITICAL_FROM_ISR(int_mask);
+        #else
+          read_len = 0;
         #endif
         seq = SEQ_UNSET;
       }
@@ -398,6 +396,12 @@ void ISR_VECT receive_callback(int packet_size) {
         modem_packet_t *modem_packet = (modem_packet_t*)malloc(sizeof(modem_packet_t) + read_len);
         if(!modem_packet) { memory_low = true; return; }
 
+        // Get packet RSSI and SNR
+        #if MCU_VARIANT == MCU_ESP32
+          modem_packet->snr_raw = LoRa->packetSnrRaw();
+          modem_packet->rssi = LoRa->packetRssi(modem_packet->snr_raw);
+        #endif
+
         // Send packet to event queue, but free the
         // allocated memory again if the queue is
         // unable to receive the packet.
@@ -406,7 +410,6 @@ void ISR_VECT receive_callback(int packet_size) {
         if (!modem_packet_queue || xQueueSendFromISR(modem_packet_queue, &modem_packet, NULL) != pdPASS) {
             free(modem_packet);
         }
-
       #endif
     }  
   } else {
@@ -458,9 +461,7 @@ bool startRadio() {
         getFrequency();
 
         LoRa->enableCrc();
-
         LoRa->onReceive(receive_callback);
-
         lora_receive();
 
         // Flash an info pattern to indicate
@@ -1113,6 +1114,10 @@ void serialCallback(uint8_t sbyte) {
   }
 }
 
+#if MCU_VARIANT == MCU_ESP32
+  portMUX_TYPE update_lock = portMUX_INITIALIZER_UNLOCKED;
+#endif
+
 void updateModemStatus() {
   #if MCU_VARIANT == MCU_ESP32
     portENTER_CRITICAL(&update_lock);
@@ -1343,15 +1348,13 @@ void loop() {
     #if MCU_VARIANT == MCU_ESP32
       modem_packet_t *modem_packet = NULL;
       if(modem_packet_queue && xQueueReceive(modem_packet_queue, &modem_packet, 0) == pdTRUE && modem_packet) {
-        memcpy(&pbuf, modem_packet->data, modem_packet->len);
         read_len = modem_packet->len;
+        last_rssi = modem_packet->rssi;
+        last_snr_raw = modem_packet->snr_raw;
+        memcpy(&pbuf, modem_packet->data, modem_packet->len);
         free(modem_packet);
         modem_packet = NULL;
 
-        portENTER_CRITICAL(&update_lock);
-        last_rssi = LoRa->packetRssi();
-        last_snr_raw = LoRa->packetSnrRaw();
-        portEXIT_CRITICAL(&update_lock);
         kiss_indicate_stat_rssi();
         kiss_indicate_stat_snr();
         kiss_write_packet();
index 1ed81a1e83b8e17d17911a1725ea15fe47d0c377..c0c07b1ca84dfb83ef3584dc1137bd19ea97b2a3 100644 (file)
@@ -562,6 +562,14 @@ int ISR_VECT sx126x::packetRssi() {
     return pkt_rssi;
 }
 
+int ISR_VECT sx126x::packetRssi(uint8_t pkt_snr_raw) {
+    // may need more calculations here
+    uint8_t buf[3] = {0};
+    executeOpcodeRead(OP_PACKET_STATUS_6X, buf, 3);
+    int pkt_rssi = -buf[0] / 2;
+    return pkt_rssi;
+}
+
 uint8_t ISR_VECT sx126x::packetSnrRaw() {
     uint8_t buf[3] = {0};
     executeOpcodeRead(OP_PACKET_STATUS_6X, buf, 3);
index 0aec47adaddd19976038d6637c90ef486a35545e..922c605ba859f3d68ffe8b76eed459e86bfc2f3f 100644 (file)
--- a/sx126x.h
+++ b/sx126x.h
@@ -36,6 +36,7 @@ public:
 
   int parsePacket(int size = 0);
   int packetRssi();
+  int packetRssi(uint8_t pkt_snr_raw);
   int currentRssi();
   uint8_t packetRssiRaw();
   uint8_t currentRssiRaw();
index 5548d5150f97864d258685fe642e35cb59976eaf..8cbe402e80818e1e19d575eb6f9ffc36a9d09b8f 100644 (file)
@@ -230,6 +230,23 @@ uint8_t sx127x::packetRssiRaw() {
     return pkt_rssi_value;
 }
 
+int ISR_VECT sx127x::packetRssi(uint8_t pkt_snr_raw) {
+  int pkt_rssi = (int)readRegister(REG_PKT_RSSI_VALUE_7X) - RSSI_OFFSET;
+  int pkt_snr = ((int8_t)pkt_snr_raw)*0.25;
+
+  if (_frequency < 820E6) pkt_rssi -= 7;
+
+  if (pkt_snr < 0) {
+      pkt_rssi += pkt_snr;
+  } else {
+      // Slope correction is (16/15)*pkt_rssi,
+      // this estimation looses one floating point
+      // operation, and should be precise enough.
+      pkt_rssi = (int)(1.066 * pkt_rssi);
+  }
+  return pkt_rssi;
+}
+
 int ISR_VECT sx127x::packetRssi() {
     int pkt_rssi = (int)readRegister(REG_PKT_RSSI_VALUE_7X) - RSSI_OFFSET;
     int pkt_snr = packetSnr();
index f154b44ee02c4558df84217371ffb1244cca64cc..12a8419df1063aff751bedce92c725962c8f36c3 100644 (file)
--- a/sx127x.h
+++ b/sx127x.h
@@ -33,6 +33,7 @@ public:
 
   int parsePacket(int size = 0);
   int packetRssi();
+  int packetRssi(uint8_t pkt_snr_raw);
   int currentRssi();
   uint8_t packetRssiRaw();
   uint8_t currentRssiRaw();
index e965a4652aae3538f968aca81d6869ecf8fff01a..9dc4b3a90e85ea1dbb622f8129a4e602750a52b8 100644 (file)
@@ -523,6 +523,14 @@ int ISR_VECT sx128x::packetRssi() {
     return pkt_rssi;
 }
 
+int ISR_VECT sx128x::packetRssi(uint8_t pkt_snr_raw) {
+    // may need more calculations here
+    uint8_t buf[5] = {0};
+    executeOpcodeRead(OP_PACKET_STATUS_8X, buf, 5);
+    int pkt_rssi = -buf[0] / 2;
+    return pkt_rssi;
+}
+
 uint8_t ISR_VECT sx128x::packetSnrRaw() {
     uint8_t buf[5] = {0};
     executeOpcodeRead(OP_PACKET_STATUS_8X, buf, 5);
index 213fc48f34246916099677709301cc62dc8e8e04..9e13eec3c4023f9ccc6aa53617026d6546eddf3a 100644 (file)
--- a/sx128x.h
+++ b/sx128x.h
@@ -35,6 +35,7 @@ public:
 
   int parsePacket(int size = 0);
   int packetRssi();
+  int packetRssi(uint8_t pkt_snr_raw);
   int currentRssi();
   uint8_t packetRssiRaw();
   uint8_t currentRssiRaw();