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

View file

@ -4,6 +4,7 @@ use smokepatio::ec::EmbeddedController;
fn main() {
let cli = args::Args::load();
#[cfg(debug_assertions)]
println!("args: {:?}", cli);
let mut ec = EmbeddedController::new(cli.data_address, cli.cmd_address);
@ -15,13 +16,44 @@ fn main() {
}
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 } => {
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 } => {
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> {
let size = self.rw_size as u8;
match self.rw_size {
ValueSize::Byte => for &b in buf {
self.wait_for_write_ready();
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
let w = [buf[i], buf[i+1]];
super::ports::outw(self.data_address, u16::from_ne_bytes(w));
},
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));
ValueSize::Byte => {
for &b in buf {
self.wait_for_write_ready();
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
let w = [buf[i], buf[i + 1]];
super::ports::outw(self.data_address, u16::from_ne_bytes(w));
}
}
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;
@ -145,7 +151,8 @@ impl embedded_io::Write for EmbeddedController {
impl embedded_io::WriteReady for EmbeddedController {
fn write_ready(&mut self) -> Result<bool, Self::Error> {
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 {
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> {
let size = self.rw_size as u8;
match self.rw_size {
ValueSize::Byte => for b in buf.iter_mut() {
self.wait_for_read_ready();
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::Byte => {
for b in buf.iter_mut() {
self.wait_for_read_ready();
self.cmd_requested = 1; // don't re-request, but still wait
*b = super::ports::inb(self.data_address);
}
},
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];
}
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) {
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 {
fn read_ready(&mut self) -> Result<bool, Self::Error> {
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 {
self.cmd_requested = 0;
}

View file

@ -8,12 +8,12 @@
mod controller;
mod ports;
pub mod unnamed_power;
pub use controller::{EmbeddedController, ControllerGet, GetValue, ControllerSet, SetValue};
pub use controller::{ControllerGet, ControllerSet, EmbeddedController, GetValue, SetValue};
#[cfg(test)]
mod tests {
use std::io::Error;
use super::unnamed_power::raw_io;
use std::io::Error;
//#[test]
#[allow(dead_code)]

View file

@ -75,7 +75,10 @@ mod ports_internal {
}
// 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 {
#[inline]
pub(crate) fn outb(port: u16, val: u8) {

View file

@ -11,10 +11,10 @@ pub enum Setting {
LEDStatic = 199,
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
// 0x40 write 0x08 makes LED red + green turn on
// 0x58 write 0x80 shuts off battery power (bms?)
// 0x63 makes blue (0x02) or white (0x01) LED breathing effect
// 0x7a write 0x01, 0x02, or 0x03 turns off display
// 0x40 write 0x08 makes LED red + green turn on
// 0x58 write 0x80 shuts off battery power (bms?)
// 0x63 makes blue (0x02) or white (0x01) LED breathing effect
// 0x7a write 0x01, 0x02, or 0x03 turns off display
}
#[derive(Copy, Clone, Debug)]

View file

@ -1,5 +1,5 @@
mod addresses;
pub use addresses::{Setting, Charge, ChargeMode, ControlBoard, BreathingColour, StaticColour};
pub use addresses::{BreathingColour, Charge, ChargeMode, ControlBoard, Setting, StaticColour};
#[cfg(feature = "std")]
pub mod raw_io;
@ -42,9 +42,9 @@ impl super::ControllerSet<Setting> for UnnamedPowerEC {
#[cfg(test)]
mod test {
use std::io::Error;
use super::*;
use crate::ec::raw_io;
use std::io::Error;
//#[test]
#[allow(dead_code)]
@ -67,7 +67,7 @@ mod test {
let original = raw_io::write_read(Setting::LEDStatic as _)?;
let sleep_dur = std::time::Duration::from_millis(1000);
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;
raw_io::write2(Setting::LEDStatic as _, actual)?;
println!("Wrote {actual:#b} to LED byte");