Add tool (WIP)

This commit is contained in:
Jeremy Soller
2020-02-20 14:20:16 -07:00
parent b0cd6f50e4
commit 08490002b9
10 changed files with 379 additions and 0 deletions

56
tool/src/pmc.rs Normal file
View File

@ -0,0 +1,56 @@
use hwio::{Io, Pio};
pub struct Pmc {
data: Pio<u8>,
cmd: Pio<u8>,
}
impl Pmc {
/// Create a new PMC instance. `base` identifies the data port. The command
/// port will be `base + 4`
pub fn new(base: u16) -> Self {
Self {
data: Pio::new(base),
cmd: Pio::new(base + 4),
}
}
/// Returns true if PMC can be read from
pub unsafe fn can_read(&mut self) -> bool {
self.cmd.read() & 1 == 1
}
/// Returns true if PMC can be written to
pub unsafe fn can_write(&mut self) -> bool {
self.cmd.read() & 2 == 0
}
/// Write a command to the PMC. Returns None if unable to write
pub unsafe fn command(&mut self, data: u8) -> Option<()> {
if self.can_write() {
self.cmd.write(data);
Some(())
} else {
None
}
}
/// Read data from the PMC. Returns None if unable to read
pub unsafe fn read(&mut self) -> Option<u8> {
if self.can_read() {
Some(self.data.read())
} else {
None
}
}
/// Write data to the PMC. Returns false if unable to write
pub unsafe fn write(&mut self, data: u8) -> Option<()> {
if self.can_write() {
self.data.write(data);
Some(())
} else {
None
}
}
}