diff --git a/src/modules/SX128x/SX128x.cpp b/src/modules/SX128x/SX128x.cpp index 288ec928..3c18d68d 100644 --- a/src/modules/SX128x/SX128x.cpp +++ b/src/modules/SX128x/SX128x.cpp @@ -772,8 +772,7 @@ int16_t SX128x::setModem(ModemType_t modem) { int16_t SX128x::getModem(ModemType_t* modem) { RADIOLIB_ASSERT_PTR(modem); - uint8_t packetType = getPacketType(); - switch(packetType) { + switch(getPacketType()) { case(RADIOLIB_SX128X_PACKET_TYPE_LORA): *modem = ModemType_t::RADIOLIB_MODEM_LORA; return(RADIOLIB_ERR_NONE); @@ -827,7 +826,7 @@ int16_t SX128x::setPreambleLength(uint32_t preambleLength) { // update packet parameters this->preambleLengthGFSK = ((preambleLength / 4) - 1) << 4; - return(setPacketParamsGFSK(this->preambleLengthGFSK, this->syncWordLen, this->syncWordMatch, this->crcGFSK, this->whitening)); + return(setPacketParamsGFSK(this->preambleLengthGFSK, this->syncWordLen, this->syncWordMatch, this->crcGFSK, this->whitening, this->packetType)); } return(RADIOLIB_ERR_WRONG_MODEM); @@ -1014,7 +1013,7 @@ int16_t SX128x::setSyncWord(const uint8_t* syncWord, uint8_t len) { /// \todo add support for multiple sync words this->syncWordMatch = RADIOLIB_SX128X_GFSK_FLRC_SYNC_WORD_1; } - return(setPacketParamsGFSK(this->preambleLengthGFSK, this->syncWordLen, this->syncWordMatch, this->crcGFSK, this->whitening)); + return(setPacketParamsGFSK(this->preambleLengthGFSK, this->syncWordLen, this->syncWordMatch, this->crcGFSK, this->whitening, this->packetType)); } int16_t SX128x::setSyncWord(uint8_t syncWord, uint8_t controlBits) { @@ -1045,7 +1044,7 @@ int16_t SX128x::setCRC(uint8_t len, uint32_t initial, uint16_t polynomial) { } } this->crcGFSK = len << 4; - state = setPacketParamsGFSK(this->preambleLengthGFSK, this->syncWordLen, this->syncWordMatch, this->crcGFSK, this->whitening); + state = setPacketParamsGFSK(this->preambleLengthGFSK, this->syncWordLen, this->syncWordMatch, this->crcGFSK, this->whitening, this->packetType); RADIOLIB_ASSERT(state); // set initial CRC value @@ -1107,7 +1106,7 @@ int16_t SX128x::setWhitening(bool enabled) { } if(modem == RADIOLIB_SX128X_PACKET_TYPE_GFSK) { - return(setPacketParamsGFSK(this->preambleLengthGFSK, this->syncWordLen, this->syncWordMatch, this->crcGFSK, this->whitening)); + return(setPacketParamsGFSK(this->preambleLengthGFSK, this->syncWordLen, this->syncWordMatch, this->crcGFSK, this->whitening, this->packetType)); } return(setPacketParamsBLE(this->connectionState, this->crcBLE, this->bleTestPayload, this->whitening)); } @@ -1284,6 +1283,14 @@ size_t SX128x::getPacketLength(bool update, uint8_t* offset) { return((size_t)rxBufStatus[0]); } +int16_t SX128x::fixedPacketLengthMode(uint8_t len) { + return(setPacketMode(RADIOLIB_SX128X_GFSK_FLRC_PACKET_FIXED, len)); +} + +int16_t SX128x::variablePacketLengthMode(uint8_t maxLen) { + return(setPacketMode(RADIOLIB_SX128X_GFSK_FLRC_PACKET_VARIABLE, maxLen)); +} + RadioLibTime_t SX128x::getTimeOnAir(size_t len) { // check active modem uint8_t modem = getPacketType(); @@ -1439,7 +1446,7 @@ int16_t SX128x::stageMode(RadioModeType_t mode, RadioModeConfig_t* cfg) { if(modem == RADIOLIB_SX128X_PACKET_TYPE_LORA) { state = setPacketParamsLoRa(this->preambleLengthLoRa, this->headerType, cfg->transmit.len, this->crcLoRa, this->invertIQEnabled); } else if((modem == RADIOLIB_SX128X_PACKET_TYPE_GFSK) || (modem == RADIOLIB_SX128X_PACKET_TYPE_FLRC)) { - state = setPacketParamsGFSK(this->preambleLengthGFSK, this->syncWordLen, this->syncWordMatch, this->crcGFSK, this->whitening, cfg->transmit.len); + state = setPacketParamsGFSK(this->preambleLengthGFSK, this->syncWordLen, this->syncWordMatch, this->crcGFSK, this->whitening, this->packetType, cfg->transmit.len); } else if(modem == RADIOLIB_SX128X_PACKET_TYPE_BLE) { state = setPacketParamsBLE(this->connectionState, this->crcBLE, this->bleTestPayload, this->whitening); } else { @@ -1603,7 +1610,7 @@ int16_t SX128x::setModulationParams(uint8_t modParam1, uint8_t modParam2, uint8_ return(this->mod->SPIwriteStream(RADIOLIB_SX128X_CMD_SET_MODULATION_PARAMS, data, 3)); } -int16_t SX128x::setPacketParamsGFSK(uint8_t preambleLen, uint8_t syncLen, uint8_t syncMatch, uint8_t crcLen, uint8_t whiten, uint8_t payLen, uint8_t hdrType) { +int16_t SX128x::setPacketParamsGFSK(uint8_t preambleLen, uint8_t syncLen, uint8_t syncMatch, uint8_t crcLen, uint8_t whiten, uint8_t hdrType, uint8_t payLen) { const uint8_t data[] = { preambleLen, syncLen, syncMatch, hdrType, payLen, crcLen, whiten }; return(this->mod->SPIwriteStream(RADIOLIB_SX128X_CMD_SET_PACKET_PARAMS, data, 7)); } @@ -1647,6 +1654,21 @@ int16_t SX128x::setPacketType(uint8_t type) { return(this->mod->SPIwriteStream(RADIOLIB_SX128X_CMD_SET_PACKET_TYPE, data, 1)); } +int16_t SX128x::setPacketMode(uint8_t mode, uint8_t len) { + // check active modem + if(getPacketType() != RADIOLIB_SX128X_PACKET_TYPE_GFSK) { + return(RADIOLIB_ERR_WRONG_MODEM); + } + + // set requested packet mode + int16_t state = setPacketParamsGFSK(this->preambleLengthGFSK, this->syncWordLen, this->syncWordMatch, this->crcGFSK, this->whitening, mode, len); + RADIOLIB_ASSERT(state); + + // update cached value + this->packetType = mode; + return(state); +} + int16_t SX128x::setHeaderType(uint8_t hdrType, size_t len) { // check active modem uint8_t modem = getPacketType(); diff --git a/src/modules/SX128x/SX128x.h b/src/modules/SX128x/SX128x.h index 0211ed26..0fe91a4b 100644 --- a/src/modules/SX128x/SX128x.h +++ b/src/modules/SX128x/SX128x.h @@ -795,6 +795,20 @@ class SX128x: public PhysicalLayer { */ size_t getPacketLength(bool update, uint8_t* offset); + /*! + \brief Set modem in fixed packet length mode. Available in GFSK mode only. + \param len Packet length. + \returns \ref status_codes + */ + int16_t fixedPacketLengthMode(uint8_t len = RADIOLIB_SX128X_MAX_PACKET_LENGTH); + + /*! + \brief Set modem in variable packet length mode. Available in GFSK mode only. + \param maxLen Maximum packet length. + \returns \ref status_codes + */ + int16_t variablePacketLengthMode(uint8_t maxLen = RADIOLIB_SX128X_MAX_PACKET_LENGTH); + /*! \brief Get expected time-on-air for a given size of payload. \param len Payload length in bytes. @@ -885,7 +899,7 @@ class SX128x: public PhysicalLayer { int16_t setTxParams(uint8_t pwr, uint8_t rampTime = RADIOLIB_SX128X_PA_RAMP_10_US); int16_t setBufferBaseAddress(uint8_t txBaseAddress = 0x00, uint8_t rxBaseAddress = 0x00); int16_t setModulationParams(uint8_t modParam1, uint8_t modParam2, uint8_t modParam3); - int16_t setPacketParamsGFSK(uint8_t preambleLen, uint8_t syncLen, uint8_t syncMatch, uint8_t crcLen, uint8_t whiten, uint8_t payLen = 0xFF, uint8_t hdrType = RADIOLIB_SX128X_GFSK_FLRC_PACKET_VARIABLE); + int16_t setPacketParamsGFSK(uint8_t preambleLen, uint8_t syncLen, uint8_t syncMatch, uint8_t crcLen, uint8_t whiten, uint8_t hdrType, uint8_t payLen = 0xFF); int16_t setPacketParamsBLE(uint8_t connState, uint8_t crcLen, uint8_t bleTest, uint8_t whiten); int16_t setPacketParamsLoRa(uint8_t preambleLen, uint8_t hdrType, uint8_t payLen, uint8_t crc, uint8_t invIQ = RADIOLIB_SX128X_LORA_IQ_STANDARD); int16_t setDioIrqParams(uint16_t irqMask, uint16_t dio1Mask, uint16_t dio2Mask = RADIOLIB_SX128X_IRQ_NONE, uint16_t dio3Mask = RADIOLIB_SX128X_IRQ_NONE); @@ -913,6 +927,7 @@ class SX128x: public PhysicalLayer { uint16_t bitRateKbps = 0; uint8_t bitRate = 0, modIndex = 0, shaping = 0; uint8_t preambleLengthGFSK = 0, syncWordLen = 0, syncWordMatch = 0, crcGFSK = 0, whitening = 0; + uint8_t packetType = RADIOLIB_SX128X_GFSK_FLRC_PACKET_VARIABLE; // cached FLRC parameters uint8_t codingRateFLRC = 0; @@ -921,6 +936,7 @@ class SX128x: public PhysicalLayer { uint8_t connectionState = 0, crcBLE = 0, bleTestPayload = 0; int16_t config(uint8_t modem); + int16_t setPacketMode(uint8_t mode, uint8_t len); int16_t setHeaderType(uint8_t hdrType, size_t len = 0xFF); };