Skip to content

Commit 6f6dc5d

Browse files
authored
Merge pull request #708 from jannic/checked-i2c-construction
Ensure that i2c pins have PullUp activated
2 parents 20a5cbc + a965f1c commit 6f6dc5d

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

rp2040-hal/examples/i2c.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use hal::fugit::RateExtU32;
2323
// A shorter alias for the Peripheral Access Crate, which provides low-level
2424
// register access and a gpio related types.
2525
use hal::{
26-
gpio::{FunctionI2C, Pin, PullUp},
26+
gpio::{FunctionI2C, Pin},
2727
pac,
2828
};
2929

@@ -78,8 +78,8 @@ fn main() -> ! {
7878
);
7979

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

8585
// Create the I²C drive, using the two pre-configured pins. This will fail

rp2040-hal/src/i2c.rs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
//!
1313
//! let mut i2c = I2C::i2c1(
1414
//! peripherals.I2C1,
15-
//! pins.gpio18.into_pull_up_input().into_function(), // sda
16-
//! pins.gpio19.into_pull_up_input().into_function(), // scl
15+
//! pins.gpio18.reconfigure(), // sda
16+
//! pins.gpio19.reconfigure(), // scl
1717
//! 400.kHz(),
1818
//! &mut peripherals.RESETS,
1919
//! 125_000_000.Hz(),
@@ -47,7 +47,7 @@ use core::{marker::PhantomData, ops::Deref};
4747
use fugit::HertzU32;
4848

4949
use crate::{
50-
gpio::{bank0::*, pin::pin_sealed::TypeLevelPinId, AnyPin, FunctionI2c},
50+
gpio::{bank0::*, pin::pin_sealed::TypeLevelPinId, AnyPin, FunctionI2c, PullUp},
5151
pac::{self, i2c0::RegisterBlock as I2CBlock, I2C0, I2C1, RESETS},
5252
resets::SubsystemReset,
5353
typelevel::Sealed,
@@ -327,12 +327,34 @@ macro_rules! hal {
327327
system_clock: SystemF) -> Self
328328
where
329329
F: Into<HertzU32>,
330-
Sda: ValidPinSda<$I2CX>,
331-
Scl: ValidPinScl<$I2CX>,
330+
Sda: ValidPinSda<$I2CX> + AnyPin<Pull = PullUp>,
331+
Scl: ValidPinScl<$I2CX> + AnyPin<Pull = PullUp>,
332332
SystemF: Into<HertzU32>,
333333
{
334334
Self::new_controller(i2c, sda_pin, scl_pin, freq.into(), resets, system_clock.into())
335335
}
336+
337+
$crate::paste::paste! {
338+
/// Configures the I2C peripheral to work in master mode
339+
///
340+
/// This function can be called without activating internal pull-ups on the I2C pins.
341+
/// It should only be used if external pull-ups are provided.
342+
pub fn [<$i2cX _with_external_pull_up>]<F, SystemF>(
343+
i2c: $I2CX,
344+
sda_pin: Sda,
345+
scl_pin: Scl,
346+
freq: F,
347+
resets: &mut RESETS,
348+
system_clock: SystemF) -> Self
349+
where
350+
F: Into<HertzU32>,
351+
Sda: ValidPinSda<$I2CX>,
352+
Scl: ValidPinScl<$I2CX>,
353+
SystemF: Into<HertzU32>,
354+
{
355+
Self::new_controller(i2c, sda_pin, scl_pin, freq.into(), resets, system_clock.into())
356+
}
357+
}
336358
}
337359
)+
338360
}

0 commit comments

Comments
 (0)