Compare commits
2 commits
821f538f6d
...
75e0a8792b
Author | SHA1 | Date | |
---|---|---|---|
75e0a8792b | |||
ef5afdee93 |
7 changed files with 102 additions and 51 deletions
|
@ -38,7 +38,9 @@ pub enum Operation {
|
||||||
},
|
},
|
||||||
/// Write a pattern to the whole address space
|
/// Write a pattern to the whole address space
|
||||||
WritePattern {
|
WritePattern {
|
||||||
// TODO
|
/// Bytes to write to every address
|
||||||
|
#[arg(value_parser = clap_num::maybe_hex::<u8>)]
|
||||||
|
bytes: Vec<u8>,
|
||||||
},
|
},
|
||||||
/// Write a value to a specific address
|
/// Write a value to a specific address
|
||||||
Write {
|
Write {
|
||||||
|
|
|
@ -4,6 +4,7 @@ use smokepatio::ec::EmbeddedController;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let cli = args::Args::load();
|
let cli = args::Args::load();
|
||||||
|
#[cfg(debug_assertions)]
|
||||||
println!("args: {:?}", cli);
|
println!("args: {:?}", cli);
|
||||||
|
|
||||||
let mut ec = EmbeddedController::new(cli.data_address, cli.cmd_address);
|
let mut ec = EmbeddedController::new(cli.data_address, cli.cmd_address);
|
||||||
|
@ -15,13 +16,44 @@ fn main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
match cli.op {
|
match cli.op {
|
||||||
args::Operation::ReadAll => todo!(),
|
args::Operation::ReadAll => {
|
||||||
|
let (bytes_per_line, range) = if cli.extended {
|
||||||
|
(16, 0..=u16::MAX)
|
||||||
|
} else {
|
||||||
|
(8, 0..=(u8::MAX as u16))
|
||||||
|
};
|
||||||
|
for addr in range {
|
||||||
|
let addr_mod = addr % bytes_per_line;
|
||||||
|
if addr_mod == 0 {
|
||||||
|
print!("{:#02x}: ", addr);
|
||||||
|
}
|
||||||
|
print!("{:#02x} ", ec.get(addr));
|
||||||
|
if addr_mod == (bytes_per_line - 1) {
|
||||||
|
println!("");
|
||||||
|
} else {
|
||||||
|
print!(" ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
println!("");
|
||||||
|
}
|
||||||
args::Operation::Read { address } => {
|
args::Operation::Read { address } => {
|
||||||
println!("{:#02x}", ec.get(address));
|
println!("{:#02x}", ec.get(address));
|
||||||
},
|
}
|
||||||
args::Operation::WritePattern { } => todo!(),
|
args::Operation::WritePattern { bytes } => {
|
||||||
|
let range = if cli.extended {
|
||||||
|
0..=u16::MAX
|
||||||
|
} else {
|
||||||
|
0..=(u8::MAX as u16)
|
||||||
|
};
|
||||||
|
for addr in range {
|
||||||
|
println!("{:#02x}", addr);
|
||||||
|
for b in bytes.iter() {
|
||||||
|
ec.set(addr, *b);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
args::Operation::Write { address, value } => {
|
args::Operation::Write { address, value } => {
|
||||||
ec.set(address, value);
|
ec.set(address, value);
|
||||||
},
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -115,24 +115,30 @@ impl embedded_io::Write for EmbeddedController {
|
||||||
fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
|
fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
|
||||||
let size = self.rw_size as u8;
|
let size = self.rw_size as u8;
|
||||||
match self.rw_size {
|
match self.rw_size {
|
||||||
ValueSize::Byte => for &b in buf {
|
ValueSize::Byte => {
|
||||||
|
for &b in buf {
|
||||||
self.wait_for_write_ready();
|
self.wait_for_write_ready();
|
||||||
self.cmd_requested = 1; // don't re-request, but still wait
|
self.cmd_requested = 1; // don't re-request, but still wait
|
||||||
super::ports::outb(self.data_address, b);
|
super::ports::outb(self.data_address, b);
|
||||||
},
|
}
|
||||||
ValueSize::Word => for i in (0..buf.len()).step_by(size as usize) {
|
}
|
||||||
|
ValueSize::Word => {
|
||||||
|
for i in (0..buf.len()).step_by(size as usize) {
|
||||||
self.wait_for_write_ready();
|
self.wait_for_write_ready();
|
||||||
self.cmd_requested = 1; // don't re-request, but still wait
|
self.cmd_requested = 1; // don't re-request, but still wait
|
||||||
let w = [buf[i], buf[i+1]];
|
let w = [buf[i], buf[i + 1]];
|
||||||
super::ports::outw(self.data_address, u16::from_ne_bytes(w));
|
super::ports::outw(self.data_address, u16::from_ne_bytes(w));
|
||||||
},
|
}
|
||||||
ValueSize::Long => for i in (0..buf.len()).step_by(size as usize) {
|
}
|
||||||
|
ValueSize::Long => {
|
||||||
|
for i in (0..buf.len()).step_by(size as usize) {
|
||||||
self.wait_for_write_ready();
|
self.wait_for_write_ready();
|
||||||
self.cmd_requested = 1; // don't re-request, but still wait
|
self.cmd_requested = 1; // don't re-request, but still wait
|
||||||
let l = [buf[i], buf[i+1], buf[i+2], buf[i+3]];
|
let l = [buf[i], buf[i + 1], buf[i + 2], buf[i + 3]];
|
||||||
super::ports::outl(self.data_address, u32::from_ne_bytes(l));
|
super::ports::outl(self.data_address, u32::from_ne_bytes(l));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
self.cmd_requested = 0;
|
self.cmd_requested = 0;
|
||||||
Ok(buf.len())
|
Ok(buf.len())
|
||||||
}
|
}
|
||||||
|
@ -145,7 +151,8 @@ impl embedded_io::Write for EmbeddedController {
|
||||||
impl embedded_io::WriteReady for EmbeddedController {
|
impl embedded_io::WriteReady for EmbeddedController {
|
||||||
fn write_ready(&mut self) -> Result<bool, Self::Error> {
|
fn write_ready(&mut self) -> Result<bool, Self::Error> {
|
||||||
self.request_if_not_already();
|
self.request_if_not_already();
|
||||||
let ready = super::ports::inb(self.cmd_address) & 2 == 0 || self.cmd_requested == MAX_WAIT_CHECKS;
|
let ready =
|
||||||
|
super::ports::inb(self.cmd_address) & 2 == 0 || self.cmd_requested == MAX_WAIT_CHECKS;
|
||||||
if ready {
|
if ready {
|
||||||
self.cmd_requested = 0;
|
self.cmd_requested = 0;
|
||||||
}
|
}
|
||||||
|
@ -157,20 +164,25 @@ impl embedded_io::Read for EmbeddedController {
|
||||||
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
|
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
|
||||||
let size = self.rw_size as u8;
|
let size = self.rw_size as u8;
|
||||||
match self.rw_size {
|
match self.rw_size {
|
||||||
ValueSize::Byte => for b in buf.iter_mut() {
|
ValueSize::Byte => {
|
||||||
|
for b in buf.iter_mut() {
|
||||||
self.wait_for_read_ready();
|
self.wait_for_read_ready();
|
||||||
self.cmd_requested = 1; // don't re-request, but still wait
|
self.cmd_requested = 1; // don't re-request, but still wait
|
||||||
*b = super::ports::inb(self.data_address);
|
*b = super::ports::inb(self.data_address);
|
||||||
},
|
}
|
||||||
ValueSize::Word => for i in (0..buf.len()).step_by(size as usize) {
|
}
|
||||||
|
ValueSize::Word => {
|
||||||
|
for i in (0..buf.len()).step_by(size as usize) {
|
||||||
self.wait_for_read_ready();
|
self.wait_for_read_ready();
|
||||||
self.cmd_requested = 1; // don't re-request, but still wait
|
self.cmd_requested = 1; // don't re-request, but still wait
|
||||||
let w = super::ports::inw(self.data_address).to_ne_bytes();
|
let w = super::ports::inw(self.data_address).to_ne_bytes();
|
||||||
for j in 0..(size as usize) {
|
for j in 0..(size as usize) {
|
||||||
buf[i + j] = w[j];
|
buf[i + j] = w[j];
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
ValueSize::Long => for i in (0..buf.len()).step_by(size as usize) {
|
}
|
||||||
|
ValueSize::Long => {
|
||||||
|
for i in (0..buf.len()).step_by(size as usize) {
|
||||||
self.wait_for_read_ready();
|
self.wait_for_read_ready();
|
||||||
self.cmd_requested = 1; // don't re-request, but still wait
|
self.cmd_requested = 1; // don't re-request, but still wait
|
||||||
let l = super::ports::inl(self.data_address).to_ne_bytes();
|
let l = super::ports::inl(self.data_address).to_ne_bytes();
|
||||||
|
@ -179,6 +191,7 @@ impl embedded_io::Read for EmbeddedController {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
self.cmd_requested = 0;
|
self.cmd_requested = 0;
|
||||||
Ok(buf.len())
|
Ok(buf.len())
|
||||||
}
|
}
|
||||||
|
@ -187,7 +200,8 @@ impl embedded_io::Read for EmbeddedController {
|
||||||
impl embedded_io::ReadReady for EmbeddedController {
|
impl embedded_io::ReadReady for EmbeddedController {
|
||||||
fn read_ready(&mut self) -> Result<bool, Self::Error> {
|
fn read_ready(&mut self) -> Result<bool, Self::Error> {
|
||||||
self.request_if_not_already();
|
self.request_if_not_already();
|
||||||
let ready = super::ports::inb(self.cmd_address) & 1 != 0 || self.cmd_requested == MAX_WAIT_CHECKS;
|
let ready =
|
||||||
|
super::ports::inb(self.cmd_address) & 1 != 0 || self.cmd_requested == MAX_WAIT_CHECKS;
|
||||||
if ready {
|
if ready {
|
||||||
self.cmd_requested = 0;
|
self.cmd_requested = 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,12 +8,12 @@
|
||||||
mod controller;
|
mod controller;
|
||||||
mod ports;
|
mod ports;
|
||||||
pub mod unnamed_power;
|
pub mod unnamed_power;
|
||||||
pub use controller::{EmbeddedController, ControllerGet, GetValue, ControllerSet, SetValue};
|
pub use controller::{ControllerGet, ControllerSet, EmbeddedController, GetValue, SetValue};
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use std::io::Error;
|
|
||||||
use super::unnamed_power::raw_io;
|
use super::unnamed_power::raw_io;
|
||||||
|
use std::io::Error;
|
||||||
|
|
||||||
//#[test]
|
//#[test]
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
|
|
|
@ -75,7 +75,10 @@ mod ports_internal {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fallback (compiler error)
|
// Fallback (compiler error)
|
||||||
#[cfg(not(any(all(any(target_arch = "x86", target_arch = "x86_64")), target_os = "linux")))]
|
#[cfg(not(any(
|
||||||
|
all(any(target_arch = "x86", target_arch = "x86_64")),
|
||||||
|
target_os = "linux"
|
||||||
|
)))]
|
||||||
mod ports_internal {
|
mod ports_internal {
|
||||||
#[inline]
|
#[inline]
|
||||||
pub(crate) fn outb(port: u16, val: u8) {
|
pub(crate) fn outb(port: u16, val: u8) {
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
mod addresses;
|
mod addresses;
|
||||||
pub use addresses::{Setting, Charge, ChargeMode, ControlBoard, BreathingColour, StaticColour};
|
pub use addresses::{BreathingColour, Charge, ChargeMode, ControlBoard, Setting, StaticColour};
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
pub mod raw_io;
|
pub mod raw_io;
|
||||||
|
|
||||||
|
@ -42,9 +42,9 @@ impl super::ControllerSet<Setting> for UnnamedPowerEC {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use std::io::Error;
|
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::ec::raw_io;
|
use crate::ec::raw_io;
|
||||||
|
use std::io::Error;
|
||||||
|
|
||||||
//#[test]
|
//#[test]
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
|
@ -67,7 +67,7 @@ mod test {
|
||||||
let original = raw_io::write_read(Setting::LEDStatic as _)?;
|
let original = raw_io::write_read(Setting::LEDStatic as _)?;
|
||||||
let sleep_dur = std::time::Duration::from_millis(1000);
|
let sleep_dur = std::time::Duration::from_millis(1000);
|
||||||
let mut value = 1;
|
let mut value = 1;
|
||||||
for _ in 0..std::mem::size_of::<u8>()*8 {
|
for _ in 0..std::mem::size_of::<u8>() * 8 {
|
||||||
let actual = 0x80 | value;
|
let actual = 0x80 | value;
|
||||||
raw_io::write2(Setting::LEDStatic as _, actual)?;
|
raw_io::write2(Setting::LEDStatic as _, actual)?;
|
||||||
println!("Wrote {actual:#b} to LED byte");
|
println!("Wrote {actual:#b} to LED byte");
|
||||||
|
|
Loading…
Reference in a new issue