From c99e79073f9fc2ae6ff70179d9dad12502cca8c3 Mon Sep 17 00:00:00 2001 From: jgromes Date: Sun, 21 Nov 2021 21:16:37 +0100 Subject: [PATCH] [Si443x] Fixed known binary receive length --- src/modules/Si443x/Si443x.cpp | 21 ++++++++++++++++++--- src/modules/Si443x/Si443x.h | 4 +++- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/modules/Si443x/Si443x.cpp b/src/modules/Si443x/Si443x.cpp index 5c485613..1e4221e9 100644 --- a/src/modules/Si443x/Si443x.cpp +++ b/src/modules/Si443x/Si443x.cpp @@ -285,14 +285,22 @@ int16_t Si443x::readData(uint8_t* data, size_t len) { clearIRQFlags(); // get packet length - size_t length = len; - if(len == RADIOLIB_SI443X_MAX_PACKET_LENGTH) { - length = getPacketLength(); + size_t length = getPacketLength(); + size_t dumpLen = 0; + if((len != 0) && (len < length)) { + // user requested less data than we got, only return what was requested + dumpLen = length - len; + length = len; } // read packet data _mod->SPIreadRegisterBurst(RADIOLIB_SI443X_REG_FIFO_ACCESS, length, data); + // dump the bytes that weren't requested + if(dumpLen != 0) { + clearFIFO(dumpLen); + } + // clear internal flag so getPacketLength can return the new packet length _packetLengthQueried = false; @@ -647,6 +655,13 @@ void Si443x::clearIRQFlags() { _mod->SPIreadRegisterBurst(RADIOLIB_SI443X_REG_INTERRUPT_STATUS_1, 2, buff); } +void Si443x::clearFIFO(size_t count) { + while(count) { + _mod->SPIreadRegister(RADIOLIB_SI443X_REG_FIFO_ACCESS); + count--; + } +} + int16_t Si443x::config() { // set mode to standby int16_t state = standby(); diff --git a/src/modules/Si443x/Si443x.h b/src/modules/Si443x/Si443x.h index 1072e289..9a5c0df1 100644 --- a/src/modules/Si443x/Si443x.h +++ b/src/modules/Si443x/Si443x.h @@ -695,7 +695,8 @@ class Si443x: public PhysicalLayer { \param data Pointer to array to save the received binary data. - \param len Number of bytes that will be received. Must be known in advance for binary transmissions. + \param len Number of bytes that will be read. When set to 0, the packet length will be retreived automatically. + When more bytes than received are requested, only the number of bytes requested will be returned. \returns \ref status_codes */ @@ -838,6 +839,7 @@ class Si443x: public PhysicalLayer { #endif bool findChip(); void clearIRQFlags(); + void clearFIFO(size_t count); int16_t config(); int16_t updateClockRecovery(); int16_t directMode();