From 5d9917eb06255e2bdddf301e65da8b88d5626d0a Mon Sep 17 00:00:00 2001 From: jgromes Date: Sun, 2 Jun 2024 09:29:55 +0200 Subject: [PATCH] [LoRaWAN] Change FSK switch to modulation variable --- src/protocols/LoRaWAN/LoRaWAN.cpp | 55 +++++++++++++++++++------------ src/protocols/LoRaWAN/LoRaWAN.h | 10 ++++-- 2 files changed, 42 insertions(+), 23 deletions(-) diff --git a/src/protocols/LoRaWAN/LoRaWAN.cpp b/src/protocols/LoRaWAN/LoRaWAN.cpp index eb2c9e552..ad33ceae9 100644 --- a/src/protocols/LoRaWAN/LoRaWAN.cpp +++ b/src/protocols/LoRaWAN/LoRaWAN.cpp @@ -1227,7 +1227,7 @@ int16_t LoRaWANNode::downlinkCommon() { // if we got here due to a timeout, stop ongoing activities if(this->phyLayer->isRxTimeout()) { this->phyLayer->standby(); // TODO check: this should be done automagically due to RxSingle? - if(!this->FSK) { + if(this->modulation == RADIOLIB_LORAWAN_MODULATION_LORA) { this->phyLayer->invertIQ(false); } @@ -1250,7 +1250,7 @@ int16_t LoRaWANNode::downlinkCommon() { // we have a message, clear actions, go to standby and reset the IQ inversion this->phyLayer->standby(); // TODO check: this should be done automagically due to RxSingle? this->phyLayer->clearPacketReceivedAction(); - if(!this->FSK) { + if(this->modulation == RADIOLIB_LORAWAN_MODULATION_LORA) { state = this->phyLayer->invertIQ(false); RADIOLIB_ASSERT(state); } @@ -1731,9 +1731,7 @@ int16_t LoRaWANNode::setPhyProperties(uint8_t dir) { // if this channel is an FSK channel, toggle the FSK switch if(this->band->dataRates[this->dataRates[dir]] == RADIOLIB_LORAWAN_DATA_RATE_FSK_50_K) { - this->FSK = true; - } else { - this->FSK = false; + this->modulation = RADIOLIB_LORAWAN_MODULATION_GFSK; } int8_t pwr = this->txPowerMax - this->txPowerSteps * 2; @@ -1752,7 +1750,7 @@ int16_t LoRaWANNode::setPhyProperties(uint8_t dir) { RADIOLIB_DEBUG_PROTOCOL_PRINTLN("PHY: SF = %d, TX = %d dBm, BW = %6.3f kHz, CR = 4/%d", dr.lora.spreadingFactor, pwr, dr.lora.bandwidth, dr.lora.codingRate); - if(this->FSK) { + if(this->modulation == RADIOLIB_LORAWAN_MODULATION_GFSK) { state = this->phyLayer->setDataShaping(RADIOLIB_SHAPING_1_0); RADIOLIB_ASSERT(state); state = this->phyLayer->setEncoding(RADIOLIB_ENCODING_WHITENING); @@ -1761,34 +1759,49 @@ int16_t LoRaWANNode::setPhyProperties(uint8_t dir) { // downlink messages are sent with inverted IQ if(dir == RADIOLIB_LORAWAN_CHANNEL_DIR_DOWNLINK) { - if(!this->FSK) { + if(this->modulation == RADIOLIB_LORAWAN_MODULATION_LORA) { state = this->phyLayer->invertIQ(true); RADIOLIB_ASSERT(state); } } // this only needs to be done once-ish - uint8_t syncWord[3] = { 0 }; + uint8_t syncWord[4] = { 0 }; uint8_t syncWordLen = 0; size_t preLen = 0; - if(this->FSK) { - preLen = 8*RADIOLIB_LORAWAN_GFSK_PREAMBLE_LEN; - syncWord[0] = (uint8_t)(RADIOLIB_LORAWAN_GFSK_SYNC_WORD >> 16); - syncWord[1] = (uint8_t)(RADIOLIB_LORAWAN_GFSK_SYNC_WORD >> 8); - syncWord[2] = (uint8_t)RADIOLIB_LORAWAN_GFSK_SYNC_WORD; - syncWordLen = 3; - - } else { - preLen = RADIOLIB_LORAWAN_LORA_PREAMBLE_LEN; - syncWord[0] = RADIOLIB_LORAWAN_LORA_SYNC_WORD; - syncWordLen = 1; - + switch(this->modulation) { + case(RADIOLIB_LORAWAN_MODULATION_GFSK): { + preLen = 8*RADIOLIB_LORAWAN_GFSK_PREAMBLE_LEN; + syncWord[0] = (uint8_t)(RADIOLIB_LORAWAN_GFSK_SYNC_WORD >> 16); + syncWord[1] = (uint8_t)(RADIOLIB_LORAWAN_GFSK_SYNC_WORD >> 8); + syncWord[2] = (uint8_t)RADIOLIB_LORAWAN_GFSK_SYNC_WORD; + syncWordLen = 3; + } break; + + case(RADIOLIB_LORAWAN_MODULATION_LORA): { + preLen = RADIOLIB_LORAWAN_LORA_PREAMBLE_LEN; + syncWord[0] = RADIOLIB_LORAWAN_LORA_SYNC_WORD; + syncWordLen = 1; + } break; + + case(RADIOLIB_LORAWAN_MODULATION_LR_FHSS): { + syncWord[0] = (uint8_t)(RADIOLIB_LORAWAN_LR_FHSS_SYNC_WORD >> 24); + syncWord[1] = (uint8_t)(RADIOLIB_LORAWAN_LR_FHSS_SYNC_WORD >> 16); + syncWord[2] = (uint8_t)(RADIOLIB_LORAWAN_LR_FHSS_SYNC_WORD >> 8); + syncWord[3] = (uint8_t)RADIOLIB_LORAWAN_LR_FHSS_SYNC_WORD; + syncWordLen = 4; + } break; + + default: + return(RADIOLIB_ERR_WRONG_MODEM); } state = this->phyLayer->setSyncWord(syncWord, syncWordLen); RADIOLIB_ASSERT(state); - state = this->phyLayer->setPreambleLength(preLen); + if(this->modulation != RADIOLIB_LORAWAN_MODULATION_LR_FHSS) { + state = this->phyLayer->setPreambleLength(preLen); + } return(state); } diff --git a/src/protocols/LoRaWAN/LoRaWAN.h b/src/protocols/LoRaWAN/LoRaWAN.h index fd75da52a..692f45e1a 100644 --- a/src/protocols/LoRaWAN/LoRaWAN.h +++ b/src/protocols/LoRaWAN/LoRaWAN.h @@ -15,11 +15,17 @@ #define RADIOLIB_LORAWAN_CLASS_B (0x0B) #define RADIOLIB_LORAWAN_CLASS_C (0x0C) +// modulation type +#define RADIOLIB_LORAWAN_MODULATION_LORA (0) +#define RADIOLIB_LORAWAN_MODULATION_GFSK (1) +#define RADIOLIB_LORAWAN_MODULATION_LR_FHSS (2) + // preamble format #define RADIOLIB_LORAWAN_LORA_SYNC_WORD (0x34) #define RADIOLIB_LORAWAN_LORA_PREAMBLE_LEN (8) #define RADIOLIB_LORAWAN_GFSK_SYNC_WORD (0xC194C1) #define RADIOLIB_LORAWAN_GFSK_PREAMBLE_LEN (5) +#define RADIOLIB_LORAWAN_LR_FHSS_SYNC_WORD (0x2C0F7995) // MAC header field encoding MSB LSB DESCRIPTION #define RADIOLIB_LORAWAN_MHDR_MTYPE_JOIN_REQUEST (0x00 << 5) // 7 5 message type: join request @@ -926,8 +932,8 @@ class LoRaWANNode { uint32_t confFCntDown = RADIOLIB_LORAWAN_FCNT_NONE; uint32_t adrFCnt = 0; - // whether the current configured channel is in FSK mode - bool FSK = false; + // modulation of the currently configured channel + uint8_t modulation = RADIOLIB_LORAWAN_MODULATION_LORA; // ADR is enabled by default bool adrEnabled = true;