diff --git a/artiq/firmware/libdrtioaux/hw.rs b/artiq/firmware/libdrtioaux/hw.rs index ea68c24e1..bd4f609b5 100644 --- a/artiq/firmware/libdrtioaux/hw.rs +++ b/artiq/firmware/libdrtioaux/hw.rs @@ -1,8 +1,42 @@ +use core::fmt; +use io::{Cursor, Error as IoError}; +use io::proto::ProtoRead; + use super::*; -use {Error, Result}; -use io::Cursor; -use io::proto::ProtoRead; +pub type Result = result::Result; + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Error { + CorruptedPacket, + TimedOut, + NoRoute, + GatewareError, + Io(IoError) +} + +impl fmt::Display for Error { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + &Error::CorruptedPacket => + write!(f, "packet CRC failed"), + &Error::TimedOut => + write!(f, "timed out waiting for data"), + &Error::NoRoute => + write!(f, "invalid node number"), + &Error::GatewareError => + write!(f, "gateware reported error"), + &Error::Io(ref io) => + write!(f, "I/O error ({})", io) + } + } +} + +impl From> for Error { + fn from(value: IoError) -> Error { + Error::Io(value) + } +} pub fn reset(linkno: u8) { let linkno = linkno as usize; @@ -26,8 +60,8 @@ fn has_rx_error(linkno: u8) -> bool { } } -fn receive(linkno: u8, f: F) -> Result, !> - where F: FnOnce(&[u8]) -> Result +fn receive(linkno: u8, f: F) -> Result> + where F: FnOnce(&[u8]) -> Result { let linkidx = linkno as usize; unsafe { @@ -44,7 +78,7 @@ fn receive(linkno: u8, f: F) -> Result, !> } } -pub fn recv_link(linkno: u8) -> Result, !> { +pub fn recv_link(linkno: u8) -> Result> { if has_rx_error(linkno) { return Err(Error::GatewareError) } @@ -64,11 +98,11 @@ pub fn recv_link(linkno: u8) -> Result, !> { } reader.set_position(0); - Packet::read_from(&mut reader) + Ok(Packet::read_from(&mut reader)?) }) } -pub fn recv_timeout_link(linkno: u8, timeout_ms: Option) -> Result { +pub fn recv_timeout_link(linkno: u8, timeout_ms: Option) -> Result { let timeout_ms = timeout_ms.unwrap_or(10); let limit = board::clock::get_ms() + timeout_ms; while board::clock::get_ms() < limit { @@ -80,8 +114,8 @@ pub fn recv_timeout_link(linkno: u8, timeout_ms: Option) -> Result(linkno: u8, f: F) -> Result<(), !> - where F: FnOnce(&mut [u8]) -> Result +fn transmit(linkno: u8, f: F) -> Result<()> + where F: FnOnce(&mut [u8]) -> Result { let linkno = linkno as usize; unsafe { @@ -95,7 +129,7 @@ fn transmit(linkno: u8, f: F) -> Result<(), !> } } -pub fn send_link(linkno: u8, packet: &Packet) -> Result<(), !> { +pub fn send_link(linkno: u8, packet: &Packet) -> Result<()> { transmit(linkno, |buffer| { let mut writer = Cursor::new(buffer); @@ -116,24 +150,24 @@ pub fn send_link(linkno: u8, packet: &Packet) -> Result<(), !> { } // TODO: routing -fn get_linkno(nodeno: u8) -> Result { +fn get_linkno(nodeno: u8) -> Result { if nodeno == 0 || nodeno as usize > board::csr::DRTIO.len() { return Err(Error::NoRoute) } Ok(nodeno - 1) } -pub fn recv(nodeno: u8) -> Result, !> { +pub fn recv(nodeno: u8) -> Result> { let linkno = get_linkno(nodeno)?; recv_link(linkno) } -pub fn recv_timeout(nodeno: u8, timeout_ms: Option) -> Result { +pub fn recv_timeout(nodeno: u8, timeout_ms: Option) -> Result { let linkno = get_linkno(nodeno)?; recv_timeout_link(linkno, timeout_ms) } -pub fn send(nodeno: u8, packet: &Packet) -> Result<(), !> { +pub fn send(nodeno: u8, packet: &Packet) -> Result<()> { let linkno = get_linkno(nodeno)?; send_link(linkno, packet) } diff --git a/artiq/firmware/libdrtioaux/lib.rs b/artiq/firmware/libdrtioaux/lib.rs index 97b7f46af..9c26144ef 100644 --- a/artiq/firmware/libdrtioaux/lib.rs +++ b/artiq/firmware/libdrtioaux/lib.rs @@ -6,59 +6,9 @@ extern crate crc; extern crate io; extern crate board; -use core::slice; -use core::result; -use core::fmt; - -use io::{Read, Write, Error as IoError}; +use io::{Read, Write, Error, Result}; use io::proto::{ProtoRead, ProtoWrite}; -pub type Result = result::Result>; - -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum Error { - UnknownPacket(u8), - CorruptedPacket, - TimedOut, - NoRoute, - GatewareError, - Io(IoError), - Other(T) -} - -impl fmt::Display for Error { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match self { - &Error::UnknownPacket(ty) => - write!(f, "unknown packet type {:#02x}", ty), - &Error::CorruptedPacket => - write!(f, "packet CRC failed"), - &Error::TimedOut => - write!(f, "timed out waiting for data"), - &Error::NoRoute => - write!(f, "invalid node number"), - &Error::GatewareError => - write!(f, "gateware reported error"), - &Error::Io(ref io) => - write!(f, "I/O error ({})", io), - &Error::Other(ref err) => - write!(f, "{}", err) - } - } -} - -impl From for Error { - fn from(value: T) -> Error { - Error::Other(value) - } -} - -impl From> for Error { - fn from(value: IoError) -> Error { - Error::Io(value) - } -} - #[derive(Debug)] pub enum Packet { EchoRequest, @@ -188,7 +138,7 @@ impl Packet { succeeded: reader.read_bool()? }, - ty => return Err(Error::UnknownPacket(ty)) + _ => return Err(Error::Unrecognized) }) } diff --git a/artiq/firmware/libio/lib.rs b/artiq/firmware/libio/lib.rs index ecfe51341..5050c8934 100644 --- a/artiq/firmware/libio/lib.rs +++ b/artiq/firmware/libio/lib.rs @@ -19,6 +19,7 @@ pub type Result = result::Result>; #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum Error { UnexpectedEof, + Unrecognized, Other(T) } @@ -27,6 +28,8 @@ impl fmt::Display for Error { match self { &Error::UnexpectedEof => write!(f, "unexpected end of stream"), + &Error::Unrecognized => + write!(f, "unrecognized input"), &Error::Other(ref err) => write!(f, "{}", err) } diff --git a/artiq/firmware/satman/main.rs b/artiq/firmware/satman/main.rs index 3c064d266..9afe0ff4c 100644 --- a/artiq/firmware/satman/main.rs +++ b/artiq/firmware/satman/main.rs @@ -27,7 +27,7 @@ fn drtio_reset_phy(reset: bool) { } } -fn process_aux_packet(packet: drtioaux::Packet) -> drtioaux::Result<(), !> { +fn process_aux_packet(packet: drtioaux::Packet) -> drtioaux::hw::Result<()> { // In the code below, *_chan_sel_write takes an u8 if there are fewer than 256 channels, // and u16 otherwise; hence the `as _` conversion. match packet {