Update to rust-lorawan with afit support

This commit is contained in:
Ulf Lilleengen 2023-04-14 23:01:13 +02:00 committed by Dario Nieuwenhuis
parent 224eaaf797
commit 63941432e3
10 changed files with 142 additions and 186 deletions

View file

@ -38,5 +38,5 @@ futures = { version = "0.3.17", default-features = false, features = [ "async-aw
embedded-hal = { version = "0.2", features = ["unproven"] } embedded-hal = { version = "0.2", features = ["unproven"] }
bit_field = { version = "0.10" } bit_field = { version = "0.10" }
lorawan-device = { version = "0.8.0", default-features = false, features = ["async"] } lorawan-device = { version = "0.8.0", git = "https://github.com/ivajloip/rust-lorawan.git", rev = "7d3eb40bc2412536c846cea40caff25198b6b068", default-features = false, features = ["async"] }
lorawan = { version = "0.7.1", default-features = false } lorawan = { version = "0.7.1", git = "https://github.com/ivajloip/rust-lorawan.git", rev = "7d3eb40bc2412536c846cea40caff25198b6b068", default-features = false }

View file

@ -1,5 +1,6 @@
#![no_std] #![no_std]
#![feature(type_alias_impl_trait)] #![feature(async_fn_in_trait, impl_trait_projections)]
#![allow(incomplete_features)]
//! embassy-lora is a collection of async radio drivers that integrate with the lorawan-device //! embassy-lora is a collection of async radio drivers that integrate with the lorawan-device
//! crate's async LoRaWAN MAC implementation. //! crate's async LoRaWAN MAC implementation.
@ -34,13 +35,11 @@ impl lorawan_device::async_device::radio::Timer for LoraTimer {
self.start = Instant::now(); self.start = Instant::now();
} }
type AtFuture<'m> = impl core::future::Future<Output = ()> + 'm; async fn at(&mut self, millis: u64) {
fn at<'m>(&'m mut self, millis: u64) -> Self::AtFuture<'m> { Timer::at(self.start + Duration::from_millis(millis)).await
Timer::at(self.start + Duration::from_millis(millis))
} }
type DelayFuture<'m> = impl core::future::Future<Output = ()> + 'm; async fn delay_ms(&mut self, millis: u64) {
fn delay_ms<'m>(&'m mut self, millis: u64) -> Self::DelayFuture<'m> { Timer::after(Duration::from_millis(millis)).await
Timer::after(Duration::from_millis(millis))
} }
} }

View file

@ -1,5 +1,5 @@
//! A radio driver integration for the radio found on STM32WL family devices. //! A radio driver integration for the radio found on STM32WL family devices.
use core::future::{poll_fn, Future}; use core::future::poll_fn;
use core::task::Poll; use core::task::Poll;
use embassy_hal_common::{into_ref, Peripheral, PeripheralRef}; use embassy_hal_common::{into_ref, Peripheral, PeripheralRef};
@ -241,14 +241,12 @@ fn configure_radio(radio: &mut SubGhz<'_, NoDma, NoDma>, config: SubGhzRadioConf
impl<'d, RS: RadioSwitch> PhyRxTx for SubGhzRadio<'d, RS> { impl<'d, RS: RadioSwitch> PhyRxTx for SubGhzRadio<'d, RS> {
type PhyError = RadioError; type PhyError = RadioError;
type TxFuture<'m> = impl Future<Output = Result<u32, Self::PhyError>> + 'm where Self: 'm; async fn tx(&mut self, config: TxConfig, buf: &[u8]) -> Result<u32, Self::PhyError> {
fn tx<'m>(&'m mut self, config: TxConfig, buf: &'m [u8]) -> Self::TxFuture<'m> { self.do_tx(config, buf).await
async move { self.do_tx(config, buf).await }
} }
type RxFuture<'m> = impl Future<Output = Result<(usize, RxQuality), Self::PhyError>> + 'm where Self: 'm; async fn rx(&mut self, config: RfConfig, buf: &mut [u8]) -> Result<(usize, RxQuality), Self::PhyError> {
fn rx<'m>(&'m mut self, config: RfConfig, buf: &'m mut [u8]) -> Self::RxFuture<'m> { self.do_rx(config, buf).await
async move { self.do_rx(config, buf).await }
} }
} }

