Skip to content

Commit ddab525

Browse files
committed
Add all I2C's available according to reference manuals
1 parent 5d5ce6f commit ddab525

File tree

2 files changed

+60
-24
lines changed

2 files changed

+60
-24
lines changed

src/i2c.rs

Lines changed: 50 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@
33
//! as of 2021-02-25.
44
55
use crate::hal::blocking::i2c::{Read, Write, WriteRead};
6-
use crate::pac::{i2c1, I2C1, I2C2};
6+
use crate::pac::{i2c1, I2C1, I2C2, I2C3, I2C4};
77
use crate::rcc::{Clocks, APB1R1};
8-
#[cfg(any(feature = "stm32l4x5", feature = "stm32l4x6"))]
9-
use crate::stm32::I2C3;
108
use crate::time::Hertz;
119
use cast::{u16, u8};
1210
use core::ops::Deref;
@@ -121,7 +119,7 @@ impl State {
121119
}
122120

123121
macro_rules! hal {
124-
($i2c_type: ident, $i2cX: ident, $i2cXen: ident, $i2cXrst: ident) => {
122+
($i2c_type: ident, $enr: ident, $rstr: ident, $i2cX: ident, $i2cXen: ident, $i2cXrst: ident) => {
125123
impl<SCL, SDA> I2c<$i2c_type, (SCL, SDA)> {
126124
pub fn $i2cX<F>(
127125
i2c: $i2c_type,
@@ -135,20 +133,25 @@ macro_rules! hal {
135133
SCL: SclPin<$i2c_type>,
136134
SDA: SdaPin<$i2c_type>,
137135
{
138-
apb1.enr().modify(|_, w| w.$i2cXen().set_bit());
139-
apb1.rstr().modify(|_, w| w.$i2cXrst().set_bit());
140-
apb1.rstr().modify(|_, w| w.$i2cXrst().clear_bit());
136+
apb1.$enr().modify(|_, w| w.$i2cXen().set_bit());
137+
apb1.$rstr().modify(|_, w| w.$i2cXrst().set_bit());
138+
apb1.$rstr().modify(|_, w| w.$i2cXrst().clear_bit());
141139
Self::new(i2c, pins, freq, clocks)
142140
}
143141
}
144142
};
145143
}
146144

147-
hal!(I2C1, i2c1, i2c1en, i2c1rst);
148-
hal!(I2C2, i2c2, i2c2en, i2c2rst);
145+
hal!(I2C1, enr, rstr, i2c1, i2c1en, i2c1rst);
146+
hal!(I2C2, enr, rstr, i2c2, i2c2en, i2c2rst);
147+
148+
#[cfg(any(feature = "stm32l4x3", feature = "stm32l4x5", feature = "stm32l4x6"))]
149+
hal!(I2C3, enr, rstr, i2c3, i2c3en, i2c3rst);
149150

151+
// Warning: available on stm32l4x5 according to reference manual,
152+
// but stm32l475rc (only one available) does not have this peripheral listed,
150153
#[cfg(any(feature = "stm32l4x5", feature = "stm32l4x6"))]
151-
hal!(I2C3, i2c3, i2c3en, i2c3rst);
154+
hal!(I2C4, enr2, rstr2, i2c4, i2c4en, i2c4rst);
152155

153156
impl<SCL, SDA, I2C> I2c<I2C, (SCL, SDA)>
154157
where
@@ -472,8 +475,8 @@ where
472475
}
473476
}
474477

475-
// All parts have I2C1 on alternate function 4 of
476-
// pins PA9/PB6 as SCL and PA10/PB7 as SDA, etc.
478+
/// **Warning**: A9 and A10 in AF4 don't have this function on stm32l471qe,
479+
/// but do on stm32l431cb and stm32l451cc
477480
mod i2c_pins_default {
478481
use super::{I2C1, I2C2};
479482
use crate::gpio::gpioa::{PA10, PA9};
@@ -489,23 +492,20 @@ mod i2c_pins_default {
489492

490493
#[cfg(any(feature = "stm32l4x1", feature = "stm32l4x2", feature = "stm32l4x6"))]
491494
mod i2c_pins_pb8_pb9 {
492-
use super::I2C1;
493-
use crate::gpio::gpiob::{PB8, PB9};
495+
use super::{I2C1, I2C2};
496+
use crate::gpio::gpiob::{PB13, PB14, PB8, PB9};
494497
use crate::gpio::{Alternate, OpenDrain, Output, AF4};
495498

496499
pins!(I2C1, AF4, SCL: [PB8], SDA: [PB9]);
497-
}
498-
499-
#[cfg(any(feature = "stm32l4x1", feature = "stm32l4x6"))]
500-
mod i2c_pins_pb13_pb14 {
501-
use super::I2C2;
502-
use crate::gpio::gpiob::{PB13, PB14};
503-
use crate::gpio::{Alternate, OpenDrain, Output, AF4};
504-
505500
pins!(I2C2, AF4, SCL: [PB13], SDA: [PB14]);
506501
}
507502

508-
#[cfg(any(feature = "stm32l4x3", feature = "stm32l4x5", feature = "stm32l4x6"))]
503+
#[cfg(any(
504+
feature = "stm32l4x1",
505+
feature = "stm32l4x3",
506+
feature = "stm32l4x5",
507+
feature = "stm32l4x6"
508+
))]
509509
mod i2c_pins_pbc0_pc1 {
510510
use super::I2C3;
511511
use crate::gpio::gpioc::{PC0, PC1};
@@ -514,7 +514,17 @@ mod i2c_pins_pbc0_pc1 {
514514
pins!(I2C3, AF4, SCL: [PC0], SDA: [PC1]);
515515
}
516516

517-
#[cfg(feature = "stm32l4x3")]
517+
/// **Warning**: PA7 and PB4 don't have this function on stm32l486jg and stm32l476je,
518+
/// but do on stm32l496ae and stm32l4a6ag
519+
///
520+
/// **Warning**: PA7 and PB4 don't have this function on stm32l471qe and stm32l475rc,
521+
/// but do on stm32l431cb and stm32l451cc
522+
#[cfg(any(
523+
feature = "stm32l4x1",
524+
feature = "stm32l4x2",
525+
feature = "stm32l4x3",
526+
feature = "stm32l4x6"
527+
))]
518528
mod i2c_pins_pa7_pb4 {
519529
use super::I2C3;
520530
use crate::gpio::gpioa::PA7;
@@ -524,3 +534,19 @@ mod i2c_pins_pa7_pb4 {
524534
// These pins aren't as nicely consistent
525535
pins!(I2C3, AF4, SCL: [PA7], SDA: [PB4]);
526536
}
537+
538+
/// **Warning**: these pins don't have this function on stm32l486jg and stm32l476je,
539+
/// but do on stm32l496ae and stm32l4a6ag
540+
#[cfg(feature = "stm32l4x6")]
541+
mod i2c_pins_pd12_pd13_pf0_pf1_pf14_pf15 {
542+
use super::{I2C2, I2C4};
543+
use crate::gpio::gpiod::{PD12, PD13};
544+
use crate::gpio::gpiof::{PF0, PF1, PF14, PF15};
545+
use crate::gpio::{Alternate, OpenDrain, Output, AF4};
546+
547+
// SCL and SDA are "reversed", correct according to reference manual
548+
pins!(I2C2, AF4, SCL: [PF1], SDA: [PF0]);
549+
550+
pins!(I2C4, AF4, SCL: [PD12], SDA: [PD13]);
551+
pins!(I2C4, AF4, SCL: [PF14], SDA: [PF15]);
552+
}

src/rcc.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,16 @@ impl APB1R1 {
255255
// NOTE(unsafe) this proxy grants exclusive access to this register
256256
unsafe { &(*RCC::ptr()).apb1rstr1 }
257257
}
258+
259+
pub(crate) fn enr2(&mut self) -> &rcc::APB1ENR2 {
260+
// NOTE(unsafe) this proxy grants exclusive access to this register
261+
unsafe { &(*RCC::ptr()).apb1enr2 }
262+
}
263+
264+
pub(crate) fn rstr2(&mut self) -> &rcc::APB1RSTR2 {
265+
// NOTE(unsafe) this proxy grants exclusive access to this register
266+
unsafe { &(*RCC::ptr()).apb1rstr2 }
267+
}
258268
}
259269

260270
/// Advanced Peripheral Bus 1 (APB1) register 2 registers

0 commit comments

Comments
 (0)