use core::ptr; use core::sync::atomic::{compiler_fence, Ordering}; use embassy_stm32::ipcc::Ipcc; use crate::cmd::{CmdPacket, CmdSerial, CmdSerialStub}; use crate::consts::TlPacketType; use crate::evt::{CcEvt, EvtBox, EvtSerial}; use crate::shci::{ShciBleInitCmdParam, SCHI_OPCODE_BLE_INIT}; use crate::tables::SysTable; use crate::unsafe_linked_list::LinkedListNode; use crate::{channels, EVT_CHANNEL, SYSTEM_EVT_QUEUE, SYS_CMD_BUF, TL_SYS_TABLE}; pub struct Sys; impl Sys { pub fn enable() { 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(), }) } Ipcc::c1_set_rx_channel(channels::cpu2::IPCC_SYSTEM_EVENT_CHANNEL, true); } pub fn cmd_evt_handler() -> CcEvt { Ipcc::c1_set_tx_channel(channels::cpu1::IPCC_SYSTEM_CMD_RSP_CHANNEL, false); // ST's command response data structure is really convoluted. // // for command response events on SYS channel, the header is missing // and one should: // 1. interpret the content of CMD_BUFFER as CmdPacket // 2. Access CmdPacket's cmdserial field and interpret its content as EvtSerial // 3. Access EvtSerial's evt field (as Evt) and interpret its payload as CcEvt // 4. CcEvt type is the actual SHCI response // 5. profit unsafe { let pcmd: *const CmdPacket = (*TL_SYS_TABLE.as_ptr()).pcmd_buffer; let cmd_serial: *const CmdSerial = &(*pcmd).cmdserial; let evt_serial: *const EvtSerial = cmd_serial.cast(); let cc: *const CcEvt = (*evt_serial).evt.payload.as_ptr().cast(); *cc } } pub fn evt_handler() { unsafe { while !LinkedListNode::is_empty(SYSTEM_EVT_QUEUE.as_mut_ptr()) { let node_ptr = LinkedListNode::remove_head(SYSTEM_EVT_QUEUE.as_mut_ptr()); let event = node_ptr.cast(); let event = EvtBox::new(event); EVT_CHANNEL.try_send(event).unwrap(); } } Ipcc::c1_clear_flag_channel(channels::cpu2::IPCC_SYSTEM_EVENT_CHANNEL); } pub fn shci_ble_init(param: ShciBleInitCmdParam) { debug!("sending SHCI"); Self::send_cmd(SCHI_OPCODE_BLE_INIT, param.payload()); } pub fn send_cmd(opcode: u16, payload: &[u8]) { unsafe { let p_cmd_serial = &mut (*SYS_CMD_BUF.as_mut_ptr()).cmdserial as *mut _ as *mut CmdSerialStub; let p_payload = &mut (*SYS_CMD_BUF.as_mut_ptr()).cmdserial.cmd.payload as *mut _; ptr::write_volatile( p_cmd_serial, CmdSerialStub { ty: TlPacketType::SysCmd as u8, cmd_code: opcode, payload_len: payload.len() as u8, }, ); ptr::copy_nonoverlapping(payload as *const _ as *const u8, p_payload, payload.len()); } compiler_fence(Ordering::SeqCst); Ipcc::c1_set_flag_channel(channels::cpu1::IPCC_SYSTEM_CMD_RSP_CHANNEL); Ipcc::c1_set_tx_channel(channels::cpu1::IPCC_SYSTEM_CMD_RSP_CHANNEL, true); } }