From 6c6158dcb72211b2badf1830df9a98f49f1a4a5c Mon Sep 17 00:00:00 2001 From: Jan Niehusmann Date: Wed, 25 Oct 2023 17:43:03 +0000 Subject: [PATCH 1/3] Ensure that i2c pins have PullUp activated 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. --- rp2040-hal/examples/i2c.rs | 6 +++--- rp2040-hal/src/i2c.rs | 26 +++++++++++++++++++++++--- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/rp2040-hal/examples/i2c.rs b/rp2040-hal/examples/i2c.rs index 94591b39e..e71d574f7 100644 --- a/rp2040-hal/examples/i2c.rs +++ b/rp2040-hal/examples/i2c.rs @@ -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, }; @@ -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 diff --git a/rp2040-hal/src/i2c.rs b/rp2040-hal/src/i2c.rs index fa1299f7e..d964374f2 100644 --- a/rp2040-hal/src/i2c.rs +++ b/rp2040-hal/src/i2c.rs @@ -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, @@ -331,12 +331,32 @@ macro_rules! hal { system_clock: SystemF) -> Self where F: Into, - Sda: ValidPinSda<$I2CX>, - Scl: ValidPinScl<$I2CX>, + Sda: ValidPinSda<$I2CX> + AnyPin, + Scl: ValidPinScl<$I2CX> + AnyPin, SystemF: Into, { 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>]( + i2c: $I2CX, + sda_pin: Sda, + scl_pin: Scl, + freq: F, + resets: &mut RESETS, + system_clock: SystemF) -> Self + where + F: Into, + Sda: ValidPinSda<$I2CX>, + Scl: ValidPinScl<$I2CX>, + SystemF: Into, + { + Self::new_controller(i2c, sda_pin, scl_pin, freq.into(), resets, system_clock.into()) + } + } } )+ } From bb8a9fd7cede768a8c11a316e505fe76be4929e5 Mon Sep 17 00:00:00 2001 From: Jan Niehusmann Date: Wed, 25 Oct 2023 17:53:00 +0000 Subject: [PATCH 2/3] Fix test case in doc comment --- rp2040-hal/src/i2c.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rp2040-hal/src/i2c.rs b/rp2040-hal/src/i2c.rs index d964374f2..19e60591a 100644 --- a/rp2040-hal/src/i2c.rs +++ b/rp2040-hal/src/i2c.rs @@ -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(), From a965f1c22ce53472545aa9f1731aaf1955077e66 Mon Sep 17 00:00:00 2001 From: Jan Niehusmann Date: Wed, 3 Jan 2024 13:26:10 +0000 Subject: [PATCH 3/3] Rename unchecked constructors --- rp2040-hal/src/i2c.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/rp2040-hal/src/i2c.rs b/rp2040-hal/src/i2c.rs index 19e60591a..9582299a7 100644 --- a/rp2040-hal/src/i2c.rs +++ b/rp2040-hal/src/i2c.rs @@ -340,8 +340,10 @@ macro_rules! hal { $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>]( + /// + /// 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>]( i2c: $I2CX, sda_pin: Sda, scl_pin: Scl,