Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SX126x/SX128x] Start reading from Rx buffer offset #1185

Merged
merged 4 commits into from
Aug 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions src/modules/SX126x/SX126x.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -709,20 +709,17 @@ int16_t SX126x::readData(uint8_t* data, size_t len) {
if((irq & RADIOLIB_SX126X_IRQ_CRC_ERR) || (irq & RADIOLIB_SX126X_IRQ_HEADER_ERR)) {
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);
RADIOLIB_ASSERT(state);

// reset the base addresses
state = setBufferBaseAddress();
// read packet data starting at offset
state = readBuffer(data, length, offset);
RADIOLIB_ASSERT(state);

// clear interrupt flags
Expand Down Expand Up @@ -1394,6 +1391,10 @@ float SX126x::getFrequencyError() {
}

size_t SX126x::getPacketLength(bool update) {
return(this->getPacketLength(update, NULL));
}

size_t SX126x::getPacketLength(bool update, uint8_t* offset) {
(void)update;

// in implicit mode, return the cached value
Expand All @@ -1403,6 +1404,9 @@ size_t SX126x::getPacketLength(bool update) {

uint8_t rxBufStatus[2] = {0, 0};
this->mod->SPIreadStream(RADIOLIB_SX126X_CMD_GET_RX_BUFFER_STATUS, rxBufStatus, 2);

if(offset) { *offset = rxBufStatus[1]; }

return((size_t)rxBufStatus[0]);
}

Expand Down
8 changes: 8 additions & 0 deletions src/modules/SX126x/SX126x.h
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,14 @@ class SX126x: 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 Set modem in fixed packet length mode. Available in FSK mode only.
\param len Packet length.
Expand Down
20 changes: 14 additions & 6 deletions src/modules/SX128x/SX128x.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -1253,6 +1254,10 @@ float SX128x::getFrequencyError() {
}

size_t SX128x::getPacketLength(bool update) {
return(this->getPacketLength(update, NULL));
}

size_t SX128x::getPacketLength(bool update, uint8_t* offset) {
(void)update;

// in implicit mode, return the cached value
Expand All @@ -1262,6 +1267,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]);
}

Expand Down Expand Up @@ -1412,8 +1420,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));
}

Expand Down
10 changes: 9 additions & 1 deletion src/modules/SX128x/SX128x.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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();
Expand Down
Loading