From 45764ce7990416e2240da932e33aa6fbb091e1e8 Mon Sep 17 00:00:00 2001 From: Folkert van Heusden Date: Sun, 30 Nov 2025 19:33:47 +0100 Subject: [PATCH] several fixes and enhancements: - allow the SPI bus to be selected (for RPI5) - allow the preamble length to be set - setLNABoost would not mask off the correct bits --- examples/rx.cpp | 28 +++++++++++++++------------- examples/tx.cpp | 27 ++++++++++++++------------- src/include/lora.h | 7 +++++-- src/lora.cpp | 35 ++++++++++++++++++++++++----------- 4 files changed, 58 insertions(+), 39 deletions(-) diff --git a/examples/rx.cpp b/examples/rx.cpp index 264553c..70631dd 100644 --- a/examples/rx.cpp +++ b/examples/rx.cpp @@ -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()); @@ -39,4 +41,4 @@ int main() { printf(" Payload : \n%s\n", p.getPayload()); } } -} \ No newline at end of file +} diff --git a/examples/tx.cpp b/examples/tx.cpp index 84cf2f0..d7a4d95 100644 --- a/examples/tx.cpp +++ b/examples/tx.cpp @@ -1,28 +1,29 @@ #include #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()); @@ -34,4 +35,4 @@ int main() { size_t bytes = lora.transmitPacket(&p); printf(" %d bytes transmitted\n", bytes); } -} \ No newline at end of file +} diff --git a/src/include/lora.h b/src/include/lora.h index 5ee8ca0..aa3f329 100644 --- a/src/include/lora.h +++ b/src/include/lora.h @@ -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; @@ -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); @@ -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(); @@ -173,6 +175,7 @@ class LoRa { lna_gain_t getLNAGain(); bool getLNABoost(); int getFrequencyError(); + uint16_t getPreambleLength(); bool begin(); void sleep(); @@ -183,4 +186,4 @@ class LoRa { size_t transmitPacket(LoRaPacket *); }; -#endif \ No newline at end of file +#endif diff --git a/src/lora.cpp b/src/lora.cpp index 3fd9a16..3558c38 100644 --- a/src/lora.cpp +++ b/src/lora.cpp @@ -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) { @@ -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 @@ -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; } @@ -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]; } @@ -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() @@ -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) { @@ -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);