Compare commits

...

2 commits

Author SHA1 Message Date
75e0a8792b cargo fmt 2024-02-18 17:33:52 -05:00
ef5afdee93 Implement ReadAll and WritePattern modes 2024-02-18 17:32:30 -05:00
7 changed files with 102 additions and 51 deletions

View file

@ -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 {

View file

@ -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);
}, }
} }
} }

View file

@ -115,22 +115,28 @@ 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 => {
self.wait_for_write_ready(); for &b in buf {
self.cmd_requested = 1; // don't re-request, but still wait self.wait_for_write_ready();
super::ports::outb(self.data_address, b); self.cmd_requested = 1; // don't re-request, but still wait
}, super::ports::outb(self.data_address, b);
ValueSize::Word => for i in (0..buf.len()).step_by(size as usize) { }
self.wait_for_write_ready(); }
self.cmd_requested = 1; // don't re-request, but still wait ValueSize::Word => {
let w = [buf[i], buf[i+1]]; for i in (0..buf.len()).step_by(size as usize) {
super::ports::outw(self.data_address, u16::from_ne_bytes(w)); self.wait_for_write_ready();
}, self.cmd_requested = 1; // don't re-request, but still wait
ValueSize::Long => for i in (0..buf.len()).step_by(size as usize) { let w = [buf[i], buf[i + 1]];
self.wait_for_write_ready(); super::ports::outw(self.data_address, u16::from_ne_bytes(w));
self.cmd_requested = 1; // don't re-request, but still wait }
let l = [buf[i], buf[i+1], buf[i+2], buf[i+3]]; }
super::ports::outl(self.data_address, u32::from_ne_bytes(l)); ValueSize::Long => {
for i in (0..buf.len()).step_by(size as usize) {
self.wait_for_write_ready();
self.cmd_requested = 1; // don't re-request, but still wait
let l = [buf[i], buf[i + 1], buf[i + 2], buf[i + 3]];
super::ports::outl(self.data_address, u32::from_ne_bytes(l));
}
} }
} }
self.cmd_requested = 0; self.cmd_requested = 0;
@ -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,25 +164,31 @@ 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 => {
self.wait_for_read_ready(); for b in buf.iter_mut() {
self.cmd_requested = 1; // don't re-request, but still wait self.wait_for_read_ready();
*b = super::ports::inb(self.data_address); self.cmd_requested = 1; // don't re-request, but still wait
}, *b = super::ports::inb(self.data_address);
ValueSize::Word => for i in (0..buf.len()).step_by(size as usize) {
self.wait_for_read_ready();
self.cmd_requested = 1; // don't re-request, but still wait
let w = super::ports::inw(self.data_address).to_ne_bytes();
for j in 0..(size as usize) {
buf[i + j] = w[j];
} }
}, }
ValueSize::Long => for i in (0..buf.len()).step_by(size as usize) { ValueSize::Word => {
self.wait_for_read_ready(); for i in (0..buf.len()).step_by(size as usize) {
self.cmd_requested = 1; // don't re-request, but still wait self.wait_for_read_ready();
let l = super::ports::inl(self.data_address).to_ne_bytes(); self.cmd_requested = 1; // don't re-request, but still wait
for j in 0..(size as usize) { let w = super::ports::inw(self.data_address).to_ne_bytes();
buf[i + j] = l[j]; for j in 0..(size as usize) {
buf[i + j] = w[j];
}
}
}
ValueSize::Long => {
for i in (0..buf.len()).step_by(size as usize) {
self.wait_for_read_ready();
self.cmd_requested = 1; // don't re-request, but still wait
let l = super::ports::inl(self.data_address).to_ne_bytes();
for j in 0..(size as usize) {
buf[i + j] = l[j];
}
} }
} }
} }
@ -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;
} }

View file

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

View file

@ -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) {

View file

@ -11,10 +11,10 @@ pub enum Setting {
LEDStatic = 199, LEDStatic = 199,
LEDBreathing = 0x63, LEDBreathing = 0x63,
FanSpeed = 0x2c, // lower 3 bits seem to not do everything, every other bit increases speed -- 5 total steps, 0xf4 seems to do something similar too FanSpeed = 0x2c, // lower 3 bits seem to not do everything, every other bit increases speed -- 5 total steps, 0xf4 seems to do something similar too
// 0x40 write 0x08 makes LED red + green turn on // 0x40 write 0x08 makes LED red + green turn on
// 0x58 write 0x80 shuts off battery power (bms?) // 0x58 write 0x80 shuts off battery power (bms?)
// 0x63 makes blue (0x02) or white (0x01) LED breathing effect // 0x63 makes blue (0x02) or white (0x01) LED breathing effect
// 0x7a write 0x01, 0x02, or 0x03 turns off display // 0x7a write 0x01, 0x02, or 0x03 turns off display
} }
#[derive(Copy, Clone, Debug)] #[derive(Copy, Clone, Debug)]

View file

@ -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");