wpan: implement initial event loop

This commit is contained in:
xoviat 2023-07-18 20:52:03 -05:00
parent 890d113b85
commit ca1d4179a7
5 changed files with 85 additions and 70 deletions

View File

@ -18,6 +18,7 @@ embassy-futures = { version = "0.1.0", path = "../embassy-futures" }
embassy-hal-common = { version = "0.1.0", path = "../embassy-hal-common" } embassy-hal-common = { version = "0.1.0", path = "../embassy-hal-common" }
embassy-embedded-hal = { version = "0.1.0", path = "../embassy-embedded-hal" } embassy-embedded-hal = { version = "0.1.0", path = "../embassy-embedded-hal" }
embassy-net-driver-channel = { version = "0.1.0", path = "../embassy-net-driver-channel", optional=true } embassy-net-driver-channel = { version = "0.1.0", path = "../embassy-net-driver-channel", optional=true }
embassy-net-driver = { version = "0.1.0", path = "../embassy-net-driver", optional=true }
defmt = { version = "0.3", optional = true } defmt = { version = "0.3", optional = true }
cortex-m = "0.7.6" cortex-m = "0.7.6"
@ -27,13 +28,14 @@ aligned = "0.4.1"
bit_field = "0.10.2" bit_field = "0.10.2"
stm32-device-signature = { version = "0.3.3", features = ["stm32wb5x"] } stm32-device-signature = { version = "0.3.3", features = ["stm32wb5x"] }
stm32wb-hci = { version = "0.1.3", optional = true } stm32wb-hci = { version = "0.1.3", optional = true }
futures = { version = "0.3.17", default-features = false, features = ["async-await"] }
bitflags = { version = "2.3.3", optional = true } bitflags = { version = "2.3.3", optional = true }
[features] [features]
defmt = ["dep:defmt", "embassy-sync/defmt", "embassy-embedded-hal/defmt", "embassy-hal-common/defmt", "stm32wb-hci?/defmt"] defmt = ["dep:defmt", "embassy-sync/defmt", "embassy-embedded-hal/defmt", "embassy-hal-common/defmt", "stm32wb-hci?/defmt"]
ble = ["dep:stm32wb-hci"] ble = ["dep:stm32wb-hci"]
mac = ["dep:bitflags", "dep:embassy-net-driver-channel"] mac = ["dep:bitflags", "dep:embassy-net-driver-channel", "dep:embassy-net-driver"]
stm32wb10cc = [ "embassy-stm32/stm32wb10cc" ] stm32wb10cc = [ "embassy-stm32/stm32wb10cc" ]
stm32wb15cc = [ "embassy-stm32/stm32wb15cc" ] stm32wb15cc = [ "embassy-stm32/stm32wb15cc" ]

View File

