From 55aff74a81d6dd682aad65c1d19fa3bf0b044c7e Mon Sep 17 00:00:00 2001 From: jgromes Date: Fri, 27 Dec 2019 13:21:21 +0100 Subject: [PATCH] [SX126x] Changed pin mapping, added reset, changed LoRa sync word to 1B --- .../SX126x_Channel_Activity_Detection.ino | 3 +- .../SX126x_FSK_Modem/SX126x_FSK_Modem.ino | 3 +- .../SX126x/SX126x_Receive/SX126x_Receive.ino | 4 +- .../SX126x_Receive_Interrupt.ino | 3 +- .../SX126x_Settings/SX126x_Settings.ino | 6 ++- .../SX126x_Transmit/SX126x_Transmit.ino | 5 +- .../SX126x_Transmit_Interrupt.ino | 3 +- src/modules/SX126x/SX1262.cpp | 2 +- src/modules/SX126x/SX1262.h | 4 +- src/modules/SX126x/SX1268.cpp | 2 +- src/modules/SX126x/SX1268.h | 4 +- src/modules/SX126x/SX126x.cpp | 52 +++++++++++++------ src/modules/SX126x/SX126x.h | 15 ++++-- 13 files changed, 68 insertions(+), 38 deletions(-) diff --git a/examples/SX126x/SX126x_Channel_Activity_Detection/SX126x_Channel_Activity_Detection.ino b/examples/SX126x/SX126x_Channel_Activity_Detection/SX126x_Channel_Activity_Detection.ino index bc26c988..62981785 100644 --- a/examples/SX126x/SX126x_Channel_Activity_Detection/SX126x_Channel_Activity_Detection.ino +++ b/examples/SX126x/SX126x_Channel_Activity_Detection/SX126x_Channel_Activity_Detection.ino @@ -18,8 +18,9 @@ // SX1262 has the following connections: // NSS pin: 10 // DIO1 pin: 2 +// NRST pin: 3 // BUSY pin: 9 -SX1262 lora = new Module(10, 2, 9); +SX1262 lora = new Module(10, 2, 3, 9); // or using RadioShield // https://github.com/jgromes/RadioShield diff --git a/examples/SX126x/SX126x_FSK_Modem/SX126x_FSK_Modem.ino b/examples/SX126x/SX126x_FSK_Modem/SX126x_FSK_Modem.ino index 333e118f..8efb8e25 100644 --- a/examples/SX126x/SX126x_FSK_Modem/SX126x_FSK_Modem.ino +++ b/examples/SX126x/SX126x_FSK_Modem/SX126x_FSK_Modem.ino @@ -19,8 +19,9 @@ // SX1262 has the following connections: // NSS pin: 10 // DIO1 pin: 2 +// NRST pin: 3 // BUSY pin: 9 -SX1262 fsk = new Module(10, 2, 9); +SX1262 fsk = new Module(10, 2, 3, 9); // or using RadioShield // https://github.com/jgromes/RadioShield diff --git a/examples/SX126x/SX126x_Receive/SX126x_Receive.ino b/examples/SX126x/SX126x_Receive/SX126x_Receive.ino index 4d6a7d34..cef823a6 100644 --- a/examples/SX126x/SX126x_Receive/SX126x_Receive.ino +++ b/examples/SX126x/SX126x_Receive/SX126x_Receive.ino @@ -23,9 +23,9 @@ // SX1262 has the following connections: // NSS pin: 10 // DIO1 pin: 2 -// DIO2 pin: 3 +// NRST pin: 3 // BUSY pin: 9 -SX1262 lora = new Module(10, 2, 9); +SX1262 lora = new Module(10, 2, 3, 9); // or using RadioShield // https://github.com/jgromes/RadioShield diff --git a/examples/SX126x/SX126x_Receive_Interrupt/SX126x_Receive_Interrupt.ino b/examples/SX126x/SX126x_Receive_Interrupt/SX126x_Receive_Interrupt.ino index c4752735..c07fa9c7 100644 --- a/examples/SX126x/SX126x_Receive_Interrupt/SX126x_Receive_Interrupt.ino +++ b/examples/SX126x/SX126x_Receive_Interrupt/SX126x_Receive_Interrupt.ino @@ -24,8 +24,9 @@ // SX1262 has the following connections: // NSS pin: 10 // DIO1 pin: 2 +// NRST pin: 3 // BUSY pin: 9 -SX1262 lora = new Module(10, 2, 9); +SX1262 lora = new Module(10, 2, 3, 9); // or using RadioShield // https://github.com/jgromes/RadioShield diff --git a/examples/SX126x/SX126x_Settings/SX126x_Settings.ino b/examples/SX126x/SX126x_Settings/SX126x_Settings.ino index 7b2e8901..f26a590b 100644 --- a/examples/SX126x/SX126x_Settings/SX126x_Settings.ino +++ b/examples/SX126x/SX126x_Settings/SX126x_Settings.ino @@ -27,14 +27,16 @@ // SX1262 has the following connections: // NSS pin: 10 // DIO1 pin: 2 +// NRST pin: 3 // BUSY pin: 9 -SX1262 loraSX1262 = new Module(10, 2, 9); +SX1262 loraSX1262 = new Module(10, 2, 3, 9); // SX12628 has different connections: // NSS pin: 8 // DIO1 pin: 4 +// NRST pin: 5 // BUSY pin: 6 -SX1268 loraSX1268 = new Module(8, 4, 6); +SX1268 loraSX1268 = new Module(8, 4, 5, 6); // or using RadioShield // https://github.com/jgromes/RadioShield diff --git a/examples/SX126x/SX126x_Transmit/SX126x_Transmit.ino b/examples/SX126x/SX126x_Transmit/SX126x_Transmit.ino index 2394b770..084e11a6 100644 --- a/examples/SX126x/SX126x_Transmit/SX126x_Transmit.ino +++ b/examples/SX126x/SX126x_Transmit/SX126x_Transmit.ino @@ -19,8 +19,9 @@ // SX1262 has the following connections: // NSS pin: 10 // DIO1 pin: 2 +// NRST pin: 3 // BUSY pin: 9 -SX1262 lora = new Module(10, 2, 9); +SX1262 lora = new Module(10, 2, 3, 9); // or using RadioShield // https://github.com/jgromes/RadioShield @@ -35,7 +36,7 @@ void setup() { // bandwidth: 125.0 kHz // spreading factor: 9 // coding rate: 7 - // sync word: 0x1424 (private network) + // sync word: 0x12 (private network) // output power: 14 dBm // current limit: 60 mA // preamble length: 8 symbols diff --git a/examples/SX126x/SX126x_Transmit_Interrupt/SX126x_Transmit_Interrupt.ino b/examples/SX126x/SX126x_Transmit_Interrupt/SX126x_Transmit_Interrupt.ino index 046ebbfd..2996f12c 100644 --- a/examples/SX126x/SX126x_Transmit_Interrupt/SX126x_Transmit_Interrupt.ino +++ b/examples/SX126x/SX126x_Transmit_Interrupt/SX126x_Transmit_Interrupt.ino @@ -20,8 +20,9 @@ // SX1262 has the following connections: // NSS pin: 10 // DIO1 pin: 2 +// NRST pin: 3 // BUSY pin: 9 -SX1262 lora = new Module(10, 2, 9); +SX1262 lora = new Module(10, 2, 3, 9); // save transmission state between loops int transmissionState = ERR_NONE; diff --git a/src/modules/SX126x/SX1262.cpp b/src/modules/SX126x/SX1262.cpp index 12e1c4b8..c7aee4ab 100644 --- a/src/modules/SX126x/SX1262.cpp +++ b/src/modules/SX126x/SX1262.cpp @@ -4,7 +4,7 @@ SX1262::SX1262(Module* mod) : SX126x(mod) { } -int16_t SX1262::begin(float freq, float bw, uint8_t sf, uint8_t cr, uint16_t syncWord, int8_t power, float currentLimit, uint16_t preambleLength, float tcxoVoltage) { +int16_t SX1262::begin(float freq, float bw, uint8_t sf, uint8_t cr, uint8_t syncWord, int8_t power, float currentLimit, uint16_t preambleLength, float tcxoVoltage) { // execute common part int16_t state = SX126x::begin(bw, sf, cr, syncWord, currentLimit, preambleLength, tcxoVoltage); if(state != ERR_NONE) { diff --git a/src/modules/SX126x/SX1262.h b/src/modules/SX126x/SX1262.h index 513bc766..3f3b9cc5 100644 --- a/src/modules/SX126x/SX1262.h +++ b/src/modules/SX126x/SX1262.h @@ -32,7 +32,7 @@ class SX1262: public SX126x { \param cr LoRa coding rate denominator. Defaults to 7 (coding rate 4/7). - \param syncWord 2-byte LoRa sync word. Defaults to SX126X_SYNC_WORD_PRIVATE (0x1424). + \param syncWord 2-byte LoRa sync word. Defaults to SX126X_SYNC_WORD_PRIVATE (0x12). \param power Output power in dBm. Defaults to 14 dBm. @@ -44,7 +44,7 @@ class SX1262: public SX126x { \returns \ref status_codes */ - int16_t begin(float freq = 434.0, float bw = 125.0, uint8_t sf = 9, uint8_t cr = 7, uint16_t syncWord = SX126X_SYNC_WORD_PRIVATE, int8_t power = 14, float currentLimit = 60.0, uint16_t preambleLength = 8, float tcxoVoltage = 1.6); + int16_t begin(float freq = 434.0, float bw = 125.0, uint8_t sf = 9, uint8_t cr = 7, uint8_t syncWord = SX126X_SYNC_WORD_PRIVATE, int8_t power = 14, float currentLimit = 60.0, uint16_t preambleLength = 8, float tcxoVoltage = 1.6); /*! \brief Initialization method for FSK modem. diff --git a/src/modules/SX126x/SX1268.cpp b/src/modules/SX126x/SX1268.cpp index cb064702..f7588165 100644 --- a/src/modules/SX126x/SX1268.cpp +++ b/src/modules/SX126x/SX1268.cpp @@ -4,7 +4,7 @@ SX1268::SX1268(Module* mod) : SX126x(mod) { } -int16_t SX1268::begin(float freq, float bw, uint8_t sf, uint8_t cr, uint16_t syncWord, int8_t power, float currentLimit, uint16_t preambleLength, float tcxoVoltage) { +int16_t SX1268::begin(float freq, float bw, uint8_t sf, uint8_t cr, uint8_t syncWord, int8_t power, float currentLimit, uint16_t preambleLength, float tcxoVoltage) { // execute common part int16_t state = SX126x::begin(bw, sf, cr, syncWord, currentLimit, preambleLength, tcxoVoltage); if(state != ERR_NONE) { diff --git a/src/modules/SX126x/SX1268.h b/src/modules/SX126x/SX1268.h index ccc0ce50..cb481705 100644 --- a/src/modules/SX126x/SX1268.h +++ b/src/modules/SX126x/SX1268.h @@ -35,7 +35,7 @@ class SX1268: public SX126x { \param cr LoRa coding rate denominator. Defaults to 7 (coding rate 4/7). - \param syncWord 2-byte LoRa sync word. Defaults to SX126X_SYNC_WORD_PRIVATE (0x1424). + \param syncWord 2-byte LoRa sync word. Defaults to SX126X_SYNC_WORD_PRIVATE (0x12). \param power Output power in dBm. Defaults to 14 dBm. @@ -47,7 +47,7 @@ class SX1268: public SX126x { \returns \ref status_codes */ - int16_t begin(float freq = 434.0, float bw = 125.0, uint8_t sf = 9, uint8_t cr = 7, uint16_t syncWord = SX126X_SYNC_WORD_PRIVATE, int8_t power = 14, float currentLimit = 60.0, uint16_t preambleLength = 8, float tcxoVoltage = 1.6); + int16_t begin(float freq = 434.0, float bw = 125.0, uint8_t sf = 9, uint8_t cr = 7, uint8_t syncWord = SX126X_SYNC_WORD_PRIVATE, int8_t power = 14, float currentLimit = 60.0, uint16_t preambleLength = 8, float tcxoVoltage = 1.6); /*! \brief Initialization method for FSK modem. diff --git a/src/modules/SX126x/SX126x.cpp b/src/modules/SX126x/SX126x.cpp index 901eb1a0..8dc93081 100644 --- a/src/modules/SX126x/SX126x.cpp +++ b/src/modules/SX126x/SX126x.cpp @@ -4,9 +4,14 @@ SX126x::SX126x(Module* mod) : PhysicalLayer(SX126X_CRYSTAL_FREQ, SX126X_DIV_EXPO _mod = mod; } -int16_t SX126x::begin(float bw, uint8_t sf, uint8_t cr, uint16_t syncWord, float currentLimit, uint16_t preambleLength, float tcxoVoltage) { +int16_t SX126x::begin(float bw, uint8_t sf, uint8_t cr, uint8_t syncWord, float currentLimit, uint16_t preambleLength, float tcxoVoltage) { // set module properties - _mod->init(RADIOLIB_USE_SPI, RADIOLIB_INT_BOTH); + _mod->init(RADIOLIB_USE_SPI); + Module::pinMode(_mod->getIrq(), INPUT); + Module::pinMode(_mod->getGpio(), INPUT); + + // reset the module + reset(); // BW in kHz and SF are required in order to calculate LDRO for setModulationParams _bwKhz = bw; @@ -79,7 +84,11 @@ int16_t SX126x::begin(float bw, uint8_t sf, uint8_t cr, uint16_t syncWord, float int16_t SX126x::beginFSK(float br, float freqDev, float rxBw, float currentLimit, uint16_t preambleLength, float dataShaping, float tcxoVoltage) { // set module properties - _mod->init(RADIOLIB_USE_SPI, RADIOLIB_INT_BOTH); + _mod->init(RADIOLIB_USE_SPI); + Module::pinMode(_mod->getIrq(), INPUT); + + // reset the module + reset(); // initialize configuration variables (will be overwritten during public settings configuration) _br = 21333; // 48.0 kbps @@ -159,6 +168,15 @@ int16_t SX126x::beginFSK(float br, float freqDev, float rxBw, float currentLimit return(state); } +void SX126x::reset() { + Module::pinMode(_mod->getRst(), OUTPUT); + Module::digitalWrite(_mod->getRst(), LOW); + delay(100); + Module::digitalWrite(_mod->getRst(), HIGH); + Module::pinMode(_mod->getRst(), INPUT); + delay(100); +} + int16_t SX126x::transmit(uint8_t* data, size_t len, uint8_t addr) { // set mode to standby int16_t state = standby(); @@ -199,7 +217,7 @@ int16_t SX126x::transmit(uint8_t* data, size_t len, uint8_t addr) { // wait for packet transmission or timeout uint32_t start = micros(); - while(!digitalRead(_mod->getInt0())) { + while(!digitalRead(_mod->getIrq())) { if(micros() - start > timeout) { clearIrqStatus(); return(ERR_TX_TIMEOUT); @@ -264,7 +282,7 @@ int16_t SX126x::receive(uint8_t* data, size_t len) { // wait for packet reception or timeout uint32_t start = micros(); - while(!digitalRead(_mod->getInt0())) { + while(!digitalRead(_mod->getIrq())) { if(micros() - start > timeout) { fixImplicitTimeout(); clearIrqStatus(); @@ -333,7 +351,7 @@ int16_t SX126x::scanChannel() { } // wait for channel activity detected or timeout - while(!digitalRead(_mod->getInt0())); + while(!digitalRead(_mod->getIrq())); // check CAD result uint16_t cadResult = getIrqStatus(); @@ -370,7 +388,7 @@ int16_t SX126x::standby(uint8_t mode) { } void SX126x::setDio1Action(void (*func)(void)) { - attachInterrupt(digitalPinToInterrupt(_mod->getInt0()), func, RISING); + attachInterrupt(digitalPinToInterrupt(_mod->getIrq()), func, RISING); } int16_t SX126x::startTransmit(uint8_t* data, size_t len, uint8_t addr) { @@ -438,7 +456,7 @@ int16_t SX126x::startTransmit(uint8_t* data, size_t len, uint8_t addr) { } // wait for BUSY to go low (= PA ramp up done) - while(digitalRead(_mod->getInt1())); + while(digitalRead(_mod->getGpio())); return(state); } @@ -660,14 +678,14 @@ int16_t SX126x::setCodingRate(uint8_t cr) { return(setModulationParams(_sf, _bw, _cr)); } -int16_t SX126x::setSyncWord(uint16_t syncWord) { +int16_t SX126x::setSyncWord(uint8_t syncWord) { // check active modem if(getPacketType() != SX126X_PACKET_TYPE_LORA) { return(ERR_WRONG_MODEM); } // update register - uint8_t data[2] = {(uint8_t)((syncWord >> 8) & 0xFF), (uint8_t)(syncWord & 0xFF)}; + uint8_t data[2] = {(uint8_t)((syncWord & 0xF0) | 0x04), (uint8_t)(((syncWord & 0x0F) << 4) | 0x04)}; return(writeRegister(SX126X_REG_LORA_SYNC_WORD_MSB, data, 2)); } @@ -1501,7 +1519,7 @@ int16_t SX126x::config(uint8_t modem) { // wait for calibration completion delay(5); - while(digitalRead(_mod->getInt1())); + while(digitalRead(_mod->getGpio())); return(ERR_NONE); } @@ -1537,7 +1555,7 @@ int16_t SX126x::SPItransfer(uint8_t* cmd, uint8_t cmdLen, bool write, uint8_t* d // ensure BUSY is low (state meachine ready) RADIOLIB_VERBOSE_PRINTLN(F("Wait for BUSY ... ")); uint32_t start = millis(); - while(digitalRead(_mod->getInt1())) { + while(digitalRead(_mod->getGpio())) { if(millis() - start >= timeout) { return(ERR_SPI_CMD_TIMEOUT); } @@ -1604,11 +1622,11 @@ int16_t SX126x::SPItransfer(uint8_t* cmd, uint8_t cmdLen, bool write, uint8_t* d if(waitForBusy) { delayMicroseconds(1); start = millis(); - while(digitalRead(_mod->getInt1())) { - if(millis() - start >= timeout) { - status = SX126X_STATUS_CMD_TIMEOUT; - break; - } + while(digitalRead(_mod->getGpio())) { + if(millis() - start >= timeout) { + status = SX126X_STATUS_CMD_TIMEOUT; + break; + } } } diff --git a/src/modules/SX126x/SX126x.h b/src/modules/SX126x/SX126x.h index 10e1184d..52d486f0 100644 --- a/src/modules/SX126x/SX126x.h +++ b/src/modules/SX126x/SX126x.h @@ -325,8 +325,8 @@ // SX126X SPI register variables //SX126X_REG_LORA_SYNC_WORD_MSB + LSB -#define SX126X_SYNC_WORD_PUBLIC 0x3444 -#define SX126X_SYNC_WORD_PRIVATE 0x1424 +#define SX126X_SYNC_WORD_PUBLIC 0x34 // actually 0x3444 NOTE: The low nibbles in each byte (0x_4_4) are masked out since apparently, they're reserved. +#define SX126X_SYNC_WORD_PRIVATE 0x12 // actually 0x1424 You couldn't make this up if you tried. /*! @@ -361,7 +361,7 @@ class SX126x: public PhysicalLayer { \param cr LoRa coding rate denominator. Allowed values range from 5 to 8. - \param syncWord 2-byte LoRa sync word. + \param syncWord 1-byte LoRa sync word. \param currentLimit Current protection limit in mA. @@ -371,7 +371,7 @@ class SX126x: public PhysicalLayer { \returns \ref status_codes */ - int16_t begin(float bw, uint8_t sf, uint8_t cr, uint16_t syncWord, float currentLimit, uint16_t preambleLength, float tcxoVoltage); + int16_t begin(float bw, uint8_t sf, uint8_t cr, uint8_t syncWord, float currentLimit, uint16_t preambleLength, float tcxoVoltage); /*! \brief Initialization method for FSK modem. @@ -394,6 +394,11 @@ class SX126x: public PhysicalLayer { */ int16_t beginFSK(float br, float freqDev, float rxBw, float currentLimit, uint16_t preambleLength, float dataShaping, float tcxoVoltage); + /*! + \brief Reset method. Will reset the chip to the default state using RST pin. + */ + void reset(); + /*! \brief Blocking binary transmit method. Overloads for string-based transmissions are implemented in PhysicalLayer. @@ -571,7 +576,7 @@ class SX126x: public PhysicalLayer { \returns \ref status_codes */ - int16_t setSyncWord(uint16_t syncWord); + int16_t setSyncWord(uint8_t syncWord); /*! \brief Sets current protection limit. Can be set in 0.25 mA steps.