diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..eff42c9 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,51 @@ +on: + pull_request: + branches: + - main + push: + workflow_dispatch: + +name: CI + +env: + CARGO_TERM_COLOR: always + +jobs: + check: + name: cargo check + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + toolchain: ["1.60.0", stable, nightly] + steps: + - uses: actions/checkout@v3 + - uses: dtolnay/rust-toolchain@v1 + with: + toolchain: ${{ matrix.toolchain }} + - uses: Swatinem/rust-cache@v2 + - run: cargo check + + rustfmt: + name: rustfmt + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: dtolnay/rust-toolchain@v1 + with: + toolchain: nightly + components: rustfmt + - uses: Swatinem/rust-cache@v2 + - run: cargo fmt --all -- --check + + clippy: + name: clippy + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: dtolnay/rust-toolchain@v1 + with: + toolchain: stable + components: clippy + - uses: Swatinem/rust-cache@v2 + - run: cargo clippy -- -A clippy::new-ret-no-self -D warnings diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6985cf1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,14 @@ +# Generated by Cargo +# will have compiled files and executables +debug/ +target/ + +# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries +# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html +Cargo.lock + +# These are backup files generated by rustfmt +**/*.rs.bk + +# MSVC Windows builds of rustc generate these, which store debugging information +*.pdb diff --git a/README.md b/README.md index 04ed501..b6c6f1a 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,11 @@ # AXP192 Rust +[![CI](https://github.com/SergioGasquez/axp192-rs/actions/workflows/ci.yml/badge.svg)](https://github.com/SergioGasquez/axp192-rs/actions/workflows/ci.yml) +![MSRV](https://img.shields.io/badge/MSRV-1.60-blue?style=flat-square) + Rust implementation of AXP192 driver used in M5Stack devices. Initial version. - +## Resources +- [AXP192 Datasheet](https://github.com/m5stack/M5-Schematic/blob/master/Core/AXP192%20Datasheet_v1.1_en_draft_2211.pdf) diff --git a/src/lib.rs b/src/lib.rs index 9dd4d5e..3cc5eeb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,5 @@ // Based on https://github.com/tuupola/axp192 - +#![allow(dead_code)] const AXP192_ADDRESS: u8 = 0x34; /* Power control registers */ const AXP192_POWER_STATUS: u8 = 0x00; @@ -106,7 +106,7 @@ pub enum Command { Dcdc3Voltage(bool), Ldo23Voltage(bool), Gpio1Control(bool), - Gpio2Control(bool) + Gpio2Control(bool), } pub enum DataFormat<'a> { @@ -123,13 +123,13 @@ impl Command { let (data, len) = match self { // Command structure: address, command, data, count & 0xf1 //Command::Dcdc3Voltage(on) => ([AXP192_ADDRESS, AXP192_LDO2 , 0x0], 3), - Command::Dcdc13Ldo23Control(on) => ([AXP192_DCDC13_LDO23_CONTROL, 119], 2), - Command::Dcdc2Slope(on) => ([AXP192_DCDC2_SLOPE, 0x0], 2), - Command::Dcdc1Voltage(on) => ([AXP192_DCDC1_VOLTAGE, 106], 2), - Command::Dcdc3Voltage(on) => ([AXP192_DCDC3_VOLTAGE, 104], 2), - Command::Ldo23Voltage(on) => ([AXP192_LDO23_VOLTAGE, 242], 2), - Command::Gpio1Control(on) => ([AXP192_GPIO1_CONTROL, 0x0], 2), - Command::Gpio2Control(on) => ([AXP192_GPIO2_CONTROL, 104], 2), + Command::Dcdc13Ldo23Control(_on) => ([AXP192_DCDC13_LDO23_CONTROL, 119], 2), + Command::Dcdc2Slope(_on) => ([AXP192_DCDC2_SLOPE, 0x0], 2), + Command::Dcdc1Voltage(_on) => ([AXP192_DCDC1_VOLTAGE, 106], 2), + Command::Dcdc3Voltage(_on) => ([AXP192_DCDC3_VOLTAGE, 104], 2), + Command::Ldo23Voltage(_on) => ([AXP192_LDO23_VOLTAGE, 242], 2), + Command::Gpio1Control(_on) => ([AXP192_GPIO1_CONTROL, 0x0], 2), + Command::Gpio2Control(_on) => ([AXP192_GPIO2_CONTROL, 104], 2), }; iface.send_commands(DataFormat::U8(&data[0..len])) } @@ -164,16 +164,15 @@ where match cmd { DataFormat::U8(data) => { + self.i2c + .write_read(self.addr, &[data[0]], &mut data_buf) + .map_err(|_| Axp192Error::WriteError)?; + //println!("read value for command {:?}: {:?}", data[0], data_buf[0]); - let result = self.i2c - .write_read(self.addr, &[data[0]], &mut data_buf) - .map_err(|_| Axp192Error::WriteError); - //println!("read value for command {:?}: {:?}", data[0], data_buf[0]); - - //println!("write value for command {:?}: {:?}", data[0], data[1]); - self.i2c - .write(self.addr, &data) - .map_err(|_| Axp192Error::WriteError) + //println!("write value for command {:?}: {:?}", data[0], data[1]); + self.i2c + .write(self.addr, data) + .map_err(|_| Axp192Error::WriteError) } } } @@ -190,8 +189,8 @@ where } impl Axp192 - where - I: Axp192ReadWrite, +where + I: Axp192ReadWrite, { // Create a new AXP192 interface pub fn new(interface: I) -> Self { @@ -211,7 +210,6 @@ impl Axp192 Ok(()) } - } pub struct I2CInterface { @@ -222,7 +220,7 @@ pub struct I2CInterface { impl I2CInterface where - I2C: embedded_hal::blocking::i2c::Write/*+ embedded_hal::blocking::i2c::WriteRead*/, + I2C: embedded_hal::blocking::i2c::Write, /*+ embedded_hal::blocking::i2c::WriteRead*/ { /// Create new I2C interface for communication with a display driver pub fn new(i2c: I2C, addr: u8, data_byte: u8) -> Self { @@ -243,7 +241,6 @@ where #[derive(Debug, Copy, Clone)] pub struct I2CPowerManagementInterface(()); - impl I2CPowerManagementInterface { pub fn new(i2c: I) -> I2CInterface where @@ -260,6 +257,3 @@ impl I2CPowerManagementInterface { I2CInterface::new(i2c, address, 0x34) } } - - -