From 16a47a0ad9eb5f40aa1f202d3fbaa03c7b77b836 Mon Sep 17 00:00:00 2001 From: Ulf Lilleengen Date: Thu, 30 Sep 2021 09:25:45 +0200 Subject: Add embassy-lora crate This crate contains async radio drivers for various lora drivers that work with embassy timers. The code is imported from Drogue Device ( https://github.com/drogue-iot/drogue-device) The radio drivers integrate with the async LoRaWAN MAC layer in the lorawan-device crate. Also added is an example for the STM32WL55 and for STM32L0 (requires the LoRa Discovery board) for LoRaWAN. Future work is to make the underlying radio drivers using fully async SPI when communicating with the peripheral. --- examples/stm32l0/Cargo.toml | 4 ++ examples/stm32l0/src/bin/lorawan.rs | 104 ++++++++++++++++++++++++++++++++++ examples/stm32wl55/Cargo.toml | 6 +- examples/stm32wl55/src/bin/lorawan.rs | 79 ++++++++++++++++++++++++++ 4 files changed, 192 insertions(+), 1 deletion(-) create mode 100644 examples/stm32l0/src/bin/lorawan.rs create mode 100644 examples/stm32wl55/src/bin/lorawan.rs (limited to 'examples') diff --git a/examples/stm32l0/Cargo.toml b/examples/stm32l0/Cargo.toml index 7dfcdb0fe..371ac68cc 100644 --- a/examples/stm32l0/Cargo.toml +++ b/examples/stm32l0/Cargo.toml @@ -23,6 +23,10 @@ embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = [" embassy-hal-common = {version = "0.1.0", path = "../../embassy-hal-common" } embassy-macros = { path = "../../embassy-macros" } +embassy-lora = { version = "0.1.0", path = "../../embassy-lora", features = ["sx127x", "time"] } +lorawan-device = { git = "https://github.com/lulf/rust-lorawan.git", rev = "a373d06fa8858d251bc70d5789cebcd9a638ec42", default-features = false, features = ["async"] } +lorawan-encoding = { git = "https://github.com/lulf/rust-lorawan.git", rev = "a373d06fa8858d251bc70d5789cebcd9a638ec42", default-features = false, features = ["default-crypto"] } + defmt = "0.2.3" defmt-rtt = "0.2.0" diff --git a/examples/stm32l0/src/bin/lorawan.rs b/examples/stm32l0/src/bin/lorawan.rs new file mode 100644 index 000000000..5ca69f9a7 --- /dev/null +++ b/examples/stm32l0/src/bin/lorawan.rs @@ -0,0 +1,104 @@ +//! 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(generic_associated_types)] +#![feature(type_alias_impl_trait)] + +#[path = "../example_common.rs"] +mod example_common; + +use embassy_lora::{sx127x::*, LoraTimer}; +use embassy_stm32::{ + dbgmcu::Dbgmcu, + dma::NoDma, + exti::ExtiInput, + gpio::{Input, Level, Output, Pull, Speed}, + rcc, + rng::Rng, + spi, + time::U32Ext, + Peripherals, +}; +use lorawan_device::async_device::{region, Device, JoinMode}; +use lorawan_encoding::default_crypto::DefaultFactory as Crypto; + +fn config() -> embassy_stm32::Config { + let mut config = embassy_stm32::Config::default(); + config.rcc = config.rcc.clock_src(embassy_stm32::rcc::ClockSrc::HSI16); + config +} + +#[embassy::main(config = "config()")] +async fn main(_spawner: embassy::executor::Spawner, mut p: Peripherals) { + unsafe { + Dbgmcu::enable_all(); + } + + let mut rcc = rcc::Rcc::new(p.RCC); + let _ = rcc.enable_hsi48(&mut p.SYSCFG, p.CRS); + + // SPI for sx127x + let spi = spi::Spi::new( + p.SPI1, + p.PB3, + p.PA7, + p.PA6, + NoDma, + NoDma, + 200_000.hz(), + 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, + &mut embassy::time::Delay, + ) + .unwrap(); + + let region = region::EU868::default().into(); + let mut radio_buffer = [0; 256]; + let mut device: Device<'_, _, Crypto, _, _> = Device::new( + region, + radio, + LoraTimer, + Rng::new(p.RNG), + &mut radio_buffer[..], + ); + + 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) {} +} diff --git a/examples/stm32wl55/Cargo.toml b/examples/stm32wl55/Cargo.toml index d0c727862..4688bdad1 100644 --- a/examples/stm32wl55/Cargo.toml +++ b/examples/stm32wl55/Cargo.toml @@ -19,8 +19,12 @@ defmt-error = [] [dependencies] embassy = { version = "0.1.0", path = "../../embassy", features = ["defmt", "defmt-trace"] } embassy-traits = { version = "0.1.0", path = "../../embassy-traits", features = ["defmt"] } -embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["defmt", "defmt-trace", "stm32wl55jc_cm4", "time-driver-tim2", "memory-x", "subghz"] } +embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["defmt", "defmt-trace", "stm32wl55jc_cm4", "time-driver-tim2", "memory-x", "subghz", "unstable-pac"] } embassy-hal-common = {version = "0.1.0", path = "../../embassy-hal-common" } +embassy-lora = { version = "0.1.0", path = "../../embassy-lora", features = ["stm32wl", "time"] } + +lorawan-device = { git = "https://github.com/lulf/rust-lorawan.git", rev = "a373d06fa8858d251bc70d5789cebcd9a638ec42", default-features = false, features = ["async"] } +lorawan-encoding = { git = "https://github.com/lulf/rust-lorawan.git", rev = "a373d06fa8858d251bc70d5789cebcd9a638ec42", default-features = false, features = ["default-crypto"] } defmt = "0.2.3" defmt-rtt = "0.2.0" diff --git a/examples/stm32wl55/src/bin/lorawan.rs b/examples/stm32wl55/src/bin/lorawan.rs new file mode 100644 index 000000000..155905ae7 --- /dev/null +++ b/examples/stm32wl55/src/bin/lorawan.rs @@ -0,0 +1,79 @@ +#![no_std] +#![no_main] +#![macro_use] +#![allow(dead_code)] +#![feature(generic_associated_types)] +#![feature(type_alias_impl_trait)] + +#[path = "../example_common.rs"] +mod example_common; + +use embassy_lora::{stm32wl::*, LoraTimer}; +use embassy_stm32::{ + dbgmcu::Dbgmcu, + dma::NoDma, + gpio::{Level, Output, Pin, Speed}, + interrupt, pac, rcc, + rng::Rng, + subghz::*, + Peripherals, +}; +use lorawan_device::async_device::{region, Device, JoinMode}; +use lorawan_encoding::default_crypto::DefaultFactory as Crypto; + +fn config() -> embassy_stm32::Config { + let mut config = embassy_stm32::Config::default(); + config.rcc = config.rcc.clock_src(embassy_stm32::rcc::ClockSrc::HSI16); + config +} + +#[embassy::main(config = "config()")] +async fn main(_spawner: embassy::executor::Spawner, p: Peripherals) { + unsafe { + Dbgmcu::enable_all(); + let mut rcc = rcc::Rcc::new(p.RCC); + rcc.enable_lsi(); + 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, p.PA5, p.PA7, p.PA6, NoDma, NoDma); + + let irq = interrupt::take!(SUBGHZ_RADIO); + static mut RADIO_STATE: SubGhzState<'static> = SubGhzState::new(); + let radio = unsafe { SubGhzRadio::new(&mut RADIO_STATE, radio, rfs, irq) }; + + let region = region::EU868::default().into(); + let mut radio_buffer = [0; 256]; + let mut device: Device<'_, _, Crypto, _, _> = Device::new( + region, + radio, + LoraTimer, + Rng::new(p.RNG), + &mut radio_buffer[..], + ); + + 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!"); +} -- cgit