embassy/embassy-stm32/src/tl_mbox/sys.rs

88 lines
3.1 KiB
Rust
Raw Normal View History

use core::mem::MaybeUninit;
2023-05-15 11:25:02 +02:00
use embassy_futures::block_on;
use super::cmd::{CmdPacket, CmdSerial};
use super::consts::TlPacketType;
use super::evt::{CcEvt, EvtBox, EvtSerial};
2023-05-20 17:11:29 +02:00
use super::unsafe_linked_list::LinkedListNode;
2023-05-15 11:25:02 +02:00
use super::{channels, SysTable, SYSTEM_EVT_QUEUE, SYS_CMD_BUF, TL_CHANNEL, TL_REF_TABLE, TL_SYS_TABLE};
use crate::ipcc::Ipcc;
pub struct Sys;
impl Sys {
2023-05-26 10:56:55 +02:00
pub(crate) fn new() -> Self {
unsafe {
2023-05-20 17:11:29 +02:00
LinkedListNode::init_head(SYSTEM_EVT_QUEUE.as_mut_ptr());
TL_SYS_TABLE = MaybeUninit::new(SysTable {
pcmd_buffer: SYS_CMD_BUF.as_mut_ptr(),
sys_queue: SYSTEM_EVT_QUEUE.as_ptr(),
});
}
2023-05-26 10:56:55 +02:00
Ipcc::c1_set_rx_channel(channels::cpu2::IPCC_SYSTEM_EVENT_CHANNEL, true);
Sys
}
2023-05-15 11:25:02 +02:00
2023-05-26 10:56:55 +02:00
pub(crate) fn evt_handler() {
2023-05-15 11:25:02 +02:00
unsafe {
let mut node_ptr = core::ptr::null_mut();
let node_ptr_ptr: *mut _ = &mut node_ptr;
2023-05-20 17:11:29 +02:00
while !LinkedListNode::is_empty(SYSTEM_EVT_QUEUE.as_mut_ptr()) {
LinkedListNode::remove_head(SYSTEM_EVT_QUEUE.as_mut_ptr(), node_ptr_ptr);
2023-05-15 11:25:02 +02:00
let event = node_ptr.cast();
let event = EvtBox::new(event);
// TODO: not really happy about this
block_on(TL_CHANNEL.send(event));
}
}
2023-05-26 10:56:55 +02:00
Ipcc::c1_clear_flag_channel(channels::cpu2::IPCC_SYSTEM_EVENT_CHANNEL);
2023-05-15 11:25:02 +02:00
}
2023-05-26 10:56:55 +02:00
pub(crate) fn cmd_evt_handler() -> CcEvt {
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 {
let cmd: *const CmdPacket = (*TL_SYS_TABLE.as_ptr()).pcmd_buffer;
let cmd_serial: *const CmdSerial = &(*cmd).cmd_serial;
let evt_serial: *const EvtSerial = cmd_serial.cast();
let cc = (*evt_serial).evt.payload.as_ptr().cast();
*cc
}
}
#[allow(dead_code)]
2023-05-26 10:56:55 +02:00
pub(crate) fn send_cmd(buf: &[u8]) {
2023-05-15 11:25:02 +02:00
unsafe {
// TODO: check this
2023-05-15 11:25:02 +02:00
let cmd_buffer = &mut *(*TL_REF_TABLE.assume_init().sys_table).pcmd_buffer;
2023-05-26 10:56:55 +02:00
let cmd_serial: *mut CmdSerial = &mut cmd_buffer.cmd_serial;
2023-05-15 11:25:02 +02:00
let cmd_serial_buf = cmd_serial.cast();
core::ptr::copy(buf.as_ptr(), cmd_serial_buf, buf.len());
2023-05-20 18:29:53 +02:00
let cmd_packet = &mut *(*TL_REF_TABLE.assume_init().sys_table).pcmd_buffer;
2023-05-15 11:25:02 +02:00
cmd_packet.cmd_serial.ty = TlPacketType::SysCmd as u8;
2023-05-26 10:56:55 +02:00
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-05-15 11:25:02 +02:00
}
}
}