From c6a57d20435d831d36fe1b444c205e487e3dc9e4 Mon Sep 17 00:00:00 2001 From: whitequark Date: Fri, 30 Sep 2016 00:15:20 +0000 Subject: [PATCH] Rust: port mailbox routines. --- artiq/runtime.rs/src/board.rs | 4 +++ artiq/runtime.rs/src/lib.rs | 1 + artiq/runtime.rs/src/mailbox.rs | 51 +++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+) create mode 100644 artiq/runtime.rs/src/mailbox.rs diff --git a/artiq/runtime.rs/src/board.rs b/artiq/runtime.rs/src/board.rs index aa97b1edb..94ad10291 100644 --- a/artiq/runtime.rs/src/board.rs +++ b/artiq/runtime.rs/src/board.rs @@ -3,6 +3,10 @@ use core::{cmp, ptr, str}; include!(concat!(env!("BUILDINC_DIRECTORY"), "/generated/mem.rs")); include!(concat!(env!("BUILDINC_DIRECTORY"), "/generated/csr.rs")); +extern { + pub fn flush_cpu_dcache(); +} + pub fn ident(buf: &mut [u8]) -> &str { unsafe { let len = ptr::read_volatile(csr::IDENTIFIER_MEM_BASE); diff --git a/artiq/runtime.rs/src/lib.rs b/artiq/runtime.rs/src/lib.rs index b0520b629..215f3c7d5 100644 --- a/artiq/runtime.rs/src/lib.rs +++ b/artiq/runtime.rs/src/lib.rs @@ -16,6 +16,7 @@ mod sched; mod config; mod clock; mod rtio_crg; +mod mailbox; mod logger; diff --git a/artiq/runtime.rs/src/mailbox.rs b/artiq/runtime.rs/src/mailbox.rs new file mode 100644 index 000000000..5d13225ce --- /dev/null +++ b/artiq/runtime.rs/src/mailbox.rs @@ -0,0 +1,51 @@ +use core::ptr::{read_volatile, write_volatile}; +use board; + +const MAILBOX: *mut u32 = board::mem::MAILBOX_BASE as *mut u32; +static mut last: u32 = 0; + +pub fn send(data: u32) { + unsafe { + last = data; + write_volatile(MAILBOX, data) + } +} + +pub fn acknowledged() -> bool { + unsafe { + let data = read_volatile(MAILBOX); + data == 0 || data != last + } +} + +pub fn send_and_wait(data: u32) { + send(data); + while !acknowledged() {} +} + +pub fn receive() -> u32 { + unsafe { + let data = read_volatile(MAILBOX); + if data == last { + 0 + } else { + if data != 0 { + board::flush_cpu_dcache() + } + data + } + } +} + +pub fn wait_and_receive() -> u32 { + loop { + let data = receive(); + if data != 0 { + return data + } + } +} + +pub fn acknowledge() { + unsafe { write_volatile(MAILBOX, 0) } +}