Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure that i2c pins have PullUp activated #708

Merged
merged 3 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -331,12 +331,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
Loading