Skip to content

Commit

Permalink
ConfigError
Browse files Browse the repository at this point in the history
  • Loading branch information
bugadani committed Nov 6, 2024
1 parent 93e915c commit 648f6ed
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions esp-hal/src/i2c/master/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
mod support;
mod version;

use core::{convert::Infallible, marker::PhantomData};
use core::marker::PhantomData;

use embassy_embedded_hal::SetConfig;
use embassy_sync::waitqueue::AtomicWaker;
Expand Down Expand Up @@ -104,6 +104,11 @@ pub enum Error {
InvalidZeroLength,
}

/// I2C-specific configuration errors
#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum ConfigError {}

#[derive(PartialEq)]
// This enum is used to keep track of the last/next operation that was/will be
// performed in an embedded-hal(-async) I2c::transaction. It is used to
Expand Down Expand Up @@ -249,11 +254,10 @@ pub struct I2c<'d, DM: Mode, T = AnyI2c> {

impl<T: Instance, DM: Mode> SetConfig for I2c<'_, DM, T> {
type Config = Config;
type ConfigError = Infallible;
type ConfigError = ConfigError;

fn set_config(&mut self, config: &Self::Config) -> Result<(), Self::ConfigError> {
self.apply_config(config);
Ok(())
self.apply_config(config)
}
}

Expand All @@ -272,13 +276,16 @@ where
PeripheralClockControl::reset(self.i2c.peripheral());
PeripheralClockControl::enable(self.i2c.peripheral());

self.driver().setup(&self.config);
// It's okay to ignore the result, we've already validated the config
// either in `apply_config` or in `new_typed_with_config`.
_ = self.driver().setup(&self.config);
}

/// Applies a new configuration.
pub fn apply_config(&mut self, config: &Config) {
pub fn apply_config(&mut self, config: &Config) -> Result<(), ConfigError> {
self.driver().setup(config)?;
self.config = *config;
self.driver().setup(&self.config);
Ok(())
}

fn transaction_impl<'a>(
Expand Down Expand Up @@ -424,7 +431,7 @@ where
PeripheralClockControl::reset(i2c.i2c.peripheral());
PeripheralClockControl::enable(i2c.i2c.peripheral());

i2c.driver().setup(&i2c.config);
unwrap!(i2c.driver().setup(&i2c.config));
i2c
}

Expand Down Expand Up @@ -866,7 +873,7 @@ struct Driver<'a> {
impl Driver<'_> {
/// Configures the I2C peripheral with the specified frequency, clocks, and
/// optional timeout.
fn setup(&self, config: &Config) {
fn setup(&self, config: &Config) -> Result<(), ConfigError> {
self.info.register_block().ctr().write(|w| {
// Set I2C controller to master mode
w.ms_mode().set_bit();
Expand All @@ -893,6 +900,8 @@ impl Driver<'_> {

// Reset entire peripheral (also resets fifo)
self.reset();

Ok(())
}

/// Resets the I2C peripheral's command registers
Expand Down

0 comments on commit 648f6ed

Please sign in to comment.