Skip to content

Commit e1889c7

Browse files
committed
Add new LoRa.setSPI(...) API to use radio with a different SPI interface
1 parent 678a64b commit e1889c7

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

API.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,17 @@ To save further pins one could connect the reset pin of the MCU with reset pin o
3838

3939
* `reset` - set to `-1` to omit this pin
4040

41+
### Set SPI interface
42+
43+
Override the default SPI interface used by the library. **Must** be called before `LoRa.begin()`.
44+
45+
```arduino
46+
LoRa.setSPI(spi);
47+
```
48+
* `spi` - new SPI interface to use, defaults to `SPI`
49+
50+
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.
51+
4152
### Set SPI Frequency
4253

4354
Override the default SPI frequency of 10 MHz used by the library. **Must** be called before `LoRa.begin()`.

src/LoRa.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252

5353
LoRaClass::LoRaClass() :
5454
_spiSettings(LORA_DEFAULT_SPI_FREQUENCY, MSBFIRST, SPI_MODE0),
55+
_spi(&LORA_DEFAULT_SPI),
5556
_ss(LORA_DEFAULT_SS_PIN), _reset(LORA_DEFAULT_RESET_PIN), _dio0(LORA_DEFAULT_DIO0_PIN),
5657
_frequency(0),
5758
_packetIndex(0),
@@ -97,7 +98,7 @@ int LoRaClass::begin(long frequency)
9798
}
9899

99100
// start SPI
100-
LORA_DEFAULT_SPI.begin();
101+
_spi->begin();
101102

102103
// check version
103104
uint8_t version = readRegister(REG_VERSION);
@@ -136,7 +137,7 @@ void LoRaClass::end()
136137
sleep();
137138

138139
// stop SPI
139-
LORA_DEFAULT_SPI.end();
140+
_spi->end();
140141
}
141142

142143
int LoRaClass::beginPacket(int implicitHeader)
@@ -457,6 +458,11 @@ void LoRaClass::setPins(int ss, int reset, int dio0)
457458
_dio0 = dio0;
458459
}
459460

461+
void LoRaClass::setSPI(SPIClass& spi)
462+
{
463+
_spi = &spi;
464+
}
465+
460466
void LoRaClass::setSPIFrequency(uint32_t frequency)
461467
{
462468
_spiSettings = SPISettings(frequency, MSBFIRST, SPI_MODE0);
@@ -528,10 +534,10 @@ uint8_t LoRaClass::singleTransfer(uint8_t address, uint8_t value)
528534

529535
digitalWrite(_ss, LOW);
530536

531-
LORA_DEFAULT_SPI.beginTransaction(_spiSettings);
532-
LORA_DEFAULT_SPI.transfer(address);
533-
response = LORA_DEFAULT_SPI.transfer(value);
534-
LORA_DEFAULT_SPI.endTransaction();
537+
_spi->beginTransaction(_spiSettings);
538+
_spi->transfer(address);
539+
response = _spi->transfer(value);
540+
_spi->endTransaction();
535541

536542
digitalWrite(_ss, HIGH);
537543

src/LoRa.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ class LoRaClass : public Stream {
7171
byte random();
7272

7373
void setPins(int ss = LORA_DEFAULT_SS_PIN, int reset = LORA_DEFAULT_RESET_PIN, int dio0 = LORA_DEFAULT_DIO0_PIN);
74+
void setSPI(SPIClass& spi);
7475
void setSPIFrequency(uint32_t frequency);
7576

7677
void dumpRegisters(Stream& out);
@@ -89,6 +90,7 @@ class LoRaClass : public Stream {
8990

9091
private:
9192
SPISettings _spiSettings;
93+
SPIClass* _spi;
9294
int _ss;
9395
int _reset;
9496
int _dio0;

0 commit comments

Comments
 (0)