diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..600d2d3 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.vscode \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..4c144fb --- /dev/null +++ b/LICENSE @@ -0,0 +1,22 @@ +MIT License + +Copyright (c) 2021 Stuart Robinson +Copyright (c) 2024 Hasan Albinsaid + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..503c5f3 --- /dev/null +++ b/README.md @@ -0,0 +1,7 @@ +# SX128x Arduino Library + +This repository is based on the [StuartsProjects](https://github.com/StuartsProjects/SX12XX-LoRa) repository (as of the March 2024 commit) and focuses exclusively on the SX128x library, with additional features and improvements. + +### Additional Features +- **Reliable Packet Transmission with Addressing**: + New functions `transmitReliableAutoACK_addr` and `receiveReliableAutoACK_addr` have been added to enhance reliable communication with built-in addressing support. diff --git a/keywords.txt b/keywords.txt index 15eed8b..20ba647 100644 --- a/keywords.txt +++ b/keywords.txt @@ -38,9 +38,11 @@ CRCCCITTSX KEYWORD2 CRCCCITT KEYWORD2 printSXBufferASCII KEYWORD2 receiveAddressed KEYWORD2 +destAddr KEYWORD2 getByteSXBuffer KEYWORD2 startWriteSXBuffer KEYWORD2 transmitAddressed KEYWORD2 +srcAddr KEYWORD2 startReadSXBuffer KEYWORD2 endReadSXBuffer KEYWORD2 readBuffer KEYWORD2 diff --git a/src/SX128XLT.cpp b/src/SX128XLT.cpp index 8043e31..60e8704 100644 --- a/src/SX128XLT.cpp +++ b/src/SX128XLT.cpp @@ -4,6 +4,12 @@ 06/02/20 */ +/* + Copyright 2024 - maintained by Hasan Albinsaid + Addition features: + - Transmit and Receive in reliable with auto ACK to specific address of a node (max payload size will be 247 byte) +*/ + /* Parts of code Copyright (c) 2013, SEMTECH S.A. See LICENSE.TXT file included in the library @@ -11,32 +17,31 @@ #include #include +#include -#define LTUNUSED(v) (void) (v) //add LTUNUSED(variable); to avoid compiler warnings +#define LTUNUSED(v) (void)(v) // add LTUNUSED(variable); to avoid compiler warnings #define USE_SPI_TRANSACTION -//#define SX128XDEBUG //enable debug messages -//#define RANGINGDEBUG //enable debug messages for ranging -//#define SX128XDEBUGRXTX //enable debug messages for RX TX switching -//#define SX128XDEBUGPINS //enable pin allocation debug messages -//#define SX128XDEBUGRELIABLE //enable for debugging reliable and data transfer (DT) packets -//#define USEPAYLOADLENGTHREGISTER //enable autoamtic setting of Payload length with register write -//#define DETECTRELIABLERRORS //enable to improve error detect reliable errors such as incorrect packet size etc -#define REVISEDCHECKBUSY //change to checkBusy() made November 2021 to improve speed, comment this #define out to use previous code. - -//Changes November 2021 -//Revised packet RSSI -//Added ranging RSSI -//Revised checkBusy() to improve performance -//Revised receive functions to preven lock up in receive mode on header error -//Revised receive functions to include failure on syncword fail, needed for relaible FLRC error detection -//Added begin() functions to allow for limited pin use -//Added setPayloadLength() to more easily use LoRa or FLRC packets -//Added option, by enabling #define USEPAYLOADLENGTHREGISTER, to set payload length via a discovered register write -//Added reliable packet functions -//Added data transfer functions - +// #define SX128XDEBUG //enable debug messages +// #define RANGINGDEBUG //enable debug messages for ranging +// #define SX128XDEBUGRXTX //enable debug messages for RX TX switching +// #define SX128XDEBUGPINS //enable pin allocation debug messages +// #define SX128XDEBUGRELIABLE //enable for debugging reliable and data transfer (DT) packets +// #define USEPAYLOADLENGTHREGISTER //enable autoamtic setting of Payload length with register write +// #define DETECTRELIABLERRORS //enable to improve error detect reliable errors such as incorrect packet size etc +#define REVISEDCHECKBUSY // change to checkBusy() made November 2021 to improve speed, comment this #define out to use previous code. +// Changes November 2021 +// Revised packet RSSI +// Added ranging RSSI +// Revised checkBusy() to improve performance +// Revised receive functions to preven lock up in receive mode on header error +// Revised receive functions to include failure on syncword fail, needed for relaible FLRC error detection +// Added begin() functions to allow for limited pin use +// Added setPayloadLength() to more easily use LoRa or FLRC packets +// Added option, by enabling #define USEPAYLOADLENGTHREGISTER, to set payload length via a discovered register write +// Added reliable packet functions +// Added data transfer functions SX128XLT::SX128XLT() { @@ -50,11 +55,10 @@ SX128XLT::SX128XLT() 5 IRQ > begin(int8_t pinNSS, int8_t pinRFBUSY, uint8_t device) */ - bool SX128XLT::begin(int8_t pinNSS, int8_t pinNRESET, int8_t pinRFBUSY, int8_t pinDIO1, int8_t pinDIO2, int8_t pinDIO3, int8_t pinRXEN, int8_t pinTXEN, uint8_t device) { - //format 1 pins, assign all available pins + // format 1 pins, assign all available pins _NSS = pinNSS; _NRESET = pinNRESET; _RFBUSY = pinRFBUSY; @@ -64,8 +68,8 @@ bool SX128XLT::begin(int8_t pinNSS, int8_t pinNRESET, int8_t pinRFBUSY, int8_t p _RXEN = pinRXEN; _TXEN = pinTXEN; _Device = device; - _TXDonePin = pinDIO1; //this is defalt pin for sensing TX done - _RXDonePin = pinDIO1; //this is defalt pin for sensing RX done + _TXDonePin = pinDIO1; // this is defalt pin for sensing TX done + _RXDonePin = pinDIO1; // this is defalt pin for sensing RX done digitalWrite(_NSS, HIGH); pinMode(_NSS, OUTPUT); @@ -74,7 +78,6 @@ bool SX128XLT::begin(int8_t pinNSS, int8_t pinNRESET, int8_t pinRFBUSY, int8_t p digitalWrite(_NRESET, LOW); pinMode(_RFBUSY, INPUT); - #ifdef SX128XDEBUGPINS Serial.println(F("1 begin()")); Serial.println(F("SX128XLT constructor instantiated successfully")); @@ -98,17 +101,17 @@ bool SX128XLT::begin(int8_t pinNSS, int8_t pinNRESET, int8_t pinRFBUSY, int8_t p if (_DIO1 >= 0) { - pinMode( _DIO1, INPUT); + pinMode(_DIO1, INPUT); } if (_DIO2 >= 0) { - pinMode( _DIO2, INPUT); + pinMode(_DIO2, INPUT); } if (_DIO3 >= 0) { - pinMode( _DIO3, INPUT); + pinMode(_DIO3, INPUT); } if ((_RXEN >= 0) && (_TXEN >= 0)) @@ -138,21 +141,20 @@ bool SX128XLT::begin(int8_t pinNSS, int8_t pinNRESET, int8_t pinRFBUSY, int8_t p return false; } - bool SX128XLT::begin(int8_t pinNSS, int8_t pinNRESET, int8_t pinRFBUSY, int8_t pinDIO1, uint8_t device) { - //format 2 pins for NiceRF, NSS, NRESET, RFBUSY, DIO1 + // format 2 pins for NiceRF, NSS, NRESET, RFBUSY, DIO1 _NSS = pinNSS; _NRESET = pinNRESET; _RFBUSY = pinRFBUSY; _DIO1 = pinDIO1; - _DIO2 = -1; //not defined, so mark as unused - _DIO3 = -1; //not defined, so mark as unused - _RXEN = -1; //not defined, so mark as unused - _TXEN = -1; //not defined, so mark as unused + _DIO2 = -1; // not defined, so mark as unused + _DIO3 = -1; // not defined, so mark as unused + _RXEN = -1; // not defined, so mark as unused + _TXEN = -1; // not defined, so mark as unused _Device = device; - _TXDonePin = pinDIO1; //this is defalt pin for sensing TX done - _RXDonePin = pinDIO1; //this is defalt pin for sensing RX done + _TXDonePin = pinDIO1; // this is defalt pin for sensing TX done + _RXDonePin = pinDIO1; // this is defalt pin for sensing RX done digitalWrite(_NSS, HIGH); pinMode(_NSS, OUTPUT); @@ -184,7 +186,7 @@ bool SX128XLT::begin(int8_t pinNSS, int8_t pinNRESET, int8_t pinRFBUSY, int8_t p if (_DIO1 >= 0) { - pinMode( _DIO1, INPUT); + pinMode(_DIO1, INPUT); } #ifdef SX128XDEBUGPINS @@ -203,21 +205,20 @@ bool SX128XLT::begin(int8_t pinNSS, int8_t pinNRESET, int8_t pinRFBUSY, int8_t p return false; } - bool SX128XLT::begin(int8_t pinNSS, int8_t pinNRESET, int8_t pinRFBUSY, int8_t pinDIO1, int8_t pinRXEN, int8_t pinTXEN, uint8_t device) { - //format 3 pins for Ebyte, NSS, NRESET, RFBUSY, DIO1, RX_EN, TX_EN + // format 3 pins for Ebyte, NSS, NRESET, RFBUSY, DIO1, RX_EN, TX_EN _NSS = pinNSS; _NRESET = pinNRESET; _RFBUSY = pinRFBUSY; _DIO1 = pinDIO1; - _DIO2 = -1; //not defined, so mark as unused - _DIO3 = -1; //not defined, so mark as unused + _DIO2 = -1; // not defined, so mark as unused + _DIO3 = -1; // not defined, so mark as unused _RXEN = pinRXEN; _TXEN = pinTXEN; _Device = device; - _TXDonePin = pinDIO1; //this is defalt pin for sensing TX done - _RXDonePin = pinDIO1; //this is defalt pin for sensing RX done + _TXDonePin = pinDIO1; // this is defalt pin for sensing TX done + _RXDonePin = pinDIO1; // this is defalt pin for sensing RX done digitalWrite(_NSS, HIGH); pinMode(_NSS, OUTPUT); @@ -249,7 +250,7 @@ bool SX128XLT::begin(int8_t pinNSS, int8_t pinNRESET, int8_t pinRFBUSY, int8_t p if (_DIO1 >= 0) { - pinMode( _DIO1, INPUT); + pinMode(_DIO1, INPUT); } if ((_RXEN >= 0) && (_TXEN >= 0)) @@ -279,21 +280,20 @@ bool SX128XLT::begin(int8_t pinNSS, int8_t pinNRESET, int8_t pinRFBUSY, int8_t p return false; } - bool SX128XLT::begin(int8_t pinNSS, int8_t pinNRESET, int8_t pinRFBUSY, uint8_t device) { - //format 4 pins for IRQ use, NSS, NRESET, RFBUSY + // format 4 pins for IRQ use, NSS, NRESET, RFBUSY _NSS = pinNSS; _NRESET = pinNRESET; _RFBUSY = pinRFBUSY; - _DIO1 = -1; //not defined, so mark as unused - _DIO2 = -1; //not defined, so mark as unused - _DIO3 = -1; //not defined, so mark as unused - _RXEN = -1; //not defined, so mark as unused - _TXEN = -1; //not defined, so mark as unused + _DIO1 = -1; // not defined, so mark as unused + _DIO2 = -1; // not defined, so mark as unused + _DIO3 = -1; // not defined, so mark as unused + _RXEN = -1; // not defined, so mark as unused + _TXEN = -1; // not defined, so mark as unused _Device = device; - _TXDonePin = -1; //not defined, so mark as unused - _RXDonePin = -1; //not defined, so mark as unused + _TXDonePin = -1; // not defined, so mark as unused + _RXDonePin = -1; // not defined, so mark as unused digitalWrite(_NSS, HIGH); pinMode(_NSS, OUTPUT); @@ -324,27 +324,26 @@ bool SX128XLT::begin(int8_t pinNSS, int8_t pinNRESET, int8_t pinRFBUSY, uint8_t return false; } - bool SX128XLT::begin(int8_t pinNSS, int8_t pinRFBUSY, uint8_t device) { - //Format 5 pins for IRQ use, NSS, RFBUSY. Some form of additional external reset is needed for - //the device, since this routine does not use resetDevice(); - //A pull up on NSS is recommended, a low to high transitions on NSS at startup can put the device - //into a busy state, with no reset available to clear it. + // Format 5 pins for IRQ use, NSS, RFBUSY. Some form of additional external reset is needed for + // the device, since this routine does not use resetDevice(); + // A pull up on NSS is recommended, a low to high transitions on NSS at startup can put the device + // into a busy state, with no reset available to clear it. _NSS = pinNSS; - _NRESET = -1; //not defined, so mark as unused + _NRESET = -1; // not defined, so mark as unused _RFBUSY = pinRFBUSY; - _DIO1 = -1; //not defined, so mark as unused - _DIO2 = -1; //not defined, so mark as unused - _DIO3 = -1; //not defined, so mark as unused - _RXEN = -1; //not defined, so mark as unused - _TXEN = -1; //not defined, so mark as unused + _DIO1 = -1; // not defined, so mark as unused + _DIO2 = -1; // not defined, so mark as unused + _DIO3 = -1; // not defined, so mark as unused + _RXEN = -1; // not defined, so mark as unused + _TXEN = -1; // not defined, so mark as unused _Device = device; - _TXDonePin = -1; //not defined, so mark as unused - _RXDonePin = -1; //not defined, so mark as unused + _TXDonePin = -1; // not defined, so mark as unused + _RXDonePin = -1; // not defined, so mark as unused - digitalWrite(_NSS, HIGH); //make sure NSS state is high, before turning on output, a low to high transision here causes issues + digitalWrite(_NSS, HIGH); // make sure NSS state is high, before turning on output, a low to high transision here causes issues pinMode(_NSS, OUTPUT); digitalWrite(_NSS, HIGH); pinMode(_RFBUSY, INPUT); @@ -370,10 +369,9 @@ bool SX128XLT::begin(int8_t pinNSS, int8_t pinRFBUSY, uint8_t device) return false; } - void SX128XLT::rxEnable() { - //Enable RX mode on device such as Ebyte E28-2G4M20S which have RX and TX enable pins + // Enable RX mode on device such as Ebyte E28-2G4M20S which have RX and TX enable pins #ifdef SX128XDEBUGRXTX Serial.println(F("rxEnable()")); #endif @@ -382,10 +380,9 @@ void SX128XLT::rxEnable() digitalWrite(_TXEN, LOW); } - void SX128XLT::txEnable() { - //Enable RX mode on device such as Ebyte E28-2G4M20S which have RX and TX enable pins + // Enable RX mode on device such as Ebyte E28-2G4M20S which have RX and TX enable pins #ifdef SX128XDEBUGRXTX Serial.println(F("txEnable()")); #endif @@ -394,10 +391,9 @@ void SX128XLT::txEnable() digitalWrite(_TXEN, HIGH); } - -//Changes made November 2021 to improve performance of checkBusy(); -//As published the revised check busy function will used -//To use the previous version of code, remove the #define REVISEDCHECKBUSY at the top of this file +// Changes made November 2021 to improve performance of checkBusy(); +// As published the revised check busy function will used +// To use the previous version of code, remove the #define REVISEDCHECKBUSY at the top of this file #ifdef REVISEDCHECKBUSY void SX128XLT::checkBusy() { @@ -409,20 +405,18 @@ void SX128XLT::checkBusy() do { - if ( ((uint32_t) (millis() - startmS) > 9)) //wait 10mS for busy to complete + if (((uint32_t)(millis() - startmS) > 9)) // wait 10mS for busy to complete { Serial.println(F("ERROR - Busy Timeout!")); resetDevice(); setMode(MODE_STDBY_RC); - config(); //re-run saved config + config(); // re-run saved config break; } } while (digitalRead(_RFBUSY)); - } #endif - #ifndef REVISEDCHECKBUSY void SX128XLT::checkBusy() { @@ -438,20 +432,18 @@ void SX128XLT::checkBusy() delay(1); busy_timeout_cnt++; - if (busy_timeout_cnt > 10) //wait 10mS for busy to complete + if (busy_timeout_cnt > 10) // wait 10mS for busy to complete { Serial.println(F("ERROR - Busy Timeout!")); resetDevice(); setMode(MODE_STDBY_RC); - config(); //re-run saved config + config(); // re-run saved config break; } } - } #endif - bool SX128XLT::config() { #ifdef SX128XDEBUG @@ -465,12 +457,11 @@ bool SX128XLT::config() setRfFrequency(savedFrequency, savedOffset); setModulationParams(savedModParam1, savedModParam2, savedModParam3); setPacketParams(savedPacketParam1, savedPacketParam2, savedPacketParam3, savedPacketParam4, savedPacketParam5, savedPacketParam6, savedPacketParam7); - setDioIrqParams(savedIrqMask, savedDio1Mask, savedDio2Mask, savedDio3Mask); //set for IRQ on RX done on DIO1 + setDioIrqParams(savedIrqMask, savedDio1Mask, savedDio2Mask, savedDio3Mask); // set for IRQ on RX done on DIO1 setHighSensitivity(); return true; } - void SX128XLT::readRegisters(uint16_t address, uint8_t *buffer, uint16_t size) { uint16_t index; @@ -480,14 +471,14 @@ void SX128XLT::readRegisters(uint16_t address, uint8_t *buffer, uint16_t size) addr_l = address & 0x00FF; checkBusy(); -#ifdef USE_SPI_TRANSACTION //to use SPI_TRANSACTION enable define at beginning of CPP file +#ifdef USE_SPI_TRANSACTION // to use SPI_TRANSACTION enable define at beginning of CPP file SPI.beginTransaction(SPISettings(LTspeedMaximum, LTdataOrder, LTdataMode)); #endif digitalWrite(_NSS, LOW); SPI.transfer(RADIO_READ_REGISTER); - SPI.transfer(addr_h); //MSB - SPI.transfer(addr_l); //LSB + SPI.transfer(addr_h); // MSB + SPI.transfer(addr_l); // LSB SPI.transfer(0xFF); for (index = 0; index < size; index++) { @@ -499,10 +490,8 @@ void SX128XLT::readRegisters(uint16_t address, uint8_t *buffer, uint16_t size) #ifdef USE_SPI_TRANSACTION SPI.endTransaction(); #endif - } - uint8_t SX128XLT::readRegister(uint16_t address) { uint8_t data; @@ -511,7 +500,6 @@ uint8_t SX128XLT::readRegister(uint16_t address) return data; } - void SX128XLT::writeRegisters(uint16_t address, uint8_t *buffer, uint16_t size) { uint8_t addr_l, addr_h; @@ -521,14 +509,14 @@ void SX128XLT::writeRegisters(uint16_t address, uint8_t *buffer, uint16_t size) addr_h = address >> 8; checkBusy(); -#ifdef USE_SPI_TRANSACTION //to use SPI_TRANSACTION enable define at beginning of CPP file +#ifdef USE_SPI_TRANSACTION // to use SPI_TRANSACTION enable define at beginning of CPP file SPI.beginTransaction(SPISettings(LTspeedMaximum, LTdataOrder, LTdataMode)); #endif digitalWrite(_NSS, LOW); SPI.transfer(RADIO_WRITE_REGISTER); - SPI.transfer(addr_h); //MSB - SPI.transfer(addr_l); //LSB + SPI.transfer(addr_h); // MSB + SPI.transfer(addr_l); // LSB for (i = 0; i < size; i++) { @@ -540,27 +528,24 @@ void SX128XLT::writeRegisters(uint16_t address, uint8_t *buffer, uint16_t size) #ifdef USE_SPI_TRANSACTION SPI.endTransaction(); #endif - } - void SX128XLT::writeRegister(uint16_t address, uint8_t value) { - writeRegisters( address, &value, 1 ); + writeRegisters(address, &value, 1); } - void SX128XLT::writeCommand(uint8_t Opcode, uint8_t *buffer, uint16_t size) { #ifdef SX128XDEBUG - //Serial.print(F("writeCommand() ")); - //Serial.println(Opcode, HEX); + // Serial.print(F("writeCommand() ")); + // Serial.println(Opcode, HEX); #endif uint8_t index; checkBusy(); -#ifdef USE_SPI_TRANSACTION //to use SPI_TRANSACTION enable define at beginning of CPP file +#ifdef USE_SPI_TRANSACTION // to use SPI_TRANSACTION enable define at beginning of CPP file SPI.beginTransaction(SPISettings(LTspeedMaximum, LTdataOrder, LTdataMode)); #endif @@ -583,18 +568,17 @@ void SX128XLT::writeCommand(uint8_t Opcode, uint8_t *buffer, uint16_t size) } } - void SX128XLT::readCommand(uint8_t Opcode, uint8_t *buffer, uint16_t size) { #ifdef SX128XDEBUG - //Serial.print(F("readCommand() ")); - //Serial.println(Opcode, HEX); + // Serial.print(F("readCommand() ")); + // Serial.println(Opcode, HEX); #endif uint8_t i; checkBusy(); -#ifdef USE_SPI_TRANSACTION //to use SPI_TRANSACTION enable define at beginning of CPP file +#ifdef USE_SPI_TRANSACTION // to use SPI_TRANSACTION enable define at beginning of CPP file SPI.beginTransaction(SPISettings(LTspeedMaximum, LTdataOrder, LTdataMode)); #endif @@ -602,26 +586,24 @@ void SX128XLT::readCommand(uint8_t Opcode, uint8_t *buffer, uint16_t size) SPI.transfer(Opcode); SPI.transfer(0xFF); - for ( i = 0; i < size; i++ ) + for (i = 0; i < size; i++) { *(buffer + i) = SPI.transfer(0xFF); } digitalWrite(_NSS, HIGH); - #ifdef USE_SPI_TRANSACTION SPI.endTransaction(); #endif } - void SX128XLT::resetDevice() { #ifdef SX128XDEBUG Serial.println(F("resetDevice()")); #endif - //Note: in the IRQ TX and RX examples _NRESET is set to -1, if so dont attempt to toggle pin + // Note: in the IRQ TX and RX examples _NRESET is set to -1, if so dont attempt to toggle pin if (_NRESET >= 0) { @@ -633,19 +615,18 @@ void SX128XLT::resetDevice() } } - bool SX128XLT::checkDevice() { - //check there is a device out there, writes a register and reads back + // check there is a device out there, writes a register and reads back #ifdef SX128XDEBUG Serial.println(F("checkDevice()")); #endif uint8_t Regdata1, Regdata2; - Regdata1 = readRegister(0x0908); //low byte of frequency setting + Regdata1 = readRegister(0x0908); // low byte of frequency setting writeRegister(0x0908, (Regdata1 + 1)); - Regdata2 = readRegister(0x0908); //read changed value back - writeRegister(0x0908, Regdata1); //restore register to original value + Regdata2 = readRegister(0x0908); // read changed value back + writeRegister(0x0908, Regdata1); // restore register to original value if (Regdata2 == (Regdata1 + 1)) { @@ -657,8 +638,7 @@ bool SX128XLT::checkDevice() } } - -void SX128XLT::setupLoRa(uint32_t frequency, int32_t offset, uint8_t modParam1, uint8_t modParam2, uint8_t modParam3) +void SX128XLT::setupLoRa(uint32_t frequency, int32_t offset, uint8_t modParam1, uint8_t modParam2, uint8_t modParam3) { #ifdef SX128XDEBUG Serial.println(F("setupLoRa()")); @@ -675,11 +655,10 @@ void SX128XLT::setupLoRa(uint32_t frequency, int32_t offset, uint8_t modParam1, setHighSensitivity(); } - -void SX128XLT::setupFLRC(uint32_t frequency, int32_t offset, uint8_t modParam1, uint8_t modParam2, uint8_t modParam3, uint32_t syncword) +void SX128XLT::setupFLRC(uint32_t frequency, int32_t offset, uint8_t modParam1, uint8_t modParam2, uint8_t modParam3, uint32_t syncword) { - //see data sheet SX1280-1_V3.2 16.4 FLRC Modem: Increased PER in FLRC Packets with Synch - //some patterns of syncword can create increased error rates in FLRC + // see data sheet SX1280-1_V3.2 16.4 FLRC Modem: Increased PER in FLRC Packets with Synch + // some patterns of syncword can create increased error rates in FLRC #ifdef SX128XDEBUG Serial.println(F("setupFLRC()")); @@ -696,7 +675,6 @@ void SX128XLT::setupFLRC(uint32_t frequency, int32_t offset, uint8_t modParam1, setHighSensitivity(); } - void SX128XLT::setMode(uint8_t modeconfig) { #ifdef SX128XDEBUG @@ -707,7 +685,7 @@ void SX128XLT::setMode(uint8_t modeconfig) checkBusy(); -#ifdef USE_SPI_TRANSACTION //to use SPI_TRANSACTION enable define at beginning of CPP file +#ifdef USE_SPI_TRANSACTION // to use SPI_TRANSACTION enable define at beginning of CPP file SPI.beginTransaction(SPISettings(LTspeedMaximum, LTdataOrder, LTdataMode)); #endif @@ -721,10 +699,8 @@ void SX128XLT::setMode(uint8_t modeconfig) #endif _OperatingMode = modeconfig; - } - void SX128XLT::setRegulatorMode(uint8_t mode) { #ifdef SX128XDEBUG @@ -736,8 +712,7 @@ void SX128XLT::setRegulatorMode(uint8_t mode) writeCommand(RADIO_SET_REGULATORMODE, &mode, 1); } - -void SX128XLT::setPacketType(uint8_t packettype ) +void SX128XLT::setPacketType(uint8_t packettype) { #ifdef SX128XDEBUG Serial.println(F("setPacketType()")); @@ -747,7 +722,6 @@ void SX128XLT::setPacketType(uint8_t packettype ) writeCommand(RADIO_SET_PACKETTYPE, &packettype, 1); } - void SX128XLT::setRfFrequency(uint32_t frequency, int32_t offset) { #ifdef SX128XDEBUG @@ -760,14 +734,13 @@ void SX128XLT::setRfFrequency(uint32_t frequency, int32_t offset) frequency = frequency + offset; uint8_t buffer[3]; uint32_t freqtemp = 0; - freqtemp = ( uint32_t )( (double) frequency / (double)FREQ_STEP); - buffer[0] = ( uint8_t )( ( freqtemp >> 16 ) & 0xFF ); - buffer[1] = ( uint8_t )( ( freqtemp >> 8 ) & 0xFF ); - buffer[2] = ( uint8_t )( freqtemp & 0xFF ); + freqtemp = (uint32_t)((double)frequency / (double)FREQ_STEP); + buffer[0] = (uint8_t)((freqtemp >> 16) & 0xFF); + buffer[1] = (uint8_t)((freqtemp >> 8) & 0xFF); + buffer[2] = (uint8_t)(freqtemp & 0xFF); writeCommand(RADIO_SET_RFFREQUENCY, buffer, 3); } - void SX128XLT::setBufferBaseAddress(uint8_t txBaseAddress, uint8_t rxBaseAddress) { #ifdef SX128XDEBUG @@ -781,10 +754,9 @@ void SX128XLT::setBufferBaseAddress(uint8_t txBaseAddress, uint8_t rxBaseAddress writeCommand(RADIO_SET_BUFFERBASEADDRESS, buffer, 2); } - -void SX128XLT::setModulationParams(uint8_t modParam1, uint8_t modParam2, uint8_t modParam3) +void SX128XLT::setModulationParams(uint8_t modParam1, uint8_t modParam2, uint8_t modParam3) { - //sequence is spreading factor, bandwidth, coding rate. + // sequence is spreading factor, bandwidth, coding rate. #ifdef SX128XDEBUG Serial.println(F("setModulationParams()")); @@ -802,53 +774,52 @@ void SX128XLT::setModulationParams(uint8_t modParam1, uint8_t modParam2, uint8_t writeCommand(RADIO_SET_MODULATIONPARAMS, buffer, 3); - //implement data sheet additions, datasheet SX1280-1_V3.2section 14.47 + // implement data sheet additions, datasheet SX1280-1_V3.2section 14.47 - writeRegister( 0x93C, 0x1 ); + writeRegister(0x93C, 0x1); switch (modParam1) { - case LORA_SF5: - writeRegister( 0x925, 0x1E ); - break; - case LORA_SF6: - writeRegister( 0x925, 0x1E ); - break; + case LORA_SF5: + writeRegister(0x925, 0x1E); + break; + case LORA_SF6: + writeRegister(0x925, 0x1E); + break; - case LORA_SF7: - writeRegister( 0x925, 0x37 ); - break; + case LORA_SF7: + writeRegister(0x925, 0x37); + break; - case LORA_SF8: - writeRegister( 0x925, 0x37 ); - break; + case LORA_SF8: + writeRegister(0x925, 0x37); + break; - case LORA_SF9: - writeRegister( 0x925, 0x32 ); - break; + case LORA_SF9: + writeRegister(0x925, 0x32); + break; - case LORA_SF10: - writeRegister( 0x925, 0x32 ); - break; + case LORA_SF10: + writeRegister(0x925, 0x32); + break; - case LORA_SF11: - writeRegister( 0x925, 0x32 ); - break; + case LORA_SF11: + writeRegister(0x925, 0x32); + break; - case LORA_SF12: - writeRegister( 0x925, 0x32 ); - break; + case LORA_SF12: + writeRegister(0x925, 0x32); + break; - default: - break; + default: + break; } } - -void SX128XLT::setPacketParams(uint8_t packetParam1, uint8_t packetParam2, uint8_t packetParam3, uint8_t packetParam4, uint8_t packetParam5, uint8_t packetParam6, uint8_t packetParam7) +void SX128XLT::setPacketParams(uint8_t packetParam1, uint8_t packetParam2, uint8_t packetParam3, uint8_t packetParam4, uint8_t packetParam5, uint8_t packetParam6, uint8_t packetParam7) { - //This function is for FLRC mode - //For FLRC: order is PreambleLength, SyncWordLength, SyncWordMatch, HeaderType, PayloadLength, CrcLength, Whitening + // This function is for FLRC mode + // For FLRC: order is PreambleLength, SyncWordLength, SyncWordMatch, HeaderType, PayloadLength, CrcLength, Whitening #ifdef SX128XDEBUG Serial.println(F("SetPacketParams(7)")); @@ -873,11 +844,10 @@ void SX128XLT::setPacketParams(uint8_t packetParam1, uint8_t packetParam2, uint writeCommand(RADIO_SET_PACKETPARAMS, buffer, 7); } - -void SX128XLT::setPacketParams(uint8_t packetParam1, uint8_t packetParam2, uint8_t packetParam3, uint8_t packetParam4, uint8_t packetParam5) +void SX128XLT::setPacketParams(uint8_t packetParam1, uint8_t packetParam2, uint8_t packetParam3, uint8_t packetParam4, uint8_t packetParam5) { - //This function is for LORa mode - //For LoRa: order is PreambleLength, HeaderType, PayloadLength, CRC, InvertIQ/chirp invert + // This function is for LORa mode + // For LoRa: order is PreambleLength, HeaderType, PayloadLength, CRC, InvertIQ/chirp invert #ifdef SX128XDEBUG Serial.println(F("SetPacketParams(5)")); @@ -898,8 +868,7 @@ void SX128XLT::setPacketParams(uint8_t packetParam1, uint8_t packetParam2, uint writeCommand(RADIO_SET_PACKETPARAMS, buffer, 5); } - -void SX128XLT::setDioIrqParams(uint16_t irqMask, uint16_t dio1Mask, uint16_t dio2Mask, uint16_t dio3Mask ) +void SX128XLT::setDioIrqParams(uint16_t irqMask, uint16_t dio1Mask, uint16_t dio2Mask, uint16_t dio3Mask) { #ifdef SX128XDEBUG Serial.println(F("setDioIrqParams()")); @@ -912,21 +881,20 @@ void SX128XLT::setDioIrqParams(uint16_t irqMask, uint16_t dio1Mask, uint16_t dio uint8_t buffer[8]; - buffer[0] = (uint8_t) (irqMask >> 8); - buffer[1] = (uint8_t) (irqMask & 0xFF); - buffer[2] = (uint8_t) (dio1Mask >> 8); - buffer[3] = (uint8_t) (dio1Mask & 0xFF); - buffer[4] = (uint8_t) (dio2Mask >> 8); - buffer[5] = (uint8_t) (dio2Mask & 0xFF); - buffer[6] = (uint8_t) (dio3Mask >> 8); - buffer[7] = (uint8_t) (dio3Mask & 0xFF); + buffer[0] = (uint8_t)(irqMask >> 8); + buffer[1] = (uint8_t)(irqMask & 0xFF); + buffer[2] = (uint8_t)(dio1Mask >> 8); + buffer[3] = (uint8_t)(dio1Mask & 0xFF); + buffer[4] = (uint8_t)(dio2Mask >> 8); + buffer[5] = (uint8_t)(dio2Mask & 0xFF); + buffer[6] = (uint8_t)(dio3Mask >> 8); + buffer[7] = (uint8_t)(dio3Mask & 0xFF); writeCommand(RADIO_SET_DIOIRQPARAMS, buffer, 8); } - void SX128XLT::setHighSensitivity() { - //set bits 7,6 of REG_LNA_REGIME + // set bits 7,6 of REG_LNA_REGIME #ifdef SX128XDEBUG Serial.println(F("setHighSensitivity()")); #endif @@ -934,10 +902,9 @@ void SX128XLT::setHighSensitivity() writeRegister(REG_LNA_REGIME, (readRegister(REG_LNA_REGIME) | 0xC0)); } - void SX128XLT::setLowPowerRX() { - //clear bits 7,6 of REG_LNA_REGIME + // clear bits 7,6 of REG_LNA_REGIME #ifdef SX128XDEBUG Serial.println(F("setLowPowerRX()")); #endif @@ -945,10 +912,9 @@ void SX128XLT::setLowPowerRX() writeRegister(REG_LNA_REGIME, (readRegister(REG_LNA_REGIME) & 0x3F)); } - void SX128XLT::printModemSettings() { - //modified November 2021 to provide more details on FLRC + // modified November 2021 to provide more details on FLRC #ifdef SX128XDEBUG Serial.println(F("printModemSettings()")); #endif @@ -959,28 +925,28 @@ void SX128XLT::printModemSettings() switch (savedPacketType) { - case PACKET_TYPE_GFSK: - Serial.print(F("GFSK")); - break; + case PACKET_TYPE_GFSK: + Serial.print(F("GFSK")); + break; - case PACKET_TYPE_LORA: - Serial.print(F("LORA")); - break; + case PACKET_TYPE_LORA: + Serial.print(F("LORA")); + break; - case PACKET_TYPE_RANGING: - Serial.print(F("RANGING")); - break; + case PACKET_TYPE_RANGING: + Serial.print(F("RANGING")); + break; - case PACKET_TYPE_FLRC: - Serial.print(F("FLRC")); - break; + case PACKET_TYPE_FLRC: + Serial.print(F("FLRC")); + break; - case PACKET_TYPE_BLE: - Serial.print(F("BLE")); - break; + case PACKET_TYPE_BLE: + Serial.print(F("BLE")); + break; - default: - Serial.print(F("Unknown")); + default: + Serial.print(F("Unknown")); } Serial.print(F(",")); @@ -1002,75 +968,74 @@ void SX128XLT::printModemSettings() Serial.print(F(",BandwidthBitRate_")); switch (savedModParam1) { - case FLRC_BR_1_300_BW_1_2: - Serial.print(F("FLRC_BR_1_300_BW_1_2")); - break; + case FLRC_BR_1_300_BW_1_2: + Serial.print(F("FLRC_BR_1_300_BW_1_2")); + break; - case FLRC_BR_1_000_BW_1_2: - Serial.print(F("FLRC_BR_1_000_BW_1_2")); - break; + case FLRC_BR_1_000_BW_1_2: + Serial.print(F("FLRC_BR_1_000_BW_1_2")); + break; - case FLRC_BR_0_650_BW_0_6: - Serial.print(F("FLRC_BR_0_650_BW_0_6")); - break; + case FLRC_BR_0_650_BW_0_6: + Serial.print(F("FLRC_BR_0_650_BW_0_6")); + break; - case FLRC_BR_0_520_BW_0_6: - Serial.print(F("FLRC_BR_0_520_BW_0_6")); - break; + case FLRC_BR_0_520_BW_0_6: + Serial.print(F("FLRC_BR_0_520_BW_0_6")); + break; - case FLRC_BR_0_325_BW_0_3: - Serial.print(F("FLRC_BR_0_325_BW_0_3")); - break; + case FLRC_BR_0_325_BW_0_3: + Serial.print(F("FLRC_BR_0_325_BW_0_3")); + break; - case FLRC_BR_0_260_BW_0_3: - Serial.print(F("FLRC_BR_0_260_BW_0_3")); - break; + case FLRC_BR_0_260_BW_0_3: + Serial.print(F("FLRC_BR_0_260_BW_0_3")); + break; - default: - Serial.print(F("Unknown")); + default: + Serial.print(F("Unknown")); } Serial.print(F(",CodingRate_")); switch (savedModParam2) { - case FLRC_CR_1_2: - Serial.print(F("CR_1_2")); - break; + case FLRC_CR_1_2: + Serial.print(F("CR_1_2")); + break; - case FLRC_CR_3_4: - Serial.print(F("CR_3_4")); - break; + case FLRC_CR_3_4: + Serial.print(F("CR_3_4")); + break; - case FLRC_CR_1_0: - Serial.print(F("CR_1_0")); - break; + case FLRC_CR_1_0: + Serial.print(F("CR_1_0")); + break; - default: - Serial.print(F("Unknown")); + default: + Serial.print(F("Unknown")); } Serial.print(F(",BT_")); switch (savedModParam3) { - case BT_DIS: - Serial.print(F("NoFiltering")); - break; + case BT_DIS: + Serial.print(F("NoFiltering")); + break; - case BT_1: - Serial.print(F("1")); - break; + case BT_1: + Serial.print(F("1")); + break; - case BT_0_5: - Serial.print(F("0_5")); - break; + case BT_0_5: + Serial.print(F("0_5")); + break; - default: - Serial.print(F("Unknown")); + default: + Serial.print(F("Unknown")); } } } - void SX128XLT::printDevice() { #ifdef SX128XDEBUG @@ -1079,20 +1044,19 @@ void SX128XLT::printDevice() switch (_Device) { - case DEVICE_SX1280: - Serial.print(F("SX1280")); - break; + case DEVICE_SX1280: + Serial.print(F("SX1280")); + break; - case DEVICE_SX1281: - Serial.print(F("SX1281")); - break; + case DEVICE_SX1281: + Serial.print(F("SX1281")); + break; - default: - Serial.print(F("Unknown Device")); + default: + Serial.print(F("Unknown Device")); } } - uint32_t SX128XLT::getFreqInt() { @@ -1100,7 +1064,7 @@ uint32_t SX128XLT::getFreqInt() Serial.println(F("getFreqInt")); #endif - //get the current set device frequency, return as long integer + // get the current set device frequency, return as long integer uint8_t Msb = 0; uint8_t Mid = 0; uint8_t Lsb = 0; @@ -1108,9 +1072,9 @@ uint32_t SX128XLT::getFreqInt() uint32_t uinttemp; float floattemp; - LTUNUSED(Msb); //to prevent a compiler warning - LTUNUSED(Mid); //to prevent a compiler warning - LTUNUSED(Lsb); //to prevent a compiler warning + LTUNUSED(Msb); // to prevent a compiler warning + LTUNUSED(Mid); // to prevent a compiler warning + LTUNUSED(Lsb); // to prevent a compiler warning if (savedPacketType == PACKET_TYPE_LORA) { @@ -1139,7 +1103,6 @@ uint32_t SX128XLT::getFreqInt() return uinttemp; } - uint8_t SX128XLT::getLoRaSF() { #ifdef SX128XDEBUG @@ -1148,7 +1111,6 @@ uint8_t SX128XLT::getLoRaSF() return (savedModParam1 >> 4); } - uint32_t SX128XLT::returnBandwidth(uint8_t data) { #ifdef SX128XDEBUG @@ -1157,26 +1119,25 @@ uint32_t SX128XLT::returnBandwidth(uint8_t data) switch (data) { - case LORA_BW_0200: - return 203125; + case LORA_BW_0200: + return 203125; - case LORA_BW_0400: - return 406250; + case LORA_BW_0400: + return 406250; - case LORA_BW_0800: - return 812500; + case LORA_BW_0800: + return 812500; - case LORA_BW_1600: - return 1625000; + case LORA_BW_1600: + return 1625000; - default: - break; + default: + break; } - return 0x0; //so that a bandwidth not set can be identified + return 0x0; // so that a bandwidth not set can be identified } - uint8_t SX128XLT::getLoRaCodingRate() { #ifdef SX128XDEBUG @@ -1186,10 +1147,9 @@ uint8_t SX128XLT::getLoRaCodingRate() return savedModParam3; } - uint8_t SX128XLT::getInvertIQ() { - //IQ mode reg 0x33 + // IQ mode reg 0x33 #ifdef SX128XDEBUG Serial.println(F("getInvertIQ")); #endif @@ -1197,7 +1157,6 @@ uint8_t SX128XLT::getInvertIQ() return savedPacketParam5; } - uint16_t SX128XLT::getPreamble() { #ifdef SX128XDEBUG @@ -1207,10 +1166,9 @@ uint16_t SX128XLT::getPreamble() return savedPacketParam1; } - void SX128XLT::printOperatingSettings() { - //modified November 2021 to provide more details on FLRC + // modified November 2021 to provide more details on FLRC #ifdef SX128XDEBUG Serial.println(F("printOperatingSettings()")); #endif @@ -1221,29 +1179,28 @@ void SX128XLT::printOperatingSettings() switch (savedPacketType) { - case PACKET_TYPE_GFSK: - Serial.print(F("GFSK")); - break; + case PACKET_TYPE_GFSK: + Serial.print(F("GFSK")); + break; - case PACKET_TYPE_LORA: - Serial.print(F("LORA")); - break; + case PACKET_TYPE_LORA: + Serial.print(F("LORA")); + break; - case PACKET_TYPE_RANGING: - Serial.print(F("RANGING")); - break; + case PACKET_TYPE_RANGING: + Serial.print(F("RANGING")); + break; - case PACKET_TYPE_FLRC: - Serial.print(F("FLRC")); - break; + case PACKET_TYPE_FLRC: + Serial.print(F("FLRC")); + break; + case PACKET_TYPE_BLE: + Serial.print(F("BLE")); + break; - case PACKET_TYPE_BLE: - Serial.print(F("BLE")); - break; - - default: - Serial.print(F("Unknown")); + default: + Serial.print(F("Unknown")); } if ((savedPacketType == PACKET_TYPE_LORA) || (savedPacketType == PACKET_TYPE_RANGING)) @@ -1253,50 +1210,50 @@ void SX128XLT::printOperatingSettings() switch (savedPacketParam2) { - case LORA_PACKET_VARIABLE_LENGTH: - Serial.print(F(",Explicit")); - break; + case LORA_PACKET_VARIABLE_LENGTH: + Serial.print(F(",Explicit")); + break; - case LORA_PACKET_FIXED_LENGTH: - Serial.print(F(",Implicit")); - break; + case LORA_PACKET_FIXED_LENGTH: + Serial.print(F(",Implicit")); + break; - default: - Serial.print(F(",Unknown")); + default: + Serial.print(F(",Unknown")); } Serial.print(F(",PayloadL_")); - Serial.print(savedPacketParam3); //savedPacketParam3 is payload length + Serial.print(savedPacketParam3); // savedPacketParam3 is payload length Serial.print(F(",CRC_")); switch (savedPacketParam4) { - case LORA_CRC_ON: - Serial.print(F("ON")); - break; + case LORA_CRC_ON: + Serial.print(F("ON")); + break; - case LORA_CRC_OFF: - Serial.print(F("OFF")); - break; + case LORA_CRC_OFF: + Serial.print(F("OFF")); + break; - default: - Serial.print(F("Unknown")); + default: + Serial.print(F("Unknown")); } Serial.print(F(",IQ_")); switch (savedPacketParam5) { - case LORA_IQ_INVERTED: - Serial.print(F("INVERTED")); - break; + case LORA_IQ_INVERTED: + Serial.print(F("INVERTED")); + break; - case LORA_IQ_NORMAL: - Serial.print(F("NORMAL")); - break; + case LORA_IQ_NORMAL: + Serial.print(F("NORMAL")); + break; - default: - Serial.print(F("Unknown")); + default: + Serial.print(F("Unknown")); } Serial.print(F(",LNAgain_")); @@ -1311,7 +1268,6 @@ void SX128XLT::printOperatingSettings() } } - if (savedPacketType == PACKET_TYPE_FLRC) { Serial.print(F(",Preamble_")); @@ -1323,50 +1279,50 @@ void SX128XLT::printOperatingSettings() switch (savedPacketParam3) { - case RADIO_RX_MATCH_SYNCWORD_1: - Serial.print(F("1")); - break; + case RADIO_RX_MATCH_SYNCWORD_1: + Serial.print(F("1")); + break; - case RADIO_RX_MATCH_SYNCWORD_2: - Serial.print(F("2")); - break; + case RADIO_RX_MATCH_SYNCWORD_2: + Serial.print(F("2")); + break; - case RADIO_RX_MATCH_SYNCWORD_1_2: - Serial.print(F("1_2")); - break; + case RADIO_RX_MATCH_SYNCWORD_1_2: + Serial.print(F("1_2")); + break; - case RADIO_RX_MATCH_SYNCWORD_3: - Serial.print(F("3")); - break; + case RADIO_RX_MATCH_SYNCWORD_3: + Serial.print(F("3")); + break; - case RADIO_RX_MATCH_SYNCWORD_1_3: - Serial.print(F("1_3")); - break; + case RADIO_RX_MATCH_SYNCWORD_1_3: + Serial.print(F("1_3")); + break; - case RADIO_RX_MATCH_SYNCWORD_2_3: - Serial.print(F("2_3")); - break; + case RADIO_RX_MATCH_SYNCWORD_2_3: + Serial.print(F("2_3")); + break; - case RADIO_RX_MATCH_SYNCWORD_1_2_3: - Serial.print(F("1_2_3")); - break; + case RADIO_RX_MATCH_SYNCWORD_1_2_3: + Serial.print(F("1_2_3")); + break; - default: - Serial.print(F("Unknown")); + default: + Serial.print(F("Unknown")); } switch (savedPacketParam4) { - case RADIO_PACKET_VARIABLE_LENGTH: - Serial.print(F(",VariableLengthPacket")); - break; + case RADIO_PACKET_VARIABLE_LENGTH: + Serial.print(F(",VariableLengthPacket")); + break; - case RADIO_PACKET_FIXED_LENGTH: - Serial.print(F(",FixedLengthPacket")); - break; + case RADIO_PACKET_FIXED_LENGTH: + Serial.print(F(",FixedLengthPacket")); + break; - default: - Serial.print(F("Unknown")); + default: + Serial.print(F("Unknown")); } Serial.print(F(",PayloadLength_")); @@ -1378,16 +1334,16 @@ void SX128XLT::printOperatingSettings() switch (savedPacketParam7) { - case RADIO_WHITENING_OFF: - Serial.print(F("OFF")); - break; + case RADIO_WHITENING_OFF: + Serial.print(F("OFF")); + break; - case RADIO_WHITENING_ON: - Serial.print(F("ON")); - break; + case RADIO_WHITENING_ON: + Serial.print(F("ON")); + break; - default: - Serial.print(F("Unknown")); + default: + Serial.print(F("Unknown")); } Serial.print(F(",LNAgain_")); @@ -1403,7 +1359,6 @@ void SX128XLT::printOperatingSettings() } } - uint8_t SX128XLT::getLNAgain() { #ifdef SX128XDEBUG @@ -1413,10 +1368,9 @@ uint8_t SX128XLT::getLNAgain() return (readRegister(REG_LNA_REGIME) & 0xC0); } - void SX128XLT::printRegisters(uint16_t Start, uint16_t End) { - //prints the contents of SX1280 registers to serial monitor + // prints the contents of SX1280 registers to serial monitor #ifdef SX128XDEBUG Serial.println(F("printRegisters()")); @@ -1427,10 +1381,10 @@ void SX128XLT::printRegisters(uint16_t Start, uint16_t End) Serial.print(F("Reg 0 1 2 3 4 5 6 7 8 9 A B C D E F")); Serial.println(); - for (Loopv1 = Start; Loopv1 <= End;) //32 lines + for (Loopv1 = Start; Loopv1 <= End;) // 32 lines { Serial.print(F("0x")); - Serial.print((Loopv1), HEX); //print the register number + Serial.print((Loopv1), HEX); // print the register number Serial.print(F(" ")); for (Loopv2 = 0; Loopv2 <= 15; Loopv2++) { @@ -1439,7 +1393,7 @@ void SX128XLT::printRegisters(uint16_t Start, uint16_t End) { Serial.print(F("0")); } - Serial.print(RegData, HEX); //print the register number + Serial.print(RegData, HEX); // print the register number Serial.print(F(" ")); Loopv1++; } @@ -1447,7 +1401,6 @@ void SX128XLT::printRegisters(uint16_t Start, uint16_t End) } } - void SX128XLT::printASCIIPacket(uint8_t *buffer, uint8_t size) { #ifdef SX128XDEBUG @@ -1460,10 +1413,8 @@ void SX128XLT::printASCIIPacket(uint8_t *buffer, uint8_t size) { Serial.write(buffer[index]); } - } - uint8_t SX128XLT::transmit(uint8_t *txbuffer, uint8_t size, uint16_t timeout, int8_t txpower, uint8_t wait) { #ifdef SX128XDEBUG @@ -1480,7 +1431,7 @@ uint8_t SX128XLT::transmit(uint8_t *txbuffer, uint8_t size, uint16_t timeout, in setMode(MODE_STDBY_RC); checkBusy(); -#ifdef USE_SPI_TRANSACTION //to use SPI_TRANSACTION enable define at beginning of CPP file +#ifdef USE_SPI_TRANSACTION // to use SPI_TRANSACTION enable define at beginning of CPP file SPI.beginTransaction(SPISettings(LTspeedMaximum, LTdataOrder, LTdataMode)); #endif @@ -1503,19 +1454,20 @@ uint8_t SX128XLT::transmit(uint8_t *txbuffer, uint8_t size, uint16_t timeout, in _TXPacketL = size; setPayloadLength(_TXPacketL); setTxParams(txpower, RAMP_TIME); - setDioIrqParams(IRQ_RADIO_ALL, (IRQ_TX_DONE + IRQ_RX_TX_TIMEOUT), 0, 0); //set for IRQ on TX done and timeout on DIO1 - setTx(timeout); //this starts the TX + setDioIrqParams(IRQ_RADIO_ALL, (IRQ_TX_DONE + IRQ_RX_TX_TIMEOUT), 0, 0); // set for IRQ on TX done and timeout on DIO1 + setTx(timeout); // this starts the TX if (!wait) { return _TXPacketL; } - while (!digitalRead(_TXDonePin)); //Wait for DIO1 to go high + while (!digitalRead(_TXDonePin)) + ; // Wait for DIO1 to go high - setMode(MODE_STDBY_RC); //ensure we leave function with TX off + setMode(MODE_STDBY_RC); // ensure we leave function with TX off - if (readIrqStatus() & IRQ_RX_TX_TIMEOUT ) //check for timeout + if (readIrqStatus() & IRQ_RX_TX_TIMEOUT) // check for timeout { return 0; } @@ -1525,7 +1477,6 @@ uint8_t SX128XLT::transmit(uint8_t *txbuffer, uint8_t size, uint16_t timeout, in } } - uint8_t SX128XLT::transmitIRQ(uint8_t *txbuffer, uint8_t size, uint16_t timeout, int8_t txpower, uint8_t wait) { #ifdef SX128XDEBUG @@ -1542,7 +1493,7 @@ uint8_t SX128XLT::transmitIRQ(uint8_t *txbuffer, uint8_t size, uint16_t timeout, setMode(MODE_STDBY_RC); checkBusy(); -#ifdef USE_SPI_TRANSACTION //to use SPI_TRANSACTION enable define at beginning of CPP file +#ifdef USE_SPI_TRANSACTION // to use SPI_TRANSACTION enable define at beginning of CPP file SPI.beginTransaction(SPISettings(LTspeedMaximum, LTdataOrder, LTdataMode)); #endif @@ -1565,19 +1516,20 @@ uint8_t SX128XLT::transmitIRQ(uint8_t *txbuffer, uint8_t size, uint16_t timeout, _TXPacketL = size; setPayloadLength(_TXPacketL); setTxParams(txpower, RAMP_TIME); - setTx(timeout); //this starts the TX + setTx(timeout); // this starts the TX if (!wait) { return _TXPacketL; } - //0x4001 = IRQ_TX_DONE + IRQ_RX_TX_TIMEOUT - while (!(readIrqStatus() & 0x4001 )); //wait for IRQs going active + // 0x4001 = IRQ_TX_DONE + IRQ_RX_TX_TIMEOUT + while (!(readIrqStatus() & 0x4001)) + ; // wait for IRQs going active - setMode(MODE_STDBY_RC); //ensure we leave function with TX off + setMode(MODE_STDBY_RC); // ensure we leave function with TX off - if (readIrqStatus() & IRQ_RX_TX_TIMEOUT ) //check for timeout + if (readIrqStatus() & IRQ_RX_TX_TIMEOUT) // check for timeout { return 0; } @@ -1587,7 +1539,6 @@ uint8_t SX128XLT::transmitIRQ(uint8_t *txbuffer, uint8_t size, uint16_t timeout, } } - void SX128XLT::setTxParams(int8_t TXpower, uint8_t RampTime) { #ifdef SX128XDEBUG @@ -1598,13 +1549,12 @@ void SX128XLT::setTxParams(int8_t TXpower, uint8_t RampTime) savedTXPower = TXpower; - //power register is set to 0 to 31 which is -18dBm to +12dBm + // power register is set to 0 to 31 which is -18dBm to +12dBm buffer[0] = (TXpower + 18); buffer[1] = (uint8_t)RampTime; writeCommand(RADIO_SET_TXPARAMS, buffer, 2); } - void SX128XLT::setTx(uint16_t timeout) { @@ -1619,14 +1569,13 @@ void SX128XLT::setTx(uint16_t timeout) uint8_t buffer[3]; - clearIrqStatus(IRQ_RADIO_ALL); //clear all interrupt flags + clearIrqStatus(IRQ_RADIO_ALL); // clear all interrupt flags buffer[0] = _PERIODBASE; - buffer[1] = ( uint8_t )( ( timeout >> 8 ) & 0x00FF ); - buffer[2] = ( uint8_t )( timeout & 0x00FF ); - writeCommand(RADIO_SET_TX, buffer, 3 ); + buffer[1] = (uint8_t)((timeout >> 8) & 0x00FF); + buffer[2] = (uint8_t)(timeout & 0x00FF); + writeCommand(RADIO_SET_TX, buffer, 3); } - void SX128XLT::clearIrqStatus(uint16_t irqMask) { #ifdef SX128XDEBUG @@ -1635,12 +1584,11 @@ void SX128XLT::clearIrqStatus(uint16_t irqMask) uint8_t buffer[2]; - buffer[0] = (uint8_t) (irqMask >> 8); - buffer[1] = (uint8_t) (irqMask & 0xFF); + buffer[0] = (uint8_t)(irqMask >> 8); + buffer[1] = (uint8_t)(irqMask & 0xFF); writeCommand(RADIO_CLR_IRQSTATUS, buffer, 2); } - uint16_t SX128XLT::readIrqStatus() { #ifdef SX128XDEBUG @@ -1655,7 +1603,6 @@ uint16_t SX128XLT::readIrqStatus() return temp; } - void SX128XLT::printIrqStatus() { #ifdef SX128XDEBUG @@ -1665,104 +1612,103 @@ void SX128XLT::printIrqStatus() uint16_t _IrqStatus; _IrqStatus = readIrqStatus(); - //0x0001 + // 0x0001 if (_IrqStatus & IRQ_TX_DONE) { Serial.print(F(",IRQ_TX_DONE")); } - //0x0002 + // 0x0002 if (_IrqStatus & IRQ_RX_DONE) { Serial.print(F(",IRQ_RX_DONE")); } - //0x0004 + // 0x0004 if (_IrqStatus & IRQ_SYNCWORD_VALID) { Serial.print(F(",IRQ_SYNCWORD_VALID")); } - //0x0008 + // 0x0008 if (_IrqStatus & IRQ_SYNCWORD_ERROR) { Serial.print(F(",IRQ_SYNCWORD_ERROR")); } - //0x0010 + // 0x0010 if (_IrqStatus & IRQ_HEADER_VALID) { Serial.print(F(",IRQ_HEADER_VALID")); } - //0x0020 + // 0x0020 if (_IrqStatus & IRQ_HEADER_ERROR) { Serial.print(F(",IRQ_HEADER_ERROR")); } - //0x0040 + // 0x0040 if (_IrqStatus & IRQ_CRC_ERROR) { Serial.print(F(",IRQ_CRC_ERROR")); } - //0x0080 + // 0x0080 if (_IrqStatus & IRQ_RANGING_SLAVE_RESPONSE_DONE) { Serial.print(F(",IRQ_RANGING_SLAVE_RESPONSE_DONE")); } - //0x0100 + // 0x0100 if (_IrqStatus & IRQ_RANGING_SLAVE_REQUEST_DISCARDED) { Serial.print(F(",IRQ_RANGING_SLAVE_REQUEST_DISCARDED")); } - //0x0200 + // 0x0200 if (_IrqStatus & IRQ_RANGING_MASTER_RESULT_VALID) { Serial.print(F(",IRQ_RANGING_MASTER_RESULT_VALID")); } - //0x0400 + // 0x0400 if (_IrqStatus & IRQ_RANGING_MASTER_RESULT_TIMEOUT) { Serial.print(F(",IRQ_RANGING_MASTER_RESULT_TIMEOUT")); } - //0x0800 + // 0x0800 if (_IrqStatus & IRQ_RANGING_SLAVE_REQUEST_VALID) { Serial.print(F(",IRQ_RANGING_SLAVE_REQUEST_VALID")); } - //0x1000 + // 0x1000 if (_IrqStatus & IRQ_CAD_DONE) { Serial.print(F(",IRQ_CAD_DONE")); } - //0x2000 + // 0x2000 if (_IrqStatus & IRQ_CAD_ACTIVITY_DETECTED) { Serial.print(F(",IRQ_CAD_ACTIVITY_DETECTED")); } - //0x4000 + // 0x4000 if (_IrqStatus & IRQ_RX_TX_TIMEOUT) { Serial.print(F(",IRQ_RX_TX_TIMEOUT")); } - //0x8000 + // 0x8000 if (_IrqStatus & IRQ_PREAMBLE_DETECTED) { Serial.print(F(",IRQ_PREAMBLE_DETECTED")); } } - uint16_t SX128XLT::CRCCCITT(uint8_t *buffer, uint32_t size, uint16_t start) { #ifdef SX128XDEBUG @@ -1773,7 +1719,7 @@ uint16_t SX128XLT::CRCCCITT(uint8_t *buffer, uint32_t size, uint16_t start) uint16_t libraryCRC; uint8_t j; - libraryCRC = start; //start value for CRC16 + libraryCRC = start; // start value for CRC16 for (index = 0; index < size; index++) { @@ -1790,7 +1736,6 @@ uint16_t SX128XLT::CRCCCITT(uint8_t *buffer, uint32_t size, uint16_t start) return libraryCRC; } - uint8_t SX128XLT::receive(uint8_t *rxbuffer, uint8_t size, uint16_t timeout, uint8_t wait) { #ifdef SX128XDEBUG @@ -1801,41 +1746,42 @@ uint8_t SX128XLT::receive(uint8_t *rxbuffer, uint8_t size, uint16_t timeout, uin uint16_t regdata; uint8_t buffer[2]; - setDioIrqParams(IRQ_RADIO_ALL, (IRQ_RX_DONE + IRQ_RX_TX_TIMEOUT + IRQ_HEADER_ERROR), 0, 0); //set for IRQ on RX done or timeout + setDioIrqParams(IRQ_RADIO_ALL, (IRQ_RX_DONE + IRQ_RX_TX_TIMEOUT + IRQ_HEADER_ERROR), 0, 0); // set for IRQ on RX done or timeout setRx(timeout); if (!wait) { - return 0; //not wait requested so no packet length to pass + return 0; // not wait requested so no packet length to pass } - while (!digitalRead(_RXDonePin)); //Wait for DIO1 to go high + while (!digitalRead(_RXDonePin)) + ; // Wait for DIO1 to go high - setMode(MODE_STDBY_RC); //ensure to stop further packet reception + setMode(MODE_STDBY_RC); // ensure to stop further packet reception regdata = readIrqStatus(); - if ( (regdata & IRQ_HEADER_ERROR) | (regdata & IRQ_CRC_ERROR) | (regdata & IRQ_RX_TX_TIMEOUT ) | (regdata & IRQ_SYNCWORD_ERROR )) //check if any of the preceding IRQs is set + if ((regdata & IRQ_HEADER_ERROR) | (regdata & IRQ_CRC_ERROR) | (regdata & IRQ_RX_TX_TIMEOUT) | (regdata & IRQ_SYNCWORD_ERROR)) // check if any of the preceding IRQs is set { - return 0; //packet is errored somewhere so return 0 + return 0; // packet is errored somewhere so return 0 } readCommand(RADIO_GET_RXBUFFERSTATUS, buffer, 2); _RXPacketL = buffer[0]; - if (_RXPacketL > size) //check passed buffer is big enough for packet + if (_RXPacketL > size) // check passed buffer is big enough for packet { - _RXPacketL = size; //truncate packet if not enough space + _RXPacketL = size; // truncate packet if not enough space } RXstart = buffer[1]; RXend = RXstart + _RXPacketL; checkBusy(); -#ifdef USE_SPI_TRANSACTION //to use SPI_TRANSACTION enable define at beginning of CPP file +#ifdef USE_SPI_TRANSACTION // to use SPI_TRANSACTION enable define at beginning of CPP file SPI.beginTransaction(SPISettings(LTspeedMaximum, LTdataOrder, LTdataMode)); #endif - digitalWrite(_NSS, LOW); //start the burst read + digitalWrite(_NSS, LOW); // start the burst read SPI.transfer(RADIO_READ_BUFFER); SPI.transfer(RXstart); SPI.transfer(0xFF); @@ -1855,7 +1801,6 @@ uint8_t SX128XLT::receive(uint8_t *rxbuffer, uint8_t size, uint16_t timeout, uin return _RXPacketL; } - uint8_t SX128XLT::receiveIRQ(uint8_t *rxbuffer, uint8_t size, uint16_t timeout, uint8_t wait) { #ifdef SX128XDEBUG @@ -1870,25 +1815,26 @@ uint8_t SX128XLT::receiveIRQ(uint8_t *rxbuffer, uint8_t size, uint16_t timeout, if (!wait) { - return 0; //not wait requested so no packet length to pass + return 0; // not wait requested so no packet length to pass } - //0x4022 = IRQ_RX_DONE + IRQ_RX_TX_TIMEOUT + IRQ_HEADER_ERROR - while (!(readIrqStatus() & 0x4022 )); //wait for IRQs going active + // 0x4022 = IRQ_RX_DONE + IRQ_RX_TX_TIMEOUT + IRQ_HEADER_ERROR + while (!(readIrqStatus() & 0x4022)) + ; // wait for IRQs going active - setMode(MODE_STDBY_RC); //ensure to stop further packet reception + setMode(MODE_STDBY_RC); // ensure to stop further packet reception - if ( readIrqStatus() & (IRQ_HEADER_ERROR + IRQ_CRC_ERROR + IRQ_RX_TX_TIMEOUT + IRQ_SYNCWORD_ERROR)) + if (readIrqStatus() & (IRQ_HEADER_ERROR + IRQ_CRC_ERROR + IRQ_RX_TX_TIMEOUT + IRQ_SYNCWORD_ERROR)) { - return 0; //packet is errored somewhere so return 0 + return 0; // packet is errored somewhere so return 0 } readCommand(RADIO_GET_RXBUFFERSTATUS, buffer, 2); _RXPacketL = buffer[0]; - if (_RXPacketL > size) //check passed buffer is big enough for packet + if (_RXPacketL > size) // check passed buffer is big enough for packet { - _RXPacketL = size; //truncate packet if not enough space + _RXPacketL = size; // truncate packet if not enough space } RXstart = buffer[1]; @@ -1896,11 +1842,11 @@ uint8_t SX128XLT::receiveIRQ(uint8_t *rxbuffer, uint8_t size, uint16_t timeout, checkBusy(); -#ifdef USE_SPI_TRANSACTION //to use SPI_TRANSACTION enable define at beginning of CPP file +#ifdef USE_SPI_TRANSACTION // to use SPI_TRANSACTION enable define at beginning of CPP file SPI.beginTransaction(SPISettings(LTspeedMaximum, LTdataOrder, LTdataMode)); #endif - digitalWrite(_NSS, LOW); //start the burst read + digitalWrite(_NSS, LOW); // start the burst read SPI.transfer(RADIO_READ_BUFFER); SPI.transfer(RXstart); SPI.transfer(0xFF); @@ -1920,7 +1866,6 @@ uint8_t SX128XLT::receiveIRQ(uint8_t *rxbuffer, uint8_t size, uint16_t timeout, return _RXPacketL; } - int16_t SX128XLT::readPacketRSSI2() { #ifdef SX128XDEBUG @@ -1930,13 +1875,12 @@ int16_t SX128XLT::readPacketRSSI2() uint8_t status[5]; int16_t rssi; - readCommand(RADIO_GET_PACKETSTATUS, status, 5) ; + readCommand(RADIO_GET_PACKETSTATUS, status, 5); rssi = -status[0] / 2; return rssi; } - int16_t SX128XLT::readPacketRSSI() { #ifdef SX128XDEBUG @@ -1944,18 +1888,18 @@ int16_t SX128XLT::readPacketRSSI() #endif int8_t snr; - int16_t rssi = 0; //so routine returns 0 if packet not LoRa or FLRC + int16_t rssi = 0; // so routine returns 0 if packet not LoRa or FLRC uint8_t status[5]; snr = readPacketSNR(); - readCommand(RADIO_GET_PACKETSTATUS, status, 5) ; + readCommand(RADIO_GET_PACKETSTATUS, status, 5); if (savedPacketType == PACKET_TYPE_LORA) { rssi = -status[0] / 2; if (snr < 0) { - rssi = rssi + snr; + rssi = rssi + snr; } } @@ -1967,7 +1911,6 @@ int16_t SX128XLT::readPacketRSSI() return rssi; } - int8_t SX128XLT::readPacketSNR() { #ifdef SX128XDEBUG @@ -1977,21 +1920,20 @@ int8_t SX128XLT::readPacketSNR() uint8_t status[5]; int8_t snr; - readCommand(RADIO_GET_PACKETSTATUS, status, 5) ; + readCommand(RADIO_GET_PACKETSTATUS, status, 5); - if ( status[1] < 128 ) + if (status[1] < 128) { - snr = status[1] / 4 ; + snr = status[1] / 4; } else { - snr = (( status[1] - 256 ) / 4); + snr = ((status[1] - 256) / 4); } return snr; } - uint8_t SX128XLT::readRXPacketL() { #ifdef SX128XDEBUG @@ -2005,7 +1947,6 @@ uint8_t SX128XLT::readRXPacketL() return _RXPacketL; } - void SX128XLT::setRx(uint16_t timeout) { #ifdef SX128XDEBUG @@ -2019,14 +1960,13 @@ void SX128XLT::setRx(uint16_t timeout) rxEnable(); } - clearIrqStatus(IRQ_RADIO_ALL); //clear all interrupt flags - buffer[0] = _PERIODBASE; //use pre determined period base setting - buffer[1] = ( uint8_t ) ((timeout >> 8 ) & 0x00FF); - buffer[2] = ( uint8_t ) (timeout & 0x00FF); + clearIrqStatus(IRQ_RADIO_ALL); // clear all interrupt flags + buffer[0] = _PERIODBASE; // use pre determined period base setting + buffer[1] = (uint8_t)((timeout >> 8) & 0x00FF); + buffer[2] = (uint8_t)(timeout & 0x00FF); writeCommand(RADIO_SET_RX, buffer, 3); } - void SX128XLT::setPeriodBase(uint8_t value) { #ifdef SX128XDEBUG @@ -2036,7 +1976,6 @@ void SX128XLT::setPeriodBase(uint8_t value) _PERIODBASE = value; } - uint8_t SX128XLT::getPeriodBase() { #ifdef SX128XDEBUG @@ -2046,65 +1985,60 @@ uint8_t SX128XLT::getPeriodBase() return _PERIODBASE; } - void SX128XLT::setSyncWord1(uint32_t syncword) { #ifdef SX128XDEBUG1 Serial.println(F("setSyncWord1()")); #endif - //For FLRC packet type. The SyncWord is one byte shorter and - //the base address is shifted by one byte versus GFSK packets - writeRegister( REG_FLRCSYNCWORD1_BASEADDR, ( syncword >> 24 ) & 0x000000FF ); - writeRegister( REG_FLRCSYNCWORD1_BASEADDR + 1, ( syncword >> 16 ) & 0x000000FF ); - writeRegister( REG_FLRCSYNCWORD1_BASEADDR + 2, ( syncword >> 8 ) & 0x000000FF ); - writeRegister( REG_FLRCSYNCWORD1_BASEADDR + 3, syncword & 0x000000FF ); + // For FLRC packet type. The SyncWord is one byte shorter and + // the base address is shifted by one byte versus GFSK packets + writeRegister(REG_FLRCSYNCWORD1_BASEADDR, (syncword >> 24) & 0x000000FF); + writeRegister(REG_FLRCSYNCWORD1_BASEADDR + 1, (syncword >> 16) & 0x000000FF); + writeRegister(REG_FLRCSYNCWORD1_BASEADDR + 2, (syncword >> 8) & 0x000000FF); + writeRegister(REG_FLRCSYNCWORD1_BASEADDR + 3, syncword & 0x000000FF); } - void SX128XLT::setSyncWord2(uint32_t syncword) { #ifdef SX128XDEBUG1 Serial.println(F("setSyncWord2()")); #endif - //For FLRC packet type. The SyncWord is one byte shorter and - //the base address is shifted by one byte versus GFSK packets - writeRegister( REG_FLRCSYNCWORD2_BASEADDR, ( syncword >> 24 ) & 0x000000FF ); - writeRegister( REG_FLRCSYNCWORD2_BASEADDR + 1, ( syncword >> 16 ) & 0x000000FF ); - writeRegister( REG_FLRCSYNCWORD2_BASEADDR + 2, ( syncword >> 8 ) & 0x000000FF ); - writeRegister( REG_FLRCSYNCWORD2_BASEADDR + 3, syncword & 0x000000FF ); + // For FLRC packet type. The SyncWord is one byte shorter and + // the base address is shifted by one byte versus GFSK packets + writeRegister(REG_FLRCSYNCWORD2_BASEADDR, (syncword >> 24) & 0x000000FF); + writeRegister(REG_FLRCSYNCWORD2_BASEADDR + 1, (syncword >> 16) & 0x000000FF); + writeRegister(REG_FLRCSYNCWORD2_BASEADDR + 2, (syncword >> 8) & 0x000000FF); + writeRegister(REG_FLRCSYNCWORD2_BASEADDR + 3, syncword & 0x000000FF); } - void SX128XLT::setSyncWord3(uint32_t syncword) { #ifdef SX128XDEBUG1 Serial.println(F("setSyncWord3()")); #endif - //For FLRC packet type. The SyncWord is one byte shorter and - //the base address is shifted by one byte versus GFSK packets - writeRegister( REG_FLRCSYNCWORD3_BASEADDR, ( syncword >> 24 ) & 0x000000FF ); - writeRegister( REG_FLRCSYNCWORD3_BASEADDR + 1, ( syncword >> 16 ) & 0x000000FF ); - writeRegister( REG_FLRCSYNCWORD3_BASEADDR + 2, ( syncword >> 8 ) & 0x000000FF ); - writeRegister( REG_FLRCSYNCWORD3_BASEADDR + 3, syncword & 0x000000FF ); + // For FLRC packet type. The SyncWord is one byte shorter and + // the base address is shifted by one byte versus GFSK packets + writeRegister(REG_FLRCSYNCWORD3_BASEADDR, (syncword >> 24) & 0x000000FF); + writeRegister(REG_FLRCSYNCWORD3_BASEADDR + 1, (syncword >> 16) & 0x000000FF); + writeRegister(REG_FLRCSYNCWORD3_BASEADDR + 2, (syncword >> 8) & 0x000000FF); + writeRegister(REG_FLRCSYNCWORD3_BASEADDR + 3, syncword & 0x000000FF); } - void SX128XLT::setSyncWordErrors(uint8_t errors) { #ifdef SX128XDEBUG1 Serial.println(F("setSyncWordErrors()")); #endif - writeRegister( REG_LR_SYNCWORDTOLERANCE, errors ); + writeRegister(REG_LR_SYNCWORDTOLERANCE, errors); } - void SX128XLT::setSleep(uint8_t sleepconfig) { -//Note: Its been reported that the TXpower setting is not retained in sleep mode + // Note: Its been reported that the TXpower setting is not retained in sleep mode #ifdef SX128XDEBUG Serial.println(F("setSleep()")); @@ -2113,11 +2047,11 @@ void SX128XLT::setSleep(uint8_t sleepconfig) setMode(MODE_STDBY_RC); checkBusy(); -#ifdef USE_SPI_TRANSACTION //to use SPI_TRANSACTION enable define at beginning of CPP file +#ifdef USE_SPI_TRANSACTION // to use SPI_TRANSACTION enable define at beginning of CPP file SPI.beginTransaction(SPISettings(LTspeedMaximum, LTdataOrder, LTdataMode)); #endif - //need to save registers to device RAM first + // need to save registers to device RAM first digitalWrite(_NSS, LOW); SPI.transfer(RADIO_SET_SAVECONTEXT); digitalWrite(_NSS, HIGH); @@ -2132,10 +2066,9 @@ void SX128XLT::setSleep(uint8_t sleepconfig) #ifdef USE_SPI_TRANSACTION SPI.endTransaction(); #endif - delay(1); //allow time for shutdown + delay(1); // allow time for shutdown } - void SX128XLT::printHEXByte(uint8_t temp) { if (temp < 0x10) @@ -2145,7 +2078,6 @@ void SX128XLT::printHEXByte(uint8_t temp) Serial.print(temp, HEX); } - void SX128XLT::wake() { #ifdef SX128XDEBUG @@ -2158,7 +2090,6 @@ void SX128XLT::wake() delay(1); } - int32_t SX128XLT::getFrequencyErrorRegValue() { #ifdef SX128XDEBUG @@ -2170,10 +2101,10 @@ int32_t SX128XLT::getFrequencyErrorRegValue() setMode(MODE_STDBY_XOSC); - regmsb = readRegister( REG_LR_ESTIMATED_FREQUENCY_ERROR_MSB ); - regmsb = regmsb & 0x0F; //clear bit 20 which is always set - regmid = readRegister( REG_LR_ESTIMATED_FREQUENCY_ERROR_MSB + 1 ); - reglsb = readRegister( REG_LR_ESTIMATED_FREQUENCY_ERROR_MSB + 2 ); + regmsb = readRegister(REG_LR_ESTIMATED_FREQUENCY_ERROR_MSB); + regmsb = regmsb & 0x0F; // clear bit 20 which is always set + regmid = readRegister(REG_LR_ESTIMATED_FREQUENCY_ERROR_MSB + 1); + reglsb = readRegister(REG_LR_ESTIMATED_FREQUENCY_ERROR_MSB + 2); setMode(MODE_STDBY_RC); @@ -2187,7 +2118,7 @@ int32_t SX128XLT::getFrequencyErrorRegValue() Serial.println(reglsb, HEX); #endif - allreg = (uint32_t) ( regmsb << 16 ) | ( regmid << 8 ) | reglsb; + allreg = (uint32_t)(regmsb << 16) | (regmid << 8) | reglsb; if (allreg & 0x80000) { @@ -2201,7 +2132,6 @@ int32_t SX128XLT::getFrequencyErrorRegValue() return FrequencyError; } - int32_t SX128XLT::getFrequencyErrorHz() { #ifdef SX128XDEBUG @@ -2212,15 +2142,14 @@ int32_t SX128XLT::getFrequencyErrorHz() uint32_t bandwidth; float divider; - bandwidth = returnBandwidth(savedModParam2); //gets the last configured bandwidth - divider = (float) 1625000 / bandwidth; //data sheet says 1600000, but bandwidth is 1625000 + bandwidth = returnBandwidth(savedModParam2); // gets the last configured bandwidth + divider = (float)1625000 / bandwidth; // data sheet says 1600000, but bandwidth is 1625000 regvalue = getFrequencyErrorRegValue(); error = (FREQ_ERROR_CORRECTION * regvalue) / divider; return error; } - uint8_t SX128XLT::transmitAddressed(uint8_t *txbuffer, uint8_t size, char txpackettype, char txdestination, char txsource, uint32_t timeout, int8_t txpower, uint8_t wait) { #ifdef SX128XDEBUG @@ -2237,7 +2166,7 @@ uint8_t SX128XLT::transmitAddressed(uint8_t *txbuffer, uint8_t size, char txpack setMode(MODE_STDBY_RC); checkBusy(); -#ifdef USE_SPI_TRANSACTION //to use SPI_TRANSACTION enable define at beginning of CPP file +#ifdef USE_SPI_TRANSACTION // to use SPI_TRANSACTION enable define at beginning of CPP file SPI.beginTransaction(SPISettings(LTspeedMaximum, LTdataOrder, LTdataMode)); #endif @@ -2245,10 +2174,10 @@ uint8_t SX128XLT::transmitAddressed(uint8_t *txbuffer, uint8_t size, char txpack SPI.transfer(RADIO_WRITE_BUFFER); SPI.transfer(0); - SPI.transfer(txpackettype); //Write the packet type - SPI.transfer(txdestination); //Destination node - SPI.transfer(txsource); //Source node - _TXPacketL = 3 + size; //we have added 3 header bytes to size + SPI.transfer(txpackettype); // Write the packet type + SPI.transfer(txdestination); // Destination node + SPI.transfer(txsource); // Source node + _TXPacketL = 3 + size; // we have added 3 header bytes to size for (index = 0; index < size; index++) { @@ -2265,19 +2194,20 @@ uint8_t SX128XLT::transmitAddressed(uint8_t *txbuffer, uint8_t size, char txpack setPayloadLength(_TXPacketL); setTxParams(txpower, RAMP_TIME); - setDioIrqParams(IRQ_RADIO_ALL, (IRQ_TX_DONE + IRQ_RX_TX_TIMEOUT), 0, 0); //set for IRQ on TX done and timeout on DIO1 - setTx(timeout); //this starts the TX + setDioIrqParams(IRQ_RADIO_ALL, (IRQ_TX_DONE + IRQ_RX_TX_TIMEOUT), 0, 0); // set for IRQ on TX done and timeout on DIO1 + setTx(timeout); // this starts the TX if (!wait) { return _TXPacketL; } - while (!digitalRead(_TXDonePin)); //Wait for DIO1 to go high + while (!digitalRead(_TXDonePin)) + ; // Wait for DIO1 to go high - setMode(MODE_STDBY_RC); //ensure we leave function with TX off + setMode(MODE_STDBY_RC); // ensure we leave function with TX off - if (readIrqStatus() & IRQ_RX_TX_TIMEOUT ) //check for timeout + if (readIrqStatus() & IRQ_RX_TX_TIMEOUT) // check for timeout { return 0; } @@ -2287,7 +2217,6 @@ uint8_t SX128XLT::transmitAddressed(uint8_t *txbuffer, uint8_t size, char txpack } } - uint8_t SX128XLT::receiveAddressed(uint8_t *rxbuffer, uint8_t size, uint16_t timeout, uint8_t wait) { #ifdef SX128XDEBUG @@ -2298,47 +2227,46 @@ uint8_t SX128XLT::receiveAddressed(uint8_t *rxbuffer, uint8_t size, uint16_t tim uint16_t regdata; uint8_t buffer[2]; - setDioIrqParams(IRQ_RADIO_ALL, (IRQ_RX_DONE + IRQ_RX_TX_TIMEOUT + IRQ_HEADER_ERROR), 0, 0); //set for IRQ on RX done or timeout + setDioIrqParams(IRQ_RADIO_ALL, (IRQ_RX_DONE + IRQ_RX_TX_TIMEOUT + IRQ_HEADER_ERROR), 0, 0); // set for IRQ on RX done or timeout setRx(timeout); if (!wait) { - return 0; //not wait requested so no packet length to pass + return 0; // not wait requested so no packet length to pass } - while (!digitalRead(_RXDonePin)); //Wait for DIO1 to go high + while (!digitalRead(_RXDonePin)) + ; // Wait for DIO1 to go high - setMode(MODE_STDBY_RC); //ensure to stop further packet reception + setMode(MODE_STDBY_RC); // ensure to stop further packet reception regdata = readIrqStatus(); - if ( (regdata & IRQ_HEADER_ERROR) | (regdata & IRQ_CRC_ERROR) | (regdata & IRQ_RX_TX_TIMEOUT ) | (regdata & IRQ_SYNCWORD_ERROR )) //check if any of the preceding IRQs is set + if ((regdata & IRQ_HEADER_ERROR) | (regdata & IRQ_CRC_ERROR) | (regdata & IRQ_RX_TX_TIMEOUT) | (regdata & IRQ_SYNCWORD_ERROR)) // check if any of the preceding IRQs is set { - return 0; //packet is errored somewhere so return 0 + return 0; // packet is errored somewhere so return 0 } readCommand(RADIO_GET_RXBUFFERSTATUS, buffer, 2); _RXPacketL = buffer[0]; - if (_RXPacketL > size) //check passed buffer is big enough for packet + if (_RXPacketL > size) // check passed buffer is big enough for packet { - _RXPacketL = size; //truncate packet if not enough space + _RXPacketL = size; // truncate packet if not enough space } RXstart = buffer[1]; RXend = RXstart + _RXPacketL; checkBusy(); -#ifdef USE_SPI_TRANSACTION //to use SPI_TRANSACTION enable define at beginning of CPP file +#ifdef USE_SPI_TRANSACTION // to use SPI_TRANSACTION enable define at beginning of CPP file SPI.beginTransaction(SPISettings(LTspeedMaximum, LTdataOrder, LTdataMode)); #endif - digitalWrite(_NSS, LOW); //start the burst read + digitalWrite(_NSS, LOW); // start the burst read SPI.transfer(RADIO_READ_BUFFER); SPI.transfer(RXstart); SPI.transfer(0xFF); - - for (index = RXstart; index < RXend; index++) { regdata = SPI.transfer(0); @@ -2351,10 +2279,9 @@ uint8_t SX128XLT::receiveAddressed(uint8_t *rxbuffer, uint8_t size, uint16_t tim SPI.endTransaction(); #endif - return _RXPacketL; //so we can check for packet having enough buffer space + return _RXPacketL; // so we can check for packet having enough buffer space } - uint8_t SX128XLT::readRXPacketType() { #ifdef SX128XDEBUG @@ -2364,7 +2291,6 @@ uint8_t SX128XLT::readRXPacketType() return _RXPacketType; } - uint8_t SX128XLT::readPacket(uint8_t *rxbuffer, uint8_t size) { #ifdef SX128XDEBUG @@ -2377,20 +2303,20 @@ uint8_t SX128XLT::readPacket(uint8_t *rxbuffer, uint8_t size) readCommand(RADIO_GET_RXBUFFERSTATUS, buffer, 2); _RXPacketL = buffer[0]; - if (_RXPacketL > size) //check passed buffer is big enough for packet + if (_RXPacketL > size) // check passed buffer is big enough for packet { - _RXPacketL = size; //truncate packet if not enough space + _RXPacketL = size; // truncate packet if not enough space } RXstart = buffer[1]; RXend = RXstart + _RXPacketL; checkBusy(); -#ifdef USE_SPI_TRANSACTION //to use SPI_TRANSACTION enable define at beginning of CPP file +#ifdef USE_SPI_TRANSACTION // to use SPI_TRANSACTION enable define at beginning of CPP file SPI.beginTransaction(SPISettings(LTspeedMaximum, LTdataOrder, LTdataMode)); #endif - digitalWrite(_NSS, LOW); //start the burst read + digitalWrite(_NSS, LOW); // start the burst read SPI.transfer(RADIO_READ_BUFFER); SPI.transfer(RXstart); SPI.transfer(0xFF); @@ -2407,10 +2333,9 @@ uint8_t SX128XLT::readPacket(uint8_t *rxbuffer, uint8_t size) SPI.endTransaction(); #endif - return _RXPacketL; //so we can check for packet having enough buffer space + return _RXPacketL; // so we can check for packet having enough buffer space } - void SX128XLT::printHEXPacket(uint8_t *buffer, uint8_t size) { #ifdef SX128XDEBUG1 @@ -2426,7 +2351,6 @@ void SX128XLT::printHEXPacket(uint8_t *buffer, uint8_t size) } } - void SX128XLT::printHEXPacket(char *buffer, uint8_t size) { #ifdef SX128XDEBUG1 @@ -2442,7 +2366,6 @@ void SX128XLT::printHEXPacket(char *buffer, uint8_t size) } } - void SX128XLT::printArrayHEX(uint8_t *buffer, uint8_t size) { #ifdef SX128XDEBUG1 @@ -2458,7 +2381,6 @@ void SX128XLT::printArrayHEX(uint8_t *buffer, uint8_t size) } } - void SX128XLT::printArrayHEX(char *buffer, uint8_t size) { #ifdef SX128XDEBUG1 @@ -2474,10 +2396,9 @@ void SX128XLT::printArrayHEX(char *buffer, uint8_t size) } } - //*********************************************************************************** -//direct access SX buffer routines -//these routines read\write variables\data direct to the LoRa devices internal buffer +// direct access SX buffer routines +// these routines read\write variables\data direct to the LoRa devices internal buffer //*********************************************************************************** void SX128XLT::startWriteSXBuffer(uint8_t ptr) @@ -2486,23 +2407,22 @@ void SX128XLT::startWriteSXBuffer(uint8_t ptr) Serial.println(F("startWriteSXBuffer()")); #endif - _TXPacketL = 0; //this variable used to keep track of bytes written + _TXPacketL = 0; // this variable used to keep track of bytes written setMode(MODE_STDBY_RC); - setBufferBaseAddress(ptr, 0); //TX,RX + setBufferBaseAddress(ptr, 0); // TX,RX checkBusy(); -#ifdef USE_SPI_TRANSACTION //to use SPI_TRANSACTION enable define at beginning of CPP file +#ifdef USE_SPI_TRANSACTION // to use SPI_TRANSACTION enable define at beginning of CPP file SPI.beginTransaction(SPISettings(LTspeedMaximum, LTdataOrder, LTdataMode)); #endif digitalWrite(_NSS, LOW); SPI.transfer(RADIO_WRITE_BUFFER); - SPI.transfer(ptr); //address in SX buffer to write to - //SPI interface ready for byte to write to buffer + SPI.transfer(ptr); // address in SX buffer to write to + // SPI interface ready for byte to write to buffer } - -uint8_t SX128XLT::endWriteSXBuffer() +uint8_t SX128XLT::endWriteSXBuffer() { #ifdef SX128XDEBUG Serial.println(F("endWriteSXBuffer()")); @@ -2517,7 +2437,6 @@ uint8_t SX128XLT::endWriteSXBuffer() return _TXPacketL; } - void SX128XLT::startReadSXBuffer(uint8_t ptr) { #ifdef SX128XDEBUG @@ -2528,20 +2447,19 @@ void SX128XLT::startReadSXBuffer(uint8_t ptr) checkBusy(); -#ifdef USE_SPI_TRANSACTION //to use SPI_TRANSACTION enable define at beginning of CPP file +#ifdef USE_SPI_TRANSACTION // to use SPI_TRANSACTION enable define at beginning of CPP file SPI.beginTransaction(SPISettings(LTspeedMaximum, LTdataOrder, LTdataMode)); #endif - digitalWrite(_NSS, LOW); //start the burst read + digitalWrite(_NSS, LOW); // start the burst read SPI.transfer(RADIO_READ_BUFFER); SPI.transfer(ptr); SPI.transfer(0xFF); - //next line would be data = SPI.transfer(0); - //SPI interface ready for byte to read from + // next line would be data = SPI.transfer(0); + // SPI interface ready for byte to read from } - uint8_t SX128XLT::endReadSXBuffer() { #ifdef SX128XDEBUG @@ -2557,7 +2475,6 @@ uint8_t SX128XLT::endReadSXBuffer() return _RXPacketL; } - void SX128XLT::writeUint8(uint8_t x) { #ifdef SX128XDEBUG @@ -2565,7 +2482,7 @@ void SX128XLT::writeUint8(uint8_t x) #endif SPI.transfer(x); - _TXPacketL++; //increment count of bytes written + _TXPacketL++; // increment count of bytes written } uint8_t SX128XLT::readUint8() @@ -2576,11 +2493,10 @@ uint8_t SX128XLT::readUint8() uint8_t x; x = SPI.transfer(0); - _RXPacketL++; //increment count of bytes read + _RXPacketL++; // increment count of bytes read return (x); } - void SX128XLT::writeInt8(int8_t x) { #ifdef SX128XDEBUG @@ -2589,10 +2505,9 @@ void SX128XLT::writeInt8(int8_t x) SPI.transfer(x); - _TXPacketL++; //increment count of bytes written + _TXPacketL++; // increment count of bytes written } - int8_t SX128XLT::readInt8() { #ifdef SX128XDEBUG @@ -2602,11 +2517,10 @@ int8_t SX128XLT::readInt8() x = SPI.transfer(0); - _RXPacketL++; //increment count of bytes read + _RXPacketL++; // increment count of bytes read return (x); } - void SX128XLT::writeInt16(int16_t x) { #ifdef SX128XDEBUG @@ -2616,10 +2530,9 @@ void SX128XLT::writeInt16(int16_t x) SPI.transfer(lowByte(x)); SPI.transfer(highByte(x)); - _TXPacketL = _TXPacketL + 2; //increment count of bytes written + _TXPacketL = _TXPacketL + 2; // increment count of bytes written } - int16_t SX128XLT::readInt16() { #ifdef SX128XDEBUG @@ -2630,11 +2543,10 @@ int16_t SX128XLT::readInt16() lowbyte = SPI.transfer(0); highbyte = SPI.transfer(0); - _RXPacketL = _RXPacketL + 2; //increment count of bytes read + _RXPacketL = _RXPacketL + 2; // increment count of bytes read return ((highbyte << 8) + lowbyte); } - void SX128XLT::writeUint16(uint16_t x) { #ifdef SX128XDEBUG @@ -2644,10 +2556,9 @@ void SX128XLT::writeUint16(uint16_t x) SPI.transfer(lowByte(x)); SPI.transfer(highByte(x)); - _TXPacketL = _TXPacketL + 2; //increment count of bytes written + _TXPacketL = _TXPacketL + 2; // increment count of bytes written } - uint16_t SX128XLT::readUint16() { #ifdef SX128XDEBUG @@ -2658,11 +2569,10 @@ uint16_t SX128XLT::readUint16() lowbyte = SPI.transfer(0); highbyte = SPI.transfer(0); - _RXPacketL = _RXPacketL + 2; //increment count of bytes read + _RXPacketL = _RXPacketL + 2; // increment count of bytes read return ((highbyte << 8) + lowbyte); } - void SX128XLT::writeInt32(int32_t x) { #ifdef SX128XDEBUG @@ -2684,10 +2594,9 @@ void SX128XLT::writeInt32(int32_t x) SPI.transfer(j); } - _TXPacketL = _TXPacketL + 4; //increment count of bytes written + _TXPacketL = _TXPacketL + 4; // increment count of bytes written } - int32_t SX128XLT::readInt32() { #ifdef SX128XDEBUG @@ -2707,11 +2616,10 @@ int32_t SX128XLT::readInt32() j = SPI.transfer(0); readdata.b[i] = j; } - _RXPacketL = _RXPacketL + 4; //increment count of bytes read + _RXPacketL = _RXPacketL + 4; // increment count of bytes read return readdata.f; } - void SX128XLT::writeUint32(uint32_t x) { #ifdef SX128XDEBUG @@ -2733,10 +2641,9 @@ void SX128XLT::writeUint32(uint32_t x) SPI.transfer(j); } - _TXPacketL = _TXPacketL + 4; //increment count of bytes written + _TXPacketL = _TXPacketL + 4; // increment count of bytes written } - uint32_t SX128XLT::readUint32() { #ifdef SX128XDEBUG @@ -2756,11 +2663,10 @@ uint32_t SX128XLT::readUint32() j = SPI.transfer(0); readdata.b[i] = j; } - _RXPacketL = _RXPacketL + 4; //increment count of bytes read + _RXPacketL = _RXPacketL + 4; // increment count of bytes read return readdata.f; } - void SX128XLT::writeFloat(float x) { #ifdef SX128XDEBUG @@ -2782,10 +2688,9 @@ void SX128XLT::writeFloat(float x) SPI.transfer(j); } - _TXPacketL = _TXPacketL + 4; //increment count of bytes written + _TXPacketL = _TXPacketL + 4; // increment count of bytes written } - float SX128XLT::readFloat() { #ifdef SX128XDEBUG @@ -2805,33 +2710,33 @@ float SX128XLT::readFloat() j = SPI.transfer(0); readdata.b[i] = j; } - _RXPacketL = _RXPacketL + 4; //increment count of bytes read + _RXPacketL = _RXPacketL + 4; // increment count of bytes read return readdata.f; } - uint8_t SX128XLT::transmitSXBuffer(uint8_t startaddr, uint8_t length, uint16_t timeout, int8_t txpower, uint8_t wait) { #ifdef SX128XDEBUG Serial.println(F("transmitSXBuffer()")); #endif - setBufferBaseAddress(startaddr, 0); //TX, RX + setBufferBaseAddress(startaddr, 0); // TX, RX setPayloadLength(length); setTxParams(txpower, RAMP_TIME); - setDioIrqParams(IRQ_RADIO_ALL, (IRQ_TX_DONE + IRQ_RX_TX_TIMEOUT), 0, 0); //set for IRQ on TX done and timeout on DIO1 - setTx(timeout); //this starts the TX + setDioIrqParams(IRQ_RADIO_ALL, (IRQ_TX_DONE + IRQ_RX_TX_TIMEOUT), 0, 0); // set for IRQ on TX done and timeout on DIO1 + setTx(timeout); // this starts the TX if (!wait) { return _TXPacketL; } - while (!digitalRead(_TXDonePin)); //Wait for DIO1 to go high + while (!digitalRead(_TXDonePin)) + ; // Wait for DIO1 to go high - setMode(MODE_STDBY_RC); //ensure we leave function with TX off + setMode(MODE_STDBY_RC); // ensure we leave function with TX off - if (readIrqStatus() & IRQ_RX_TX_TIMEOUT ) //check for timeout + if (readIrqStatus() & IRQ_RX_TX_TIMEOUT) // check for timeout { return 0; } @@ -2841,29 +2746,29 @@ uint8_t SX128XLT::transmitSXBuffer(uint8_t startaddr, uint8_t length, uint16_t t } } - uint8_t SX128XLT::transmitSXBufferIRQ(uint8_t startaddr, uint8_t length, uint16_t timeout, int8_t txpower, uint8_t wait) { #ifdef SX128XDEBUG Serial.println(F("transmitSXBuffer()")); #endif - setBufferBaseAddress(startaddr, 0); //TX, RX + setBufferBaseAddress(startaddr, 0); // TX, RX setPayloadLength(length); setTxParams(txpower, RAMP_TIME); - setTx(timeout); //this starts the TX + setTx(timeout); // this starts the TX if (!wait) { return _TXPacketL; } - //0x4001 = IRQ_TX_DONE + IRQ_RX_TX_TIMEOUT - while (!(readIrqStatus() & 0x4001 )); //wait for IRQs going active + // 0x4001 = IRQ_TX_DONE + IRQ_RX_TX_TIMEOUT + while (!(readIrqStatus() & 0x4001)) + ; // wait for IRQs going active - setMode(MODE_STDBY_RC); //ensure we leave function with TX off + setMode(MODE_STDBY_RC); // ensure we leave function with TX off - if (readIrqStatus() & IRQ_RX_TX_TIMEOUT ) //check for timeout + if (readIrqStatus() & IRQ_RX_TX_TIMEOUT) // check for timeout { return 0; } @@ -2873,7 +2778,6 @@ uint8_t SX128XLT::transmitSXBufferIRQ(uint8_t startaddr, uint8_t length, uint16_ } } - void SX128XLT::writeBuffer(uint8_t *txbuffer, uint8_t size) { #ifdef SX128XDEBUG1 @@ -2882,8 +2786,8 @@ void SX128XLT::writeBuffer(uint8_t *txbuffer, uint8_t size) uint8_t index, regdata; - _TXPacketL = _TXPacketL + size; //these are the number of bytes that will be added - size--; //loose one byte from size, the last byte written MUST be a 0 + _TXPacketL = _TXPacketL + size; // these are the number of bytes that will be added + size--; // loose one byte from size, the last byte written MUST be a 0 for (index = 0; index < size; index++) { @@ -2891,12 +2795,10 @@ void SX128XLT::writeBuffer(uint8_t *txbuffer, uint8_t size) SPI.transfer(regdata); } - SPI.transfer(0); //this ensures last byte of buffer written really is a null (0) - + SPI.transfer(0); // this ensures last byte of buffer written really is a null (0) } - -uint8_t SX128XLT::receiveSXBuffer(uint8_t startaddr, uint16_t timeout, uint8_t wait ) +uint8_t SX128XLT::receiveSXBuffer(uint8_t startaddr, uint16_t timeout, uint8_t wait) { #ifdef SX128XDEBUG1 Serial.println(F("receiveSXBuffer()")); @@ -2906,8 +2808,8 @@ uint8_t SX128XLT::receiveSXBuffer(uint8_t startaddr, uint16_t timeout, uint8_t w uint8_t buffer[2]; setMode(MODE_STDBY_RC); - setBufferBaseAddress(0, startaddr); //order is TX RX - setDioIrqParams(IRQ_RADIO_ALL, (IRQ_RX_DONE + IRQ_RX_TX_TIMEOUT + IRQ_HEADER_ERROR), 0, 0); //set for IRQ on RX done or timeout + setBufferBaseAddress(0, startaddr); // order is TX RX + setDioIrqParams(IRQ_RADIO_ALL, (IRQ_RX_DONE + IRQ_RX_TX_TIMEOUT + IRQ_HEADER_ERROR), 0, 0); // set for IRQ on RX done or timeout setRx(timeout); if (!wait) @@ -2915,15 +2817,16 @@ uint8_t SX128XLT::receiveSXBuffer(uint8_t startaddr, uint16_t timeout, uint8_t w return 0; } - while (!digitalRead(_RXDonePin)); //Wait for DIO1 to go high + while (!digitalRead(_RXDonePin)) + ; // Wait for DIO1 to go high - setMode(MODE_STDBY_RC); //ensure to stop further packet reception + setMode(MODE_STDBY_RC); // ensure to stop further packet reception regdata = readIrqStatus(); - if ( (regdata & IRQ_HEADER_ERROR) | (regdata & IRQ_CRC_ERROR) | (regdata & IRQ_RX_TX_TIMEOUT ) | (regdata & IRQ_SYNCWORD_ERROR )) + if ((regdata & IRQ_HEADER_ERROR) | (regdata & IRQ_CRC_ERROR) | (regdata & IRQ_RX_TX_TIMEOUT) | (regdata & IRQ_SYNCWORD_ERROR)) { - return 0; //no RX done and header valid only, could be CRC error + return 0; // no RX done and header valid only, could be CRC error } readCommand(RADIO_GET_RXBUFFERSTATUS, buffer, 2); @@ -2932,8 +2835,7 @@ uint8_t SX128XLT::receiveSXBuffer(uint8_t startaddr, uint16_t timeout, uint8_t w return _RXPacketL; } - -uint8_t SX128XLT::receiveSXBufferIRQ(uint8_t startaddr, uint16_t timeout, uint8_t wait ) +uint8_t SX128XLT::receiveSXBufferIRQ(uint8_t startaddr, uint16_t timeout, uint8_t wait) { #ifdef SX128XDEBUG1 Serial.println(F("receiveSXBufferIRQ()")); @@ -2942,7 +2844,7 @@ uint8_t SX128XLT::receiveSXBufferIRQ(uint8_t startaddr, uint16_t timeout, uint8_ uint8_t buffer[2]; setMode(MODE_STDBY_RC); - setBufferBaseAddress(0, startaddr); //order is TX RX + setBufferBaseAddress(0, startaddr); // order is TX RX setRx(timeout); if (!wait) @@ -2950,15 +2852,15 @@ uint8_t SX128XLT::receiveSXBufferIRQ(uint8_t startaddr, uint16_t timeout, uint8_ return 0; } - //0x4022 = IRQ_RX_DONE + IRQ_RX_TX_TIMEOUT + IRQ_HEADER_ERROR - while (!(readIrqStatus() & 0x4022 )); //wait for IRQs going active + // 0x4022 = IRQ_RX_DONE + IRQ_RX_TX_TIMEOUT + IRQ_HEADER_ERROR + while (!(readIrqStatus() & 0x4022)) + ; // wait for IRQs going active + setMode(MODE_STDBY_RC); // ensure to stop further packet reception - setMode(MODE_STDBY_RC); //ensure to stop further packet reception - - if ( readIrqStatus() & (IRQ_HEADER_ERROR + IRQ_CRC_ERROR + IRQ_RX_TX_TIMEOUT + IRQ_SYNCWORD_ERROR)) + if (readIrqStatus() & (IRQ_HEADER_ERROR + IRQ_CRC_ERROR + IRQ_RX_TX_TIMEOUT + IRQ_SYNCWORD_ERROR)) { - return 0; //no RX done and header valid only, could be CRC error + return 0; // no RX done and header valid only, could be CRC error } readCommand(RADIO_GET_RXBUFFERSTATUS, buffer, 2); @@ -2967,7 +2869,6 @@ uint8_t SX128XLT::receiveSXBufferIRQ(uint8_t startaddr, uint16_t timeout, uint8_ return _RXPacketL; } - uint8_t SX128XLT::readBuffer(uint8_t *rxbuffer) { #ifdef SX128XDEBUG1 @@ -2976,22 +2877,20 @@ uint8_t SX128XLT::readBuffer(uint8_t *rxbuffer) uint8_t index = 0, regdata; - do //need to find the size of the buffer first + do // need to find the size of the buffer first { regdata = SPI.transfer(0); - rxbuffer[index] = regdata; //fill the buffer. + rxbuffer[index] = regdata; // fill the buffer. index++; - } while (regdata != 0); //keep reading until we have reached the null (0) at the buffer end - //or exceeded size of buffer allowed - _RXPacketL = _RXPacketL + index; //increment count of bytes read - return index; //return the actual size of the buffer, till the null (0) detected - + } while (regdata != 0); // keep reading until we have reached the null (0) at the buffer end + // or exceeded size of buffer allowed + _RXPacketL = _RXPacketL + index; // increment count of bytes read + return index; // return the actual size of the buffer, till the null (0) detected } - uint16_t SX128XLT::CRCCCITTSX(uint8_t startadd, uint8_t endadd, uint16_t startvalue) { - //genrates a CRC of an area of the internal SX buffer + // genrates a CRC of an area of the internal SX buffer #ifdef SX126XDEBUG1 Serial.println(F("CRCCCITTSX()")); @@ -3000,12 +2899,12 @@ uint16_t SX128XLT::CRCCCITTSX(uint8_t startadd, uint8_t endadd, uint16_t startva uint16_t index, libraryCRC; uint8_t j; - libraryCRC = startvalue; //start value for CRC16 - startReadSXBuffer(startadd); //begin the buffer read + libraryCRC = startvalue; // start value for CRC16 + startReadSXBuffer(startadd); // begin the buffer read for (index = startadd; index <= endadd; index++) { - libraryCRC ^= (((uint16_t) readUint8() ) << 8); + libraryCRC ^= (((uint16_t)readUint8()) << 8); for (j = 0; j < 8; j++) { if (libraryCRC & 0x8000) @@ -3015,11 +2914,10 @@ uint16_t SX128XLT::CRCCCITTSX(uint8_t startadd, uint8_t endadd, uint16_t startva } } - endReadSXBuffer(); //end the buffer read + endReadSXBuffer(); // end the buffer read return libraryCRC; } - uint8_t SX128XLT::getByteSXBuffer(uint8_t addr) { #ifdef SX128XDEBUG1 @@ -3028,14 +2926,14 @@ uint8_t SX128XLT::getByteSXBuffer(uint8_t addr) uint8_t regdata; - setMode(MODE_STDBY_RC); //this is needed to ensure we can read from buffer OK. + setMode(MODE_STDBY_RC); // this is needed to ensure we can read from buffer OK. checkBusy(); -#ifdef USE_SPI_TRANSACTION //to use SPI_TRANSACTION enable define at beginning of CPP file +#ifdef USE_SPI_TRANSACTION // to use SPI_TRANSACTION enable define at beginning of CPP file SPI.beginTransaction(SPISettings(LTspeedMaximum, LTdataOrder, LTdataMode)); #endif - digitalWrite(_NSS, LOW); //start the burst read + digitalWrite(_NSS, LOW); // start the burst read SPI.transfer(RADIO_READ_BUFFER); SPI.transfer(addr); SPI.transfer(0xFF); @@ -3049,7 +2947,6 @@ uint8_t SX128XLT::getByteSXBuffer(uint8_t addr) return regdata; } - void SX128XLT::printSXBufferHEX(uint8_t start, uint8_t end) { #ifdef SX128XDEBUG @@ -3058,16 +2955,15 @@ void SX128XLT::printSXBufferHEX(uint8_t start, uint8_t end) uint8_t index, regdata; - setMode(MODE_STDBY_RC); checkBusy(); -#ifdef USE_SPI_TRANSACTION //to use SPI_TRANSACTION enable define at beginning of CPP file +#ifdef USE_SPI_TRANSACTION // to use SPI_TRANSACTION enable define at beginning of CPP file SPI.beginTransaction(SPISettings(LTspeedMaximum, LTdataOrder, LTdataMode)); #endif - digitalWrite(_NSS, LOW); //start the burst read + digitalWrite(_NSS, LOW); // start the burst read SPI.transfer(RADIO_READ_BUFFER); SPI.transfer(start); SPI.transfer(0xFF); @@ -3077,17 +2973,14 @@ void SX128XLT::printSXBufferHEX(uint8_t start, uint8_t end) regdata = SPI.transfer(0); printHEXByte(regdata); Serial.print(F(" ")); - } digitalWrite(_NSS, HIGH); #ifdef USE_SPI_TRANSACTION SPI.endTransaction(); #endif - } - void SX128XLT::writeBufferChar(char *txbuffer, uint8_t size) { #ifdef SX128XDEBUG1 @@ -3096,8 +2989,8 @@ void SX128XLT::writeBufferChar(char *txbuffer, uint8_t size) uint8_t index, regdata; - _TXPacketL = _TXPacketL + size; //these are the number of bytes that will be added - size--; //loose one byte from size, the last byte written MUST be a 0 + _TXPacketL = _TXPacketL + size; // these are the number of bytes that will be added + size--; // loose one byte from size, the last byte written MUST be a 0 for (index = 0; index < size; index++) { @@ -3105,10 +2998,9 @@ void SX128XLT::writeBufferChar(char *txbuffer, uint8_t size) SPI.transfer(regdata); } - SPI.transfer(0); //this ensures last byte of buffer writen really is a null (0) + SPI.transfer(0); // this ensures last byte of buffer writen really is a null (0) } - uint8_t SX128XLT::readBufferChar(char *rxbuffer) { #ifdef SX128XDEBUG1 @@ -3117,23 +3009,21 @@ uint8_t SX128XLT::readBufferChar(char *rxbuffer) uint8_t index = 0, regdata; - do //need to find the size of the buffer first + do // need to find the size of the buffer first { regdata = SPI.transfer(0); - rxbuffer[index] = regdata; //fill the buffer. + rxbuffer[index] = regdata; // fill the buffer. index++; - } while (regdata != 0); //keep reading until we have reached the null (0) at the buffer end - //or exceeded size of buffer allowed + } while (regdata != 0); // keep reading until we have reached the null (0) at the buffer end + // or exceeded size of buffer allowed - _RXPacketL = _RXPacketL + index; //increment count of bytes read - - return index; //return the actual size of the buffer, till the null (0) detected + _RXPacketL = _RXPacketL + index; // increment count of bytes read + return index; // return the actual size of the buffer, till the null (0) detected } - -//There are two ways of setting the payload length, enabled by default is the appropriate function setPacketParams() -//To set payload length with a discovered direct register length enable the #define USEPAYLOADLENGTHREGISTER +// There are two ways of setting the payload length, enabled by default is the appropriate function setPacketParams() +// To set payload length with a discovered direct register length enable the #define USEPAYLOADLENGTHREGISTER void SX128XLT::setPayloadLength(uint8_t length) { @@ -3143,10 +3033,10 @@ void SX128XLT::setPayloadLength(uint8_t length) if (savedPacketType == PACKET_TYPE_LORA) { #ifdef USEPAYLOADLENGTHREGISTER - //Serial.println(F(" USEPAYLOADLENGTHREGISTER ")); + // Serial.println(F(" USEPAYLOADLENGTHREGISTER ")); writeRegister(REG_LR_PAYLOADLENGTH, length); #else - //Serial.println(F(" USE setPacketParams() ")); + // Serial.println(F(" USE setPacketParams() ")); setPacketParams(savedPacketParam1, savedPacketParam2, length, savedPacketParam4, savedPacketParam5); #endif } @@ -3154,20 +3044,18 @@ void SX128XLT::setPayloadLength(uint8_t length) if (savedPacketType == PACKET_TYPE_FLRC) { #ifdef USEPAYLOADLENGTHREGISTER - //Serial.println(F(" USEPAYLOADLENGTHREGISTER FLRC ")); + // Serial.println(F(" USEPAYLOADLENGTHREGISTER FLRC ")); writeRegister(REG_LR_FLRCPAYLOADLENGTH, length); #else - //Serial.println(F(" USE setPacketParams() FLRC ")); + // Serial.println(F(" USE setPacketParams() FLRC ")); setPacketParams(savedPacketParam1, savedPacketParam2, savedPacketParam3, savedPacketParam4, length, savedPacketParam6, savedPacketParam7); #endif } - } - void SX128XLT::setFLRCPayloadLengthReg(uint8_t length) { - //uses a discovered register that is the FLRC payload length for TX and RX filtering + // uses a discovered register that is the FLRC payload length for TX and RX filtering #ifdef SX128XDEBUG Serial.println(F("setFLRCPayloadLengthReg()")); #endif @@ -3175,10 +3063,9 @@ void SX128XLT::setFLRCPayloadLengthReg(uint8_t length) writeRegister(REG_LR_FLRCPAYLOADLENGTH, length); } - void SX128XLT::setLoRaPayloadLengthReg(uint8_t length) { - //uses a discovered register that is the LoRa payload length for TX + // uses a discovered register that is the LoRa payload length for TX #ifdef SX128XDEBUG Serial.println(F("setLoRaPayloadLengthReg()")); #endif @@ -3186,7 +3073,6 @@ void SX128XLT::setLoRaPayloadLengthReg(uint8_t length) writeRegister(REG_LR_PAYLOADLENGTH, length); } - uint8_t SX128XLT::getPacketType() { #ifdef SX128XDEBUGRELIABLE @@ -3197,62 +3083,58 @@ uint8_t SX128XLT::getPacketType() } //******************************************************************************* -//Ranging routines +// Ranging routines //******************************************************************************* void SX128XLT::setRangingSlaveAddress(uint32_t address) { - //sets address of ranging slave + // sets address of ranging slave #ifdef SX128XDEBUG Serial.println(F("SetRangingSlaveAddress()")); #endif uint8_t buffer[4]; - buffer[0] = (address >> 24u ) & 0xFFu; + buffer[0] = (address >> 24u) & 0xFFu; buffer[1] = (address >> 16u) & 0xFFu; - buffer[2] = (address >> 8u) & 0xFFu; + buffer[2] = (address >> 8u) & 0xFFu; buffer[3] = (address & 0xFFu); - writeRegisters(0x916, buffer, 4 ); + writeRegisters(0x916, buffer, 4); } - void SX128XLT::setRangingSlaveAddress(uint32_t address, uint8_t bits) { - //sets address of ranging slave + // sets address of ranging slave #ifdef SX128XDEBUG Serial.println(F("SetRangingSlaveAddress()")); #endif uint8_t buffer[4]; - buffer[0] = (address >> 24u ) & 0xFFu; + buffer[0] = (address >> 24u) & 0xFFu; buffer[1] = (address >> 16u) & 0xFFu; - buffer[2] = (address >> 8u) & 0xFFu; + buffer[2] = (address >> 8u) & 0xFFu; buffer[3] = (address & 0xFFu); - writeRegisters(0x916, buffer, 4 ); - writeRegister(REG_LR_RANGINGIDCHECKLENGTH, bits); //set slave to check all 32 bits of address + writeRegisters(0x916, buffer, 4); + writeRegister(REG_LR_RANGINGIDCHECKLENGTH, bits); // set slave to check all 32 bits of address } - - void SX128XLT::setRangingMasterAddress(uint32_t address) { - //sets address of ranging master + // sets address of ranging master #ifdef SX128XDEBUG Serial.println(F("SetRangingMasterAddress()")); #endif uint8_t buffer[4]; - buffer[0] = (address >> 24u ) & 0xFFu; + buffer[0] = (address >> 24u) & 0xFFu; buffer[1] = (address >> 16u) & 0xFFu; - buffer[2] = (address >> 8u) & 0xFFu; + buffer[2] = (address >> 8u) & 0xFFu; buffer[3] = (address & 0xFFu); - writeRegisters(0x912, buffer, 4 ); + writeRegisters(0x912, buffer, 4); } - void SX128XLT::setRangingCalibration(uint16_t cal) { #ifdef SX128XDEBUG @@ -3260,11 +3142,10 @@ void SX128XLT::setRangingCalibration(uint16_t cal) #endif savedCalibration = cal; - writeRegister( REG_LR_RANGINGRERXTXDELAYCAL, ( uint8_t )( ( cal >> 8 ) & 0xFF ) ); - writeRegister( REG_LR_RANGINGRERXTXDELAYCAL + 1, ( uint8_t )( ( cal ) & 0xFF ) ); + writeRegister(REG_LR_RANGINGRERXTXDELAYCAL, (uint8_t)((cal >> 8) & 0xFF)); + writeRegister(REG_LR_RANGINGRERXTXDELAYCAL + 1, (uint8_t)((cal) & 0xFF)); } - void SX128XLT::setRangingRole(uint8_t role) { #ifdef SX128XDEBUG @@ -3274,28 +3155,26 @@ void SX128XLT::setRangingRole(uint8_t role) uint8_t buffer[1]; buffer[0] = role; - writeCommand(RADIO_SET_RANGING_ROLE, buffer, 1 ); + writeCommand(RADIO_SET_RANGING_ROLE, buffer, 1); } - uint32_t SX128XLT::getRangingResultRegValue(uint8_t resultType) { uint32_t valLsb = 0; setMode(MODE_STDBY_XOSC); - writeRegister( 0x97F, readRegister( 0x97F ) | ( 1 << 1 ) ); // enable LORA modem clock - writeRegister( REG_LR_RANGINGRESULTCONFIG, ( readRegister( REG_LR_RANGINGRESULTCONFIG ) & MASK_RANGINGMUXSEL ) | ( ( ( ( uint8_t )resultType ) & 0x03 ) << 4 ) ); - valLsb = ( ( (uint32_t) readRegister( REG_LR_RANGINGRESULTBASEADDR ) << 16 ) | ( (uint32_t) readRegister( REG_LR_RANGINGRESULTBASEADDR + 1 ) << 8 ) | ( readRegister( REG_LR_RANGINGRESULTBASEADDR + 2 ) ) ); + writeRegister(0x97F, readRegister(0x97F) | (1 << 1)); // enable LORA modem clock + writeRegister(REG_LR_RANGINGRESULTCONFIG, (readRegister(REG_LR_RANGINGRESULTCONFIG) & MASK_RANGINGMUXSEL) | ((((uint8_t)resultType) & 0x03) << 4)); + valLsb = (((uint32_t)readRegister(REG_LR_RANGINGRESULTBASEADDR) << 16) | ((uint32_t)readRegister(REG_LR_RANGINGRESULTBASEADDR + 1) << 8) | (readRegister(REG_LR_RANGINGRESULTBASEADDR + 2))); setMode(MODE_STDBY_RC); return valLsb; } - double SX128XLT::getRangingDistance(uint8_t resultType, int32_t regval, float adjust) { float val = 0.0; - if (regval >= 0x800000) //raw reg value at low distance can goto 0x800000 which is negative, set distance to zero if this happens + if (regval >= 0x800000) // raw reg value at low distance can goto 0x800000 which is negative, set distance to zero if this happens { regval = 0; } @@ -3304,32 +3183,31 @@ double SX128XLT::getRangingDistance(uint8_t resultType, int32_t regval, float ad switch (resultType) { - case RANGING_RESULT_RAW: - // Convert the ranging LSB to distance in meter. The theoretical conversion from register value to distance [m] is given by: - // distance [m] = ( complement2( register ) * 150 ) / ( 2^12 * bandwidth[MHz] ) ). The API provide BW in [Hz] so the implemented - // formula is complement2( register ) / bandwidth[Hz] * A, where A = 150 / (2^12 / 1e6) = 36621.09 - val = ( double ) regval / ( double ) returnBandwidth(savedModParam2) * 36621.09375; - break; + case RANGING_RESULT_RAW: + // Convert the ranging LSB to distance in meter. The theoretical conversion from register value to distance [m] is given by: + // distance [m] = ( complement2( register ) * 150 ) / ( 2^12 * bandwidth[MHz] ) ). The API provide BW in [Hz] so the implemented + // formula is complement2( register ) / bandwidth[Hz] * A, where A = 150 / (2^12 / 1e6) = 36621.09 + val = (double)regval / (double)returnBandwidth(savedModParam2) * 36621.09375; + break; - case RANGING_RESULT_AVERAGED: - case RANGING_RESULT_DEBIASED: - case RANGING_RESULT_FILTERED: - Serial.print(F("??")); - val = ( double )regval * 20.0 / 100.0; - break; - default: - val = 0.0; - break; + case RANGING_RESULT_AVERAGED: + case RANGING_RESULT_DEBIASED: + case RANGING_RESULT_FILTERED: + Serial.print(F("??")); + val = (double)regval * 20.0 / 100.0; + break; + default: + val = 0.0; + break; } val = val * adjust; return val; } - -bool SX128XLT::setupRanging(uint32_t frequency, int32_t offset, uint8_t modParam1, uint8_t modParam2, uint8_t modParam3, uint32_t address, uint8_t role) +bool SX128XLT::setupRanging(uint32_t frequency, int32_t offset, uint8_t modParam1, uint8_t modParam2, uint8_t modParam3, uint32_t address, uint8_t role) { - //sequence is frequency, offset, spreading factor, bandwidth, coding rate, calibration, role. + // sequence is frequency, offset, spreading factor, bandwidth, coding rate, calibration, role. #ifdef SX128XDEBUG Serial.println(F("setupRanging()")); #endif @@ -3347,7 +3225,6 @@ bool SX128XLT::setupRanging(uint32_t frequency, int32_t offset, uint8_t modParam return true; } - bool SX128XLT::transmitRanging(uint32_t address, uint16_t timeout, int8_t txpower, uint8_t wait) { #ifdef SX128XDEBUG @@ -3363,18 +3240,19 @@ bool SX128XLT::transmitRanging(uint32_t address, uint16_t timeout, int8_t txpowe setRangingMasterAddress(address); setTxParams(txpower, RADIO_RAMP_02_US); setDioIrqParams(IRQ_RADIO_ALL, (IRQ_TX_DONE + IRQ_RANGING_MASTER_RESULT_VALID + IRQ_RANGING_MASTER_RESULT_TIMEOUT), 0, 0); - setTx(timeout); //this sends the ranging packet + setTx(timeout); // this sends the ranging packet if (!wait) { return true; } - while (!digitalRead(_TXDonePin)); //Wait for DIO1 to go high + while (!digitalRead(_TXDonePin)) + ; // Wait for DIO1 to go high - setMode(MODE_STDBY_RC); //ensure we leave function with TX off + setMode(MODE_STDBY_RC); // ensure we leave function with TX off - if (readIrqStatus() & IRQ_RANGING_MASTER_RESULT_VALID ) //check for timeout + if (readIrqStatus() & IRQ_RANGING_MASTER_RESULT_VALID) // check for timeout { return true; } @@ -3384,7 +3262,6 @@ bool SX128XLT::transmitRanging(uint32_t address, uint16_t timeout, int8_t txpowe } } - uint8_t SX128XLT::receiveRanging(uint32_t address, uint16_t timeout, int8_t txpower, uint8_t wait) { #ifdef SX128XDEBUG @@ -3398,12 +3275,13 @@ uint8_t SX128XLT::receiveRanging(uint32_t address, uint16_t timeout, int8_t txpo if (!wait) { - return NO_WAIT; //not wait requested so no packet length to pass + return NO_WAIT; // not wait requested so no packet length to pass } - while (!digitalRead(_RXDonePin)); + while (!digitalRead(_RXDonePin)) + ; - setMode(MODE_STDBY_RC); //ensure to stop further packet reception + setMode(MODE_STDBY_RC); // ensure to stop further packet reception if (readIrqStatus() & IRQ_RANGING_SLAVE_REQUEST_VALID) { @@ -3411,11 +3289,10 @@ uint8_t SX128XLT::receiveRanging(uint32_t address, uint16_t timeout, int8_t txpo } else { - return false; //so we can check for packet having enough buffer space + return false; // so we can check for packet having enough buffer space } } - uint8_t SX128XLT::receiveRanging(uint32_t address, uint8_t bits, uint16_t timeout, int8_t txpower, uint8_t wait) { #ifdef SX128XDEBUG @@ -3429,12 +3306,13 @@ uint8_t SX128XLT::receiveRanging(uint32_t address, uint8_t bits, uint16_t timeou if (!wait) { - return NO_WAIT; //not wait requested so no packet length to pass + return NO_WAIT; // not wait requested so no packet length to pass } - while (!digitalRead(_RXDonePin)); + while (!digitalRead(_RXDonePin)) + ; - setMode(MODE_STDBY_RC); //ensure to stop further packet reception + setMode(MODE_STDBY_RC); // ensure to stop further packet reception if (readIrqStatus() & IRQ_RANGING_SLAVE_REQUEST_VALID) { @@ -3442,41 +3320,36 @@ uint8_t SX128XLT::receiveRanging(uint32_t address, uint8_t bits, uint16_t timeou } else { - return false; //so we can check for packet having enough buffer space + return false; // so we can check for packet having enough buffer space } } - - - uint16_t SX128XLT::lookupCalibrationValue(uint8_t spreadingfactor, uint8_t bandwidth) { - //this looks up the calibration value from the table in SX128XLT_Definitions.hifdef SX128XDEBUG + // this looks up the calibration value from the table in SX128XLT_Definitions.hifdef SX128XDEBUG #ifdef SX128XDEBUG Serial.println(F("lookupCalibrationValue()")); #endif switch (bandwidth) { - case LORA_BW_0400: - savedCalibration = RNG_CALIB_0400[(spreadingfactor >> 4) - 5]; - return savedCalibration; + case LORA_BW_0400: + savedCalibration = RNG_CALIB_0400[(spreadingfactor >> 4) - 5]; + return savedCalibration; - case LORA_BW_0800: - savedCalibration = RNG_CALIB_0800[(spreadingfactor >> 4) - 5]; - return savedCalibration; + case LORA_BW_0800: + savedCalibration = RNG_CALIB_0800[(spreadingfactor >> 4) - 5]; + return savedCalibration; + case LORA_BW_1600: + savedCalibration = RNG_CALIB_1600[(spreadingfactor >> 4) - 5]; + return savedCalibration; - case LORA_BW_1600: - savedCalibration = RNG_CALIB_1600[(spreadingfactor >> 4) - 5]; - return savedCalibration; - - default: - return 0xFFFF; + default: + return 0xFFFF; } } - uint16_t SX128XLT::getSetCalibrationValue() { #ifdef SX128XDEBUG @@ -3486,10 +3359,9 @@ uint16_t SX128XLT::getSetCalibrationValue() return savedCalibration; } - int16_t SX128XLT::getRangingRSSI() { - //Added November 2021 - see datasheet SX1280-1_V3.2, 14.5.3 Ranging RSSI + // Added November 2021 - see datasheet SX1280-1_V3.2, 14.5.3 Ranging RSSI int16_t regdata; regdata = readRegister(REG_RANGING_RSSI); regdata = regdata - 150; @@ -3515,7 +3387,6 @@ void SX128XLT::printASCIIArray(uint8_t *buffer, uint8_t size) } } - uint8_t SX128XLT::getReliableConfig(uint8_t bitread) { #ifdef SX128XDEBUGRELIABLE @@ -3526,7 +3397,6 @@ uint8_t SX128XLT::getReliableConfig(uint8_t bitread) return bitRead(_ReliableConfig, bitread); } - uint8_t SX128XLT::transmitReliable(uint8_t *txbuffer, uint8_t size, uint16_t networkID, uint32_t txtimeout, int8_t txpower, uint8_t wait) { #ifdef SX128XDEBUGRELIABLE @@ -3560,7 +3430,7 @@ uint8_t SX128XLT::transmitReliable(uint8_t *txbuffer, uint8_t size, uint16_t net else { payloadcrc = CRCCCITT(txbuffer, size, 0xFFFF); - //payloadcrc = CRCCCITT(txbuffer, size, 0xFFFF) + 1; + // payloadcrc = CRCCCITT(txbuffer, size, 0xFFFF) + 1; } checkBusy(); @@ -3593,7 +3463,7 @@ uint8_t SX128XLT::transmitReliable(uint8_t *txbuffer, uint8_t size, uint16_t net setPayloadLength(_TXPacketL); setTxParams(txpower, RAMP_TIME); - setDioIrqParams(IRQ_RADIO_ALL, (IRQ_TX_DONE + IRQ_RX_TX_TIMEOUT), 0, 0); //set for IRQ on TX done and timeout on DIO1 + setDioIrqParams(IRQ_RADIO_ALL, (IRQ_TX_DONE + IRQ_RX_TX_TIMEOUT), 0, 0); // set for IRQ on TX done and timeout on DIO1 setTx(txtimeout); if (!wait) @@ -3601,11 +3471,12 @@ uint8_t SX128XLT::transmitReliable(uint8_t *txbuffer, uint8_t size, uint16_t net return _TXPacketL; } - while (!digitalRead(_TXDonePin)); //Wait for DIO1 to go high + while (!digitalRead(_TXDonePin)) + ; // Wait for DIO1 to go high - setMode(MODE_STDBY_RC); //ensure we leave function with TX off + setMode(MODE_STDBY_RC); // ensure we leave function with TX off - if (readIrqStatus() & IRQ_RX_TX_TIMEOUT ) //check for timeout + if (readIrqStatus() & IRQ_RX_TX_TIMEOUT) // check for timeout { return 0; } @@ -3613,7 +3484,6 @@ uint8_t SX128XLT::transmitReliable(uint8_t *txbuffer, uint8_t size, uint16_t net return _TXPacketL; } - uint16_t SX128XLT::getTXPayloadCRC(uint8_t length) { #ifdef SX128XDEBUGRELIABLE @@ -3623,7 +3493,6 @@ uint16_t SX128XLT::getTXPayloadCRC(uint8_t length) return readUint16SXBuffer(length - 2); } - uint16_t SX128XLT::getRXPayloadCRC(uint8_t length) { #ifdef SX128XDEBUGRELIABLE @@ -3633,7 +3502,6 @@ uint16_t SX128XLT::getRXPayloadCRC(uint8_t length) return readUint16SXBuffer(length - 2); } - uint16_t SX128XLT::getRXNetworkID(uint8_t length) { #ifdef SX128XDEBUGRELIABLE @@ -3643,7 +3511,6 @@ uint16_t SX128XLT::getRXNetworkID(uint8_t length) return readUint16SXBuffer(length - 4); } - uint16_t SX128XLT::getTXNetworkID(uint8_t length) { #ifdef SX128XDEBUGRELIABLE @@ -3653,58 +3520,61 @@ uint16_t SX128XLT::getTXNetworkID(uint8_t length) return readUint16SXBuffer(length - 4); } - void SX128XLT::printReliableStatus() { #ifdef SX128XDEBUGRELIABLE Serial.print(F(" {RELIABLE} printReliableStatus() ")); #endif - //0x00 + // 0x00 if (bitRead(_ReliableErrors, ReliableCRCError)) { Serial.print(F(",ReliableCRCError")); } - //0x01 + // 0x01 if (bitRead(_ReliableErrors, ReliableIDError)) { Serial.print(F(",ReliableIDError")); } - //0x02 + // 0x02 if (bitRead(_ReliableErrors, ReliableSizeError)) { Serial.print(F(",ReliableSizeError")); } - //0x03 + // 0x03 if (bitRead(_ReliableErrors, ReliableACKError)) { Serial.print(F(",NoReliableACK")); } - //0x04 + // 0x04 if (bitRead(_ReliableErrors, ReliableTimeout)) { Serial.print(F(",ReliableTimeout")); } - //0x00 + // 0x07 + if (bitRead(_ReliableErrors, ReliableAddrError)) + { + Serial.print(F(",ReliableAddrError")); + } + + // 0x00 if (bitRead(_ReliableFlags, ReliableACKSent)) { Serial.print(F(",ACKsent")); } - //0x01 + // 0x01 if (bitRead(_ReliableFlags, ReliableACKReceived)) { Serial.print(F(",ACKreceived")); } - } - void SX128XLT::writeUint16SXBuffer(uint8_t addr, uint16_t regdata) { #ifdef SX128XDEBUGRELIABLE @@ -3714,14 +3584,14 @@ void SX128XLT::writeUint16SXBuffer(uint8_t addr, uint16_t regdata) Serial.println(regdata, HEX); #endif - setMode(MODE_STDBY_RC); //this is needed to ensure we can write to buffer OK. + setMode(MODE_STDBY_RC); // this is needed to ensure we can write to buffer OK. checkBusy(); #ifdef USE_SPI_TRANSACTION SPI.beginTransaction(SPISettings(LTspeedMaximum, LTdataOrder, LTdataMode)); #endif - digitalWrite(_NSS, LOW); //start the burst read + digitalWrite(_NSS, LOW); // start the burst read SPI.transfer(RADIO_WRITE_BUFFER); SPI.transfer(addr); SPI.transfer(lowByte(regdata)); @@ -3733,7 +3603,6 @@ void SX128XLT::writeUint16SXBuffer(uint8_t addr, uint16_t regdata) #endif } - uint16_t SX128XLT::readUint16SXBuffer(uint8_t addr) { #ifdef SX128XDEBUGRELIABLE @@ -3742,7 +3611,7 @@ uint16_t SX128XLT::readUint16SXBuffer(uint8_t addr) #endif uint8_t regdatalow, regdatahigh; - setMode(MODE_STDBY_RC); //this is needed to ensure we can read from buffer OK. + setMode(MODE_STDBY_RC); // this is needed to ensure we can read from buffer OK. checkBusy(); @@ -3750,7 +3619,7 @@ uint16_t SX128XLT::readUint16SXBuffer(uint8_t addr) SPI.beginTransaction(SPISettings(LTspeedMaximum, LTdataOrder, LTdataMode)); #endif - digitalWrite(_NSS, LOW); //start the burst read + digitalWrite(_NSS, LOW); // start the burst read SPI.transfer(RADIO_READ_BUFFER); SPI.transfer(addr); SPI.transfer(0xFF); @@ -3769,13 +3638,12 @@ uint16_t SX128XLT::readUint16SXBuffer(uint8_t addr) // Reliable packet routines - added November 2021 //****************************************************************************************** - -uint8_t SX128XLT::receiveReliable(uint8_t *rxbuffer, uint8_t size, uint16_t networkID, uint32_t rxtimeout, uint8_t wait ) +uint8_t SX128XLT::receiveReliable(uint8_t *rxbuffer, uint8_t size, uint16_t networkID, uint32_t rxtimeout, uint8_t wait) { - //Maximum total packet size is 255 bytes, so allowing for the 4 bytes appended to the end of a reliable - //packet, the maximum payload size for LORa is 251 bytes and 117 bytes for FLRC. So to avoid overwriting - //memory, we do need to check if the passed array is big enough to take the payload received in the packet. - //The assumed payload length will always be 4 bytes less than the received packet length. + // Maximum total packet size is 255 bytes, so allowing for the 4 bytes appended to the end of a reliable + // packet, the maximum payload size for LORa is 251 bytes and 117 bytes for FLRC. So to avoid overwriting + // memory, we do need to check if the passed array is big enough to take the payload received in the packet. + // The assumed payload length will always be 4 bytes less than the received packet length. #ifdef SX128XDEBUGRELIABLE Serial.println(); @@ -3792,40 +3660,40 @@ uint8_t SX128XLT::receiveReliable(uint8_t *rxbuffer, uint8_t size, uint16_t netw _ReliableErrors = 0; _ReliableFlags = 0; - if (size > 251 ) + if (size > 251) { bitSet(_ReliableErrors, ReliableSizeError); return 0; } setMode(MODE_STDBY_RC); - setDioIrqParams(IRQ_RADIO_ALL, (IRQ_RX_DONE + IRQ_RX_TX_TIMEOUT + IRQ_HEADER_ERROR), 0, 0); //set for IRQ on RX done or timeout + setDioIrqParams(IRQ_RADIO_ALL, (IRQ_RX_DONE + IRQ_RX_TX_TIMEOUT + IRQ_HEADER_ERROR), 0, 0); // set for IRQ on RX done or timeout setRx(rxtimeout); if (!wait) { - return 0; //not wait requested so no packet length to pass + return 0; // not wait requested so no packet length to pass } - while (!digitalRead(_RXDonePin)); //Wait for DIO1 to go high - setMode(MODE_STDBY_RC); //ensure to stop further packet reception + while (!digitalRead(_RXDonePin)) + ; // Wait for DIO1 to go high + setMode(MODE_STDBY_RC); // ensure to stop further packet reception - if ( readIrqStatus() & (IRQ_HEADER_ERROR + IRQ_CRC_ERROR + IRQ_RX_TX_TIMEOUT + IRQ_SYNCWORD_ERROR)) + if (readIrqStatus() & (IRQ_HEADER_ERROR + IRQ_CRC_ERROR + IRQ_RX_TX_TIMEOUT + IRQ_SYNCWORD_ERROR)) { - return 0; //packet is errored somewhere so return 0 + return 0; // packet is errored somewhere so return 0 } readCommand(RADIO_GET_RXBUFFERSTATUS, buffer, 2); _RXPacketL = buffer[0]; - - if (_RXPacketL < 4) //check received packet is 4 or more bytes long + if (_RXPacketL < 4) // check received packet is 4 or more bytes long { bitSet(_ReliableErrors, ReliableSizeError); return 0; } - if ((_RXPacketL - 4) > size ) //check if calculated payload size (_RXPacketL -4) fits in array + if ((_RXPacketL - 4) > size) // check if calculated payload size (_RXPacketL -4) fits in array { bitSet(_ReliableErrors, ReliableSizeError); return 0; @@ -3837,7 +3705,7 @@ uint8_t SX128XLT::receiveReliable(uint8_t *rxbuffer, uint8_t size, uint16_t netw SPI.beginTransaction(SPISettings(LTspeedMaximum, LTdataOrder, LTdataMode)); #endif - digitalWrite(_NSS, LOW); //start the burst read + digitalWrite(_NSS, LOW); // start the burst read SPI.transfer(RADIO_READ_BUFFER); SPI.transfer(0); SPI.transfer(0xFF); @@ -3850,7 +3718,7 @@ uint8_t SX128XLT::receiveReliable(uint8_t *rxbuffer, uint8_t size, uint16_t netw regdataL = SPI.transfer(0); regdataH = SPI.transfer(0); - RXnetworkID = ((uint16_t) regdataH << 8) + regdataL; + RXnetworkID = ((uint16_t)regdataH << 8) + regdataL; regdataL = SPI.transfer(0); regdataH = SPI.transfer(0); @@ -3863,7 +3731,7 @@ uint8_t SX128XLT::receiveReliable(uint8_t *rxbuffer, uint8_t size, uint16_t netw if (!bitRead(_ReliableConfig, NoReliableCRC)) { payloadcrc = CRCCCITT(rxbuffer, (_RXPacketL - 4), 0xFFFF); - RXcrc = ((uint16_t) regdataH << 8) + regdataL; + RXcrc = ((uint16_t)regdataH << 8) + regdataL; if (payloadcrc != RXcrc) { @@ -3876,30 +3744,28 @@ uint8_t SX128XLT::receiveReliable(uint8_t *rxbuffer, uint8_t size, uint16_t netw bitSet(_ReliableErrors, ReliableIDError); } - if (_ReliableErrors) //if there has been a reliable error return a RX fail + if (_ReliableErrors) // if there has been a reliable error return a RX fail { return 0; } - return _RXPacketL; //return and indicate RX OK. + return _RXPacketL; // return and indicate RX OK. } - void SX128XLT::setReliableRX(uint16_t timeout) { - //existing setRx() does not setup LoRa device as a receiver completly, just turns on receiver mode - //this routine does all the required setup for receive mode + // existing setRx() does not setup LoRa device as a receiver completly, just turns on receiver mode + // this routine does all the required setup for receive mode #ifdef SX128XDEBUGRELIABLE Serial.println(F(" {RELIABLE} setReliableRX()")); #endif - setMode(MODE_STDBY_RC); //stops receiver - clearIrqStatus(IRQ_RADIO_ALL); //clear current interrupt flags + setMode(MODE_STDBY_RC); // stops receiver + clearIrqStatus(IRQ_RADIO_ALL); // clear current interrupt flags setDioIrqParams(IRQ_RADIO_ALL, (IRQ_RX_DONE + IRQ_RX_TX_TIMEOUT + IRQ_HEADER_ERROR), 0, 0); setRx(timeout); } - uint8_t SX128XLT::transmitReliableAutoACK(uint8_t *txbuffer, uint8_t size, uint16_t networkID, uint32_t acktimeout, uint32_t txtimeout, int8_t txpower, uint8_t wait) { #ifdef SX128XDEBUGRELIABLE @@ -3933,7 +3799,7 @@ uint8_t SX128XLT::transmitReliableAutoACK(uint8_t *txbuffer, uint8_t size, uint1 else { payloadcrc = CRCCCITT(txbuffer, size, 0xFFFF); - //payloadcrc = CRCCCITT(txbuffer, size, 0xFFFF) + 1; + // payloadcrc = CRCCCITT(txbuffer, size, 0xFFFF) + 1; } checkBusy(); @@ -3965,7 +3831,7 @@ uint8_t SX128XLT::transmitReliableAutoACK(uint8_t *txbuffer, uint8_t size, uint1 setPayloadLength(_TXPacketL); setTxParams(txpower, RAMP_TIME); - setDioIrqParams(IRQ_RADIO_ALL, (IRQ_TX_DONE + IRQ_RX_TX_TIMEOUT), 0, 0); //set for IRQ on TX done and timeout on DIO1 + setDioIrqParams(IRQ_RADIO_ALL, (IRQ_TX_DONE + IRQ_RX_TX_TIMEOUT), 0, 0); // set for IRQ on TX done and timeout on DIO1 setTx(txtimeout); if (!wait) @@ -3973,11 +3839,12 @@ uint8_t SX128XLT::transmitReliableAutoACK(uint8_t *txbuffer, uint8_t size, uint1 return _TXPacketL; } - while (!digitalRead(_TXDonePin)); //Wait for DIO1 to go high + while (!digitalRead(_TXDonePin)) + ; // Wait for DIO1 to go high - setMode(MODE_STDBY_RC); //ensure we leave function with TX off + setMode(MODE_STDBY_RC); // ensure we leave function with TX off - if (readIrqStatus() & IRQ_RX_TX_TIMEOUT ) //check for timeout + if (readIrqStatus() & IRQ_RX_TX_TIMEOUT) // check for timeout { return 0; } @@ -3992,7 +3859,6 @@ uint8_t SX128XLT::transmitReliableAutoACK(uint8_t *txbuffer, uint8_t size, uint1 return _TXPacketL; } - uint8_t SX128XLT::waitReliableACK(uint16_t networkID, uint16_t payloadcrc, uint32_t acktimeout) { @@ -4009,9 +3875,9 @@ uint8_t SX128XLT::waitReliableACK(uint16_t networkID, uint16_t payloadcrc, uint3 do { - if (digitalRead(_RXDonePin)) //has a packet arrived ? + if (digitalRead(_RXDonePin)) // has a packet arrived ? { - if ( readIrqStatus() & (IRQ_HEADER_ERROR + IRQ_CRC_ERROR + IRQ_RX_TX_TIMEOUT + IRQ_SYNCWORD_ERROR)) + if (readIrqStatus() & (IRQ_HEADER_ERROR + IRQ_CRC_ERROR + IRQ_RX_TX_TIMEOUT + IRQ_SYNCWORD_ERROR)) { setReliableRX(0); continue; @@ -4022,10 +3888,10 @@ uint8_t SX128XLT::waitReliableACK(uint16_t networkID, uint16_t payloadcrc, uint3 RXnetworkID = readUint16SXBuffer(_RXPacketL - 4); RXcrc = readUint16SXBuffer(_RXPacketL - 2); - if ( (RXnetworkID == networkID) && (RXcrc == payloadcrc)) + if ((RXnetworkID == networkID) && (RXcrc == payloadcrc)) { bitSet(_ReliableFlags, ReliableACKReceived); - return 4; //return value of 4 indicates valid ack + return 4; // return value of 4 indicates valid ack } else { @@ -4034,16 +3900,15 @@ uint8_t SX128XLT::waitReliableACK(uint16_t networkID, uint16_t payloadcrc, uint3 } } - } while ( ((uint32_t) (millis() - startmS) < acktimeout)); + } while (((uint32_t)(millis() - startmS) < acktimeout)); bitSet(_ReliableErrors, ReliableACKError); return 0; } - uint8_t SX128XLT::waitReliableACK(uint8_t *rxbuffer, uint8_t size, uint16_t networkID, uint16_t payloadcrc, uint32_t acktimeout) { - //overloaded version of waitReliableACK() for use when ack contains payload data + // overloaded version of waitReliableACK() for use when ack contains payload data #ifdef SX128XDEBUGRELIABLE Serial.println(F(" {RELIABLE} waitReliableACK()")); #endif @@ -4053,8 +3918,7 @@ uint8_t SX128XLT::waitReliableACK(uint8_t *rxbuffer, uint8_t size, uint16_t netw uint8_t buffer[2]; uint8_t regdata, index; - - if (size > 251 ) + if (size > 251) { bitSet(_ReliableErrors, ReliableSizeError); return 0; @@ -4065,11 +3929,11 @@ uint8_t SX128XLT::waitReliableACK(uint8_t *rxbuffer, uint8_t size, uint16_t netw do { - if (digitalRead(_RXDonePin)) //has a packet arrived ? + if (digitalRead(_RXDonePin)) // has a packet arrived ? { regdata = readIrqStatus(); - if ( readIrqStatus() & (IRQ_HEADER_ERROR + IRQ_CRC_ERROR + IRQ_RX_TX_TIMEOUT + IRQ_SYNCWORD_ERROR)) + if (readIrqStatus() & (IRQ_HEADER_ERROR + IRQ_CRC_ERROR + IRQ_RX_TX_TIMEOUT + IRQ_SYNCWORD_ERROR)) { setReliableRX(0); continue; @@ -4080,9 +3944,9 @@ uint8_t SX128XLT::waitReliableACK(uint8_t *rxbuffer, uint8_t size, uint16_t netw RXnetworkID = readUint16SXBuffer(_RXPacketL - 4); RXcrc = readUint16SXBuffer(_RXPacketL - 2); - if ( (RXnetworkID == networkID) && (RXcrc == payloadcrc)) + if ((RXnetworkID == networkID) && (RXcrc == payloadcrc)) { - if ((_RXPacketL - 4) > size ) //check passed buffer is big enough for payload + if ((_RXPacketL - 4) > size) // check passed buffer is big enough for payload { bitSet(_ReliableErrors, ReliableACKError); bitSet(_ReliableErrors, ReliableSizeError); @@ -4096,12 +3960,12 @@ uint8_t SX128XLT::waitReliableACK(uint8_t *rxbuffer, uint8_t size, uint16_t netw SPI.beginTransaction(SPISettings(LTspeedMaximum, LTdataOrder, LTdataMode)); #endif - digitalWrite(_NSS, LOW); //start the burst read + digitalWrite(_NSS, LOW); // start the burst read SPI.transfer(RADIO_READ_BUFFER); SPI.transfer(0); SPI.transfer(0xFF); - for (index = 0; index < (_RXPacketL - 4); index++) //read packet into rxbuffer + for (index = 0; index < (_RXPacketL - 4); index++) // read packet into rxbuffer { regdata = SPI.transfer(0); rxbuffer[index] = regdata; @@ -4112,7 +3976,7 @@ uint8_t SX128XLT::waitReliableACK(uint8_t *rxbuffer, uint8_t size, uint16_t netw SPI.endTransaction(); #endif - return _RXPacketL; //_RXPacketL should be payload length + 4 + return _RXPacketL; //_RXPacketL should be payload length + 4 } else { @@ -4120,21 +3984,18 @@ uint8_t SX128XLT::waitReliableACK(uint8_t *rxbuffer, uint8_t size, uint16_t netw continue; } } - } while ( ((uint32_t) (millis() - startmS) < acktimeout) ); + } while (((uint32_t)(millis() - startmS) < acktimeout)); bitSet(_ReliableErrors, ReliableACKError); return 0; } - - -uint8_t SX128XLT::receiveReliableAutoACK(uint8_t *rxbuffer, uint8_t size, uint16_t networkID, uint32_t ackdelay, int8_t txpower, uint32_t rxtimeout, uint8_t wait ) +uint8_t SX128XLT::receiveReliableAutoACK(uint8_t *rxbuffer, uint8_t size, uint16_t networkID, uint32_t ackdelay, int8_t txpower, uint32_t rxtimeout, uint8_t wait) { - //Maximum total packet size is 255 bytes, so allowing for the 4 bytes appended to the end of a reliable - //packet, the maximum payload size for LORa is 251 bytes and 117 bytes for FLRC. So to avoid overwriting - //memory, we do need to check if the passed array is big enough to take the payload received in the packet. - //The assumed payload length will always be 4 bytes less than the received packet length. - + // Maximum total packet size is 255 bytes, so allowing for the 4 bytes appended to the end of a reliable + // packet, the maximum payload size for LORa is 251 bytes and 117 bytes for FLRC. So to avoid overwriting + // memory, we do need to check if the passed array is big enough to take the payload received in the packet. + // The assumed payload length will always be 4 bytes less than the received packet length. #ifdef SX128XDEBUGRELIABLE Serial.println(); @@ -4150,39 +4011,40 @@ uint8_t SX128XLT::receiveReliableAutoACK(uint8_t *rxbuffer, uint8_t size, uint16 _ReliableErrors = 0; _ReliableFlags = 0; - if (size > 251 ) + if (size > 251) { bitSet(_ReliableErrors, ReliableSizeError); return 0; } setMode(MODE_STDBY_RC); - setDioIrqParams(IRQ_RADIO_ALL, (IRQ_RX_DONE + IRQ_RX_TX_TIMEOUT + IRQ_HEADER_ERROR), 0, 0); //set for IRQ on RX done or timeout + setDioIrqParams(IRQ_RADIO_ALL, (IRQ_RX_DONE + IRQ_RX_TX_TIMEOUT + IRQ_HEADER_ERROR), 0, 0); // set for IRQ on RX done or timeout setRx(rxtimeout); if (!wait) { - return 0; //not wait requested so no packet length to pass + return 0; // not wait requested so no packet length to pass } - while (!digitalRead(_RXDonePin)); //Wait for DIO1 to go high - setMode(MODE_STDBY_RC); //ensure to stop further packet reception + while (!digitalRead(_RXDonePin)) + ; // Wait for DIO1 to go high + setMode(MODE_STDBY_RC); // ensure to stop further packet reception - if ( readIrqStatus() & (IRQ_HEADER_ERROR + IRQ_CRC_ERROR + IRQ_RX_TX_TIMEOUT + IRQ_SYNCWORD_ERROR)) + if (readIrqStatus() & (IRQ_HEADER_ERROR + IRQ_CRC_ERROR + IRQ_RX_TX_TIMEOUT + IRQ_SYNCWORD_ERROR)) { - return 0; //packet is errored somewhere so return 0 + return 0; // packet is errored somewhere so return 0 } readCommand(RADIO_GET_RXBUFFERSTATUS, buffer, 2); _RXPacketL = buffer[0]; - if (_RXPacketL < 4) //check received packet is 4 or more bytes long + if (_RXPacketL < 4) // check received packet is 4 or more bytes long { bitSet(_ReliableErrors, ReliableSizeError); return 0; } - if ((_RXPacketL - 4) > size ) //check if calculated payload size (_RXPacketL -4) fits in array + if ((_RXPacketL - 4) > size) // check if calculated payload size (_RXPacketL -4) fits in array { bitSet(_ReliableErrors, ReliableSizeError); return 0; @@ -4194,7 +4056,7 @@ uint8_t SX128XLT::receiveReliableAutoACK(uint8_t *rxbuffer, uint8_t size, uint16 SPI.beginTransaction(SPISettings(LTspeedMaximum, LTdataOrder, LTdataMode)); #endif - digitalWrite(_NSS, LOW); //start the burst read + digitalWrite(_NSS, LOW); // start the burst read SPI.transfer(RADIO_READ_BUFFER); SPI.transfer(0); SPI.transfer(0xFF); @@ -4207,7 +4069,7 @@ uint8_t SX128XLT::receiveReliableAutoACK(uint8_t *rxbuffer, uint8_t size, uint16 regdataL = SPI.transfer(0); regdataH = SPI.transfer(0); - RXnetworkID = ((uint16_t) regdataH << 8) + regdataL; + RXnetworkID = ((uint16_t)regdataH << 8) + regdataL; regdataL = SPI.transfer(0); regdataH = SPI.transfer(0); digitalWrite(_NSS, HIGH); @@ -4219,8 +4081,8 @@ uint8_t SX128XLT::receiveReliableAutoACK(uint8_t *rxbuffer, uint8_t size, uint16 if (!bitRead(_ReliableConfig, NoReliableCRC)) { payloadcrc = CRCCCITT(rxbuffer, (_RXPacketL - 4), 0xFFFF); - //payloadcrc = CRCCCITT(rxbuffer, (_RXPacketL - 4), 0xFFFF) + 1; - RXcrc = ((uint16_t) regdataH << 8) + regdataL; + // payloadcrc = CRCCCITT(rxbuffer, (_RXPacketL - 4), 0xFFFF) + 1; + RXcrc = ((uint16_t)regdataH << 8) + regdataL; if (payloadcrc != RXcrc) { @@ -4233,7 +4095,7 @@ uint8_t SX128XLT::receiveReliableAutoACK(uint8_t *rxbuffer, uint8_t size, uint16 bitSet(_ReliableErrors, ReliableIDError); } - if (_ReliableErrors) //if there has been a reliable error return a RX fail + if (_ReliableErrors) // if there has been a reliable error return a RX fail { return 0; } @@ -4246,10 +4108,9 @@ uint8_t SX128XLT::receiveReliableAutoACK(uint8_t *rxbuffer, uint8_t size, uint16 return 0; } - return _RXPacketL; //return and indicate RX OK. + return _RXPacketL; // return and indicate RX OK. } - uint8_t SX128XLT::sendReliableACK(uint16_t networkID, uint16_t payloadcrc, int8_t txpower) { @@ -4257,11 +4118,11 @@ uint8_t SX128XLT::sendReliableACK(uint16_t networkID, uint16_t payloadcrc, int8_ Serial.println(F(" {RELIABLE} sendReliableACK()")); #endif - uint32_t txtimeout = 12000; //set TX timeout to 12 seconds, longest packet is 8.7secs - _TXPacketL = 4; //packet is networkId (2 bytes) + payloadCRC (2 bytes) + uint32_t txtimeout = 12000; // set TX timeout to 12 seconds, longest packet is 8.7secs + _TXPacketL = 4; // packet is networkId (2 bytes) + payloadCRC (2 bytes) setMode(MODE_STDBY_RC); - //payloadcrc++; + // payloadcrc++; writeUint16SXBuffer(0, networkID); writeUint16SXBuffer(2, payloadcrc); @@ -4269,36 +4130,36 @@ uint8_t SX128XLT::sendReliableACK(uint16_t networkID, uint16_t payloadcrc, int8_ checkBusy(); setPayloadLength(_TXPacketL); setTxParams(txpower, RAMP_TIME); - setDioIrqParams(IRQ_RADIO_ALL, (IRQ_TX_DONE + IRQ_RX_TX_TIMEOUT), 0, 0); //set for IRQ on TX done and timeout on DIO1 + setDioIrqParams(IRQ_RADIO_ALL, (IRQ_TX_DONE + IRQ_RX_TX_TIMEOUT), 0, 0); // set for IRQ on TX done and timeout on DIO1 setTx(txtimeout); - while (!digitalRead(_TXDonePin)); + while (!digitalRead(_TXDonePin)) + ; - setMode(MODE_STDBY_RC); //ensure we leave function with TX off + setMode(MODE_STDBY_RC); // ensure we leave function with TX off - if (readIrqStatus() & IRQ_RX_TX_TIMEOUT ) //check for timeout + if (readIrqStatus() & IRQ_RX_TX_TIMEOUT) // check for timeout { return 0; } bitSet(_ReliableFlags, ReliableACKSent); - return 4; //TX OK so return TXpacket length + return 4; // TX OK so return TXpacket length } - uint8_t SX128XLT::sendReliableACK(uint8_t *txbuffer, uint8_t size, uint16_t networkID, uint16_t payloadcrc, int8_t txpower) { - //overloaded version of sendReliableACK() for use when ack contains payload data + // overloaded version of sendReliableACK() for use when ack contains payload data #ifdef SX128XDEBUGRELIABLE Serial.print(F(" {RELIABLE} sendReliableACK() ")); Serial.print(F("buffer size ")); Serial.println(size); #endif - uint32_t txtimeout = 12000; //set TX timeout to 12 seconds, longest packet is 8.7secs + uint32_t txtimeout = 12000; // set TX timeout to 12 seconds, longest packet is 8.7secs uint8_t bufferdata, index; - //payloadcrc++; + // payloadcrc++; setMode(MODE_STDBY_RC); _TXPacketL = size + 4; @@ -4331,14 +4192,15 @@ uint8_t SX128XLT::sendReliableACK(uint8_t *txbuffer, uint8_t size, uint16_t netw setPayloadLength(_TXPacketL); setTxParams(txpower, RAMP_TIME); - setDioIrqParams(IRQ_RADIO_ALL, (IRQ_TX_DONE + IRQ_RX_TX_TIMEOUT), 0, 0); //set for IRQ on TX done and timeout on DIO1 - setTx(txtimeout); //this starts the TX + setDioIrqParams(IRQ_RADIO_ALL, (IRQ_TX_DONE + IRQ_RX_TX_TIMEOUT), 0, 0); // set for IRQ on TX done and timeout on DIO1 + setTx(txtimeout); // this starts the TX - while (!digitalRead(_TXDonePin)); //Wait for DIO1 to go high + while (!digitalRead(_TXDonePin)) + ; // Wait for DIO1 to go high - setMode(MODE_STDBY_RC); //ensure we leave function with TX off + setMode(MODE_STDBY_RC); // ensure we leave function with TX off - if (readIrqStatus() & IRQ_RX_TX_TIMEOUT ) //check for timeout + if (readIrqStatus() & IRQ_RX_TX_TIMEOUT) // check for timeout { return 0; } @@ -4348,9 +4210,319 @@ uint8_t SX128XLT::sendReliableACK(uint8_t *txbuffer, uint8_t size, uint16_t netw } bitSet(_ReliableFlags, ReliableACKSent); - return _TXPacketL; //TX OK so return TXpacket length + return _TXPacketL; // TX OK so return TXpacket length } +uint8_t SX128XLT::transmitReliableAutoACK_addr(uint8_t *txbuffer, uint8_t size, uint16_t networkID, uint16_t destAddr, uint16_t srcAddr, uint32_t acktimeout, uint32_t txtimeout, int8_t txpower, uint8_t wait) +{ +#ifdef SX128XDEBUGRELIABLE + Serial.println(F(" {RELIABLE} transmitReliableAutoACK_addr() ")); + Serial.print(F(" {RELIABLE} _ReliableConfig ")); + Serial.println(_ReliableConfig, HEX); + Serial.print(F(" {RELIABLE} Payload length ")); + Serial.println(size); +#endif + + uint8_t index, tempdata, RXPacketL; + uint16_t payloadcrc; + + _ReliableErrors = 0; + _ReliableFlags = 0; + + if (size > 247) // 255 - (networkID + destAddr + srcAddr + payloadcrc) + { + bitSet(_ReliableErrors, ReliableSizeError); + return 0; + } + + setMode(MODE_STDBY_RC); + checkBusy(); + _TXPacketL = size + 8; // Extra 8 bytes for payload header + + if (bitRead(_ReliableConfig, NoReliableCRC)) + { + payloadcrc = 0; + } + else + { + payloadcrc = CRCCCITT(txbuffer, size, 0xFFFF); + } + + checkBusy(); + +#ifdef USE_SPI_TRANSACTION + SPI.beginTransaction(SPISettings(LTspeedMaximum, LTdataOrder, LTdataMode)); +#endif + + digitalWrite(_NSS, LOW); + SPI.transfer(RADIO_WRITE_BUFFER); + SPI.transfer(0); + + for (index = 0; index < size; index++) + { + tempdata = txbuffer[index]; + SPI.transfer(tempdata); + } + + SPI.transfer(lowByte(networkID)); + SPI.transfer(highByte(networkID)); + SPI.transfer(lowByte(destAddr)); + SPI.transfer(highByte(destAddr)); + SPI.transfer(lowByte(srcAddr)); + SPI.transfer(highByte(srcAddr)); + SPI.transfer(lowByte(payloadcrc)); + SPI.transfer(highByte(payloadcrc)); + + digitalWrite(_NSS, HIGH); + +#ifdef USE_SPI_TRANSACTION + SPI.endTransaction(); +#endif + + setPayloadLength(_TXPacketL); + setTxParams(txpower, RAMP_TIME); + setDioIrqParams(IRQ_RADIO_ALL, (IRQ_TX_DONE + IRQ_RX_TX_TIMEOUT), 0, 0); + setTx(txtimeout); + + if (!wait) + { + return _TXPacketL; + } + + while (!digitalRead(_TXDonePin)) + ; + + setMode(MODE_STDBY_RC); + + if (readIrqStatus() & IRQ_RX_TX_TIMEOUT) + { + return 0; + } + + RXPacketL = waitReliableACK_addr(networkID, destAddr, srcAddr, payloadcrc, acktimeout); + + if (RXPacketL != 8) + { + return 0; + } + + return _TXPacketL; +} + +uint8_t SX128XLT::waitReliableACK_addr(uint16_t networkID, uint16_t destAddr, uint16_t srcAddr, uint16_t payloadcrc, uint32_t acktimeout) +{ +#ifdef SX128XDEBUGRELIABLE + Serial.println(F(" {RELIABLE} waitReliableACK_addr()")); +#endif + + uint16_t RXnetworkID, RXsrcAddr, RXdestAddr, RXcrc; + uint32_t startmS; + uint8_t buffer[2]; + + setReliableRX(0); + startmS = millis(); + + do + { + if (digitalRead(_RXDonePin)) + { + if (readIrqStatus() & (IRQ_HEADER_ERROR + IRQ_CRC_ERROR + IRQ_RX_TX_TIMEOUT + IRQ_SYNCWORD_ERROR)) + { + setReliableRX(0); + continue; + } + + readCommand(RADIO_GET_RXBUFFERSTATUS, buffer, 2); + _RXPacketL = buffer[0]; + + RXnetworkID = readUint16SXBuffer(_RXPacketL - 8); + RXdestAddr = readUint16SXBuffer(_RXPacketL - 6); + RXsrcAddr = readUint16SXBuffer(_RXPacketL - 4); + RXcrc = readUint16SXBuffer(_RXPacketL - 2); + + if ((RXnetworkID == networkID) && (RXsrcAddr == srcAddr) && (RXdestAddr == destAddr) && (RXcrc == payloadcrc)) + { + bitSet(_ReliableFlags, ReliableACKReceived); + return 8; + } + else + { + setReliableRX(0); + continue; + } + } + } while (((uint32_t)(millis() - startmS) < acktimeout)); + + bitSet(_ReliableErrors, ReliableACKError); + return 0; +} + +uint8_t SX128XLT::receiveReliableAutoACK_addr(uint8_t *rxbuffer, uint8_t size, uint16_t networkID, uint16_t destAddr, uint32_t ackdelay, int8_t txpower, uint32_t rxtimeout, uint8_t wait, uint16_t *srcAddr) +{ +#ifdef SX128XDEBUGRELIABLE + Serial.println(); + Serial.println(F(" {RELIABLE} receiveReliableAutoACK_addr()")); + Serial.print(F(" {RELIABLE} _ReliableConfig ")); + Serial.println(_ReliableConfig, HEX); +#endif + + uint16_t payloadcrc = 0, RXcrc, RXnetworkID, RXdestAddr, RXsrcAddr; + uint8_t regdataL, regdataH, index; + uint8_t buffer[2]; + + _ReliableErrors = 0; + _ReliableFlags = 0; + + if (size > 247) + { + bitSet(_ReliableErrors, ReliableSizeError); + return 0; + } + + setMode(MODE_STDBY_RC); + setDioIrqParams(IRQ_RADIO_ALL, (IRQ_RX_DONE + IRQ_RX_TX_TIMEOUT + IRQ_HEADER_ERROR), 0, 0); + setRx(rxtimeout); + + if (!wait) + { + return 0; + } + + while (!digitalRead(_RXDonePin)) + ; // Wait for packet + setMode(MODE_STDBY_RC); + + if (readIrqStatus() & (IRQ_HEADER_ERROR + IRQ_CRC_ERROR + IRQ_RX_TX_TIMEOUT + IRQ_SYNCWORD_ERROR)) + { + return 0; + } + + readCommand(RADIO_GET_RXBUFFERSTATUS, buffer, 2); + _RXPacketL = buffer[0]; + + if (_RXPacketL < 8) + { + bitSet(_ReliableErrors, ReliableSizeError); + return 0; + } + + if ((_RXPacketL - 8) > size) + { + bitSet(_ReliableErrors, ReliableSizeError); + return 0; + } + + checkBusy(); + +#ifdef USE_SPI_TRANSACTION + SPI.beginTransaction(SPISettings(LTspeedMaximum, LTdataOrder, LTdataMode)); +#endif + + digitalWrite(_NSS, LOW); + SPI.transfer(RADIO_READ_BUFFER); + SPI.transfer(0); + SPI.transfer(0xFF); + + for (index = 0; index < (_RXPacketL - 8); index++) + { + regdataL = SPI.transfer(0); + rxbuffer[index] = regdataL; + } + + regdataL = SPI.transfer(0); + regdataH = SPI.transfer(0); + RXnetworkID = ((uint16_t)regdataH << 8) + regdataL; + regdataL = SPI.transfer(0); + regdataH = SPI.transfer(0); + RXdestAddr = ((uint16_t)regdataH << 8) + regdataL; + regdataL = SPI.transfer(0); + regdataH = SPI.transfer(0); + RXsrcAddr = ((uint16_t)regdataH << 8) + regdataL; + regdataL = SPI.transfer(0); + regdataH = SPI.transfer(0); + RXcrc = ((uint16_t)regdataH << 8) + regdataL; + + if (srcAddr) + { // If the argument of srcAddr is available, pass the received Source address + *srcAddr = RXsrcAddr; + } + + digitalWrite(_NSS, HIGH); + +#ifdef USE_SPI_TRANSACTION + SPI.endTransaction(); +#endif + + // Validate the networkID + if (RXnetworkID != networkID) + { + bitSet(_ReliableErrors, ReliableIDError); + return 0; + } + + // Validate the Destination address + if (RXdestAddr != destAddr) + { + bitSet(_ReliableErrors, ReliableAddrError); + return 0; + } + + // Validate CRC + if (!bitRead(_ReliableConfig, NoReliableCRC)) + { + payloadcrc = CRCCCITT(rxbuffer, (_RXPacketL - 8), 0xFFFF); + if (payloadcrc != RXcrc) + { + bitSet(_ReliableErrors, ReliableCRCError); + return 0; + } + } + + delay(ackdelay); // Wait before sending the acknowledgment + _TXPacketL = sendReliableACK_addr(networkID, RXsrcAddr, destAddr, payloadcrc, txpower); + + if (_TXPacketL != 8) + { + return 0; + } + + return _RXPacketL; // Return the packet length +} + +uint8_t SX128XLT::sendReliableACK_addr(uint16_t networkID, uint16_t srcAddr, uint16_t destAddr, uint16_t payloadcrc, int8_t txpower) +{ +#ifdef SX128XDEBUGRELIABLE + Serial.println(F(" {RELIABLE} sendReliableACK_addr()")); +#endif + + uint32_t txtimeout = 12000; + _TXPacketL = 8; // Packet includes networkID, srcAddr, destAddr, and payloadCRC + setMode(MODE_STDBY_RC); + + writeUint16SXBuffer(0, networkID); + writeUint16SXBuffer(2, destAddr); + writeUint16SXBuffer(4, srcAddr); // Responding to the source address of the received packet + writeUint16SXBuffer(6, payloadcrc); + + checkBusy(); + setPayloadLength(_TXPacketL); + setTxParams(txpower, RAMP_TIME); + setDioIrqParams(IRQ_RADIO_ALL, (IRQ_TX_DONE + IRQ_RX_TX_TIMEOUT), 0, 0); + setTx(txtimeout); + + while (!digitalRead(_TXDonePin)) + ; + + setMode(MODE_STDBY_RC); + + if (readIrqStatus() & IRQ_RX_TX_TIMEOUT) + { + return 0; + } + + bitSet(_ReliableFlags, ReliableACKSent); + return 8; +} uint16_t SX128XLT::CRCCCITTReliable(uint8_t startadd, uint8_t endadd, uint16_t startvalue) { @@ -4358,7 +4530,7 @@ uint16_t SX128XLT::CRCCCITTReliable(uint8_t startadd, uint8_t endadd, uint16_t s Serial.println(F(" {RELIABLE} CRCCCITTReliable()")); #endif - //generates a CRC of bytes from the internal SX buffer, _RXPackletL and _TXPackletL are not affected + // generates a CRC of bytes from the internal SX buffer, _RXPackletL and _TXPackletL are not affected #ifdef SX128DEBUGRELIABLE Serial.println(F(" {RELIABLE} CRCCCITTReliable() ")); @@ -4367,7 +4539,7 @@ uint16_t SX128XLT::CRCCCITTReliable(uint8_t startadd, uint8_t endadd, uint16_t s uint16_t index, libraryCRC; uint8_t j, readSX; - libraryCRC = startvalue; //start value for CRC16 + libraryCRC = startvalue; // start value for CRC16 setMode(MODE_STDBY_RC); checkBusy(); @@ -4375,7 +4547,7 @@ uint16_t SX128XLT::CRCCCITTReliable(uint8_t startadd, uint8_t endadd, uint16_t s SPI.beginTransaction(SPISettings(LTspeedMaximum, LTdataOrder, LTdataMode)); #endif - digitalWrite(_NSS, LOW); //start the burst read + digitalWrite(_NSS, LOW); // start the burst read SPI.transfer(RADIO_READ_BUFFER); SPI.transfer(startadd); SPI.transfer(0xFF); @@ -4383,7 +4555,7 @@ uint16_t SX128XLT::CRCCCITTReliable(uint8_t startadd, uint8_t endadd, uint16_t s for (index = startadd; index <= endadd; index++) { readSX = SPI.transfer(0); - libraryCRC ^= (((uint16_t) readSX ) << 8); + libraryCRC ^= (((uint16_t)readSX) << 8); for (j = 0; j < 8; j++) { if (libraryCRC & 0x8000) @@ -4402,7 +4574,6 @@ uint16_t SX128XLT::CRCCCITTReliable(uint8_t startadd, uint8_t endadd, uint16_t s return libraryCRC; } - void SX128XLT::setReliableConfig(uint8_t bitset) { #ifdef SX128XDEBUGRELIABLE @@ -4414,7 +4585,6 @@ void SX128XLT::setReliableConfig(uint8_t bitset) return; } - void SX128XLT::clearReliableConfig(uint8_t bitset) { #ifdef SX128XDEBUGRELIABLE @@ -4426,8 +4596,6 @@ void SX128XLT::clearReliableConfig(uint8_t bitset) return; } - - uint8_t SX128XLT::readReliableErrors() { #ifdef SX128XDEBUGRELIABLE @@ -4446,13 +4614,11 @@ uint8_t SX128XLT::readReliableFlags() return _ReliableFlags; } - //****************************************************************************************** // Reliable SX packet routines - added November 2021 // SX Packet routines directly read\write data\bvariables to the LoRa device internal buffer //****************************************************************************************** - uint8_t SX128XLT::transmitSXReliable(uint8_t startaddr, uint8_t length, uint16_t networkID, uint32_t txtimeout, int8_t txpower, uint8_t wait) { #ifdef SX128XDEBUGRELIABLE @@ -4484,7 +4650,7 @@ uint8_t SX128XLT::transmitSXReliable(uint8_t startaddr, uint8_t length, uint16_t else { payloadcrc = CRCCCITTReliable(startaddr, startaddr + length - 1, 0xFFFF); - //payloadcrc = CRCCCITTReliable(startaddr, startaddr + length - 1, 0xFFFF) + 1; + // payloadcrc = CRCCCITTReliable(startaddr, startaddr + length - 1, 0xFFFF) + 1; } writeUint16SXBuffer(startaddr + _TXPacketL - 4, networkID); @@ -4492,19 +4658,20 @@ uint8_t SX128XLT::transmitSXReliable(uint8_t startaddr, uint8_t length, uint16_t setPayloadLength(_TXPacketL); setTxParams(txpower, RAMP_TIME); - setDioIrqParams(IRQ_RADIO_ALL, (IRQ_TX_DONE + IRQ_RX_TX_TIMEOUT), 0, 0); //set for IRQ on TX done and timeout on DIO1 - setTx(txtimeout); //this starts the TX + setDioIrqParams(IRQ_RADIO_ALL, (IRQ_TX_DONE + IRQ_RX_TX_TIMEOUT), 0, 0); // set for IRQ on TX done and timeout on DIO1 + setTx(txtimeout); // this starts the TX if (!wait) { return _TXPacketL; } - while (!digitalRead(_TXDonePin)); //Wait for pin to go high, TX finished + while (!digitalRead(_TXDonePin)) + ; // Wait for pin to go high, TX finished - setMode(MODE_STDBY_RC); //ensure we leave function with TX off + setMode(MODE_STDBY_RC); // ensure we leave function with TX off - if (readIrqStatus() & IRQ_RX_TX_TIMEOUT ) //check for timeout + if (readIrqStatus() & IRQ_RX_TX_TIMEOUT) // check for timeout { return 0; } @@ -4512,7 +4679,6 @@ uint8_t SX128XLT::transmitSXReliable(uint8_t startaddr, uint8_t length, uint16_t return _TXPacketL; } - uint8_t SX128XLT::transmitSXReliableIRQ(uint8_t startaddr, uint8_t length, uint16_t networkID, uint32_t txtimeout, int8_t txpower, uint8_t wait) { #ifdef SX128XDEBUGRELIABLE @@ -4544,26 +4710,27 @@ uint8_t SX128XLT::transmitSXReliableIRQ(uint8_t startaddr, uint8_t length, uint1 else { payloadcrc = CRCCCITTReliable(startaddr, startaddr + length - 1, 0xFFFF); - //payloadcrc = CRCCCITTReliable(startaddr, startaddr + length - 1, 0xFFFF) + 1; + // payloadcrc = CRCCCITTReliable(startaddr, startaddr + length - 1, 0xFFFF) + 1; } writeUint16SXBuffer(startaddr + _TXPacketL - 4, networkID); writeUint16SXBuffer(startaddr + _TXPacketL - 2, payloadcrc); setPayloadLength(_TXPacketL); setTxParams(txpower, RAMP_TIME); - setTx(txtimeout); //this starts the TX + setTx(txtimeout); // this starts the TX if (!wait) { return _TXPacketL; } - //0x4001 = IRQ_TX_DONE + IRQ_RX_TX_TIMEOUT - while (!(readIrqStatus() & 0x4001 )); //wait for IRQs going active + // 0x4001 = IRQ_TX_DONE + IRQ_RX_TX_TIMEOUT + while (!(readIrqStatus() & 0x4001)) + ; // wait for IRQs going active - setMode(MODE_STDBY_RC); //ensure we leave function with TX off + setMode(MODE_STDBY_RC); // ensure we leave function with TX off - if (readIrqStatus() & IRQ_RX_TX_TIMEOUT ) //check for timeout + if (readIrqStatus() & IRQ_RX_TX_TIMEOUT) // check for timeout { return 0; } @@ -4571,8 +4738,7 @@ uint8_t SX128XLT::transmitSXReliableIRQ(uint8_t startaddr, uint8_t length, uint1 return _TXPacketL; } - -uint8_t SX128XLT::receiveSXReliable(uint8_t startaddr, uint16_t networkID, uint32_t rxtimeout, uint8_t wait ) +uint8_t SX128XLT::receiveSXReliable(uint8_t startaddr, uint16_t networkID, uint32_t rxtimeout, uint8_t wait) { #ifdef SX128XDEBUGRELIABLE Serial.println(); @@ -4589,27 +4755,28 @@ uint8_t SX128XLT::receiveSXReliable(uint8_t startaddr, uint16_t networkID, uint3 setMode(MODE_STDBY_RC); checkBusy(); - setDioIrqParams(IRQ_RADIO_ALL, (IRQ_RX_DONE + IRQ_RX_TX_TIMEOUT + IRQ_HEADER_ERROR), 0, 0); //set for IRQ on RX done or timeout + setDioIrqParams(IRQ_RADIO_ALL, (IRQ_RX_DONE + IRQ_RX_TX_TIMEOUT + IRQ_HEADER_ERROR), 0, 0); // set for IRQ on RX done or timeout setRx(rxtimeout); if (!wait) { - return 0; //not wait requested so no packet length to pass + return 0; // not wait requested so no packet length to pass } - while (!digitalRead(_RXDonePin)); //Wait for DIO1 to go high, no timeout, RX DONE + while (!digitalRead(_RXDonePin)) + ; // Wait for DIO1 to go high, no timeout, RX DONE - setMode(MODE_STDBY_RC); //ensure to stop further packet reception + setMode(MODE_STDBY_RC); // ensure to stop further packet reception - if ( readIrqStatus() & (IRQ_HEADER_ERROR + IRQ_CRC_ERROR + IRQ_RX_TX_TIMEOUT + IRQ_SYNCWORD_ERROR)) + if (readIrqStatus() & (IRQ_HEADER_ERROR + IRQ_CRC_ERROR + IRQ_RX_TX_TIMEOUT + IRQ_SYNCWORD_ERROR)) { - return 0; //no RX done and header valid only, could be CRC error + return 0; // no RX done and header valid only, could be CRC error } readCommand(RADIO_GET_RXBUFFERSTATUS, buffer, 2); _RXPacketL = buffer[0]; - if (_RXPacketL < 4) //check received packet is 4 or more bytes long + if (_RXPacketL < 4) // check received packet is 4 or more bytes long { bitSet(_ReliableErrors, ReliableSizeError); return 0; @@ -4625,7 +4792,7 @@ uint8_t SX128XLT::receiveSXReliable(uint8_t startaddr, uint16_t networkID, uint3 if (!bitRead(_ReliableConfig, NoReliableCRC)) { payloadcrc = CRCCCITTReliable(startaddr, (startaddr + _RXPacketL - 5), 0xFFFF); - //payloadcrc = CRCCCITTReliable(startaddr, (startaddr + _RXPacketL - 5), 0xFFFF) + 1; + // payloadcrc = CRCCCITTReliable(startaddr, (startaddr + _RXPacketL - 5), 0xFFFF) + 1; RXcrc = readUint16SXBuffer(startaddr + _RXPacketL - 2); if (payloadcrc != RXcrc) @@ -4634,15 +4801,14 @@ uint8_t SX128XLT::receiveSXReliable(uint8_t startaddr, uint16_t networkID, uint3 } } - if (_ReliableErrors) //if there has been a reliable error return a RX fail + if (_ReliableErrors) // if there has been a reliable error return a RX fail { return 0; } - return _RXPacketL; //return and RX OK. + return _RXPacketL; // return and RX OK. } - -uint8_t SX128XLT::receiveSXReliableIRQ(uint8_t startaddr, uint16_t networkID, uint32_t rxtimeout, uint8_t wait ) +uint8_t SX128XLT::receiveSXReliableIRQ(uint8_t startaddr, uint16_t networkID, uint32_t rxtimeout, uint8_t wait) { #ifdef SX128XDEBUGRELIABLE Serial.println(); @@ -4663,23 +4829,24 @@ uint8_t SX128XLT::receiveSXReliableIRQ(uint8_t startaddr, uint16_t networkID, ui if (!wait) { - return 0; //not wait requested so no packet length to pass + return 0; // not wait requested so no packet length to pass } - //0x4022 = IRQ_RX_DONE + IRQ_RX_TX_TIMEOUT + IRQ_HEADER_ERROR - while (!(readIrqStatus() & 0x4022 )); //wait for IRQs going active + // 0x4022 = IRQ_RX_DONE + IRQ_RX_TX_TIMEOUT + IRQ_HEADER_ERROR + while (!(readIrqStatus() & 0x4022)) + ; // wait for IRQs going active - setMode(MODE_STDBY_RC); //ensure to stop further packet reception + setMode(MODE_STDBY_RC); // ensure to stop further packet reception - if ( readIrqStatus() & (IRQ_HEADER_ERROR + IRQ_CRC_ERROR + IRQ_RX_TX_TIMEOUT + IRQ_SYNCWORD_ERROR)) + if (readIrqStatus() & (IRQ_HEADER_ERROR + IRQ_CRC_ERROR + IRQ_RX_TX_TIMEOUT + IRQ_SYNCWORD_ERROR)) { - return 0; //no RX done and header valid only, could be CRC error + return 0; // no RX done and header valid only, could be CRC error } readCommand(RADIO_GET_RXBUFFERSTATUS, buffer, 2); _RXPacketL = buffer[0]; - if (_RXPacketL < 4) //check received packet is 4 or more bytes long + if (_RXPacketL < 4) // check received packet is 4 or more bytes long { bitSet(_ReliableErrors, ReliableSizeError); return 0; @@ -4695,7 +4862,7 @@ uint8_t SX128XLT::receiveSXReliableIRQ(uint8_t startaddr, uint16_t networkID, ui if (!bitRead(_ReliableConfig, NoReliableCRC)) { payloadcrc = CRCCCITTReliable(startaddr, (startaddr + _RXPacketL - 5), 0xFFFF); - //payloadcrc = CRCCCITTReliable(startaddr, (startaddr + _RXPacketL - 5), 0xFFFF) + 1; + // payloadcrc = CRCCCITTReliable(startaddr, (startaddr + _RXPacketL - 5), 0xFFFF) + 1; RXcrc = readUint16SXBuffer(startaddr + _RXPacketL - 2); if (payloadcrc != RXcrc) @@ -4704,15 +4871,14 @@ uint8_t SX128XLT::receiveSXReliableIRQ(uint8_t startaddr, uint16_t networkID, ui } } - if (_ReliableErrors) //if there has been a reliable error return a RX fail + if (_ReliableErrors) // if there has been a reliable error return a RX fail { return 0; } - return _RXPacketL; //return and RX OK. + return _RXPacketL; // return and RX OK. } - uint8_t SX128XLT::transmitSXReliableAutoACK(uint8_t startaddr, uint8_t length, uint16_t networkID, uint32_t acktimeout, uint32_t txtimeout, int8_t txpower, uint8_t wait) { #ifdef SX128XDEBUGRELIABLE @@ -4745,14 +4911,14 @@ uint8_t SX128XLT::transmitSXReliableAutoACK(uint8_t startaddr, uint8_t length, u else { payloadcrc = CRCCCITTReliable(startaddr, startaddr + length - 1, 0xFFFF); - //payloadcrc = CRCCCITTReliable(startaddr, startaddr + length - 1, 0xFFFF)+1; + // payloadcrc = CRCCCITTReliable(startaddr, startaddr + length - 1, 0xFFFF)+1; } writeUint16SXBuffer(startaddr + _TXPacketL - 4, networkID); writeUint16SXBuffer(startaddr + _TXPacketL - 2, payloadcrc); setPayloadLength(_TXPacketL); setTxParams(txpower, RAMP_TIME); - setDioIrqParams(IRQ_RADIO_ALL, (IRQ_TX_DONE + IRQ_RX_TX_TIMEOUT), 0, 0); //set for IRQ on TX done and timeout on DIO1 + setDioIrqParams(IRQ_RADIO_ALL, (IRQ_TX_DONE + IRQ_RX_TX_TIMEOUT), 0, 0); // set for IRQ on TX done and timeout on DIO1 setTx(txtimeout); if (!wait) @@ -4765,11 +4931,12 @@ uint8_t SX128XLT::transmitSXReliableAutoACK(uint8_t startaddr, uint8_t length, u return _TXPacketL; } - while (!digitalRead(_TXDonePin)); //Wait for DIO1 to go high + while (!digitalRead(_TXDonePin)) + ; // Wait for DIO1 to go high - setMode(MODE_STDBY_RC); //ensure we leave function with TX off + setMode(MODE_STDBY_RC); // ensure we leave function with TX off - if (readIrqStatus() & IRQ_RX_TX_TIMEOUT ) //check for timeout + if (readIrqStatus() & IRQ_RX_TX_TIMEOUT) // check for timeout { return 0; } @@ -4785,8 +4952,7 @@ uint8_t SX128XLT::transmitSXReliableAutoACK(uint8_t startaddr, uint8_t length, u return _TXPacketL; } - -uint8_t SX128XLT::receiveSXReliableAutoACK(uint8_t startaddr, uint16_t networkID, uint32_t ackdelay, int8_t txpower, uint32_t rxtimeout, uint8_t wait ) +uint8_t SX128XLT::receiveSXReliableAutoACK(uint8_t startaddr, uint16_t networkID, uint32_t ackdelay, int8_t txpower, uint32_t rxtimeout, uint8_t wait) { #ifdef SX128XDEBUGRELIABLE Serial.println(); @@ -4803,26 +4969,27 @@ uint8_t SX128XLT::receiveSXReliableAutoACK(uint8_t startaddr, uint16_t networkID _ReliableFlags = 0; setMode(MODE_STDBY_RC); - setDioIrqParams(IRQ_RADIO_ALL, (IRQ_RX_DONE + IRQ_RX_TX_TIMEOUT + IRQ_HEADER_ERROR), 0, 0); //set for IRQ on RX done or timeout + setDioIrqParams(IRQ_RADIO_ALL, (IRQ_RX_DONE + IRQ_RX_TX_TIMEOUT + IRQ_HEADER_ERROR), 0, 0); // set for IRQ on RX done or timeout setRx(rxtimeout); if (!wait) { - return 0; //not wait requested so no packet length to pass + return 0; // not wait requested so no packet length to pass } - while (!digitalRead(_RXDonePin)); //Wait for DIO1 to go high - setMode(MODE_STDBY_RC); //ensure to stop further packet reception + while (!digitalRead(_RXDonePin)) + ; // Wait for DIO1 to go high + setMode(MODE_STDBY_RC); // ensure to stop further packet reception - if ( readIrqStatus() & (IRQ_HEADER_ERROR + IRQ_CRC_ERROR + IRQ_RX_TX_TIMEOUT + IRQ_SYNCWORD_ERROR)) + if (readIrqStatus() & (IRQ_HEADER_ERROR + IRQ_CRC_ERROR + IRQ_RX_TX_TIMEOUT + IRQ_SYNCWORD_ERROR)) { - return 0; //packet is errored somewhere so return 0 + return 0; // packet is errored somewhere so return 0 } readCommand(RADIO_GET_RXBUFFERSTATUS, buffer, 2); _RXPacketL = buffer[0]; - if (_RXPacketL < 4) //check received packet is 4 or more bytes long + if (_RXPacketL < 4) // check received packet is 4 or more bytes long { bitSet(_ReliableErrors, ReliableSizeError); return 0; @@ -4838,7 +5005,7 @@ uint8_t SX128XLT::receiveSXReliableAutoACK(uint8_t startaddr, uint16_t networkID if (!bitRead(_ReliableConfig, NoReliableCRC)) { payloadcrc = CRCCCITTReliable(startaddr, (startaddr + _RXPacketL - 5), 0xFFFF); - //payloadcrc = CRCCCITTReliable(startaddr, (startaddr + _RXPacketL - 5), 0xFFFF)+1; + // payloadcrc = CRCCCITTReliable(startaddr, (startaddr + _RXPacketL - 5), 0xFFFF)+1; RXcrc = readUint16SXBuffer(startaddr + _RXPacketL - 2); if (payloadcrc != RXcrc) @@ -4847,17 +5014,17 @@ uint8_t SX128XLT::receiveSXReliableAutoACK(uint8_t startaddr, uint16_t networkID } } - if (_ReliableErrors) //if there has been a reliable error return a RX fail + if (_ReliableErrors) // if there has been a reliable error return a RX fail { return 0; } delay(ackdelay); - temp1 = readUint16SXBuffer(startaddr); //save bytes that would be overwritten by ack - temp2 = readUint16SXBuffer(startaddr + 2); //save bytes that would be overwritten by ack + temp1 = readUint16SXBuffer(startaddr); // save bytes that would be overwritten by ack + temp2 = readUint16SXBuffer(startaddr + 2); // save bytes that would be overwritten by ack _TXPacketL = sendReliableACK(RXnetworkID, payloadcrc, txpower); - writeUint16SXBuffer(startaddr, temp1); //restore bytes that would be overwritten by ack - writeUint16SXBuffer(startaddr + 2, temp2); //restore bytes that would be overwritten by ack + writeUint16SXBuffer(startaddr, temp1); // restore bytes that would be overwritten by ack + writeUint16SXBuffer(startaddr + 2, temp2); // restore bytes that would be overwritten by ack if (_TXPacketL != 4) { @@ -4865,10 +5032,9 @@ uint8_t SX128XLT::receiveSXReliableAutoACK(uint8_t startaddr, uint16_t networkID return 0; } - return _RXPacketL; //return indicating RX ack sent OK. + return _RXPacketL; // return indicating RX ack sent OK. } - uint8_t SX128XLT::waitSXReliableACK(uint8_t startaddr, uint16_t networkID, uint16_t payloadcrc, uint32_t acktimeout) { @@ -4886,9 +5052,9 @@ uint8_t SX128XLT::waitSXReliableACK(uint8_t startaddr, uint16_t networkID, uint1 do { - if (digitalRead(_RXDonePin)) //has a packet arrived ? + if (digitalRead(_RXDonePin)) // has a packet arrived ? { - if ( readIrqStatus() & (IRQ_HEADER_ERROR + IRQ_CRC_ERROR + IRQ_RX_TX_TIMEOUT + IRQ_SYNCWORD_ERROR)) + if (readIrqStatus() & (IRQ_HEADER_ERROR + IRQ_CRC_ERROR + IRQ_RX_TX_TIMEOUT + IRQ_SYNCWORD_ERROR)) { setReliableRX(0); continue; @@ -4899,10 +5065,10 @@ uint8_t SX128XLT::waitSXReliableACK(uint8_t startaddr, uint16_t networkID, uint1 RXnetworkID = readUint16SXBuffer(startaddr + _RXPacketL - 4); RXcrc = readUint16SXBuffer(startaddr + _RXPacketL - 2); - if ( (RXnetworkID == networkID) && (RXcrc == payloadcrc)) + if ((RXnetworkID == networkID) && (RXcrc == payloadcrc)) { bitSet(_ReliableFlags, ReliableACKReceived); - return _RXPacketL; //_RXPacketL should be payload length + 4 + return _RXPacketL; //_RXPacketL should be payload length + 4 } else { @@ -4910,13 +5076,12 @@ uint8_t SX128XLT::waitSXReliableACK(uint8_t startaddr, uint16_t networkID, uint1 continue; } } - } while ( ((uint32_t) (millis() - startmS) < acktimeout) ); + } while (((uint32_t)(millis() - startmS) < acktimeout)); bitSet(_ReliableErrors, ReliableACKError); return 0; } - uint8_t SX128XLT::waitSXReliableACKIRQ(uint8_t startaddr, uint16_t networkID, uint16_t payloadcrc, uint32_t acktimeout) { @@ -4934,10 +5099,10 @@ uint8_t SX128XLT::waitSXReliableACKIRQ(uint8_t startaddr, uint16_t networkID, ui do { - //0x4022 = IRQ_RX_DONE + IRQ_RX_TX_TIMEOUT + IRQ_HEADER_ERROR - if (readIrqStatus() & 0x4022 ) + // 0x4022 = IRQ_RX_DONE + IRQ_RX_TX_TIMEOUT + IRQ_HEADER_ERROR + if (readIrqStatus() & 0x4022) { - if ( readIrqStatus() & (IRQ_HEADER_ERROR + IRQ_CRC_ERROR + IRQ_RX_TX_TIMEOUT + IRQ_SYNCWORD_ERROR)) + if (readIrqStatus() & (IRQ_HEADER_ERROR + IRQ_CRC_ERROR + IRQ_RX_TX_TIMEOUT + IRQ_SYNCWORD_ERROR)) { setReliableRX(0); continue; @@ -4948,10 +5113,10 @@ uint8_t SX128XLT::waitSXReliableACKIRQ(uint8_t startaddr, uint16_t networkID, ui RXnetworkID = readUint16SXBuffer(startaddr + _RXPacketL - 4); RXcrc = readUint16SXBuffer(startaddr + _RXPacketL - 2); - if ( (RXnetworkID == networkID) && (RXcrc == payloadcrc)) + if ((RXnetworkID == networkID) && (RXcrc == payloadcrc)) { bitSet(_ReliableFlags, ReliableACKReceived); - return _RXPacketL; //_RXPacketL should be payload length + 4 + return _RXPacketL; //_RXPacketL should be payload length + 4 } else { @@ -4959,13 +5124,12 @@ uint8_t SX128XLT::waitSXReliableACKIRQ(uint8_t startaddr, uint16_t networkID, ui continue; } } - } while ( ((uint32_t) (millis() - startmS) < acktimeout) ); + } while (((uint32_t)(millis() - startmS) < acktimeout)); bitSet(_ReliableErrors, ReliableACKError); return 0; } - uint8_t SX128XLT::sendSXReliableACK(uint8_t startaddr, uint8_t length, uint16_t networkID, uint16_t payloadcrc, int8_t txpower) { @@ -4973,9 +5137,9 @@ uint8_t SX128XLT::sendSXReliableACK(uint8_t startaddr, uint8_t length, uint16_t Serial.print(F(" {RELIABLE} sendSXReliableACK() ")); #endif - uint32_t txtimeout = 12000; //set TX timeout to 12 seconds, longest packet is 8.7secs + uint32_t txtimeout = 12000; // set TX timeout to 12 seconds, longest packet is 8.7secs - _TXPacketL = length + 4; //packet is networkId (2 bytes) + payloadCRC (2 bytes) + _TXPacketL = length + 4; // packet is networkId (2 bytes) + payloadCRC (2 bytes) setMode(MODE_STDBY_RC); writeUint16SXBuffer((length + startaddr), networkID); @@ -4983,23 +5147,23 @@ uint8_t SX128XLT::sendSXReliableACK(uint8_t startaddr, uint8_t length, uint16_t checkBusy(); setPayloadLength(_TXPacketL); setTxParams(txpower, RAMP_TIME); - setDioIrqParams(IRQ_RADIO_ALL, (IRQ_TX_DONE + IRQ_RX_TX_TIMEOUT), 0, 0); //set for IRQ on TX done and timeout on DIO1 - setTx(txtimeout); //start transmission + setDioIrqParams(IRQ_RADIO_ALL, (IRQ_TX_DONE + IRQ_RX_TX_TIMEOUT), 0, 0); // set for IRQ on TX done and timeout on DIO1 + setTx(txtimeout); // start transmission - while (!digitalRead(_TXDonePin)); + while (!digitalRead(_TXDonePin)) + ; - setMode(MODE_STDBY_RC); //ensure we leave function with TX off + setMode(MODE_STDBY_RC); // ensure we leave function with TX off - if (readIrqStatus() & IRQ_RX_TX_TIMEOUT ) //check for timeout + if (readIrqStatus() & IRQ_RX_TX_TIMEOUT) // check for timeout { return 0; } bitSet(_ReliableFlags, ReliableACKSent); - return _TXPacketL; //TX OK so return TXpacket length + return _TXPacketL; // TX OK so return TXpacket length } - uint8_t SX128XLT::sendSXReliableACKIRQ(uint8_t startaddr, uint8_t length, uint16_t networkID, uint16_t payloadcrc, int8_t txpower) { @@ -5007,9 +5171,9 @@ uint8_t SX128XLT::sendSXReliableACKIRQ(uint8_t startaddr, uint8_t length, uint16 Serial.print(F(" {RELIABLE} sendSXReliableACK() ")); #endif - uint32_t txtimeout = 12000; //set TX timeout to 12 seconds, longest packet is 8.7secs + uint32_t txtimeout = 12000; // set TX timeout to 12 seconds, longest packet is 8.7secs - _TXPacketL = length + 4; //packet is networkId (2 bytes) + payloadCRC (2 bytes) + _TXPacketL = length + 4; // packet is networkId (2 bytes) + payloadCRC (2 bytes) setMode(MODE_STDBY_RC); writeUint16SXBuffer((length + startaddr), networkID); @@ -5018,25 +5182,26 @@ uint8_t SX128XLT::sendSXReliableACKIRQ(uint8_t startaddr, uint8_t length, uint16 checkBusy(); setPayloadLength(_TXPacketL); setTxParams(txpower, RAMP_TIME); - setTx(txtimeout); //start transmission + setTx(txtimeout); // start transmission - //0x4001 = IRQ_TX_DONE + IRQ_RX_TX_TIMEOUT - while (!(readIrqStatus() & 0x4001 )); //wait for IRQs going active + // 0x4001 = IRQ_TX_DONE + IRQ_RX_TX_TIMEOUT + while (!(readIrqStatus() & 0x4001)) + ; // wait for IRQs going active - setMode(MODE_STDBY_RC); //ensure we leave function with TX off + setMode(MODE_STDBY_RC); // ensure we leave function with TX off - if (readIrqStatus() & IRQ_RX_TX_TIMEOUT ) //check for timeout + if (readIrqStatus() & IRQ_RX_TX_TIMEOUT) // check for timeout { return 0; } bitSet(_ReliableFlags, ReliableACKSent); - return _TXPacketL; //TX OK so return TXpacket length + return _TXPacketL; // TX OK so return TXpacket length } //*********************************************************************************** -//Data Transfer functions - Added November 2021 -//TX and RX base addresses assumed to be 0 +// Data Transfer functions - Added November 2021 +// TX and RX base addresses assumed to be 0 //*********************************************************************************** uint8_t SX128XLT::transmitDT(uint8_t *header, uint8_t headersize, uint8_t *dataarray, uint8_t datasize, uint16_t networkID, uint32_t txtimeout, int8_t txpower, uint8_t wait) @@ -5052,7 +5217,7 @@ uint8_t SX128XLT::transmitDT(uint8_t *header, uint8_t headersize, uint8_t *dataa _ReliableFlags = 0; #ifdef DETECTRELIABLERRORS - if (datasize > (251 - headersize)) //its 251 because of 4 bytes appended to packet + if (datasize > (251 - headersize)) // its 251 because of 4 bytes appended to packet { bitSet(_ReliableErrors, ReliableSizeError); return 0; @@ -5080,21 +5245,21 @@ uint8_t SX128XLT::transmitDT(uint8_t *header, uint8_t headersize, uint8_t *dataa SPI.transfer(RADIO_WRITE_BUFFER); SPI.transfer(0); - //load up the header + // load up the header for (index = 0; index < headersize; index++) { bufferdata = header[index]; SPI.transfer(bufferdata); } - //load up the data array + // load up the data array for (index = 0; index < datasize; index++) { bufferdata = dataarray[index]; SPI.transfer(bufferdata); } - //append the network ID and payload CRC at end + // append the network ID and payload CRC at end SPI.transfer(lowByte(networkID)); SPI.transfer(highByte(networkID)); SPI.transfer(lowByte(payloadcrc)); @@ -5108,7 +5273,7 @@ uint8_t SX128XLT::transmitDT(uint8_t *header, uint8_t headersize, uint8_t *dataa setPayloadLength(_TXPacketL); setTxParams(txpower, RAMP_TIME); - setDioIrqParams(IRQ_RADIO_ALL, (IRQ_TX_DONE + IRQ_RX_TX_TIMEOUT), 0, 0); //set for IRQ on TX done and timeout on DIO1 + setDioIrqParams(IRQ_RADIO_ALL, (IRQ_TX_DONE + IRQ_RX_TX_TIMEOUT), 0, 0); // set for IRQ on TX done and timeout on DIO1 setTx(txtimeout); if (!wait) @@ -5116,11 +5281,12 @@ uint8_t SX128XLT::transmitDT(uint8_t *header, uint8_t headersize, uint8_t *dataa return _TXPacketL; } - while (!digitalRead(_TXDonePin)); //Wait for DIO1 to go high + while (!digitalRead(_TXDonePin)) + ; // Wait for DIO1 to go high - setMode(MODE_STDBY_RC); //ensure we leave function with TX off + setMode(MODE_STDBY_RC); // ensure we leave function with TX off - if (readIrqStatus() & IRQ_RX_TX_TIMEOUT ) //check for timeout + if (readIrqStatus() & IRQ_RX_TX_TIMEOUT) // check for timeout { return 0; } @@ -5129,7 +5295,7 @@ uint8_t SX128XLT::transmitDT(uint8_t *header, uint8_t headersize, uint8_t *dataa return _TXPacketL; } - if (readIrqStatus() & IRQ_RX_TX_TIMEOUT ) //check for timeout + if (readIrqStatus() & IRQ_RX_TX_TIMEOUT) // check for timeout { return 0; } @@ -5139,7 +5305,6 @@ uint8_t SX128XLT::transmitDT(uint8_t *header, uint8_t headersize, uint8_t *dataa } } - uint8_t SX128XLT::waitACKDT(uint8_t *header, uint8_t headersize, uint32_t acktimeout) { @@ -5157,19 +5322,19 @@ uint8_t SX128XLT::waitACKDT(uint8_t *header, uint8_t headersize, uint32_t acktim _ReliableErrors = 0; _ReliableFlags = 0; - networkID = readUint16SXBuffer(_TXPacketL - 4); //get networkID used to transmit previous packet, before next RX - payloadCRC = readUint16SXBuffer(_TXPacketL - 2); //get payloadCRC used to transmit previous packet, before next RX + networkID = readUint16SXBuffer(_TXPacketL - 4); // get networkID used to transmit previous packet, before next RX + payloadCRC = readUint16SXBuffer(_TXPacketL - 2); // get payloadCRC used to transmit previous packet, before next RX setReliableRX(0); - startmS = millis(); //setReliableRX has a timeount, but here we want an overall timeout waiting for ACK + startmS = millis(); // setReliableRX has a timeount, but here we want an overall timeout waiting for ACK do { - if (digitalRead(_RXDonePin)) //has a packet arrived ? + if (digitalRead(_RXDonePin)) // has a packet arrived ? { - setMode(MODE_STDBY_RC); //ensure to stop further packet reception + setMode(MODE_STDBY_RC); // ensure to stop further packet reception - //IRQ_HEADER_ERROR + IRQ_CRC_ERROR + IRQ_RX_TX_TIMEOUT + IRQ_SYNCWORD_ERROR = 0x4068 + // IRQ_HEADER_ERROR + IRQ_CRC_ERROR + IRQ_RX_TX_TIMEOUT + IRQ_SYNCWORD_ERROR = 0x4068 if (readIrqStatus() & 0x4068) { setReliableRX(0); @@ -5180,7 +5345,7 @@ uint8_t SX128XLT::waitACKDT(uint8_t *header, uint8_t headersize, uint32_t acktim RXnetworkID = readUint16SXBuffer(_RXPacketL - 4); RXcrc = readUint16SXBuffer(_RXPacketL - 2); - if ((_RXPacketL - 4) > headersize ) //check passed buffer is big enough for header + if ((_RXPacketL - 4) > headersize) // check passed buffer is big enough for header { setReliableRX(0); continue; @@ -5196,7 +5361,7 @@ uint8_t SX128XLT::waitACKDT(uint8_t *header, uint8_t headersize, uint32_t acktim } } - if ( (RXnetworkID == networkID)) + if ((RXnetworkID == networkID)) { bitSet(_ReliableFlags, ReliableACKReceived); @@ -5206,12 +5371,12 @@ uint8_t SX128XLT::waitACKDT(uint8_t *header, uint8_t headersize, uint32_t acktim SPI.beginTransaction(SPISettings(LTspeedMaximum, LTdataOrder, LTdataMode)); #endif - digitalWrite(_NSS, LOW); //start the burst read + digitalWrite(_NSS, LOW); // start the burst read SPI.transfer(RADIO_READ_BUFFER); SPI.transfer(0); SPI.transfer(0xFF); - for (index = 0; index < (_RXPacketL - 4); index++) //read packet into rxbuffer + for (index = 0; index < (_RXPacketL - 4); index++) // read packet into rxbuffer { regdata = SPI.transfer(0); header[index] = regdata; @@ -5222,7 +5387,7 @@ uint8_t SX128XLT::waitACKDT(uint8_t *header, uint8_t headersize, uint32_t acktim SPI.endTransaction(); #endif - return _RXPacketL; //_RXPacketL should be payload length + 4 + return _RXPacketL; //_RXPacketL should be payload length + 4 } else { @@ -5230,7 +5395,7 @@ uint8_t SX128XLT::waitACKDT(uint8_t *header, uint8_t headersize, uint32_t acktim continue; } } - } while ( ((uint32_t) (millis() - startmS) < acktimeout)); + } while (((uint32_t)(millis() - startmS) < acktimeout)); bitSet(_ReliableErrors, ReliableACKError); bitSet(_ReliableErrors, ReliableTimeout); @@ -5238,8 +5403,7 @@ uint8_t SX128XLT::waitACKDT(uint8_t *header, uint8_t headersize, uint32_t acktim return 0; } - -uint8_t SX128XLT::receiveDT(uint8_t *header, uint8_t headersize, uint8_t *dataarray, uint8_t datasize, uint16_t networkID, uint32_t rxtimeout, uint8_t wait ) +uint8_t SX128XLT::receiveDT(uint8_t *header, uint8_t headersize, uint8_t *dataarray, uint8_t datasize, uint16_t networkID, uint32_t rxtimeout, uint8_t wait) { #ifdef SX128XDEBUGRELIABLE Serial.println(); @@ -5258,26 +5422,27 @@ uint8_t SX128XLT::receiveDT(uint8_t *header, uint8_t headersize, uint8_t *dataar _ReliableErrors = 0; _ReliableFlags = 0; setMode(MODE_STDBY_RC); - setPayloadLength(127); //set for maximum packet length in FLRC mode, packets might be filtered otherwise + setPayloadLength(127); // set for maximum packet length in FLRC mode, packets might be filtered otherwise setDioIrqParams(IRQ_RADIO_ALL, (IRQ_RX_DONE + IRQ_RX_TX_TIMEOUT + IRQ_HEADER_ERROR), 0, 0); setRx(rxtimeout); if (!wait) { - return 0; //not wait requested so no packet length to pass + return 0; // not wait requested so no packet length to pass } - while (!digitalRead(_RXDonePin)); //Wait for DIO1 to go high, no timeout, RX DONE + while (!digitalRead(_RXDonePin)) + ; // Wait for DIO1 to go high, no timeout, RX DONE - setMode(MODE_STDBY_RC); //ensure to stop further packet reception + setMode(MODE_STDBY_RC); // ensure to stop further packet reception - //IRQ_HEADER_ERROR + IRQ_CRC_ERROR + IRQ_RX_TX_TIMEOUT + IRQ_SYNCWORD_ERROR = 0x4068 + // IRQ_HEADER_ERROR + IRQ_CRC_ERROR + IRQ_RX_TX_TIMEOUT + IRQ_SYNCWORD_ERROR = 0x4068 if (readIrqStatus() & 0x4068) { #ifdef SX128XDEBUGRELIABLE Serial.println(F(" {RELIABLE} Packet error")); #endif - return 0; //packet is errored somewhere so return 0 + return 0; // packet is errored somewhere so return 0 } RXHeaderL = getByteSXBuffer(2); @@ -5298,7 +5463,7 @@ uint8_t SX128XLT::receiveDT(uint8_t *header, uint8_t headersize, uint8_t *dataar Serial.println(); #endif - if (RXHeaderL > headersize ) + if (RXHeaderL > headersize) { #ifdef SX128XDEBUGRELIABLE Serial.print(F(" {RELIABLE} Header size error ")); @@ -5308,7 +5473,7 @@ uint8_t SX128XLT::receiveDT(uint8_t *header, uint8_t headersize, uint8_t *dataar return 0; } - if (RXDataL > datasize ) + if (RXDataL > datasize) { #ifdef SX128XDEBUGRELIABLE Serial.print(F(" {RELIABLE} Data size error ")); @@ -5331,9 +5496,9 @@ uint8_t SX128XLT::receiveDT(uint8_t *header, uint8_t headersize, uint8_t *dataar Serial.println(); #endif - if (_RXPacketL < 10) //check received packet is 10 or more bytes long + if (_RXPacketL < 10) // check received packet is 10 or more bytes long { - //Serial.println(F(" {RELIABLE} _RXPacketL < 10")); + // Serial.println(F(" {RELIABLE} _RXPacketL < 10")); bitSet(_ReliableErrors, ReliableSizeError); return 0; } @@ -5344,7 +5509,7 @@ uint8_t SX128XLT::receiveDT(uint8_t *header, uint8_t headersize, uint8_t *dataar SPI.beginTransaction(SPISettings(LTspeedMaximum, LTdataOrder, LTdataMode)); #endif - digitalWrite(_NSS, LOW); //start the burst read + digitalWrite(_NSS, LOW); // start the burst read SPI.transfer(RADIO_READ_BUFFER); SPI.transfer(RXstart); SPI.transfer(0xFF); @@ -5363,7 +5528,7 @@ uint8_t SX128XLT::receiveDT(uint8_t *header, uint8_t headersize, uint8_t *dataar regdataL = SPI.transfer(0); regdataH = SPI.transfer(0); - RXnetworkID = ((uint16_t) regdataH << 8) + regdataL; + RXnetworkID = ((uint16_t)regdataH << 8) + regdataL; regdataL = SPI.transfer(0); regdataH = SPI.transfer(0); @@ -5376,7 +5541,7 @@ uint8_t SX128XLT::receiveDT(uint8_t *header, uint8_t headersize, uint8_t *dataar if (!bitRead(_ReliableConfig, NoReliableCRC)) { payloadcrc = CRCCCITT(dataarray, RXDataL, 0xFFFF); - RXcrc = ((uint16_t) regdataH << 8) + regdataL; + RXcrc = ((uint16_t)regdataH << 8) + regdataL; #ifdef SX128XDEBUGRELIABLE Serial.println(F(" {RELIABLE} Payload CRC check enabled")); @@ -5412,7 +5577,7 @@ uint8_t SX128XLT::receiveDT(uint8_t *header, uint8_t headersize, uint8_t *dataar bitSet(_ReliableErrors, ReliableIDError); } - if (_ReliableErrors) //if there has been a reliable error return a RX fail + if (_ReliableErrors) // if there has been a reliable error return a RX fail { #ifdef SX128XDEBUGRELIABLE Serial.print(F(" {RELIABLE} Reliable errors")); @@ -5421,10 +5586,9 @@ uint8_t SX128XLT::receiveDT(uint8_t *header, uint8_t headersize, uint8_t *dataar return 0; } - return _RXPacketL; //return and indicate RX OK. + return _RXPacketL; // return and indicate RX OK. } - uint8_t SX128XLT::sendACKDT(uint8_t *header, uint8_t headersize, int8_t txpower) { @@ -5432,7 +5596,7 @@ uint8_t SX128XLT::sendACKDT(uint8_t *header, uint8_t headersize, int8_t txpower) Serial.print(F(" {RELIABLE} sendACKDT() ")); #endif - uint32_t txtimeout = 12000; //set TX timeout to 12 seconds, longest packet is 8.7secs + uint32_t txtimeout = 12000; // set TX timeout to 12 seconds, longest packet is 8.7secs uint8_t bufferdata, index; uint16_t networkID; uint16_t payloadCRC; @@ -5470,29 +5634,27 @@ uint8_t SX128XLT::sendACKDT(uint8_t *header, uint8_t headersize, int8_t txpower) setPayloadLength(_TXPacketL); setTxParams(txpower, RAMP_TIME); - setDioIrqParams(IRQ_RADIO_ALL, (IRQ_TX_DONE + IRQ_RX_TX_TIMEOUT), 0, 0); //set for IRQ on TX done and timeout on DIO1 + setDioIrqParams(IRQ_RADIO_ALL, (IRQ_TX_DONE + IRQ_RX_TX_TIMEOUT), 0, 0); // set for IRQ on TX done and timeout on DIO1 setTx(txtimeout); - while (!digitalRead(_TXDonePin)); //Wait for DIO1 to go high + while (!digitalRead(_TXDonePin)) + ; // Wait for DIO1 to go high - if (readIrqStatus() & IRQ_RX_TX_TIMEOUT ) //check for timeout + if (readIrqStatus() & IRQ_RX_TX_TIMEOUT) // check for timeout { bitSet(_ReliableFlags, ReliableTimeout); return 0; } bitSet(_ReliableFlags, ReliableACKSent); - return _TXPacketL; //TX OK so return TXpacket length + return _TXPacketL; // TX OK so return TXpacket length } - //*********************************************************************************** -//IRQ Data Transfer functions - Added November 2021 -//TX and RX base addresses assumed to be 0 +// IRQ Data Transfer functions - Added November 2021 +// TX and RX base addresses assumed to be 0 //*********************************************************************************** - - uint8_t SX128XLT::transmitDTIRQ(uint8_t *header, uint8_t headersize, uint8_t *dataarray, uint8_t datasize, uint16_t networkID, uint32_t txtimeout, int8_t txpower, uint8_t wait) { #ifdef SX128XDEBUGRELIABLE @@ -5506,7 +5668,7 @@ uint8_t SX128XLT::transmitDTIRQ(uint8_t *header, uint8_t headersize, uint8_t *da _ReliableFlags = 0; #ifdef DETECTRELIABLERRORS - if (datasize > (251 - headersize)) //its 251 because of 4 bytes appended to packet + if (datasize > (251 - headersize)) // its 251 because of 4 bytes appended to packet { bitSet(_ReliableErrors, ReliableSizeError); return 0; @@ -5534,21 +5696,21 @@ uint8_t SX128XLT::transmitDTIRQ(uint8_t *header, uint8_t headersize, uint8_t *da SPI.transfer(RADIO_WRITE_BUFFER); SPI.transfer(0); - //load up the header + // load up the header for (index = 0; index < headersize; index++) { bufferdata = header[index]; SPI.transfer(bufferdata); } - //load up the data array + // load up the data array for (index = 0; index < datasize; index++) { bufferdata = dataarray[index]; SPI.transfer(bufferdata); } - //append the network ID and payload CRC at end + // append the network ID and payload CRC at end SPI.transfer(lowByte(networkID)); SPI.transfer(highByte(networkID)); SPI.transfer(lowByte(payloadcrc)); @@ -5562,7 +5724,7 @@ uint8_t SX128XLT::transmitDTIRQ(uint8_t *header, uint8_t headersize, uint8_t *da setPayloadLength(_TXPacketL); setTxParams(txpower, RAMP_TIME); - setDioIrqParams(IRQ_RADIO_ALL, (IRQ_TX_DONE + IRQ_RX_TX_TIMEOUT), 0, 0); //set for IRQ on TX done and timeout on DIO1 + setDioIrqParams(IRQ_RADIO_ALL, (IRQ_TX_DONE + IRQ_RX_TX_TIMEOUT), 0, 0); // set for IRQ on TX done and timeout on DIO1 setTx(txtimeout); if (!wait) @@ -5570,15 +5732,15 @@ uint8_t SX128XLT::transmitDTIRQ(uint8_t *header, uint8_t headersize, uint8_t *da return _TXPacketL; } - //while (!digitalRead(_TXDonePin)); //Wait for DIO1 to go high + // while (!digitalRead(_TXDonePin)); //Wait for DIO1 to go high - //0x4001 = IRQ_TX_DONE + IRQ_RX_TX_TIMEOUT - while (!(readIrqStatus() & 0x4001 )); //wait for IRQs going active + // 0x4001 = IRQ_TX_DONE + IRQ_RX_TX_TIMEOUT + while (!(readIrqStatus() & 0x4001)) + ; // wait for IRQs going active + setMode(MODE_STDBY_RC); // ensure we leave function with TX off - setMode(MODE_STDBY_RC); //ensure we leave function with TX off - - if (readIrqStatus() & IRQ_RX_TX_TIMEOUT ) //check for timeout + if (readIrqStatus() & IRQ_RX_TX_TIMEOUT) // check for timeout { return 0; } @@ -5587,7 +5749,7 @@ uint8_t SX128XLT::transmitDTIRQ(uint8_t *header, uint8_t headersize, uint8_t *da return _TXPacketL; } - if (readIrqStatus() & IRQ_RX_TX_TIMEOUT ) //check for timeout + if (readIrqStatus() & IRQ_RX_TX_TIMEOUT) // check for timeout { return 0; } @@ -5597,7 +5759,6 @@ uint8_t SX128XLT::transmitDTIRQ(uint8_t *header, uint8_t headersize, uint8_t *da } } - uint8_t SX128XLT::waitACKDTIRQ(uint8_t *header, uint8_t headersize, uint32_t acktimeout) { @@ -5615,20 +5776,20 @@ uint8_t SX128XLT::waitACKDTIRQ(uint8_t *header, uint8_t headersize, uint32_t ack _ReliableErrors = 0; _ReliableFlags = 0; - networkID = readUint16SXBuffer(_TXPacketL - 4); //get networkID used to transmit previous packet, before next RX - payloadCRC = readUint16SXBuffer(_TXPacketL - 2); //get payloadCRC used to transmit previous packet, before next RX + networkID = readUint16SXBuffer(_TXPacketL - 4); // get networkID used to transmit previous packet, before next RX + payloadCRC = readUint16SXBuffer(_TXPacketL - 2); // get payloadCRC used to transmit previous packet, before next RX setReliableRX(0); - startmS = millis(); //setReliableRX has a timeount, but here we want an overall timeout waiting for ACK + startmS = millis(); // setReliableRX has a timeount, but here we want an overall timeout waiting for ACK do { - //0x4022 = IRQ_RX_DONE + IRQ_RX_TX_TIMEOUT + IRQ_HEADER_ERROR - if (readIrqStatus() & 0x4022 ) + // 0x4022 = IRQ_RX_DONE + IRQ_RX_TX_TIMEOUT + IRQ_HEADER_ERROR + if (readIrqStatus() & 0x4022) { - setMode(MODE_STDBY_RC); //ensure to stop further packet reception + setMode(MODE_STDBY_RC); // ensure to stop further packet reception - //IRQ_HEADER_ERROR + IRQ_CRC_ERROR + IRQ_RX_TX_TIMEOUT + IRQ_SYNCWORD_ERROR = 0x4068 + // IRQ_HEADER_ERROR + IRQ_CRC_ERROR + IRQ_RX_TX_TIMEOUT + IRQ_SYNCWORD_ERROR = 0x4068 if (readIrqStatus() & 0x4068) { setReliableRX(0); @@ -5639,7 +5800,7 @@ uint8_t SX128XLT::waitACKDTIRQ(uint8_t *header, uint8_t headersize, uint32_t ack RXnetworkID = readUint16SXBuffer(_RXPacketL - 4); RXcrc = readUint16SXBuffer(_RXPacketL - 2); - if ((_RXPacketL - 4) > headersize ) //check passed buffer is big enough for header + if ((_RXPacketL - 4) > headersize) // check passed buffer is big enough for header { setReliableRX(0); continue; @@ -5655,7 +5816,7 @@ uint8_t SX128XLT::waitACKDTIRQ(uint8_t *header, uint8_t headersize, uint32_t ack } } - if ( (RXnetworkID == networkID)) + if ((RXnetworkID == networkID)) { bitSet(_ReliableFlags, ReliableACKReceived); @@ -5665,12 +5826,12 @@ uint8_t SX128XLT::waitACKDTIRQ(uint8_t *header, uint8_t headersize, uint32_t ack SPI.beginTransaction(SPISettings(LTspeedMaximum, LTdataOrder, LTdataMode)); #endif - digitalWrite(_NSS, LOW); //start the burst read + digitalWrite(_NSS, LOW); // start the burst read SPI.transfer(RADIO_READ_BUFFER); SPI.transfer(0); SPI.transfer(0xFF); - for (index = 0; index < (_RXPacketL - 4); index++) //read packet into rxbuffer + for (index = 0; index < (_RXPacketL - 4); index++) // read packet into rxbuffer { regdata = SPI.transfer(0); header[index] = regdata; @@ -5681,7 +5842,7 @@ uint8_t SX128XLT::waitACKDTIRQ(uint8_t *header, uint8_t headersize, uint32_t ack SPI.endTransaction(); #endif - return _RXPacketL; //_RXPacketL should be payload length + 4 + return _RXPacketL; //_RXPacketL should be payload length + 4 } else { @@ -5689,15 +5850,14 @@ uint8_t SX128XLT::waitACKDTIRQ(uint8_t *header, uint8_t headersize, uint32_t ack continue; } } - } while ( ((uint32_t) (millis() - startmS) < acktimeout)); + } while (((uint32_t)(millis() - startmS) < acktimeout)); bitSet(_ReliableErrors, ReliableACKError); bitSet(_ReliableErrors, ReliableTimeout); return 0; } - -uint8_t SX128XLT::receiveDTIRQ(uint8_t *header, uint8_t headersize, uint8_t *dataarray, uint8_t datasize, uint16_t networkID, uint32_t rxtimeout, uint8_t wait ) +uint8_t SX128XLT::receiveDTIRQ(uint8_t *header, uint8_t headersize, uint8_t *dataarray, uint8_t datasize, uint16_t networkID, uint32_t rxtimeout, uint8_t wait) { #ifdef SX128XDEBUGRELIABLE Serial.println(); @@ -5716,27 +5876,28 @@ uint8_t SX128XLT::receiveDTIRQ(uint8_t *header, uint8_t headersize, uint8_t *dat _ReliableErrors = 0; _ReliableFlags = 0; setMode(MODE_STDBY_RC); - setPayloadLength(127); //set for maximum packet length in FLRC mode, packets might be filtered otherwise + setPayloadLength(127); // set for maximum packet length in FLRC mode, packets might be filtered otherwise setDioIrqParams(IRQ_RADIO_ALL, (IRQ_RX_DONE + IRQ_RX_TX_TIMEOUT + IRQ_HEADER_ERROR), 0, 0); setRx(rxtimeout); if (!wait) { - return 0; //not wait requested so no packet length to pass + return 0; // not wait requested so no packet length to pass } - //0x4022 = IRQ_RX_DONE + IRQ_RX_TX_TIMEOUT + IRQ_HEADER_ERROR - while (!(readIrqStatus() & 0x4022 )); //wait for IRQs going active + // 0x4022 = IRQ_RX_DONE + IRQ_RX_TX_TIMEOUT + IRQ_HEADER_ERROR + while (!(readIrqStatus() & 0x4022)) + ; // wait for IRQs going active - setMode(MODE_STDBY_RC); //ensure to stop further packet reception + setMode(MODE_STDBY_RC); // ensure to stop further packet reception - //IRQ_HEADER_ERROR + IRQ_CRC_ERROR + IRQ_RX_TX_TIMEOUT + IRQ_SYNCWORD_ERROR = 0x4068 + // IRQ_HEADER_ERROR + IRQ_CRC_ERROR + IRQ_RX_TX_TIMEOUT + IRQ_SYNCWORD_ERROR = 0x4068 if (readIrqStatus() & 0x4068) { #ifdef SX128XDEBUGRELIABLE Serial.println(F(" {RELIABLE} Packet error")); #endif - return 0; //packet is errored somewhere so return 0 + return 0; // packet is errored somewhere so return 0 } RXHeaderL = getByteSXBuffer(2); @@ -5757,7 +5918,7 @@ uint8_t SX128XLT::receiveDTIRQ(uint8_t *header, uint8_t headersize, uint8_t *dat Serial.println(); #endif - if (RXHeaderL > headersize ) + if (RXHeaderL > headersize) { #ifdef SX128XDEBUGRELIABLE Serial.print(F(" {RELIABLE} Header size error ")); @@ -5767,7 +5928,7 @@ uint8_t SX128XLT::receiveDTIRQ(uint8_t *header, uint8_t headersize, uint8_t *dat return 0; } - if (RXDataL > datasize ) + if (RXDataL > datasize) { #ifdef SX128XDEBUGRELIABLE Serial.print(F(" {RELIABLE} Data size error ")); @@ -5790,9 +5951,9 @@ uint8_t SX128XLT::receiveDTIRQ(uint8_t *header, uint8_t headersize, uint8_t *dat Serial.println(); #endif - if (_RXPacketL < 10) //check received packet is 10 or more bytes long + if (_RXPacketL < 10) // check received packet is 10 or more bytes long { - //Serial.println(F(" {RELIABLE} _RXPacketL < 10")); + // Serial.println(F(" {RELIABLE} _RXPacketL < 10")); bitSet(_ReliableErrors, ReliableSizeError); return 0; } @@ -5803,7 +5964,7 @@ uint8_t SX128XLT::receiveDTIRQ(uint8_t *header, uint8_t headersize, uint8_t *dat SPI.beginTransaction(SPISettings(LTspeedMaximum, LTdataOrder, LTdataMode)); #endif - digitalWrite(_NSS, LOW); //start the burst read + digitalWrite(_NSS, LOW); // start the burst read SPI.transfer(RADIO_READ_BUFFER); SPI.transfer(RXstart); SPI.transfer(0xFF); @@ -5822,7 +5983,7 @@ uint8_t SX128XLT::receiveDTIRQ(uint8_t *header, uint8_t headersize, uint8_t *dat regdataL = SPI.transfer(0); regdataH = SPI.transfer(0); - RXnetworkID = ((uint16_t) regdataH << 8) + regdataL; + RXnetworkID = ((uint16_t)regdataH << 8) + regdataL; regdataL = SPI.transfer(0); regdataH = SPI.transfer(0); @@ -5835,7 +5996,7 @@ uint8_t SX128XLT::receiveDTIRQ(uint8_t *header, uint8_t headersize, uint8_t *dat if (!bitRead(_ReliableConfig, NoReliableCRC)) { payloadcrc = CRCCCITT(dataarray, RXDataL, 0xFFFF); - RXcrc = ((uint16_t) regdataH << 8) + regdataL; + RXcrc = ((uint16_t)regdataH << 8) + regdataL; #ifdef SX128XDEBUGRELIABLE Serial.println(F(" {RELIABLE} Payload CRC check enabled")); @@ -5871,7 +6032,7 @@ uint8_t SX128XLT::receiveDTIRQ(uint8_t *header, uint8_t headersize, uint8_t *dat bitSet(_ReliableErrors, ReliableIDError); } - if (_ReliableErrors) //if there has been a reliable error return a RX fail + if (_ReliableErrors) // if there has been a reliable error return a RX fail { #ifdef SX128XDEBUGRELIABLE Serial.print(F(" {RELIABLE} Reliable errors")); @@ -5880,10 +6041,9 @@ uint8_t SX128XLT::receiveDTIRQ(uint8_t *header, uint8_t headersize, uint8_t *dat return 0; } - return _RXPacketL; //return and indicate RX OK. + return _RXPacketL; // return and indicate RX OK. } - uint8_t SX128XLT::sendACKDTIRQ(uint8_t *header, uint8_t headersize, int8_t txpower) { @@ -5891,7 +6051,7 @@ uint8_t SX128XLT::sendACKDTIRQ(uint8_t *header, uint8_t headersize, int8_t txpow Serial.print(F(" {RELIABLE} sendACKDT() ")); #endif - uint32_t txtimeout = 12000; //set TX timeout to 12 seconds, longest packet is 8.7secs + uint32_t txtimeout = 12000; // set TX timeout to 12 seconds, longest packet is 8.7secs uint8_t bufferdata, index; uint16_t networkID; uint16_t payloadCRC; @@ -5929,27 +6089,23 @@ uint8_t SX128XLT::sendACKDTIRQ(uint8_t *header, uint8_t headersize, int8_t txpow setPayloadLength(_TXPacketL); setTxParams(txpower, RAMP_TIME); - setDioIrqParams(IRQ_RADIO_ALL, (IRQ_TX_DONE + IRQ_RX_TX_TIMEOUT), 0, 0); //set for IRQ on TX done and timeout on DIO1 + setDioIrqParams(IRQ_RADIO_ALL, (IRQ_TX_DONE + IRQ_RX_TX_TIMEOUT), 0, 0); // set for IRQ on TX done and timeout on DIO1 setTx(txtimeout); - //0x4001 = IRQ_TX_DONE + IRQ_RX_TX_TIMEOUT - while (!(readIrqStatus() & 0x4001 )); //wait for IRQs going active + // 0x4001 = IRQ_TX_DONE + IRQ_RX_TX_TIMEOUT + while (!(readIrqStatus() & 0x4001)) + ; // wait for IRQs going active - - - if (readIrqStatus() & IRQ_RX_TX_TIMEOUT ) //check for timeout + if (readIrqStatus() & IRQ_RX_TX_TIMEOUT) // check for timeout { bitSet(_ReliableFlags, ReliableTimeout); return 0; } bitSet(_ReliableFlags, ReliableACKSent); - return _TXPacketL; //TX OK so return TXpacket length + return _TXPacketL; // TX OK so return TXpacket length } - - - /* MIT license diff --git a/src/SX128XLT.h b/src/SX128XLT.h index 9ddb694..e744a5e 100644 --- a/src/SX128XLT.h +++ b/src/SX128XLT.h @@ -1,259 +1,257 @@ #ifndef SX128XLT_h #define SX128XLT_h -#include "Arduino.h" +#include #include +class SX128XLT +{ -class SX128XLT { +public: + SX128XLT(); - public: + bool begin(int8_t pinNSS, int8_t pinNRESET, int8_t pinRFBUSY, int8_t pinDIO1, int8_t pinDIO2, int8_t pinDIO3, int8_t pinRXEN, int8_t pinTXEN, uint8_t device); + bool begin(int8_t pinNSS, int8_t pinNRESET, int8_t pinRFBUSY, int8_t pinDIO1, uint8_t device); + bool begin(int8_t pinNSS, int8_t pinNRESET, int8_t pinRFBUSY, int8_t pinDIO1, int8_t pinRXEN, int8_t pinTXEN, uint8_t device); + bool begin(int8_t pinNSS, int8_t pinNRESET, int8_t pinRFBUSY, uint8_t device); + bool begin(int8_t pinNSS, int8_t pinRFBUSY, uint8_t device); - SX128XLT(); + void rxEnable(); + void txEnable(); - bool begin(int8_t pinNSS, int8_t pinNRESET, int8_t pinRFBUSY, int8_t pinDIO1, int8_t pinDIO2, int8_t pinDIO3, int8_t pinRXEN, int8_t pinTXEN, uint8_t device); - bool begin(int8_t pinNSS, int8_t pinNRESET, int8_t pinRFBUSY, int8_t pinDIO1, uint8_t device); - bool begin(int8_t pinNSS, int8_t pinNRESET, int8_t pinRFBUSY, int8_t pinDIO1, int8_t pinRXEN, int8_t pinTXEN, uint8_t device); - bool begin(int8_t pinNSS, int8_t pinNRESET, int8_t pinRFBUSY, uint8_t device); - bool begin(int8_t pinNSS, int8_t pinRFBUSY, uint8_t device); + void checkBusy(); + bool config(); + void readRegisters(uint16_t address, uint8_t *buffer, uint16_t size); + uint8_t readRegister(uint16_t address); + void writeRegisters(uint16_t address, uint8_t *buffer, uint16_t size); + void writeRegister(uint16_t address, uint8_t value); + void writeCommand(uint8_t Opcode, uint8_t *buffer, uint16_t size); + void readCommand(uint8_t Opcode, uint8_t *buffer, uint16_t size); + void resetDevice(); + bool checkDevice(); + void setupLoRa(uint32_t frequency, int32_t offset, uint8_t modParam1, uint8_t modParam2, uint8_t modParam3); + void setMode(uint8_t modeconfig); + void setRegulatorMode(uint8_t mode); + void setPacketType(uint8_t PacketType); + void setRfFrequency(uint32_t frequency, int32_t offset); + void setBufferBaseAddress(uint8_t txBaseAddress, uint8_t rxBaseAddress); + void setModulationParams(uint8_t modParam1, uint8_t modParam2, uint8_t modParam3); + void setPacketParams(uint8_t packetParam1, uint8_t packetParam2, uint8_t packetParam3, uint8_t packetParam4, uint8_t packetParam5, uint8_t packetParam6, uint8_t packetParam7); + void setPacketParams(uint8_t packetParam1, uint8_t packetParam2, uint8_t packetParam3, uint8_t packetParam4, uint8_t packetParam5); + void setDioIrqParams(uint16_t irqMask, uint16_t dio1Mask, uint16_t dio2Mask, uint16_t dio3Mask); + void setHighSensitivity(); + void setLowPowerRX(); + void printModemSettings(); + void printDevice(); + uint32_t getFreqInt(); + uint8_t getLoRaSF(); + uint32_t returnBandwidth(uint8_t data); + uint8_t getLoRaCodingRate(); + uint8_t getInvertIQ(); + uint16_t getPreamble(); + void printOperatingSettings(); + uint8_t getLNAgain(); + void printRegisters(uint16_t Start, uint16_t End); + void printASCIIPacket(uint8_t *buff, uint8_t tsize); + uint8_t transmit(uint8_t *txbuffer, uint8_t size, uint16_t timeout, int8_t txpower, uint8_t wait); + uint8_t transmitIRQ(uint8_t *txbuffer, uint8_t size, uint16_t timeout, int8_t txpower, uint8_t wait); + void setTxParams(int8_t TXpower, uint8_t RampTime); + void setTx(uint16_t timeout); + void clearIrqStatus(uint16_t irq); + uint16_t readIrqStatus(); + void printIrqStatus(); + uint16_t CRCCCITT(uint8_t *buffer, uint32_t size, uint16_t start); + uint8_t receive(uint8_t *rxbuffer, uint8_t size, uint16_t timeout, uint8_t wait); + uint8_t receiveIRQ(uint8_t *rxbuffer, uint8_t size, uint16_t timeout, uint8_t wait); + int16_t readPacketRSSI2(); + int16_t readPacketRSSI(); + int8_t readPacketSNR(); + uint8_t readRXPacketL(); + void setRx(uint16_t timeout); + void setSyncWord1(uint32_t syncword); + void setSyncWord2(uint32_t syncword); + void setSyncWord3(uint32_t syncword); + void setSyncWordErrors(uint8_t errors); + void setSleep(uint8_t sleepconfig); + uint16_t CRCCCITTSX(uint8_t startadd, uint8_t endadd, uint16_t startvalue); + uint8_t getByteSXBuffer(uint8_t addr); + int32_t getFrequencyErrorRegValue(); + int32_t getFrequencyErrorHz(); + void printHEXByte(uint8_t temp); + void wake(); + uint8_t transmitAddressed(uint8_t *txbuffer, uint8_t size, char txpackettype, char txdestination, char txsource, uint32_t timeout, int8_t txpower, uint8_t wait); + uint8_t receiveAddressed(uint8_t *rxbuffer, uint8_t size, uint16_t timeout, uint8_t wait); + uint8_t readRXPacketType(); + uint8_t readPacket(uint8_t *rxbuffer, uint8_t size); + void setPeriodBase(uint8_t value); + uint8_t getPeriodBase(); - void rxEnable(); - void txEnable(); + //*************************************************************************** + // Start direct access SX buffer routines + //*************************************************************************** + void startWriteSXBuffer(uint8_t ptr); + uint8_t endWriteSXBuffer(); + void startReadSXBuffer(uint8_t ptr); + uint8_t endReadSXBuffer(); + void writeUint8(uint8_t x); + uint8_t readUint8(); - void checkBusy(); - bool config(); - void readRegisters( uint16_t address, uint8_t *buffer, uint16_t size ); - uint8_t readRegister( uint16_t address ); - void writeRegisters( uint16_t address, uint8_t *buffer, uint16_t size ); - void writeRegister( uint16_t address, uint8_t value ); - void writeCommand(uint8_t Opcode, uint8_t *buffer, uint16_t size ); - void readCommand( uint8_t Opcode, uint8_t *buffer, uint16_t size ); - void resetDevice(); - bool checkDevice(); - void setupLoRa(uint32_t frequency, int32_t offset, uint8_t modParam1, uint8_t modParam2, uint8_t modParam3); - void setMode(uint8_t modeconfig); - void setRegulatorMode(uint8_t mode); - void setPacketType(uint8_t PacketType); - void setRfFrequency( uint32_t frequency, int32_t offset ); - void setBufferBaseAddress(uint8_t txBaseAddress, uint8_t rxBaseAddress); - void setModulationParams(uint8_t modParam1, uint8_t modParam2, uint8_t modParam3); - void setPacketParams(uint8_t packetParam1, uint8_t packetParam2, uint8_t packetParam3, uint8_t packetParam4, uint8_t packetParam5, uint8_t packetParam6, uint8_t packetParam7); - void setPacketParams(uint8_t packetParam1, uint8_t packetParam2, uint8_t packetParam3, uint8_t packetParam4, uint8_t packetParam5); - void setDioIrqParams(uint16_t irqMask, uint16_t dio1Mask, uint16_t dio2Mask, uint16_t dio3Mask ); - void setHighSensitivity(); - void setLowPowerRX(); - void printModemSettings(); - void printDevice(); - uint32_t getFreqInt(); - uint8_t getLoRaSF(); - uint32_t returnBandwidth(uint8_t data); - uint8_t getLoRaCodingRate(); - uint8_t getInvertIQ(); - uint16_t getPreamble(); - void printOperatingSettings(); - uint8_t getLNAgain(); - void printRegisters(uint16_t Start, uint16_t End); - void printASCIIPacket(uint8_t *buff, uint8_t tsize); - uint8_t transmit(uint8_t *txbuffer, uint8_t size, uint16_t timeout, int8_t txpower, uint8_t wait); - uint8_t transmitIRQ(uint8_t *txbuffer, uint8_t size, uint16_t timeout, int8_t txpower, uint8_t wait); - void setTxParams(int8_t TXpower, uint8_t RampTime); - void setTx(uint16_t timeout); - void clearIrqStatus( uint16_t irq ); - uint16_t readIrqStatus(); - void printIrqStatus(); - uint16_t CRCCCITT(uint8_t *buffer, uint32_t size, uint16_t start); - uint8_t receive(uint8_t *rxbuffer, uint8_t size, uint16_t timeout, uint8_t wait); - uint8_t receiveIRQ(uint8_t *rxbuffer, uint8_t size, uint16_t timeout, uint8_t wait); - int16_t readPacketRSSI2(); - int16_t readPacketRSSI(); - int8_t readPacketSNR(); - uint8_t readRXPacketL(); - void setRx(uint16_t timeout); - void setSyncWord1(uint32_t syncword); - void setSyncWord2(uint32_t syncword); - void setSyncWord3(uint32_t syncword); - void setSyncWordErrors(uint8_t errors); - void setSleep(uint8_t sleepconfig); - uint16_t CRCCCITTSX(uint8_t startadd, uint8_t endadd, uint16_t startvalue); - uint8_t getByteSXBuffer(uint8_t addr); - int32_t getFrequencyErrorRegValue(); - int32_t getFrequencyErrorHz(); - void printHEXByte(uint8_t temp); - void wake(); - uint8_t transmitAddressed(uint8_t *txbuffer, uint8_t size, char txpackettype, char txdestination, char txsource, uint32_t timeout, int8_t txpower, uint8_t wait); - uint8_t receiveAddressed(uint8_t *rxbuffer, uint8_t size, uint16_t timeout, uint8_t wait); - uint8_t readRXPacketType(); - uint8_t readPacket(uint8_t *rxbuffer, uint8_t size); - void setPeriodBase(uint8_t value); - uint8_t getPeriodBase(); + void writeInt8(int8_t x); + int8_t readInt8(); + void writeInt16(int16_t x); + int16_t readInt16(); - //*************************************************************************** - //Start direct access SX buffer routines - //*************************************************************************** - void startWriteSXBuffer(uint8_t ptr); - uint8_t endWriteSXBuffer(); - void startReadSXBuffer(uint8_t ptr); - uint8_t endReadSXBuffer(); + void writeUint16(uint16_t x); + uint16_t readUint16(); - void writeUint8(uint8_t x); - uint8_t readUint8(); + void writeInt32(int32_t x); + int32_t readInt32(); - void writeInt8(int8_t x); - int8_t readInt8(); + void writeUint32(uint32_t x); + uint32_t readUint32(); - void writeInt16(int16_t x); - int16_t readInt16(); + void writeFloat(float x); + float readFloat(); - void writeUint16(uint16_t x); - uint16_t readUint16(); + uint8_t transmitSXBuffer(uint8_t startaddr, uint8_t length, uint16_t timeout, int8_t txpower, uint8_t wait); + uint8_t transmitSXBufferIRQ(uint8_t startaddr, uint8_t length, uint16_t timeout, int8_t txpower, uint8_t wait); + void writeBuffer(uint8_t *txbuffer, uint8_t size); + // void writeBuffer(uint8_t *txbuffer, uint8_t startaddr, uint8_t size); + uint8_t receiveSXBuffer(uint8_t startaddr, uint16_t timeout, uint8_t wait); + uint8_t receiveSXBufferIRQ(uint8_t startaddr, uint16_t timeout, uint8_t wait); + uint8_t readBuffer(uint8_t *rxbuffer); + // uint8_t readBuffer(uint8_t *rxbuffer, uint8_t startaddr, uint8_t len); + void printSXBufferHEX(uint8_t start, uint8_t end); + void writeBufferChar(char *txbuffer, uint8_t size); + uint8_t readBufferChar(char *rxbuffer); - void writeInt32(int32_t x); - int32_t readInt32(); + //*************************************************************************** + // Start ranging routines + //*************************************************************************** + void setRangingSlaveAddress(uint32_t address); + void setRangingSlaveAddress(uint32_t address, uint8_t bits); + void setRangingMasterAddress(uint32_t address); + void setRangingCalibration(uint16_t cal); + void setRangingRole(uint8_t role); + double getRangingDistance(uint8_t resultType, int32_t regval, float adjust); + uint32_t getRangingResultRegValue(uint8_t resultType); + int32_t complement2(uint32_t num, uint8_t bitCnt); + bool setupRanging(uint32_t frequency, int32_t offset, uint8_t modParam1, uint8_t modParam2, uint8_t modParam3, uint32_t address, uint8_t role); + bool transmitRanging(uint32_t address, uint16_t timeout, int8_t txpower, uint8_t wait); + uint8_t receiveRanging(uint32_t address, uint16_t timeout, int8_t txpower, uint8_t wait); + uint8_t receiveRanging(uint32_t address, uint8_t bits, uint16_t timeout, int8_t txpower, uint8_t wait); + uint16_t lookupCalibrationValue(uint8_t spreadingfactor, uint8_t bandwidth); + uint16_t getSetCalibrationValue(); + int16_t getRangingRSSI(); - void writeUint32(uint32_t x); - uint32_t readUint32(); + //******************************************************************************* + // Reliable RX\TX routines + //******************************************************************************* + uint8_t transmitReliable(uint8_t *txbuffer, uint8_t size, uint16_t networkID, uint32_t txtimeout, int8_t txpower, uint8_t wait); + uint8_t receiveReliable(uint8_t *rxbuffer, uint8_t size, uint16_t networkID, uint32_t rxtimeout, uint8_t wait); - void writeFloat(float x); - float readFloat(); + uint8_t transmitReliableAutoACK(uint8_t *txbuffer, uint8_t size, uint16_t networkID, uint32_t acktimeout, uint32_t txtimeout, int8_t txpower, uint8_t wait); + uint8_t receiveReliableAutoACK(uint8_t *rxbuffer, uint8_t size, uint16_t networkID, uint32_t ackdelay, int8_t txpower, uint32_t rxtimeout, uint8_t wait); - uint8_t transmitSXBuffer(uint8_t startaddr, uint8_t length, uint16_t timeout, int8_t txpower, uint8_t wait); - uint8_t transmitSXBufferIRQ(uint8_t startaddr, uint8_t length, uint16_t timeout, int8_t txpower, uint8_t wait); - void writeBuffer(uint8_t *txbuffer, uint8_t size); - //void writeBuffer(uint8_t *txbuffer, uint8_t startaddr, uint8_t size); - uint8_t receiveSXBuffer(uint8_t startaddr, uint16_t timeout, uint8_t wait); - uint8_t receiveSXBufferIRQ(uint8_t startaddr, uint16_t timeout, uint8_t wait ); - uint8_t readBuffer(uint8_t *rxbuffer); - //uint8_t readBuffer(uint8_t *rxbuffer, uint8_t startaddr, uint8_t len); - void printSXBufferHEX(uint8_t start, uint8_t end); - void writeBufferChar(char *txbuffer, uint8_t size); - uint8_t readBufferChar(char *rxbuffer); + uint8_t transmitReliableAutoACK_addr(uint8_t *txbuffer, uint8_t size, uint16_t networkID, uint16_t destAddr, uint16_t srcAddr, uint32_t acktimeout, uint32_t txtimeout, int8_t txpower, uint8_t wait); + uint8_t receiveReliableAutoACK_addr(uint8_t *rxbuffer, uint8_t size, uint16_t networkID, uint16_t destAddr, uint32_t ackdelay, int8_t txpower, uint32_t rxtimeout, uint8_t wait, uint16_t *srcAddr = nullptr); + uint8_t sendReliableACK(uint16_t networkID, uint16_t payloadcrc, int8_t txpower); + uint8_t sendReliableACK(uint8_t *txbuffer, uint8_t size, uint16_t networkID, uint16_t payloadcrc, int8_t txpower); + uint8_t waitReliableACK(uint16_t networkID, uint16_t payloadcrc, uint32_t acktimeout); + uint8_t waitReliableACK(uint8_t *rxbuffer, uint8_t size, uint16_t networkID, uint16_t payloadcrc, uint32_t acktimeout); - //*************************************************************************** - //Start ranging routines - //*************************************************************************** - void setRangingSlaveAddress(uint32_t address); - void setRangingSlaveAddress(uint32_t address, uint8_t bits); - void setRangingMasterAddress(uint32_t address); - void setRangingCalibration(uint16_t cal); - void setRangingRole(uint8_t role); - double getRangingDistance(uint8_t resultType, int32_t regval, float adjust); - uint32_t getRangingResultRegValue(uint8_t resultType); - int32_t complement2( uint32_t num, uint8_t bitCnt ); - bool setupRanging(uint32_t frequency, int32_t offset, uint8_t modParam1, uint8_t modParam2, uint8_t modParam3, uint32_t address, uint8_t role); - bool transmitRanging(uint32_t address, uint16_t timeout, int8_t txpower, uint8_t wait); - uint8_t receiveRanging(uint32_t address, uint16_t timeout, int8_t txpower, uint8_t wait); - uint8_t receiveRanging(uint32_t address, uint8_t bits, uint16_t timeout, int8_t txpower, uint8_t wait); - uint16_t lookupCalibrationValue(uint8_t spreadingfactor, uint8_t bandwidth); - uint16_t getSetCalibrationValue(); - int16_t getRangingRSSI(); + uint8_t sendReliableACK_addr(uint16_t networkID, uint16_t srcAddr, uint16_t destAddr, uint16_t payloadcrc, int8_t txpower); + uint8_t waitReliableACK_addr(uint16_t networkID, uint16_t destAddr, uint16_t srcAddr, uint16_t payloadcrc, uint32_t acktimeout); + void printASCIIArray(uint8_t *buffer, uint8_t size); + uint8_t getReliableConfig(uint8_t bitread); - //******************************************************************************* - //Reliable RX\TX routines - //******************************************************************************* - uint8_t transmitReliable(uint8_t *txbuffer, uint8_t size, uint16_t networkID, uint32_t txtimeout, int8_t txpower, uint8_t wait); - uint8_t receiveReliable(uint8_t *rxbuffer, uint8_t size, uint16_t networkID, uint32_t rxtimeout, uint8_t wait ); + uint16_t getTXPayloadCRC(uint8_t length); + void printReliableStatus(); + uint16_t getRXPayloadCRC(uint8_t length); + uint16_t getRXNetworkID(uint8_t length); + uint16_t getTXNetworkID(uint8_t length); + void writeUint16SXBuffer(uint8_t addr, uint16_t regdata); + uint16_t readUint16SXBuffer(uint8_t addr); - uint8_t transmitReliableAutoACK(uint8_t *txbuffer, uint8_t size, uint16_t networkID, uint32_t acktimeout, uint32_t txtimeout, int8_t txpower, uint8_t wait); - uint8_t receiveReliableAutoACK(uint8_t *rxbuffer, uint8_t size, uint16_t networkID, uint32_t ackdelay, int8_t txpower, uint32_t rxtimeout, uint8_t wait ); + void printHEXPacket(uint8_t *buffer, uint8_t size); + void printHEXPacket(char *buffer, uint8_t size); + void printArrayHEX(uint8_t *buffer, uint8_t size); + void printArrayHEX(char *buffer, uint8_t size); + void setReliableConfig(uint8_t bitset); + void clearReliableConfig(uint8_t bitset); - uint8_t sendReliableACK(uint16_t networkID, uint16_t payloadcrc, int8_t txpower); - uint8_t sendReliableACK(uint8_t *txbuffer, uint8_t size, uint16_t networkID, uint16_t payloadcrc, int8_t txpower); - uint8_t waitReliableACK(uint16_t networkID, uint16_t payloadcrc, uint32_t acktimeout); - uint8_t waitReliableACK(uint8_t *rxbuffer, uint8_t size, uint16_t networkID, uint16_t payloadcrc, uint32_t acktimeout); + uint8_t getPacketType(); + void setReliableRX(uint16_t timeout); - void printASCIIArray(uint8_t *buffer, uint8_t size); - uint8_t getReliableConfig(uint8_t bitread); + void setupFLRC(uint32_t frequency, int32_t offset, uint8_t modParam1, uint8_t modParam2, uint8_t modParam3, uint32_t syncword); + void setFLRCPayloadLengthReg(uint8_t length); + void setLoRaPayloadLengthReg(uint8_t length); + void setPayloadLength(uint8_t length); + uint16_t CRCCCITTReliable(uint8_t startadd, uint8_t endadd, uint16_t startvalue); - uint16_t getTXPayloadCRC(uint8_t length); - void printReliableStatus(); - uint16_t getRXPayloadCRC(uint8_t length); - uint16_t getRXNetworkID(uint8_t length); - uint16_t getTXNetworkID(uint8_t length); - void writeUint16SXBuffer(uint8_t addr, uint16_t regdata); - uint16_t readUint16SXBuffer(uint8_t addr); + uint8_t transmitSXReliable(uint8_t startaddr, uint8_t length, uint16_t networkID, uint32_t txtimeout, int8_t txpower, uint8_t wait); + uint8_t transmitSXReliableIRQ(uint8_t startaddr, uint8_t length, uint16_t networkID, uint32_t txtimeout, int8_t txpower, uint8_t wait); + uint8_t receiveSXReliable(uint8_t startaddr, uint16_t networkID, uint32_t rxtimeout, uint8_t wait); + uint8_t receiveSXReliableIRQ(uint8_t startaddr, uint16_t networkID, uint32_t rxtimeout, uint8_t wait); + uint8_t transmitSXReliableAutoACK(uint8_t startaddr, uint8_t length, uint16_t networkID, uint32_t acktimeout, uint32_t txtimeout, int8_t txpower, uint8_t wait); + uint8_t receiveSXReliableAutoACK(uint8_t startaddr, uint16_t networkID, uint32_t ackdelay, int8_t txpower, uint32_t rxtimeout, uint8_t wait); + uint8_t waitSXReliableACK(uint8_t startaddr, uint16_t networkID, uint16_t payloadcrc, uint32_t acktimeout); + uint8_t waitSXReliableACKIRQ(uint8_t startaddr, uint16_t networkID, uint16_t payloadcrc, uint32_t acktimeout); + uint8_t sendSXReliableACK(uint8_t startaddr, uint8_t length, uint16_t networkID, uint16_t payloadcrc, int8_t txpower); + uint8_t sendSXReliableACKIRQ(uint8_t startaddr, uint8_t length, uint16_t networkID, uint16_t payloadcrc, int8_t txpower); + uint8_t readReliableErrors(); + uint8_t readReliableFlags(); - void printHEXPacket(uint8_t *buffer, uint8_t size); - void printHEXPacket(char *buffer, uint8_t size); - void printArrayHEX(uint8_t *buffer, uint8_t size); - void printArrayHEX(char *buffer, uint8_t size); - void setReliableConfig(uint8_t bitset); - void clearReliableConfig(uint8_t bitset); + //******************************************************************************* + // Data Transfer functions + //******************************************************************************* + uint8_t transmitDT(uint8_t *header, uint8_t headersize, uint8_t *dataarray, uint8_t datasize, uint16_t networkID, uint32_t txtimeout, int8_t txpower, uint8_t wait); + uint8_t waitACKDT(uint8_t *header, uint8_t headersize, uint32_t acktimeout); + uint8_t receiveDT(uint8_t *header, uint8_t headersize, uint8_t *dataarray, uint8_t datasize, uint16_t networkID, uint32_t rxtimeout, uint8_t wait); + uint8_t sendACKDT(uint8_t *header, uint8_t headersize, int8_t txpower); - uint8_t getPacketType(); - void setReliableRX(uint16_t timeout); + uint8_t transmitDTIRQ(uint8_t *header, uint8_t headersize, uint8_t *dataarray, uint8_t datasize, uint16_t networkID, uint32_t txtimeout, int8_t txpower, uint8_t wait); + uint8_t waitACKDTIRQ(uint8_t *header, uint8_t headersize, uint32_t acktimeout); + uint8_t receiveDTIRQ(uint8_t *header, uint8_t headersize, uint8_t *dataarray, uint8_t datasize, uint16_t networkID, uint32_t rxtimeout, uint8_t wait); + uint8_t sendACKDTIRQ(uint8_t *header, uint8_t headersize, int8_t txpower); - void setupFLRC(uint32_t frequency, int32_t offset, uint8_t modParam1, uint8_t modParam2, uint8_t modParam3, uint32_t syncword); - void setFLRCPayloadLengthReg(uint8_t length); - void setLoRaPayloadLengthReg(uint8_t length); - void setPayloadLength(uint8_t length); - uint16_t CRCCCITTReliable(uint8_t startadd, uint8_t endadd, uint16_t startvalue); +private: + int8_t _NSS, _NRESET, _RFBUSY, _DIO1, _DIO2, _DIO3; + int8_t _RXEN, _TXEN; + uint8_t _RXPacketL; // length of packet received + uint8_t _RXPacketType; // type number of received packet + uint8_t _RXDestination; // destination address of received packet + uint8_t _RXSource; // source address of received packet + // int8_t _PacketRSSI; //RSSI of received packet - removed November 2021, not used + // int8_t _PacketSNR; //signal to noise ratio of received packet - removed November 2021, not used + int8_t _TXPacketL; // transmitted packet length + uint8_t _RXcount; // used to keep track of the bytes read from SX1280 buffer during readFloat() etc + uint8_t _TXcount; // used to keep track of the bytes written to SX1280 buffer during writeFloat() etc + uint8_t _OperatingMode; // current operating mode + bool _rxtxpinmode = false; // set to true if RX and TX pin mode is used. - uint8_t transmitSXReliable(uint8_t startaddr, uint8_t length, uint16_t networkID, uint32_t txtimeout, int8_t txpower, uint8_t wait); - uint8_t transmitSXReliableIRQ(uint8_t startaddr, uint8_t length, uint16_t networkID, uint32_t txtimeout, int8_t txpower, uint8_t wait); - uint8_t receiveSXReliable(uint8_t startaddr, uint16_t networkID, uint32_t rxtimeout, uint8_t wait ); - uint8_t receiveSXReliableIRQ(uint8_t startaddr, uint16_t networkID, uint32_t rxtimeout, uint8_t wait ); - uint8_t transmitSXReliableAutoACK(uint8_t startaddr, uint8_t length, uint16_t networkID, uint32_t acktimeout, uint32_t txtimeout, int8_t txpower, uint8_t wait); - uint8_t receiveSXReliableAutoACK(uint8_t startaddr, uint16_t networkID, uint32_t ackdelay, int8_t txpower, uint32_t rxtimeout, uint8_t wait ); - uint8_t waitSXReliableACK(uint8_t startaddr, uint16_t networkID, uint16_t payloadcrc, uint32_t acktimeout); - uint8_t waitSXReliableACKIRQ(uint8_t startaddr, uint16_t networkID, uint16_t payloadcrc, uint32_t acktimeout); - uint8_t sendSXReliableACK(uint8_t startaddr, uint8_t length, uint16_t networkID, uint16_t payloadcrc, int8_t txpower); - uint8_t sendSXReliableACKIRQ(uint8_t startaddr, uint8_t length, uint16_t networkID, uint16_t payloadcrc, int8_t txpower); - uint8_t readReliableErrors(); - uint8_t readReliableFlags(); + uint8_t _Device; // saved device type + uint8_t _TXDonePin; // the pin that will indicate TX done + uint8_t _RXDonePin; // the pin that will indicate RX done + uint8_t _PERIODBASE = PERIODBASE_01_MS; - //******************************************************************************* - //Data Transfer functions - //******************************************************************************* - uint8_t transmitDT(uint8_t *header, uint8_t headersize, uint8_t *dataarray, uint8_t datasize, uint16_t networkID, uint32_t txtimeout, int8_t txpower, uint8_t wait); - uint8_t waitACKDT(uint8_t *header, uint8_t headersize, uint32_t acktimeout); - uint8_t receiveDT(uint8_t *header, uint8_t headersize, uint8_t *dataarray, uint8_t datasize, uint16_t networkID, uint32_t rxtimeout, uint8_t wait ); - uint8_t sendACKDT(uint8_t *header, uint8_t headersize, int8_t txpower); - - uint8_t transmitDTIRQ(uint8_t *header, uint8_t headersize, uint8_t *dataarray, uint8_t datasize, uint16_t networkID, uint32_t txtimeout, int8_t txpower, uint8_t wait); - uint8_t waitACKDTIRQ(uint8_t *header, uint8_t headersize, uint32_t acktimeout); - uint8_t receiveDTIRQ(uint8_t *header, uint8_t headersize, uint8_t *dataarray, uint8_t datasize, uint16_t networkID, uint32_t rxtimeout, uint8_t wait ); - uint8_t sendACKDTIRQ(uint8_t *header, uint8_t headersize, int8_t txpower); - - - private: - - int8_t _NSS, _NRESET, _RFBUSY, _DIO1, _DIO2, _DIO3; - int8_t _RXEN, _TXEN; - uint8_t _RXPacketL; //length of packet received - uint8_t _RXPacketType; //type number of received packet - uint8_t _RXDestination; //destination address of received packet - uint8_t _RXSource; //source address of received packet - //int8_t _PacketRSSI; //RSSI of received packet - removed November 2021, not used - //int8_t _PacketSNR; //signal to noise ratio of received packet - removed November 2021, not used - int8_t _TXPacketL; //transmitted packet length - uint8_t _RXcount; //used to keep track of the bytes read from SX1280 buffer during readFloat() etc - uint8_t _TXcount; //used to keep track of the bytes written to SX1280 buffer during writeFloat() etc - uint8_t _OperatingMode; //current operating mode - bool _rxtxpinmode = false; //set to true if RX and TX pin mode is used. - - uint8_t _Device; //saved device type - uint8_t _TXDonePin; //the pin that will indicate TX done - uint8_t _RXDonePin; //the pin that will indicate RX done - uint8_t _PERIODBASE = PERIODBASE_01_MS; - - uint8_t savedRegulatorMode; - uint8_t savedPacketType; - uint32_t savedFrequency; - int32_t savedOffset; - uint8_t savedModParam1, savedModParam2, savedModParam3; //sequence is spreading factor, bandwidth, coding rate - uint8_t savedPacketParam1, savedPacketParam2, savedPacketParam3, savedPacketParam4, savedPacketParam5, savedPacketParam6, savedPacketParam7; - uint16_t savedIrqMask, savedDio1Mask, savedDio2Mask, savedDio3Mask; - int8_t savedTXPower; - uint16_t savedCalibration; - uint32_t savedFrequencyReg; - - uint8_t _ReliableErrors; //Reliable status byte - uint8_t _ReliableFlags; //Reliable flags byte - uint8_t _ReliableConfig; //Reliable config byte + uint8_t savedRegulatorMode; + uint8_t savedPacketType; + uint32_t savedFrequency; + int32_t savedOffset; + uint8_t savedModParam1, savedModParam2, savedModParam3; // sequence is spreading factor, bandwidth, coding rate + uint8_t savedPacketParam1, savedPacketParam2, savedPacketParam3, savedPacketParam4, savedPacketParam5, savedPacketParam6, savedPacketParam7; + uint16_t savedIrqMask, savedDio1Mask, savedDio2Mask, savedDio3Mask; + int8_t savedTXPower; + uint16_t savedCalibration; + uint32_t savedFrequencyReg; + uint8_t _ReliableErrors; // Reliable status byte + uint8_t _ReliableFlags; // Reliable flags byte + uint8_t _ReliableConfig; // Reliable config byte }; #endif diff --git a/src/SX128XLT_Definitions.h b/src/SX128XLT_Definitions.h index 887cd12..b8aac24 100644 --- a/src/SX128XLT_Definitions.h +++ b/src/SX128XLT_Definitions.h @@ -405,6 +405,7 @@ const double CH_freq[] = {0E6, #define ReliableTimeout 0x04 // bit number set in _ReliableErrors when there is a timeout error #define SegmentSequenceError 0x05 // bit number set in _ReliableErrors when there is a segment sequence error #define FileError 0x06 // bit number set in _ReliableErrors when there ia a file (SD) error +#define ReliableAddrError 0x07 // bit number set in _ReliableErrors when there is intended Address is incorrect // These are the bit numbers which when set indicate reliable status flags, variable _ReliableFlags #define ReliableACKSent 0x00 // bit number set in _ReliableFlags when there is a ACK sent