cargo fmt

This commit is contained in:
NGnius (Graham) 2024-02-18 17:33:52 -05:00
parent ef5afdee93
commit 75e0a8792b
6 changed files with 77 additions and 52 deletions

View file

@ -17,7 +17,11 @@ fn main() {
match cli.op { match cli.op {
args::Operation::ReadAll => { args::Operation::ReadAll => {
let (bytes_per_line, range) = if cli.extended { (16, 0..=u16::MAX) } else { (8, 0..=(u8::MAX as u16)) }; let (bytes_per_line, range) = if cli.extended {
(16, 0..=u16::MAX)
} else {
(8, 0..=(u8::MAX as u16))
};
for addr in range { for addr in range {
let addr_mod = addr % bytes_per_line; let addr_mod = addr % bytes_per_line;
if addr_mod == 0 { if addr_mod == 0 {
@ -31,21 +35,25 @@ fn main() {
} }
} }
println!(""); println!("");
}, }
args::Operation::Read { address } => { args::Operation::Read { address } => {
println!("{:#02x}", ec.get(address)); println!("{:#02x}", ec.get(address));
}, }
args::Operation::WritePattern { bytes } => { args::Operation::WritePattern { bytes } => {
let range = if cli.extended { 0..=u16::MAX } else { 0..=(u8::MAX as u16) }; let range = if cli.extended {
0..=u16::MAX
} else {
0..=(u8::MAX as u16)
};
for addr in range { for addr in range {
println!("{:#02x}", addr); println!("{:#02x}", addr);
for b in bytes.iter() { for b in bytes.iter() {
ec.set(addr, *b); 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,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;
} }

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

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