cleaning up RTIO_DEVICE_MAP

This commit is contained in:
Simon Renblad 2024-10-18 18:07:07 +08:00
parent 15f9d1a9e5
commit 7a2a527916

View File

@ -35,6 +35,7 @@ pub mod rtio;
#[rustfmt::skip] #[rustfmt::skip]
#[path = "../../../build/pl.rs"] #[path = "../../../build/pl.rs"]
pub mod pl; pub mod pl;
use core::ptr::addr_of;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -115,6 +116,7 @@ pub async fn report_async_rtio_errors() {
} }
} }
// use interior mutability -> no need for this to be a static mut (no volatile r/ws or FFI)
static mut RTIO_DEVICE_MAP: BTreeMap<u32, String> = BTreeMap::new(); static mut RTIO_DEVICE_MAP: BTreeMap<u32, String> = BTreeMap::new();
fn read_device_map(cfg: &Config) -> BTreeMap<u32, String> { fn read_device_map(cfg: &Config) -> BTreeMap<u32, String> {
@ -146,15 +148,14 @@ fn read_device_map(cfg: &Config) -> BTreeMap<u32, String> {
device_map device_map
} }
fn _resolve_channel_name(channel: u32, device_map: &BTreeMap<u32, String>) -> String {
match device_map.get(&channel) {
Some(val) => val.clone(),
None => String::from("unknown"),
}
}
pub fn resolve_channel_name(channel: u32) -> String { pub fn resolve_channel_name(channel: u32) -> String {
_resolve_channel_name(channel, unsafe { &RTIO_DEVICE_MAP }) unsafe {
let ptr: *const BTreeMap<u32, String> = addr_of!(RTIO_DEVICE_MAP);
match ptr.as_ref().unwrap().get(&channel) {
Some(val) => val.clone(),
None => String::from("unknown"),
}
}
} }
pub fn setup_device_map(cfg: &Config) { pub fn setup_device_map(cfg: &Config) {