Skip to content

Commit

Permalink
[SX126x] Implemented getPacketLength
Browse files Browse the repository at this point in the history
  • Loading branch information
jgromes committed Jun 16, 2019
1 parent 94301c9 commit 5527573
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 32 deletions.
32 changes: 17 additions & 15 deletions examples/SX126x/SX126x_Transmit/SX126x_Transmit.ino
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,22 @@ void setup() {
// control must be enabled by calling
// setTCXO() and specifying the reference
// voltage.

/*
Serial.print(F("[SX1262] Setting TCXO reference ... "));
// enable TCXO
// reference voltage: 1.6 V
// timeout: 5000 us
state = lora.setTCXO(1.6);
if (state == ERR_NONE) {
Serial.println(F("success!"));
} else {
Serial.print(F("failed, code "));
Serial.println(state);
while (true);
}
Serial.print(F("[SX1262] Setting TCXO reference ... "));
// enable TCXO
// reference voltage: 1.6 V
// timeout: 5000 us
state = lora.setTCXO(1.6);
if (state == ERR_NONE) {
Serial.println(F("success!"));
} else {
Serial.print(F("failed, code "));
Serial.println(state);
while (true);
}
*/

}

void loop() {
Expand All @@ -90,7 +92,7 @@ void loop() {

if (state == ERR_NONE) {
// the packet was successfully transmitted
Serial.println(F(" success!"));
Serial.println(F("success!"));

// print measured data rate
Serial.print(F("[SX1262] Datarate:\t"));
Expand All @@ -99,11 +101,11 @@ void loop() {

} else if (state == ERR_PACKET_TOO_LONG) {
// the supplied packet was longer than 256 bytes
Serial.println(F(" too long!"));
Serial.println(F("too long!"));

} else if (state == ERR_TX_TIMEOUT) {
// timeout occured while transmitting packet
Serial.println(F(" timeout!"));
Serial.println(F("timeout!"));

} else {
// some other error occurred
Expand Down
29 changes: 12 additions & 17 deletions src/modules/SX126x.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "SX126x.h"

SX126x::SX126x(Module* mod) : PhysicalLayer(SX126X_CRYSTAL_FREQ, SX126X_DIV_EXPONENT) {
SX126x::SX126x(Module* mod) : PhysicalLayer(SX126X_CRYSTAL_FREQ, SX126X_DIV_EXPONENT, SX126X_MAX_PACKET_LENGTH) {
_mod = mod;
}

Expand Down Expand Up @@ -137,7 +137,7 @@ int16_t SX126x::transmit(uint8_t* data, size_t len, uint8_t addr) {
}

// check packet length
if(len >= 256) {
if(len > SX126X_MAX_PACKET_LENGTH) {
return(ERR_PACKET_TOO_LONG);
}

Expand Down Expand Up @@ -359,7 +359,7 @@ int16_t SX126x::startTransmit(uint8_t* data, size_t len, uint8_t addr) {
(void)addr;

// check packet length
if(len >= 256) {
if(len > SX126X_MAX_PACKET_LENGTH) {
return(ERR_PACKET_TOO_LONG);
}

Expand Down Expand Up @@ -447,22 +447,10 @@ int16_t SX126x::readData(uint8_t* data, size_t len) {
}

// get packet length
uint8_t rxBufStatus[2];
int16_t state = SPIreadCommand(SX126X_CMD_GET_RX_BUFFER_STATUS, rxBufStatus, 2);
if(state != ERR_NONE) {
return(state);
}

size_t length = rxBufStatus[0];
size_t length = getPacketLength();

// read packet data
if(len == 0) {
// argument 'len' equal to zero indicates String call, which means dynamically allocated data array
// dispose of the original and create a new one
delete[] data;
data = new uint8_t[length + 1];
}
state = readBuffer(data, length);
int16_t state = readBuffer(data, length);
if(state != ERR_NONE) {
return(state);
}
Expand Down Expand Up @@ -872,6 +860,13 @@ float SX126x::getSNR() {
return(snrPkt/4.0);
}

size_t SX126x::getPacketLength(bool update) {
(void)update;
uint8_t rxBufStatus[2];
SPIreadCommand(SX126X_CMD_GET_RX_BUFFER_STATUS, rxBufStatus, 2);
return((size_t)rxBufStatus[0]);
}

int16_t SX126x::setTCXO(float voltage, uint32_t timeout) {
// set mode to standby
standby();
Expand Down
10 changes: 10 additions & 0 deletions src/modules/SX126x.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
// SX126X physical layer properties
#define SX126X_CRYSTAL_FREQ 32.0
#define SX126X_DIV_EXPONENT 25
#define SX126X_MAX_PACKET_LENGTH 255

// SX126X SPI commands
// operational modes commands
Expand Down Expand Up @@ -685,6 +686,15 @@ class SX126x: public PhysicalLayer {
*/
float getSNR();

/*!
\brief Query modem for the packet length of received payload.
\param update Update received packet length. Will return cached value when set to false.
\returns Length of last received packet in bytes.
*/
size_t getPacketLength(bool update = true);

protected:
// SX1276x SPI command implementations
int16_t setTx(uint32_t timeout = 0);
Expand Down

0 comments on commit 5527573

Please sign in to comment.