diff --git a/embassy-stm32/Cargo.toml b/embassy-stm32/Cargo.toml index 0d7e03bf..653f328c 100644 --- a/embassy-stm32/Cargo.toml +++ b/embassy-stm32/Cargo.toml @@ -110,6 +110,16 @@ unstable-pac = [] # Implement embedded-hal-async traits if `nightly` is set as well. unstable-traits = ["embedded-hal-1", "dep:embedded-hal-nb"] +# stm32wb specific +# support for wireless stacks +ble = [] +thread = [] +lld-tests = [] +ble-lld = [] +mac-802_15_4 = [] +zigbee = [] +traces = [] + # Chip-selection features stm32c011d6 = [ "stm32-metapac/stm32c011d6" ] stm32c011f4 = [ "stm32-metapac/stm32c011f4" ] diff --git a/embassy-stm32/src/tl_mbox/ble.rs b/embassy-stm32/src/tl_mbox/ble.rs index 5cc0bb20..0699d186 100644 --- a/embassy-stm32/src/tl_mbox/ble.rs +++ b/embassy-stm32/src/tl_mbox/ble.rs @@ -65,4 +65,15 @@ impl Ble { Ipcc::c1_set_flag_channel(channels::cpu1::IPCC_BLE_CMD_CHANNEL); } + + pub(crate) fn send_acl_data() { + unsafe { + (*(*TL_REF_TABLE.assume_init().ble_table).phci_acl_data_buffer) + .acl_data_serial + .ty = TlPacketType::AclData as u8; + } + + Ipcc::c1_set_flag_channel(channels::Cpu1Channel::HciAclData.into()); + Ipcc::c1_set_tx_channel(channels::Cpu1Channel::HciAclData.into(), true); + } } diff --git a/embassy-stm32/src/tl_mbox/channels.rs b/embassy-stm32/src/tl_mbox/channels.rs index aaa6ce17..94e97230 100644 --- a/embassy-stm32/src/tl_mbox/channels.rs +++ b/embassy-stm32/src/tl_mbox/channels.rs @@ -49,56 +49,104 @@ //! | | //! -pub mod cpu1 { - use crate::ipcc::IpccChannel; +use crate::ipcc::IpccChannel; - // Not used currently but reserved - pub const IPCC_BLE_CMD_CHANNEL: IpccChannel = IpccChannel::Channel1; - // Not used currently but reserved - pub const IPCC_SYSTEM_CMD_RSP_CHANNEL: IpccChannel = IpccChannel::Channel2; - #[allow(dead_code)] // Not used currently but reserved - pub const IPCC_THREAD_OT_CMD_RSP_CHANNEL: IpccChannel = IpccChannel::Channel3; - #[allow(dead_code)] // Not used currently but reserved - pub const IPCC_ZIGBEE_CMD_APPLI_CHANNEL: IpccChannel = IpccChannel::Channel3; - #[allow(dead_code)] // Not used currently but reserved - pub const IPCC_MAC_802_15_4_CMD_RSP_CHANNEL: IpccChannel = IpccChannel::Channel3; - // Not used currently but reserved - pub const IPCC_MM_RELEASE_BUFFER_CHANNEL: IpccChannel = IpccChannel::Channel4; - #[allow(dead_code)] // Not used currently but reserved - pub const IPCC_THREAD_CLI_CMD_CHANNEL: IpccChannel = IpccChannel::Channel5; - #[allow(dead_code)] // Not used currently but reserved - pub const IPCC_LLDTESTS_CLI_CMD_CHANNEL: IpccChannel = IpccChannel::Channel5; - #[allow(dead_code)] // Not used currently but reserved - pub const IPCC_BLE_LLD_CMD_CHANNEL: IpccChannel = IpccChannel::Channel5; - #[allow(dead_code)] // Not used currently but reserved - pub const IPCC_HCI_ACL_DATA_CHANNEL: IpccChannel = IpccChannel::Channel6; +pub enum Cpu1Channel { + BleCmd, + SystemCmdRsp, + #[cfg(feature = "thread")] + ThreadOtCmdRsp, + #[cfg(feature = "zigbee")] + ZigbeeCmdAppli, + MmReleaseBuffer, + #[cfg(feature = "mac-802_15_4")] + Mac802_15_4cmdRsp, + #[cfg(feature = "thread")] + ThreadCliCmd, + #[cfg(feature = "lld-tests")] + LldTestsCliCmd, + #[cfg(feature = "ble-lld")] + BleLldCmd, + HciAclData, } -pub mod cpu2 { - use crate::ipcc::IpccChannel; - - pub const IPCC_BLE_EVENT_CHANNEL: IpccChannel = IpccChannel::Channel1; - pub const IPCC_SYSTEM_EVENT_CHANNEL: IpccChannel = IpccChannel::Channel2; - #[allow(dead_code)] // Not used currently but reserved - pub const IPCC_THREAD_NOTIFICATION_ACK_CHANNEL: IpccChannel = IpccChannel::Channel3; - #[allow(dead_code)] // Not used currently but reserved - pub const IPCC_ZIGBEE_APPLI_NOTIF_ACK_CHANNEL: IpccChannel = IpccChannel::Channel3; - #[allow(dead_code)] // Not used currently but reserved - pub const IPCC_MAC_802_15_4_NOTIFICATION_ACK_CHANNEL: IpccChannel = IpccChannel::Channel3; - #[allow(dead_code)] // Not used currently but reserved - pub const IPCC_LDDTESTS_M0_CMD_CHANNEL: IpccChannel = IpccChannel::Channel3; - #[allow(dead_code)] // Not used currently but reserved - pub const IPCC_BLE_LLD_M0_CMD_CHANNEL: IpccChannel = IpccChannel::Channel3; - #[allow(dead_code)] // Not used currently but reserved - pub const IPCC_TRACES_CHANNEL: IpccChannel = IpccChannel::Channel4; - #[allow(dead_code)] // Not used currently but reserved - pub const IPCC_THREAD_CLI_NOTIFICATION_ACK_CHANNEL: IpccChannel = IpccChannel::Channel5; - #[allow(dead_code)] // Not used currently but reserved - pub const IPCC_LLDTESTS_CLI_RSP_CHANNEL: IpccChannel = IpccChannel::Channel5; - #[allow(dead_code)] // Not used currently but reserved - pub const IPCC_BLE_LLD_CLI_RSP_CHANNEL: IpccChannel = IpccChannel::Channel5; - #[allow(dead_code)] // Not used currently but reserved - pub const IPCC_BLE_LLD_RSP_CHANNEL: IpccChannel = IpccChannel::Channel5; - #[allow(dead_code)] // Not used currently but reserved - pub const IPCC_ZIGBEE_M0_REQUEST_CHANNEL: IpccChannel = IpccChannel::Channel5; +impl From for IpccChannel { + fn from(value: Cpu1Channel) -> Self { + match value { + Cpu1Channel::BleCmd => IpccChannel::Channel1, + Cpu1Channel::SystemCmdRsp => IpccChannel::Channel2, + #[cfg(feature = "thread")] + Cpu1Channel::ThreadOtCmdRsp => IpccChannel::Channel3, + #[cfg(feature = "zigbee")] + Cpu1Channel::ZigbeeCmdAppli => IpccChannel::Channel3, + #[cfg(feature = "mac-802_15_4")] + Cpu1Channel::Mac802_15_4cmdRsp => IpccChannel::Channel3, + Cpu1Channel::MmReleaseBuffer => IpccChannel::Channel4, + #[cfg(feature = "thread")] + Cpu1Channel::ThreadCliCmd => IpccChannel::Channel5, + #[cfg(feature = "lld-tests")] + Cpu1Channel::LldTestsCliCmd => IpccChannel::Channel5, + #[cfg(feature = "ble-lld")] + Cpu1Channel::BleLldCmd => IpccChannel::Channel5, + Cpu1Channel::HciAclData => IpccChannel::Channel6, + } + } +} + +pub enum Cpu2Channel { + BleEvent, + SystemEvent, + #[cfg(feature = "thread")] + ThreadNotifAck, + #[cfg(feature = "zigbee")] + ZigbeeAppliNotifAck, + #[cfg(feature = "mac-802_15_4")] + Mac802_15_4NotifAck, + #[cfg(feature = "lld-tests")] + LldTestsM0Cmd, + #[cfg(feature = "ble-lld")] + BleLldM0Cmd, + #[cfg(feature = "traces")] + Traces, + #[cfg(feature = "thread")] + ThreadCliNotifAck, + #[cfg(feature = "lld-tests")] + LldTestsCliRsp, + #[cfg(feature = "ble-lld")] + BleLldCliRsp, + #[cfg(feature = "ble-lld")] + BleLldRsp, + #[cfg(feature = "zigbee")] + ZigbeeM0Request, +} + +impl From for IpccChannel { + fn from(value: Cpu2Channel) -> Self { + match value { + Cpu2Channel::BleEvent => IpccChannel::Channel1, + Cpu2Channel::SystemEvent => IpccChannel::Channel2, + #[cfg(feature = "thread")] + Cpu2Channel::ThreadNotifAck => IpccChannel::Channel3, + #[cfg(feature = "zigbee")] + Cpu2Channel::ZigbeeAppliNotifAck => IpccChannel::Channel3, + #[cfg(feature = "mac-802_15_4")] + Cpu2Channel::Mac802_15_4NotifAck => IpccChannel::Channel3, + #[cfg(feature = "lld-tests")] + Cpu2Channel::LldTestsM0Cmd => IpccChannel::Channel3, + #[cfg(feature = "ble-lld")] + Cpu2Channel::BleLldM0Cmd => IpccChannel::Channel3, + #[cfg(feature = "traces")] + Cpu2Channel::Traces => IpccChannel::Channel4, + #[cfg(feature = "thread")] + Cpu2Channel::ThreadCliNotifAck => IpccChannel::Channel5, + #[cfg(feature = "lld-tests")] + Cpu2Channel::LldTestsCliRsp => IpccChannel::Channel5, + #[cfg(feature = "ble-lld")] + Cpu2Channel::BleLldCliRsp => IpccChannel::Channel5, + #[cfg(feature = "ble-lld")] + Cpu2Channel::BleLldRsp => IpccChannel::Channel5, + #[cfg(feature = "zigbee")] + Cpu2Channel::ZigbeeM0Request => IpccChannel::Channel5, + } + } } diff --git a/embassy-stm32/src/tl_mbox/mac_802_15_4.rs b/embassy-stm32/src/tl_mbox/mac_802_15_4.rs new file mode 100644 index 00000000..19f95113 --- /dev/null +++ b/embassy-stm32/src/tl_mbox/mac_802_15_4.rs @@ -0,0 +1,82 @@ +use core::mem::MaybeUninit; + +use embassy_futures::block_on; + +use super::cmd::{CmdPacket, CmdSerial}; +use super::consts::TlPacketType; +use super::evt::{EvtBox, EvtPacket}; +use super::unsafe_linked_list::LinkedListNode; +use super::{ + channels, Mac802_15_4Table, EVT_QUEUE, MAC_802_15_4_CMD_BUFFER, MAC_802_15_4_NOTIF_RSP_EVT_BUFFER, TL_CHANNEL, + TL_MAC_802_15_4_TABLE, TL_REF_TABLE, +}; +use crate::ipcc::Ipcc; + +pub struct Mac802_15_4; + +impl Mac802_15_4 { + pub(crate) fn init(ipcc: &mut Ipcc) -> Self { + unsafe { + LinkedListNode::init_head(EVT_QUEUE.as_mut_ptr()); + + TL_MAC_802_15_4_TABLE = MaybeUninit::new(Mac802_15_4Table { + pcmd_rsp_buffer: MAC_802_15_4_CMD_BUFFER.as_mut_ptr().cast(), + pnotack_buffer: MAC_802_15_4_NOTIF_RSP_EVT_BUFFER.as_mut_ptr().cast(), + evt_queue: EVT_QUEUE.as_ptr().cast(), + }); + } + + ipcc.c1_set_rx_channel(channels::Cpu2Channel::Mac802_15_4NotifAck.into(), true); + + Self + } + + pub(crate) fn notif_evt_handler(ipcc: &mut Ipcc) { + unsafe { + let notif_buffer: *mut EvtPacket = (*TL_REF_TABLE.assume_init().mac_802_15_4_table).pnotack_buffer.cast(); + let event = EvtBox::new(notif_buffer); + + block_on(TL_CHANNEL.send(event)); + } + + ipcc.c1_set_rx_channel(channels::Cpu2Channel::Mac802_15_4NotifAck.into(), false); + } + + pub(crate) fn cmd_evt_handler(ipcc: &mut Ipcc) { + unsafe { + let _notif_buffer = (*TL_REF_TABLE.assume_init().mac_802_15_4_table).pcmd_rsp_buffer; + + // NOTE: ST's HAL does nothing with this buffer, ?????? + } + + ipcc.c1_set_tx_channel(channels::Cpu1Channel::Mac802_15_4cmdRsp.into(), false); + } + + pub(crate) fn send_cmd(ipcc: &mut Ipcc, buf: &[u8]) { + unsafe { + let pcmd_buffer: *mut CmdPacket = (*TL_REF_TABLE.assume_init().mac_802_15_4_table).pcmd_rsp_buffer.cast(); + let pcmd_serial: *mut CmdSerial = &mut (*pcmd_buffer).cmd_serial; + let pcmd_serial_buf: *mut u8 = pcmd_serial.cast(); + + core::ptr::copy(buf.as_ptr(), pcmd_serial_buf, buf.len()); + + let cmd_packet: &mut CmdPacket = + &mut *(*TL_REF_TABLE.assume_init().mac_802_15_4_table).pcmd_rsp_buffer.cast(); + cmd_packet.cmd_serial.ty = TlPacketType::OtCmd as u8; + } + + ipcc.c1_set_flag_channel(channels::Cpu1Channel::Mac802_15_4cmdRsp.into()); + ipcc.c1_set_tx_channel(channels::Cpu1Channel::Mac802_15_4cmdRsp.into(), true); + } + + pub(crate) fn send_ack(ipcc: &mut Ipcc) { + // TODO + unsafe { + let packet: &mut CmdPacket = &mut *(*TL_REF_TABLE.assume_init().mac_802_15_4_table).pnotack_buffer.cast(); + packet.cmd_serial.ty = TlPacketType::OtAck as u8; + } + + ipcc.c1_clear_flag_channel(channels::Cpu2Channel::Mac802_15_4NotifAck.into()); + ipcc.c1_set_rx_channel(channels::Cpu2Channel::Mac802_15_4NotifAck.into(), true); + } +} diff --git a/embassy-stm32/src/tl_mbox/mm.rs b/embassy-stm32/src/tl_mbox/mm.rs index 6e0818cf..cb161ce5 100644 --- a/embassy-stm32/src/tl_mbox/mm.rs +++ b/embassy-stm32/src/tl_mbox/mm.rs @@ -11,7 +11,7 @@ use crate::ipcc::Ipcc; pub struct MemoryManager; impl MemoryManager { - pub fn new() -> Self { + pub fn init() -> Self { unsafe { LinkedListNode::init_head(FREE_BUFF_QUEUE.as_mut_ptr()); LinkedListNode::init_head(LOCAL_FREE_BUF_QUEUE.as_mut_ptr()); diff --git a/embassy-stm32/src/tl_mbox/mod.rs b/embassy-stm32/src/tl_mbox/mod.rs index 1c927efa..fe366f6d 100644 --- a/embassy-stm32/src/tl_mbox/mod.rs +++ b/embassy-stm32/src/tl_mbox/mod.rs @@ -5,9 +5,12 @@ use embassy_cortex_m::interrupt::{Interrupt, InterruptExt}; use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex; use embassy_sync::channel::Channel; +#[cfg(feature = "ble")] use self::ble::Ble; use self::cmd::{AclDataPacket, CmdPacket}; use self::evt::{CsEvt, EvtBox}; +#[cfg(feature = "mac-802_15_4")] +use self::mac_802_15_4::Mac802_15_4; use self::mm::MemoryManager; use self::shci::{shci_ble_init, ShciBleInitCmdParam}; use self::sys::Sys; @@ -15,7 +18,6 @@ use self::unsafe_linked_list::LinkedListNode; use crate::interrupt; use crate::ipcc::{Config, Ipcc}; -mod ble; mod channels; mod cmd; mod consts; @@ -25,6 +27,11 @@ mod shci; mod sys; mod unsafe_linked_list; +#[cfg(feature = "ble")] +mod ble; +#[cfg(feature = "mac-802_15_4")] +mod mac_802_15_4; + pub type PacketHeader = LinkedListNode; const TL_PACKET_HEADER_SIZE: usize = core::mem::size_of::(); @@ -212,8 +219,8 @@ struct TracesTable { #[repr(C, packed)] struct Mac802_15_4Table { - pcmd_rsp_buffer: *const u8, - pnotack_buffer: *const u8, + pcmd_rsp_buffer: *mut u8, + pnotack_buffer: *mut u8, evt_queue: *const u8, } @@ -233,6 +240,7 @@ pub struct RefTable { ble_lld_table: *const BleLldTable, } +// -------------------- reference table -------------------- #[link_section = "TL_REF_TABLE"] pub static mut TL_REF_TABLE: MaybeUninit = MaybeUninit::uninit(); @@ -266,38 +274,50 @@ static mut TL_MAC_802_15_4_TABLE: MaybeUninit = MaybeUninit::u #[link_section = "MB_MEM1"] static mut TL_ZIGBEE_TABLE: MaybeUninit = MaybeUninit::uninit(); -#[allow(dead_code)] // Not used currently but reserved +// -------------------- tables -------------------- #[link_section = "MB_MEM1"] static mut FREE_BUFF_QUEUE: MaybeUninit = MaybeUninit::uninit(); -// not in shared RAM -static mut LOCAL_FREE_BUF_QUEUE: MaybeUninit = MaybeUninit::uninit(); - #[link_section = "MB_MEM2"] static mut CS_BUFFER: MaybeUninit<[u8; TL_PACKET_HEADER_SIZE + TL_EVT_HEADER_SIZE + TL_CS_EVT_SIZE]> = MaybeUninit::uninit(); -#[link_section = "MB_MEM2"] +#[link_section = "MB_MEM1"] static mut EVT_QUEUE: MaybeUninit = MaybeUninit::uninit(); #[link_section = "MB_MEM2"] static mut SYSTEM_EVT_QUEUE: MaybeUninit = MaybeUninit::uninit(); -#[link_section = "MB_MEM2"] -static mut SYS_CMD_BUF: MaybeUninit = MaybeUninit::uninit(); +// not in shared RAM +static mut LOCAL_FREE_BUF_QUEUE: MaybeUninit = MaybeUninit::uninit(); +// -------------------- app tables -------------------- #[link_section = "MB_MEM2"] static mut EVT_POOL: MaybeUninit<[u8; POOL_SIZE]> = MaybeUninit::uninit(); +#[link_section = "MB_MEM2"] +static mut SYS_CMD_BUF: MaybeUninit = MaybeUninit::uninit(); + #[link_section = "MB_MEM2"] static mut SYS_SPARE_EVT_BUF: MaybeUninit<[u8; TL_PACKET_HEADER_SIZE + TL_EVT_HEADER_SIZE + 255]> = MaybeUninit::uninit(); +#[cfg(feature = "mac-802_15_4")] +#[link_section = "MB_MEM2"] +static mut MAC_802_15_4_CMD_BUFFER: MaybeUninit = MaybeUninit::uninit(); + +#[cfg(feature = "mac-802_15_4")] +#[link_section = "MB_MEM2"] +static mut MAC_802_15_4_NOTIF_RSP_EVT_BUFFER: MaybeUninit<[u8; TL_PACKET_HEADER_SIZE + TL_EVT_HEADER_SIZE + 255]> = + MaybeUninit::uninit(); + +#[cfg(feature = "ble")] #[link_section = "MB_MEM2"] static mut BLE_SPARE_EVT_BUF: MaybeUninit<[u8; TL_PACKET_HEADER_SIZE + TL_EVT_HEADER_SIZE + 255]> = MaybeUninit::uninit(); -#[link_section = "MB_MEM2"] +#[cfg(feature = "ble")] +#[link_section = "MB_MEM1"] static mut BLE_CMD_BUFFER: MaybeUninit = MaybeUninit::uninit(); #[link_section = "MB_MEM2"] @@ -307,10 +327,14 @@ static mut HCI_ACL_DATA_BUFFER: MaybeUninit<[u8; TL_PACKET_HEADER_SIZE + 5 + 251 // TODO: get a better size, this is a placeholder pub(crate) static TL_CHANNEL: Channel = Channel::new(); -pub struct TlMbox { - _sys: Sys, - _ble: Ble, - _mm: MemoryManager, +pub struct TlMbox; + +pub enum MailboxTarget { + Sys, + #[cfg(feature = "ble")] + Ble, + #[cfg(feature = "mac-802_15_4")] + Mac802_15_4, } impl TlMbox { @@ -356,9 +380,14 @@ impl TlMbox { Ipcc::init(config); - let _sys = Sys::new(); - let _ble = Ble::new(); - let _mm = MemoryManager::new(); + Sys::init(); + MemoryManager::init(); + + #[cfg(feature = "ble")] + Ble::init(); + + #[cfg(feature = "mac-802_15_4")] + Mac802_15_4::init(); // enable interrupts unsafe { crate::interrupt::IPCC_C1_RX::steal() }.unpend(); @@ -367,7 +396,7 @@ impl TlMbox { unsafe { crate::interrupt::IPCC_C1_RX::steal() }.enable(); unsafe { crate::interrupt::IPCC_C1_TX::steal() }.enable(); - TlMbox { _sys, _ble, _mm } + Self } pub fn wireless_fw_info(&self) -> Option { @@ -381,17 +410,30 @@ impl TlMbox { } } + #[cfg(feature = "ble")] pub fn shci_ble_init(&self, param: ShciBleInitCmdParam) { shci_ble_init(param); } - pub fn send_ble_cmd(&self, buf: &[u8]) { - ble::Ble::send_cmd(buf); + pub fn send_cmd(&self, buf: &[u8], target: MailboxTarget) { + match target { + MailboxTarget::Sys => Sys::send_cmd(buf), + #[cfg(feature = "ble")] + MailboxTarget::Ble => Ble::send_cmd(buf), + #[cfg(feature = "mac-802_15_4")] + MailboxTarget::Mac802_15_4 => Mac802_15_4::send_cmd(buf), + } } - // pub fn send_sys_cmd(&self, buf: &[u8]) { - // sys::Sys::send_cmd(buf); - // } + pub fn send_ack(&self, target: MailboxTarget) { + match target { + #[cfg(feature = "ble")] + MailboxTarget::Ble => Ble::send_acl_data(), + #[cfg(feature = "mac-802_15_4")] + MailboxTarget::Mac802_15_4 => Mac802_15_4::send_ack(), + MailboxTarget::Sys => { /* does nothing */ } + } + } pub async fn read(&self) -> EvtBox { TL_CHANNEL.recv().await diff --git a/embassy-stm32/src/tl_mbox/sys.rs b/embassy-stm32/src/tl_mbox/sys.rs index 79e25714..4ffbe8e6 100644 --- a/embassy-stm32/src/tl_mbox/sys.rs +++ b/embassy-stm32/src/tl_mbox/sys.rs @@ -70,7 +70,6 @@ impl Sys { #[allow(dead_code)] pub(crate) fn send_cmd(buf: &[u8]) { unsafe { - // TODO: check this let cmd_buffer = &mut *(*TL_REF_TABLE.assume_init().sys_table).pcmd_buffer; let cmd_serial: *mut CmdSerial = &mut cmd_buffer.cmd_serial; let cmd_serial_buf = cmd_serial.cast();