[SX126x] Implemented getPacketLength

This commit is contained in:
jgromes 2019-06-16 14:34:19 +02:00
parent 94301c9043
commit 5527573692
3 changed files with 39 additions and 32 deletions

View file

@ -56,20 +56,22 @@ void setup() {
// control must be enabled by calling // control must be enabled by calling
// setTCXO() and specifying the reference // setTCXO() and specifying the reference
// voltage. // voltage.
/* /*
Serial.print(F("[SX1262] Setting TCXO reference ... ")); Serial.print(F("[SX1262] Setting TCXO reference ... "));
// enable TCXO // enable TCXO
// reference voltage: 1.6 V // reference voltage: 1.6 V
// timeout: 5000 us // timeout: 5000 us
state = lora.setTCXO(1.6); state = lora.setTCXO(1.6);
if (state == ERR_NONE) { if (state == ERR_NONE) {
Serial.println(F("success!")); Serial.println(F("success!"));
} else { } else {
Serial.print(F("failed, code ")); Serial.print(F("failed, code "));
Serial.println(state); Serial.println(state);
while (true); while (true);
} }
*/ */
} }
void loop() { void loop() {
@ -90,7 +92,7 @@ void loop() {
if (state == ERR_NONE) { if (state == ERR_NONE) {
// the packet was successfully transmitted // the packet was successfully transmitted
Serial.println(F(" success!")); Serial.println(F("success!"));
// print measured data rate // print measured data rate
Serial.print(F("[SX1262] Datarate:\t")); Serial.print(F("[SX1262] Datarate:\t"));
@ -99,11 +101,11 @@ void loop() {
} else if (state == ERR_PACKET_TOO_LONG) { } else if (state == ERR_PACKET_TOO_LONG) {
// the supplied packet was longer than 256 bytes // the supplied packet was longer than 256 bytes
Serial.println(F(" too long!")); Serial.println(F("too long!"));
} else if (state == ERR_TX_TIMEOUT) { } else if (state == ERR_TX_TIMEOUT) {
// timeout occured while transmitting packet // timeout occured while transmitting packet
Serial.println(F(" timeout!")); Serial.println(F("timeout!"));
} else { } else {
// some other error occurred // some other error occurred

View file

@ -1,6 +1,6 @@
#include "SX126x.h" #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; _mod = mod;
} }
@ -137,7 +137,7 @@ int16_t SX126x::transmit(uint8_t* data, size_t len, uint8_t addr) {
} }
// check packet length // check packet length
if(len >= 256) { if(len > SX126X_MAX_PACKET_LENGTH) {
return(ERR_PACKET_TOO_LONG); return(ERR_PACKET_TOO_LONG);
} }
@ -359,7 +359,7 @@ int16_t SX126x::startTransmit(uint8_t* data, size_t len, uint8_t addr) {
(void)addr; (void)addr;
// check packet length // check packet length
if(len >= 256) { if(len > SX126X_MAX_PACKET_LENGTH) {
return(ERR_PACKET_TOO_LONG); return(ERR_PACKET_TOO_LONG);
} }
@ -447,22 +447,10 @@ int16_t SX126x::readData(uint8_t* data, size_t len) {
} }
// get packet length // get packet length
uint8_t rxBufStatus[2]; size_t length = getPacketLength();
int16_t state = SPIreadCommand(SX126X_CMD_GET_RX_BUFFER_STATUS, rxBufStatus, 2);
if(state != ERR_NONE) {
return(state);
}
size_t length = rxBufStatus[0];
// read packet data // read packet data
if(len == 0) { int16_t state = readBuffer(data, length);
// 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);
if(state != ERR_NONE) { if(state != ERR_NONE) {
return(state); return(state);
} }
@ -872,6 +860,13 @@ float SX126x::getSNR() {
return(snrPkt/4.0); 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) { int16_t SX126x::setTCXO(float voltage, uint32_t timeout) {
// set mode to standby // set mode to standby
standby(); standby();

View file

@ -9,6 +9,7 @@
// SX126X physical layer properties // SX126X physical layer properties
#define SX126X_CRYSTAL_FREQ 32.0 #define SX126X_CRYSTAL_FREQ 32.0
#define SX126X_DIV_EXPONENT 25 #define SX126X_DIV_EXPONENT 25
#define SX126X_MAX_PACKET_LENGTH 255
// SX126X SPI commands // SX126X SPI commands
// operational modes commands // operational modes commands
@ -685,6 +686,15 @@ class SX126x: public PhysicalLayer {
*/ */
float getSNR(); 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: protected:
// SX1276x SPI command implementations // SX1276x SPI command implementations
int16_t setTx(uint32_t timeout = 0); int16_t setTx(uint32_t timeout = 0);