Skip to content

Commit a2393df

Browse files
committed
Add alternate function open-drain pins
1 parent 8e77d06 commit a2393df

File tree

14 files changed

+1154
-328
lines changed

14 files changed

+1154
-328
lines changed

codegen/src/codegen/gpio.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ fn get_pin_reset_mode(pin: &gpio::Pin) -> Result<&'static str> {
134134
// Debug pins default to their debug function (AF0), everything else
135135
// defaults to floating input.
136136
let mode = match (pin.port()?, pin.number()?) {
137-
('A', 13) | ('A', 14) | ('A', 15) | ('B', 3) | ('B', 4) => "AF0",
137+
('A', 13) | ('A', 14) | ('A', 15) | ('B', 3) | ('B', 4) => "AF0<PushPull>",
138138
_ => "Input<Floating>",
139139
};
140140
Ok(mode)

examples/can.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,20 @@ fn main() -> ! {
3838
.freeze(&mut flash.acr);
3939

4040
// Configure CAN RX and TX pins (AF9)
41-
let can_rx = gpioa.pa11.into_af9(&mut gpioa.moder, &mut gpioa.afrh);
42-
let can_tx = gpioa.pa12.into_af9(&mut gpioa.moder, &mut gpioa.afrh);
41+
let rx = gpioa
42+
.pa11
43+
.into_af9_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh);
44+
let tx = gpioa
45+
.pa12
46+
.into_af9_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh);
4347

4448
// Initialize the CAN peripheral
45-
let can = Can::new(dp.CAN, can_rx, can_tx, &mut rcc.apb1);
49+
let can = Can::new(dp.CAN, rx, tx, &mut rcc.apb1);
4650

4751
// Uncomment the following line to enable CAN interrupts
4852
// can.listen(Event::Fifo0Fmp);
4953

50-
let (mut can_tx, mut rx0, _rx1) = can.split();
54+
let (mut tx, mut rx0, _rx1) = can.split();
5155

