Add new LoRa.setSPI(...) API to use radio with a different SPI interface

mkr-wan-1300
Sandeep Mistry 2018-02-16 13:15:33 -05:00
parent 678a64b2d1
commit e1889c78ce
3 changed files with 25 additions and 6 deletions

11
API.md
View File

@ -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()`.

View File

@ -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);

View File

@ -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;