From a4895b591a23661af9226609d25b17105490b96a Mon Sep 17 00:00:00 2001 From: mwojcik Date: Mon, 29 May 2023 13:07:06 +0800 Subject: [PATCH] analyzer: fix satellite behavior --- artiq/firmware/runtime/analyzer.rs | 3 ++- artiq/firmware/satman/analyzer.rs | 39 ++++++++++++++++++------------ 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/artiq/firmware/runtime/analyzer.rs b/artiq/firmware/runtime/analyzer.rs index 4b5bd222d..e7c555d76 100644 --- a/artiq/firmware/runtime/analyzer.rs +++ b/artiq/firmware/runtime/analyzer.rs @@ -1,4 +1,6 @@ use io::{Write, Error as IoError}; +#[cfg(has_drtio)] +use alloc::vec::Vec; use board_misoc::{csr, cache}; use sched::{Io, Mutex, TcpListener, TcpStream, Error as SchedError}; use analyzer_proto::*; @@ -42,7 +44,6 @@ fn disarm() { pub mod remote_analyzer { use super::*; use rtio_mgt::drtio; - use alloc::vec::Vec; pub struct RemoteBuffer { pub total_byte_count: u64, diff --git a/artiq/firmware/satman/analyzer.rs b/artiq/firmware/satman/analyzer.rs index 8ad021c2f..e2a43ba22 100644 --- a/artiq/firmware/satman/analyzer.rs +++ b/artiq/firmware/satman/analyzer.rs @@ -1,3 +1,4 @@ +use core::cmp::min; use board_misoc::{csr, cache}; use proto_artiq::drtioaux_proto::ANALYZER_MAX_SIZE; @@ -35,8 +36,9 @@ fn disarm() { pub struct Analyzer { // necessary for keeping track of sent data + data_len: usize, sent_bytes: usize, - data_iter: usize + data_pointer: usize } pub struct Header { @@ -50,48 +52,53 @@ pub struct AnalyzerSliceMeta { pub last: bool } +impl Drop for Analyzer { + fn drop(&mut self) { + disarm(); + } +} + impl Analyzer { pub fn new() -> Analyzer { // create and arm new Analyzer arm(); Analyzer { + data_len: 0, sent_bytes: 0, - data_iter: 0 + data_pointer: 0 } } - fn drop(&mut self) { - disarm(); - } - pub fn get_header(&mut self) -> Header { disarm(); let overflow = unsafe { csr::rtio_analyzer::message_encoder_overflow_read() != 0 }; let total_byte_count = unsafe { csr::rtio_analyzer::dma_byte_count_read() }; let wraparound = total_byte_count >= BUFFER_SIZE as u64; - self.sent_bytes = if wraparound { BUFFER_SIZE } else { total_byte_count as usize }; - self.data_iter = if wraparound { (total_byte_count % BUFFER_SIZE as u64) as usize } else { 0 }; - + self.data_len = if wraparound { BUFFER_SIZE } else { total_byte_count as usize }; + self.data_pointer = if wraparound { (total_byte_count % BUFFER_SIZE as u64) as usize } else { 0 }; + self.sent_bytes = 0; + Header { total_byte_count: total_byte_count, - sent_bytes: self.sent_bytes as u32, + sent_bytes: self.data_len as u32, overflow: overflow } } pub fn get_data(&mut self, data_slice: &mut [u8; ANALYZER_MAX_SIZE]) -> AnalyzerSliceMeta { let data = unsafe { &BUFFER.data[..] }; - let i = self.data_iter; - let len = if i + ANALYZER_MAX_SIZE < self.sent_bytes { ANALYZER_MAX_SIZE } else { self.sent_bytes - i }; - let last = i + len == self.sent_bytes; + let i = (self.data_pointer + self.sent_bytes) % BUFFER_SIZE; + let len = min(ANALYZER_MAX_SIZE, self.data_len - self.sent_bytes); + let last = self.sent_bytes + len == self.data_len; + if i + len >= BUFFER_SIZE { - data_slice[..len].clone_from_slice(&data[i..BUFFER_SIZE]); - data_slice[..len].clone_from_slice(&data[..(i+len) % BUFFER_SIZE]); + data_slice[..(BUFFER_SIZE-i)].clone_from_slice(&data[i..BUFFER_SIZE]); + data_slice[(BUFFER_SIZE-i)..len].clone_from_slice(&data[..(i + len) % BUFFER_SIZE]); } else { data_slice[..len].clone_from_slice(&data[i..i+len]); } - self.data_iter += len; + self.sent_bytes += len; if last { arm();