5256
let mut led0 = gpiob
5357
.pb15
@@ -68,7 +72,7 @@ fn main() -> ! {
6872

6973
let frame = CanFrame::new_data(CanId::BaseId(ID), &data);
7074

71-
block!(can_tx.transmit(&frame)).expect("Cannot send first CAN frame");
75+
block!(tx.transmit(&frame)).expect("Cannot send first CAN frame");
7276

7377
loop {
7478
let rcv_frame = block!(rx0.receive()).expect("Cannot receive CAN frame");
@@ -83,7 +87,7 @@ fn main() -> ! {
8387
let data: [u8; 1] = [counter];
8488
let frame = CanFrame::new_data(CanId::BaseId(ID), &data);
8589

86-
block!(can_tx.transmit(&frame)).expect("Cannot send CAN frame");
90+
block!(tx.transmit(&frame)).expect("Cannot send CAN frame");
8791
}
8892

8993
iwdg.feed();

examples/i2c_scanner.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//! Example of using I2C.
22
//! Scans available I2C devices on bus and print the result.
3-
//! Appropriate pull-up registers should be installed on I2C bus.
43
//! Target board: STM32F3DISCOVERY
54
65
#![no_std]
@@ -29,14 +28,19 @@ fn main() -> ! {
2928
let mut gpiob = dp.GPIOB.split(&mut rcc.ahb);
3029

3130
// Configure I2C1
32-
let pins = (
33-
gpiob.pb6.into_af4(&mut gpiob.moder, &mut gpiob.afrl), // SCL
34-
gpiob.pb7.into_af4(&mut gpiob.moder, &mut gpiob.afrl), // SDA
35-
);
36-
31+
let mut scl =
32+
gpiob
33+
.pb6
34+
.into_af4_open_drain(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrl);
35+
let mut sda =
36+
gpiob
37+
.pb7
38+
.into_af4_open_drain(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrl);
39+
scl.internal_pull_up(&mut gpiob.pupdr, true);
40+
sda.internal_pull_up(&mut gpiob.pupdr, true);
3741
let mut i2c = hal::i2c::I2c::new(
3842
dp.I2C1,
39-
pins,
43+
(scl, sda),
4044
100u32.kHz().try_into().unwrap(),
4145
clocks,
4246
&mut rcc.apb1,

examples/pwm.rs

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,40 @@ fn main() -> ! {
3232

3333
// Prep the pins we need in their correct alternate function
3434
let mut gpioa = dp.GPIOA.split(&mut rcc.ahb);
35-
let pa4 = gpioa.pa4.into_af2(&mut gpioa.moder, &mut gpioa.afrl);
36-
let pa6 = gpioa.pa6.into_af2(&mut gpioa.moder, &mut gpioa.afrl);
37-
let pa7 = gpioa.pa7.into_af2(&mut gpioa.moder, &mut gpioa.afrl);
35+
let pa4 = gpioa
36+
.pa4
37+
.into_af2_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrl);
38+
let pa6 = gpioa
39+
.pa6
40+
.into_af2_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrl);
41+
let pa7 = gpioa
42+
.pa7
43+
.into_af2_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrl);
3844

3945
let mut gpiob = dp.GPIOB.split(&mut rcc.ahb);
40-
let pb0 = gpiob.pb0.into_af2(&mut gpiob.moder, &mut gpiob.afrl);
41-
let pb1 = gpiob.pb1.into_af2(&mut gpiob.moder, &mut gpiob.afrl);
42-
let pb4 = gpiob.pb4.into_af2(&mut gpiob.moder, &mut gpiob.afrl);
43-
let pb5 = gpiob.pb5.into_af2(&mut gpiob.moder, &mut gpiob.afrl);
44-
let pb8 = gpiob.pb8.into_af1(&mut gpiob.moder, &mut gpiob.afrh);
45-
let pb10 = gpiob.pb10.into_af1(&mut gpiob.moder, &mut gpiob.afrh);
46+
let pb0 = gpiob
47+
.pb0
48+
.into_af2_push_pull(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrl);
49+
let pb1 = gpiob
50+
.pb1
51+
.into_af2_push_pull(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrl);
52+
let pb4 = gpiob
53+
.pb4
54+
.into_af2_push_pull(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrl);
55+
let pb5 = gpiob
56+
.pb5
57+
.into_af2_push_pull(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrl);
58+
let pb8 = gpiob
59+
.pb8
60+
.into_af1_push_pull(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrh);
61+
let pb10 = gpiob
62+
.pb10
63+
.into_af1_push_pull(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrh);
4664

4765
let mut gpioc = dp.GPIOC.split(&mut rcc.ahb);
48-
let pc10 = gpioc.pc10.into_af4(&mut gpioc.moder, &mut gpioc.afrh);
66+
let pc10 = gpioc
67+
.pc10
68+
.into_af4_push_pull(&mut gpioc.moder, &mut gpioc.otyper, &mut gpioc.afrh);
4969

5070
// TIM3
5171
//

examples/serial_dma.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,12 @@ fn main() -> ! {
2222
let mut gpioa = dp.GPIOA.split(&mut rcc.ahb);
2323

2424
let pins = (
25-
gpioa.pa9.into_af7(&mut gpioa.moder, &mut gpioa.afrh),
26-
gpioa.pa10.into_af7(&mut gpioa.moder, &mut gpioa.afrh),
25+
gpioa
26+
.pa9
27+
.into_af7_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh),
28+
gpioa
29+
.pa10
30+
.into_af7_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh),
2731
);
2832
let serial = Serial::usart1(dp.USART1, pins, 9600.Bd(), clocks, &mut rcc.apb2);
2933
let (tx, rx) = serial.split();

examples/spi.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,15 @@ fn main() -> ! {
3232
.freeze(&mut flash.acr);
3333

3434
// Configure pins for SPI
35-
let sck = gpioa.pa5.into_af5(&mut gpioa.moder, &mut gpioa.afrl);
36-
let miso = gpioa.pa6.into_af5(&mut gpioa.moder, &mut gpioa.afrl);
37-
let mosi = gpioa.pa7.into_af5(&mut gpioa.moder, &mut gpioa.afrl);
35+
let sck = gpioa
36+
.pa5
37+
.into_af5_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrl);
38+
let miso = gpioa
39+
.pa6
40+
.into_af5_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrl);
41+
let mosi = gpioa
42+
.pa7
43+
.into_af5_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrl);
3844

