]> git.tnoah.ca Git - lora-radio.git/commitdiff
Data carrier detection
authorMark Qvist <mark@adepta.io>
Thu, 26 Apr 2018 13:52:43 +0000 (15:52 +0200)
committerMark Qvist <mark@adepta.io>
Thu, 26 Apr 2018 13:52:43 +0000 (15:52 +0200)
Config.h
LoRa.cpp
LoRa.h
RNode_Firmware.ino

index 812a319b0b5fa877f5dbe502b263e802f8a2ca2e..965f2b09ee13e4a8cd5d0335533c77baa82d963f 100644 (file)
--- a/Config.h
+++ b/Config.h
        #endif
 
        // MCU independent configuration parameters
-       const long serial_baudrate = 115200;
-       const int  rssi_offset     = 164;
+       const long serial_baudrate  = 115200;
+       const int  rssi_offset      = 164;
+
+       const int lora_rx_turnaround_ms = 5;
 
        // Default LoRa settings
        int  lora_sf   = 0;
        uint32_t stat_rx                = 0;
        uint32_t stat_tx                = 0;
 
+       bool outbound_ready       = false;
+
+       bool stat_signal_detected = false;
+       bool stat_signal_synced   = false;
+       bool stat_rx_ongoing      = false;
+       bool dcd                                  = false;
+       bool dcd_led              = false;
+       bool dcd_waiting          = false;
+       uint16_t dcd_count        = 0;
+       uint16_t dcd_threshold    = 15;
+
+       uint32_t status_interval_ms = 3;
+       uint32_t last_status_update = 0;
+
+       // Status flags
+       const uint8_t SIG_DETECT = 0x01;
+       const uint8_t SIG_SYNCED = 0x02;
+       const uint8_t RX_ONGOING = 0x04;
+
 #endif
\ No newline at end of file
index f012253b16dd1754173ef042616e45ea5146d171..2b92f85c09013b69cb813654d3cbddbd987bc16f 100644 (file)
--- a/LoRa.cpp
+++ b/LoRa.cpp
@@ -1,5 +1,8 @@
 // Copyright (c) Sandeep Mistry. All rights reserved.
-// Licensed under the MIT license. See LICENSE file in the project root for full license information.
+// Licensed under the MIT license.
+
+// Modifications and additions copyright 2018 by Mark Qvist
+// Obviously still under the MIT license.
 
 #include <LoRa.h>
 
@@ -17,6 +20,7 @@
 #define REG_FIFO_RX_CURRENT_ADDR 0x10
 #define REG_IRQ_FLAGS            0x12
 #define REG_RX_NB_BYTES          0x13
+#define REG_MODEM_STAT           0x18
 #define REG_PKT_SNR_VALUE        0x19
 #define REG_PKT_RSSI_VALUE       0x1a
 #define REG_MODEM_CONFIG_1       0x1d
@@ -204,6 +208,10 @@ int LoRaClass::parsePacket(int size)
   return packetLength;
 }
 
+uint8_t LoRaClass::modemStatus() {
+  return readRegister(REG_MODEM_STAT);
+}
+
 int LoRaClass::packetRssi()
 {
   return (readRegister(REG_PKT_RSSI_VALUE) - (_frequency < 868E6 ? 164 : 157));
diff --git a/LoRa.h b/LoRa.h
index 327244c59c7832dc9d2f7f38090a62d983ac7b37..daaa77fe6dd2140e3c8ff58b5b400ec99e47051a 100644 (file)
--- a/LoRa.h
+++ b/LoRa.h
@@ -54,6 +54,7 @@ public:
   void setCodingRate4(int denominator);
   void setPreambleLength(long length);
   void setSyncWord(int sw);
+  uint8_t modemStatus();
   void enableCrc();
   void disableCrc();
 
index 65eb3b0af2e1165c18e2ef0f3e6251be59409024..d36af9df4bffc7e880ce6b70837db97bcc6563ec 100644 (file)
@@ -81,8 +81,6 @@ void update_radio_lock() {
 }
 
 void receiveCallback(int packet_size) {
-  led_rx_on();
-
   uint8_t header   = LoRa.read(); packet_size--;
   uint8_t sequence = packetSequence(header);
   bool    ready    = false;
@@ -148,7 +146,6 @@ void receiveCallback(int packet_size) {
     Serial.write(FEND);
     read_len = 0;
   }
-  led_rx_off();
 }
 
 void transmit(size_t size) {
@@ -178,21 +175,22 @@ void transmit(size_t size) {
 
     LoRa.endPacket();
     led_tx_off();
-    LoRa.receive();
 
-    if (FLOW_CONTROL_ENABLED)
-      kiss_indicate_ready();
+    LoRa.receive();
 
   } else {
     kiss_indicate_error(ERROR_TXFAILED);
     led_indicate_error(5);
   }
+
+  if (FLOW_CONTROL_ENABLED)
+      kiss_indicate_ready();
 }
 
 void serialCallback(uint8_t sbyte) {
   if (IN_FRAME && sbyte == FEND && command == CMD_DATA) {
     IN_FRAME = false;
-    transmit(frame_len);
+    outbound_ready = true;
   } else if (sbyte == FEND) {
     IN_FRAME = true;
     command = CMD_UNKNOWN;
@@ -308,7 +306,61 @@ void serialCallback(uint8_t sbyte) {
   }
 }
 
+void updateModemStatus() {
+  uint8_t status = LoRa.modemStatus();
+  last_status_update = millis();
+  if (status & SIG_DETECT == 0x01) { stat_signal_detected = true; } else { stat_signal_detected = false; }
+  if (status & SIG_SYNCED == 0x01) { stat_signal_synced = true; } else { stat_signal_synced = false; }
+  if (status & RX_ONGOING == 0x01) { stat_rx_ongoing = true; } else { stat_rx_ongoing = false; }
+
+  if (stat_signal_detected || stat_signal_synced || stat_rx_ongoing) {
+    if (dcd_count < dcd_threshold) {
+      dcd_count++;
+      dcd = true;
+    } else {
+      dcd = true;
+      dcd_led = true;
+    }
+  } else {
+    if (dcd_count > 0) {
+      dcd_count--;
+    } else {
+      dcd_led = false;
+    }
+    dcd = false;
+  }
+
+  if (dcd_led) {
+    led_rx_on();
+  } else {
+    led_rx_off();
+  }
+}
+
+void checkModemStatus() {
+  if (millis()-last_status_update >= status_interval_ms) {
+    led_tx_on();
+    updateModemStatus();
+    led_tx_off();
+  }
+}
+
 void loop() {
+  if (radio_online) {
+    checkModemStatus();
+    if (outbound_ready) {
+      if (!dcd_waiting) updateModemStatus();
+      if (!dcd && !dcd_led) {
+        if (dcd_waiting) delay(lora_rx_turnaround_ms);
+        outbound_ready = false;
+        dcd_waiting = false;
+        transmit(frame_len);
+      } else {
+        dcd_waiting = true;
+      }
+    }
+  }
+
   if (Serial.available()) {
     char sbyte = Serial.read();
     serialCallback(sbyte);