@ -29,13 +29,14 @@ impl<'d> embassy_net_driver::Driver for Driver<'d> {
type TxToken<'a> = TxToken<'d> where Self: 'a; type TxToken<'a> = TxToken<'d> where Self: 'a;
fn receive(&mut self, cx: &mut Context) -> Option<(Self::RxToken<'_>, Self::TxToken<'_>)> { fn receive(&mut self, cx: &mut Context) -> Option<(Self::RxToken<'_>, Self::TxToken<'_>)> {
if self.runner.rx_channel.poll_ready_to_receive(cx) && self.runner.tx_channel.poll_ready_to_receive(cx) { if self.runner.rx_channel.poll_ready_to_receive(cx) && self.runner.tx_buf_channel.poll_ready_to_receive(cx) {
Some(( Some((
RxToken { RxToken {
rx: &self.runner.rx_channel, rx: &self.runner.rx_channel,
}, },
TxToken { TxToken {
tx: &self.runner.tx_channel, tx: &self.runner.tx_channel,
tx_buf: &self.runner.tx_buf_channel,
}, },
)) ))
} else { } else {
@ -44,9 +45,10 @@ impl<'d> embassy_net_driver::Driver for Driver<'d> {
} }
fn transmit(&mut self, cx: &mut Context) -> Option<Self::TxToken<'_>> { fn transmit(&mut self, cx: &mut Context) -> Option<Self::TxToken<'_>> {
if self.runner.tx_channel.poll_ready_to_receive(cx) { if self.runner.tx_buf_channel.poll_ready_to_receive(cx) {
Some(TxToken { Some(TxToken {
tx: &self.runner.tx_channel, tx: &self.runner.tx_channel,
tx_buf: &self.runner.tx_buf_channel,
}) })
} else { } else {
None None
@ -106,8 +108,8 @@ impl<'d> embassy_net_driver::RxToken for RxToken<'d> {
} }
pub struct TxToken<'d> { pub struct TxToken<'d> {
tx: &'d Channel<CriticalSectionRawMutex, &'d [u8], 1>, tx: &'d Channel<CriticalSectionRawMutex, &'d mut [u8], 5>,
// tx: &'a mut TDesRing<'d>, tx_buf: &'d Channel<CriticalSectionRawMutex, &'d mut [u8], 5>,
} }
impl<'d> embassy_net_driver::TxToken for TxToken<'d> { impl<'d> embassy_net_driver::TxToken for TxToken<'d> {
@ -115,11 +117,13 @@ impl<'d> embassy_net_driver::TxToken for TxToken<'d> {
where where
F: FnOnce(&mut [u8]) -> R, F: FnOnce(&mut [u8]) -> R,
{ {
// NOTE(unwrap): we checked the queue wasn't full when creating the token. // Only valid tx buffers should be put into the queue
// let pkt = unwrap!(self.tx.available()); let buf = self.tx_buf.try_recv().unwrap();
let pkt = &mut []; let r = f(&mut buf[..len]);
let r = f(&mut pkt[..len]);
// self.tx.transmit(len); // The tx channel should always be of equal capacity to the tx_buf channel
self.tx.try_send(buf).unwrap();
r r
} }
} }

View File

@ -1,83 +1,80 @@
use embassy_futures::select::{select3, Either3}; use embassy_futures::join;
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex; use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
use embassy_sync::channel::Channel; use embassy_sync::channel::Channel;
use embassy_sync::waitqueue::AtomicWaker;
use crate::mac::commands::DataRequest;
use crate::mac::event::{Event, MacEvent}; use crate::mac::event::{Event, MacEvent};
use crate::mac::typedefs::{AddressMode, MacAddress, PanId, SecurityLevel};
use crate::mac::MTU; use crate::mac::MTU;
use crate::sub::mac::Mac; use crate::sub::mac::Mac;
pub(crate) struct TxRing {
// stores n packets of up to mtu size
ring: [[u8; MTU]; 5],
pending: bool,
// start: u8,
// end: u8,
}
impl TxRing {
pub(crate) fn new() -> Self {
Self {
ring: [[0; MTU]; 5],
pending: false,
}
}
// wait for a free packet to become available
pub fn is_packet_free(&self) -> bool {
!self.pending
}
// get the next available free packet
pub fn get_free_packet<'a>(&'a mut self) -> &'a mut [u8] {
self.pending = true;
&mut self.ring[0]
}
pub fn get_packet_to_transmit<'a>(&'a mut self) -> Option<&'a [u8]> {
if self.pending {
self.pending = false;
Some(&self.ring[0])
} else {
None
}
}
}
pub struct Runner<'a> { pub struct Runner<'a> {
mac_subsystem: Mac, mac_subsystem: Mac,
pub(crate) rx_channel: Channel<CriticalSectionRawMutex, Event, 1>, pub(crate) rx_channel: Channel<CriticalSectionRawMutex, Event, 1>,
pub(crate) tx_channel: Channel<CriticalSectionRawMutex, &'a [u8], 1>, pub(crate) tx_channel: Channel<CriticalSectionRawMutex, &'a mut [u8], 5>,
pub(crate) tx_buf_channel: Channel<CriticalSectionRawMutex, &'a mut [u8], 5>,
} }
impl<'a> Runner<'a> { impl<'a> Runner<'a> {
pub fn new(mac: Mac) -> Self { pub fn new(mac: Mac, tx_buf_queue: [&'a mut [u8; MTU]; 5]) -> Self {
Self { let this = Self {
mac_subsystem: mac, mac_subsystem: mac,
rx_channel: Channel::new(), rx_channel: Channel::new(),
tx_channel: Channel::new(), tx_channel: Channel::new(),
} tx_buf_channel: Channel::new(),
} };
pub(crate) async fn init(&mut self, firmware: &[u8]) { for buf in tx_buf_queue {
debug!("wifi init done"); this.tx_buf_channel.try_send(buf).unwrap();
}
this
} }
pub async fn run(&self) -> ! { pub async fn run(&self) -> ! {
loop { join::join(
let event = self.mac_subsystem.read().await; async {
if let Ok(evt) = event.mac_event() { loop {
match evt { let event = self.mac_subsystem.read().await;
MacEvent::McpsDataInd(data_ind) => { if let Ok(evt) = event.mac_event() {
self.rx_channel.try_send(event); match evt {
MacEvent::McpsDataInd(_) => {
self.rx_channel.send(event).await;
}
_ => {}
}
} }
_ => {}
} }
} },
async {
loop {
let buf = self.tx_channel.recv().await;
// TODO: select tx event self.mac_subsystem
} .send_command(
DataRequest {
src_addr_mode: AddressMode::Short,
dst_addr_mode: AddressMode::Short,
dst_pan_id: PanId([0x1A, 0xAA]),
dst_address: MacAddress::BROADCAST,
msdu_handle: 0x02,
ack_tx: 0x00,
gts_tx: false,
security_level: SecurityLevel::Unsecure,
..Default::default()
}
.set_buffer(&buf),
)
.await
.unwrap();
// The tx channel should always be of equal capacity to the tx_buf channel
self.tx_buf_channel.try_send(buf).unwrap();
}
},
)
.await;
loop {}
} }
} }

View File

@ -21,7 +21,7 @@ embedded-hal = "0.2.6"
panic-probe = { version = "0.3", features = ["print-defmt"] } panic-probe = { version = "0.3", features = ["print-defmt"] }
futures = { version = "0.3.17", default-features = false, features = ["async-await"] } futures = { version = "0.3.17", default-features = false, features = ["async-await"] }
heapless = { version = "0.7.5", default-features = false } heapless = { version = "0.7.5", default-features = false }
static_cell = { version = "1.1", features = ["nightly"]}
[features] [features]
default = ["ble", "mac"] default = ["ble", "mac"]
@ -40,6 +40,10 @@ required-features = ["mac"]
name = "mac_ffd" name = "mac_ffd"
required-features = ["mac"] required-features = ["mac"]
[[bin]]
name = "mac_ffd_net"
required-features = ["mac"]
[[bin]] [[bin]]
name = "eddystone_beacon" name = "eddystone_beacon"
required-features = ["ble"] required-features = ["ble"]

View File

@ -172,7 +172,15 @@ async fn main(spawner: Spawner) {
defmt::info!("{:#x}", evt.mac_event()); defmt::info!("{:#x}", evt.mac_event());
} }
let runner = make_static!(Runner::new(mbox.mac_subsystem)); let tx_queue = [
make_static!([0u8; 127]),
make_static!([0u8; 127]),
make_static!([0u8; 127]),
make_static!([0u8; 127]),
make_static!([0u8; 127]),
];
let runner = make_static!(Runner::new(mbox.mac_subsystem, tx_queue));
spawner.spawn(run_mac(runner)).unwrap(); spawner.spawn(run_mac(runner)).unwrap();