Skip to content

Commit

Permalink
Fix error enums
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbeechey committed Oct 15, 2024
1 parent 0bd067f commit ceb2ed1
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
14 changes: 11 additions & 3 deletions boards/stm32l476rg/src/io/i2c.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use embassy_stm32::{i2c::I2c, mode::Blocking};
use hyped_io::i2c::HypedI2c;
use hyped_io::i2c::{HypedI2c, I2cError};

pub struct Stm32l476rgI2c<'d> {
i2c: I2c<'d, Blocking>,
Expand All @@ -24,13 +24,21 @@ impl<'d> HypedI2c for Stm32l476rgI2c<'d> {
device_address: u8,
register_address: u8,
data: u8,
) -> Result<(), ()> {
) -> Result<(), I2cError> {
let result = self
.i2c
.blocking_write(device_address, [register_address, data].as_ref());
match result {
Ok(_) => Ok(()),
Err(_) => Err(()),
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,
}),
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions lib/io/src/i2c.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/// I2C trait used to abstract the I2C peripheral
#[derive(Debug)]
pub enum I2cError {
// Error codes nased https://docs.embassy.dev/embassy-stm32/git/stm32g031c8/i2c/enum.Error.html
Bus,
Expand All @@ -11,6 +12,7 @@ pub enum I2cError {
ZeroLengthTransfer,
Unknown,
}

pub trait HypedI2c {
fn read_byte(&mut self, device_address: u8, register_address: u8) -> Option<u8>;
fn write_byte_to_register(
Expand Down
10 changes: 8 additions & 2 deletions lib/sensors/src/temperature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ pub struct Temperature<T: HypedI2c> {

impl<T: HypedI2c> Temperature<T> {
/// Create a new instance of the temperature sensor and attempt to configure it
pub fn new(mut i2c: T, device_address: TemperatureAddresses) -> Result<Self, I2cError> {
pub fn new(mut i2c: T, device_address: TemperatureAddresses) -> Result<Self, TemperatureError> {
// Set up the temperature sensor by sending the configuration settings to the STTS22H_CTRL register
let device_address = device_address as u8;
match i2c.write_byte_to_register(device_address, STTS22H_CTRL, STTS22H_CONFIG_SETTINGS) {
Ok(_) => Ok(Self {
i2c,
device_address,
}),
Err(_) => Err(I2cError::Unknown),
Err(e) => Err(TemperatureError::I2cError(e)),
}
}

Expand Down Expand Up @@ -70,6 +70,12 @@ pub enum TemperatureAddresses {
Address3e = 0x3e,
}

/// Represents the possible errors that can occur when reading the temperature sensor
#[derive(Debug)]
pub enum TemperatureError {
I2cError(I2cError),
}

/// Represents the possible statuses of the temperature sensor
pub enum Status {
Busy,
Expand Down

0 comments on commit ceb2ed1

Please sign in to comment.