From be98b0d7f76e7c8621b659f46f1131773f347f83 Mon Sep 17 00:00:00 2001 From: Mark Qvist Date: Thu, 26 Apr 2018 15:52:43 +0200 Subject: [PATCH] Data carrier detection --- Config.h | 25 ++++++++++++++++-- LoRa.cpp | 10 ++++++- LoRa.h | 1 + RNode_Firmware.ino | 66 +++++++++++++++++++++++++++++++++++++++++----- 4 files changed, 92 insertions(+), 10 deletions(-) diff --git a/Config.h b/Config.h index 812a319..965f2b0 100644 --- a/Config.h +++ b/Config.h @@ -43,8 +43,10 @@ #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; @@ -67,4 +69,23 @@ 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 diff --git a/LoRa.cpp b/LoRa.cpp index f012253..2b92f85 100644 --- 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 @@ -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 327244c..daaa77f 100644 --- 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(); diff --git a/RNode_Firmware.ino b/RNode_Firmware.ino index 65eb3b0..d36af9d 100644 --- a/RNode_Firmware.ino +++ b/RNode_Firmware.ino @@ -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);