Skip to content

Commit 5563423

Browse files
committed
update i2c pins
1 parent 87a743d commit 5563423

File tree

5 files changed

+80
-73
lines changed

5 files changed

+80
-73
lines changed

examples/i2c.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#![deny(warnings)]
2+
#![deny(unsafe_code)]
3+
#![no_main]
4+
#![no_std]
5+
6+
extern crate cortex_m;
7+
extern crate cortex_m_rt as rt;
8+
extern crate panic_halt;
9+
extern crate stm32c0xx_hal as hal;
10+
11+
use hal::i2c::Config;
12+
use hal::prelude::*;
13+
use hal::rcc;
14+
use hal::stm32;
15+
use rt::entry;
16+
17+
#[entry]
18+
fn main() -> ! {
19+
let dp = stm32::Peripherals::take().expect("cannot take peripherals");
20+
let mut rcc = dp.RCC.freeze(rcc::Config::hsi(rcc::Prescaler::NotDivided));
21+
let mut delay = dp.TIM3.delay(&mut rcc);
22+
23+
let gpiob = dp.GPIOB.split(&mut rcc);
24+
let sda = gpiob.pb9.into_open_drain_output_in_state(PinState::High);
25+
let scl = gpiob.pb8.into_open_drain_output_in_state(PinState::High);
26+
27+
let mut i2c = dp.I2C.i2c(sda, scl, Config::new(400.kHz()), &mut rcc);
28+
29+
i2c.write(0x2a, &[0x80, 0xff]).unwrap();
30+
i2c.write(0x2a, &[0x01, 0x04, 0x00, 0x00]).unwrap();
31+
32+
let mut buf: [u8; 4] = [0, 0, 0, 0];
33+
loop {
34+
delay.delay(100.millis());
35+
buf[3] = (buf[3] + 1) % 24;
36+
i2c.write(0x2b, &buf).unwrap();
37+
}
38+
}

src/exti.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::gpio::SignalEdge;
33
use crate::stm32::EXTI;
44

