From cdf63c5ea0a6dd3ac9e8f5b5f45fdbeb9f2bd17c Mon Sep 17 00:00:00 2001 From: whitequark Date: Mon, 30 Oct 2017 07:46:23 +0000 Subject: [PATCH] runtime: interrogate ethmac error counters and display changes. --- artiq/firmware/runtime/ethmac.rs | 49 ++++++++++++++++++++++++++++++-- artiq/firmware/runtime/lib.rs | 5 ++++ 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/artiq/firmware/runtime/ethmac.rs b/artiq/firmware/runtime/ethmac.rs index c70199623..b2a580a0f 100644 --- a/artiq/firmware/runtime/ethmac.rs +++ b/artiq/firmware/runtime/ethmac.rs @@ -1,8 +1,9 @@ -use core::slice; -use board::{csr, mem}; +use core::{slice, fmt}; use smoltcp::Error; use smoltcp::phy::{DeviceCapabilities, Device}; +use board::{csr, mem}; + const RX_SLOTS: usize = csr::ETHMAC_RX_SLOTS as usize; const TX_SLOTS: usize = csr::ETHMAC_TX_SLOTS as usize; const SLOT_SIZE: usize = csr::ETHMAC_SLOT_SIZE as usize; @@ -84,3 +85,47 @@ impl Drop for TxBuffer { unsafe { csr::ethmac::sram_reader_start_write(1) } } } + +#[derive(Debug, Clone, PartialEq, Eq, Default)] +pub struct EthernetStatistics { + rx_errors: u32, + rx_dropped: u32, +} + +impl EthernetStatistics { + pub fn new() -> Self { + unsafe { + EthernetStatistics { + rx_errors: csr::ethmac::crc_errors_read(), + rx_dropped: csr::ethmac::sram_writer_errors_read(), + } + } + } + + pub fn update(&mut self) -> Option { + let old = self.clone(); + *self = Self::new(); + + let diff = EthernetStatistics { + rx_errors: self.rx_errors.wrapping_sub(old.rx_errors), + rx_dropped: self.rx_dropped.wrapping_sub(old.rx_dropped), + }; + if diff == EthernetStatistics::default() { + None + } else { + Some(diff) + } + } +} + +impl fmt::Display for EthernetStatistics { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + if self.rx_errors > 0 { + write!(f, " rx crc errors: {}", self.rx_errors)? + } + if self.rx_dropped > 0 { + write!(f, " rx dropped: {}", self.rx_dropped)? + } + Ok(()) + } +} diff --git a/artiq/firmware/runtime/lib.rs b/artiq/firmware/runtime/lib.rs index 4539cfab0..d08e01c29 100644 --- a/artiq/firmware/runtime/lib.rs +++ b/artiq/firmware/runtime/lib.rs @@ -153,6 +153,7 @@ fn startup() { } } + let mut net_stats = ethmac::EthernetStatistics::new(); loop { scheduler.run(); @@ -162,6 +163,10 @@ fn startup() { Err(smoltcp::Error::Unrecognized) => (), Err(err) => warn!("network error: {}", err) } + + if let Some(net_stats_diff) = net_stats.update() { + warn!("ethernet mac:{}", net_stats_diff); // mac:{} (sic) + } } }