From 039b138d8006ecf66953deafb8fe9478a0a66b35 Mon Sep 17 00:00:00 2001 From: Sandeep Mistry Date: Mon, 7 May 2018 13:02:06 -0400 Subject: [PATCH] Add support for Arduino MKR WAN 1300 boards (#105) * Add support for Arduino MKR WAN 1300 board * Add new LoRa.setSPI(...) API to use radio with a different SPI interface * Disable LoRa.onReceive(...) and LoRa.receive() on Arduino MKR WAN 1300 * Add errors on sketches not compatible with the Arduino MKR WAN 1300 --- .travis.yml | 9 ++++- API.md | 13 +++++++ README.md | 3 ++ .../LoRaDuplexCallback/LoRaDuplexCallback.ino | 4 ++ .../LoRaReceiverCallback.ino | 4 ++ src/LoRa.cpp | 39 +++++++++++++++---- src/LoRa.h | 24 +++++++++--- 7 files changed, 82 insertions(+), 14 deletions(-) diff --git a/.travis.yml b/.travis.yml index 2dcac81..8a9cb12 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,6 +9,7 @@ env: - BOARD="arduino:samd:arduino_zero_edbg" - BOARD="arduino:samd:mkr1000" - BOARD="arduino:samd:mkrzero" + - BOARD="arduino:samd:mkrwan1300" before_install: - wget http://downloads.arduino.cc/arduino-$IDE_VERSION-linux64.tar.xz - tar xf arduino-$IDE_VERSION-linux64.tar.xz @@ -24,9 +25,13 @@ install: script: - buildExampleSketch LoRaDumpRegisters - buildExampleSketch LoRaDuplex - - buildExampleSketch LoRaDuplexCallback + - if [[ "$BOARD" != "arduino:samd:mkrwan1300" ]]; then + buildExampleSketch LoRaDuplexCallback; + fi - buildExampleSketch LoRaReceiver - - buildExampleSketch LoRaReceiverCallback + - if [[ "$BOARD" != "arduino:samd:mkrwan1300" ]]; then + buildExampleSketch LoRaReceiverCallback; + fi - buildExampleSketch LoRaSender - buildExampleSketch LoRaSetSpread - buildExampleSketch LoRaSetSyncWord diff --git a/API.md b/API.md index 0e4a8e9..a9a89b5 100644 --- a/API.md +++ b/API.md @@ -38,6 +38,17 @@ To save further pins one could connect the reset pin of the MCU with reset pin o * `reset` - set to `-1` to omit this pin +### Set SPI interface + +Override the default SPI interface used by the library. **Must** be called before `LoRa.begin()`. + +```arduino +LoRa.setSPI(spi); +``` + * `spi` - new SPI interface to use, defaults to `SPI` + +This call is optional and only needs to be used if you need to change the default SPI interface used, in the case your Arduino (or compatible) board has more than one SPI interface present. + ### Set SPI Frequency Override the default SPI frequency of 10 MHz used by the library. **Must** be called before `LoRa.begin()`. @@ -122,6 +133,8 @@ Returns the packet size in bytes or `0` if no packet was received. ### Continuous receive mode +**WARNING**: Not supported on the Arduino MKR WAN 1300 board! + #### Register callback Register a callback function for when a packet is received. diff --git a/README.md b/README.md index da9446e..f527971 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,9 @@ An [Arduino](https://arduino.cc/) library for sending and receiving data using [ * [Dragino Lora Shield](http://www.dragino.com/products/module/item/102-lora-shield.html) * [HopeRF](http://www.hoperf.com/rf_transceiver/lora/) [RFM95W](http://www.hoperf.com/rf_transceiver/lora/RFM95W.html), [RFM96W](http://www.hoperf.com/rf_transceiver/lora/RFM96W.html), and [RFM98W](http://www.hoperf.com/rf_transceiver/lora/RFM98W.html) * [Modtronix](http://modtronix.com/) [inAir4](http://modtronix.com/inair4.html), [inAir9](http://modtronix.com/inair9.html), and [inAir9B](http://modtronix.com/inair9b.html) + * [Arduino MKR WAN 1300](https://store.arduino.cc/usa/mkr-wan-1300) + * **NOTE:** Requires firmware v1.1.6 or later on the on-board Murata module + * **WARNING**: [LoRa.onReceive(...)](https://github.com/sandeepmistry/arduino-LoRa/blob/master/API.md#register-callback) and [LoRa.recieve()](https://github.com/sandeepmistry/arduino-LoRa/blob/master/API.md#receive-mode) is not compatible with this board! ### Semtech SX1276/77/78/79 wiring diff --git a/examples/LoRaDuplexCallback/LoRaDuplexCallback.ino b/examples/LoRaDuplexCallback/LoRaDuplexCallback.ino index 2385188..0511f3e 100644 --- a/examples/LoRaDuplexCallback/LoRaDuplexCallback.ino +++ b/examples/LoRaDuplexCallback/LoRaDuplexCallback.ino @@ -15,6 +15,10 @@ #include // include libraries #include +#ifdef ARDUINO_SAMD_MKRWAN1300 +#error "This example is not compatible with the Arduino MKR WAN 1300 board!" +#endif + const int csPin = 7; // LoRa radio chip select const int resetPin = 6; // LoRa radio reset const int irqPin = 1; // change for your board; must be a hardware interrupt pin diff --git a/examples/LoRaReceiverCallback/LoRaReceiverCallback.ino b/examples/LoRaReceiverCallback/LoRaReceiverCallback.ino index 1920c5d..e227e23 100644 --- a/examples/LoRaReceiverCallback/LoRaReceiverCallback.ino +++ b/examples/LoRaReceiverCallback/LoRaReceiverCallback.ino @@ -1,6 +1,10 @@ #include #include +#ifdef ARDUINO_SAMD_MKRWAN1300 +#error "This example is not compatible with the Arduino MKR WAN 1300 board!" +#endif + void setup() { Serial.begin(9600); while (!Serial); diff --git a/src/LoRa.cpp b/src/LoRa.cpp index c026d5f..4296e76 100644 --- a/src/LoRa.cpp +++ b/src/LoRa.cpp @@ -54,7 +54,8 @@ #define MAX_PKT_LENGTH 255 LoRaClass::LoRaClass() : - _spiSettings(8E6, MSBFIRST, SPI_MODE0), + _spiSettings(LORA_DEFAULT_SPI_FREQUENCY, MSBFIRST, SPI_MODE0), + _spi(&LORA_DEFAULT_SPI), _ss(LORA_DEFAULT_SS_PIN), _reset(LORA_DEFAULT_RESET_PIN), _dio0(LORA_DEFAULT_DIO0_PIN), _frequency(0), _packetIndex(0), @@ -67,6 +68,23 @@ LoRaClass::LoRaClass() : int LoRaClass::begin(long frequency) { +#ifdef ARDUINO_SAMD_MKRWAN1300 + pinMode(LORA_IRQ_DUMB, OUTPUT); + digitalWrite(LORA_IRQ_DUMB, LOW); + + // Hardware reset + pinMode(LORA_BOOT0, OUTPUT); + digitalWrite(LORA_BOOT0, LOW); + + pinMode(LORA_RESET, OUTPUT); + digitalWrite(LORA_RESET, HIGH); + delay(200); + digitalWrite(LORA_RESET, LOW); + delay(200); + digitalWrite(LORA_RESET, HIGH); + delay(50); +#endif + // setup pins pinMode(_ss, OUTPUT); // set SS high @@ -83,7 +101,7 @@ int LoRaClass::begin(long frequency) } // start SPI - SPI.begin(); + _spi->begin(); // check version uint8_t version = readRegister(REG_VERSION); @@ -122,7 +140,7 @@ void LoRaClass::end() sleep(); // stop SPI - SPI.end(); + _spi->end(); } int LoRaClass::beginPacket(int implicitHeader) @@ -296,6 +314,7 @@ void LoRaClass::flush() { } +#ifndef ARDUINO_SAMD_MKRWAN1300 void LoRaClass::onReceive(void(*callback)(int)) { _onReceive = callback; @@ -328,6 +347,7 @@ void LoRaClass::receive(int size) writeRegister(REG_OP_MODE, MODE_LONG_RANGE_MODE | MODE_RX_CONTINUOUS); } +#endif void LoRaClass::idle() { @@ -504,6 +524,11 @@ void LoRaClass::setPins(int ss, int reset, int dio0) _dio0 = dio0; } +void LoRaClass::setSPI(SPIClass& spi) +{ + _spi = &spi; +} + void LoRaClass::setSPIFrequency(uint32_t frequency) { _spiSettings = SPISettings(frequency, MSBFIRST, SPI_MODE0); @@ -575,10 +600,10 @@ uint8_t LoRaClass::singleTransfer(uint8_t address, uint8_t value) digitalWrite(_ss, LOW); - SPI.beginTransaction(_spiSettings); - SPI.transfer(address); - response = SPI.transfer(value); - SPI.endTransaction(); + _spi->beginTransaction(_spiSettings); + _spi->transfer(address); + response = _spi->transfer(value); + _spi->endTransaction(); digitalWrite(_ss, HIGH); diff --git a/src/LoRa.h b/src/LoRa.h index 46c2880..248a988 100644 --- a/src/LoRa.h +++ b/src/LoRa.h @@ -7,12 +7,22 @@ #include #include -#define LORA_DEFAULT_SS_PIN 10 -#define LORA_DEFAULT_RESET_PIN 9 -#define LORA_DEFAULT_DIO0_PIN 2 +#ifdef ARDUINO_SAMD_MKRWAN1300 +#define LORA_DEFAULT_SPI SPI1 +#define LORA_DEFAULT_SPI_FREQUENCY 250000 +#define LORA_DEFAULT_SS_PIN LORA_IRQ_DUMB +#define LORA_DEFAULT_RESET_PIN -1 +#define LORA_DEFAULT_DIO0_PIN -1 +#else +#define LORA_DEFAULT_SPI SPI +#define LORA_DEFAULT_SPI_FREQUENCY 8E6 +#define LORA_DEFAULT_SS_PIN 10 +#define LORA_DEFAULT_RESET_PIN 9 +#define LORA_DEFAULT_DIO0_PIN 2 +#endif -#define PA_OUTPUT_RFO_PIN 0 -#define PA_OUTPUT_PA_BOOST_PIN 1 +#define PA_OUTPUT_RFO_PIN 0 +#define PA_OUTPUT_PA_BOOST_PIN 1 class LoRaClass : public Stream { public: @@ -39,9 +49,11 @@ public: virtual int peek(); virtual void flush(); +#ifndef ARDUINO_SAMD_MKRWAN1300 void onReceive(void(*callback)(int)); void receive(int size = 0); +#endif void idle(); void sleep(); @@ -62,6 +74,7 @@ public: byte random(); void setPins(int ss = LORA_DEFAULT_SS_PIN, int reset = LORA_DEFAULT_RESET_PIN, int dio0 = LORA_DEFAULT_DIO0_PIN); + void setSPI(SPIClass& spi); void setSPIFrequency(uint32_t frequency); void dumpRegisters(Stream& out); @@ -85,6 +98,7 @@ private: private: SPISettings _spiSettings; + SPIClass* _spi; int _ss; int _reset; int _dio0;