Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SNS - I2C Mux (HYPE-13) #14

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
48 changes: 24 additions & 24 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions boards/stm32f767zi/src/io/i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,23 @@ impl<'d> HypedI2c for Stm32f767ziI2c<'d> {
}),
}
}

/// Write a byte to a device
fn write_byte(&mut self, device_address: u8, data: u8) -> Result<(), I2cError> {
let result = self.i2c.blocking_write(device_address, [data].as_ref());
match result {
Ok(_) => Ok(()),
Err(e) => Err(match e {
embassy_stm32::i2c::Error::Bus => I2cError::Bus,
embassy_stm32::i2c::Error::Arbitration => I2cError::Arbitration,
embassy_stm32::i2c::Error::Nack => I2cError::Nack,
embassy_stm32::i2c::Error::Timeout => I2cError::Timeout,
embassy_stm32::i2c::Error::Crc => I2cError::Crc,
embassy_stm32::i2c::Error::Overrun => I2cError::Overrun,
embassy_stm32::i2c::Error::ZeroLengthTransfer => I2cError::ZeroLengthTransfer,
}),
}
}
}

impl<'d> Stm32f767ziI2c<'d> {
Expand Down
17 changes: 17 additions & 0 deletions boards/stm32l476rg/src/io/i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,23 @@ impl<'d> HypedI2c for Stm32l476rgI2c<'d> {
}),
}
}

/// Write a byte to a device
fn write_byte(&mut self, device_address: u8, data: u8) -> Result<(), I2cError> {
let result = self.i2c.blocking_write(device_address, [data].as_ref());
match result {
Ok(_) => Ok(()),
Err(e) => Err(match e {
embassy_stm32::i2c::Error::Bus => I2cError::Bus,
embassy_stm32::i2c::Error::Arbitration => I2cError::Arbitration,
embassy_stm32::i2c::Error::Nack => I2cError::Nack,
embassy_stm32::i2c::Error::Timeout => I2cError::Timeout,
embassy_stm32::i2c::Error::Crc => I2cError::Crc,
embassy_stm32::i2c::Error::Overrun => I2cError::Overrun,
embassy_stm32::i2c::Error::ZeroLengthTransfer => I2cError::ZeroLengthTransfer,
}),
}
}
}

impl<'d> Stm32l476rgI2c<'d> {
Expand Down
2 changes: 1 addition & 1 deletion lib/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ rust-mqtt = { version = "0.3.0", default-features = false, features = ["defmt"]
embedded-io-async = { version = "0.6.1" }
rand_core = "0.6.3"
serde = { version = "1.0", default-features = false, features = ["derive"] }
embassy-net = { version = "0.4.0", default-features = false, features = ["defmt", "tcp", "proto-ipv4", "medium-ip"], git = "https://github.com/embassy-rs/embassy", rev = "1c466b81e6af6b34b1f706318cc0870a459550b7"}
embassy-net = { version = "0.4.0", default-features = false, features = ["defmt", "tcp", "proto-ipv4", "medium-ip"], git = "https://github.com/embassy-rs/embassy", rev = "1c466b81e6af6b34b1f706318cc0870a459550b7"}

[features]
std = []
6 changes: 6 additions & 0 deletions lib/io/src/i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,10 @@ pub trait HypedI2c {
register_address: u8,
data: u8,
) -> Result<(), I2cError>;
fn write_byte(&mut self, device_address: u8, data: u8) -> Result<(), I2cError>;
}

/// I2C mux trait used to abstract the I2C multiplexer
pub trait HypedI2cMux: HypedI2c {
fn select_channel(&mut self) -> Result<(), I2cError>;
}
68 changes: 68 additions & 0 deletions lib/io/src/i2c_mux.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use crate::i2c::{HypedI2c, HypedI2cMux, I2cError};

pub struct I2cMux<T: HypedI2c> {
i2c: T,
mux_address: u8,
channel: u8,
}

pub enum I2cMuxError {
InvalidChannel,
}

impl<T: HypedI2c> I2cMux<T> {
pub fn new(i2c: T, channel: u8, mux_address: u8) -> Result<Self, I2cMuxError> {
// Check that the channel is valid
if channel >= MAX_MUX_CHANNELS {
return Err(I2cMuxError::InvalidChannel);
}
Ok(Self {
i2c,
channel,
mux_address,
})
}
}

impl<T: HypedI2c> HypedI2c for I2cMux<T> {
fn read_byte(&mut self, device_address: u8, register_address: u8) -> Option<u8> {
match self.select_channel() {
Ok(_) => self.i2c.read_byte(device_address, register_address),
Err(_) => None,
}
}

fn write_byte_to_register(
&mut self,
device_address: u8,
register_address: u8,
data: u8,
) -> Result<(), I2cError> {
match self.select_channel() {
Ok(_) => self
.i2c
.write_byte_to_register(device_address, register_address, data),
Err(e) => Err(e as I2cError),
}
}

fn write_byte(&mut self, device_address: u8, data: u8) -> Result<(), I2cError> {
match self.select_channel() {
Ok(_) => self.i2c.write_byte(device_address, data),
Err(e) => Err(e as I2cError),
}
}
}

impl<T: HypedI2c> HypedI2cMux for I2cMux<T> {
/// Selects the channel on the multiplexer
fn select_channel(&mut self) -> Result<(), I2cError> {
match self.i2c.write_byte(self.mux_address, 1 << self.channel) {
Ok(_) => Ok(()),
Err(e) => Err(e as I2cError),
}
}
}

pub const DEFAULT_MUX_ADDRESS: u8 = 0x70;
const MAX_MUX_CHANNELS: u8 = 8;
1 change: 1 addition & 0 deletions lib/io/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

pub mod gpio;
pub mod i2c;
pub mod i2c_mux;
pub mod spi;