Skip to content
This repository has been archived by the owner on Dec 6, 2023. It is now read-only.

Minor improvements #1

Merged
merged 5 commits into from
May 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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)
46 changes: 20 additions & 26 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -106,7 +106,7 @@ pub enum Command {
Dcdc3Voltage(bool),
Ldo23Voltage(bool),
Gpio1Control(bool),
Gpio2Control(bool)
Gpio2Control(bool),
}

pub enum DataFormat<'a> {
Expand All @@ -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]))
}
Expand Down Expand Up @@ -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)
}
}
}
Expand All @@ -190,8 +189,8 @@ where
}

impl<I> Axp192<I>
where
I: Axp192ReadWrite,
where
I: Axp192ReadWrite,
{
// Create a new AXP192 interface
pub fn new(interface: I) -> Self {
Expand All @@ -211,7 +210,6 @@ impl<I> Axp192<I>

Ok(())
}

}

pub struct I2CInterface<I2C> {
Expand All @@ -222,7 +220,7 @@ pub struct I2CInterface<I2C> {

impl<I2C> I2CInterface<I2C>
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 {
Expand All @@ -243,7 +241,6 @@ where
#[derive(Debug, Copy, Clone)]
pub struct I2CPowerManagementInterface(());


impl I2CPowerManagementInterface {
pub fn new<I>(i2c: I) -> I2CInterface<I>
where
Expand All @@ -260,6 +257,3 @@ impl I2CPowerManagementInterface {
I2CInterface::new(i2c, address, 0x34)
}
}