Add lora-phy examples.

This commit is contained in:
ceekdee 2023-04-23 18:32:34 -05:00
parent a3f727e2e1
commit 73f25093c7
20 changed files with 1218 additions and 316 deletions

View File

@ -10,10 +10,13 @@ pub(crate) mod fmt;
pub mod iv; pub mod iv;
#[cfg(feature = "stm32wl")] #[cfg(feature = "stm32wl")]
#[deprecated(note = "use the external LoRa physical layer crate - https://crates.io/crates/lora-phy")]
pub mod stm32wl; pub mod stm32wl;
#[cfg(feature = "sx126x")] #[cfg(feature = "sx126x")]
#[deprecated(note = "use the external LoRa physical layer crate - https://crates.io/crates/lora-phy")]
pub mod sx126x; pub mod sx126x;
#[cfg(feature = "sx127x")] #[cfg(feature = "sx127x")]
#[deprecated(note = "use the external LoRa physical layer crate - https://crates.io/crates/lora-phy")]
pub mod sx127x; pub mod sx127x;
#[cfg(feature = "time")] #[cfg(feature = "time")]

View File

@ -1,6 +1,6 @@
//! This example runs on the RAK4631 WisBlock, which has an nRF52840 MCU and Semtech Sx126x radio. //! This example runs on the RAK4631 WisBlock, which has an nRF52840 MCU and Semtech Sx126x radio.
//! Other nrf/sx126x combinations may work with appropriate pin modifications. //! Other nrf/sx126x combinations may work with appropriate pin modifications.
//! It demonstates LORA CAD functionality. //! It demonstrates LORA CAD functionality.
#![no_std] #![no_std]
#![no_main] #![no_main]
#![macro_use] #![macro_use]
@ -17,6 +17,8 @@ use lora_phy::sx1261_2::SX1261_2;
use lora_phy::LoRa; use lora_phy::LoRa;
use {defmt_rtt as _, panic_probe as _}; use {defmt_rtt as _, panic_probe as _};
const LORA_FREQUENCY_IN_HZ: u32 = 903_900_000; // warning: set this appropriately for the region
bind_interrupts!(struct Irqs { bind_interrupts!(struct Irqs {
SPIM1_SPIS1_TWIM1_TWIS1_SPI1_TWI1 => spim::InterruptHandler<peripherals::TWISPI1>; SPIM1_SPIS1_TWIM1_TWIS1_SPI1_TWI1 => spim::InterruptHandler<peripherals::TWISPI1>;
}); });
@ -59,7 +61,12 @@ async fn main(_spawner: Spawner) {
start_indicator.set_low(); start_indicator.set_low();
let mdltn_params = { let mdltn_params = {
match lora.create_modulation_params(SpreadingFactor::_10, Bandwidth::_250KHz, CodingRate::_4_8, 903900000) { match lora.create_modulation_params(
SpreadingFactor::_10,
Bandwidth::_250KHz,
CodingRate::_4_8,
LORA_FREQUENCY_IN_HZ,
) {
Ok(mp) => mp, Ok(mp) => mp,
Err(err) => { Err(err) => {
info!("Radio error = {}", err); info!("Radio error = {}", err);
@ -84,7 +91,7 @@ async fn main(_spawner: Spawner) {
info!("cad successful without activity detected") info!("cad successful without activity detected")
} }
debug_indicator.set_high(); debug_indicator.set_high();
Timer::after(Duration::from_secs(15)).await; Timer::after(Duration::from_secs(5)).await;
debug_indicator.set_low(); debug_indicator.set_low();
} }
Err(err) => info!("cad unsuccessful = {}", err), Err(err) => info!("cad unsuccessful = {}", err),

View File

@ -0,0 +1,83 @@
//! This example runs on the RAK4631 WisBlock, which has an nRF52840 MCU and Semtech Sx126x radio.
//! Other nrf/sx126x combinations may work with appropriate pin modifications.
//! It demonstrates LoRaWAN join functionality.
#![no_std]
#![no_main]
#![macro_use]
#![feature(type_alias_impl_trait)]
use defmt::*;
use embassy_executor::Spawner;
use embassy_lora::iv::GenericSx126xInterfaceVariant;
use embassy_lora::LoraTimer;
use embassy_nrf::gpio::{Input, Level, Output, OutputDrive, Pin as _, Pull};
use embassy_nrf::rng::Rng;
use embassy_nrf::{bind_interrupts, peripherals, rng, spim};
use embassy_time::Delay;
use lora_phy::mod_params::*;
use lora_phy::sx1261_2::SX1261_2;
use lora_phy::LoRa;
use lorawan::default_crypto::DefaultFactory as Crypto;
use lorawan_device::async_device::lora_radio::LoRaRadio;
use lorawan_device::async_device::{region, Device, JoinMode};
use {defmt_rtt as _, panic_probe as _};
const LORAWAN_REGION: region::Region = region::Region::EU868; // warning: set this appropriately for the region
bind_interrupts!(struct Irqs {
SPIM1_SPIS1_TWIM1_TWIS1_SPI1_TWI1 => spim::InterruptHandler<peripherals::TWISPI1>;
RNG => rng::InterruptHandler<peripherals::RNG>;
});
#[embassy_executor::main]
async fn main(_spawner: Spawner) {
let p = embassy_nrf::init(Default::default());
let mut spi_config = spim::Config::default();
spi_config.frequency = spim::Frequency::M16;
let spim = spim::Spim::new(p.TWISPI1, Irqs, p.P1_11, p.P1_13, p.P1_12, spi_config);
let nss = Output::new(p.P1_10.degrade(), Level::High, OutputDrive::Standard);
let reset = Output::new(p.P1_06.degrade(), Level::High, OutputDrive::Standard);
let dio1 = Input::new(p.P1_15.degrade(), Pull::Down);
let busy = Input::new(p.P1_14.degrade(), Pull::Down);
let rf_switch_rx = Output::new(p.P1_05.degrade(), Level::Low, OutputDrive::Standard);
let rf_switch_tx = Output::new(p.P1_07.degrade(), Level::Low, OutputDrive::Standard);
let iv =
GenericSx126xInterfaceVariant::new(nss, reset, dio1, busy, Some(rf_switch_rx), Some(rf_switch_tx)).unwrap();
let mut delay = Delay;
let lora = {
match LoRa::new(SX1261_2::new(BoardType::Rak4631Sx1262, spim, iv), true, &mut delay).await {
Ok(l) => l,
Err(err) => {
info!("Radio error = {}", err);
return;
}
}
};
let radio = LoRaRadio::new(lora);
let region: region::Configuration = region::Configuration::new(LORAWAN_REGION);
let mut device: Device<_, Crypto, _, _> = Device::new(region, radio, LoraTimer::new(), Rng::new(p.RNG, Irqs));
defmt::info!("Joining LoRaWAN network");
// TODO: Adjust the EUI and Keys according to your network credentials
match device
.join(&JoinMode::OTAA {
deveui: [0, 0, 0, 0, 0, 0, 0, 0],
appeui: [0, 0, 0, 0, 0, 0, 0, 0],
appkey: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
})
.await
{
Ok(()) => defmt::info!("LoRaWAN network joined"),
Err(err) => {
info!("Radio error = {}", err);
return;
}
};
}

View File

@ -0,0 +1,121 @@
//! This example runs on the RAK4631 WisBlock, which has an nRF52840 MCU and Semtech Sx126x radio.
//! Other nrf/sx126x combinations may work with appropriate pin modifications.
//! It demonstrates LORA P2P receive functionality in conjunction with the lora_p2p_send example.
#![no_std]
#![no_main]
#![macro_use]
#![feature(type_alias_impl_trait)]
use defmt::*;
use embassy_executor::Spawner;
use embassy_lora::iv::GenericSx126xInterfaceVariant;
use embassy_nrf::gpio::{Input, Level, Output, OutputDrive, Pin as _, Pull};
use embassy_nrf::{bind_interrupts, peripherals, spim};
use embassy_time::{Delay, Duration, Timer};
use lora_phy::mod_params::*;
use lora_phy::sx1261_2::SX1261_2;
use lora_phy::LoRa;
use {defmt_rtt as _, panic_probe as _};
const LORA_FREQUENCY_IN_HZ: u32 = 903_900_000; // warning: set this appropriately for the region
bind_interrupts!(struct Irqs {
SPIM1_SPIS1_TWIM1_TWIS1_SPI1_TWI1 => spim::InterruptHandler<peripherals::TWISPI1>;
});
#[embassy_executor::main]
async fn main(_spawner: Spawner) {
let p = embassy_nrf::init(Default::default());
let mut spi_config = spim::Config::default();
spi_config.frequency = spim::Frequency::M16;
let spim = spim::Spim::new(p.TWISPI1, Irqs, p.P1_11, p.P1_13, p.P1_12, spi_config);
let nss = Output::new(p.P1_10.degrade(), Level::High, OutputDrive::Standard);
let reset = Output::new(p.P1_06.degrade(), Level::High, OutputDrive::Standard);
let dio1 = Input::new(p.P1_15.degrade(), Pull::Down);
let busy = Input::new(p.P1_14.degrade(), Pull::Down);
let rf_switch_rx = Output::new(p.P1_05.degrade(), Level::Low, OutputDrive::Standard);
let rf_switch_tx = Output::new(p.P1_07.degrade(), Level::Low, OutputDrive::Standard);
let iv =
GenericSx126xInterfaceVariant::new(nss, reset, dio1, busy, Some(rf_switch_rx), Some(rf_switch_tx)).unwrap();
let mut delay = Delay;
let mut lora = {
match LoRa::new(SX1261_2::new(BoardType::Rak4631Sx1262, spim, iv), false, &mut delay).await {
Ok(l) => l,
Err(err) => {
info!("Radio error = {}", err);
return;
}
}
};
let mut debug_indicator = Output::new(p.P1_03, Level::Low, OutputDrive::Standard);
let mut start_indicator = Output::new(p.P1_04, Level::Low, OutputDrive::Standard);
start_indicator.set_high();
Timer::after(Duration::from_secs(5)).await;
start_indicator.set_low();
let mut receiving_buffer = [00u8; 100];
let mdltn_params = {
match lora.create_modulation_params(
SpreadingFactor::_10,
Bandwidth::_250KHz,
CodingRate::_4_8,
LORA_FREQUENCY_IN_HZ,
) {
Ok(mp) => mp,
Err(err) => {
info!("Radio error = {}", err);
return;
}
}
};
let rx_pkt_params = {
match lora.create_rx_packet_params(4, false, receiving_buffer.len() as u8, true, false, &mdltn_params) {
Ok(pp) => pp,
Err(err) => {
info!("Radio error = {}", err);
return;
}
}
};
match lora
.prepare_for_rx(&mdltn_params, &rx_pkt_params, None, true, false, 0, 0x00ffffffu32)
.await
{
Ok(()) => {}
Err(err) => {
info!("Radio error = {}", err);
return;
}
};
loop {
receiving_buffer = [00u8; 100];
match lora.rx(&rx_pkt_params, &mut receiving_buffer).await {
Ok((received_len, _rx_pkt_status)) => {
if (received_len == 3)
&& (receiving_buffer[0] == 0x01u8)
&& (receiving_buffer[1] == 0x02u8)
&& (receiving_buffer[2] == 0x03u8)
{
info!("rx successful");
debug_indicator.set_high();
Timer::after(Duration::from_secs(5)).await;
debug_indicator.set_low();
} else {
info!("rx unknown packet");
}
}
Err(err) => info!("rx unsuccessful = {}", err),
}
}
}

View File

@ -1,6 +1,6 @@
//! This example runs on the RAK4631 WisBlock, which has an nRF52840 MCU and Semtech Sx126x radio. //! This example runs on the RAK4631 WisBlock, which has an nRF52840 MCU and Semtech Sx126x radio.
//! Other nrf/sx126x combinations may work with appropriate pin modifications. //! Other nrf/sx126x combinations may work with appropriate pin modifications.
//! It demonstates LoRa Rx duty cycle functionality. //! It demonstrates LoRa Rx duty cycle functionality in conjunction with the lora_p2p_send example.
#![no_std] #![no_std]
#![no_main] #![no_main]
#![macro_use] #![macro_use]
@ -17,6 +17,8 @@ use lora_phy::sx1261_2::SX1261_2;
use lora_phy::LoRa; use lora_phy::LoRa;
use {defmt_rtt as _, panic_probe as _}; use {defmt_rtt as _, panic_probe as _};
const LORA_FREQUENCY_IN_HZ: u32 = 903_900_000; // warning: set this appropriately for the region
bind_interrupts!(struct Irqs { bind_interrupts!(struct Irqs {
SPIM1_SPIS1_TWIM1_TWIS1_SPI1_TWI1 => spim::InterruptHandler<peripherals::TWISPI1>; SPIM1_SPIS1_TWIM1_TWIS1_SPI1_TWI1 => spim::InterruptHandler<peripherals::TWISPI1>;
}); });
@ -61,7 +63,12 @@ async fn main(_spawner: Spawner) {
let mut receiving_buffer = [00u8; 100]; let mut receiving_buffer = [00u8; 100];
let mdltn_params = { let mdltn_params = {
match lora.create_modulation_params(SpreadingFactor::_10, Bandwidth::_250KHz, CodingRate::_4_8, 903900000) { match lora.create_modulation_params(
SpreadingFactor::_10,
Bandwidth::_250KHz,
CodingRate::_4_8,
LORA_FREQUENCY_IN_HZ,
) {
Ok(mp) => mp, Ok(mp) => mp,
Err(err) => { Err(err) => {
info!("Radio error = {}", err); info!("Radio error = {}", err);

View File

@ -0,0 +1,104 @@
//! This example runs on the RAK4631 WisBlock, which has an nRF52840 MCU and Semtech Sx126x radio.
//! Other nrf/sx126x combinations may work with appropriate pin modifications.
//! It demonstrates LORA P2P send functionality.
#![no_std]
#![no_main]
#![macro_use]
#![feature(type_alias_impl_trait)]
use defmt::*;
use embassy_executor::Spawner;
use embassy_lora::iv::GenericSx126xInterfaceVariant;
use embassy_nrf::gpio::{Input, Level, Output, OutputDrive, Pin as _, Pull};
use embassy_nrf::{bind_interrupts, peripherals, spim};
use embassy_time::Delay;
use lora_phy::mod_params::*;
use lora_phy::sx1261_2::SX1261_2;
use lora_phy::LoRa;
use {defmt_rtt as _, panic_probe as _};
const LORA_FREQUENCY_IN_HZ: u32 = 903_900_000; // warning: set this appropriately for the region
bind_interrupts!(struct Irqs {
SPIM1_SPIS1_TWIM1_TWIS1_SPI1_TWI1 => spim::InterruptHandler<peripherals::TWISPI1>;
});
#[embassy_executor::main]
async fn main(_spawner: Spawner) {
let p = embassy_nrf::init(Default::default());
let mut spi_config = spim::Config::default();
spi_config.frequency = spim::Frequency::M16;
let spim = spim::Spim::new(p.TWISPI1, Irqs, p.P1_11, p.P1_13, p.P1_12, spi_config);
let nss = Output::new(p.P1_10.degrade(), Level::High, OutputDrive::Standard);
let reset = Output::new(p.P1_06.degrade(), Level::High, OutputDrive::Standard);
let dio1 = Input::new(p.P1_15.degrade(), Pull::Down);
let busy = Input::new(p.P1_14.degrade(), Pull::Down);
let rf_switch_rx = Output::new(p.P1_05.degrade(), Level::Low, OutputDrive::Standard);
let rf_switch_tx = Output::new(p.P1_07.degrade(), Level::Low, OutputDrive::Standard);
let iv =
GenericSx126xInterfaceVariant::new(nss, reset, dio1, busy, Some(rf_switch_rx), Some(rf_switch_tx)).unwrap();
let mut delay = Delay;
let mut lora = {
match LoRa::new(SX1261_2::new(BoardType::Rak4631Sx1262, spim, iv), false, &mut delay).await {
Ok(l) => l,
Err(err) => {
info!("Radio error = {}", err);
return;
}
}
};
let mdltn_params = {
match lora.create_modulation_params(
SpreadingFactor::_10,
Bandwidth::_250KHz,
CodingRate::_4_8,
LORA_FREQUENCY_IN_HZ,
) {
Ok(mp) => mp,
Err(err) => {
info!("Radio error = {}", err);
return;
}
}
};
let mut tx_pkt_params = {
match lora.create_tx_packet_params(4, false, true, false, &mdltn_params) {
Ok(pp) => pp,
Err(err) => {
info!("Radio error = {}", err);
return;
}
}
};
match lora.prepare_for_tx(&mdltn_params, 20, false).await {
Ok(()) => {}
Err(err) => {
info!("Radio error = {}", err);
return;
}
};
let buffer = [0x01u8, 0x02u8, 0x03u8];
match lora.tx(&mdltn_params, &mut tx_pkt_params, &buffer, 0xffffff).await {
Ok(()) => {
info!("TX DONE");
}
Err(err) => {
info!("Radio error = {}", err);
return;
}
};
match lora.sleep(&mut delay).await {
Ok(()) => info!("Sleep successful"),
Err(err) => info!("Sleep unsuccessful = {}", err),
}
}

View File

@ -1,128 +0,0 @@
//! This example runs on the RAK4631 WisBlock, which has an nRF52840 MCU and Semtech Sx126x radio.
//! Other nrf/sx126x combinations may work with appropriate pin modifications.
//! It demonstates LORA P2P functionality in conjunction with example lora_p2p_report.rs.
#![no_std]
#![no_main]
#![macro_use]
#![feature(type_alias_impl_trait)]
#![feature(alloc_error_handler)]
#![allow(incomplete_features)]
use defmt::*;
use embassy_executor::Spawner;
use embassy_lora::sx126x::*;
use embassy_nrf::gpio::{Input, Level, Output, OutputDrive, Pin as _, Pull};
use embassy_nrf::{bind_interrupts, peripherals, spim};
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
use embassy_sync::pubsub::{PubSubChannel, Publisher};
use embassy_time::{Duration, Timer};
use lorawan_device::async_device::radio::{Bandwidth, CodingRate, PhyRxTx, RfConfig, SpreadingFactor, TxConfig};
use {defmt_rtt as _, panic_probe as _, panic_probe as _};
bind_interrupts!(struct Irqs {
SPIM1_SPIS1_TWIM1_TWIS1_SPI1_TWI1 => spim::InterruptHandler<peripherals::TWISPI1>;
});
// Message bus: queue of 2, 1 subscriber (Lora P2P), 2 publishers (temperature, motion detection)
static MESSAGE_BUS: PubSubChannel<CriticalSectionRawMutex, Message, 2, 1, 2> = PubSubChannel::new();
#[derive(Clone, defmt::Format)]
enum Message {
Temperature(i32),
MotionDetected,
}
#[embassy_executor::task]
async fn temperature_task(publisher: Publisher<'static, CriticalSectionRawMutex, Message, 2, 1, 2>) {
// Publish a fake temperature every 43 seconds, minimizing LORA traffic.
loop {
Timer::after(Duration::from_secs(43)).await;
publisher.publish(Message::Temperature(9)).await;
}
}
#[embassy_executor::task]
async fn motion_detection_task(publisher: Publisher<'static, CriticalSectionRawMutex, Message, 2, 1, 2>) {
// Publish a fake motion detection every 79 seconds, minimizing LORA traffic.
loop {
Timer::after(Duration::from_secs(79)).await;
publisher.publish(Message::MotionDetected).await;
}
}
#[embassy_executor::main]
async fn main(spawner: Spawner) {
let p = embassy_nrf::init(Default::default());
// set up to funnel temperature and motion detection events to the Lora Tx task
let mut lora_tx_subscriber = unwrap!(MESSAGE_BUS.subscriber());
let temperature_publisher = unwrap!(MESSAGE_BUS.publisher());
let motion_detection_publisher = unwrap!(MESSAGE_BUS.publisher());
let mut spi_config = spim::Config::default();
spi_config.frequency = spim::Frequency::M16;
let mut radio = {
let spim = spim::Spim::new(p.TWISPI1, Irqs, p.P1_11, p.P1_13, p.P1_12, spi_config);
let cs = Output::new(p.P1_10.degrade(), Level::High, OutputDrive::Standard);
let reset = Output::new(p.P1_06.degrade(), Level::High, OutputDrive::Standard);
let dio1 = Input::new(p.P1_15.degrade(), Pull::Down);
let busy = Input::new(p.P1_14.degrade(), Pull::Down);
let antenna_rx = Output::new(p.P1_05.degrade(), Level::Low, OutputDrive::Standard);
let antenna_tx = Output::new(p.P1_07.degrade(), Level::Low, OutputDrive::Standard);
match Sx126xRadio::new(spim, cs, reset, antenna_rx, antenna_tx, dio1, busy, false).await {
Ok(r) => r,
Err(err) => {
info!("Sx126xRadio error = {}", err);
return;
}
}
};
let mut start_indicator = Output::new(p.P1_04, Level::Low, OutputDrive::Standard);
start_indicator.set_high();
Timer::after(Duration::from_secs(5)).await;
start_indicator.set_low();
match radio.lora.sleep().await {
Ok(()) => info!("Sleep successful"),
Err(err) => info!("Sleep unsuccessful = {}", err),
}
unwrap!(spawner.spawn(temperature_task(temperature_publisher)));
unwrap!(spawner.spawn(motion_detection_task(motion_detection_publisher)));
loop {
let message = lora_tx_subscriber.next_message_pure().await;
let tx_config = TxConfig {
// 11 byte maximum payload for Bandwidth 125 and SF 10
pw: 10, // up to 20
rf: RfConfig {
frequency: 903900000, // channel in Hz, not MHz
bandwidth: Bandwidth::_250KHz,
spreading_factor: SpreadingFactor::_10,
coding_rate: CodingRate::_4_8,
},
};
let mut buffer = [0x00u8];
match message {
Message::Temperature(temperature) => buffer[0] = temperature as u8,
Message::MotionDetected => buffer[0] = 0x01u8,
};
// unencrypted
match radio.tx(tx_config, &buffer).await {
Ok(ret_val) => info!("TX ret_val = {}", ret_val),
Err(err) => info!("TX error = {}", err),
}
match radio.lora.sleep().await {
Ok(()) => info!("Sleep successful"),
Err(err) => info!("Sleep unsuccessful = {}", err),
}
}
}

View File

@ -0,0 +1,80 @@
//! This example runs on the Raspberry Pi Pico with a Waveshare board containing a Semtech Sx1262 radio.
//! It demonstrates LoRaWAN join functionality.
#![no_std]
#![no_main]
#![macro_use]
#![feature(type_alias_impl_trait)]
use defmt::*;
use embassy_executor::Spawner;
use embassy_lora::iv::GenericSx126xInterfaceVariant;
use embassy_lora::LoraTimer;
use embassy_rp::gpio::{Input, Level, Output, Pin, Pull};
use embassy_rp::spi::{Config, Spi};
use embassy_time::Delay;
use lora_phy::mod_params::*;
use lora_phy::sx1261_2::SX1261_2;
use lora_phy::LoRa;
use lorawan::default_crypto::DefaultFactory as Crypto;
use lorawan_device::async_device::lora_radio::LoRaRadio;
use lorawan_device::async_device::{region, Device, JoinMode};
use {defmt_rtt as _, panic_probe as _};
const LORAWAN_REGION: region::Region = region::Region::EU868; // warning: set this appropriately for the region
#[embassy_executor::main]
async fn main(_spawner: Spawner) {
let p = embassy_rp::init(Default::default());
let miso = p.PIN_12;
let mosi = p.PIN_11;
let clk = p.PIN_10;
let spi = Spi::new(p.SPI1, clk, mosi, miso, p.DMA_CH0, p.DMA_CH1, Config::default());
let nss = Output::new(p.PIN_3.degrade(), Level::High);
let reset = Output::new(p.PIN_15.degrade(), Level::High);
let dio1 = Input::new(p.PIN_20.degrade(), Pull::None);
let busy = Input::new(p.PIN_2.degrade(), Pull::None);
let iv = GenericSx126xInterfaceVariant::new(nss, reset, dio1, busy, None, None).unwrap();
let mut delay = Delay;
let lora = {
match LoRa::new(
SX1261_2::new(BoardType::RpPicoWaveshareSx1262, spi, iv),
true,
&mut delay,
)
.await
{
Ok(l) => l,
Err(err) => {
info!("Radio error = {}", err);
return;
}
}
};
let radio = LoRaRadio::new(lora);
let region: region::Configuration = region::Configuration::new(LORAWAN_REGION);
let mut device: Device<_, Crypto, _, _> = Device::new(region, radio, LoraTimer::new(), embassy_rp::clocks::RoscRng);
defmt::info!("Joining LoRaWAN network");
// TODO: Adjust the EUI and Keys according to your network credentials
match device
.join(&JoinMode::OTAA {
deveui: [0, 0, 0, 0, 0, 0, 0, 0],
appeui: [0, 0, 0, 0, 0, 0, 0, 0],
appkey: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
})
.await
{
Ok(()) => defmt::info!("LoRaWAN network joined"),
Err(err) => {
info!("Radio error = {}", err);
return;
}
};
}

View File

@ -0,0 +1,115 @@
//! This example runs on the Raspberry Pi Pico with a Waveshare board containing a Semtech Sx1262 radio.
//! It demonstrates LORA P2P receive functionality in conjunction with the lora_p2p_send example.
#![no_std]
#![no_main]
#![macro_use]
#![feature(type_alias_impl_trait)]
use defmt::*;
use embassy_executor::Spawner;
use embassy_lora::iv::GenericSx126xInterfaceVariant;
use embassy_rp::gpio::{Input, Level, Output, Pin, Pull};
use embassy_rp::spi::{Config, Spi};
use embassy_time::{Delay, Duration, Timer};
use lora_phy::mod_params::*;
use lora_phy::sx1261_2::SX1261_2;
use lora_phy::LoRa;
use {defmt_rtt as _, panic_probe as _};
const LORA_FREQUENCY_IN_HZ: u32 = 903_900_000; // warning: set this appropriately for the region
#[embassy_executor::main]
async fn main(_spawner: Spawner) {
let p = embassy_rp::init(Default::default());
let miso = p.PIN_12;
let mosi = p.PIN_11;
let clk = p.PIN_10;
let spi = Spi::new(p.SPI1, clk, mosi, miso, p.DMA_CH0, p.DMA_CH1, Config::default());
let nss = Output::new(p.PIN_3.degrade(), Level::High);
let reset = Output::new(p.PIN_15.degrade(), Level::High);
let dio1 = Input::new(p.PIN_20.degrade(), Pull::None);
let busy = Input::new(p.PIN_2.degrade(), Pull::None);
let iv = GenericSx126xInterfaceVariant::new(nss, reset, dio1, busy, None, None).unwrap();
let mut delay = Delay;
let mut lora = {
match LoRa::new(
SX1261_2::new(BoardType::RpPicoWaveshareSx1262, spi, iv),
false,
&mut delay,
)
.await
{
Ok(l) => l,
Err(err) => {
info!("Radio error = {}", err);
return;
}
}
};
let mut debug_indicator = Output::new(p.PIN_25, Level::Low);
let mut receiving_buffer = [00u8; 100];
let mdltn_params = {
match lora.create_modulation_params(
SpreadingFactor::_10,
Bandwidth::_250KHz,
CodingRate::_4_8,
LORA_FREQUENCY_IN_HZ,
) {
Ok(mp) => mp,
Err(err) => {
info!("Radio error = {}", err);
return;
}
}
};
let rx_pkt_params = {
match lora.create_rx_packet_params(4, false, receiving_buffer.len() as u8, true, false, &mdltn_params) {
Ok(pp) => pp,
Err(err) => {
info!("Radio error = {}", err);
return;
}
}
};
match lora
.prepare_for_rx(&mdltn_params, &rx_pkt_params, None, true, false, 0, 0x00ffffffu32)
.await
{
Ok(()) => {}
Err(err) => {
info!("Radio error = {}", err);
return;
}
};
loop {
receiving_buffer = [00u8; 100];
match lora.rx(&rx_pkt_params, &mut receiving_buffer).await {
Ok((received_len, _rx_pkt_status)) => {
if (received_len == 3)
&& (receiving_buffer[0] == 0x01u8)
&& (receiving_buffer[1] == 0x02u8)
&& (receiving_buffer[2] == 0x03u8)
{
info!("rx successful");
debug_indicator.set_high();
Timer::after(Duration::from_secs(5)).await;
debug_indicator.set_low();
} else {
info!("rx unknown packet");
}
}
Err(err) => info!("rx unsuccessful = {}", err),
}
}
}

View File

@ -0,0 +1,103 @@
//! This example runs on the Raspberry Pi Pico with a Waveshare board containing a Semtech Sx1262 radio.
//! It demonstrates LORA P2P send functionality.
#![no_std]
#![no_main]
#![macro_use]
#![feature(type_alias_impl_trait)]
use defmt::*;
use embassy_executor::Spawner;
use embassy_lora::iv::GenericSx126xInterfaceVariant;
use embassy_rp::gpio::{Input, Level, Output, Pin, Pull};
use embassy_rp::spi::{Config, Spi};
use embassy_time::Delay;
use lora_phy::mod_params::*;
use lora_phy::sx1261_2::SX1261_2;
use lora_phy::LoRa;
use {defmt_rtt as _, panic_probe as _};
const LORA_FREQUENCY_IN_HZ: u32 = 903_900_000; // warning: set this appropriately for the region
#[embassy_executor::main]
async fn main(_spawner: Spawner) {
let p = embassy_rp::init(Default::default());
let miso = p.PIN_12;
let mosi = p.PIN_11;
let clk = p.PIN_10;
let spi = Spi::new(p.SPI1, clk, mosi, miso, p.DMA_CH0, p.DMA_CH1, Config::default());
let nss = Output::new(p.PIN_3.degrade(), Level::High);
let reset = Output::new(p.PIN_15.degrade(), Level::High);
let dio1 = Input::new(p.PIN_20.degrade(), Pull::None);
let busy = Input::new(p.PIN_2.degrade(), Pull::None);
let iv = GenericSx126xInterfaceVariant::new(nss, reset, dio1, busy, None, None).unwrap();
let mut delay = Delay;
let mut lora = {
match LoRa::new(
SX1261_2::new(BoardType::RpPicoWaveshareSx1262, spi, iv),
false,
&mut delay,
)
.await
{
Ok(l) => l,
Err(err) => {
info!("Radio error = {}", err);
return;
}
}
};
let mdltn_params = {
match lora.create_modulation_params(
SpreadingFactor::_10,
Bandwidth::_250KHz,
CodingRate::_4_8,
LORA_FREQUENCY_IN_HZ,
) {
Ok(mp) => mp,
Err(err) => {
info!("Radio error = {}", err);
return;
}
}
};
let mut tx_pkt_params = {
match lora.create_tx_packet_params(4, false, true, false, &mdltn_params) {
Ok(pp) => pp,
Err(err) => {
info!("Radio error = {}", err);
return;
}
}
};
match lora.prepare_for_tx(&mdltn_params, 20, false).await {
Ok(()) => {}
Err(err) => {
info!("Radio error = {}", err);
return;
}
};
let buffer = [0x01u8, 0x02u8, 0x03u8];
match lora.tx(&mdltn_params, &mut tx_pkt_params, &buffer, 0xffffff).await {
Ok(()) => {
info!("TX DONE");
}
Err(err) => {
info!("Radio error = {}", err);
return;
}
};
match lora.sleep(&mut delay).await {
Ok(()) => info!("Sleep successful"),
Err(err) => info!("Sleep unsuccessful = {}", err),
}
}

View File

@ -0,0 +1,139 @@
//! This example runs on the Raspberry Pi Pico with a Waveshare board containing a Semtech Sx1262 radio.
//! It demonstrates LORA P2P send functionality using the second core, with data provided by the first core.
#![no_std]
#![no_main]
#![macro_use]
#![feature(type_alias_impl_trait)]
use defmt::*;
use embassy_executor::Executor;
use embassy_executor::_export::StaticCell;
use embassy_lora::iv::GenericSx126xInterfaceVariant;
use embassy_rp::gpio::{AnyPin, Input, Level, Output, Pin, Pull};
use embassy_rp::multicore::{spawn_core1, Stack};
use embassy_rp::peripherals::SPI1;
use embassy_rp::spi::{Async, Config, Spi};
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
use embassy_sync::channel::Channel;
use embassy_time::{Delay, Duration, Timer};
use lora_phy::mod_params::*;
use lora_phy::sx1261_2::SX1261_2;
use lora_phy::LoRa;
use {defmt_rtt as _, panic_probe as _};
static mut CORE1_STACK: Stack<4096> = Stack::new();
static EXECUTOR0: StaticCell<Executor> = StaticCell::new();
static EXECUTOR1: StaticCell<Executor> = StaticCell::new();
static CHANNEL: Channel<CriticalSectionRawMutex, [u8; 3], 1> = Channel::new();
const LORA_FREQUENCY_IN_HZ: u32 = 903_900_000; // warning: set this appropriately for the region
#[cortex_m_rt::entry]
fn main() -> ! {
let p = embassy_rp::init(Default::default());
let miso = p.PIN_12;
let mosi = p.PIN_11;
let clk = p.PIN_10;
let spi = Spi::new(p.SPI1, clk, mosi, miso, p.DMA_CH0, p.DMA_CH1, Config::default());
let nss = Output::new(p.PIN_3.degrade(), Level::High);
let reset = Output::new(p.PIN_15.degrade(), Level::High);
let dio1 = Input::new(p.PIN_20.degrade(), Pull::None);
let busy = Input::new(p.PIN_2.degrade(), Pull::None);
let iv = GenericSx126xInterfaceVariant::new(nss, reset, dio1, busy, None, None).unwrap();
spawn_core1(p.CORE1, unsafe { &mut CORE1_STACK }, move || {
let executor1 = EXECUTOR1.init(Executor::new());
executor1.run(|spawner| unwrap!(spawner.spawn(core1_task(spi, iv))));
});
let executor0 = EXECUTOR0.init(Executor::new());
executor0.run(|spawner| unwrap!(spawner.spawn(core0_task())));
}
#[embassy_executor::task]
async fn core0_task() {
info!("Hello from core 0");
loop {
CHANNEL.send([0x01u8, 0x02u8, 0x03u8]).await;
Timer::after(Duration::from_millis(60 * 1000)).await;
}
}
#[embassy_executor::task]
async fn core1_task(
spi: Spi<'static, SPI1, Async>,
iv: GenericSx126xInterfaceVariant<Output<'static, AnyPin>, Input<'static, AnyPin>>,
) {
info!("Hello from core 1");
let mut delay = Delay;
let mut lora = {
match LoRa::new(
SX1261_2::new(BoardType::RpPicoWaveshareSx1262, spi, iv),
false,
&mut delay,
)
.await
{
Ok(l) => l,
Err(err) => {
info!("Radio error = {}", err);
return;
}
}
};
let mdltn_params = {
match lora.create_modulation_params(
SpreadingFactor::_10,
Bandwidth::_250KHz,
CodingRate::_4_8,
LORA_FREQUENCY_IN_HZ,
) {
Ok(mp) => mp,
Err(err) => {
info!("Radio error = {}", err);
return;
}
}
};
let mut tx_pkt_params = {
match lora.create_tx_packet_params(4, false, true, false, &mdltn_params) {
Ok(pp) => pp,
Err(err) => {
info!("Radio error = {}", err);
return;
}
}
};
loop {
let buffer: [u8; 3] = CHANNEL.recv().await;
match lora.prepare_for_tx(&mdltn_params, 20, false).await {
Ok(()) => {}
Err(err) => {
info!("Radio error = {}", err);
return;
}
};
match lora.tx(&mdltn_params, &mut tx_pkt_params, &buffer, 0xffffff).await {
Ok(()) => {
info!("TX DONE");
}
Err(err) => {
info!("Radio error = {}", err);
return;
}
};
match lora.sleep(&mut delay).await {
Ok(()) => info!("Sleep successful"),
Err(err) => info!("Sleep unsuccessful = {}", err),
}
}
}

View File

@ -0,0 +1,105 @@
//! This example runs on the STM32 LoRa Discovery board, which has a builtin Semtech Sx1276 radio.
//! It demonstrates LORA P2P CAD functionality.
#![no_std]
#![no_main]
#![macro_use]
#![feature(type_alias_impl_trait)]
use defmt::*;
use embassy_executor::Spawner;
use embassy_lora::iv::Stm32l0InterfaceVariant;
use embassy_stm32::exti::{Channel, ExtiInput};
use embassy_stm32::gpio::{Input, Level, Output, Pin, Pull, Speed};
use embassy_stm32::spi;
use embassy_stm32::time::khz;
use embassy_time::{Delay, Duration, Timer};
use lora_phy::mod_params::*;
use lora_phy::sx1276_7_8_9::SX1276_7_8_9;
use lora_phy::LoRa;
use {defmt_rtt as _, panic_probe as _};
const LORA_FREQUENCY_IN_HZ: u32 = 903_900_000; // warning: set this appropriately for the region
#[embassy_executor::main]
async fn main(_spawner: Spawner) {
let mut config = embassy_stm32::Config::default();
config.rcc.mux = embassy_stm32::rcc::ClockSrc::HSI16;
config.rcc.enable_hsi48 = true;
let p = embassy_stm32::init(config);
// SPI for sx1276
let spi = spi::Spi::new(
p.SPI1,
p.PB3,
p.PA7,
p.PA6,
p.DMA1_CH3,
p.DMA1_CH2,
khz(200),
spi::Config::default(),
);
let nss = Output::new(p.PA15.degrade(), Level::High, Speed::Low);
let reset = Output::new(p.PC0.degrade(), Level::High, Speed::Low);
let irq_pin = Input::new(p.PB4.degrade(), Pull::Up);
let irq = ExtiInput::new(irq_pin, p.EXTI4.degrade());
let iv = Stm32l0InterfaceVariant::new(nss, reset, irq, None, None).unwrap();
let mut delay = Delay;
let mut lora = {
match LoRa::new(SX1276_7_8_9::new(BoardType::Stm32l0Sx1276, spi, iv), false, &mut delay).await {
Ok(l) => l,
Err(err) => {
info!("Radio error = {}", err);
return;
}
}
};
let mut debug_indicator = Output::new(p.PB5, Level::Low, Speed::Low);
let mut start_indicator = Output::new(p.PB6, Level::Low, Speed::Low);
start_indicator.set_high();
Timer::after(Duration::from_secs(5)).await;
start_indicator.set_low();
let mdltn_params = {
match lora.create_modulation_params(
SpreadingFactor::_10,
Bandwidth::_250KHz,
CodingRate::_4_8,
LORA_FREQUENCY_IN_HZ,
) {
Ok(mp) => mp,
Err(err) => {
info!("Radio error = {}", err);
return;
}
}
};
match lora.prepare_for_cad(&mdltn_params, true).await {
Ok(()) => {}
Err(err) => {
info!("Radio error = {}", err);
return;
}
};
match lora.cad().await {
Ok(cad_activity_detected) => {
if cad_activity_detected {
info!("cad successful with activity detected")
} else {
info!("cad successful without activity detected")
}
debug_indicator.set_high();
Timer::after(Duration::from_secs(5)).await;
debug_indicator.set_low();
}
Err(err) => info!("cad unsuccessful = {}", err),
}
}

View File

@ -0,0 +1,88 @@
//! This example runs on the STM32 LoRa Discovery board, which has a builtin Semtech Sx1276 radio.
//! It demonstrates LoRaWAN join functionality.
#![no_std]
#![no_main]
#![macro_use]
#![feature(type_alias_impl_trait)]
use defmt::*;
use embassy_executor::Spawner;
use embassy_lora::iv::Stm32l0InterfaceVariant;
use embassy_lora::LoraTimer;
use embassy_stm32::exti::{Channel, ExtiInput};
use embassy_stm32::gpio::{Input, Level, Output, Pin, Pull, Speed};
use embassy_stm32::rng::Rng;
use embassy_stm32::spi;
use embassy_stm32::time::khz;
use embassy_time::Delay;
use lora_phy::mod_params::*;
use lora_phy::sx1276_7_8_9::SX1276_7_8_9;
use lora_phy::LoRa;
use lorawan::default_crypto::DefaultFactory as Crypto;
use lorawan_device::async_device::lora_radio::LoRaRadio;
use lorawan_device::async_device::{region, Device, JoinMode};
use {defmt_rtt as _, panic_probe as _};
const LORAWAN_REGION: region::Region = region::Region::EU868; // warning: set this appropriately for the region
#[embassy_executor::main]
async fn main(_spawner: Spawner) {
let mut config = embassy_stm32::Config::default();
config.rcc.mux = embassy_stm32::rcc::ClockSrc::HSI16;
config.rcc.enable_hsi48 = true;
let p = embassy_stm32::init(config);
// SPI for sx1276
let spi = spi::Spi::new(
p.SPI1,
p.PB3,
p.PA7,
p.PA6,
p.DMA1_CH3,
p.DMA1_CH2,
khz(200),
spi::Config::default(),
);
let nss = Output::new(p.PA15.degrade(), Level::High, Speed::Low);
let reset = Output::new(p.PC0.degrade(), Level::High, Speed::Low);
let irq_pin = Input::new(p.PB4.degrade(), Pull::Up);
let irq = ExtiInput::new(irq_pin, p.EXTI4.degrade());
let iv = Stm32l0InterfaceVariant::new(nss, reset, irq, None, None).unwrap();
let mut delay = Delay;
let lora = {
match LoRa::new(SX1276_7_8_9::new(BoardType::Stm32l0Sx1276, spi, iv), true, &mut delay).await {
Ok(l) => l,
Err(err) => {
info!("Radio error = {}", err);
return;
}
}
};
let radio = LoRaRadio::new(lora);
let region: region::Configuration = region::Configuration::new(LORAWAN_REGION);
let mut device: Device<_, Crypto, _, _> = Device::new(region, radio, LoraTimer::new(), Rng::new(p.RNG));
defmt::info!("Joining LoRaWAN network");
// TODO: Adjust the EUI and Keys according to your network credentials
match device
.join(&JoinMode::OTAA {
deveui: [0, 0, 0, 0, 0, 0, 0, 0],
appeui: [0, 0, 0, 0, 0, 0, 0, 0],
appkey: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
})
.await
{
Ok(()) => defmt::info!("LoRaWAN network joined"),
Err(err) => {
info!("Radio error = {}", err);
return;
}
};
}

View File

@ -1,5 +1,5 @@
//! This example runs on the STM32 LoRa Discovery board, which has a builtin Semtech Sx1276 radio. //! This example runs on the STM32 LoRa Discovery board, which has a builtin Semtech Sx1276 radio.
//! It demonstrates LORA P2P receive functionality. //! It demonstrates LORA P2P receive functionality in conjunction with the lora_p2p_send example.
#![no_std] #![no_std]
#![no_main] #![no_main]
#![macro_use] #![macro_use]
@ -18,6 +18,8 @@ use lora_phy::sx1276_7_8_9::SX1276_7_8_9;
use lora_phy::LoRa; use lora_phy::LoRa;
use {defmt_rtt as _, panic_probe as _}; use {defmt_rtt as _, panic_probe as _};
const LORA_FREQUENCY_IN_HZ: u32 = 903_900_000; // warning: set this appropriately for the region
#[embassy_executor::main] #[embassy_executor::main]
async fn main(_spawner: Spawner) { async fn main(_spawner: Spawner) {
let mut config = embassy_stm32::Config::default(); let mut config = embassy_stm32::Config::default();
@ -67,7 +69,12 @@ async fn main(_spawner: Spawner) {
let mut receiving_buffer = [00u8; 100]; let mut receiving_buffer = [00u8; 100];
let mdltn_params = { let mdltn_params = {
match lora.create_modulation_params(SpreadingFactor::_10, Bandwidth::_250KHz, CodingRate::_4_8, 903900000) { match lora.create_modulation_params(
SpreadingFactor::_10,
Bandwidth::_250KHz,
CodingRate::_4_8,
LORA_FREQUENCY_IN_HZ,
) {
Ok(mp) => mp, Ok(mp) => mp,
Err(err) => { Err(err) => {
info!("Radio error = {}", err); info!("Radio error = {}", err);

View File

@ -0,0 +1,110 @@
//! This example runs on the STM32 LoRa Discovery board, which has a builtin Semtech Sx1276 radio.
//! It demonstrates LORA P2P send functionality.
#![no_std]
#![no_main]
#![macro_use]
#![feature(type_alias_impl_trait)]
use defmt::*;
use embassy_executor::Spawner;
use embassy_lora::iv::Stm32l0InterfaceVariant;
use embassy_stm32::exti::{Channel, ExtiInput};
use embassy_stm32::gpio::{Input, Level, Output, Pin, Pull, Speed};
use embassy_stm32::spi;
use embassy_stm32::time::khz;
use embassy_time::Delay;
use lora_phy::mod_params::*;
use lora_phy::sx1276_7_8_9::SX1276_7_8_9;
use lora_phy::LoRa;
use {defmt_rtt as _, panic_probe as _};
const LORA_FREQUENCY_IN_HZ: u32 = 903_900_000; // warning: set this appropriately for the region
#[embassy_executor::main]
async fn main(_spawner: Spawner) {
let mut config = embassy_stm32::Config::default();
config.rcc.mux = embassy_stm32::rcc::ClockSrc::HSI16;
config.rcc.enable_hsi48 = true;
let p = embassy_stm32::init(config);
// SPI for sx1276
let spi = spi::Spi::new(
p.SPI1,
p.PB3,
p.PA7,
p.PA6,
p.DMA1_CH3,
p.DMA1_CH2,
khz(200),
spi::Config::default(),
);
let nss = Output::new(p.PA15.degrade(), Level::High, Speed::Low);
let reset = Output::new(p.PC0.degrade(), Level::High, Speed::Low);
let irq_pin = Input::new(p.PB4.degrade(), Pull::Up);
let irq = ExtiInput::new(irq_pin, p.EXTI4.degrade());
let iv = Stm32l0InterfaceVariant::new(nss, reset, irq, None, None).unwrap();
let mut delay = Delay;
let mut lora = {
match LoRa::new(SX1276_7_8_9::new(BoardType::Stm32l0Sx1276, spi, iv), false, &mut delay).await {
Ok(l) => l,
Err(err) => {
info!("Radio error = {}", err);
return;
}
}
};
let mdltn_params = {
match lora.create_modulation_params(
SpreadingFactor::_10,
Bandwidth::_250KHz,
CodingRate::_4_8,
LORA_FREQUENCY_IN_HZ,
) {
Ok(mp) => mp,
Err(err) => {
info!("Radio error = {}", err);
return;
}
}
};
let mut tx_pkt_params = {
match lora.create_tx_packet_params(4, false, true, false, &mdltn_params) {
Ok(pp) => pp,
Err(err) => {
info!("Radio error = {}", err);
return;
}
}
};
match lora.prepare_for_tx(&mdltn_params, 17, true).await {
Ok(()) => {}
Err(err) => {
info!("Radio error = {}", err);
return;
}
};
let buffer = [0x01u8, 0x02u8, 0x03u8];
match lora.tx(&mdltn_params, &mut tx_pkt_params, &buffer, 0xffffff).await {
Ok(()) => {
info!("TX DONE");
}
Err(err) => {
info!("Radio error = {}", err);
return;
}
};
match lora.sleep(&mut delay).await {
Ok(()) => info!("Sleep successful"),
Err(err) => info!("Sleep unsuccessful = {}", err),
}
}

View File

@ -1,74 +0,0 @@
//! This example runs on the STM32 LoRa Discovery board which has a builtin Semtech Sx127x radio
#![no_std]
#![no_main]
#![macro_use]
#![allow(dead_code)]
#![feature(type_alias_impl_trait)]
use embassy_executor::Spawner;
use embassy_lora::sx127x::*;
use embassy_lora::LoraTimer;
use embassy_stm32::exti::ExtiInput;
use embassy_stm32::gpio::{Input, Level, Output, Pull, Speed};
use embassy_stm32::rng::Rng;
use embassy_stm32::spi;
use embassy_stm32::time::khz;
use lorawan::default_crypto::DefaultFactory as Crypto;
use lorawan_device::async_device::{region, Device, JoinMode};
use {defmt_rtt as _, panic_probe as _};
#[embassy_executor::main]
async fn main(_spawner: Spawner) {
let mut config = embassy_stm32::Config::default();
config.rcc.mux = embassy_stm32::rcc::ClockSrc::HSI16;
config.rcc.enable_hsi48 = true;
let p = embassy_stm32::init(config);
// SPI for sx127x
let spi = spi::Spi::new(
p.SPI1,
p.PB3,
p.PA7,
p.PA6,
p.DMA1_CH3,
p.DMA1_CH2,
khz(200),
spi::Config::default(),
);
let cs = Output::new(p.PA15, Level::High, Speed::Low);
let reset = Output::new(p.PC0, Level::High, Speed::Low);
let _ = Input::new(p.PB1, Pull::None);
let ready = Input::new(p.PB4, Pull::Up);
let ready_pin = ExtiInput::new(ready, p.EXTI4);
let radio = Sx127xRadio::new(spi, cs, reset, ready_pin, DummySwitch).await.unwrap();
let region = region::Configuration::new(region::Region::EU868);
let mut device: Device<_, Crypto, _, _> = Device::new(region, radio, LoraTimer::new(), Rng::new(p.RNG));
defmt::info!("Joining LoRaWAN network");
// TODO: Adjust the EUI and Keys according to your network credentials
device
.join(&JoinMode::OTAA {
deveui: [0, 0, 0, 0, 0, 0, 0, 0],
appeui: [0, 0, 0, 0, 0, 0, 0, 0],
appkey: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
})
.await
.ok()
.unwrap();
defmt::info!("LoRaWAN network joined");
defmt::info!("Sending 'PING'");
device.send(b"PING", 1, false).await.ok().unwrap();
defmt::info!("Message sent!");
}
pub struct DummySwitch;
impl RadioSwitch for DummySwitch {
fn set_rx(&mut self) {}
fn set_tx(&mut self) {}
}

View File

@ -1,5 +1,5 @@
//! This example runs on a STM32WL board, which has a builtin Semtech Sx1262 radio. //! This example runs on a STM32WL board, which has a builtin Semtech Sx1262 radio.
//! It demonstrates LoRaWAN functionality. //! It demonstrates LoRaWAN join functionality.
#![no_std] #![no_std]
#![no_main] #![no_main]
#![macro_use] #![macro_use]
@ -28,6 +28,8 @@ use lorawan_device::async_device::lora_radio::LoRaRadio;
use lorawan_device::async_device::{region, Device, JoinMode}; use lorawan_device::async_device::{region, Device, JoinMode};
use {defmt_rtt as _, panic_probe as _}; use {defmt_rtt as _, panic_probe as _};
const LORAWAN_REGION: region::Region = region::Region::EU868; // warning: set this appropriately for the region
#[embassy_executor::main] #[embassy_executor::main]
async fn main(_spawner: Spawner) { async fn main(_spawner: Spawner) {
let mut config = embassy_stm32::Config::default(); let mut config = embassy_stm32::Config::default();
@ -65,7 +67,7 @@ async fn main(_spawner: Spawner) {
} }
}; };
let radio = LoRaRadio::new(lora); let radio = LoRaRadio::new(lora);
let region: region::Configuration = region::Configuration::new(region::Region::EU868); let region: region::Configuration = region::Configuration::new(LORAWAN_REGION);
let mut device: Device<_, Crypto, _, _> = Device::new(region, radio, LoraTimer::new(), Rng::new(p.RNG)); let mut device: Device<_, Crypto, _, _> = Device::new(region, radio, LoraTimer::new(), Rng::new(p.RNG));
defmt::info!("Joining LoRaWAN network"); defmt::info!("Joining LoRaWAN network");

View File

@ -0,0 +1,127 @@
//! This example runs on the STM32WL board, which has a builtin Semtech Sx1262 radio.
//! It demonstrates LORA P2P receive functionality in conjunction with the lora_p2p_send example.
#![no_std]
#![no_main]
#![macro_use]
#![feature(type_alias_impl_trait, async_fn_in_trait)]
#![allow(incomplete_features)]
use defmt::info;
use embassy_embedded_hal::adapter::BlockingAsync;
use embassy_executor::Spawner;
use embassy_lora::iv::Stm32wlInterfaceVariant;
use embassy_stm32::dma::NoDma;
use embassy_stm32::gpio::{Level, Output, Pin, Speed};
use embassy_stm32::peripherals::SUBGHZSPI;
use embassy_stm32::rcc::low_level::RccPeripheral;
use embassy_stm32::spi::{BitOrder, Config as SpiConfig, Spi, MODE_0};
use embassy_stm32::time::Hertz;
use embassy_stm32::{interrupt, into_ref, Peripheral};
use embassy_time::{Delay, Duration, Timer};
use lora_phy::mod_params::*;
use lora_phy::sx1261_2::SX1261_2;
use lora_phy::LoRa;
use {defmt_rtt as _, panic_probe as _};
const LORA_FREQUENCY_IN_HZ: u32 = 903_900_000; // warning: set this appropriately for the region
#[embassy_executor::main]
async fn main(_spawner: Spawner) {
let mut config = embassy_stm32::Config::default();
config.rcc.mux = embassy_stm32::rcc::ClockSrc::HSE32;
let p = embassy_stm32::init(config);
let clk = Hertz(core::cmp::min(SUBGHZSPI::frequency().0 / 2, 16_000_000));
let mut spi_config = SpiConfig::default();
spi_config.mode = MODE_0;
spi_config.bit_order = BitOrder::MsbFirst;
let spi = Spi::new_subghz(p.SUBGHZSPI, NoDma, NoDma, clk, spi_config);
let spi = BlockingAsync::new(spi);
let irq = interrupt::take!(SUBGHZ_RADIO);
into_ref!(irq);
// Set CTRL1 and CTRL3 for high-power transmission, while CTRL2 acts as an RF switch between tx and rx
let _ctrl1 = Output::new(p.PC4.degrade(), Level::Low, Speed::High);
let ctrl2 = Output::new(p.PC5.degrade(), Level::High, Speed::High);
let _ctrl3 = Output::new(p.PC3.degrade(), Level::High, Speed::High);
let iv = Stm32wlInterfaceVariant::new(irq, None, Some(ctrl2)).unwrap();
let mut delay = Delay;
let mut lora = {
match LoRa::new(SX1261_2::new(BoardType::Stm32wlSx1262, spi, iv), false, &mut delay).await {
Ok(l) => l,
Err(err) => {
info!("Radio error = {}", err);
return;
}
}
};
let mut debug_indicator = Output::new(p.PB9, Level::Low, Speed::Low);
let mut start_indicator = Output::new(p.PB15, Level::Low, Speed::Low);
start_indicator.set_high();
Timer::after(Duration::from_secs(5)).await;
start_indicator.set_low();
let mut receiving_buffer = [00u8; 100];
let mdltn_params = {
match lora.create_modulation_params(
SpreadingFactor::_10,
Bandwidth::_250KHz,
CodingRate::_4_8,
LORA_FREQUENCY_IN_HZ,
) {
Ok(mp) => mp,
Err(err) => {
info!("Radio error = {}", err);
return;
}
}
};
let rx_pkt_params = {
match lora.create_rx_packet_params(4, false, receiving_buffer.len() as u8, true, false, &mdltn_params) {
Ok(pp) => pp,
Err(err) => {
info!("Radio error = {}", err);
return;
}
}
};
match lora
.prepare_for_rx(&mdltn_params, &rx_pkt_params, None, true, false, 0, 0x00ffffffu32)
.await
{
Ok(()) => {}
Err(err) => {
info!("Radio error = {}", err);
return;
}
};
loop {
receiving_buffer = [00u8; 100];
match lora.rx(&rx_pkt_params, &mut receiving_buffer).await {
Ok((received_len, _rx_pkt_status)) => {
if (received_len == 3)
&& (receiving_buffer[0] == 0x01u8)
&& (receiving_buffer[1] == 0x02u8)
&& (receiving_buffer[2] == 0x03u8)
{
info!("rx successful");
debug_indicator.set_high();
Timer::after(Duration::from_secs(5)).await;
debug_indicator.set_low();
} else {
info!("rx unknown packet");
}
}
Err(err) => info!("rx unsuccessful = {}", err),
}
}
}

View File

@ -23,6 +23,8 @@ use lora_phy::sx1261_2::SX1261_2;
use lora_phy::LoRa; use lora_phy::LoRa;
use {defmt_rtt as _, panic_probe as _}; use {defmt_rtt as _, panic_probe as _};
const LORA_FREQUENCY_IN_HZ: u32 = 903_900_000; // warning: set this appropriately for the region
#[embassy_executor::main] #[embassy_executor::main]
async fn main(_spawner: Spawner) { async fn main(_spawner: Spawner) {
let mut config = embassy_stm32::Config::default(); let mut config = embassy_stm32::Config::default();
@ -58,7 +60,12 @@ async fn main(_spawner: Spawner) {
}; };
let mdltn_params = { let mdltn_params = {
match lora.create_modulation_params(SpreadingFactor::_10, Bandwidth::_250KHz, CodingRate::_4_8, 903900000) { match lora.create_modulation_params(
SpreadingFactor::_10,
Bandwidth::_250KHz,
CodingRate::_4_8,
LORA_FREQUENCY_IN_HZ,
) {
Ok(mp) => mp, Ok(mp) => mp,
Err(err) => { Err(err) => {
info!("Radio error = {}", err); info!("Radio error = {}", err);

View File

@ -1,104 +0,0 @@
#![no_std]
#![no_main]
#![macro_use]
#![allow(dead_code)]
#![feature(type_alias_impl_trait)]
use embassy_executor::Spawner;
use embassy_lora::stm32wl::*;
use embassy_lora::LoraTimer;
use embassy_stm32::dma::NoDma;
use embassy_stm32::gpio::{AnyPin, Level, Output, Pin, Speed};
use embassy_stm32::rng::Rng;
use embassy_stm32::subghz::*;
use embassy_stm32::{interrupt, pac};
use lorawan::default_crypto::DefaultFactory as Crypto;
use lorawan_device::async_device::{region, Device, JoinMode};
use {defmt_rtt as _, panic_probe as _};
struct RadioSwitch<'a> {
ctrl1: Output<'a, AnyPin>,
ctrl2: Output<'a, AnyPin>,
ctrl3: Output<'a, AnyPin>,
}
impl<'a> RadioSwitch<'a> {
fn new(ctrl1: Output<'a, AnyPin>, ctrl2: Output<'a, AnyPin>, ctrl3: Output<'a, AnyPin>) -> Self {
Self { ctrl1, ctrl2, ctrl3 }
}
}
impl<'a> embassy_lora::stm32wl::RadioSwitch for RadioSwitch<'a> {
fn set_rx(&mut self) {
self.ctrl1.set_high();
self.ctrl2.set_low();
self.ctrl3.set_high();
}
fn set_tx(&mut self) {
self.ctrl1.set_high();
self.ctrl2.set_high();
self.ctrl3.set_high();
}
}
#[embassy_executor::main]
async fn main(_spawner: Spawner) {
let mut config = embassy_stm32::Config::default();
config.rcc.mux = embassy_stm32::rcc::ClockSrc::HSI16;
config.rcc.enable_lsi = true;
let p = embassy_stm32::init(config);
unsafe { pac::RCC.ccipr().modify(|w| w.set_rngsel(0b01)) }
let ctrl1 = Output::new(p.PC3.degrade(), Level::High, Speed::High);
let ctrl2 = Output::new(p.PC4.degrade(), Level::High, Speed::High);
let ctrl3 = Output::new(p.PC5.degrade(), Level::High, Speed::High);
let rfs = RadioSwitch::new(ctrl1, ctrl2, ctrl3);
let radio = SubGhz::new(p.SUBGHZSPI, NoDma, NoDma);
let irq = interrupt::take!(SUBGHZ_RADIO);
let mut radio_config = SubGhzRadioConfig::default();
radio_config.calibrate_image = CalibrateImage::ISM_863_870;
let radio = SubGhzRadio::new(radio, rfs, irq, radio_config).unwrap();
let mut region = region::Configuration::new(region::Region::EU868);
// NOTE: This is specific for TTN, as they have a special RX1 delay
region.set_receive_delay1(5000);
let mut device: Device<_, Crypto, _, _> = Device::new(region, radio, LoraTimer::new(), Rng::new(p.RNG));
// Depending on network, this might be part of JOIN
device.set_datarate(region::DR::_0); // SF12
// device.set_datarate(region::DR::_1); // SF11
// device.set_datarate(region::DR::_2); // SF10
// device.set_datarate(region::DR::_3); // SF9
// device.set_datarate(region::DR::_4); // SF8
// device.set_datarate(region::DR::_5); // SF7
defmt::info!("Joining LoRaWAN network");
// TODO: Adjust the EUI and Keys according to your network credentials
device
.join(&JoinMode::OTAA {
deveui: [0, 0, 0, 0, 0, 0, 0, 0],
appeui: [0, 0, 0, 0, 0, 0, 0, 0],
appkey: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
})
.await
.ok()
.unwrap();
defmt::info!("LoRaWAN network joined");
let mut rx: [u8; 255] = [0; 255];
defmt::info!("Sending 'PING'");
let len = device.send_recv(b"PING", &mut rx[..], 1, true).await.ok().unwrap();
if len > 0 {
defmt::info!("Message sent, received downlink: {:?}", &rx[..len]);
} else {
defmt::info!("Message sent!");
}
}