diff --git a/src/modules/SX128x/SX128x.cpp b/src/modules/SX128x/SX128x.cpp index 8f41a797..aa8ec58c 100644 --- a/src/modules/SX128x/SX128x.cpp +++ b/src/modules/SX128x/SX128x.cpp @@ -617,15 +617,16 @@ int16_t SX128x::readData(uint8_t* data, size_t len) { crcState = RADIOLIB_ERR_CRC_MISMATCH; } - // get packet length - size_t length = getPacketLength(); + // get packet length and Rx buffer offset + uint8_t offset = 0; + size_t length = getPacketLength(true, &offset); if((len != 0) && (len < length)) { // user requested less data than we got, only return what was requested length = len; } - // read packet data - state = readBuffer(data, length); + // read packet data starting at offset + state = readBuffer(data, length, offset); RADIOLIB_ASSERT(state); // clear interrupt flags @@ -1254,6 +1255,11 @@ float SX128x::getFrequencyError() { size_t SX128x::getPacketLength(bool update) { (void)update; + return(this->getPacketLength(update, NULL)); +} + +size_t SX128x::getPacketLength(bool update, uint8_t* offset) { + (void)update; // in implicit mode, return the cached value if((getPacketType() == RADIOLIB_SX128X_PACKET_TYPE_LORA) && (this->headerType == RADIOLIB_SX128X_LORA_HEADER_IMPLICIT)) { @@ -1262,6 +1268,9 @@ size_t SX128x::getPacketLength(bool update) { uint8_t rxBufStatus[2] = {0, 0}; this->mod->SPIreadStream(RADIOLIB_SX128X_CMD_GET_RX_BUFFER_STATUS, rxBufStatus, 2); + + if(offset) { *offset = rxBufStatus[1]; } + return((size_t)rxBufStatus[0]); } @@ -1412,8 +1421,8 @@ int16_t SX128x::writeBuffer(uint8_t* data, uint8_t numBytes, uint8_t offset) { return(this->mod->SPIwriteStream(cmd, 2, data, numBytes)); } -int16_t SX128x::readBuffer(uint8_t* data, uint8_t numBytes) { - uint8_t cmd[] = { RADIOLIB_SX128X_CMD_READ_BUFFER, RADIOLIB_SX128X_CMD_NOP }; +int16_t SX128x::readBuffer(uint8_t* data, uint8_t numBytes, uint8_t offset) { + uint8_t cmd[] = { RADIOLIB_SX128X_CMD_READ_BUFFER, offset }; return(this->mod->SPIreadStream(cmd, 2, data, numBytes)); } diff --git a/src/modules/SX128x/SX128x.h b/src/modules/SX128x/SX128x.h index 6ce9164f..8c0d465a 100644 --- a/src/modules/SX128x/SX128x.h +++ b/src/modules/SX128x/SX128x.h @@ -745,6 +745,14 @@ class SX128x: public PhysicalLayer { */ size_t getPacketLength(bool update = true) override; + /*! + \brief Query modem for the packet length of received payload and Rx buffer offset. + \param update Update received packet length. Will return cached value when set to false. + \param offset Pointer to variable to store the Rx buffer offset. + \returns Length of last received packet in bytes. + */ + size_t getPacketLength(bool update, uint8_t* offset); + /*! \brief Get expected time-on-air for a given size of payload. \param len Payload length in bytes. @@ -820,7 +828,7 @@ class SX128x: public PhysicalLayer { int16_t writeRegister(uint16_t addr, uint8_t* data, uint8_t numBytes); int16_t readRegister(uint16_t addr, uint8_t* data, uint8_t numBytes); int16_t writeBuffer(uint8_t* data, uint8_t numBytes, uint8_t offset = 0x00); - int16_t readBuffer(uint8_t* data, uint8_t numBytes); + int16_t readBuffer(uint8_t* data, uint8_t numBytes, uint8_t offset = 0x00); int16_t setTx(uint16_t periodBaseCount = RADIOLIB_SX128X_TX_TIMEOUT_NONE, uint8_t periodBase = RADIOLIB_SX128X_PERIOD_BASE_15_625_US); int16_t setRx(uint16_t periodBaseCount, uint8_t periodBase = RADIOLIB_SX128X_PERIOD_BASE_15_625_US); int16_t setCad();