2023-06-17 22:37:34 +02:00
|
|
|
use core::marker::PhantomData;
|
2023-06-18 15:37:26 +02:00
|
|
|
use core::ptr;
|
2023-06-17 22:37:34 +02:00
|
|
|
|
2023-06-17 19:06:00 +02:00
|
|
|
use crate::cmd::CmdPacket;
|
2023-06-16 04:02:10 +02:00
|
|
|
use crate::consts::TlPacketType;
|
2023-06-18 15:37:26 +02:00
|
|
|
use crate::evt::{CcEvt, EvtBox, EvtPacket};
|
|
|
|
use crate::shci::{SchiCommandStatus, ShciBleInitCmdParam, ShciOpcode};
|
2023-06-17 22:37:34 +02:00
|
|
|
use crate::tables::{SysTable, WirelessFwInfoTable};
|
2023-06-12 13:27:51 +02:00
|
|
|
use crate::unsafe_linked_list::LinkedListNode;
|
2023-06-17 22:37:34 +02:00
|
|
|
use crate::{channels, Ipcc, SYSTEM_EVT_QUEUE, SYS_CMD_BUF, TL_DEVICE_INFO_TABLE, TL_SYS_TABLE};
|
2023-05-02 13:16:48 +02:00
|
|
|
|
2023-06-17 22:37:34 +02:00
|
|
|
pub struct Sys {
|
|
|
|
phantom: PhantomData<Sys>,
|
|
|
|
}
|
2023-05-02 13:16:48 +02:00
|
|
|
|
|
|
|
impl Sys {
|
2023-06-17 19:00:33 +02:00
|
|
|
/// TL_Sys_Init
|
2023-06-17 22:37:34 +02:00
|
|
|
pub(crate) fn new() -> Self {
|
2023-05-02 13:16:48 +02:00
|
|
|
unsafe {
|
2023-05-20 17:11:29 +02:00
|
|
|
LinkedListNode::init_head(SYSTEM_EVT_QUEUE.as_mut_ptr());
|
2023-05-02 13:16:48 +02:00
|
|
|
|
2023-06-14 00:12:34 +02:00
|
|
|
TL_SYS_TABLE.as_mut_ptr().write_volatile(SysTable {
|
2023-05-02 13:16:48 +02:00
|
|
|
pcmd_buffer: SYS_CMD_BUF.as_mut_ptr(),
|
|
|
|
sys_queue: SYSTEM_EVT_QUEUE.as_ptr(),
|
2023-06-17 19:00:33 +02:00
|
|
|
});
|
2023-05-02 13:16:48 +02:00
|
|
|
}
|
2023-06-17 22:37:34 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2023-05-15 11:25:02 +02:00
|
|
|
}
|
|
|
|
|
2023-06-18 15:37:26 +02:00
|
|
|
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);
|
2023-06-17 19:00:33 +02:00
|
|
|
})
|
|
|
|
.await;
|
2023-06-18 15:37:26 +02:00
|
|
|
}
|
2023-06-16 04:02:10 +02:00
|
|
|
|
2023-06-18 15:37:26 +02:00
|
|
|
/// `HW_IPCC_SYS_CmdEvtNot`
|
|
|
|
pub async fn write_and_get_response(&self, opcode: ShciOpcode, payload: &[u8]) -> SchiCommandStatus {
|
|
|
|
self.write(opcode, payload).await;
|
2023-06-17 19:00:33 +02:00
|
|
|
Ipcc::flush(channels::cpu1::IPCC_SYSTEM_CMD_RSP_CHANNEL).await;
|
2023-06-18 15:37:26 +02:00
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-19 01:56:53 +02:00
|
|
|
#[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")]
|
2023-06-18 15:37:26 +02:00
|
|
|
pub async fn shci_c2_ble_init(&self, param: ShciBleInitCmdParam) -> SchiCommandStatus {
|
|
|
|
self.write_and_get_response(ShciOpcode::BleInit, param.payload()).await
|
2023-06-17 19:00:33 +02:00
|
|
|
}
|
2023-06-16 04:02:10 +02:00
|
|
|
|
2023-06-17 19:00:33 +02:00
|
|
|
/// `HW_IPCC_SYS_EvtNot`
|
2023-06-17 22:37:34 +02:00
|
|
|
pub async fn read(&self) -> EvtBox {
|
2023-06-17 19:00:33 +02:00
|
|
|
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
|
2023-06-12 13:27:51 +02:00
|
|
|
}
|
2023-06-08 17:26:47 +02:00
|
|
|
}
|