2023-06-12 13:27:51 +02:00
|
|
|
use embassy_stm32::ipcc::Ipcc;
|
|
|
|
|
|
|
|
use crate::cmd::{CmdPacket, CmdSerial};
|
|
|
|
use crate::evt::{CcEvt, EvtBox, EvtSerial};
|
|
|
|
use crate::tables::SysTable;
|
|
|
|
use crate::unsafe_linked_list::LinkedListNode;
|
|
|
|
use crate::{channels, EVT_CHANNEL, SYSTEM_EVT_QUEUE, SYS_CMD_BUF, TL_SYS_TABLE};
|
2023-05-02 13:16:48 +02:00
|
|
|
|
|
|
|
pub struct Sys;
|
|
|
|
|
|
|
|
impl Sys {
|
2023-06-12 13:27:51 +02:00
|
|
|
pub fn enable() {
|
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-08 17:26:47 +02:00
|
|
|
})
|
2023-05-02 13:16:48 +02:00
|
|
|
}
|
|
|
|
|
2023-05-26 10:56:55 +02:00
|
|
|
Ipcc::c1_set_rx_channel(channels::cpu2::IPCC_SYSTEM_EVENT_CHANNEL, true);
|
2023-05-15 11:25:02 +02:00
|
|
|
}
|
|
|
|
|
2023-05-27 22:05:23 +02:00
|
|
|
pub fn cmd_evt_handler() -> CcEvt {
|
2023-05-26 10:56:55 +02:00
|
|
|
Ipcc::c1_set_tx_channel(channels::cpu1::IPCC_SYSTEM_CMD_RSP_CHANNEL, false);
|
2023-05-15 11:25:02 +02:00
|
|
|
|
|
|
|
// 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 {
|
2023-06-08 17:26:47 +02:00
|
|
|
let pcmd: *const CmdPacket = (*TL_SYS_TABLE.as_ptr()).pcmd_buffer;
|
|
|
|
let cmd_serial: *const CmdSerial = &(*pcmd).cmdserial;
|
2023-05-15 11:25:02 +02:00
|
|
|
let evt_serial: *const EvtSerial = cmd_serial.cast();
|
2023-06-08 17:26:47 +02:00
|
|
|
let cc: *const CcEvt = (*evt_serial).evt.payload.as_ptr().cast();
|
2023-05-15 11:25:02 +02:00
|
|
|
*cc
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-08 17:26:47 +02:00
|
|
|
pub fn evt_handler() {
|
2023-05-15 11:25:02 +02:00
|
|
|
unsafe {
|
2023-06-08 17:26:47 +02:00
|
|
|
let mut node_ptr = core::ptr::null_mut();
|
|
|
|
let node_ptr_ptr: *mut _ = &mut node_ptr;
|
2023-05-15 11:25:02 +02:00
|
|
|
|
2023-06-08 17:26:47 +02:00
|
|
|
while !LinkedListNode::is_empty(SYSTEM_EVT_QUEUE.as_mut_ptr()) {
|
2023-06-14 00:12:34 +02:00
|
|
|
LinkedListNode::get_next_node(SYSTEM_EVT_QUEUE.as_mut_ptr(), node_ptr_ptr);
|
2023-05-15 11:25:02 +02:00
|
|
|
|
2023-06-08 17:26:47 +02:00
|
|
|
let event = node_ptr.cast();
|
|
|
|
let event = EvtBox::new(event);
|
2023-05-15 11:25:02 +02:00
|
|
|
|
2023-06-12 13:27:51 +02:00
|
|
|
EVT_CHANNEL.try_send(event).unwrap();
|
2023-06-14 00:12:34 +02:00
|
|
|
|
|
|
|
break;
|
2023-06-08 17:26:47 +02:00
|
|
|
}
|
2023-05-15 11:25:02 +02:00
|
|
|
}
|
2023-06-08 17:26:47 +02:00
|
|
|
|
|
|
|
Ipcc::c1_clear_flag_channel(channels::cpu2::IPCC_SYSTEM_EVENT_CHANNEL);
|
2023-05-15 11:25:02 +02:00
|
|
|
}
|
2023-06-08 17:26:47 +02:00
|
|
|
|
2023-06-12 13:27:51 +02:00
|
|
|
pub fn send_cmd() {
|
|
|
|
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);
|
|
|
|
}
|
2023-06-08 17:26:47 +02:00
|
|
|
}
|