Skip to content

Commit

Permalink
Merge pull request #708 from jannic/checked-i2c-construction
Browse files Browse the repository at this point in the history
Ensure that i2c pins have PullUp activated
  • Loading branch information
jannic authored Jan 3, 2024
2 parents 20a5cbc + a965f1c commit 6f6dc5d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 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
32 changes: 27 additions & 5 deletions rp2040-hal/src/i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
//!
//! let mut i2c = I2C::i2c1(
//! peripherals.I2C1,
//! pins.gpio18.into_pull_up_input().into_function(), // sda
//! pins.gpio19.into_pull_up_input().into_function(), // scl
//! pins.gpio18.reconfigure(), // sda
//! pins.gpio19.reconfigure(), // scl
//! 400.kHz(),
//! &mut peripherals.RESETS,
//! 125_000_000.Hz(),
Expand Down 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 @@ -327,12 +327,34 @@ 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 activating internal pull-ups on the I2C pins.
/// It should only be used if external pull-ups are provided.
pub fn [<$i2cX _with_external_pull_up>]<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 6f6dc5d

Please sign in to comment.