Merge pull request #410 from lulf/embassy-lora

Add embassy-lora crate
This commit is contained in:
Dario Nieuwenhuis
2021-10-10 21:23:02 +02:00
committed by GitHub
11 changed files with 1742 additions and 1 deletions

View File

@ -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"

View File

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

View File

@ -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"

View File

@ -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!");
}