Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions examples/rx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,27 @@
#include "lora.h"
#include "packet.h"

#define SPI_BUS 1
#define SPI_CHANNEL 0
#define SS_PIN 6
#define DIO0_PIN 7
#define RST_PIN 0
#define SS_PIN 27 // CE2, GPIO16
#define DIO0_PIN 21 // GPIO5, interrupt
#define RST_PIN 6 // GPIO25

int main() {
printf("Setting up LoRa\n");
LoRa lora(SPI_CHANNEL, SS_PIN, DIO0_PIN, RST_PIN);
LoRa lora(SPI_BUS, SPI_CHANNEL, SS_PIN, DIO0_PIN, RST_PIN);
if (lora.begin()) {
printf("LoRa setup successful: chipset version 0x%02x\n", lora.version());
printf("Configuring radio\n");
lora.setFrequency(LoRa::FREQ_433)
->setTXPower(17)
->setSpreadFactor(LoRa::SF_12)
->setBandwidth(LoRa::BW_125k)
->setCodingRate(LoRa::CR_48)
->setSyncWord(0x12)
->setHeaderMode(LoRa::HM_EXPLICIT)
->enableCRC();
lora.setFrequency(869618000)
->setTXPower(20)
->setSpreadFactor(LoRa::SF_8)
->setBandwidth(LoRa::BW_62k5)
->setCodingRate(LoRa::CR_48)
->setSyncWord(0x12)
->setHeaderMode(LoRa::HM_EXPLICIT)
->enableCRC()
->setPreambleLength(16);
printf(" TX power : %d dB\n", lora.getTXPower());
printf(" Frequency : %d Hz\n", lora.getFrequency());
printf(" Spread factor: %d\n", lora.getSpreadFactor());
Expand All @@ -39,4 +41,4 @@ int main() {
printf(" Payload : \n%s\n", p.getPayload());
}
}
}
}
27 changes: 14 additions & 13 deletions examples/tx.cpp
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
#include <cstdio>
#include "lora.h"

#define SPI_BUS 1
#define SPI_CHANNEL 0
#define SS_PIN 6
#define DIO0_PIN 7
#define RST_PIN 0
#define SS_PIN 27 // CE2, GPIO16
#define DIO0_PIN 21 // GPIO5, interrupt
#define RST_PIN 6 // GPIO25