3945
let spi_mode = Mode {
4046
polarity: Polarity::IdleLow,

examples/usb_serial.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,11 @@ fn main() -> ! {
5353
usb_dp.set_low().ok();
5454
delay(clocks.sysclk().0 / 100);
5555

56-
let usb_dm = gpioa.pa11.into_af14(&mut gpioa.moder, &mut gpioa.afrh);
57-
let usb_dp = usb_dp.into_af14(&mut gpioa.moder, &mut gpioa.afrh);
56+
let usb_dm =
57+
gpioa
58+
.pa11
59+
.into_af14_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh);
60+
let usb_dp = usb_dp.into_af14_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh);
5861

5962
let usb = Peripheral {
6063
usb: dp.USB,

src/can.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
pub use embedded_hal_can::{self, Filter, Frame, Id, Receiver, Transmitter};
1515

1616
use crate::gpio::gpioa;
17-
use crate::gpio::AF9;
17+
use crate::gpio::{PushPull, AF9};
1818
use crate::rcc::APB1;
1919
use crate::stm32;
2020
use nb::{self, Error};
@@ -148,8 +148,8 @@ static FILTER_INDEX: AtomicU8 = AtomicU8::new(0);
148148
/// Controll Area Network (CAN) Peripheral
149149
pub struct Can {
150150
can: stm32::CAN,
151-
_rx: gpioa::PA11<AF9>,
152-
_tx: gpioa::PA12<AF9>,
151+
_rx: gpioa::PA11<AF9<PushPull>>,
152+
_tx: gpioa::PA12<AF9<PushPull>>,
153153
}
154154

155155
/// A CAN FIFO which is used to receive and buffer messages from the CAN
@@ -161,8 +161,8 @@ pub struct CanFifo {
161161
/// A CAN transmitter which is used to send messages to the CAN network.
162162
pub struct CanTransmitter {
163163
_can: stm32::CAN,
164-
_rx: gpioa::PA11<AF9>,
165-
_tx: gpioa::PA12<AF9>,
164+
_rx: gpioa::PA11<AF9<PushPull>>,
165+
_tx: gpioa::PA12<AF9<PushPull>>,
166166
}
167167

168168
// TODO Use wrapper type around interal pac definition
@@ -317,8 +317,8 @@ impl Can {
317317
/// Initialize the CAN peripheral using the options specified by `opts`.
318318
pub fn new_with_opts(
319319
can: stm32::CAN,
320-
rx: gpioa::PA11<AF9>,
321-
tx: gpioa::PA12<AF9>,
320+
rx: gpioa::PA11<AF9<PushPull>>,
321+
tx: gpioa::PA12<AF9<PushPull>>,
322322
apb1: &mut APB1,
323323
opts: CanOpts,
324324
) -> Can {
@@ -363,8 +363,8 @@ impl Can {
363363
/// Initialize the CAN Peripheral using default options from `CanOpts::default()`
364364
pub fn new(
365365
can: stm32::CAN,
366-
rx: gpioa::PA11<AF9>,
367-
tx: gpioa::PA12<AF9>,
366+
rx: gpioa::PA11<AF9<PushPull>>,
367+
tx: gpioa::PA12<AF9<PushPull>>,
368368
apb1: &mut APB1,
369369
) -> Self {
370370
Can::new_with_opts(can, rx, tx, apb1, CanOpts::default())
@@ -396,7 +396,13 @@ impl Can {
396396
}
397397

398398
/// Release owned peripherals
399-
pub fn free(self) -> (stm32::CAN, gpioa::PA11<AF9>, gpioa::PA12<AF9>) {
399+
pub fn free(
400+
self,
401+
) -> (
402+
stm32::CAN,
403+
gpioa::PA11<AF9<PushPull>>,
404+
gpioa::PA12<AF9<PushPull>>,
405+
) {
400406
(self.can, self._rx, self._tx)
401407
}
402408
}

0 commit comments

Comments
 (0)