View file

@ -1,5 +1,3 @@
use core::future::Future;
use defmt::Format; use defmt::Format;
use embedded_hal::digital::v2::OutputPin; use embedded_hal::digital::v2::OutputPin;
use embedded_hal_async::digital::Wait; use embedded_hal_async::digital::Wait;
@ -71,83 +69,69 @@ where
{ {
type PhyError = RadioError<BUS>; type PhyError = RadioError<BUS>;
type TxFuture<'m> = impl Future<Output = Result<u32, Self::PhyError>> + 'm async fn tx(&mut self, config: TxConfig, buffer: &[u8]) -> Result<u32, Self::PhyError> {
where
SPI: 'm,
CTRL: 'm,
WAIT: 'm,
BUS: 'm;
fn tx<'m>(&'m mut self, config: TxConfig, buffer: &'m [u8]) -> Self::TxFuture<'m> {
trace!("TX START"); trace!("TX START");
async move { self.lora
self.lora .set_tx_config(
.set_tx_config( config.pw,
config.pw, config.rf.spreading_factor.into(),
config.rf.spreading_factor.into(), config.rf.bandwidth.into(),
config.rf.bandwidth.into(), config.rf.coding_rate.into(),
config.rf.coding_rate.into(), 8,
8, false,
false, true,
true, false,
false, 0,
0, false,
false, )
) .await?;
.await?; self.lora.set_max_payload_length(buffer.len() as u8).await?;
self.lora.set_max_payload_length(buffer.len() as u8).await?; self.lora.set_channel(config.rf.frequency).await?;
self.lora.set_channel(config.rf.frequency).await?; self.lora.send(buffer, 0xffffff).await?;
self.lora.send(buffer, 0xffffff).await?; self.lora.process_irq(None, None, None).await?;
self.lora.process_irq(None, None, None).await?; trace!("TX DONE");
trace!("TX DONE"); return Ok(0);
return Ok(0);
}
} }
type RxFuture<'m> = impl Future<Output = Result<(usize, RxQuality), Self::PhyError>> + 'm async fn rx(
where &mut self,
SPI: 'm, config: RfConfig,
CTRL: 'm, receiving_buffer: &mut [u8],
WAIT: 'm, ) -> Result<(usize, RxQuality), Self::PhyError> {
BUS: 'm;
fn rx<'m>(&'m mut self, config: RfConfig, receiving_buffer: &'m mut [u8]) -> Self::RxFuture<'m> {
trace!("RX START"); trace!("RX START");
async move { self.lora
self.lora .set_rx_config(
.set_rx_config( config.spreading_factor.into(),
config.spreading_factor.into(), config.bandwidth.into(),
config.bandwidth.into(), config.coding_rate.into(),
config.coding_rate.into(), 8,
8, 4,
4, false,
false, 0u8,
0u8, true,
true, false,
false, 0,
0, true,
true, true,
true, )
) .await?;
.await?; self.lora.set_max_payload_length(receiving_buffer.len() as u8).await?;
self.lora.set_max_payload_length(receiving_buffer.len() as u8).await?; self.lora.set_channel(config.frequency).await?;
self.lora.set_channel(config.frequency).await?; self.lora.rx(90 * 1000).await?;
self.lora.rx(90 * 1000).await?; let mut received_len = 0u8;
let mut received_len = 0u8; self.lora
self.lora .process_irq(Some(receiving_buffer), Some(&mut received_len), None)
.process_irq(Some(receiving_buffer), Some(&mut received_len), None) .await?;
.await?; trace!("RX DONE");
trace!("RX DONE");
let packet_status = self.lora.get_latest_packet_status(); let packet_status = self.lora.get_latest_packet_status();
let mut rssi = 0i16; let mut rssi = 0i16;
let mut snr = 0i8; let mut snr = 0i8;
if packet_status.is_some() { if packet_status.is_some() {
rssi = packet_status.unwrap().rssi as i16; rssi = packet_status.unwrap().rssi as i16;
snr = packet_status.unwrap().snr; snr = packet_status.unwrap().snr;
}
Ok((received_len as usize, RxQuality::new(rssi, snr)))
} }
Ok((received_len as usize, RxQuality::new(rssi, snr)))
} }
} }

