diff options
Diffstat (limited to 'embassy-stm32-wpan/src/mac/runner.rs')
| -rw-r--r-- | embassy-stm32-wpan/src/mac/runner.rs | 73 |
1 files changed, 44 insertions, 29 deletions
diff --git a/embassy-stm32-wpan/src/mac/runner.rs b/embassy-stm32-wpan/src/mac/runner.rs index 2409f994d..26fdf23e0 100644 --- a/embassy-stm32-wpan/src/mac/runner.rs +++ b/embassy-stm32-wpan/src/mac/runner.rs | |||
| @@ -8,52 +8,65 @@ use embassy_sync::mutex::Mutex; | |||
| 8 | use embassy_sync::signal::Signal; | 8 | use embassy_sync::signal::Signal; |
| 9 | 9 | ||
| 10 | use crate::mac::MTU; | 10 | use crate::mac::MTU; |
| 11 | use crate::mac::commands::DataRequest; | 11 | use crate::mac::commands::*; |
| 12 | use crate::mac::driver::NetworkState; | ||
| 12 | use crate::mac::event::MacEvent; | 13 | use crate::mac::event::MacEvent; |
| 13 | use crate::mac::typedefs::{AddressMode, MacAddress, PanId, SecurityLevel}; | 14 | use crate::mac::typedefs::*; |
| 14 | use crate::sub::mac::Mac; | 15 | use crate::sub::mac::{MacRx, MacTx}; |
| 15 | 16 | ||
| 16 | type ZeroCopyPubSub<M, T> = blocking_mutex::Mutex<M, RefCell<Option<Signal<NoopRawMutex, T>>>>; | 17 | pub type ZeroCopyPubSub<M, T> = blocking_mutex::Mutex<M, RefCell<Option<Signal<NoopRawMutex, T>>>>; |
| 18 | |||
| 19 | pub const BUF_SIZE: usize = 3; | ||
| 17 | 20 | ||
| 18 | pub struct Runner<'a> { | 21 | pub struct Runner<'a> { |
| 19 | pub(crate) mac_subsystem: Mac, | ||
| 20 | // rx event backpressure is already provided through the MacEvent drop mechanism | 22 | // rx event backpressure is already provided through the MacEvent drop mechanism |
| 21 | // therefore, we don't need to worry about overwriting events | 23 | // therefore, we don't need to worry about overwriting events |
| 22 | pub(crate) rx_event_channel: ZeroCopyPubSub<CriticalSectionRawMutex, MacEvent<'a>>, | 24 | rx_event_channel: &'a ZeroCopyPubSub<CriticalSectionRawMutex, MacEvent<'a>>, |
| 23 | pub(crate) read_mutex: Mutex<CriticalSectionRawMutex, ()>, | 25 | rx_data_channel: &'a Channel<CriticalSectionRawMutex, MacEvent<'a>, 1>, |
| 24 | pub(crate) write_mutex: Mutex<CriticalSectionRawMutex, ()>, | 26 | mac_rx: &'a mut MacRx, |
| 25 | pub(crate) rx_channel: Channel<CriticalSectionRawMutex, MacEvent<'a>, 1>, | 27 | |
| 26 | pub(crate) tx_channel: Channel<CriticalSectionRawMutex, (&'a mut [u8; MTU], usize), 5>, | 28 | tx_data_channel: &'a Channel<CriticalSectionRawMutex, (&'a mut [u8; MTU], usize), BUF_SIZE>, |
| 27 | pub(crate) tx_buf_channel: Channel<CriticalSectionRawMutex, &'a mut [u8; MTU], 5>, | 29 | tx_buf_channel: &'a Channel<CriticalSectionRawMutex, &'a mut [u8; MTU], BUF_SIZE>, |
| 30 | mac_tx: &'a Mutex<CriticalSectionRawMutex, MacTx>, | ||
| 31 | |||
| 32 | #[allow(unused)] | ||
| 33 | network_state: &'a blocking_mutex::Mutex<CriticalSectionRawMutex, RefCell<NetworkState>>, | ||
| 28 | } | 34 | } |
| 29 | 35 | ||
| 30 | impl<'a> Runner<'a> { | 36 | impl<'a> Runner<'a> { |
| 31 | pub fn new(mac: Mac, tx_buf_queue: [&'a mut [u8; MTU]; 5]) -> Self { | 37 | pub fn new( |
| 32 | let this = Self { | 38 | rx_event_channel: &'a ZeroCopyPubSub<CriticalSectionRawMutex, MacEvent<'a>>, |
| 33 | mac_subsystem: mac, | 39 | rx_data_channel: &'a Channel<CriticalSectionRawMutex, MacEvent<'a>, 1>, |
| 34 | rx_event_channel: blocking_mutex::Mutex::new(RefCell::new(None)), | 40 | mac_rx: &'a mut MacRx, |
| 35 | read_mutex: Mutex::new(()), | 41 | tx_data_channel: &'a Channel<CriticalSectionRawMutex, (&'a mut [u8; MTU], usize), BUF_SIZE>, |
| 36 | write_mutex: Mutex::new(()), | 42 | tx_buf_channel: &'a Channel<CriticalSectionRawMutex, &'a mut [u8; MTU], BUF_SIZE>, |
| 37 | rx_channel: Channel::new(), | 43 | mac_tx: &'a Mutex<CriticalSectionRawMutex, MacTx>, |
| 38 | tx_channel: Channel::new(), | 44 | tx_buf_queue: &'a mut [[u8; MTU]; BUF_SIZE], |
| 39 | tx_buf_channel: Channel::new(), | 45 | network_state: &'a blocking_mutex::Mutex<CriticalSectionRawMutex, RefCell<NetworkState>>, |
| 40 | }; | 46 | ) -> Self { |
| 41 | |||
| 42 | for buf in tx_buf_queue { | 47 | for buf in tx_buf_queue { |
| 43 | this.tx_buf_channel.try_send(buf).unwrap(); | 48 | tx_buf_channel.try_send(buf).unwrap(); |
| 44 | } | 49 | } |
| 45 | 50 | ||
| 46 | this | 51 | Self { |
| 52 | rx_event_channel, | ||
| 53 | rx_data_channel, | ||
| 54 | mac_rx, | ||
| 55 | tx_data_channel, | ||
| 56 | tx_buf_channel, | ||
| 57 | mac_tx, | ||
| 58 | network_state, | ||
| 59 | } | ||
| 47 | } | 60 | } |
| 48 | 61 | ||
| 49 | pub async fn run(&'a self) -> ! { | 62 | pub async fn run(&'a self) -> ! { |
| 50 | join::join( | 63 | join::join( |
| 51 | async { | 64 | async { |
| 52 | loop { | 65 | loop { |
| 53 | if let Ok(mac_event) = self.mac_subsystem.read().await { | 66 | if let Ok(mac_event) = self.mac_rx.read().await { |
| 54 | match mac_event { | 67 | match mac_event { |
| 55 | MacEvent::McpsDataInd(_) => { | 68 | MacEvent::McpsDataInd(_) => { |
| 56 | self.rx_channel.send(mac_event).await; | 69 | self.rx_data_channel.send(mac_event).await; |
| 57 | } | 70 | } |
| 58 | _ => { | 71 | _ => { |
| 59 | self.rx_event_channel.lock(|s| { | 72 | self.rx_event_channel.lock(|s| { |
| @@ -73,11 +86,13 @@ impl<'a> Runner<'a> { | |||
| 73 | let mut msdu_handle = 0x02; | 86 | let mut msdu_handle = 0x02; |
| 74 | 87 | ||
| 75 | loop { | 88 | loop { |
| 76 | let (buf, len) = self.tx_channel.receive().await; | 89 | let (buf, len) = self.tx_data_channel.receive().await; |
| 77 | let _wm = self.write_mutex.lock().await; | 90 | let mac_tx = self.mac_tx.lock().await; |
| 91 | |||
| 92 | // TODO: skip this if the link state is down | ||
| 78 | 93 | ||
| 79 | // The mutex should be dropped on the next loop iteration | 94 | // The mutex should be dropped on the next loop iteration |
| 80 | self.mac_subsystem | 95 | mac_tx |
| 81 | .send_command( | 96 | .send_command( |
| 82 | DataRequest { | 97 | DataRequest { |
| 83 | src_addr_mode: AddressMode::Short, | 98 | src_addr_mode: AddressMode::Short, |
