-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[example] Adding SX128x LoRa example
- Loading branch information
1 parent
000868a
commit 3a3100a
Showing
2 changed files
with
294 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,279 @@ | ||
// coding: utf-8 | ||
/* | ||
* Copyright (c) 2023, Rasmus Kleist Hørlyck Sørensen | ||
* | ||
* This file is part of the modm project. | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
// ---------------------------------------------------------------------------- | ||
|
||
#include <modm/board.hpp> | ||
#include <modm/debug/logger/logger.hpp> | ||
#include <modm/driver/radio/sx128x.hpp> | ||
#include <modm/platform.hpp> | ||
#include <modm/processing.hpp> | ||
|
||
using Sck = GpioA5; | ||
using Miso = GpioA6; | ||
using Mosi = GpioA7; | ||
using SpiMaster = modm::platform::SpiMaster1; | ||
|
||
namespace | ||
{ | ||
|
||
namespace rx | ||
{ | ||
volatile bool dio1 = false; | ||
volatile bool dio2 = false; | ||
volatile bool dio3 = false; | ||
} | ||
|
||
namespace tx | ||
{ | ||
volatile bool dio1 = false; | ||
volatile bool dio2 = false; | ||
volatile bool dio3 = false; | ||
} | ||
|
||
static constexpr modm::sx128x::LoRa::ModulationParams modulationParams = { | ||
spreadingFactor : modm::sx128x::LoRa::ModulationParams::SpreadingFactor::Sf9, | ||
bandwidth : modm::sx128x::LoRa::ModulationParams::Bandwidth::Bw400, | ||
codingRate : modm::sx128x::LoRa::ModulationParams::CodingRate::Cr_Li_4_7 | ||
}; | ||
|
||
static constexpr modm::sx128x::LoRa::PacketParams packetParams = { | ||
preambleLength : 12, | ||
headerType : modm::sx128x::LoRa::PacketParams::HeaderType::Explicit, | ||
payloadLength : 4, | ||
crc : modm::sx128x::LoRa::PacketParams::Crc::Enable, | ||
invertIq : modm::sx128x::LoRa::PacketParams::InvertIq::Standard | ||
}; | ||
|
||
} | ||
|
||
class RxThread : public modm::sx128x, public modm::pt::Protothread | ||
{ | ||
using Reset = GpioB3; | ||
using Busy = GpioB4; | ||
using Dio1 = GpioB5; | ||
using Dio2 = GpioB6; | ||
using Dio3 = GpioB7; | ||
|
||
using Nss = GpioD2; | ||
using Hal = modm::Sx128xHalSpi<SpiMaster, Nss, Busy, Reset>; | ||
|
||
public: | ||
RxThread() {} | ||
|
||
inline bool | ||
run() | ||
{ | ||
PT_BEGIN(); | ||
|
||
Nss::setOutput(modm::Gpio::High); | ||
Reset::setOutput(modm::Gpio::Low); | ||
Busy::setInput(modm::platform::Gpio::InputType::PullDown); | ||
|
||
Dio1::setInput(modm::platform::Gpio::InputType::PullDown); | ||
Exti::connect<Dio1>(Exti::Trigger::RisingEdge, [](uint8_t) mutable { rx::dio1 = true; }); | ||
|
||
Dio2::setInput(modm::platform::Gpio::InputType::PullDown); | ||
Exti::connect<Dio2>(Exti::Trigger::RisingEdge, [](uint8_t) mutable { rx::dio2 = true; }); | ||
|
||
Dio3::setInput(modm::platform::Gpio::InputType::PullDown); | ||
Exti::connect<Dio3>(Exti::Trigger::RisingEdge, [](uint8_t) mutable { rx::dio3 = true; }); | ||
|
||
radio.reset(); | ||
PT_CALL(radio.setStandby()); | ||
|
||
// Initialize the sx128x | ||
PT_CALL(radio.setPacketType(PacketType::LoRa)); | ||
PT_CALL(radio.setRfFrequency(2457_MHz)); | ||
PT_CALL(radio.setRegulatorMode(RegulatorMode::Ldo)); | ||
PT_CALL(radio.setBufferBaseAddress(0, 0)); | ||
PT_CALL(radio.setModulationParams(modulationParams)); | ||
PT_CALL(radio.writeRegister(Register::SfAdditionalConfiguration, 0x32)); | ||
PT_CALL(radio.writeRegister(Register::FrequencyErrorCorrection, 0x01)); | ||
PT_CALL(radio.setPacketParams(packetParams)); | ||
PT_CALL(radio.setDioIrqParams(Irq::RxDone | Irq::RxTxTimeout, Irq::RxDone, Irq::RxTxTimeout, Irq::RxTxTimeout)); | ||
PT_CALL(radio.setRx(sx128x::PeriodBase::ms1, 1000)); | ||
|
||
MODM_LOG_DEBUG << "Sx128x initialization complete!" << modm::endl; | ||
|
||
while (true) | ||
{ | ||
if (rx::dio1) | ||
{ | ||
PT_CALL(radio.getIrqStatus(irqStatus)); | ||
if (irqStatus.any(Irq::RxDone)) | ||
{ | ||
PT_CALL(radio.clearIrqStatus(Irq::RxDone | Irq::RxTxTimeout)); | ||
PT_CALL(radio.setRx(sx128x::PeriodBase::ms1, 1000)); | ||
|
||
PT_CALL(radio.getRxBufferStatus(rxBufferStatus)); | ||
PT_CALL(radio.getPacketStatus(packetStatus)); | ||
PT_CALL(radio.readBuffer(rxBufferStatus.rxStartBufferPointer, buffer, std::min<uint8_t>(bufferSize, rxBufferStatus.rxPayloadLength))); | ||
|
||
/// TODO: read frequency error indicator (FEI) from register 0x0954 (MSB) 0x0955, 0x0956 (LSB). | ||
|
||
MODM_LOG_DEBUG << "Received Message" << modm::endl; | ||
MODM_LOG_DEBUG << "Counter: " << reinterpret_cast<uint32_t*>(buffer)[0] << modm::endl; | ||
MODM_LOG_DEBUG << modm::endl; | ||
} | ||
rx::dio1 = false; | ||
} | ||
|
||
if (rx::dio3) | ||
{ | ||
PT_CALL(radio.getIrqStatus(irqStatus)); | ||
if (irqStatus.any(Irq::RxTxTimeout)) | ||
{ | ||
// RxTxTimeout Interrupt recieved! | ||
// Clear irq and set to rx again. | ||
PT_CALL(radio.clearIrqStatus(Irq::RxTxTimeout)); | ||
PT_CALL(radio.setRx(sx128x::PeriodBase::ms1, 1000)); | ||
MODM_LOG_DEBUG << "RxTxTimeout Interrupt!" << modm::endl; | ||
} | ||
rx::dio3 = false; | ||
} | ||
PT_YIELD(); | ||
} | ||
PT_END(); | ||
} | ||
|
||
private: | ||
Irq_t irqStatus; | ||
PacketStatus packetStatus; | ||
RxBufferStatus rxBufferStatus; | ||
|
||
modm::Sx128x< Hal > radio; | ||
|
||
private: | ||
|
||
static constexpr uint8_t bufferSize = 124; | ||
uint8_t buffer[bufferSize]; | ||
} rxThread; | ||
|
||
class TxThread : public modm::sx128x, public modm::pt::Protothread | ||
{ | ||
using Reset = modm::platform::GpioC2; | ||
using Busy = modm::platform::GpioC3; | ||
using Dio1 = modm::platform::GpioA0; | ||
using Dio2 = modm::platform::GpioA1; | ||
using Dio3 = modm::platform::GpioA2; | ||
|
||
using Nss = modm::platform::GpioC1; | ||
using Hal = modm::Sx128xHalSpi<SpiMaster, Nss, Busy, Reset>; | ||
|
||
public: | ||
TxThread() : timer(std::chrono::milliseconds(500)) {} | ||
|
||
inline bool | ||
run() | ||
{ | ||
PT_BEGIN(); | ||
|
||
Nss::setOutput(modm::Gpio::High); | ||
Reset::setOutput(modm::Gpio::Low); | ||
Busy::setInput(modm::platform::Gpio::InputType::PullDown); | ||
|
||
Dio1::setInput(modm::platform::Gpio::InputType::PullDown); | ||
Exti::connect<Dio1>(Exti::Trigger::RisingEdge, [](uint8_t) mutable { tx::dio1 = true; }); | ||
|
||
Dio2::setInput(modm::platform::Gpio::InputType::PullDown); | ||
Exti::connect<Dio2>(Exti::Trigger::RisingEdge, [](uint8_t) mutable { tx::dio2 = true; }); | ||
|
||
Dio3::setInput(modm::platform::Gpio::InputType::PullDown); | ||
Exti::connect<Dio3>(Exti::Trigger::RisingEdge, [](uint8_t) mutable { tx::dio3 = true; }); | ||
|
||
|
||
radio.reset(); | ||
PT_CALL(radio.setStandby()); | ||
|
||
// Initialize the sx128x | ||
PT_CALL(radio.setPacketType(PacketType::LoRa)); | ||
PT_CALL(radio.setRfFrequency(2457_MHz)); | ||
PT_CALL(radio.setRegulatorMode(RegulatorMode::Ldo)); | ||
PT_CALL(radio.setBufferBaseAddress(0, 0)); | ||
PT_CALL(radio.setModulationParams(modulationParams)); | ||
PT_CALL(radio.writeRegister(Register::SfAdditionalConfiguration, 0x32)); | ||
PT_CALL(radio.writeRegister(Register::FrequencyErrorCorrection, 0x01)); | ||
PT_CALL(radio.setPacketParams(packetParams)); | ||
PT_CALL(radio.setDioIrqParams(Irq::TxDone | Irq::RxTxTimeout, Irq::TxDone, Irq::RxTxTimeout)); | ||
|
||
MODM_LOG_DEBUG << "Sx128x initialization complete!" << modm::endl; | ||
|
||
while (true) | ||
{ | ||
if (tx::dio1) | ||
{ | ||
PT_CALL(radio.getIrqStatus(irqStatus)); | ||
if (irqStatus.any(Irq::TxDone)) | ||
{ | ||
PT_CALL(radio.clearIrqStatus(Irq::TxDone)); | ||
irqStatus.reset(Irq::TxDone); | ||
|
||
MODM_LOG_DEBUG << "Message sent" << modm::endl; | ||
MODM_LOG_DEBUG << "Counter: " << counter << modm::endl; | ||
counter++; | ||
} | ||
tx::dio1 = false; | ||
} | ||
|
||
if (tx::dio2) | ||
{ | ||
PT_CALL(radio.getIrqStatus(irqStatus)); | ||
if (irqStatus.any(Irq::RxTxTimeout)) | ||
{ | ||
PT_CALL(radio.clearIrqStatus(Irq::RxTxTimeout)); | ||
irqStatus.reset(Irq::RxTxTimeout); | ||
|
||
MODM_LOG_DEBUG << "Received a timeout" << modm::endl; | ||
} | ||
tx::dio2 = false; | ||
} | ||
|
||
if (timer.execute()) | ||
{ | ||
PT_CALL(radio.writeBuffer(0, (uint8_t*) &counter, sizeof(counter))); | ||
PT_CALL(radio.setTx(PeriodBase::ms1, 100)); | ||
} | ||
PT_YIELD(); | ||
} | ||
|
||
PT_END(); | ||
} | ||
|
||
private: | ||
Irq_t irqStatus; | ||
modm::Sx128x< Hal > radio; | ||
|
||
uint32_t counter = 0; | ||
modm::PeriodicTimer timer; | ||
}; | ||
|
||
int | ||
main() | ||
{ | ||
Board::initialize(); | ||
|
||
SpiMaster::connect<Mosi::Mosi, Mosi::Mosi, Sck::Sck>(); | ||
SpiMaster::initialize<Board::SystemClock, 21.5_MHz>(); | ||
|
||
MODM_LOG_INFO << "==========MAX 31855 Test==========" << modm::endl; | ||
MODM_LOG_DEBUG << "Debug logging here" << modm::endl; | ||
MODM_LOG_INFO << "Info logging here" << modm::endl; | ||
MODM_LOG_WARNING << "Warning logging here" << modm::endl; | ||
MODM_LOG_ERROR << "Error logging here" << modm::endl; | ||
MODM_LOG_INFO << "===============================" << modm::endl; | ||
|
||
while (true) | ||
{ | ||
rxThread.run(); | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<library> | ||
<extends>modm:nucleo-g474re</extends> | ||
<options> | ||
<option name="modm:build:build.path">../../../build/nucleo_g474re/sx128x_lora</option> | ||
</options> | ||
<modules> | ||
<module>modm:driver:sx128x</module> | ||
<module>modm:platform:exti</module> | ||
<module>modm:platform:gpio</module> | ||
<module>modm:platform:spi:1</module> | ||
<module>modm:processing:protothread</module> | ||
<module>modm:processing:timer</module> | ||
<module>modm:build:scons</module> | ||
</modules> | ||
</library> |