View file

@ -1,5 +1,3 @@
use core::future::Future;
use embedded_hal::digital::v2::OutputPin; use embedded_hal::digital::v2::OutputPin;
use embedded_hal_async::digital::Wait; use embedded_hal_async::digital::Wait;
use embedded_hal_async::spi::*; use embedded_hal_async::spi::*;
@ -88,101 +86,78 @@ where
{ {
type PhyError = Sx127xError; type PhyError = Sx127xError;
type TxFuture<'m> = impl Future<Output = Result<u32, Self::PhyError>> + 'm async fn tx(&mut self, config: TxConfig, buf: &[u8]) -> Result<u32, Self::PhyError> {
where
SPI: 'm,
CS: 'm,
RESET: 'm,
E: 'm,
I: 'm,
RFS: 'm;
fn tx<'m>(&'m mut self, config: TxConfig, buf: &'m [u8]) -> Self::TxFuture<'m> {
trace!("TX START"); trace!("TX START");
async move { self.radio.set_mode(RadioMode::Stdby).await.ok().unwrap();
self.rfs.set_tx();
self.radio.set_tx_power(14, 0).await?;
self.radio.set_frequency(config.rf.frequency).await?;
// TODO: Modify radio to support other coding rates
self.radio.set_coding_rate_4(5).await?;
self.radio
.set_signal_bandwidth(bandwidth_to_i64(config.rf.bandwidth))
.await?;
self.radio
.set_spreading_factor(spreading_factor_to_u8(config.rf.spreading_factor))
.await?;
self.radio.set_preamble_length(8).await?;
self.radio.set_lora_pa_ramp().await?;
self.radio.set_lora_sync_word().await?;
self.radio.set_invert_iq(false).await?;
self.radio.set_crc(true).await?;
self.radio.set_dio0_tx_done().await?;
self.radio.transmit_start(buf).await?;
loop {
self.irq.wait_for_rising_edge().await.unwrap();
self.radio.set_mode(RadioMode::Stdby).await.ok().unwrap(); self.radio.set_mode(RadioMode::Stdby).await.ok().unwrap();
self.rfs.set_tx(); let irq = self.radio.clear_irq().await.ok().unwrap();
self.radio.set_tx_power(14, 0).await?; if (irq & IRQ::IrqTxDoneMask.addr()) != 0 {
self.radio.set_frequency(config.rf.frequency).await?; trace!("TX DONE");
// TODO: Modify radio to support other coding rates return Ok(0);
self.radio.set_coding_rate_4(5).await?;
self.radio
.set_signal_bandwidth(bandwidth_to_i64(config.rf.bandwidth))
.await?;
self.radio
.set_spreading_factor(spreading_factor_to_u8(config.rf.spreading_factor))
.await?;
self.radio.set_preamble_length(8).await?;
self.radio.set_lora_pa_ramp().await?;
self.radio.set_lora_sync_word().await?;
self.radio.set_invert_iq(false).await?;
self.radio.set_crc(true).await?;
self.radio.set_dio0_tx_done().await?;
self.radio.transmit_start(buf).await?;
loop {
self.irq.wait_for_rising_edge().await.unwrap();
self.radio.set_mode(RadioMode::Stdby).await.ok().unwrap();
let irq = self.radio.clear_irq().await.ok().unwrap();
if (irq & IRQ::IrqTxDoneMask.addr()) != 0 {
trace!("TX DONE");
return Ok(0);
}
} }
} }
} }
type RxFuture<'m> = impl Future<Output = Result<(usize, RxQuality), Self::PhyError>> + 'm async fn rx(&mut self, config: RfConfig, buf: &mut [u8]) -> Result<(usize, RxQuality), Self::PhyError> {
where self.rfs.set_rx();
SPI: 'm, self.radio.reset_payload_length().await?;
CS: 'm, self.radio.set_frequency(config.frequency).await?;
RESET: 'm, // TODO: Modify radio to support other coding rates
E: 'm, self.radio.set_coding_rate_4(5).await?;
I: 'm, self.radio
RFS: 'm; .set_signal_bandwidth(bandwidth_to_i64(config.bandwidth))
.await?;
self.radio
.set_spreading_factor(spreading_factor_to_u8(config.spreading_factor))
.await?;
fn rx<'m>(&'m mut self, config: RfConfig, buf: &'m mut [u8]) -> Self::RxFuture<'m> { self.radio.set_preamble_length(8).await?;
trace!("RX START"); self.radio.set_lora_sync_word().await?;
async move { self.radio.set_invert_iq(true).await?;
self.rfs.set_rx(); self.radio.set_crc(true).await?;
self.radio.reset_payload_length().await?;
self.radio.set_frequency(config.frequency).await?;
// TODO: Modify radio to support other coding rates
self.radio.set_coding_rate_4(5).await?;
self.radio
.set_signal_bandwidth(bandwidth_to_i64(config.bandwidth))
.await?;
self.radio
.set_spreading_factor(spreading_factor_to_u8(config.spreading_factor))
.await?;
self.radio.set_preamble_length(8).await?; self.radio.set_dio0_rx_done().await?;
self.radio.set_lora_sync_word().await?; self.radio.set_mode(RadioMode::RxContinuous).await?;
self.radio.set_invert_iq(true).await?;
self.radio.set_crc(true).await?;
self.radio.set_dio0_rx_done().await?; loop {
self.radio.set_mode(RadioMode::RxContinuous).await?; self.irq.wait_for_rising_edge().await.unwrap();
self.radio.set_mode(RadioMode::Stdby).await.ok().unwrap();
loop { let irq = self.radio.clear_irq().await.ok().unwrap();
self.irq.wait_for_rising_edge().await.unwrap(); if (irq & IRQ::IrqRxDoneMask.addr()) != 0 {
self.radio.set_mode(RadioMode::Stdby).await.ok().unwrap(); let rssi = self.radio.get_packet_rssi().await.unwrap_or(0) as i16;
let irq = self.radio.clear_irq().await.ok().unwrap(); let snr = self.radio.get_packet_snr().await.unwrap_or(0.0) as i8;
if (irq & IRQ::IrqRxDoneMask.addr()) != 0 { let response = if let Ok(size) = self.radio.read_packet_size().await {
let rssi = self.radio.get_packet_rssi().await.unwrap_or(0) as i16; self.radio.read_packet(buf).await?;
let snr = self.radio.get_packet_snr().await.unwrap_or(0.0) as i8; Ok((size, RxQuality::new(rssi, snr)))
let response = if let Ok(size) = self.radio.read_packet_size().await { } else {
self.radio.read_packet(buf).await?; Ok((0, RxQuality::new(rssi, snr)))
Ok((size, RxQuality::new(rssi, snr))) };
} else { trace!("RX DONE");
Ok((0, RxQuality::new(rssi, snr))) return response;
};
trace!("RX DONE");
return response;
}
} }
} }
} }

View file

@ -20,8 +20,8 @@ embassy-usb = { version = "0.1.0", path = "../../embassy-usb", features = ["defm
embedded-io = "0.4.0" embedded-io = "0.4.0"
embassy-lora = { version = "0.1.0", path = "../../embassy-lora", features = ["sx126x", "time", "defmt"], optional = true } embassy-lora = { version = "0.1.0", path = "../../embassy-lora", features = ["sx126x", "time", "defmt"], optional = true }
lorawan-device = { version = "0.8.0", default-features = false, features = ["async"], optional = true } lorawan-device = { version = "0.8.0", git = "https://github.com/ivajloip/rust-lorawan.git", rev = "7d3eb40bc2412536c846cea40caff25198b6b068", default-features = false, features = ["async"], optional = true }
lorawan = { version = "0.7.1", default-features = false, features = ["default-crypto"], optional = true } lorawan = { version = "0.7.1", git = "https://github.com/ivajloip/rust-lorawan.git", rev = "7d3eb40bc2412536c846cea40caff25198b6b068", default-features = false, features = ["default-crypto"], optional = true }
defmt = "0.3" defmt = "0.3"
defmt-rtt = "0.4" defmt-rtt = "0.4"

View file

@ -15,8 +15,8 @@ embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["de
embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["defmt", "stm32l072cz", "time-driver-any", "exti", "unstable-traits", "memory-x"] } embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["defmt", "stm32l072cz", "time-driver-any", "exti", "unstable-traits", "memory-x"] }
embassy-lora = { version = "0.1.0", path = "../../embassy-lora", features = ["sx127x", "time", "defmt"], optional = true} embassy-lora = { version = "0.1.0", path = "../../embassy-lora", features = ["sx127x", "time", "defmt"], optional = true}
lorawan-device = { version = "0.8.0", default-features = false, features = ["async"], optional = true } lorawan-device = { version = "0.8.0", git = "https://github.com/ivajloip/rust-lorawan.git", rev = "7d3eb40bc2412536c846cea40caff25198b6b068", default-features = false, features = ["async"], optional = true }
lorawan = { version = "0.7.1", default-features = false, features = ["default-crypto"], optional = true } lorawan = { version = "0.7.1", git = "https://github.com/ivajloip/rust-lorawan.git", rev = "7d3eb40bc2412536c846cea40caff25198b6b068", default-features = false, features = ["default-crypto"], optional = true }
defmt = "0.3" defmt = "0.3"
defmt-rtt = "0.4" defmt-rtt = "0.4"

View file

@ -45,7 +45,7 @@ async fn main(_spawner: Spawner) {
let radio = Sx127xRadio::new(spi, cs, reset, ready_pin, DummySwitch).await.unwrap(); let radio = Sx127xRadio::new(spi, cs, reset, ready_pin, DummySwitch).await.unwrap();
let region = region::EU868::default().into(); let region = region::Configuration::new(region::Region::EU868);
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

@ -11,8 +11,8 @@ embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["de
embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "defmt", "stm32wl55jc-cm4", "time-driver-any", "memory-x", "unstable-pac", "exti"] } embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["nightly", "defmt", "stm32wl55jc-cm4", "time-driver-any", "memory-x", "unstable-pac", "exti"] }
embassy-lora = { version = "0.1.0", path = "../../embassy-lora", features = ["stm32wl", "time", "defmt"] } embassy-lora = { version = "0.1.0", path = "../../embassy-lora", features = ["stm32wl", "time", "defmt"] }
lorawan-device = { version = "0.8.0", default-features = false, features = ["async"] } lorawan-device = { version = "0.8.0", git = "https://github.com/ivajloip/rust-lorawan.git", rev = "7d3eb40bc2412536c846cea40caff25198b6b068", default-features = false, features = ["async"] }
lorawan = { version = "0.7.1", default-features = false, features = ["default-crypto"] } lorawan = { version = "0.7.1", git = "https://github.com/ivajloip/rust-lorawan.git", rev = "7d3eb40bc2412536c846cea40caff25198b6b068", default-features = false, features = ["default-crypto"] }
defmt = "0.3" defmt = "0.3"
defmt-rtt = "0.4" defmt-rtt = "0.4"

View file

@ -63,7 +63,7 @@ async fn main(_spawner: Spawner) {
radio_config.calibrate_image = CalibrateImage::ISM_863_870; radio_config.calibrate_image = CalibrateImage::ISM_863_870;
let radio = SubGhzRadio::new(radio, rfs, irq, radio_config).unwrap(); let radio = SubGhzRadio::new(radio, rfs, irq, radio_config).unwrap();
let mut region: region::Configuration = region::EU868::default().into(); let mut region = region::Configuration::new(region::Region::EU868);
// NOTE: This is specific for TTN, as they have a special RX1 delay // NOTE: This is specific for TTN, as they have a special RX1 delay
region.set_receive_delay1(5000); region.set_receive_delay1(5000);