diff --git a/embassy-stm32-wpan/src/cmd.rs b/embassy-stm32-wpan/src/cmd.rs index 8428b6ffc..928357384 100644 --- a/embassy-stm32-wpan/src/cmd.rs +++ b/embassy-stm32-wpan/src/cmd.rs @@ -37,7 +37,7 @@ pub struct CmdSerialStub { } #[derive(Copy, Clone, Default)] -#[repr(C, packed(4))] +#[repr(C, packed)] pub struct CmdPacket { pub header: PacketHeader, pub cmdserial: CmdSerial, diff --git a/embassy-stm32-wpan/src/consts.rs b/embassy-stm32-wpan/src/consts.rs index f234151d7..1c84addb1 100644 --- a/embassy-stm32-wpan/src/consts.rs +++ b/embassy-stm32-wpan/src/consts.rs @@ -79,6 +79,7 @@ pub const CFG_TL_BLE_MOST_EVENT_PAYLOAD_SIZE: usize = 255; pub const TL_BLE_EVENT_FRAME_SIZE: usize = TL_EVT_HEADER_SIZE + CFG_TL_BLE_MOST_EVENT_PAYLOAD_SIZE; pub const POOL_SIZE: usize = CFG_TL_BLE_EVT_QUEUE_LENGTH * 4 * divc(TL_PACKET_HEADER_SIZE + TL_BLE_EVENT_FRAME_SIZE, 4); +pub const C_SIZE_CMD_STRING: usize = 256; pub const fn divc(x: usize, y: usize) -> usize { (x + y - 1) / y diff --git a/embassy-stm32-wpan/src/lib.rs b/embassy-stm32-wpan/src/lib.rs index 99c610583..3a45c5978 100644 --- a/embassy-stm32-wpan/src/lib.rs +++ b/embassy-stm32-wpan/src/lib.rs @@ -60,9 +60,9 @@ impl<'d> TlMbox<'d> { mem_manager_table: TL_MEM_MANAGER_TABLE.as_ptr(), traces_table: TL_TRACES_TABLE.as_ptr(), mac_802_15_4_table: TL_MAC_802_15_4_TABLE.as_ptr(), - // zigbee_table: TL_ZIGBEE_TABLE.as_ptr(), - // lld_tests_table: TL_LLD_TESTS_TABLE.as_ptr(), - // ble_lld_table: TL_BLE_LLD_TABLE.as_ptr(), + zigbee_table: TL_ZIGBEE_TABLE.as_ptr(), + lld_tests_table: TL_LLD_TESTS_TABLE.as_ptr(), + ble_lld_table: TL_BLE_LLD_TABLE.as_ptr(), }); TL_SYS_TABLE @@ -87,15 +87,15 @@ impl<'d> TlMbox<'d> { TL_MAC_802_15_4_TABLE .as_mut_ptr() .write_volatile(MaybeUninit::zeroed().assume_init()); - // TL_ZIGBEE_TABLE - // .as_mut_ptr() - // .write_volatile(MaybeUninit::zeroed().assume_init()); - // TL_LLD_TESTS_TABLE - // .as_mut_ptr() - // .write_volatile(MaybeUninit::zeroed().assume_init()); - // TL_BLE_LLD_TABLE - // .as_mut_ptr() - // .write_volatile(MaybeUninit::zeroed().assume_init()); + TL_ZIGBEE_TABLE + .as_mut_ptr() + .write_volatile(MaybeUninit::zeroed().assume_init()); + TL_LLD_TESTS_TABLE + .as_mut_ptr() + .write_volatile(MaybeUninit::zeroed().assume_init()); + TL_BLE_LLD_TABLE + .as_mut_ptr() + .write_volatile(MaybeUninit::zeroed().assume_init()); EVT_POOL .as_mut_ptr() @@ -103,18 +103,30 @@ impl<'d> TlMbox<'d> { SYS_SPARE_EVT_BUF .as_mut_ptr() .write_volatile(MaybeUninit::zeroed().assume_init()); - BLE_SPARE_EVT_BUF + CS_BUFFER .as_mut_ptr() .write_volatile(MaybeUninit::zeroed().assume_init()); + #[cfg(feature = "ble")] { + BLE_SPARE_EVT_BUF + .as_mut_ptr() + .write_volatile(MaybeUninit::zeroed().assume_init()); + BLE_CMD_BUFFER .as_mut_ptr() .write_volatile(MaybeUninit::zeroed().assume_init()); HCI_ACL_DATA_BUFFER .as_mut_ptr() .write_volatile(MaybeUninit::zeroed().assume_init()); - CS_BUFFER + } + + #[cfg(feature = "mac")] + { + MAC_802_15_4_CMD_BUFFER + .as_mut_ptr() + .write_volatile(MaybeUninit::zeroed().assume_init()); + MAC_802_15_4_NOTIF_RSP_EVT_BUFFER .as_mut_ptr() .write_volatile(MaybeUninit::zeroed().assume_init()); } diff --git a/embassy-stm32-wpan/src/sub/mac.rs b/embassy-stm32-wpan/src/sub/mac.rs index fd8af8609..f7a59b0e4 100644 --- a/embassy-stm32-wpan/src/sub/mac.rs +++ b/embassy-stm32-wpan/src/sub/mac.rs @@ -11,9 +11,7 @@ use embassy_sync::waitqueue::AtomicWaker; use crate::cmd::CmdPacket; use crate::consts::TlPacketType; use crate::evt::{EvtBox, EvtPacket}; -use crate::tables::{ - Mac802_15_4Table, MAC_802_15_4_CMD_BUFFER, MAC_802_15_4_NOTIF_RSP_EVT_BUFFER, TL_MAC_802_15_4_TABLE, -}; +use crate::tables::{MAC_802_15_4_CMD_BUFFER, MAC_802_15_4_NOTIF_RSP_EVT_BUFFER}; use crate::{channels, evt}; static MAC_WAKER: AtomicWaker = AtomicWaker::new(); @@ -25,14 +23,6 @@ pub struct Mac { impl Mac { pub(crate) fn new() -> Self { - unsafe { - TL_MAC_802_15_4_TABLE.as_mut_ptr().write_volatile(Mac802_15_4Table { - p_cmdrsp_buffer: MAC_802_15_4_CMD_BUFFER.as_mut_ptr().cast(), - p_notack_buffer: MAC_802_15_4_NOTIF_RSP_EVT_BUFFER.as_mut_ptr().cast(), - evt_queue: ptr::null_mut(), - }); - } - Self { phantom: PhantomData } } diff --git a/embassy-stm32-wpan/src/sub/mm.rs b/embassy-stm32-wpan/src/sub/mm.rs index 1f2ecac2e..da05ad1dd 100644 --- a/embassy-stm32-wpan/src/sub/mm.rs +++ b/embassy-stm32-wpan/src/sub/mm.rs @@ -4,20 +4,21 @@ use core::marker::PhantomData; use core::mem::MaybeUninit; use core::task::Poll; +use aligned::{Aligned, A4}; use cortex_m::interrupt; use embassy_stm32::ipcc::Ipcc; use embassy_sync::waitqueue::AtomicWaker; use crate::consts::POOL_SIZE; use crate::evt::EvtPacket; -use crate::tables::{ - MemManagerTable, BLE_SPARE_EVT_BUF, EVT_POOL, FREE_BUF_QUEUE, SYS_SPARE_EVT_BUF, TL_MEM_MANAGER_TABLE, -}; +#[cfg(feature = "ble")] +use crate::tables::BLE_SPARE_EVT_BUF; +use crate::tables::{MemManagerTable, EVT_POOL, FREE_BUF_QUEUE, SYS_SPARE_EVT_BUF, TL_MEM_MANAGER_TABLE}; use crate::unsafe_linked_list::LinkedListNode; use crate::{channels, evt}; static MM_WAKER: AtomicWaker = AtomicWaker::new(); -static mut LOCAL_FREE_BUF_QUEUE: MaybeUninit = MaybeUninit::uninit(); +static mut LOCAL_FREE_BUF_QUEUE: Aligned> = Aligned(MaybeUninit::uninit()); pub struct MemoryManager { phantom: PhantomData, @@ -30,7 +31,10 @@ impl MemoryManager { LinkedListNode::init_head(LOCAL_FREE_BUF_QUEUE.as_mut_ptr()); TL_MEM_MANAGER_TABLE.as_mut_ptr().write_volatile(MemManagerTable { + #[cfg(feature = "ble")] spare_ble_buffer: BLE_SPARE_EVT_BUF.as_ptr().cast(), + #[cfg(not(feature = "ble"))] + spare_ble_buffer: core::ptr::null(), spare_sys_buffer: SYS_SPARE_EVT_BUF.as_ptr().cast(), blepool: EVT_POOL.as_ptr().cast(), blepoolsize: POOL_SIZE as u32, diff --git a/embassy-stm32-wpan/src/sub/sys.rs b/embassy-stm32-wpan/src/sub/sys.rs index af652860d..caa4845f2 100644 --- a/embassy-stm32-wpan/src/sub/sys.rs +++ b/embassy-stm32-wpan/src/sub/sys.rs @@ -65,6 +65,25 @@ impl Sys { #[cfg(feature = "mac")] pub async fn shci_c2_mac_802_15_4_init(&self) -> SchiCommandStatus { + use crate::tables::{ + Mac802_15_4Table, TracesTable, MAC_802_15_4_CMD_BUFFER, MAC_802_15_4_NOTIF_RSP_EVT_BUFFER, + TL_MAC_802_15_4_TABLE, TL_TRACES_TABLE, TRACES_EVT_QUEUE, + }; + + unsafe { + LinkedListNode::init_head(TRACES_EVT_QUEUE.as_mut_ptr() as *mut _); + + TL_TRACES_TABLE.as_mut_ptr().write_volatile(TracesTable { + traces_queue: TRACES_EVT_QUEUE.as_ptr() as *const _, + }); + + TL_MAC_802_15_4_TABLE.as_mut_ptr().write_volatile(Mac802_15_4Table { + p_cmdrsp_buffer: MAC_802_15_4_CMD_BUFFER.as_mut_ptr().cast(), + p_notack_buffer: MAC_802_15_4_NOTIF_RSP_EVT_BUFFER.as_mut_ptr().cast(), + evt_queue: core::ptr::null_mut(), + }); + }; + self.write_and_get_response(ShciOpcode::Mac802_15_4Init, &[]).await } diff --git a/embassy-stm32-wpan/src/tables.rs b/embassy-stm32-wpan/src/tables.rs index 1b5dcdf2e..f2c250527 100644 --- a/embassy-stm32-wpan/src/tables.rs +++ b/embassy-stm32-wpan/src/tables.rs @@ -4,6 +4,8 @@ use aligned::{Aligned, A4}; use bit_field::BitField; use crate::cmd::{AclDataPacket, CmdPacket}; +#[cfg(feature = "mac")] +use crate::consts::C_SIZE_CMD_STRING; use crate::consts::{POOL_SIZE, TL_CS_EVT_SIZE, TL_EVT_HEADER_SIZE, TL_PACKET_HEADER_SIZE}; use crate::unsafe_linked_list::LinkedListNode; @@ -80,7 +82,7 @@ impl WirelessFwInfoTable { } #[derive(Debug, Clone)] -#[repr(C, align(4))] +#[repr(C)] pub struct DeviceInfoTable { pub safe_boot_info_table: SafeBootInfoTable, pub rss_info_table: RssInfoTable, @@ -88,7 +90,7 @@ pub struct DeviceInfoTable { } #[derive(Debug)] -#[repr(C, align(4))] +#[repr(C)] pub struct BleTable { pub pcmd_buffer: *mut CmdPacket, pub pcs_buffer: *const u8, @@ -97,16 +99,15 @@ pub struct BleTable { } #[derive(Debug)] -#[repr(C, align(4))] +#[repr(C)] pub struct ThreadTable { pub nostack_buffer: *const u8, pub clicmdrsp_buffer: *const u8, pub otcmdrsp_buffer: *const u8, } -// TODO: use later #[derive(Debug)] -#[repr(C, align(4))] +#[repr(C)] pub struct LldTestsTable { pub clicmdrsp_buffer: *const u8, pub m0cmd_buffer: *const u8, @@ -114,7 +115,7 @@ pub struct LldTestsTable { // TODO: use later #[derive(Debug)] -#[repr(C, align(4))] +#[repr(C)] pub struct BleLldTable { pub cmdrsp_buffer: *const u8, pub m0cmd_buffer: *const u8, @@ -122,7 +123,7 @@ pub struct BleLldTable { // TODO: use later #[derive(Debug)] -#[repr(C, align(4))] +#[repr(C)] pub struct ZigbeeTable { pub notif_m0_to_m4_buffer: *const u8, pub appli_cmd_m4_to_m0_bufer: *const u8, @@ -130,14 +131,14 @@ pub struct ZigbeeTable { } #[derive(Debug)] -#[repr(C, align(4))] +#[repr(C)] pub struct SysTable { pub pcmd_buffer: *mut CmdPacket, pub sys_queue: *const LinkedListNode, } #[derive(Debug)] -#[repr(C, align(4))] +#[repr(C)] pub struct MemManagerTable { pub spare_ble_buffer: *const u8, pub spare_sys_buffer: *const u8, @@ -152,13 +153,13 @@ pub struct MemManagerTable { } #[derive(Debug)] -#[repr(C, align(4))] +#[repr(C)] pub struct TracesTable { pub traces_queue: *const u8, } #[derive(Debug)] -#[repr(C, align(4))] +#[repr(C)] pub struct Mac802_15_4Table { pub p_cmdrsp_buffer: *const u8, pub p_notack_buffer: *const u8, @@ -176,6 +177,9 @@ pub struct RefTable { pub mem_manager_table: *const MemManagerTable, pub traces_table: *const TracesTable, pub mac_802_15_4_table: *const Mac802_15_4Table, + pub zigbee_table: *const ZigbeeTable, + pub lld_tests_table: *const LldTestsTable, + pub ble_lld_table: *const BleLldTable, } // --------------------- ref table --------------------- @@ -183,57 +187,57 @@ pub struct RefTable { pub static mut TL_REF_TABLE: MaybeUninit = MaybeUninit::uninit(); #[link_section = "MB_MEM1"] -pub static mut TL_DEVICE_INFO_TABLE: MaybeUninit = MaybeUninit::uninit(); +pub static mut TL_DEVICE_INFO_TABLE: Aligned> = Aligned(MaybeUninit::uninit()); #[link_section = "MB_MEM1"] -pub static mut TL_BLE_TABLE: MaybeUninit = MaybeUninit::uninit(); +pub static mut TL_BLE_TABLE: Aligned> = Aligned(MaybeUninit::uninit()); #[link_section = "MB_MEM1"] -pub static mut TL_THREAD_TABLE: MaybeUninit = MaybeUninit::uninit(); - -// #[link_section = "MB_MEM1"] -// pub static mut TL_LLD_TESTS_TABLE: MaybeUninit = MaybeUninit::uninit(); - -// #[link_section = "MB_MEM1"] -// pub static mut TL_BLE_LLD_TABLE: MaybeUninit = MaybeUninit::uninit(); +pub static mut TL_THREAD_TABLE: Aligned> = Aligned(MaybeUninit::uninit()); #[link_section = "MB_MEM1"] -pub static mut TL_SYS_TABLE: MaybeUninit = MaybeUninit::uninit(); +pub static mut TL_LLD_TESTS_TABLE: Aligned> = Aligned(MaybeUninit::uninit()); #[link_section = "MB_MEM1"] -pub static mut TL_MEM_MANAGER_TABLE: MaybeUninit = MaybeUninit::uninit(); +pub static mut TL_BLE_LLD_TABLE: Aligned> = Aligned(MaybeUninit::uninit()); #[link_section = "MB_MEM1"] -pub static mut TL_TRACES_TABLE: MaybeUninit = MaybeUninit::uninit(); +pub static mut TL_SYS_TABLE: Aligned> = Aligned(MaybeUninit::uninit()); #[link_section = "MB_MEM1"] -pub static mut TL_MAC_802_15_4_TABLE: MaybeUninit = MaybeUninit::uninit(); +pub static mut TL_MEM_MANAGER_TABLE: Aligned> = Aligned(MaybeUninit::uninit()); -// #[link_section = "MB_MEM1"] -// pub static mut TL_ZIGBEE_TABLE: MaybeUninit = MaybeUninit::uninit(); +#[link_section = "MB_MEM1"] +pub static mut TL_TRACES_TABLE: Aligned> = Aligned(MaybeUninit::uninit()); + +#[link_section = "MB_MEM1"] +pub static mut TL_MAC_802_15_4_TABLE: Aligned> = Aligned(MaybeUninit::uninit()); + +#[link_section = "MB_MEM1"] +pub static mut TL_ZIGBEE_TABLE: Aligned> = Aligned(MaybeUninit::uninit()); // --------------------- tables --------------------- #[link_section = "MB_MEM1"] -pub static mut FREE_BUF_QUEUE: MaybeUninit = MaybeUninit::uninit(); +pub static mut FREE_BUF_QUEUE: Aligned> = Aligned(MaybeUninit::uninit()); #[allow(dead_code)] #[link_section = "MB_MEM1"] -pub static mut TRACES_EVT_QUEUE: MaybeUninit = MaybeUninit::uninit(); +pub static mut TRACES_EVT_QUEUE: Aligned> = Aligned(MaybeUninit::uninit()); #[link_section = "MB_MEM2"] -pub static mut CS_BUFFER: MaybeUninit> = - MaybeUninit::uninit(); +pub static mut CS_BUFFER: Aligned> = + Aligned(MaybeUninit::uninit()); #[link_section = "MB_MEM2"] -pub static mut EVT_QUEUE: MaybeUninit = MaybeUninit::uninit(); +pub static mut EVT_QUEUE: Aligned> = Aligned(MaybeUninit::uninit()); #[link_section = "MB_MEM2"] -pub static mut SYSTEM_EVT_QUEUE: MaybeUninit = MaybeUninit::uninit(); +pub static mut SYSTEM_EVT_QUEUE: Aligned> = Aligned(MaybeUninit::uninit()); // --------------------- app tables --------------------- #[cfg(feature = "mac")] #[link_section = "MB_MEM2"] -pub static mut MAC_802_15_4_CMD_BUFFER: MaybeUninit = MaybeUninit::uninit(); +pub static mut MAC_802_15_4_CMD_BUFFER: Aligned> = Aligned(MaybeUninit::uninit()); #[cfg(feature = "mac")] #[link_section = "MB_MEM2"] @@ -242,23 +246,31 @@ pub static mut MAC_802_15_4_NOTIF_RSP_EVT_BUFFER: MaybeUninit< > = MaybeUninit::uninit(); #[link_section = "MB_MEM2"] -pub static mut EVT_POOL: MaybeUninit> = MaybeUninit::uninit(); +pub static mut EVT_POOL: Aligned> = Aligned(MaybeUninit::uninit()); #[link_section = "MB_MEM2"] -pub static mut SYS_CMD_BUF: MaybeUninit = MaybeUninit::uninit(); +pub static mut SYS_CMD_BUF: Aligned> = Aligned(MaybeUninit::uninit()); #[link_section = "MB_MEM2"] -pub static mut SYS_SPARE_EVT_BUF: MaybeUninit> = - MaybeUninit::uninit(); +pub static mut SYS_SPARE_EVT_BUF: Aligned> = + Aligned(MaybeUninit::uninit()); +#[cfg(feature = "mac")] +#[link_section = "MB_MEM2"] +pub static mut MAC_802_15_4_CNFINDNOT: Aligned> = + Aligned(MaybeUninit::uninit()); + +#[cfg(feature = "ble")] #[link_section = "MB_MEM1"] -pub static mut BLE_CMD_BUFFER: MaybeUninit = MaybeUninit::uninit(); +pub static mut BLE_CMD_BUFFER: Aligned> = Aligned(MaybeUninit::uninit()); +#[cfg(feature = "ble")] #[link_section = "MB_MEM2"] -pub static mut BLE_SPARE_EVT_BUF: MaybeUninit> = - MaybeUninit::uninit(); +pub static mut BLE_SPARE_EVT_BUF: Aligned> = + Aligned(MaybeUninit::uninit()); +#[cfg(feature = "ble")] #[link_section = "MB_MEM2"] // fuck these "magic" numbers from ST ---v---v -pub static mut HCI_ACL_DATA_BUFFER: MaybeUninit> = - MaybeUninit::uninit(); +pub static mut HCI_ACL_DATA_BUFFER: Aligned> = + Aligned(MaybeUninit::uninit()); diff --git a/examples/stm32wb/src/bin/tl_mbox_mac.rs b/examples/stm32wb/src/bin/tl_mbox_mac.rs index f67be4682..5931c392b 100644 --- a/examples/stm32wb/src/bin/tl_mbox_mac.rs +++ b/examples/stm32wb/src/bin/tl_mbox_mac.rs @@ -6,6 +6,7 @@ use defmt::*; use embassy_executor::Spawner; use embassy_stm32::bind_interrupts; use embassy_stm32::ipcc::{Config, ReceiveInterruptHandler, TransmitInterruptHandler}; +use embassy_stm32_wpan::sub::mm; use embassy_stm32_wpan::TlMbox; use {defmt_rtt as _, panic_probe as _}; @@ -14,8 +15,13 @@ bind_interrupts!(struct Irqs{ IPCC_C1_TX => TransmitInterruptHandler; }); +#[embassy_executor::task] +async fn run_mm_queue(memory_manager: mm::MemoryManager) { + memory_manager.run_queue().await; +} + #[embassy_executor::main] -async fn main(_spawner: Spawner) { +async fn main(spawner: Spawner) { /* How to make this work: @@ -46,9 +52,13 @@ async fn main(_spawner: Spawner) { let config = Config::default(); let mbox = TlMbox::init(p.IPCC, Irqs, config); + spawner.spawn(run_mm_queue(mbox.mm_subsystem)).unwrap(); + let sys_event = mbox.sys_subsystem.read().await; info!("sys event: {}", sys_event.payload()); + core::mem::drop(sys_event); + let result = mbox.sys_subsystem.shci_c2_mac_802_15_4_init().await; info!("initialized mac: {}", result);