aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorxoviat <[email protected]>2023-06-25 16:44:52 +0000
committerGitHub <[email protected]>2023-06-25 16:44:52 +0000
commit03e0116a56d7bc4b8eb639fd590eaa186d039b2b (patch)
tree7fe23e3a556a06202bf926d6176d39949f4c1ab6
parent4dd48099bee305fc31e47a586f34d09c3ec02673 (diff)
parent018622f607a17037903ef7c5592dda762002f89b (diff)
Merge pull request #1580 from xoviat/wpan
stm32/wpan: cleanup
-rw-r--r--embassy-stm32-wpan/Cargo.toml1
-rw-r--r--embassy-stm32-wpan/build.rs13
-rw-r--r--embassy-stm32-wpan/src/consts.rs4
-rw-r--r--embassy-stm32-wpan/src/evt.rs33
-rw-r--r--embassy-stm32-wpan/src/lhci.rs9
-rw-r--r--embassy-stm32-wpan/src/lib.rs26
-rw-r--r--embassy-stm32-wpan/src/sub/ble.rs (renamed from embassy-stm32-wpan/src/ble.rs)25
-rw-r--r--embassy-stm32-wpan/src/sub/mac.rs (renamed from embassy-stm32-wpan/src/mac.rs)48
-rw-r--r--embassy-stm32-wpan/src/sub/mm.rs (renamed from embassy-stm32-wpan/src/mm.rs)23
-rw-r--r--embassy-stm32-wpan/src/sub/mod.rs6
-rw-r--r--embassy-stm32-wpan/src/sub/sys.rs (renamed from embassy-stm32-wpan/src/sys.rs)3
-rw-r--r--embassy-stm32-wpan/src/tables.rs34
-rw-r--r--embassy-stm32-wpan/tl_mbox.x.in (renamed from embassy-stm32/tl_mbox.x.in)0
-rw-r--r--embassy-stm32/build.rs10
-rw-r--r--examples/stm32wb/src/bin/eddystone_beacon.rs14
-rw-r--r--examples/stm32wb/src/bin/tl_mbox_mac.rs4
-rw-r--r--tests/stm32/src/bin/tl_mbox.rs21
17 files changed, 146 insertions, 128 deletions
diff --git a/embassy-stm32-wpan/Cargo.toml b/embassy-stm32-wpan/Cargo.toml
index 6d78ca577..4b830cab3 100644
--- a/embassy-stm32-wpan/Cargo.toml
+++ b/embassy-stm32-wpan/Cargo.toml
@@ -21,6 +21,7 @@ embassy-embedded-hal = { version = "0.1.0", path = "../embassy-embedded-hal" }
21defmt = { version = "0.3", optional = true } 21defmt = { version = "0.3", optional = true }
22cortex-m = "0.7.6" 22cortex-m = "0.7.6"
23heapless = "0.7.16" 23heapless = "0.7.16"
24aligned = "0.4.1"
24 25
25bit_field = "0.10.2" 26bit_field = "0.10.2"
26stm32-device-signature = { version = "0.3.3", features = ["stm32wb5x"] } 27stm32-device-signature = { version = "0.3.3", features = ["stm32wb5x"] }
diff --git a/embassy-stm32-wpan/build.rs b/embassy-stm32-wpan/build.rs
index 4edf73d59..94aac070d 100644
--- a/embassy-stm32-wpan/build.rs
+++ b/embassy-stm32-wpan/build.rs
@@ -1,4 +1,5 @@
1use std::env; 1use std::path::PathBuf;
2use std::{env, fs};
2 3
3fn main() { 4fn main() {
4 match env::vars() 5 match env::vars()
@@ -10,6 +11,16 @@ fn main() {
10 Err(GetOneError::None) => panic!("No stm32xx Cargo feature enabled"), 11 Err(GetOneError::None) => panic!("No stm32xx Cargo feature enabled"),
11 Err(GetOneError::Multiple) => panic!("Multiple stm32xx Cargo features enabled"), 12 Err(GetOneError::Multiple) => panic!("Multiple stm32xx Cargo features enabled"),
12 } 13 }
14
15 let out_dir = &PathBuf::from(env::var_os("OUT_DIR").unwrap());
16
17 // ========
18 // stm32wb tl_mbox link sections
19
20 let out_file = out_dir.join("tl_mbox.x").to_string_lossy().to_string();
21 fs::write(out_file, fs::read_to_string("tl_mbox.x.in").unwrap()).unwrap();
22 println!("cargo:rustc-link-search={}", out_dir.display());
23 println!("cargo:rerun-if-changed=tl_mbox.x.in");
13} 24}
14 25
15enum GetOneError { 26enum GetOneError {
diff --git a/embassy-stm32-wpan/src/consts.rs b/embassy-stm32-wpan/src/consts.rs
index 9a107306c..f234151d7 100644
--- a/embassy-stm32-wpan/src/consts.rs
+++ b/embassy-stm32-wpan/src/consts.rs
@@ -87,3 +87,7 @@ pub const fn divc(x: usize, y: usize) -> usize {
87pub const TL_BLE_EVT_CS_PACKET_SIZE: usize = TL_EVT_HEADER_SIZE + TL_CS_EVT_SIZE; 87pub const TL_BLE_EVT_CS_PACKET_SIZE: usize = TL_EVT_HEADER_SIZE + TL_CS_EVT_SIZE;
88#[allow(dead_code)] 88#[allow(dead_code)]
89pub const TL_BLE_EVT_CS_BUFFER_SIZE: usize = TL_PACKET_HEADER_SIZE + TL_BLE_EVT_CS_PACKET_SIZE; 89pub const TL_BLE_EVT_CS_BUFFER_SIZE: usize = TL_PACKET_HEADER_SIZE + TL_BLE_EVT_CS_PACKET_SIZE;
90
91pub const TL_BLEEVT_CC_OPCODE: u8 = 0x0E;
92pub const TL_BLEEVT_CS_OPCODE: u8 = 0x0F;
93pub const TL_BLEEVT_VS_OPCODE: u8 = 0xFF;
diff --git a/embassy-stm32-wpan/src/evt.rs b/embassy-stm32-wpan/src/evt.rs
index 25249a13a..c6528413d 100644
--- a/embassy-stm32-wpan/src/evt.rs
+++ b/embassy-stm32-wpan/src/evt.rs
@@ -1,3 +1,4 @@
1use core::marker::PhantomData;
1use core::{ptr, slice}; 2use core::{ptr, slice};
2 3
3use super::PacketHeader; 4use super::PacketHeader;
@@ -93,17 +94,22 @@ impl EvtPacket {
93 } 94 }
94} 95}
95 96
97pub trait MemoryManager {
98 unsafe fn drop_event_packet(evt: *mut EvtPacket);
99}
100
96/// smart pointer to the [`EvtPacket`] that will dispose of [`EvtPacket`] buffer automatically 101/// smart pointer to the [`EvtPacket`] that will dispose of [`EvtPacket`] buffer automatically
97/// on [`Drop`] 102/// on [`Drop`]
98#[derive(Debug)] 103#[derive(Debug)]
99pub struct EvtBox { 104pub struct EvtBox<T: MemoryManager> {
100 ptr: *mut EvtPacket, 105 ptr: *mut EvtPacket,
106 mm: PhantomData<T>,
101} 107}
102 108
103unsafe impl Send for EvtBox {} 109unsafe impl<T: MemoryManager> Send for EvtBox<T> {}
104impl EvtBox { 110impl<T: MemoryManager> EvtBox<T> {
105 pub(super) fn new(ptr: *mut EvtPacket) -> Self { 111 pub(super) fn new(ptr: *mut EvtPacket) -> Self {
106 Self { ptr } 112 Self { ptr, mm: PhantomData }
107 } 113 }
108 114
109 /// Returns information about the event 115 /// Returns information about the event
@@ -126,9 +132,6 @@ impl EvtBox {
126 } 132 }
127 } 133 }
128 134
129 /// writes an underlying [`EvtPacket`] into the provided buffer.
130 /// Returns the number of bytes that were written.
131 /// Returns an error if event kind is unknown or if provided buffer size is not enough.
132 pub fn serial<'a>(&'a self) -> &'a [u8] { 135 pub fn serial<'a>(&'a self) -> &'a [u8] {
133 unsafe { 136 unsafe {
134 let evt_serial: *const EvtSerial = &(*self.ptr).evt_serial; 137 let evt_serial: *const EvtSerial = &(*self.ptr).evt_serial;
@@ -141,20 +144,8 @@ impl EvtBox {
141 } 144 }
142} 145}
143 146
144impl Drop for EvtBox { 147impl<T: MemoryManager> Drop for EvtBox<T> {
145 fn drop(&mut self) { 148 fn drop(&mut self) {
146 #[cfg(feature = "ble")] 149 unsafe { T::drop_event_packet(self.ptr) };
147 unsafe {
148 use crate::mm;
149
150 mm::MemoryManager::drop_event_packet(self.ptr)
151 };
152
153 #[cfg(feature = "mac")]
154 unsafe {
155 use crate::mac;
156
157 mac::Mac::drop_event_packet(self.ptr)
158 }
159 } 150 }
160} 151}
diff --git a/embassy-stm32-wpan/src/lhci.rs b/embassy-stm32-wpan/src/lhci.rs
index 62116a695..89f204f99 100644
--- a/embassy-stm32-wpan/src/lhci.rs
+++ b/embassy-stm32-wpan/src/lhci.rs
@@ -1,8 +1,9 @@
1use core::ptr;
2
1use crate::cmd::CmdPacket; 3use crate::cmd::CmdPacket;
2use crate::consts::{TlPacketType, TL_EVT_HEADER_SIZE}; 4use crate::consts::{TlPacketType, TL_EVT_HEADER_SIZE};
3use crate::evt::{CcEvt, EvtPacket, EvtSerial}; 5use crate::evt::{CcEvt, EvtPacket, EvtSerial};
4use crate::tables::{DeviceInfoTable, RssInfoTable, SafeBootInfoTable, WirelessFwInfoTable}; 6use crate::tables::{DeviceInfoTable, RssInfoTable, SafeBootInfoTable, WirelessFwInfoTable, TL_DEVICE_INFO_TABLE};
5use crate::TL_REF_TABLE;
6 7
7const TL_BLEEVT_CC_OPCODE: u8 = 0x0e; 8const TL_BLEEVT_CC_OPCODE: u8 = 0x0e;
8const LHCI_OPCODE_C1_DEVICE_INF: u16 = 0xfd62; 9const LHCI_OPCODE_C1_DEVICE_INF: u16 = 0xfd62;
@@ -38,7 +39,7 @@ impl Default for LhciC1DeviceInformationCcrp {
38 safe_boot_info_table, 39 safe_boot_info_table,
39 rss_info_table, 40 rss_info_table,
40 wireless_fw_info_table, 41 wireless_fw_info_table,
41 } = unsafe { &*(*TL_REF_TABLE.as_ptr()).device_info_table }.clone(); 42 } = unsafe { ptr::read_volatile(TL_DEVICE_INFO_TABLE.as_ptr()) };
42 43
43 let device_id = stm32_device_signature::device_id(); 44 let device_id = stm32_device_signature::device_id();
44 let uid96_0 = (device_id[3] as u32) << 24 45 let uid96_0 = (device_id[3] as u32) << 24
@@ -105,7 +106,7 @@ impl LhciC1DeviceInformationCcrp {
105 let self_ptr: *const LhciC1DeviceInformationCcrp = self; 106 let self_ptr: *const LhciC1DeviceInformationCcrp = self;
106 let self_buf = self_ptr.cast(); 107 let self_buf = self_ptr.cast();
107 108
108 core::ptr::copy(self_buf, evt_cc_payload_buf, self_size); 109 ptr::copy(self_buf, evt_cc_payload_buf, self_size);
109 } 110 }
110 } 111 }
111} 112}
diff --git a/embassy-stm32-wpan/src/lib.rs b/embassy-stm32-wpan/src/lib.rs
index bf0f0466e..99c610583 100644
--- a/embassy-stm32-wpan/src/lib.rs
+++ b/embassy-stm32-wpan/src/lib.rs
@@ -11,26 +11,24 @@ use embassy_hal_common::{into_ref, Peripheral, PeripheralRef};
11use embassy_stm32::interrupt; 11use embassy_stm32::interrupt;
12use embassy_stm32::ipcc::{Config, Ipcc, ReceiveInterruptHandler, TransmitInterruptHandler}; 12use embassy_stm32::ipcc::{Config, Ipcc, ReceiveInterruptHandler, TransmitInterruptHandler};
13use embassy_stm32::peripherals::IPCC; 13use embassy_stm32::peripherals::IPCC;
14use mm::MemoryManager; 14use sub::mm::MemoryManager;
15use sys::Sys; 15use sub::sys::Sys;
16use tables::*; 16use tables::*;
17use unsafe_linked_list::LinkedListNode; 17use unsafe_linked_list::LinkedListNode;
18 18
19#[cfg(feature = "ble")]
20pub mod ble;
21pub mod channels; 19pub mod channels;
22pub mod cmd; 20pub mod cmd;
23pub mod consts; 21pub mod consts;
24pub mod evt; 22pub mod evt;
25pub mod lhci; 23pub mod lhci;
26#[cfg(feature = "mac")]
27pub mod mac;
28pub mod mm;
29pub mod shci; 24pub mod shci;
30pub mod sys; 25pub mod sub;
31pub mod tables; 26pub mod tables;
32pub mod unsafe_linked_list; 27pub mod unsafe_linked_list;
33 28
29#[cfg(feature = "ble")]
30pub use crate::sub::ble::hci;
31
34type PacketHeader = LinkedListNode; 32type PacketHeader = LinkedListNode;
35 33
36pub struct TlMbox<'d> { 34pub struct TlMbox<'d> {
@@ -39,9 +37,9 @@ pub struct TlMbox<'d> {
39 pub sys_subsystem: Sys, 37 pub sys_subsystem: Sys,
40 pub mm_subsystem: MemoryManager, 38 pub mm_subsystem: MemoryManager,
41 #[cfg(feature = "ble")] 39 #[cfg(feature = "ble")]
42 pub ble_subsystem: ble::Ble, 40 pub ble_subsystem: sub::ble::Ble,
43 #[cfg(feature = "mac")] 41 #[cfg(feature = "mac")]
44 pub mac_subsystem: mac::Mac, 42 pub mac_subsystem: sub::mac::Mac,
45} 43}
46 44
47impl<'d> TlMbox<'d> { 45impl<'d> TlMbox<'d> {
@@ -128,12 +126,12 @@ impl<'d> TlMbox<'d> {
128 126
129 Self { 127 Self {
130 _ipcc: ipcc, 128 _ipcc: ipcc,
131 sys_subsystem: sys::Sys::new(), 129 sys_subsystem: sub::sys::Sys::new(),
132 #[cfg(feature = "ble")] 130 #[cfg(feature = "ble")]
133 ble_subsystem: ble::Ble::new(), 131 ble_subsystem: sub::ble::Ble::new(),
134 #[cfg(feature = "mac")] 132 #[cfg(feature = "mac")]
135 mac_subsystem: mac::Mac::new(), 133 mac_subsystem: sub::mac::Mac::new(),
136 mm_subsystem: mm::MemoryManager::new(), 134 mm_subsystem: sub::mm::MemoryManager::new(),
137 } 135 }
138 } 136 }
139} 137}
diff --git a/embassy-stm32-wpan/src/ble.rs b/embassy-stm32-wpan/src/sub/ble.rs
index 619cd66a0..cd32692e1 100644
--- a/embassy-stm32-wpan/src/ble.rs
+++ b/embassy-stm32-wpan/src/sub/ble.rs
@@ -1,14 +1,16 @@
1use core::marker::PhantomData; 1use core::marker::PhantomData;
2use core::ptr;
2 3
3use embassy_stm32::ipcc::Ipcc; 4use embassy_stm32::ipcc::Ipcc;
4use hci::Opcode; 5use hci::Opcode;
5 6
6use crate::channels;
7use crate::cmd::CmdPacket; 7use crate::cmd::CmdPacket;
8use crate::consts::TlPacketType; 8use crate::consts::{TlPacketType, TL_BLEEVT_CC_OPCODE, TL_BLEEVT_CS_OPCODE};
9use crate::evt::EvtBox; 9use crate::evt::{EvtBox, EvtPacket, EvtStub};
10use crate::sub::mm;
10use crate::tables::{BleTable, BLE_CMD_BUFFER, CS_BUFFER, EVT_QUEUE, HCI_ACL_DATA_BUFFER, TL_BLE_TABLE}; 11use crate::tables::{BleTable, BLE_CMD_BUFFER, CS_BUFFER, EVT_QUEUE, HCI_ACL_DATA_BUFFER, TL_BLE_TABLE};
11use crate::unsafe_linked_list::LinkedListNode; 12use crate::unsafe_linked_list::LinkedListNode;
13use crate::{channels, evt};
12 14
13pub struct Ble { 15pub struct Ble {
14 phantom: PhantomData<Ble>, 16 phantom: PhantomData<Ble>,
@@ -30,7 +32,7 @@ impl Ble {
30 Self { phantom: PhantomData } 32 Self { phantom: PhantomData }
31 } 33 }
32 /// `HW_IPCC_BLE_EvtNot` 34 /// `HW_IPCC_BLE_EvtNot`
33 pub async fn tl_read(&self) -> EvtBox { 35 pub async fn tl_read(&self) -> EvtBox<Self> {
34 Ipcc::receive(channels::cpu2::IPCC_BLE_EVENT_CHANNEL, || unsafe { 36 Ipcc::receive(channels::cpu2::IPCC_BLE_EVENT_CHANNEL, || unsafe {
35 if let Some(node_ptr) = LinkedListNode::remove_head(EVT_QUEUE.as_mut_ptr()) { 37 if let Some(node_ptr) = LinkedListNode::remove_head(EVT_QUEUE.as_mut_ptr()) {
36 Some(EvtBox::new(node_ptr.cast())) 38 Some(EvtBox::new(node_ptr.cast()))
@@ -63,6 +65,21 @@ impl Ble {
63 } 65 }
64} 66}
65 67
68impl evt::MemoryManager for Ble {
69 /// SAFETY: passing a pointer to something other than a managed event packet is UB
70 unsafe fn drop_event_packet(evt: *mut EvtPacket) {
71 let stub = unsafe {
72 let p_evt_stub = &(*evt).evt_serial as *const _ as *const EvtStub;
73
74 ptr::read_volatile(p_evt_stub)
75 };
76
77 if !(stub.evt_code == TL_BLEEVT_CS_OPCODE || stub.evt_code == TL_BLEEVT_CC_OPCODE) {
78 mm::MemoryManager::drop_event_packet(evt);
79 }
80 }
81}
82
66pub extern crate stm32wb_hci as hci; 83pub extern crate stm32wb_hci as hci;
67 84
68impl hci::Controller for Ble { 85impl hci::Controller for Ble {
diff --git a/embassy-stm32-wpan/src/mac.rs b/embassy-stm32-wpan/src/sub/mac.rs
index d2be1b85c..fd8af8609 100644
--- a/embassy-stm32-wpan/src/mac.rs
+++ b/embassy-stm32-wpan/src/sub/mac.rs
@@ -8,13 +8,13 @@ use embassy_futures::poll_once;
8use embassy_stm32::ipcc::Ipcc; 8use embassy_stm32::ipcc::Ipcc;
9use embassy_sync::waitqueue::AtomicWaker; 9use embassy_sync::waitqueue::AtomicWaker;
10 10
11use crate::channels;
12use crate::cmd::CmdPacket; 11use crate::cmd::CmdPacket;
13use crate::consts::TlPacketType; 12use crate::consts::TlPacketType;
14use crate::evt::{EvtBox, EvtPacket}; 13use crate::evt::{EvtBox, EvtPacket};
15use crate::tables::{ 14use crate::tables::{
16 Mac802_15_4Table, MAC_802_15_4_CMD_BUFFER, MAC_802_15_4_NOTIF_RSP_EVT_BUFFER, TL_MAC_802_15_4_TABLE, 15 Mac802_15_4Table, MAC_802_15_4_CMD_BUFFER, MAC_802_15_4_NOTIF_RSP_EVT_BUFFER, TL_MAC_802_15_4_TABLE,
17}; 16};
17use crate::{channels, evt};
18 18
19static MAC_WAKER: AtomicWaker = AtomicWaker::new(); 19static MAC_WAKER: AtomicWaker = AtomicWaker::new();
20static MAC_EVT_OUT: AtomicBool = AtomicBool::new(false); 20static MAC_EVT_OUT: AtomicBool = AtomicBool::new(false);
@@ -36,31 +36,10 @@ impl Mac {
36 Self { phantom: PhantomData } 36 Self { phantom: PhantomData }
37 } 37 }
38 38
39 /// SAFETY: passing a pointer to something other than a managed event packet is UB
40 pub(crate) unsafe fn drop_event_packet(_: *mut EvtPacket) {
41 // Write the ack
42 CmdPacket::write_into(
43 MAC_802_15_4_NOTIF_RSP_EVT_BUFFER.as_mut_ptr() as *mut _,
44 TlPacketType::OtAck,
45 0,
46 &[],
47 );
48
49 // Clear the rx flag
50 let _ = poll_once(Ipcc::receive::<bool>(
51 channels::cpu2::IPCC_MAC_802_15_4_NOTIFICATION_ACK_CHANNEL,
52 || None,
53 ));
54
55 // Allow a new read call
56 MAC_EVT_OUT.store(false, Ordering::SeqCst);
57 MAC_WAKER.wake();
58 }
59
60 /// `HW_IPCC_MAC_802_15_4_EvtNot` 39 /// `HW_IPCC_MAC_802_15_4_EvtNot`
61 /// 40 ///
62 /// This function will stall if the previous `EvtBox` has not been dropped 41 /// This function will stall if the previous `EvtBox` has not been dropped
63 pub async fn read(&self) -> EvtBox { 42 pub async fn read(&self) -> EvtBox<Self> {
64 // Wait for the last event box to be dropped 43 // Wait for the last event box to be dropped
65 poll_fn(|cx| { 44 poll_fn(|cx| {
66 MAC_WAKER.register(cx.waker()); 45 MAC_WAKER.register(cx.waker());
@@ -109,3 +88,26 @@ impl Mac {
109 .await; 88 .await;
110 } 89 }
111} 90}
91
92impl evt::MemoryManager for Mac {
93 /// SAFETY: passing a pointer to something other than a managed event packet is UB
94 unsafe fn drop_event_packet(_: *mut EvtPacket) {
95 // Write the ack
96 CmdPacket::write_into(
97 MAC_802_15_4_NOTIF_RSP_EVT_BUFFER.as_mut_ptr() as *mut _,
98 TlPacketType::OtAck,
99 0,
100 &[],
101 );
102
103 // Clear the rx flag
104 let _ = poll_once(Ipcc::receive::<bool>(
105 channels::cpu2::IPCC_MAC_802_15_4_NOTIFICATION_ACK_CHANNEL,
106 || None,
107 ));
108
109 // Allow a new read call
110 MAC_EVT_OUT.store(false, Ordering::SeqCst);
111 MAC_WAKER.wake();
112 }
113}
diff --git a/embassy-stm32-wpan/src/mm.rs b/embassy-stm32-wpan/src/sub/mm.rs
index 047fddcd4..1f2ecac2e 100644
--- a/embassy-stm32-wpan/src/mm.rs
+++ b/embassy-stm32-wpan/src/sub/mm.rs
@@ -8,13 +8,13 @@ use cortex_m::interrupt;
8use embassy_stm32::ipcc::Ipcc; 8use embassy_stm32::ipcc::Ipcc;
9use embassy_sync::waitqueue::AtomicWaker; 9use embassy_sync::waitqueue::AtomicWaker;
10 10
11use crate::channels;
12use crate::consts::POOL_SIZE; 11use crate::consts::POOL_SIZE;
13use crate::evt::EvtPacket; 12use crate::evt::EvtPacket;
14use crate::tables::{ 13use crate::tables::{
15 MemManagerTable, BLE_SPARE_EVT_BUF, EVT_POOL, FREE_BUF_QUEUE, SYS_SPARE_EVT_BUF, TL_MEM_MANAGER_TABLE, 14 MemManagerTable, BLE_SPARE_EVT_BUF, EVT_POOL, FREE_BUF_QUEUE, SYS_SPARE_EVT_BUF, TL_MEM_MANAGER_TABLE,
16}; 15};
17use crate::unsafe_linked_list::LinkedListNode; 16use crate::unsafe_linked_list::LinkedListNode;
17use crate::{channels, evt};
18 18
19static MM_WAKER: AtomicWaker = AtomicWaker::new(); 19static MM_WAKER: AtomicWaker = AtomicWaker::new();
20static mut LOCAL_FREE_BUF_QUEUE: MaybeUninit<LinkedListNode> = MaybeUninit::uninit(); 20static mut LOCAL_FREE_BUF_QUEUE: MaybeUninit<LinkedListNode> = MaybeUninit::uninit();
@@ -43,16 +43,6 @@ impl MemoryManager {
43 Self { phantom: PhantomData } 43 Self { phantom: PhantomData }
44 } 44 }
45 45
46 #[allow(dead_code)]
47 /// SAFETY: passing a pointer to something other than a managed event packet is UB
48 pub(crate) unsafe fn drop_event_packet(evt: *mut EvtPacket) {
49 interrupt::free(|_| unsafe {
50 LinkedListNode::insert_head(LOCAL_FREE_BUF_QUEUE.as_mut_ptr(), evt as *mut _);
51 });
52
53 MM_WAKER.wake();
54 }
55
56 pub async fn run_queue(&self) { 46 pub async fn run_queue(&self) {
57 loop { 47 loop {
58 poll_fn(|cx| unsafe { 48 poll_fn(|cx| unsafe {
@@ -77,3 +67,14 @@ impl MemoryManager {
77 } 67 }
78 } 68 }
79} 69}
70
71impl evt::MemoryManager for MemoryManager {
72 /// SAFETY: passing a pointer to something other than a managed event packet is UB
73 unsafe fn drop_event_packet(evt: *mut EvtPacket) {
74 interrupt::free(|_| unsafe {
75 LinkedListNode::insert_head(LOCAL_FREE_BUF_QUEUE.as_mut_ptr(), evt as *mut _);
76 });
77
78 MM_WAKER.wake();
79 }
80}
diff --git a/embassy-stm32-wpan/src/sub/mod.rs b/embassy-stm32-wpan/src/sub/mod.rs
new file mode 100644
index 000000000..bee3dbdf1
--- /dev/null
+++ b/embassy-stm32-wpan/src/sub/mod.rs
@@ -0,0 +1,6 @@
1#[cfg(feature = "ble")]
2pub mod ble;
3#[cfg(feature = "mac")]
4pub mod mac;
5pub mod mm;
6pub mod sys;
diff --git a/embassy-stm32-wpan/src/sys.rs b/embassy-stm32-wpan/src/sub/sys.rs
index 2b699b725..af652860d 100644
--- a/embassy-stm32-wpan/src/sys.rs
+++ b/embassy-stm32-wpan/src/sub/sys.rs
@@ -6,6 +6,7 @@ use crate::consts::TlPacketType;
6use crate::evt::{CcEvt, EvtBox, EvtPacket}; 6use crate::evt::{CcEvt, EvtBox, EvtPacket};
7#[allow(unused_imports)] 7#[allow(unused_imports)]
8use crate::shci::{SchiCommandStatus, ShciBleInitCmdParam, ShciOpcode}; 8use crate::shci::{SchiCommandStatus, ShciBleInitCmdParam, ShciOpcode};
9use crate::sub::mm;
9use crate::tables::{SysTable, WirelessFwInfoTable}; 10use crate::tables::{SysTable, WirelessFwInfoTable};
10use crate::unsafe_linked_list::LinkedListNode; 11use crate::unsafe_linked_list::LinkedListNode;
11use crate::{channels, Ipcc, SYSTEM_EVT_QUEUE, SYS_CMD_BUF, TL_DEVICE_INFO_TABLE, TL_SYS_TABLE}; 12use crate::{channels, Ipcc, SYSTEM_EVT_QUEUE, SYS_CMD_BUF, TL_DEVICE_INFO_TABLE, TL_SYS_TABLE};
@@ -73,7 +74,7 @@ impl Sys {
73 } 74 }
74 75
75 /// `HW_IPCC_SYS_EvtNot` 76 /// `HW_IPCC_SYS_EvtNot`
76 pub async fn read(&self) -> EvtBox { 77 pub async fn read(&self) -> EvtBox<mm::MemoryManager> {
77 Ipcc::receive(channels::cpu2::IPCC_SYSTEM_EVENT_CHANNEL, || unsafe { 78 Ipcc::receive(channels::cpu2::IPCC_SYSTEM_EVENT_CHANNEL, || unsafe {
78 if let Some(node_ptr) = LinkedListNode::remove_head(SYSTEM_EVT_QUEUE.as_mut_ptr()) { 79 if let Some(node_ptr) = LinkedListNode::remove_head(SYSTEM_EVT_QUEUE.as_mut_ptr()) {
79 Some(EvtBox::new(node_ptr.cast())) 80 Some(EvtBox::new(node_ptr.cast()))
diff --git a/embassy-stm32-wpan/src/tables.rs b/embassy-stm32-wpan/src/tables.rs
index 3f26282c6..1b5dcdf2e 100644
--- a/embassy-stm32-wpan/src/tables.rs
+++ b/embassy-stm32-wpan/src/tables.rs
@@ -1,5 +1,6 @@
1use core::mem::MaybeUninit; 1use core::mem::MaybeUninit;
2 2
3use aligned::{Aligned, A4};
3use bit_field::BitField; 4use bit_field::BitField;
4 5
5use crate::cmd::{AclDataPacket, CmdPacket}; 6use crate::cmd::{AclDataPacket, CmdPacket};
@@ -164,9 +165,6 @@ pub struct Mac802_15_4Table {
164 pub evt_queue: *const u8, 165 pub evt_queue: *const u8,
165} 166}
166 167
167#[repr(C, align(4))]
168pub struct AlignedData<const L: usize>([u8; L]);
169
170/// Reference table. Contains pointers to all other tables. 168/// Reference table. Contains pointers to all other tables.
171#[derive(Debug, Copy, Clone)] 169#[derive(Debug, Copy, Clone)]
172#[repr(C)] 170#[repr(C)]
@@ -222,10 +220,9 @@ pub static mut FREE_BUF_QUEUE: MaybeUninit<LinkedListNode> = MaybeUninit::uninit
222#[link_section = "MB_MEM1"] 220#[link_section = "MB_MEM1"]
223pub static mut TRACES_EVT_QUEUE: MaybeUninit<LinkedListNode> = MaybeUninit::uninit(); 221pub static mut TRACES_EVT_QUEUE: MaybeUninit<LinkedListNode> = MaybeUninit::uninit();
224 222
225const CS_BUFFER_SIZE: usize = TL_PACKET_HEADER_SIZE + TL_EVT_HEADER_SIZE + TL_CS_EVT_SIZE;
226
227#[link_section = "MB_MEM2"] 223#[link_section = "MB_MEM2"]
228pub static mut CS_BUFFER: MaybeUninit<AlignedData<CS_BUFFER_SIZE>> = MaybeUninit::uninit(); 224pub static mut CS_BUFFER: MaybeUninit<Aligned<A4, [u8; TL_PACKET_HEADER_SIZE + TL_EVT_HEADER_SIZE + TL_CS_EVT_SIZE]>> =
225 MaybeUninit::uninit();
229 226
230#[link_section = "MB_MEM2"] 227#[link_section = "MB_MEM2"]
231pub static mut EVT_QUEUE: MaybeUninit<LinkedListNode> = MaybeUninit::uninit(); 228pub static mut EVT_QUEUE: MaybeUninit<LinkedListNode> = MaybeUninit::uninit();
@@ -239,34 +236,29 @@ pub static mut SYSTEM_EVT_QUEUE: MaybeUninit<LinkedListNode> = MaybeUninit::unin
239pub static mut MAC_802_15_4_CMD_BUFFER: MaybeUninit<CmdPacket> = MaybeUninit::uninit(); 236pub static mut MAC_802_15_4_CMD_BUFFER: MaybeUninit<CmdPacket> = MaybeUninit::uninit();
240 237
241#[cfg(feature = "mac")] 238#[cfg(feature = "mac")]
242const MAC_802_15_4_NOTIF_RSP_EVT_BUFFER_SIZE: usize = TL_PACKET_HEADER_SIZE + TL_EVT_HEADER_SIZE + 255;
243
244#[cfg(feature = "mac")]
245#[link_section = "MB_MEM2"] 239#[link_section = "MB_MEM2"]
246pub static mut MAC_802_15_4_NOTIF_RSP_EVT_BUFFER: MaybeUninit<AlignedData<MAC_802_15_4_NOTIF_RSP_EVT_BUFFER_SIZE>> = 240pub static mut MAC_802_15_4_NOTIF_RSP_EVT_BUFFER: MaybeUninit<
247 MaybeUninit::uninit(); 241 Aligned<A4, [u8; TL_PACKET_HEADER_SIZE + TL_EVT_HEADER_SIZE + 255]>,
242> = MaybeUninit::uninit();
248 243
249#[link_section = "MB_MEM2"] 244#[link_section = "MB_MEM2"]
250pub static mut EVT_POOL: MaybeUninit<[u8; POOL_SIZE]> = MaybeUninit::uninit(); 245pub static mut EVT_POOL: MaybeUninit<Aligned<A4, [u8; POOL_SIZE]>> = MaybeUninit::uninit();
251 246
252#[link_section = "MB_MEM2"] 247#[link_section = "MB_MEM2"]
253pub static mut SYS_CMD_BUF: MaybeUninit<CmdPacket> = MaybeUninit::uninit(); 248pub static mut SYS_CMD_BUF: MaybeUninit<CmdPacket> = MaybeUninit::uninit();
254 249
255const SYS_SPARE_EVT_BUF_SIZE: usize = TL_PACKET_HEADER_SIZE + TL_EVT_HEADER_SIZE + 255;
256
257#[link_section = "MB_MEM2"] 250#[link_section = "MB_MEM2"]
258pub static mut SYS_SPARE_EVT_BUF: MaybeUninit<AlignedData<SYS_SPARE_EVT_BUF_SIZE>> = MaybeUninit::uninit(); 251pub static mut SYS_SPARE_EVT_BUF: MaybeUninit<Aligned<A4, [u8; TL_PACKET_HEADER_SIZE + TL_EVT_HEADER_SIZE + 255]>> =
252 MaybeUninit::uninit();
259 253
260#[link_section = "MB_MEM1"] 254#[link_section = "MB_MEM1"]
261pub static mut BLE_CMD_BUFFER: MaybeUninit<CmdPacket> = MaybeUninit::uninit(); 255pub static mut BLE_CMD_BUFFER: MaybeUninit<CmdPacket> = MaybeUninit::uninit();
262 256
263const BLE_SPARE_EVT_BUF_SIZE: usize = TL_PACKET_HEADER_SIZE + TL_EVT_HEADER_SIZE + 255;
264
265#[link_section = "MB_MEM2"] 257#[link_section = "MB_MEM2"]
266pub static mut BLE_SPARE_EVT_BUF: MaybeUninit<AlignedData<BLE_SPARE_EVT_BUF_SIZE>> = MaybeUninit::uninit(); 258pub static mut BLE_SPARE_EVT_BUF: MaybeUninit<Aligned<A4, [u8; TL_PACKET_HEADER_SIZE + TL_EVT_HEADER_SIZE + 255]>> =
267 259 MaybeUninit::uninit();
268const HCI_ACL_DATA_BUFFER_SIZE: usize = TL_PACKET_HEADER_SIZE + 5 + 251;
269 260
270#[link_section = "MB_MEM2"] 261#[link_section = "MB_MEM2"]
271// fuck these "magic" numbers from ST ---v---v 262// fuck these "magic" numbers from ST ---v---v
272pub static mut HCI_ACL_DATA_BUFFER: MaybeUninit<[u8; HCI_ACL_DATA_BUFFER_SIZE]> = MaybeUninit::uninit(); 263pub static mut HCI_ACL_DATA_BUFFER: MaybeUninit<Aligned<A4, [u8; TL_PACKET_HEADER_SIZE + 5 + 251]>> =
264 MaybeUninit::uninit();
diff --git a/embassy-stm32/tl_mbox.x.in b/embassy-stm32-wpan/tl_mbox.x.in
index b6eecb429..b6eecb429 100644
--- a/embassy-stm32/tl_mbox.x.in
+++ b/embassy-stm32-wpan/tl_mbox.x.in
diff --git a/embassy-stm32/build.rs b/embassy-stm32/build.rs
index f71074bcf..40103d322 100644
--- a/embassy-stm32/build.rs
+++ b/embassy-stm32/build.rs
@@ -911,16 +911,6 @@ fn main() {
911 println!("cargo:rustc-cfg={}x{}", &chip_name[..9], &chip_name[10..11]); 911 println!("cargo:rustc-cfg={}x{}", &chip_name[..9], &chip_name[10..11]);
912 } 912 }
913 913
914 // ========
915 // stm32wb tl_mbox link sections
916
917 if chip_name.starts_with("stm32wb") {
918 let out_file = out_dir.join("tl_mbox.x").to_string_lossy().to_string();
919 fs::write(out_file, fs::read_to_string("tl_mbox.x.in").unwrap()).unwrap();
920 println!("cargo:rustc-link-search={}", out_dir.display());
921 println!("cargo:rerun-if-changed=tl_mbox.x.in");
922 }
923
924 // ======= 914 // =======
925 // Features for targeting groups of chips 915 // Features for targeting groups of chips
926 916
diff --git a/examples/stm32wb/src/bin/eddystone_beacon.rs b/examples/stm32wb/src/bin/eddystone_beacon.rs
index fdd5be4a2..b99f8cb2e 100644
--- a/examples/stm32wb/src/bin/eddystone_beacon.rs
+++ b/examples/stm32wb/src/bin/eddystone_beacon.rs
@@ -8,15 +8,15 @@ use defmt::*;
8use embassy_executor::Spawner; 8use embassy_executor::Spawner;
9use embassy_stm32::bind_interrupts; 9use embassy_stm32::bind_interrupts;
10use embassy_stm32::ipcc::{Config, ReceiveInterruptHandler, TransmitInterruptHandler}; 10use embassy_stm32::ipcc::{Config, ReceiveInterruptHandler, TransmitInterruptHandler};
11use embassy_stm32_wpan::ble::hci::host::uart::UartHci; 11use embassy_stm32_wpan::hci::host::uart::UartHci;
12use embassy_stm32_wpan::ble::hci::host::{AdvertisingFilterPolicy, EncryptionKey, HostHci, OwnAddressType}; 12use embassy_stm32_wpan::hci::host::{AdvertisingFilterPolicy, EncryptionKey, HostHci, OwnAddressType};
13use embassy_stm32_wpan::ble::hci::types::AdvertisingType; 13use embassy_stm32_wpan::hci::types::AdvertisingType;
14use embassy_stm32_wpan::ble::hci::vendor::stm32wb::command::gap::{ 14use embassy_stm32_wpan::hci::vendor::stm32wb::command::gap::{
15 AdvertisingDataType, DiscoverableParameters, GapCommands, Role, 15 AdvertisingDataType, DiscoverableParameters, GapCommands, Role,
16}; 16};
17use embassy_stm32_wpan::ble::hci::vendor::stm32wb::command::gatt::GattCommands; 17use embassy_stm32_wpan::hci::vendor::stm32wb::command::gatt::GattCommands;
18use embassy_stm32_wpan::ble::hci::vendor::stm32wb::command::hal::{ConfigData, HalCommands, PowerLevel}; 18use embassy_stm32_wpan::hci::vendor::stm32wb::command::hal::{ConfigData, HalCommands, PowerLevel};
19use embassy_stm32_wpan::ble::hci::BdAddr; 19use embassy_stm32_wpan::hci::BdAddr;
20use embassy_stm32_wpan::lhci::LhciC1DeviceInformationCcrp; 20use embassy_stm32_wpan::lhci::LhciC1DeviceInformationCcrp;
21use embassy_stm32_wpan::TlMbox; 21use embassy_stm32_wpan::TlMbox;
22use {defmt_rtt as _, panic_probe as _}; 22use {defmt_rtt as _, panic_probe as _};
diff --git a/examples/stm32wb/src/bin/tl_mbox_mac.rs b/examples/stm32wb/src/bin/tl_mbox_mac.rs
index afd319a41..f67be4682 100644
--- a/examples/stm32wb/src/bin/tl_mbox_mac.rs
+++ b/examples/stm32wb/src/bin/tl_mbox_mac.rs
@@ -49,7 +49,9 @@ async fn main(_spawner: Spawner) {
49 let sys_event = mbox.sys_subsystem.read().await; 49 let sys_event = mbox.sys_subsystem.read().await;
50 info!("sys event: {}", sys_event.payload()); 50 info!("sys event: {}", sys_event.payload());
51 51
52 mbox.sys_subsystem.shci_c2_mac_802_15_4_init().await; 52 let result = mbox.sys_subsystem.shci_c2_mac_802_15_4_init().await;
53 info!("initialized mac: {}", result);
54
53 // 55 //
54 // info!("starting ble..."); 56 // info!("starting ble...");
55 // mbox.ble_subsystem.t_write(0x0c, &[]).await; 57 // mbox.ble_subsystem.t_write(0x0c, &[]).await;
diff --git a/tests/stm32/src/bin/tl_mbox.rs b/tests/stm32/src/bin/tl_mbox.rs
index 76c736a5b..af3832709 100644
--- a/tests/stm32/src/bin/tl_mbox.rs
+++ b/tests/stm32/src/bin/tl_mbox.rs
@@ -12,17 +12,18 @@ use common::*;
12use embassy_executor::Spawner; 12use embassy_executor::Spawner;
13use embassy_stm32::bind_interrupts; 13use embassy_stm32::bind_interrupts;
14use embassy_stm32::ipcc::{Config, ReceiveInterruptHandler, TransmitInterruptHandler}; 14use embassy_stm32::ipcc::{Config, ReceiveInterruptHandler, TransmitInterruptHandler};
15use embassy_stm32_wpan::ble::hci::host::uart::UartHci; 15use embassy_stm32_wpan::hci::host::uart::UartHci;
16use embassy_stm32_wpan::ble::hci::host::{AdvertisingFilterPolicy, EncryptionKey, HostHci, OwnAddressType}; 16use embassy_stm32_wpan::hci::host::{AdvertisingFilterPolicy, EncryptionKey, HostHci, OwnAddressType};
17use embassy_stm32_wpan::ble::hci::types::AdvertisingType; 17use embassy_stm32_wpan::hci::types::AdvertisingType;
18use embassy_stm32_wpan::ble::hci::vendor::stm32wb::command::gap::{ 18use embassy_stm32_wpan::hci::vendor::stm32wb::command::gap::{
19 AdvertisingDataType, DiscoverableParameters, GapCommands, Role, 19 AdvertisingDataType, DiscoverableParameters, GapCommands, Role,
20}; 20};
21use embassy_stm32_wpan::ble::hci::vendor::stm32wb::command::gatt::GattCommands; 21use embassy_stm32_wpan::hci::vendor::stm32wb::command::gatt::GattCommands;
22use embassy_stm32_wpan::ble::hci::vendor::stm32wb::command::hal::{ConfigData, HalCommands, PowerLevel}; 22use embassy_stm32_wpan::hci::vendor::stm32wb::command::hal::{ConfigData, HalCommands, PowerLevel};
23use embassy_stm32_wpan::ble::hci::BdAddr; 23use embassy_stm32_wpan::hci::BdAddr;
24use embassy_stm32_wpan::lhci::LhciC1DeviceInformationCcrp; 24use embassy_stm32_wpan::lhci::LhciC1DeviceInformationCcrp;
25use embassy_stm32_wpan::{mm, TlMbox}; 25use embassy_stm32_wpan::sub::mm;
26use embassy_stm32_wpan::TlMbox;
26use {defmt_rtt as _, panic_probe as _}; 27use {defmt_rtt as _, panic_probe as _};
27 28
28bind_interrupts!(struct Irqs{ 29bind_interrupts!(struct Irqs{
@@ -38,14 +39,14 @@ async fn run_mm_queue(memory_manager: mm::MemoryManager) {
38} 39}
39 40
40#[embassy_executor::main] 41#[embassy_executor::main]
41async fn main(_spawner: Spawner) { 42async fn main(spawner: Spawner) {
42 let p = embassy_stm32::init(config()); 43 let p = embassy_stm32::init(config());
43 info!("Hello World!"); 44 info!("Hello World!");
44 45
45 let config = Config::default(); 46 let config = Config::default();
46 let mut mbox = TlMbox::init(p.IPCC, Irqs, config); 47 let mut mbox = TlMbox::init(p.IPCC, Irqs, config);
47 48
48 // spawner.spawn(run_mm_queue(mbox.mm_subsystem)).unwrap(); 49 spawner.spawn(run_mm_queue(mbox.mm_subsystem)).unwrap();
49 50
50 let sys_event = mbox.sys_subsystem.read().await; 51 let sys_event = mbox.sys_subsystem.read().await;
51 info!("sys event: {}", sys_event.payload()); 52 info!("sys event: {}", sys_event.payload());