stm32/wpan: reorg subsystems
This commit is contained in:
79
embassy-stm32-wpan/src/sub/ble.rs
Normal file
79
embassy-stm32-wpan/src/sub/ble.rs
Normal file
@ -0,0 +1,79 @@
|
||||
use core::marker::PhantomData;
|
||||
|
||||
use embassy_stm32::ipcc::Ipcc;
|
||||
use hci::Opcode;
|
||||
|
||||
use crate::channels;
|
||||
use crate::cmd::CmdPacket;
|
||||
use crate::consts::TlPacketType;
|
||||
use crate::evt::EvtBox;
|
||||
use crate::tables::{BleTable, BLE_CMD_BUFFER, CS_BUFFER, EVT_QUEUE, HCI_ACL_DATA_BUFFER, TL_BLE_TABLE};
|
||||
use crate::unsafe_linked_list::LinkedListNode;
|
||||
|
||||
pub struct Ble {
|
||||
phantom: PhantomData<Ble>,
|
||||
}
|
||||
|
||||
impl Ble {
|
||||
pub(crate) fn new() -> Self {
|
||||
unsafe {
|
||||
LinkedListNode::init_head(EVT_QUEUE.as_mut_ptr());
|
||||
|
||||
TL_BLE_TABLE.as_mut_ptr().write_volatile(BleTable {
|
||||
pcmd_buffer: BLE_CMD_BUFFER.as_mut_ptr().cast(),
|
||||
pcs_buffer: CS_BUFFER.as_ptr().cast(),
|
||||
pevt_queue: EVT_QUEUE.as_ptr().cast(),
|
||||
phci_acl_data_buffer: HCI_ACL_DATA_BUFFER.as_mut_ptr().cast(),
|
||||
});
|
||||
}
|
||||
|
||||
Self { phantom: PhantomData }
|
||||
}
|
||||
/// `HW_IPCC_BLE_EvtNot`
|
||||
pub async fn tl_read(&self) -> EvtBox {
|
||||
Ipcc::receive(channels::cpu2::IPCC_BLE_EVENT_CHANNEL, || unsafe {
|
||||
if let Some(node_ptr) = LinkedListNode::remove_head(EVT_QUEUE.as_mut_ptr()) {
|
||||
Some(EvtBox::new(node_ptr.cast()))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
||||
/// `TL_BLE_SendCmd`
|
||||
pub async fn tl_write(&self, opcode: u16, payload: &[u8]) {
|
||||
Ipcc::send(channels::cpu1::IPCC_BLE_CMD_CHANNEL, || unsafe {
|
||||
CmdPacket::write_into(BLE_CMD_BUFFER.as_mut_ptr(), TlPacketType::BleCmd, opcode, payload);
|
||||
})
|
||||
.await;
|
||||
}
|
||||
|
||||
/// `TL_BLE_SendAclData`
|
||||
pub async fn acl_write(&self, handle: u16, payload: &[u8]) {
|
||||
Ipcc::send(channels::cpu1::IPCC_HCI_ACL_DATA_CHANNEL, || unsafe {
|
||||
CmdPacket::write_into(
|
||||
HCI_ACL_DATA_BUFFER.as_mut_ptr() as *mut _,
|
||||
TlPacketType::AclData,
|
||||
handle,
|
||||
payload,
|
||||
);
|
||||
})
|
||||
.await;
|
||||
}
|
||||
}
|
||||
|
||||
pub extern crate stm32wb_hci as hci;
|
||||
|
||||
impl hci::Controller for Ble {
|
||||
async fn controller_write(&mut self, opcode: Opcode, payload: &[u8]) {
|
||||
self.tl_write(opcode.0, payload).await;
|
||||
}
|
||||
|
||||
async fn controller_read_into(&self, buf: &mut [u8]) {
|
||||
let evt_box = self.tl_read().await;
|
||||
let evt_serial = evt_box.serial();
|
||||
|
||||
buf[..evt_serial.len()].copy_from_slice(evt_serial);
|
||||
}
|
||||
}
|
111
embassy-stm32-wpan/src/sub/mac.rs
Normal file
111
embassy-stm32-wpan/src/sub/mac.rs
Normal file
@ -0,0 +1,111 @@
|
||||
use core::future::poll_fn;
|
||||
use core::marker::PhantomData;
|
||||
use core::ptr;
|
||||
use core::sync::atomic::{AtomicBool, Ordering};
|
||||
use core::task::Poll;
|
||||
|
||||
use embassy_futures::poll_once;
|
||||
use embassy_stm32::ipcc::Ipcc;
|
||||
use embassy_sync::waitqueue::AtomicWaker;
|
||||
|
||||
use crate::channels;
|
||||
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,
|
||||
};
|
||||
|
||||
static MAC_WAKER: AtomicWaker = AtomicWaker::new();
|
||||
static MAC_EVT_OUT: AtomicBool = AtomicBool::new(false);
|
||||
|
||||
pub struct Mac {
|
||||
phantom: PhantomData<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 }
|
||||
}
|
||||
|
||||
/// SAFETY: passing a pointer to something other than a managed event packet is UB
|
||||
pub(crate) unsafe fn drop_event_packet(_: *mut EvtPacket) {
|
||||
// Write the ack
|
||||
CmdPacket::write_into(
|
||||
MAC_802_15_4_NOTIF_RSP_EVT_BUFFER.as_mut_ptr() as *mut _,
|
||||
TlPacketType::OtAck,
|
||||
0,
|
||||
&[],
|
||||
);
|
||||
|
||||
// Clear the rx flag
|
||||
let _ = poll_once(Ipcc::receive::<bool>(
|
||||
channels::cpu2::IPCC_MAC_802_15_4_NOTIFICATION_ACK_CHANNEL,
|
||||
|| None,
|
||||
));
|
||||
|
||||
// Allow a new read call
|
||||
MAC_EVT_OUT.store(false, Ordering::SeqCst);
|
||||
MAC_WAKER.wake();
|
||||
}
|
||||
|
||||
/// `HW_IPCC_MAC_802_15_4_EvtNot`
|
||||
///
|
||||
/// This function will stall if the previous `EvtBox` has not been dropped
|
||||
pub async fn read(&self) -> EvtBox {
|
||||
// Wait for the last event box to be dropped
|
||||
poll_fn(|cx| {
|
||||
MAC_WAKER.register(cx.waker());
|
||||
if MAC_EVT_OUT.load(Ordering::SeqCst) {
|
||||
Poll::Pending
|
||||
} else {
|
||||
Poll::Ready(())
|
||||
}
|
||||
})
|
||||
.await;
|
||||
|
||||
// Return a new event box
|
||||
Ipcc::receive(channels::cpu2::IPCC_MAC_802_15_4_NOTIFICATION_ACK_CHANNEL, || unsafe {
|
||||
// The closure is not async, therefore the closure must execute to completion (cannot be dropped)
|
||||
// Therefore, the event box is guaranteed to be cleaned up if it's not leaked
|
||||
MAC_EVT_OUT.store(true, Ordering::SeqCst);
|
||||
|
||||
Some(EvtBox::new(MAC_802_15_4_NOTIF_RSP_EVT_BUFFER.as_mut_ptr() as *mut _))
|
||||
})
|
||||
.await
|
||||
}
|
||||
|
||||
/// `HW_IPCC_MAC_802_15_4_CmdEvtNot`
|
||||
pub async fn write_and_get_response(&self, opcode: u16, payload: &[u8]) -> u8 {
|
||||
self.write(opcode, payload).await;
|
||||
Ipcc::flush(channels::cpu1::IPCC_SYSTEM_CMD_RSP_CHANNEL).await;
|
||||
|
||||
unsafe {
|
||||
let p_event_packet = MAC_802_15_4_CMD_BUFFER.as_ptr() as *const EvtPacket;
|
||||
let p_mac_rsp_evt = &((*p_event_packet).evt_serial.evt.payload) as *const u8;
|
||||
|
||||
ptr::read_volatile(p_mac_rsp_evt)
|
||||
}
|
||||
}
|
||||
|
||||
/// `TL_MAC_802_15_4_SendCmd`
|
||||
pub async fn write(&self, opcode: u16, payload: &[u8]) {
|
||||
Ipcc::send(channels::cpu1::IPCC_MAC_802_15_4_CMD_RSP_CHANNEL, || unsafe {
|
||||
CmdPacket::write_into(
|
||||
MAC_802_15_4_CMD_BUFFER.as_mut_ptr(),
|
||||
TlPacketType::OtCmd,
|
||||
opcode,
|
||||
payload,
|
||||
);
|
||||
})
|
||||
.await;
|
||||
}
|
||||
}
|
79
embassy-stm32-wpan/src/sub/mm.rs
Normal file
79
embassy-stm32-wpan/src/sub/mm.rs
Normal file
@ -0,0 +1,79 @@
|
||||
//! Memory manager routines
|
||||
use core::future::poll_fn;
|
||||
use core::marker::PhantomData;
|
||||
use core::mem::MaybeUninit;
|
||||
use core::task::Poll;
|
||||
|
||||
use cortex_m::interrupt;
|
||||
use embassy_stm32::ipcc::Ipcc;
|
||||
use embassy_sync::waitqueue::AtomicWaker;
|
||||
|
||||
use crate::channels;
|
||||
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,
|
||||
};
|
||||
use crate::unsafe_linked_list::LinkedListNode;
|
||||
|
||||
static MM_WAKER: AtomicWaker = AtomicWaker::new();
|
||||
static mut LOCAL_FREE_BUF_QUEUE: MaybeUninit<LinkedListNode> = MaybeUninit::uninit();
|
||||
|
||||
pub struct MemoryManager {
|
||||
phantom: PhantomData<MemoryManager>,
|
||||
}
|
||||
|
||||
impl MemoryManager {
|
||||
pub(crate) fn new() -> Self {
|
||||
unsafe {
|
||||
LinkedListNode::init_head(FREE_BUF_QUEUE.as_mut_ptr());
|
||||
LinkedListNode::init_head(LOCAL_FREE_BUF_QUEUE.as_mut_ptr());
|
||||
|
||||
TL_MEM_MANAGER_TABLE.as_mut_ptr().write_volatile(MemManagerTable {
|
||||
spare_ble_buffer: BLE_SPARE_EVT_BUF.as_ptr().cast(),
|
||||
spare_sys_buffer: SYS_SPARE_EVT_BUF.as_ptr().cast(),
|
||||
blepool: EVT_POOL.as_ptr().cast(),
|
||||
blepoolsize: POOL_SIZE as u32,
|
||||
pevt_free_buffer_queue: FREE_BUF_QUEUE.as_mut_ptr(),
|
||||
traces_evt_pool: core::ptr::null(),
|
||||
tracespoolsize: 0,
|
||||
});
|
||||
}
|
||||
|
||||
Self { phantom: PhantomData }
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
/// SAFETY: passing a pointer to something other than a managed event packet is UB
|
||||
pub(crate) unsafe fn drop_event_packet(evt: *mut EvtPacket) {
|
||||
interrupt::free(|_| unsafe {
|
||||
LinkedListNode::insert_head(LOCAL_FREE_BUF_QUEUE.as_mut_ptr(), evt as *mut _);
|
||||
});
|
||||
|
||||
MM_WAKER.wake();
|
||||
}
|
||||
|
||||
pub async fn run_queue(&self) {
|
||||
loop {
|
||||
poll_fn(|cx| unsafe {
|
||||
MM_WAKER.register(cx.waker());
|
||||
if LinkedListNode::is_empty(LOCAL_FREE_BUF_QUEUE.as_mut_ptr()) {
|
||||
Poll::Pending
|
||||
} else {
|
||||
Poll::Ready(())
|
||||
}
|
||||
})
|
||||
.await;
|
||||
|
||||
Ipcc::send(channels::cpu1::IPCC_MM_RELEASE_BUFFER_CHANNEL, || {
|
||||
interrupt::free(|_| unsafe {
|
||||
// CS required while moving nodes
|
||||
while let Some(node_ptr) = LinkedListNode::remove_head(LOCAL_FREE_BUF_QUEUE.as_mut_ptr()) {
|
||||
LinkedListNode::insert_head(FREE_BUF_QUEUE.as_mut_ptr(), node_ptr);
|
||||
}
|
||||
})
|
||||
})
|
||||
.await;
|
||||
}
|
||||
}
|
||||
}
|
6
embassy-stm32-wpan/src/sub/mod.rs
Normal file
6
embassy-stm32-wpan/src/sub/mod.rs
Normal file
@ -0,0 +1,6 @@
|
||||
#[cfg(feature = "ble")]
|
||||
pub mod ble;
|
||||
#[cfg(feature = "mac")]
|
||||
pub mod mac;
|
||||
pub mod mm;
|
||||
pub mod sys;
|
86
embassy-stm32-wpan/src/sub/sys.rs
Normal file
86
embassy-stm32-wpan/src/sub/sys.rs
Normal file
@ -0,0 +1,86 @@
|
||||
use core::marker::PhantomData;
|
||||
use core::ptr;
|
||||
|
||||
use crate::cmd::CmdPacket;
|
||||
use crate::consts::TlPacketType;
|
||||
use crate::evt::{CcEvt, EvtBox, EvtPacket};
|
||||
#[allow(unused_imports)]
|
||||
use crate::shci::{SchiCommandStatus, ShciBleInitCmdParam, ShciOpcode};
|
||||
use crate::tables::{SysTable, WirelessFwInfoTable};
|
||||
use crate::unsafe_linked_list::LinkedListNode;
|
||||
use crate::{channels, Ipcc, SYSTEM_EVT_QUEUE, SYS_CMD_BUF, TL_DEVICE_INFO_TABLE, TL_SYS_TABLE};
|
||||
|
||||
pub struct Sys {
|
||||
phantom: PhantomData<Sys>,
|
||||
}
|
||||
|
||||
impl Sys {
|
||||
/// TL_Sys_Init
|
||||
pub(crate) fn new() -> Self {
|
||||
unsafe {
|
||||
LinkedListNode::init_head(SYSTEM_EVT_QUEUE.as_mut_ptr());
|
||||
|
||||
TL_SYS_TABLE.as_mut_ptr().write_volatile(SysTable {
|
||||
pcmd_buffer: SYS_CMD_BUF.as_mut_ptr(),
|
||||
sys_queue: SYSTEM_EVT_QUEUE.as_ptr(),
|
||||
});
|
||||
}
|
||||
|
||||
Self { phantom: PhantomData }
|
||||
}
|
||||
|
||||
/// Returns CPU2 wireless firmware information (if present).
|
||||
pub fn wireless_fw_info(&self) -> Option<WirelessFwInfoTable> {
|
||||
let info = unsafe { TL_DEVICE_INFO_TABLE.as_mut_ptr().read_volatile().wireless_fw_info_table };
|
||||
|
||||
// Zero version indicates that CPU2 wasn't active and didn't fill the information table
|
||||
if info.version != 0 {
|
||||
Some(info)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn write(&self, opcode: ShciOpcode, payload: &[u8]) {
|
||||
Ipcc::send(channels::cpu1::IPCC_SYSTEM_CMD_RSP_CHANNEL, || unsafe {
|
||||
CmdPacket::write_into(SYS_CMD_BUF.as_mut_ptr(), TlPacketType::SysCmd, opcode as u16, payload);
|
||||
})
|
||||
.await;
|
||||
}
|
||||
|
||||
/// `HW_IPCC_SYS_CmdEvtNot`
|
||||
pub async fn write_and_get_response(&self, opcode: ShciOpcode, payload: &[u8]) -> SchiCommandStatus {
|
||||
self.write(opcode, payload).await;
|
||||
Ipcc::flush(channels::cpu1::IPCC_SYSTEM_CMD_RSP_CHANNEL).await;
|
||||
|
||||
unsafe {
|
||||
let p_event_packet = SYS_CMD_BUF.as_ptr() as *const EvtPacket;
|
||||
let p_command_event = &((*p_event_packet).evt_serial.evt.payload) as *const _ as *const CcEvt;
|
||||
let p_payload = &((*p_command_event).payload) as *const u8;
|
||||
|
||||
ptr::read_volatile(p_payload).try_into().unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "mac")]
|
||||
pub async fn shci_c2_mac_802_15_4_init(&self) -> SchiCommandStatus {
|
||||
self.write_and_get_response(ShciOpcode::Mac802_15_4Init, &[]).await
|
||||
}
|
||||
|
||||
#[cfg(feature = "ble")]
|
||||
pub async fn shci_c2_ble_init(&self, param: ShciBleInitCmdParam) -> SchiCommandStatus {
|
||||
self.write_and_get_response(ShciOpcode::BleInit, param.payload()).await
|
||||
}
|
||||
|
||||
/// `HW_IPCC_SYS_EvtNot`
|
||||
pub async fn read(&self) -> EvtBox {
|
||||
Ipcc::receive(channels::cpu2::IPCC_SYSTEM_EVENT_CHANNEL, || unsafe {
|
||||
if let Some(node_ptr) = LinkedListNode::remove_head(SYSTEM_EVT_QUEUE.as_mut_ptr()) {
|
||||
Some(EvtBox::new(node_ptr.cast()))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.await
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user