From 58f6a3d6a3dc21a9e054e67053537dbfac35efc1 Mon Sep 17 00:00:00 2001 From: Mark Qvist Date: Wed, 8 Jan 2025 17:45:23 +0100 Subject: [PATCH] Reworked DCD methods for SX1276 and SX1280 --- sx127x.cpp | 9 ++++++++- sx127x.h | 7 ++++++- sx128x.cpp | 34 +++++++++++++++++++++------------- sx128x.h | 2 +- 4 files changed, 36 insertions(+), 16 deletions(-) diff --git a/sx127x.cpp b/sx127x.cpp index 8871593..d354d9b 100644 --- a/sx127x.cpp +++ b/sx127x.cpp @@ -86,7 +86,6 @@ uint8_t ISR_VECT sx127x::readRegister(uint8_t address) { return singleTransfer(a void sx127x::writeRegister(uint8_t address, uint8_t value) { singleTransfer(address | 0x80, value); } void sx127x::standby() { writeRegister(REG_OP_MODE_7X, MODE_LONG_RANGE_MODE_7X | MODE_STDBY_7X); } void sx127x::sleep() { writeRegister(REG_OP_MODE_7X, MODE_LONG_RANGE_MODE_7X | MODE_SLEEP_7X); } -uint8_t sx127x::modemStatus() { return readRegister(REG_MODEM_STAT_7X); } void sx127x::setSyncWord(uint8_t sw) { writeRegister(REG_SYNC_WORD_7X, sw); } void sx127x::enableCrc() { writeRegister(REG_MODEM_CONFIG_2_7X, readRegister(REG_MODEM_CONFIG_2_7X) | 0x04); } void sx127x::disableCrc() { writeRegister(REG_MODEM_CONFIG_2_7X, readRegister(REG_MODEM_CONFIG_2_7X) & 0xfb); } @@ -195,6 +194,14 @@ int sx127x::endPacket() { return 1; } +bool sx127x::dcd() { + bool carrier_detected = false; + uint8_t status = readRegister(REG_MODEM_STAT_7X); + if ((status & SIG_DETECT) == SIG_DETECT) { carrier_detected = true; } + if ((status & SIG_SYNCED) == SIG_SYNCED) { carrier_detected = true; } + return carrier_detected; +} + uint8_t sx127x::currentRssiRaw() { uint8_t rssi = readRegister(REG_RSSI_VALUE_7X); return rssi; diff --git a/sx127x.h b/sx127x.h index c87b636..7639857 100644 --- a/sx127x.h +++ b/sx127x.h @@ -18,6 +18,11 @@ #define RSSI_OFFSET 157 +// Modem status flags +#define SIG_DETECT 0x01 +#define SIG_SYNCED 0x02 +#define RX_ONGOING 0x04 + class sx127x : public Stream { public: sx127x(); @@ -65,7 +70,7 @@ public: void setCodingRate4(int denominator); void setPreambleLength(long preamble_symbols); void setSyncWord(uint8_t sw); - uint8_t modemStatus(); + bool dcd(); void enableCrc(); void disableCrc(); void enableTCXO(); diff --git a/sx128x.cpp b/sx128x.cpp index 9aa6d7d..89fb70a 100644 --- a/sx128x.cpp +++ b/sx128x.cpp @@ -410,23 +410,31 @@ int sx128x::endPacket() { else { return 1; } } -uint8_t sx128x::modemStatus() { - // Imitate the register status from the sx1276 / 78 - uint8_t buf[2] = {0}; - executeOpcodeRead(OP_GET_IRQ_STATUS_8X, buf, 2); - uint8_t clearbuf[2] = {0}; - uint8_t byte = 0x00; +unsigned long preamble_detected_at = 0; +unsigned long header_detected_at = 0; +extern long lora_preamble_time_ms; +extern long lora_header_time_ms; +bool sx128x::dcd() { + bool carrier_detected = false; + uint8_t buf[2] = {0}; executeOpcodeRead(OP_GET_IRQ_STATUS_8X, buf, 2); - if ((buf[0] & IRQ_PREAMBLE_DET_MASK_8X) != 0) { - byte = byte | 0x01 | 0x04; - // Clear register after reading - clearbuf[0] = IRQ_PREAMBLE_DET_MASK_8X; + if ((buf[1] & IRQ_PREAMBLE_DET_MASK_8X) != 0) { + carrier_detected = true; + if (preamble_detected_at == 0) preamble_detected_at = millis(); + if (millis() - preamble_detected_at > lora_preamble_time_ms + lora_header_time_ms) { + preamble_detected_at = 0; + uint8_t clearbuf[2] = {0}; + clearbuf[1] = IRQ_PREAMBLE_DET_MASK_8X; + executeOpcode(OP_CLEAR_IRQ_STATUS_8X, clearbuf, 2); } + } - if ((buf[1] & IRQ_HEADER_DET_MASK_8X) != 0) { byte = byte | 0x02 | 0x04; } - executeOpcode(OP_CLEAR_IRQ_STATUS_8X, clearbuf, 2); + if ((buf[1] & IRQ_HEADER_DET_MASK_8X) != 0) { + carrier_detected = true; + header_detected_at = millis(); + } - return byte; + return carrier_detected; } diff --git a/sx128x.h b/sx128x.h index 5977f7d..df15775 100644 --- a/sx128x.h +++ b/sx128x.h @@ -69,7 +69,7 @@ public: uint8_t getCodingRate4(); void setPreambleLength(long preamble_symbols); void setSyncWord(int sw); - uint8_t modemStatus(); + bool dcd(); void clearIRQStatus(); void enableCrc(); void disableCrc();