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

@ -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),
}
}
}