libboard_artiq: tried moving drtio to io::proto

This commit is contained in:
mwojcik 2021-08-27 14:44:54 +02:00
parent e0516eeda9
commit 581f6c6b4e
4 changed files with 37 additions and 22 deletions

View File

@ -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"}

View File

@ -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<T> {
pub enum Error {
GatewareError,
CorruptedPacket,
@ -22,17 +23,17 @@ pub enum Error<T> {
RoutingError,
Protocol(ProtocolError<T>)
Protocol(ProtocolError)
}
impl<T> From<ProtocolError<T>> for Error<T> {
fn from(value: ProtocolError<T>) -> Error<T> {
impl From<ProtocolError> for Error {
fn from(value: ProtocolError) -> Error {
Error::Protocol(value)
}
}
impl<T> From<IoError<T>> for Error<T> {
fn from(value: IoError<T>) -> Error<T> {
impl From<IoError> 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<F, T>(linkno: u8, f: F) -> Result<Option<T>, Error<!>>
where F: FnOnce(&[u8]) -> Result<T, Error<!>>
fn receive<F, T>(linkno: u8, f: F) -> Result<Option<T>, Error>
where F: FnOnce(&[u8]) -> Result<T, Error>
{
let linkidx = linkno as usize;
unsafe {
@ -76,7 +77,7 @@ fn receive<F, T>(linkno: u8, f: F) -> Result<Option<T>, Error<!>>
}
}
pub fn recv(linkno: u8) -> Result<Option<Packet>, Error<!>> {
pub fn recv(linkno: u8) -> Result<Option<Packet>, Error> {
if has_rx_error(linkno) {
return Err(Error::GatewareError)
}
@ -101,7 +102,7 @@ pub fn recv(linkno: u8) -> Result<Option<Packet>, Error<!>> {
}
pub fn recv_timeout(linkno: u8, timeout_ms: Option<u64>,
timer: GlobalTimer) -> Result<Packet, Error<!>>
timer: GlobalTimer) -> Result<Packet, Error>
{
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<u64>,
Err(Error::TimedOut)
}
fn transmit<F>(linkno: u8, f: F) -> Result<(), Error<!>>
where F: FnOnce(&mut [u8]) -> Result<usize, Error<!>>
fn transmit<F>(linkno: u8, f: F) -> Result<(), Error>
where F: FnOnce(&mut [u8]) -> Result<usize, Error>
{
let linkno = linkno as usize;
unsafe {
@ -129,7 +130,7 @@ fn transmit<F>(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);

View File

@ -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<T> {
pub enum Error {
UnknownPacket(u8),
Io(IoError<T>)
Io(IoError)
}
impl<T> From<IoError<T>> for Error<T> {
fn from(value: IoError<T>) -> Error<T> {
impl From<IoError> for Error {
fn from(value: IoError) -> Error {
Error::Io(value)
}
}
/* Why does this not work?!
impl From<Write::WriteError> 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<R>(reader: &mut R) -> Result<Self, Error<R::ReadError>>
where R: io::Read + ?Sized
pub fn read_from<R>(reader: &mut R) -> Result<Self, Error>
where R: Read + ?Sized
{
Ok(match reader.read_u8()? {
0x00 => Packet::EchoRequest,
@ -194,9 +204,10 @@ impl Packet {
})
}
pub fn write_to<W>(&self, writer: &mut W) -> Result<(), IoError<W::WriteError>>
pub fn write_to<W>(&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(())
}
}

View File

@ -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"]