Add timeout macro to simplify timeout logic
This commit is contained in:
parent
85e7225e95
commit
369a695bd9
@ -1,8 +1,11 @@
|
|||||||
use hwio::{Io, Pio};
|
use hwio::{Io, Pio};
|
||||||
|
|
||||||
use crate::error::Error;
|
use crate::{
|
||||||
use crate::super_io::SuperIo;
|
Error,
|
||||||
use crate::timeout::Timeout;
|
SuperIo,
|
||||||
|
Timeout,
|
||||||
|
timeout
|
||||||
|
};
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug)]
|
#[derive(Clone, Copy, Debug)]
|
||||||
#[repr(u8)]
|
#[repr(u8)]
|
||||||
@ -96,27 +99,8 @@ impl<T: Timeout> Ec<T> {
|
|||||||
pub unsafe fn command(&mut self, cmd: EcCmd) -> Result<(), Error> {
|
pub unsafe fn command(&mut self, cmd: EcCmd) -> Result<(), Error> {
|
||||||
self.timeout.reset();
|
self.timeout.reset();
|
||||||
|
|
||||||
while self.timeout.running() {
|
timeout!(self.timeout, self.command_start(cmd))?;
|
||||||
match self.command_start(cmd) {
|
timeout!(self.timeout, self.command_finish())
|
||||||
Ok(()) => break,
|
|
||||||
Err(err) => match err {
|
|
||||||
Error::WouldBlock => (),
|
|
||||||
_ => return Err(err)
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
while self.timeout.running() {
|
|
||||||
match self.command_finish() {
|
|
||||||
Ok(ok) => return Ok(ok),
|
|
||||||
Err(err) => match err {
|
|
||||||
Error::WouldBlock => (),
|
|
||||||
_ => return Err(err)
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Err(Error::Timeout)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Probe for EC
|
/// Probe for EC
|
||||||
|
@ -1,7 +1,16 @@
|
|||||||
#![no_std]
|
#![no_std]
|
||||||
|
|
||||||
pub mod ec;
|
pub use self::ec::Ec;
|
||||||
pub mod error;
|
mod ec;
|
||||||
pub mod pmc;
|
|
||||||
pub mod super_io;
|
pub use self::error::Error;
|
||||||
pub mod timeout;
|
mod error;
|
||||||
|
|
||||||
|
pub use self::pmc::Pmc;
|
||||||
|
mod pmc;
|
||||||
|
|
||||||
|
pub use self::super_io::SuperIo;
|
||||||
|
mod super_io;
|
||||||
|
|
||||||
|
pub use self::timeout::Timeout;
|
||||||
|
mod timeout;
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
use ectool::{
|
use ectool::{
|
||||||
ec::Ec,
|
Ec,
|
||||||
error::Error,
|
Error,
|
||||||
timeout::Timeout,
|
Timeout,
|
||||||
};
|
};
|
||||||
use std::{
|
use std::{
|
||||||
io,
|
io,
|
||||||
|
@ -1,17 +1,25 @@
|
|||||||
use hwio::{Io, Pio};
|
use hwio::{Io, Pio};
|
||||||
|
|
||||||
pub struct Pmc {
|
use crate::{
|
||||||
|
Error,
|
||||||
|
Timeout,
|
||||||
|
timeout
|
||||||
|
};
|
||||||
|
|
||||||
|
pub struct Pmc<T: Timeout> {
|
||||||
data: Pio<u8>,
|
data: Pio<u8>,
|
||||||
cmd: Pio<u8>,
|
cmd: Pio<u8>,
|
||||||
|
timeout: T,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Pmc {
|
impl<T: Timeout> Pmc<T> {
|
||||||
/// Create a new PMC instance. `base` identifies the data port. The command
|
/// Create a new PMC instance. `base` identifies the data port. The command
|
||||||
/// port will be `base + 4`
|
/// port will be `base + 4`
|
||||||
pub fn new(base: u16) -> Self {
|
pub fn new(base: u16, timeout: T) -> Self {
|
||||||
Self {
|
Self {
|
||||||
data: Pio::new(base),
|
data: Pio::new(base),
|
||||||
cmd: Pio::new(base + 4),
|
cmd: Pio::new(base + 4),
|
||||||
|
timeout,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -26,31 +34,39 @@ impl Pmc {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Write a command to the PMC. Returns None if unable to write
|
/// Write a command to the PMC. Returns None if unable to write
|
||||||
pub unsafe fn command(&mut self, data: u8) -> Option<()> {
|
pub unsafe fn command(&mut self, data: u8) -> Result<(), Error> {
|
||||||
if self.can_write() {
|
if self.can_write() {
|
||||||
self.cmd.write(data);
|
self.cmd.write(data);
|
||||||
Some(())
|
Ok(())
|
||||||
} else {
|
} else {
|
||||||
None
|
Err(Error::WouldBlock)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Read data from the PMC. Returns None if unable to read
|
/// Read data from the PMC. Returns None if unable to read
|
||||||
pub unsafe fn read(&mut self) -> Option<u8> {
|
pub unsafe fn read(&mut self) -> Result<u8, Error> {
|
||||||
if self.can_read() {
|
if self.can_read() {
|
||||||
Some(self.data.read())
|
Ok(self.data.read())
|
||||||
} else {
|
} else {
|
||||||
None
|
Err(Error::WouldBlock)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Write data to the PMC. Returns false if unable to write
|
/// Write data to the PMC. Returns false if unable to write
|
||||||
pub unsafe fn write(&mut self, data: u8) -> Option<()> {
|
pub unsafe fn write(&mut self, data: u8) -> Result<(), Error> {
|
||||||
if self.can_write() {
|
if self.can_write() {
|
||||||
self.data.write(data);
|
self.data.write(data);
|
||||||
Some(())
|
Ok(())
|
||||||
} else {
|
} else {
|
||||||
None
|
Err(Error::WouldBlock)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub unsafe fn acpi_read(&mut self, addr: u8) -> Result<u8, Error> {
|
||||||
|
self.timeout.reset();
|
||||||
|
|
||||||
|
timeout!(self.timeout, self.command(0x80))?;
|
||||||
|
timeout!(self.timeout, self.write(addr))?;
|
||||||
|
timeout!(self.timeout, self.read())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,3 +1,26 @@
|
|||||||
|
#[macro_export]
|
||||||
|
macro_rules! timeout {
|
||||||
|
($t:expr, $f:expr) => {{
|
||||||
|
let mut result = Err($crate::Error::Timeout);
|
||||||
|
while $t.running() {
|
||||||
|
match $f {
|
||||||
|
Ok(ok) => {
|
||||||
|
result = Ok(ok);
|
||||||
|
break;
|
||||||
|
},
|
||||||
|
Err(err) => match err {
|
||||||
|
$crate::Error::WouldBlock => (),
|
||||||
|
_ => {
|
||||||
|
result = Err(err);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
result
|
||||||
|
}};
|
||||||
|
}
|
||||||
|
|
||||||
pub trait Timeout {
|
pub trait Timeout {
|
||||||
fn reset(&mut self);
|
fn reset(&mut self);
|
||||||
fn running(&self) -> bool;
|
fn running(&self) -> bool;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user