Skip to content

Commit

Permalink
Move dw1000 out of the main crate
Browse files Browse the repository at this point in the history
  • Loading branch information
diondokter committed Feb 17, 2025
1 parent 21c45db commit 78123e7
Show file tree
Hide file tree
Showing 14 changed files with 92 additions and 63 deletions.
9 changes: 9 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,12 @@ jobs:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- run: cargo clippy -- -D warnings

build-no-std:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
with:
targets: thumbv7em-none-eabihf
- run: cargo build --target thumbv7em-none-eabihf --no-default-features --features defmt-03
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[workspace]
resolver = "3"
members = [
"lr-wpan-rs"
"lr-wpan-rs",
"lr-wpan-rs-dw1000"
]
17 changes: 17 additions & 0 deletions lr-wpan-rs-dw1000/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "lr-wpan-rs-dw1000"
version = "0.1.0"
edition = "2021"

[dependencies]
lr-wpan-rs = { path = "../lr-wpan-rs", default-features = false }
dw1000 = { git = "https://github.com/jkelleyrtp/dw1000-rs", rev = "698987dbfebb2db16bf20c166bf0d75c16f982c1" }
embassy-futures = "0.1.1"
embedded-hal = "1.0.0"
embedded-hal-async = "1.0.0"
micromath = "2.1.0"
nb = "1.1.0"
defmt = { version = "0.3.10", optional = true }

[features]
defmt-03 = ["dep:defmt"]
39 changes: 20 additions & 19 deletions lr-wpan-rs/src/phy/dw1000.rs → lr-wpan-rs-dw1000/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![no_std]

use core::fmt::{Debug, Display};

