diff options
| author | erwin <[email protected]> | 2025-08-18 12:16:30 +0200 |
|---|---|---|
| committer | erwin <[email protected]> | 2025-08-18 14:39:18 +0200 |
| commit | bbc3e49c585a2bf58091add9aeac3628d9044297 (patch) | |
| tree | 300bd5a2e8236b5233760bd4e3d4c7ed0bdfe50d /embassy-rp/src | |
| parent | 39e75bb02f0c566b08ba75133c9e67c8d71b5b01 (diff) | |
Add configurable internal pullups for rp i2c
- Example updated to demonstrate enabling internal pullups
- Add `sda_pullup` and `scl_pullup` fields to I2C Config
Diffstat (limited to 'embassy-rp/src')
| -rw-r--r-- | embassy-rp/src/i2c.rs | 26 | ||||
| -rw-r--r-- | embassy-rp/src/i2c_slave.rs | 16 |
2 files changed, 33 insertions, 9 deletions
diff --git a/embassy-rp/src/i2c.rs b/embassy-rp/src/i2c.rs index 172193a07..089c2d080 100644 --- a/embassy-rp/src/i2c.rs +++ b/embassy-rp/src/i2c.rs | |||
| @@ -64,14 +64,26 @@ pub enum ConfigError { | |||
| 64 | pub struct Config { | 64 | pub struct Config { |
| 65 | /// Frequency. | 65 | /// Frequency. |
| 66 | pub frequency: u32, | 66 | pub frequency: u32, |
| 67 | /// Enable internal pullup on SDA. | ||
| 68 | /// | ||
| 69 | /// Using external pullup resistors is recommended for I2C. If you do | ||
| 70 | /// have external pullups you should not enable this. | ||
| 71 | pub sda_pullup: bool, | ||
| 72 | /// Enable internal pullup on SCL. | ||
| 73 | /// | ||
| 74 | /// Using external pullup resistors is recommended for I2C. If you do | ||
| 75 | /// have external pullups you should not enable this. | ||
| 76 | pub scl_pullup: bool, | ||
| 67 | } | 77 | } |
| 68 | |||
| 69 | impl Default for Config { | 78 | impl Default for Config { |
| 70 | fn default() -> Self { | 79 | fn default() -> Self { |
| 71 | Self { frequency: 100_000 } | 80 | Self { |
| 81 | frequency: 100_000, | ||
| 82 | sda_pullup: false, | ||
| 83 | scl_pullup: false, | ||
| 84 | } | ||
| 72 | } | 85 | } |
| 73 | } | 86 | } |
| 74 | |||
| 75 | /// Size of I2C FIFO. | 87 | /// Size of I2C FIFO. |
| 76 | pub const FIFO_SIZE: u8 = 16; | 88 | pub const FIFO_SIZE: u8 = 16; |
| 77 | 89 | ||
| @@ -359,7 +371,7 @@ impl<T: Instance> interrupt::typelevel::Handler<T::Interrupt> for InterruptHandl | |||
| 359 | } | 371 | } |
| 360 | } | 372 | } |
| 361 | 373 | ||
| 362 | pub(crate) fn set_up_i2c_pin<P, T>(pin: &P) | 374 | pub(crate) fn set_up_i2c_pin<P, T>(pin: &P, pullup: bool) |
| 363 | where | 375 | where |
| 364 | P: core::ops::Deref<Target = T>, | 376 | P: core::ops::Deref<Target = T>, |
| 365 | T: crate::gpio::Pin, | 377 | T: crate::gpio::Pin, |
| @@ -372,7 +384,7 @@ where | |||
| 372 | w.set_slewfast(false); | 384 | w.set_slewfast(false); |
| 373 | w.set_ie(true); | 385 | w.set_ie(true); |
| 374 | w.set_od(false); | 386 | w.set_od(false); |
| 375 | w.set_pue(true); | 387 | w.set_pue(pullup); |
| 376 | w.set_pde(false); | 388 | w.set_pde(false); |
| 377 | }); | 389 | }); |
| 378 | } | 390 | } |
| @@ -384,8 +396,8 @@ impl<'d, T: Instance + 'd, M: Mode> I2c<'d, T, M> { | |||
| 384 | crate::reset::unreset_wait(reset); | 396 | crate::reset::unreset_wait(reset); |
| 385 | 397 | ||
| 386 | // Configure SCL & SDA pins | 398 | // Configure SCL & SDA pins |
| 387 | set_up_i2c_pin(&scl); | 399 | set_up_i2c_pin(&scl, config.scl_pullup); |
| 388 | set_up_i2c_pin(&sda); | 400 | set_up_i2c_pin(&sda, config.sda_pullup); |
| 389 | 401 | ||
| 390 | let mut me = Self { phantom: PhantomData }; | 402 | let mut me = Self { phantom: PhantomData }; |
| 391 | 403 | ||
diff --git a/embassy-rp/src/i2c_slave.rs b/embassy-rp/src/i2c_slave.rs index 7bc14511d..bfee0c45b 100644 --- a/embassy-rp/src/i2c_slave.rs +++ b/embassy-rp/src/i2c_slave.rs | |||
| @@ -65,6 +65,16 @@ pub struct Config { | |||
| 65 | pub addr: u16, | 65 | pub addr: u16, |
| 66 | /// Control if the peripheral should ack to and report general calls. | 66 | /// Control if the peripheral should ack to and report general calls. |
| 67 | pub general_call: bool, | 67 | pub general_call: bool, |
| 68 | /// Enable internal pullup on SDA. | ||
| 69 | /// | ||
| 70 | /// Using external pullup resistors is recommended for I2C. If you do | ||
| 71 | /// have external pullups you should not enable this. | ||
| 72 | pub sda_pullup: bool, | ||
| 73 | /// Enable internal pullup on SCL. | ||
| 74 | /// | ||
| 75 | /// Using external pullup resistors is recommended for I2C. If you do | ||
| 76 | /// have external pullups you should not enable this. | ||
| 77 | pub scl_pullup: bool, | ||
| 68 | } | 78 | } |
| 69 | 79 | ||
| 70 | impl Default for Config { | 80 | impl Default for Config { |
| @@ -72,6 +82,8 @@ impl Default for Config { | |||
| 72 | Self { | 82 | Self { |
| 73 | addr: 0x55, | 83 | addr: 0x55, |
| 74 | general_call: true, | 84 | general_call: true, |
| 85 | sda_pullup: false, | ||
| 86 | scl_pullup: false, | ||
| 75 | } | 87 | } |
| 76 | } | 88 | } |
| 77 | } | 89 | } |
| @@ -95,8 +107,8 @@ impl<'d, T: Instance> I2cSlave<'d, T> { | |||
| 95 | assert!(config.addr != 0); | 107 | assert!(config.addr != 0); |
| 96 | 108 | ||
| 97 | // Configure SCL & SDA pins | 109 | // Configure SCL & SDA pins |
| 98 | set_up_i2c_pin(&scl); | 110 | set_up_i2c_pin(&scl, config.scl_pullup); |
| 99 | set_up_i2c_pin(&sda); | 111 | set_up_i2c_pin(&sda, config.sda_pullup); |
| 100 | 112 | ||
| 101 | let mut ret = Self { | 113 | let mut ret = Self { |
| 102 | phantom: PhantomData, | 114 | phantom: PhantomData, |
