runtime: share rtio_log format function

This commit is contained in:
Sebastien Bourdeauducq 2020-08-04 13:27:18 +08:00
parent 1e20259c36
commit 8337c9173e
3 changed files with 22 additions and 33 deletions

View File

@ -1,11 +1,29 @@
use core::ffi::VaList;
use core::ptr;
use libc::{c_char, c_int, size_t};
use libm; use libm;
use alloc::vec;
use crate::eh_artiq; use crate::eh_artiq;
use crate::rtio; use crate::rtio;
use super::rpc::{rpc_send, rpc_send_async, rpc_recv}; use super::rpc::{rpc_send, rpc_send_async, rpc_recv};
use super::dma; use super::dma;
use super::cache; use super::cache;
extern "C" {
fn vsnprintf_(buffer: *mut c_char, count: size_t, format: *const c_char, va: VaList) -> c_int;
}
pub unsafe extern fn rtio_log(fmt: *const c_char, mut args: ...) {
let size = vsnprintf_(ptr::null_mut(), 0, fmt, args.as_va_list()) as usize;
let mut buf = vec![0; size + 1];
vsnprintf_(buf.as_mut_ptr(), size + 1, fmt, args.as_va_list());
rtio::write_log(buf.as_slice());
}
macro_rules! api { macro_rules! api {
($i:ident) => ({ ($i:ident) => ({
extern { static $i: u8; } extern { static $i: u8; }
@ -50,7 +68,7 @@ pub fn resolve(required: &[u8]) -> Option<u32> {
api!(rtio_input_timestamp = rtio::input_timestamp), api!(rtio_input_timestamp = rtio::input_timestamp),
api!(rtio_input_data = rtio::input_data), api!(rtio_input_data = rtio::input_data),
api!(rtio_input_timestamped_data = rtio::input_timestamped_data), api!(rtio_input_timestamped_data = rtio::input_timestamped_data),
api!(rtio_log = rtio::log), api!(rtio_log = rtio_log),
// rtio dma // rtio dma
api!(dma_record_start = dma::dma_record_start), api!(dma_record_start = dma::dma_record_start),

View File

@ -1,8 +1,4 @@
use core::ptr::{self, read_volatile, write_volatile};
use core::ffi::VaList;
use alloc::vec;
use cslice::CSlice; use cslice::CSlice;
use libc::{c_char, c_int, size_t};
use crate::artiq_raise; use crate::artiq_raise;
@ -65,17 +61,6 @@ pub extern fn input_timestamped_data(timeout: i64, channel: i32) -> TimestampedD
unimplemented!(); unimplemented!();
} }
extern "C" { pub fn write_log(data: &[i8]) {
fn vsnprintf_(buffer: *mut c_char, count: size_t, format: *const c_char, va: VaList) -> c_int;
}
fn write_rtio_log(data: &[i8]) {
unimplemented!(); unimplemented!();
} }
pub unsafe extern fn log(fmt: *const c_char, mut args: ...) {
let size = vsnprintf_(ptr::null_mut(), 0, fmt, args.as_va_list()) as usize;
let mut buf = vec![0; size + 1];
vsnprintf_(buf.as_mut_ptr(), size + 1, fmt, args.as_va_list());
write_rtio_log(buf.as_slice());
}

View File

@ -1,8 +1,5 @@
use core::ptr::{self, read_volatile, write_volatile}; use core::ptr::{read_volatile, write_volatile};
use core::ffi::VaList;
use alloc::vec;
use cslice::CSlice; use cslice::CSlice;
use libc::{c_char, c_int, size_t};
use crate::artiq_raise; use crate::artiq_raise;
@ -203,11 +200,7 @@ pub extern fn input_timestamped_data(timeout: i64, channel: i32) -> TimestampedD
} }
} }
extern "C" { pub fn write_log(data: &[i8]) {
fn vsnprintf_(buffer: *mut c_char, count: size_t, format: *const c_char, va: VaList) -> c_int;
}
fn write_rtio_log(data: &[i8]) {
unsafe { unsafe {
csr::rtio::target_write(csr::CONFIG_RTIO_LOG_CHANNEL << 8); csr::rtio::target_write(csr::CONFIG_RTIO_LOG_CHANNEL << 8);
@ -226,10 +219,3 @@ fn write_rtio_log(data: &[i8]) {
} }
} }
} }
pub unsafe extern fn log(fmt: *const c_char, mut args: ...) {
let size = vsnprintf_(ptr::null_mut(), 0, fmt, args.as_va_list()) as usize;
let mut buf = vec![0; size + 1];
vsnprintf_(buf.as_mut_ptr(), size + 1, fmt, args.as_va_list());
write_rtio_log(buf.as_slice());
}