pub use dw1000;
Expand All @@ -7,19 +9,17 @@ use dw1000::{
use embassy_futures::select::{select, Either};
use embedded_hal::{delay::DelayNs as DelayNsSync, digital::ErrorType, spi::SpiDevice};
use embedded_hal_async::{delay::DelayNs, digital::Wait};
#[allow(unused_imports)]
use micromath::F32Ext;

use super::{ModulationType, Phy, ReceivedMessage};
use crate::{
phy::SendContinuation,
use lr_wpan_rs::{
phy::{ModulationType, Phy, ReceivedMessage, SendContinuation},
pib::{
CcaMode, ChannelDescription, NativePrf, PhyPib, PhyPibWrite, TXPowerTolerance,
UwbCurrentPulseShape,
},
time::{Duration, Instant},
ChannelPage,
};
#[allow(unused_imports)]
use micromath::F32Ext;

const TIME_CHECK_INTERVAL_MILLIS: u32 = 5000;
const TIME_CHECK_MILLIS_PER_DELAY: u32 = 100;
Expand Down Expand Up @@ -90,7 +90,7 @@ impl<SPI: SpiDevice, IRQ: Wait, DELAY: DelayNs> DW1000Phy<SPI, IRQ, DELAY> {
}
}

impl<SPI: SpiDevice, IRQ: Wait, DELAY: DelayNs> super::Phy for DW1000Phy<SPI, IRQ, DELAY> {
impl<SPI: SpiDevice, IRQ: Wait, DELAY: DelayNs> Phy for DW1000Phy<SPI, IRQ, DELAY> {
type Error = Error<SPI, IRQ>;

type ProcessingContext = Either<Result<(), IRQ::Error>, ()>;
Expand All @@ -108,7 +108,8 @@ impl<SPI: SpiDevice, IRQ: Wait, DELAY: DelayNs> super::Phy for DW1000Phy<SPI, IR
const SYMBOLS_PER_OCTET: f32 = 9.17648; // Not too sure... This is `8 * (1s / symbol duration in secs (Tdsym)) / 850_000`
const SHR_DURATION: u32 = NUM_PREAMBLE_SYMBOLS + NUM_SFD_SYMBOLS;
let max_frame_duration = SHR_DURATION
+ (((crate::consts::MAX_PHY_PACKET_SIZE + 1) as f32 * SYMBOLS_PER_OCTET).ceil() as u32);
+ (((lr_wpan_rs::consts::MAX_PHY_PACKET_SIZE + 1) as f32 * SYMBOLS_PER_OCTET).ceil()
as u32);

self.phy_pib = PhyPib {
pib_write: PhyPibWrite {
Expand All @@ -118,8 +119,8 @@ impl<SPI: SpiDevice, IRQ: Wait, DELAY: DelayNs> super::Phy for DW1000Phy<SPI, IR
cca_mode: CcaMode::Aloha, // TODO: Not reflected in driver
current_page: UWB_CHANNEL_PAGE,
uwb_current_pulse_shape: UwbCurrentPulseShape::Mandatory, // Only supported shape
uwb_cou_pulse: crate::pib::UwbCouPulse::CCh1,
uwb_cs_pulse: crate::pib::UwbCsPulse::No1,
uwb_cou_pulse: lr_wpan_rs::pib::UwbCouPulse::CCh1,
uwb_cs_pulse: lr_wpan_rs::pib::UwbCsPulse::No1,
uwb_lcp_weight1: 0,
uwb_lcp_weight2: 0,
uwb_lcp_weight3: 0,
Expand Down Expand Up @@ -179,7 +180,7 @@ impl<SPI: SpiDevice, IRQ: Wait, DELAY: DelayNs> super::Phy for DW1000Phy<SPI, IR
Ok(())
}

async fn get_instant(&mut self) -> Result<crate::time::Instant, Self::Error> {
async fn get_instant(&mut self) -> Result<lr_wpan_rs::time::Instant, Self::Error> {
let sys_time = match &mut self.dw1000 {
DW1000::Empty => return Err(Error::WrongState),
DW1000::Ready(dw1000) => dw1000.sys_time()?.value(),
Expand Down Expand Up @@ -209,11 +210,11 @@ impl<SPI: SpiDevice, IRQ: Wait, DELAY: DelayNs> super::Phy for DW1000Phy<SPI, IR
async fn send(
&mut self,
data: &[u8],
send_time: Option<crate::time::Instant>,
send_time: Option<lr_wpan_rs::time::Instant>,
ranging: bool,
use_csma: bool,
continuation: super::SendContinuation,
) -> Result<super::SendResult, Self::Error> {
continuation: lr_wpan_rs::phy::SendContinuation,
) -> Result<lr_wpan_rs::phy::SendResult, Self::Error> {
assert!(!use_csma, "Not supported");
assert!(
!matches!(continuation, SendContinuation::WaitForResponse { .. }),
Expand Down Expand Up @@ -272,7 +273,7 @@ impl<SPI: SpiDevice, IRQ: Wait, DELAY: DelayNs> super::Phy for DW1000Phy<SPI, IR
Err((_dw1000, e)) => {
// No real recovery possible...
#[cfg(feature = "defmt-03")]
panic!("Could not finish sending: {}", defmt::Debug2Format(&e));
defmt::panic!("Could not finish sending: {}", defmt::Debug2Format(&e));
#[cfg(not(feature = "defmt-03"))]
panic!("Could not finish sending: {:?}", e);
}
Expand All @@ -285,7 +286,7 @@ impl<SPI: SpiDevice, IRQ: Wait, DELAY: DelayNs> super::Phy for DW1000Phy<SPI, IR
self.start_receive().await?;
}

Ok(super::SendResult::Success(tx_time))
Ok(lr_wpan_rs::phy::SendResult::Success(tx_time))
}

async fn start_receive(&mut self) -> Result<(), Self::Error> {
Expand Down Expand Up @@ -350,7 +351,7 @@ impl<SPI: SpiDevice, IRQ: Wait, DELAY: DelayNs> super::Phy for DW1000Phy<SPI, IR
Ok(message) => {
let timestamp = self.convert_to_mac_time(message.rx_time).await?;

Ok(Some(super::ReceivedMessage {
Ok(Some(lr_wpan_rs::phy::ReceivedMessage {
timestamp,
data: message.bytes.try_into().unwrap(),
lqi: 255, // TODO
Expand Down Expand Up @@ -379,7 +380,7 @@ impl<SPI: SpiDevice, IRQ: Wait, DELAY: DelayNs> super::Phy for DW1000Phy<SPI, IR

async fn update_phy_pib<U>(
&mut self,
f: impl FnOnce(&mut crate::pib::PhyPibWrite) -> U,
f: impl FnOnce(&mut lr_wpan_rs::pib::PhyPibWrite) -> U,
) -> Result<U, Self::Error> {
let old_pib = self.phy_pib.pib_write.clone();
let old_rx_config = self.current_rx_config;
Expand Down Expand Up @@ -492,7 +493,7 @@ impl<SPI: SpiDevice, IRQ: Wait, DELAY: DelayNs> super::Phy for DW1000Phy<SPI, IR
}
}

async fn get_phy_pib(&mut self) -> Result<&crate::pib::PhyPib, Self::Error> {
async fn get_phy_pib(&mut self) -> Result<&lr_wpan_rs::pib::PhyPib, Self::Error> {
Ok(&self.phy_pib)
}
}
Expand Down
22 changes: 10 additions & 12 deletions lr-wpan-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,28 @@ repository = "https://github.com/tweedegolf/lr-wpan-rs"
readme = "README.md"

[dependencies]
embassy-futures = { git = "https://github.com/embassy-rs/embassy.git", tag = "embassy-stm32-v0.2.0", default-features = false }
embassy-sync = { git = "https://github.com/embassy-rs/embassy.git", tag = "embassy-stm32-v0.2.0" }
embassy-futures = { version = "0.1.1", default-features = false }
embassy-sync = { version = "0.6.2" }

arrayvec = { version = "0.7.6", default-features = false }
critical-section = "1.2.0"
maitake-sync = { version = "0.2.1", default-features = false }
dw1000 = { git = "https://github.com/jkelleyrtp/dw1000-rs", rev = "698987dbfebb2db16bf20c166bf0d75c16f982c1" }
embedded-hal = "1.0.0"
embedded-hal-async = "1.0.0"
nb = "1.1.0"
micromath = "2.1.0"
rand_core = "0.9.0"
pcap-file = { version = "2.0.0", optional = true }
defmt = { version = "0.3.8", optional = true }
log = { version = "0.4.22", optional = true }
arraydeque = { version = "0.5.1", default-features = false }
byte = "0.2.7"
rand = { version = "0.9.0", optional = true }
futures = { version = "0.3.31", default-features = false, features = ["async-await"] }
micromath = "2.1.0"
embedded-hal-async = "1.0.0"

heapless = "0.8.0"
ccm = { version = "0.4.0", default-features = false }
cipher = { version = "0.3.0", default-features = false }

# test-helpers
pcap-file = { version = "2.0.0", optional = true }
log = { version = "0.4.22", optional = true }
tokio = { version = "1.41.0", optional = true, default-features = false, features = ["time", "test-util"] }
rand = { version = "0.9.0", optional = true }

[dev-dependencies]
test-log = "0.2.16"
Expand All @@ -47,6 +45,6 @@ default = ["test_helpers"]
std = ["critical-section/std"]
test_helpers = ["std", "dep:tokio", "log-04", "dep:rand", "dep:pcap-file"]
## Use [`defmt`](https://docs.rs/defmt/latest/defmt/) for logging
defmt-03 = ["dep:defmt"]
defmt-03 = ["dep:defmt", "heapless/defmt-03"]
## Use [`log`](https://docs.rs/log/latest/log/) for logging
log-04 = ["dep:log"]
2 changes: 0 additions & 2 deletions lr-wpan-rs/src/mac/mlme_reset.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#[allow(unused_imports)]
use micromath::F32Ext;
use rand_core::RngCore;

use super::{commander::RequestResponder, state::MacState, MacConfig, MacError};
Expand Down
5 changes: 4 additions & 1 deletion lr-wpan-rs/src/mac/mlme_scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,10 @@ impl ScanProcess<'_> {
}

// Push the descriptor
self.results.pan_descriptor_list.push(pan_descriptor);
self.results
.pan_descriptor_list
.push(pan_descriptor)
.unwrap();
self.results.result_list_size += 1;

// End the scan if full
Expand Down
15 changes: 9 additions & 6 deletions lr-wpan-rs/src/mac/state.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use arraydeque::ArrayDeque;
use arrayvec::ArrayVec;
use heapless::Vec;
use rand_core::RngCore;

use super::{callback::SendCallback, mlme_scan::ScanProcess, MacConfig};
Expand Down Expand Up @@ -57,10 +57,13 @@ impl MacState<'_> {
pub fn serialize_frame(
&mut self,
frame: crate::wire::Frame<'_>,
) -> ArrayVec<u8, { crate::consts::MAX_PHY_PACKET_SIZE }> {
) -> Vec<u8, { crate::consts::MAX_PHY_PACKET_SIZE }> {
use byte::TryWrite;

let mut buffer = ArrayVec::from([0; crate::consts::MAX_PHY_PACKET_SIZE]);
let mut buffer = Vec::new();
buffer
.resize_default(crate::consts::MAX_PHY_PACKET_SIZE)
.unwrap();
let length = frame
.try_write(&mut buffer, &mut self.frame_ser_des_context())
.expect("Buffer is always big enough");
Expand Down Expand Up @@ -107,7 +110,7 @@ pub struct MessageScheduler<'a> {
impl<'a> MessageScheduler<'a> {
pub fn schedule_broadcast_priority(
&mut self,
data: ArrayVec<u8, { crate::consts::MAX_PHY_PACKET_SIZE }>,
data: Vec<u8, { crate::consts::MAX_PHY_PACKET_SIZE }>,
callback: SendCallback<'a>,
) {
if self
Expand All @@ -122,7 +125,7 @@ impl<'a> MessageScheduler<'a> {
#[expect(dead_code, reason = "for future use")]
pub fn schedule_broadcast(
&mut self,
data: ArrayVec<u8, { crate::consts::MAX_PHY_PACKET_SIZE }>,
data: Vec<u8, { crate::consts::MAX_PHY_PACKET_SIZE }>,
callback: SendCallback<'a>,
) {
if self
Expand All @@ -148,7 +151,7 @@ impl<'a> MessageScheduler<'a> {
}

pub struct ScheduledMessage<'a> {
pub data: ArrayVec<u8, { crate::consts::MAX_PHY_PACKET_SIZE }>,
pub data: Vec<u8, { crate::consts::MAX_PHY_PACKET_SIZE }>,
pub callback: SendCallback<'a>,
}

Expand Down
6 changes: 2 additions & 4 deletions lr-wpan-rs/src/phy/mod.rs → lr-wpan-rs/src/phy.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use arrayvec::ArrayVec;
use heapless::Vec;

use crate::{
pib::{PhyPib, PhyPibWrite},
time::{Duration, Instant},
ChannelPage,
};

pub mod dw1000;

pub trait Phy {
#[cfg(not(feature = "defmt-03"))]
type Error: core::error::Error;
Expand Down Expand Up @@ -111,7 +109,7 @@ pub enum SendContinuation {
pub struct ReceivedMessage {
/// The time at which the message was received
pub timestamp: Instant,
pub data: ArrayVec<u8, 127>,
pub data: Vec<u8, 127>,
/// The LQI at which the network beacon was received. Lower values represent lower LQI, as defined in 8.2.6.
pub lqi: u8,
/// The channel on which the message was received
Expand Down
4 changes: 2 additions & 2 deletions lr-wpan-rs/src/sap/beacon_notify.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use arrayvec::ArrayVec;
use heapless::Vec;

use super::{Indication, IndicationValue, PanDescriptor};
use crate::{consts::MAX_BEACON_PAYLOAD_LENGTH, wire::beacon::PendingAddress};
Expand All @@ -16,7 +16,7 @@ pub struct BeaconNotifyIndication {
/// The set of octets comprising the beacon
/// payload to be transferred from the MAC
/// sublayer entity to the next higher layer.
pub sdu: ArrayVec<u8, MAX_BEACON_PAYLOAD_LENGTH>,
pub sdu: Vec<u8, MAX_BEACON_PAYLOAD_LENGTH>,
}

impl From<IndicationValue> for BeaconNotifyIndication {
Expand Down
6 changes: 3 additions & 3 deletions lr-wpan-rs/src/sap/data.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use arrayvec::ArrayVec;
use heapless::Vec;

use super::{
ConfirmValue, Indication, IndicationValue, Request, RequestValue, SecurityInfo, Status,
Expand Down Expand Up @@ -49,7 +49,7 @@ pub struct DataRequest {
/// The individual device address of the entity to which the MSDU is being transferred.
pub dst_addr: Option<DeviceAddress>,
/// The set of octets forming the MSDU to be transmitted by the MAC sublayer entity.
pub msdu: ArrayVec<u8, { crate::consts::MAX_MAC_PAYLOAD_SIZE }>,
pub msdu: Vec<u8, { crate::consts::MAX_MAC_PAYLOAD_SIZE }>,
/// The handle associated with the MSDU to be transmitted by the MAC sublayer entity.
pub msdu_handle: u8,
/// TRUE if acknowledged transmission is used, FALSE otherwise.
Expand Down Expand Up @@ -206,7 +206,7 @@ pub struct DataIndication {
/// The individual device address of the entity to which the MSDU is being transferred.
pub dst_addr: Option<DeviceAddress>,
/// The set of octets forming the MSDU being indicated by the MAC sublayer entity.
pub msdu: ArrayVec<u8, { crate::consts::MAX_MAC_PAYLOAD_SIZE }>,
pub msdu: Vec<u8, { crate::consts::MAX_MAC_PAYLOAD_SIZE }>,
/// LQI value measured during reception of the MPDU.
/// Lower values represent lower LQI, as described in 8.2.6.
pub mpdu_link_quality: u8,
Expand Down
12 changes: 6 additions & 6 deletions lr-wpan-rs/src/sap/scan.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use arrayvec::ArrayVec;
use heapless::Vec;

use super::{ConfirmValue, PanDescriptor, Request, RequestValue, SecurityInfo, Status};
use crate::ChannelPage;
Expand All @@ -11,7 +11,7 @@ use crate::ChannelPage;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ScanRequest {
pub scan_type: ScanType,
pub scan_channels: ArrayVec<u8, 16>,
pub scan_channels: Vec<u8, 16>,
/// A value used to calculate the length of time to
/// spend scanning each channel for ED, active,
/// and passive scans. This parameter is ignored for
Expand Down Expand Up @@ -83,7 +83,7 @@ pub struct ScanConfirm {
/// A list of the channels given in the
/// request which were not scanned. This
/// parameter is not valid for ED scans.
pub unscanned_channels: ArrayVec<u8, 16>,
pub unscanned_channels: Vec<u8, 16>,
/// The number of elements returned in
/// the appropriate result lists. This value
/// is zero for the result of an orphan scan.
Expand All @@ -92,14 +92,14 @@ pub struct ScanConfirm {
/// for each channel searched during an
/// ED scan. This parameter is null for
/// active, passive, and orphan scans.
pub energy_detect_list: ArrayVec<u8, 16>,
pub energy_detect_list: Vec<u8, 16>,
/// The list of PAN descriptors, one for
/// each beacon found during an active or
/// passive scan if macAutoRequest is set
/// to TRUE. This parameter is null for
/// ED and orphan scans or when macAutoRequest is set to FALSE during an
/// active or passive scan.
pub pan_descriptor_list: alloc::boxed::Box<ArrayVec<PanDescriptor, 16>>,
pub pan_descriptor_list: alloc::boxed::Box<Vec<PanDescriptor, 16>>,
/// Categorization of energy detected in
/// channel with the following values:
/// - 0: Category detection is not supported
Expand All @@ -113,7 +113,7 @@ pub struct ScanConfirm {
/// ResultListSize. This parameter is null
/// for active, passive, and orphan scans. It
/// is also null for non-UWB PHYs.
pub uwb_energy_detect_list: ArrayVec<u8, 16>,
pub uwb_energy_detect_list: Vec<u8, 16>,
}

impl From<ConfirmValue> for ScanConfirm {
Expand Down
Loading

0 comments on commit 78123e7

Please sign in to comment.