From e64f59723c4eadd354efd90eaca70a30e30251b0 Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Sat, 25 Jul 2020 12:12:56 +0800 Subject: [PATCH] dma: use const initializer for manager --- src/runtime/src/kernel/core1.rs | 4 ---- src/runtime/src/kernel/dma.rs | 37 ++++++++++++--------------------- src/runtime/src/main.rs | 1 + 3 files changed, 14 insertions(+), 28 deletions(-) diff --git a/src/runtime/src/kernel/core1.rs b/src/runtime/src/kernel/core1.rs index a592e8e5..83dab17a 100644 --- a/src/runtime/src/kernel/core1.rs +++ b/src/runtime/src/kernel/core1.rs @@ -15,7 +15,6 @@ use crate::eh_artiq; use super::{ api::resolve, rpc::rpc_send_async, - dma::init_dma, CHANNEL_0TO1, CHANNEL_1TO0, KERNEL_CHANNEL_0TO1, KERNEL_CHANNEL_1TO0, KERNEL_IMAGE, @@ -126,9 +125,6 @@ pub fn main_core1() { enable_fpu(); debug!("FPU enabled on Core1"); - init_dma(); - debug!("Init DMA!"); - let mut core1_tx = None; while core1_tx.is_none() { core1_tx = CHANNEL_1TO0.lock().take(); diff --git a/src/runtime/src/kernel/dma.rs b/src/runtime/src/kernel/dma.rs index 51eecb44..c16365b7 100644 --- a/src/runtime/src/kernel/dma.rs +++ b/src/runtime/src/kernel/dma.rs @@ -46,7 +46,7 @@ pub struct Manager { // Copied from https://github.com/m-labs/artiq/blob/master/artiq/firmware/runtime/rtio_dma.rs // basically without modification except removing some warnings. impl Manager { - pub fn new() -> Manager { + pub const fn new() -> Manager { Manager { entries: BTreeMap::new(), recording_name: String::new(), @@ -106,7 +106,7 @@ impl Manager { } -static mut DMA_MANAGER: Option = None; +static mut DMA_MANAGER: Manager = Manager::new(); #[repr(C)] pub struct DmaTrace { @@ -114,16 +114,9 @@ pub struct DmaTrace { address: i32, } -pub fn init_dma() { - unsafe { - DMA_MANAGER = Some(Manager::new()); - } -} - fn dma_record_flush() { unsafe { - let manager = DMA_MANAGER.as_mut().unwrap(); - manager.record_append(&DMA_RECORDER.buffer[..DMA_RECORDER.data_len]); + DMA_MANAGER.record_append(&DMA_RECORDER.buffer[..DMA_RECORDER.data_len]); DMA_RECORDER.data_len = 0; } } @@ -143,8 +136,7 @@ pub extern fn dma_record_start(name: CSlice) { dma_record_output_wide as *const ()).unwrap(); DMA_RECORDER.active = true; - let manager = DMA_MANAGER.as_mut().unwrap(); - manager.record_start(name); + DMA_MANAGER.record_start(name); } } @@ -163,8 +155,7 @@ pub extern fn dma_record_stop(duration: i64) { rtio::output_wide as *const ()).unwrap(); DMA_RECORDER.active = false; - let manager = DMA_MANAGER.as_mut().unwrap(); - manager.record_stop(duration as u64); + DMA_MANAGER.record_stop(duration as u64); } } @@ -238,23 +229,21 @@ pub extern fn dma_record_output_wide(target: i32, words: CSlice) { pub extern fn dma_erase(name: CSlice) { let name = str::from_utf8(name.as_ref()).unwrap(); - let manager = unsafe { - DMA_MANAGER.as_mut().unwrap() + unsafe { + DMA_MANAGER.erase(name); }; - manager.erase(name); } pub extern fn dma_retrieve(name: CSlice) -> DmaTrace { let name = str::from_utf8(name.as_ref()).unwrap(); - let manager = unsafe { - DMA_MANAGER.as_mut().unwrap() + let (trace, duration) = unsafe { + DMA_MANAGER.with_trace(name, |trace, duration| (trace.map(|v| { + dcci_slice(v); + dsb(); + v.as_ptr() + }), duration)) }; - let (trace, duration) = manager.with_trace(name, |trace, duration| (trace.map(|v| { - dcci_slice(v); - dsb(); - v.as_ptr() - }), duration)); match trace { Some(ptr) => Ok(DmaTrace { address: ptr as i32, diff --git a/src/runtime/src/main.rs b/src/runtime/src/main.rs index 6691dee7..c82381eb 100644 --- a/src/runtime/src/main.rs +++ b/src/runtime/src/main.rs @@ -4,6 +4,7 @@ #![feature(alloc_error_handler)] #![feature(panic_info_message)] #![feature(c_variadic)] +#![feature(const_btree_new)] extern crate alloc;