ectool 0.2.2: Use buffer to improve SPI performance

This commit is contained in:
Jeremy Soller 2020-10-06 09:23:26 -06:00 committed by Jeremy Soller
parent 3ed8db09c5
commit b2aa7ba975
4 changed files with 36 additions and 30 deletions

10
tool/Cargo.lock generated
View File

@ -11,13 +11,13 @@ version = "1.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [ dependencies = [
"cc 1.0.60 (registry+https://github.com/rust-lang/crates.io-index)", "cc 1.0.60 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.78 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.79 (registry+https://github.com/rust-lang/crates.io-index)",
"pkg-config 0.3.18 (registry+https://github.com/rust-lang/crates.io-index)", "pkg-config 0.3.18 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.78" version = "0.2.79"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
@ -32,16 +32,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]] [[package]]
name = "system76_ectool" name = "system76_ectool"
version = "0.2.1" version = "0.2.2"
dependencies = [ dependencies = [
"hidapi 1.2.3 (registry+https://github.com/rust-lang/crates.io-index)", "hidapi 1.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.78 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.79 (registry+https://github.com/rust-lang/crates.io-index)",
"redox_hwio 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", "redox_hwio 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[metadata] [metadata]
"checksum cc 1.0.60 (registry+https://github.com/rust-lang/crates.io-index)" = "ef611cc68ff783f18535d77ddd080185275713d852c4f5cbb6122c462a7a825c" "checksum cc 1.0.60 (registry+https://github.com/rust-lang/crates.io-index)" = "ef611cc68ff783f18535d77ddd080185275713d852c4f5cbb6122c462a7a825c"
"checksum hidapi 1.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "5c6ffb97f2ec5835ec73bcea5256fc2cd57a13c5958230778ef97f11900ba661" "checksum hidapi 1.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "5c6ffb97f2ec5835ec73bcea5256fc2cd57a13c5958230778ef97f11900ba661"
"checksum libc 0.2.78 (registry+https://github.com/rust-lang/crates.io-index)" = "aa7087f49d294270db4e1928fc110c976cd4b9e5a16348e0a1df09afa99e6c98" "checksum libc 0.2.79 (registry+https://github.com/rust-lang/crates.io-index)" = "2448f6066e80e3bfc792e9c98bf705b4b0fc6e8ef5b43e5889aff0eaa9c58743"
"checksum pkg-config 0.3.18 (registry+https://github.com/rust-lang/crates.io-index)" = "d36492546b6af1463394d46f0c834346f31548646f6ba10849802c9c9a27ac33" "checksum pkg-config 0.3.18 (registry+https://github.com/rust-lang/crates.io-index)" = "d36492546b6af1463394d46f0c834346f31548646f6ba10849802c9c9a27ac33"
"checksum redox_hwio 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "41aa2c4c67329a04106644cad336238aa5adecfd73d06fb10339d472ce6d8070" "checksum redox_hwio 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "41aa2c4c67329a04106644cad336238aa5adecfd73d06fb10339d472ce6d8070"

View File

@ -1,6 +1,6 @@
[package] [package]
name = "system76_ectool" name = "system76_ectool"
version = "0.2.1" version = "0.2.2"
edition = "2018" edition = "2018"
description = "System76 EC tool" description = "System76 EC tool"
license = "MIT" license = "MIT"

View File

@ -1,3 +1,9 @@
#[cfg(not(feature = "std"))]
use alloc::{
boxed::Box,
vec,
};
use crate::{ use crate::{
Access, Access,
Error, Error,
@ -125,10 +131,12 @@ impl<A: Access> Ec<A> {
/// Access EC SPI bus /// Access EC SPI bus
pub unsafe fn spi(&mut self, target: SpiTarget, scratch: bool) -> Result<EcSpi<A>, Error> { pub unsafe fn spi(&mut self, target: SpiTarget, scratch: bool) -> Result<EcSpi<A>, Error> {
let data_size = self.access.data_size();
let mut spi = EcSpi { let mut spi = EcSpi {
ec: self, ec: self,
target, target,
scratch, scratch,
buffer: vec![0; data_size].into_boxed_slice(),
}; };
spi.reset()?; spi.reset()?;
Ok(spi) Ok(spi)
@ -191,6 +199,7 @@ pub struct EcSpi<'a, A: Access> {
ec: &'a mut Ec<A>, ec: &'a mut Ec<A>,
target: SpiTarget, target: SpiTarget,
scratch: bool, scratch: bool,
buffer: Box<[u8]>,
} }
impl<'a, A: Access> EcSpi<'a, A> { impl<'a, A: Access> EcSpi<'a, A> {
@ -228,12 +237,10 @@ impl<'a, A: Access> Spi for EcSpi<'a, A> {
/// Disable SPI chip, must be done before and after a transaction /// Disable SPI chip, must be done before and after a transaction
unsafe fn reset(&mut self) -> Result<(), Error> { unsafe fn reset(&mut self) -> Result<(), Error> {
let flags = self.flags(false, true); let flags = self.flags(false, true);
let mut data = [ self.buffer[0] = flags;
flags, self.buffer[1] = 0;
0, self.ec.command(Cmd::Spi, &mut self.buffer[..2])?;
]; if self.buffer[1] != 0 {
self.ec.command(Cmd::Spi, &mut data)?;
if data[1] != 0 {
return Err(Error::Verify); return Err(Error::Verify);
} }
Ok(()) Ok(())
@ -241,18 +248,16 @@ impl<'a, A: Access> Spi for EcSpi<'a, A> {
/// SPI read /// SPI read
unsafe fn read(&mut self, data: &mut [u8]) -> Result<usize, Error> { unsafe fn read(&mut self, data: &mut [u8]) -> Result<usize, Error> {
//TODO: use self.access.data_size()
let flags = self.flags(true, false); let flags = self.flags(true, false);
for chunk in data.chunks_mut(256 - 4) { for chunk in data.chunks_mut(self.buffer.len() - 2) {
let mut data = [0; 256 - 2]; self.buffer[0] = flags;
data[0] = flags; self.buffer[1] = chunk.len() as u8;
data[1] = chunk.len() as u8; self.ec.command(Cmd::Spi, &mut self.buffer[..(chunk.len() + 2)])?;
self.ec.command(Cmd::Spi, &mut data)?; if self.buffer[1] != chunk.len() as u8 {
if data[1] != chunk.len() as u8 {
return Err(Error::Verify); return Err(Error::Verify);
} }
for i in 0..chunk.len() { for i in 0..chunk.len() {
chunk[i] = data[i + 2]; chunk[i] = self.buffer[i + 2];
} }
} }
Ok(data.len()) Ok(data.len())
@ -260,17 +265,15 @@ impl<'a, A: Access> Spi for EcSpi<'a, A> {
/// SPI write /// SPI write
unsafe fn write(&mut self, data: &[u8]) -> Result<usize, Error> { unsafe fn write(&mut self, data: &[u8]) -> Result<usize, Error> {
//TODO: use self.access.data_size()
let flags = self.flags(false, false); let flags = self.flags(false, false);
for chunk in data.chunks(256 - 4) { for chunk in data.chunks(self.buffer.len() - 2) {
let mut data = [0; 256 - 2]; self.buffer[0] = flags;
data[0] = flags; self.buffer[1] = chunk.len() as u8;
data[1] = chunk.len() as u8;
for i in 0..chunk.len() { for i in 0..chunk.len() {
data[i + 2] = chunk[i]; self.buffer[i + 2] = chunk[i];
} }
self.ec.command(Cmd::Spi, &mut data)?; self.ec.command(Cmd::Spi, &mut self.buffer[..(chunk.len() + 2)])?;
if data[1] != chunk.len() as u8 { if self.buffer[1] != chunk.len() as u8 {
return Err(Error::Verify); return Err(Error::Verify);
} }
} }

View File

@ -1,5 +1,3 @@
#![cfg_attr(not(feature = "std"), no_std)]
//! Library for accessing System76 ECs //! Library for accessing System76 ECs
//! First, construct an access method, using an object implementing the `Access` trait. Next, an Ec //! First, construct an access method, using an object implementing the `Access` trait. Next, an Ec
//! object can be contructed, which exposes the command interface. //! object can be contructed, which exposes the command interface.
@ -14,6 +12,11 @@
//! compiler. It is only recommended to use these in firmware, as mutual exclusion is not //! compiler. It is only recommended to use these in firmware, as mutual exclusion is not
//! guaranteed. //! guaranteed.
#![cfg_attr(not(feature = "std"), no_std)]
#[cfg(not(feature = "std"))]
extern crate alloc;
pub use self::access::*; pub use self::access::*;
mod access; mod access;