static const char *message = "{'hello': ['world']}";
int main() {
printf("Setting up LoRa\n");
LoRa lora(SPI_CHANNEL, SS_PIN, DIO0_PIN, RST_PIN);
LoRa lora(SPI_BUS, SPI_CHANNEL, SS_PIN, DIO0_PIN, RST_PIN);
if (lora.begin()) {
printf("LoRa setup successful: chipset version 0x%02x\n", lora.version());
LoRaPacket p((unsigned char *)message, strlen(message));
printf("Constructed packet: payload='%s', length=%d\n", p.getPayload(), p.payloadLength());
printf("Configuring radio\n");
lora.setFrequency(LoRa::FREQ_433)
->setTXPower(17)
->setSpreadFactor(LoRa::SF_12)
->setBandwidth(LoRa::BW_125k)
->setCodingRate(LoRa::CR_48)
->setSyncWord(0x12)
->setHeaderMode(LoRa::HM_EXPLICIT)
->enableCRC();
lora.setFrequency(869618000)
->setTXPower(17)
->setSpreadFactor(LoRa::SF_8)
->setBandwidth(LoRa::BW_62k5)
->setCodingRate(LoRa::CR_48)
->setSyncWord(0x12)
->setHeaderMode(LoRa::HM_EXPLICIT)
->enableCRC();
printf(" TX power : %d dB\n", lora.getTXPower());
printf(" Frequency : %d Hz\n", lora.getFrequency());
printf(" Spread factor: %d\n", lora.getSpreadFactor());
Expand All @@ -34,4 +35,4 @@ int main() {
size_t bytes = lora.transmitPacket(&p);
printf(" %d bytes transmitted\n", bytes);
}
}
}
7 changes: 5 additions & 2 deletions src/include/lora.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
class LoRa {
private:
unsigned char _spibuf[2];
uint8_t _bus;
uint8_t _spi_channel;
uint8_t _ss_pin;
uint8_t _dio0_pin;
Expand Down Expand Up @@ -151,7 +152,7 @@ class LoRa {
LNA_AGC
};

LoRa(uint8_t, uint8_t, uint8_t, uint8_t);
LoRa(uint8_t, uint8_t, uint8_t, uint8_t, uint8_t);
LoRa *setSpreadFactor(sf_t);
LoRa *setFrequency(uint32_t);
LoRa *setBandwidth(bw_t);
Expand All @@ -161,6 +162,7 @@ class LoRa {
LoRa *setSyncWord(uint8_t);
LoRa *setLNAGain(lna_gain_t);
LoRa *setLNABoost(bool);
LoRa *setPreambleLength(uint16_t length);
LoRa *enableCRC();
LoRa *disableCRC();
sf_t getSpreadFactor();
Expand All @@ -173,6 +175,7 @@ class LoRa {
lna_gain_t getLNAGain();
bool getLNABoost();
int getFrequencyError();
uint16_t getPreambleLength();

bool begin();
void sleep();
Expand All @@ -183,4 +186,4 @@ class LoRa {
size_t transmitPacket(LoRaPacket *);
};

#endif
#endif
35 changes: 24 additions & 11 deletions src/lora.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ const uint32_t LoRa::bw[10] = {
500000
};

LoRa::LoRa(uint8_t spi, uint8_t ss, uint8_t dio0, uint8_t rst) : _spi_channel(spi),
_ss_pin(ss),
_dio0_pin(dio0),
_rst_pin(rst) {}
LoRa::LoRa(uint8_t bus, uint8_t spi, uint8_t ss, uint8_t dio0, uint8_t rst) :
_bus(bus),
_spi_channel(spi),
_ss_pin(ss),
_dio0_pin(dio0),
_rst_pin(rst) {}

LoRa *LoRa::setSpreadFactor(LoRa::sf_t sf)
{
Expand Down Expand Up @@ -87,6 +89,18 @@ LoRa::bw_t LoRa::getBandwidth()
return (LoRa::bw_t)(readRegister(REG_MODEM_CONFIG1) >> 4);
}

LoRa *LoRa::setPreambleLength(uint16_t length)
{
writeRegister(REG_PREAMBLE_MSB, length >> 8);
writeRegister(REG_PREAMBLE_LSB, length);
return this;
}

uint16_t LoRa::getPreambleLength()
{
return (readRegister(REG_PREAMBLE_MSB) << 8) | readRegister(REG_PREAMBLE_LSB);
}

LoRa *LoRa::setTXPower(uint8_t p)
{
p = p < 2
Expand Down Expand Up @@ -177,7 +191,7 @@ LoRa::lna_gain_t LoRa::getLNAGain()

LoRa *LoRa::setLNABoost(bool boost)
{
writeRegister(REG_LNA, (readRegister(REG_LNA) & 0xFA) | (boost ? 0x03 : 0x00));
writeRegister(REG_LNA, readRegister(REG_LNA) | (boost ? 0x03 : 0x00));
return this;
}

Expand All @@ -203,7 +217,7 @@ uint8_t LoRa::readRegister(uint8_t addr)
_spibuf[0] = addr & 0x7f;
_spibuf[1] = 0x00;
selectReceiver();
wiringPiSPIDataRW(_spi_channel, _spibuf, 2);
wiringPiSPIxDataRW(_bus, _spi_channel, _spibuf, 2);
deselectReceiver();
return _spibuf[1];
}
Expand All @@ -213,14 +227,13 @@ void LoRa::writeRegister(uint8_t addr, uint8_t val)
_spibuf[0] = addr | 0x80;
_spibuf[1] = val;
selectReceiver();
wiringPiSPIDataRW(_spi_channel, _spibuf, 2);
wiringPiSPIxDataRW(_bus, _spi_channel, _spibuf, 2);
deselectReceiver();
}

void LoRa::setOpMode(uint8_t mode)
{
// Preserve AccessSharedReg and LowFrequencyModeOn
writeRegister(REG_OPMODE, (readRegister(REG_OPMODE) & 0xC8) | OPMODE_LORA | mode);
writeRegister(REG_OPMODE, OPMODE_LORA | mode);
}

uint8_t LoRa::getOpMode()
Expand Down Expand Up @@ -260,7 +273,7 @@ bool LoRa::begin()
pinMode(_ss_pin, OUTPUT);
pinMode(_dio0_pin, INPUT);
pinMode(_rst_pin, OUTPUT);
wiringPiSPISetup(_spi_channel, 500000);
wiringPiSPIxSetupMode(_bus, _spi_channel, 500000, 0);
sleep();
if (version() != 0x12)
{
Expand Down Expand Up @@ -323,7 +336,7 @@ size_t LoRa::write(const uint8_t *data, size_t size)
buf[i + 1] = data[i];
}
selectReceiver();
int bytes = wiringPiSPIDataRW(_spi_channel, buf, size + 1);
int bytes = wiringPiSPIxDataRW(_bus, _spi_channel, buf, size + 1);
deselectReceiver();
writeRegister(REG_PAYLOAD_LENGTH, currentLength + size);
free(buf);
Expand Down