76 lines
2.6 KiB
Rust
Raw Normal View History

2023-06-08 16:26:47 +01:00
//! Memory manager routines
use core::mem::MaybeUninit;
2023-05-15 10:25:02 +01:00
use super::evt::EvtPacket;
2023-06-08 16:26:47 +01:00
use super::ipcc::Ipcc;
2023-05-20 10:11:29 -05:00
use super::unsafe_linked_list::LinkedListNode;
use super::{
2023-06-08 16:26:47 +01:00
channels, MemManagerTable, BLE_SPARE_EVT_BUF, EVT_POOL, FREE_BUF_QUEUE, LOCAL_FREE_BUF_QUEUE, POOL_SIZE,
2023-05-15 10:25:02 +01:00
SYS_SPARE_EVT_BUF, TL_MEM_MANAGER_TABLE, TL_REF_TABLE,
};
2023-06-08 16:26:47 +01:00
pub(super) struct MemoryManager;
impl MemoryManager {
2023-06-08 16:26:47 +01:00
pub fn new() -> Self {
unsafe {
2023-06-08 16:26:47 +01:00
LinkedListNode::init_head(FREE_BUF_QUEUE.as_mut_ptr());
2023-05-20 10:11:29 -05:00
LinkedListNode::init_head(LOCAL_FREE_BUF_QUEUE.as_mut_ptr());
2023-06-08 16:26:47 +01:00
TL_MEM_MANAGER_TABLE = MaybeUninit::new(MemManagerTable {
spare_ble_buffer: BLE_SPARE_EVT_BUF.as_ptr().cast(),
spare_sys_buffer: SYS_SPARE_EVT_BUF.as_ptr().cast(),
2023-06-08 16:26:47 +01:00
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(),
2023-06-08 16:26:47 +01:00
tracespoolsize: 0,
});
}
2023-05-15 10:25:02 +01:00
2023-06-08 16:26:47 +01:00
MemoryManager
2023-05-15 10:25:02 +01:00
}
2023-05-26 09:56:55 +01:00
pub fn evt_drop(evt: *mut EvtPacket) {
2023-05-15 10:25:02 +01:00
unsafe {
let list_node = evt.cast();
2023-06-08 16:26:47 +01:00
LinkedListNode::insert_tail(LOCAL_FREE_BUF_QUEUE.as_mut_ptr(), list_node);
2023-05-15 10:25:02 +01:00
2023-06-08 16:26:47 +01:00
let channel_is_busy = Ipcc::c1_is_active_flag(channels::cpu1::IPCC_MM_RELEASE_BUFFER_CHANNEL);
2023-05-15 10:25:02 +01:00
2023-06-08 16:26:47 +01:00
// postpone event buffer freeing to IPCC interrupt handler
if channel_is_busy {
Ipcc::c1_set_tx_channel(channels::cpu1::IPCC_MM_RELEASE_BUFFER_CHANNEL, true);
} else {
Self::send_free_buf();
Ipcc::c1_set_flag_channel(channels::cpu1::IPCC_MM_RELEASE_BUFFER_CHANNEL);
}
2023-05-15 10:25:02 +01:00
}
}
2023-06-08 16:26:47 +01:00
/// gives free event buffers back to CPU2 from local buffer queue
pub fn send_free_buf() {
2023-05-15 10:25:02 +01:00
unsafe {
let mut node_ptr = core::ptr::null_mut();
let node_ptr_ptr: *mut _ = &mut node_ptr;
2023-05-20 10:11:29 -05:00
while !LinkedListNode::is_empty(LOCAL_FREE_BUF_QUEUE.as_mut_ptr()) {
LinkedListNode::remove_head(LOCAL_FREE_BUF_QUEUE.as_mut_ptr(), node_ptr_ptr);
LinkedListNode::insert_tail(
2023-05-15 10:25:02 +01:00
(*(*TL_REF_TABLE.as_ptr()).mem_manager_table).pevt_free_buffer_queue,
node_ptr,
);
}
}
}
2023-06-08 16:26:47 +01:00
/// free buffer channel interrupt handler
pub fn free_buf_handler() {
Ipcc::c1_set_tx_channel(channels::cpu1::IPCC_MM_RELEASE_BUFFER_CHANNEL, false);
Self::send_free_buf();
Ipcc::c1_set_flag_channel(channels::cpu1::IPCC_MM_RELEASE_BUFFER_CHANNEL);
}
}