From 1b0384c513704378c49ce96a5e64b3fd97401069 Mon Sep 17 00:00:00 2001 From: whitequark Date: Tue, 15 May 2018 16:35:05 +0000 Subject: [PATCH] firmware: fix satman build. --- artiq/firmware/Cargo.lock | 8 ++- artiq/firmware/libboard_artiq/Cargo.toml | 2 + artiq/firmware/libboard_artiq/drtioaux.rs | 67 ++++++++++------------- artiq/firmware/libboard_artiq/lib.rs | 4 ++ artiq/firmware/satman/main.rs | 2 +- 5 files changed, 42 insertions(+), 41 deletions(-) diff --git a/artiq/firmware/Cargo.lock b/artiq/firmware/Cargo.lock index 12568fcff..e8ae40379 100644 --- a/artiq/firmware/Cargo.lock +++ b/artiq/firmware/Cargo.lock @@ -17,6 +17,8 @@ dependencies = [ "build_misoc 0.0.0", "byteorder 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "crc 1.7.0 (registry+https://github.com/rust-lang/crates.io-index)", + "failure 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", + "failure_derive 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "io 0.0.0", "log 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", "proto_artiq 0.0.0", @@ -176,7 +178,7 @@ dependencies = [ [[package]] name = "log_buffer" version = "1.2.0" -source = "git+https://github.com/whitequark/rust-log_buffer?rev=rust-1.25#ff84e565d9954d5864d60aec12b9e1381505d72a" +source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "logger_artiq" @@ -184,7 +186,7 @@ version = "0.0.0" dependencies = [ "board_misoc 0.0.0", "log 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", - "log_buffer 1.2.0 (git+https://github.com/whitequark/rust-log_buffer?rev=rust-1.25)", + "log_buffer 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -340,7 +342,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum libc 0.2.40 (registry+https://github.com/rust-lang/crates.io-index)" = "6fd41f331ac7c5b8ac259b8bf82c75c0fb2e469bbf37d2becbba9a6a2221965b" "checksum log 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "e19e8d5c34a3e0e2223db8e060f9e8264aeeb5c5fc64a4ee9965c062211c024b" "checksum log 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "89f010e843f2b1a31dbd316b3b8d443758bc634bed37aabade59c686d644e0a2" -"checksum log_buffer 1.2.0 (git+https://github.com/whitequark/rust-log_buffer?rev=rust-1.25)" = "" +"checksum log_buffer 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f033173c9486b7fe97a79c895c0a3483ae395ab6744c985d10078950e2492419" "checksum managed 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "43e2737ecabe4ae36a68061398bf27d2bfd0763f4c3c837a398478459494c4b7" "checksum managed 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "5a31885241e61ba264d780d2e6686e7e59561c947b4581470364eb3e10102d86" "checksum quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e920b65c65f10b2ae65c831a81a073a89edd28c7cce89475bff467ab4167a" diff --git a/artiq/firmware/libboard_artiq/Cargo.toml b/artiq/firmware/libboard_artiq/Cargo.toml index 776392d42..b04990823 100644 --- a/artiq/firmware/libboard_artiq/Cargo.toml +++ b/artiq/firmware/libboard_artiq/Cargo.toml @@ -13,6 +13,8 @@ build_misoc = { path = "../libbuild_misoc" } build_artiq = { path = "../libbuild_artiq" } [dependencies] +failure = { version = "0.1", default-features = false } +failure_derive = { version = "0.1", default-features = false } bitflags = "1.0" byteorder = { version = "1.0", default-features = false } crc = { version = "1.7", default-features = false } diff --git a/artiq/firmware/libboard_artiq/drtioaux.rs b/artiq/firmware/libboard_artiq/drtioaux.rs index 207f7baf1..97a06e3b2 100644 --- a/artiq/firmware/libboard_artiq/drtioaux.rs +++ b/artiq/firmware/libboard_artiq/drtioaux.rs @@ -1,43 +1,36 @@ -use core::{slice, fmt, result}; +use core::slice; use crc; -use io::{Cursor, Error as IoError}; -use io::proto::{ProtoRead, ProtoWrite}; +use io::{ProtoRead, ProtoWrite, Cursor, Error as IoError}; use board_misoc::{csr::DRTIO, mem::DRTIO_AUX, clock}; +use proto_artiq::drtioaux_proto::Error as ProtocolError; pub use proto_artiq::drtioaux_proto::Packet; -pub type Result = result::Result; - -#[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum Error { +// this is parametric over T because there's no impl Fail for !. +#[derive(Fail, Debug)] +pub enum Error { + #[fail(display = "packet CRC failed")] CorruptedPacket, + #[fail(display = "timed out waiting for data")] TimedOut, + #[fail(display = "invalid node number")] NoRoute, + #[fail(display = "gateware reported error")] GatewareError, - Io(IoError) + #[fail(display = "protocol error: {}", _0)] + Protocol(#[cause] ProtocolError) } -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: ProtocolError) -> Error { + Error::Protocol(value) } } -impl From> for Error { - fn from(value: IoError) -> Error { - Error::Io(value) +impl From> for Error { + fn from(value: IoError) -> Error { + Error::Protocol(ProtocolError::Io(value)) } } @@ -63,8 +56,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, Error> + where F: FnOnce(&[u8]) -> Result> { let linkidx = linkno as usize; unsafe { @@ -80,14 +73,14 @@ fn receive(linkno: u8, f: F) -> Result> } } -pub fn recv_link(linkno: u8) -> Result> { +pub fn recv_link(linkno: u8) -> Result, Error> { if has_rx_error(linkno) { return Err(Error::GatewareError) } receive(linkno, |buffer| { if buffer.len() < 8 { - return Err(Error::Io(IoError::UnexpectedEof)) + return Err(IoError::UnexpectedEnd.into()) } let mut reader = Cursor::new(buffer); @@ -104,7 +97,7 @@ pub fn recv_link(linkno: u8) -> Result> { }) } -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 = clock::get_ms() + timeout_ms; while clock::get_ms() < limit { @@ -116,8 +109,8 @@ pub fn recv_timeout_link(linkno: u8, timeout_ms: Option) -> Result Err(Error::TimedOut) } -fn transmit(linkno: u8, f: F) -> Result<()> - where F: FnOnce(&mut [u8]) -> Result +fn transmit(linkno: u8, f: F) -> Result<(), Error> + where F: FnOnce(&mut [u8]) -> Result> { let linkno = linkno as usize; unsafe { @@ -131,7 +124,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<(), Error> { transmit(linkno, |buffer| { let mut writer = Cursor::new(buffer); @@ -152,24 +145,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 > DRTIO.len() { return Err(Error::NoRoute) } Ok(nodeno - 1) } -pub fn recv(nodeno: u8) -> Result> { +pub fn recv(nodeno: u8) -> Result, Error> { 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<(), Error> { let linkno = get_linkno(nodeno)?; send_link(linkno, packet) } diff --git a/artiq/firmware/libboard_artiq/lib.rs b/artiq/firmware/libboard_artiq/lib.rs index 6f22153c7..af8c1336a 100644 --- a/artiq/firmware/libboard_artiq/lib.rs +++ b/artiq/firmware/libboard_artiq/lib.rs @@ -1,6 +1,10 @@ #![feature(asm, lang_items, never_type)] #![no_std] +extern crate failure; +#[cfg(has_drtio)] +#[macro_use] +extern crate failure_derive; #[macro_use] extern crate bitflags; extern crate byteorder; diff --git a/artiq/firmware/satman/main.rs b/artiq/firmware/satman/main.rs index eb478cc6a..6cbadcecb 100644 --- a/artiq/firmware/satman/main.rs +++ b/artiq/firmware/satman/main.rs @@ -26,7 +26,7 @@ fn drtio_reset_phy(reset: bool) { } } -fn process_aux_packet(packet: drtioaux::Packet) -> drtioaux::Result<()> { +fn process_aux_packet(packet: drtioaux::Packet) -> Result<(), drtioaux::Error> { // 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 {