Skip to content

Commit

Permalink
Derive common traits in i2c::master module for various types
Browse files Browse the repository at this point in the history
  • Loading branch information
jessebraham committed Dec 17, 2024
1 parent 231dc2e commit 3b2a112
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions esp-hal/src/i2c/master/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ const I2C_CHUNK_SIZE: usize = 254;
const MAX_ITERATIONS: u32 = 1_000_000;

/// I2C-specific transmission errors
#[derive(Debug, Clone, Copy, PartialEq)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[non_exhaustive]
pub enum Error {
Expand Down Expand Up @@ -126,6 +126,7 @@ enum OpKind {
/// I2C operation.
///
/// Several operations can be combined as part of a transaction.
#[derive(Debug, PartialEq, Eq, Hash, strum::Display)]
pub enum Operation<'a> {
/// Write data from the provided buffer.
Write(&'a [u8]),
Expand Down Expand Up @@ -239,7 +240,7 @@ impl From<Ack> for u32 {
}

/// I2C driver configuration
#[derive(Debug, Clone, Copy, procmacros::BuilderLite)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, procmacros::BuilderLite)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[non_exhaustive]
pub struct Config {
Expand All @@ -263,6 +264,13 @@ pub struct Config {
pub timeout: Option<u32>,
}

impl core::hash::Hash for Config {
fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
self.frequency.to_Hz().hash(state); // `HertzU32` doesn't implement `Hash`
self.timeout.hash(state);
}
}

impl Default for Config {
fn default() -> Self {
use fugit::RateExtU32;
Expand All @@ -274,6 +282,8 @@ impl Default for Config {
}

/// I2C driver
#[derive(Debug, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct I2c<'d, DM: Mode, T = AnyI2c> {
i2c: PeripheralRef<'d, T>,
phantom: PhantomData<DM>,
Expand Down Expand Up @@ -934,6 +944,7 @@ fn configure_clock(
}

/// Peripheral data describing a particular I2C instance.
#[derive(Debug)]
#[non_exhaustive]
pub struct Info {
/// Pointer to the register block for this I2C instance.
Expand Down Expand Up @@ -970,6 +981,14 @@ impl Info {
}
}

impl PartialEq for Info {
fn eq(&self, other: &Self) -> bool {
self.register_block == other.register_block
}
}

unsafe impl Sync for Info {}

#[allow(dead_code)] // Some versions don't need `state`
struct Driver<'a> {
info: &'a Info,
Expand Down Expand Up @@ -2106,14 +2125,6 @@ impl Driver<'_> {
}
}

impl PartialEq for Info {
fn eq(&self, other: &Self) -> bool {
self.register_block == other.register_block
}
}

unsafe impl Sync for Info {}

/// Peripheral state for an I2C instance.
#[non_exhaustive]
pub struct State {
Expand Down

0 comments on commit 3b2a112

Please sign in to comment.