Skip to content

Commit

Permalink
Reworked DCD methods for SX1276 and SX1280
Browse files Browse the repository at this point in the history
  • Loading branch information
markqvist committed Jan 8, 2025
1 parent a796e56 commit 58f6a3d
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 17 deletions.
9 changes: 8 additions & 1 deletion sx127x.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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); }
Expand Down Expand Up @@ -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;
Expand Down
7 changes: 6 additions & 1 deletion sx127x.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -65,7 +70,7 @@ class sx127x : public Stream {
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();
Expand Down
36 changes: 22 additions & 14 deletions sx128x.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

if ((buf[0] & IRQ_PREAMBLE_DET_MASK_8X) != 0) {
byte = byte | 0x01 | 0x04;
// Clear register after reading
clearbuf[0] = IRQ_PREAMBLE_DET_MASK_8X;
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[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;
}


Expand Down
2 changes: 1 addition & 1 deletion sx128x.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class sx128x : public Stream {
uint8_t getCodingRate4();
void setPreambleLength(long preamble_symbols);
void setSyncWord(int sw);
uint8_t modemStatus();
bool dcd();
void clearIRQStatus();
void enableCrc();
void disableCrc();
Expand Down

0 comments on commit 58f6a3d

Please sign in to comment.