From 581f6c6b4e7cdd582a5f6cff4dc226155aa57f48 Mon Sep 17 00:00:00 2001 From: mwojcik Date: Fri, 27 Aug 2021 14:44:54 +0200 Subject: [PATCH] libboard_artiq: tried moving drtio to io::proto --- src/libboard_artiq/Cargo.toml | 1 + src/libboard_artiq/src/drtioaux.rs | 29 ++++++++++++------------ src/libboard_artiq/src/drtioaux_proto.rs | 28 ++++++++++++++++------- src/libboard_artiq/src/lib.rs | 1 + 4 files changed, 37 insertions(+), 22 deletions(-) diff --git a/src/libboard_artiq/Cargo.toml b/src/libboard_artiq/Cargo.toml index b201deb..e6d4a96 100644 --- a/src/libboard_artiq/Cargo.toml +++ b/src/libboard_artiq/Cargo.toml @@ -13,6 +13,7 @@ build_zynq = { path = "../libbuild_zynq" } log = "0.4" log_buffer = { version = "1.2" } crc = { version = "1.7", default-features = false } +core_io = { version = "0.1", features = ["collections"] } io = { path = "../libio", features = ["byteorder"] } libboard_zynq = { git = "https://git.m-labs.hk/M-Labs/zynq-rs.git"} diff --git a/src/libboard_artiq/src/drtioaux.rs b/src/libboard_artiq/src/drtioaux.rs index 576973c..4e3deaa 100644 --- a/src/libboard_artiq/src/drtioaux.rs +++ b/src/libboard_artiq/src/drtioaux.rs @@ -1,8 +1,9 @@ use core::slice; use crc; +use core_io::Error as IoError; -use io::{proto::ProtoRead, proto::ProtoWrite, Cursor, Error as IoError}; +use io::{proto::ProtoRead, proto::ProtoWrite, Cursor}; use mem::mem::DRTIOAUX_MEM; use pl::csr::DRTIOAUX; use drtioaux_proto::Error as ProtocolError; @@ -12,7 +13,7 @@ pub use drtioaux_proto::Packet; // this is parametric over T because there's no impl Fail for !. #[derive(Debug)] -pub enum Error { +pub enum Error { GatewareError, CorruptedPacket, @@ -22,17 +23,17 @@ pub enum Error { RoutingError, - Protocol(ProtocolError) + Protocol(ProtocolError) } -impl From> for Error { - fn from(value: ProtocolError) -> Error { +impl From for Error { + fn from(value: ProtocolError) -> Error { Error::Protocol(value) } } -impl From> for Error { - fn from(value: IoError) -> Error { +impl From for Error { + fn from(value: IoError) -> Error { Error::Protocol(ProtocolError::Io(value)) } } @@ -59,8 +60,8 @@ fn has_rx_error(linkno: u8) -> bool { } } -fn receive(linkno: u8, f: F) -> Result, Error> - where F: FnOnce(&[u8]) -> Result> +fn receive(linkno: u8, f: F) -> Result, Error> + where F: FnOnce(&[u8]) -> Result { let linkidx = linkno as usize; unsafe { @@ -76,7 +77,7 @@ fn receive(linkno: u8, f: F) -> Result, Error> } } -pub fn recv(linkno: u8) -> Result, Error> { +pub fn recv(linkno: u8) -> Result, Error> { if has_rx_error(linkno) { return Err(Error::GatewareError) } @@ -101,7 +102,7 @@ pub fn recv(linkno: u8) -> Result, Error> { } pub fn recv_timeout(linkno: u8, timeout_ms: Option, - timer: GlobalTimer) -> Result> + timer: GlobalTimer) -> Result { let timeout_ms = Milliseconds(timeout_ms.unwrap_or(10)); let limit = timer.get_time() + timeout_ms; @@ -114,8 +115,8 @@ pub fn recv_timeout(linkno: u8, timeout_ms: Option, Err(Error::TimedOut) } -fn transmit(linkno: u8, f: F) -> Result<(), Error> - 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 { @@ -129,7 +130,7 @@ fn transmit(linkno: u8, f: F) -> Result<(), Error> } } -pub fn send(linkno: u8, packet: &Packet) -> Result<(), Error> { +pub fn send(linkno: u8, packet: &Packet) -> Result<(), Error> { transmit(linkno, |buffer| { let mut writer = Cursor::new(buffer); diff --git a/src/libboard_artiq/src/drtioaux_proto.rs b/src/libboard_artiq/src/drtioaux_proto.rs index e6ad582..53c1745 100644 --- a/src/libboard_artiq/src/drtioaux_proto.rs +++ b/src/libboard_artiq/src/drtioaux_proto.rs @@ -1,17 +1,27 @@ -use io::{Read, proto::ProtoRead, Write, proto::ProtoWrite, Error as IoError}; +use core_io::Error as IoError; + +use io::proto::{ProtoWrite as Write, ProtoRead as Read}; #[derive(Debug)] -pub enum Error { +pub enum Error { UnknownPacket(u8), - Io(IoError) + Io(IoError) } -impl From> for Error { - fn from(value: IoError) -> Error { +impl From for Error { + fn from(value: IoError) -> Error { Error::Io(value) } } +/* Why does this not work?! +impl From for Error { + fn from(value: Write::WriteError) -> Error { + Error::Io(value) + } +}*/ + + #[derive(PartialEq, Debug)] pub enum Packet { EchoRequest, @@ -57,8 +67,8 @@ pub enum Packet { } impl Packet { - pub fn read_from(reader: &mut R) -> Result> - where R: io::Read + ?Sized + pub fn read_from(reader: &mut R) -> Result + where R: Read + ?Sized { Ok(match reader.read_u8()? { 0x00 => Packet::EchoRequest, @@ -194,9 +204,10 @@ impl Packet { }) } - pub fn write_to(&self, writer: &mut W) -> Result<(), IoError> + pub fn write_to(&self, writer: &mut W) -> Result<(), IoError> where W: Write + ?Sized { + match *self { Packet::EchoRequest => writer.write_u8(0x00)?, @@ -357,4 +368,5 @@ impl Packet { } Ok(()) } + } diff --git a/src/libboard_artiq/src/lib.rs b/src/libboard_artiq/src/lib.rs index ebb4d3e..0d56f02 100644 --- a/src/libboard_artiq/src/lib.rs +++ b/src/libboard_artiq/src/lib.rs @@ -8,6 +8,7 @@ extern crate libconfig; extern crate libcortex_a9; extern crate log_buffer; extern crate io; +extern crate core_io; // has csr; taken from runtime main #[path = "../../../build/pl.rs"]