diff --git a/API.md b/API.md index 07c808e..624a0f7 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()`. diff --git a/src/LoRa.cpp b/src/LoRa.cpp index 86e29e0..b7909ec 100644 --- a/src/LoRa.cpp +++ b/src/LoRa.cpp @@ -52,6 +52,7 @@ LoRaClass::LoRaClass() : _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), @@ -97,7 +98,7 @@ int LoRaClass::begin(long frequency) } // start SPI - LORA_DEFAULT_SPI.begin(); + _spi->begin(); // check version uint8_t version = readRegister(REG_VERSION); @@ -136,7 +137,7 @@ void LoRaClass::end() sleep(); // stop SPI - LORA_DEFAULT_SPI.end(); + _spi->end(); } int LoRaClass::beginPacket(int implicitHeader) @@ -457,6 +458,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); @@ -528,10 +534,10 @@ uint8_t LoRaClass::singleTransfer(uint8_t address, uint8_t value) digitalWrite(_ss, LOW); - LORA_DEFAULT_SPI.beginTransaction(_spiSettings); - LORA_DEFAULT_SPI.transfer(address); - response = LORA_DEFAULT_SPI.transfer(value); - LORA_DEFAULT_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 ff20251..6653e5a 100644 --- a/src/LoRa.h +++ b/src/LoRa.h @@ -71,6 +71,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); @@ -89,6 +90,7 @@ private: private: SPISettings _spiSettings; + SPIClass* _spi; int _ss; int _reset; int _dio0;