55
/// EXTI trigger event
6-
#[derive(Eq, PartialEq, PartialOrd, Clone, Copy)]
6+
#[derive(Eq, PartialEq, PartialOrd, Clone, Copy, Debug)]
77
pub enum Event {
88
GPIO0 = 0,
99
GPIO1 = 1,
@@ -21,16 +21,9 @@ pub enum Event {
2121
GPIO13 = 13,
2222
GPIO14 = 14,
2323
GPIO15 = 15,
24-
PVD = 16,
2524
RTC = 19,
26-
TAMP = 21,
2725
I2C1 = 23,
2826
USART1 = 25,
29-
USART2 = 26,
30-
CEC = 27,
31-
LPUART1 = 28,
32-
LPTIM1 = 29,
33-
LPTIM2 = 30,
3427
LSE_CSS = 31,
3528
}
3629

src/i2c/blocking.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,13 @@ macro_rules! busy_wait {
112112

113113
macro_rules! i2c {
114114
($I2CX:ident, $i2cx:ident,
115-
sda: [ $($PSDA:ty,)+ ],
116-
scl: [ $($PSCL:ty,)+ ],
115+
sda: [ $(($PSDA:ty, $AFSDA:expr),)+ ],
116+
scl: [ $(($PSCL:ty, $AFSCL:expr),)+ ],
117117
) => {
118118
$(
119119
impl SDAPin<$I2CX> for $PSDA {
120120
fn setup(&self) {
121-
self.set_alt_mode(AltFunction::AF6)
121+
self.set_alt_mode($AFSDA)
122122
}
123123

124124
fn release(self) -> Self {
@@ -130,7 +130,7 @@ macro_rules! i2c {
130130
$(
131131
impl SCLPin<$I2CX> for $PSCL {
132132
fn setup(&self) {
133-
self.set_alt_mode(AltFunction::AF6)
133+
self.set_alt_mode($AFSCL)
134134
}
135135

136136
fn release(self) -> Self {
@@ -247,7 +247,6 @@ macro_rules! i2c {
247247
snd_buffer: &[u8],
248248
rcv_buffer: &mut [u8],
249249
) -> Result<(), Self::Error> {
250-
// TODO support transfers of more than 255 bytes
251250
let sndlen = snd_buffer.len();
252251
let rcvlen = rcv_buffer.len();
253252
assert!(sndlen < 256 && sndlen > 0);
@@ -368,7 +367,6 @@ macro_rules! i2c {
368367

369368
fn read(&mut self, addr: u8, bytes: &mut [u8]) -> Result<(), Self::Error> {
370369
let buflen = bytes.len();
371-
// TODO support transfers of more than 255 bytes
372370
assert!(buflen < 256 && buflen > 0);
373371

374372
// Wait for any previous address sequence to end automatically.
@@ -443,7 +441,6 @@ macro_rules! i2c {
443441

444442
fn slave_write(&mut self, bytes: &[u8]) -> Result<(), Error> {
445443
let buflen = bytes.len();
446-
// TODO support transfers of more than 255 bytes
447444
assert!(buflen < 256 && buflen > 0);
448445

449446
// Set the nbytes and prepare to send bytes into `buffer`.
@@ -476,7 +473,6 @@ macro_rules! i2c {
476473

477474
fn slave_read(&mut self, bytes: &mut [u8]) -> Result<(), Error> {
478475
let buflen = bytes.len();
479-
// TODO support transfers of more than 255 bytes
480476
assert!(buflen < 256 && buflen > 0);
481477

482478
// Set the nbytes START and prepare to receive bytes into `buffer`.
@@ -513,13 +509,15 @@ i2c!(
513509
I2C,
514510
i2c1,
515511
sda: [
516-
PA10<Output<OpenDrain>>,
517-
PB7<Output<OpenDrain>>,
518-
PB9<Output<OpenDrain>>,
512+
(PA10<Output<OpenDrain>>, AltFunction::AF6),
513+
(PB7<Output<OpenDrain>>, AltFunction::AF6),
514+
(PB9<Output<OpenDrain>>, AltFunction::AF6),
515+
(PC14<Output<OpenDrain>>, AltFunction::AF14),
519516
],
520517
scl: [
521-
PA9<Output<OpenDrain>>,
522-
PB6<Output<OpenDrain>>,
523-
PB8<Output<OpenDrain>>,
518+
(PA9<Output<OpenDrain>>, AltFunction::AF6),
519+
(PB6<Output<OpenDrain>>, AltFunction::AF6),
520+
(PB8<Output<OpenDrain>>, AltFunction::AF6),
521+
(PB7<Output<OpenDrain>>, AltFunction::AF14),
524522
],
525523
);

src/i2c/nonblocking.rs

Lines changed: 26 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::gpio::{AltFunction, OpenDrain, Output};
44
use crate::i2c::config::Config;
55
use crate::i2c::{Error, I2c, I2cDirection, I2cExt, I2cResult, SCLPin, SDAPin};
66
use crate::rcc::*;
7-
use crate::stm32::{I2C1, I2C2};
7+
use crate::stm32::I2C;
88
use nb::Error::{Other, WouldBlock};
99

1010
pub trait I2cControl {
@@ -101,13 +101,13 @@ macro_rules! flush_rxdr {
101101

102102
macro_rules! i2c {
103103
($I2CX:ident, $i2cx:ident,
104-
sda: [ $($PSDA:ty,)+ ],
105-
scl: [ $($PSCL:ty,)+ ],
104+
sda: [ $(($PSDA:ty, $AFSDA:expr),)+ ],
105+
scl: [ $(($PSCL:ty, $AFSCL:expr),)+ ],
106106
) => {
107107
$(
108108
impl SDAPin<$I2CX> for $PSDA {
109109
fn setup(&self) {
110-
self.set_alt_mode(AltFunction::AF6)
110+
self.set_alt_mode($AFSDA)
111111
}
112112

113113
fn release(self) -> Self {
@@ -119,7 +119,7 @@ macro_rules! i2c {
119119
$(
120120
impl SCLPin<$I2CX> for $PSCL {
121121
fn setup(&self) {
122-
self.set_alt_mode(AltFunction::AF6)
122+
self.set_alt_mode($AFSCL)
123123
}
124124

125125
fn release(self) -> Self {
@@ -175,22 +175,11 @@ macro_rules! i2c {
175175
});
176176

177177
if config.slave_address_1 > 0 {
178-
if config.address_11bits {
179-
i2c.oar1.write(|w| unsafe {
180-
let addr = config.slave_address_1;
181-
w.oa1_0() .bit(addr&0x1 == 0x1)
182-
.oa1_7_1().bits( ((addr >> 1) & 0x7F )as u8)
183-
.oa1_8_9().bits( ((addr >> 8) & 0x3 )as u8)
184-
.oa1mode().set_bit()
185-
.oa1en().set_bit()
186-
});
187-
}else {
188-
i2c.oar1.write(|w| unsafe {
189-
w.oa1_7_1().bits(config.slave_address_1 as u8)
190-
.oa1mode().clear_bit()
191-
.oa1en().set_bit()
192-
});
193-
}
178+
i2c.oar1.write(|w| unsafe {
179+
w.oa1().bits(config.slave_address_1)
180+
.oa1mode().bit(config.address_11bits)
181+
.oa1en().set_bit()
182+
});
194183
// Enable acknowlidge control
195184
i2c.cr1.modify(|_, w| w.sbc().set_bit() );
196185
}
@@ -202,7 +191,7 @@ macro_rules! i2c {
202191
.oa2en().set_bit()
203192
});
204193
// Enable acknowlidge control
205-
i2c.cr1.modify(|_, w| w.sbc().set_bit() );
194+
i2c.cr1.modify(|_, w| w.sbc().set_bit() );
206195
}
207196

208197
// Enable pins
@@ -268,11 +257,11 @@ macro_rules! i2c {
268257
self.errors += 1;
269258
self.watchdog = 0;
270259
// Disable I2C processing, resetting all hardware state machines
271-
self.i2c.cr1.modify(|_, w| unsafe {w.pe().clear_bit() } );
260+
self.i2c.cr1.modify(|_, w| w.pe().clear_bit());
272261
// force enough wait states for the pe clear
273262
let _ = self.i2c.cr1.read();
274263
// Enable the I2C processing again
275-
self.i2c.cr1.modify(|_, w| unsafe {w.pe().set_bit() });
264+
self.i2c.cr1.modify(|_, w| w.pe().set_bit());
276265
},
277266
_ => {self.watchdog -= 1},
278267
}
@@ -372,14 +361,14 @@ macro_rules! i2c {
372361
return Err( WouldBlock)
373362
} else
374363
if self.index == 0 {
375-
self.i2c.cr2.modify(|_, w| unsafe {
364+
self.i2c.cr2.modify(|_, w| {
376365
w.stop().set_bit()
377366
});
378367
self.errors += 1;
379368
return Err( Other(Error::Nack))
380369
} else
381370
{
382-
self.i2c.cr2.modify(|_, w| unsafe {
371+
self.i2c.cr2.modify(|_, w| {
383372
w.stop().set_bit()
384373
});
385374
self.errors += 1;
@@ -555,20 +544,19 @@ macro_rules! i2c {
555544

556545
fn set_address(&mut self, address:u16) {
557546
self.i2c.oar1.write(|w| unsafe {
558-
w.oa1_7_1().bits(address as u8)
547+
w.oa1().bits(address as _)
559548
.oa1en().clear_bit()
560549
});
561550
// set the 7 bits address
562551
self.i2c.oar1.write(|w| unsafe {
563-
w.oa1_7_1().bits(address as u8)
552+
w.oa1().bits(address as _)
564553
.oa1mode().clear_bit()
565554
.oa1en().set_bit()
566555
});
567556
}
568557

569558
fn slave_write(&mut self, bytes: &[u8]) -> Result<(), Error> {
570559
let buflen = bytes.len();
571-
// TODO support transfers of more than 255 bytes
572560
assert!(buflen < 256 && buflen > 0);
573561

574562
self.length = buflen;
@@ -601,31 +589,18 @@ macro_rules! i2c {
601589
}
602590

603591
i2c!(
604-
I2C1,
592+
I2C,
605593
i2c1,
606594
sda: [
607-
PA10<Output<OpenDrain>>,
608-
PB7<Output<OpenDrain>>,
609-
PB9<Output<OpenDrain>>,
610-
],
611-
scl: [
612-
PA9<Output<OpenDrain>>,
613-
PB6<Output<OpenDrain>>,
614-
PB8<Output<OpenDrain>>,
615-
],
616-
);
617-
618-
i2c!(
619-
I2C2,
620-
i2c2,
621-
sda: [
622-
PA12<Output<OpenDrain>>,
623-
PB11<Output<OpenDrain>>,
624-
PB14<Output<OpenDrain>>,
595+
(PA10<Output<OpenDrain>>, AltFunction::AF6),
596+
(PB7<Output<OpenDrain>>, AltFunction::AF6),
597+
(PB9<Output<OpenDrain>>, AltFunction::AF6),
598+
(PC14<Output<OpenDrain>>, AltFunction::AF14),
625599
],
626600
scl: [
627-
PA11<Output<OpenDrain>>,
628-
PB10<Output<OpenDrain>>,
629-
PB13<Output<OpenDrain>>,
601+
(PA9<Output<OpenDrain>>, AltFunction::AF6),
602+
(PB6<Output<OpenDrain>>, AltFunction::AF6),
603+
(PB8<Output<OpenDrain>>, AltFunction::AF6),
604+
(PB7<Output<OpenDrain>>, AltFunction::AF14),
630605
],
631606
);

src/prelude.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ pub use crate::analog::adc::AdcExt as _;
22
pub use crate::crc::CrcExt as _;
33
pub use crate::exti::ExtiExt as _;
44
pub use crate::gpio::GpioExt as _;
5+
#[cfg(feature = "i2c-nonblocking")]
6+
pub use crate::i2c::nonblocking::I2cSlave;
7+
#[cfg(feature = "i2c-blocking")]
58
pub use crate::i2c::blocking::I2cSlave;
69
pub use crate::i2c::I2cExt as _;
710
pub use crate::power::PowerExt as _;

0 commit comments

Comments
 (0)