Skip to content

Commit

Permalink
Ensure that i2c pins have PullUp activated
Browse files Browse the repository at this point in the history
It is possible to skip this check by using `hal::I2C::i2c0_unchecked(...)`
or `hal::I2C::i2c1_unchecked(...)`, but the default constructor
functions now only accept pins with activated PullUp.

Fixes #690.
  • Loading branch information
jannic committed Nov 17, 2023
1 parent aecf366 commit 6c6158d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
6 changes: 3 additions & 3 deletions rp2040-hal/examples/i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use hal::fugit::RateExtU32;
// A shorter alias for the Peripheral Access Crate, which provides low-level
// register access and a gpio related types.
use hal::{
gpio::{FunctionI2C, Pin, PullUp},
gpio::{FunctionI2C, Pin},
pac,
};

Expand Down Expand Up @@ -78,8 +78,8 @@ fn main() -> ! {
);

// Configure two pins as being I²C, not GPIO
let sda_pin: Pin<_, FunctionI2C, PullUp> = pins.gpio18.reconfigure();
let scl_pin: Pin<_, FunctionI2C, PullUp> = pins.gpio19.reconfigure();
let sda_pin: Pin<_, FunctionI2C, _> = pins.gpio18.reconfigure();
let scl_pin: Pin<_, FunctionI2C, _> = pins.gpio19.reconfigure();
// let not_an_scl_pin: Pin<_, FunctionI2C, PullUp> = pins.gpio20.reconfigure();

// Create the I²C drive, using the two pre-configured pins. This will fail
Expand Down
26 changes: 23 additions & 3 deletions rp2040-hal/src/i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ use core::{marker::PhantomData, ops::Deref};
use fugit::HertzU32;

use crate::{
gpio::{bank0::*, pin::pin_sealed::TypeLevelPinId, AnyPin, FunctionI2c},
gpio::{bank0::*, pin::pin_sealed::TypeLevelPinId, AnyPin, FunctionI2c, PullUp},
pac::{self, i2c0::RegisterBlock as I2CBlock, I2C0, I2C1, RESETS},
resets::SubsystemReset,
typelevel::Sealed,
Expand Down Expand Up @@ -331,12 +331,32 @@ macro_rules! hal {
system_clock: SystemF) -> Self
where
F: Into<HertzU32>,
Sda: ValidPinSda<$I2CX>,
Scl: ValidPinScl<$I2CX>,
Sda: ValidPinSda<$I2CX> + AnyPin<Pull = PullUp>,
Scl: ValidPinScl<$I2CX> + AnyPin<Pull = PullUp>,
SystemF: Into<HertzU32>,
{
Self::new_controller(i2c, sda_pin, scl_pin, freq.into(), resets, system_clock.into())
}

$crate::paste::paste! {
/// Configures the I2C peripheral to work in master mode
/// This function can be called without the pull-ups on the I2C pins.
pub fn [<$i2cX _unchecked>]<F, SystemF>(
i2c: $I2CX,
sda_pin: Sda,
scl_pin: Scl,
freq: F,
resets: &mut RESETS,
system_clock: SystemF) -> Self
where
F: Into<HertzU32>,
Sda: ValidPinSda<$I2CX>,
Scl: ValidPinScl<$I2CX>,
SystemF: Into<HertzU32>,
{
Self::new_controller(i2c, sda_pin, scl_pin, freq.into(), resets, system_clock.into())
}
}
}
)+
}
Expand Down

0 comments on commit 6c6158d

Please sign in to comment.