Skip to content

Commit 97c5591

Browse files
committed
Utilize LDREX/STREX in GPIO configure functions
1 parent 51f2ffc commit 97c5591

26 files changed

+767
-957
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
2020
- `RccBus`, `Enable`, `Reset` traits and implementations for peripherals ([#299])
2121
- Support cortex-m-rt `v0.7.0` but still allow `v0.6.13` ([#283])
2222
- Make timer `InterruptTypes` fields public to be useful. ([#304])
23+
- Implement `into_xxx` methods for erased pins ([#213])
2324

2425
### Fixed
2526

@@ -36,6 +37,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
3637
- Update `stm32f3` pac to v0.14.0 ([#282])
3738
- Remove the `bxcan` re-export. ([#304])
3839
- The MSRV was bumped to 1.59 ([#213])
40+
- GPIO configuration functions no longer require registers as arguments ([#213])
3941

4042
## [v0.8.2] - 2021-12-14
4143

README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,9 @@ fn main() -> ! {
120120
let dp = pac::Peripherals::take().unwrap();
121121

122122
let mut rcc = dp.RCC.constrain();
123-
let mut gpioe = dp.GPIOE.split(&mut rcc.ahb);
123+
let gpioe = dp.GPIOE.split(&mut rcc.ahb);
124124

125-
let mut led = gpioe
126-
.pe13
127-
.into_push_pull_output(&mut gpioe.moder, &mut gpioe.otyper);
125+
let mut led = gpioe.pe13.into_push_pull_output();
128126

129127
loop {
130128
led.toggle().unwrap();

codegen/src/codegen/gpio.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,11 @@ fn get_port_pac_module(port: &Port, feature: &str) -> &'static str {
131131
fn gen_pin(pin: &gpio::Pin) -> Result<()> {
132132
let nr = pin.number()?;
133133
let reset_mode = get_pin_reset_mode(pin)?;
134-
let afr = if nr < 8 { 'L' } else { 'H' };
135134
let af_numbers = get_pin_af_numbers(pin)?;
136135

137136
println!(
138-
" {} => {{ reset: {}, afr: {}, af: {:?} }},",
139-
nr, reset_mode, afr, af_numbers,
137+
" {} => {{ reset: {}, af: {:?} }},",
138+
nr, reset_mode, af_numbers,
140139
);
141140

142141
Ok(())

examples/adc.rs

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

3333
// Set up pin PA0 as analog pin.
3434
// This pin is connected to the user button on the stm32f3discovery board.
35-
let mut gpioa = dp.GPIOA.split(&mut rcc.ahb);
36-
let mut adc1_in1_pin = gpioa.pa0.into_analog(&mut gpioa.moder, &mut gpioa.pupdr);
35+
let gpio_a = dp.GPIOA.split(&mut rcc.ahb);
36+
let mut adc1_in1_pin = gpio_a.pa0.into_analog();
3737

3838
// Be aware that the values in the table below depend on the input of VREF.
3939
// To have a stable VREF input, put a condensator and a volt limiting diode in front of it.

examples/can.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ fn main() -> ! {
2828

2929
let mut flash = dp.FLASH.constrain();
3030
let mut rcc = dp.RCC.constrain();
31-
let mut gpiob = dp.GPIOB.split(&mut rcc.ahb);
32-
let mut gpioa = dp.GPIOA.split(&mut rcc.ahb);
31+
let gpiob = dp.GPIOB.split(&mut rcc.ahb);
32+
let gpioa = dp.GPIOA.split(&mut rcc.ahb);
3333

3434
let _clocks = rcc
3535
.cfgr
@@ -41,12 +41,8 @@ fn main() -> ! {
4141
.freeze(&mut flash.acr);
4242

4343
// Configure CAN RX and TX pins (AF9)
44-
let rx = gpioa
45-
.pa11
46-
.into_af9_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh);
47-
let tx = gpioa
48-
.pa12
49-
.into_af9_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh);
44+
let rx = gpioa.pa11.into_af9_push_pull();
45+
let tx = gpioa.pa12.into_af9_push_pull();
5046

5147
// Initialize the CAN peripheral
5248
let mut can = Can::new(dp.CAN, tx, rx, &mut rcc.apb1);
@@ -69,9 +65,7 @@ fn main() -> ! {
6965
// Sync to the bus and start normal operation.
7066
block!(can.enable()).ok();
7167

72-
let mut led0 = gpiob
73-
.pb15
74-
.into_push_pull_output(&mut gpiob.moder, &mut gpiob.otyper);
68+
let mut led0 = gpiob.pb15.into_push_pull_output();
7569
led0.set_high().unwrap();
7670

7771
// Watchdog makes sure this gets restarted periodically if nothing happens

examples/gpio_erased.rs

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,31 +19,15 @@ fn main() -> ! {
1919
let dp = pac::Peripherals::take().unwrap();
2020

2121
let mut rcc = dp.RCC.constrain();
22-
let mut gpiob = dp.GPIOB.split(&mut rcc.ahb);
23-
let mut gpioc = dp.GPIOC.split(&mut rcc.ahb);
24-
let mut gpiod = dp.GPIOD.split(&mut rcc.ahb);
22+
let gpiob = dp.GPIOB.split(&mut rcc.ahb);
23+
let gpioc = dp.GPIOC.split(&mut rcc.ahb);
24+
let gpiod = dp.GPIOD.split(&mut rcc.ahb);
2525

2626
let mut pin_array: [gpio::PXx<Input>; 4] = [
27-
gpiob
28-
.pb11
29-
.into_floating_input(&mut gpiob.moder, &mut gpiob.pupdr)
30-
.downgrade()
31-
.downgrade(),
32-
gpioc
33-
.pc4
34-
.into_floating_input(&mut gpioc.moder, &mut gpioc.pupdr)
35-
.downgrade()
36-
.downgrade(),
37-
gpiod
38-
.pd3
39-
.into_floating_input(&mut gpiod.moder, &mut gpiod.pupdr)
40-
.downgrade()
41-
.downgrade(),
42-
gpiod
43-
.pd2
44-
.into_floating_input(&mut gpiod.moder, &mut gpiod.pupdr)
45-
.downgrade()
46-
.downgrade(),
27+
gpiob.pb11.into_floating_input().downgrade().downgrade(),
28+
gpioc.pc4.into_floating_input().downgrade().downgrade(),
29+
gpiod.pd3.into_floating_input().downgrade().downgrade(),
30+
gpiod.pd2.into_floating_input().downgrade().downgrade(),
4731
];
4832

4933
hprintln!("Start scanning pin array").unwrap();

examples/gpio_interrupts.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,18 @@ fn main() -> ! {
2828
let mut rcc = device_peripherals.RCC.constrain();
2929
let mut syscfg = device_peripherals.SYSCFG.constrain(&mut rcc.apb2);
3030
let mut exti = device_peripherals.EXTI;
31-
let mut gpioe = device_peripherals.GPIOE.split(&mut rcc.ahb);
32-
let mut gpioa = device_peripherals.GPIOA.split(&mut rcc.ahb);
31+
let gpioe = device_peripherals.GPIOE.split(&mut rcc.ahb);
32+
let gpioa = device_peripherals.GPIOA.split(&mut rcc.ahb);
3333

34-
let mut led = gpioe
35-
.pe9
36-
.into_push_pull_output(&mut gpioe.moder, &mut gpioe.otyper);
34+
let mut led = gpioe.pe9.into_push_pull_output();
3735
// Turn the led on so we know the configuration step occurred.
3836
led.toggle().expect("unable to toggle led in configuration");
3937

4038
// Move the ownership of the led to the global LED
4139
cortex_m::interrupt::free(|cs| *LED.borrow(cs).borrow_mut() = Some(led));
4240

4341
// Configuring the user button to trigger an interrupt when the button is pressed.
44-
let mut user_button = gpioa
45-
.pa0
46-
.into_pull_down_input(&mut gpioa.moder, &mut gpioa.pupdr);
42+
let mut user_button = gpioa.pa0.into_pull_down_input();
4743
syscfg.select_exti_interrupt_source(&user_button);
4844
user_button.trigger_on_edge(&mut exti, Edge::Rising);
4945
user_button.enable_interrupt(&mut exti);

examples/i2c_scanner.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,13 @@ fn main() -> ! {
2525
let mut rcc = dp.RCC.constrain();
2626
let clocks = rcc.cfgr.freeze(&mut flash.acr);
2727

28-
let mut gpiob = dp.GPIOB.split(&mut rcc.ahb);
28+
let gpiob = dp.GPIOB.split(&mut rcc.ahb);
2929

3030
// Configure I2C1
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);
31+
let mut scl = gpiob.pb6.into_af4_open_drain();
32+
let mut sda = gpiob.pb7.into_af4_open_drain();
33+
scl.internal_pull_up(true);
34+
sda.internal_pull_up(true);
4135
let mut i2c = hal::i2c::I2c::new(
4236
dp.I2C1,
4337
(scl, sda),

examples/pwm.rs

Lines changed: 17 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -31,41 +31,21 @@ fn main() -> ! {
3131
let clocks = rcc.cfgr.sysclk(16.MHz()).freeze(&mut flash.acr);
3232

3333
// Prep the pins we need in their correct alternate function
34-
let mut gpioa = dp.GPIOA.split(&mut rcc.ahb);
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);
44-
45-
let mut gpiob = dp.GPIOB.split(&mut rcc.ahb);
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);
64-
65-
let mut gpioc = dp.GPIOC.split(&mut rcc.ahb);
66-
let pc10 = gpioc
67-
.pc10
68-
.into_af4_push_pull(&mut gpioc.moder, &mut gpioc.otyper, &mut gpioc.afrh);
34+
let gpioa = dp.GPIOA.split(&mut rcc.ahb);
35+
let pa4 = gpioa.pa4.into_af2_push_pull();
36+
let pa6 = gpioa.pa6.into_af2_push_pull();
37+
let pa7 = gpioa.pa7.into_af2_push_pull();
38+
39+
let gpiob = dp.GPIOB.split(&mut rcc.ahb);
40+
let pb0 = gpiob.pb0.into_af2_push_pull();
41+
let pb1 = gpiob.pb1.into_af2_push_pull();
42+
let pb4 = gpiob.pb4.into_af2_push_pull();
43+
let pb5 = gpiob.pb5.into_af2_push_pull();
44+
let pb8 = gpiob.pb8.into_af1_push_pull();
45+
let pb10 = gpiob.pb10.into_af1_push_pull();
46+
47+
let gpioc = dp.GPIOC.split(&mut rcc.ahb);
48+
let pc10 = gpioc.pc10.into_af4_push_pull();
6949

7050
// TIM3
7151
//
@@ -114,7 +94,7 @@ fn main() -> ! {
11494
// from the channel.
11595
//
11696
// DOES NOT COMPILE
117-
// pb0.into_af15(&mut gpiob.moder, &mut gpiob.afrl);
97+
// pb0.into_af15();
11898

11999
// TIM2
120100
//
@@ -167,7 +147,7 @@ fn main() -> ! {
167147
// use).
168148
//
169149
// DOES NOT COMPILE
170-
// tim8_ch1.output_to_pc6(gpioc.pc6.into_af4(&mut gpioc.moder, &mut gpioc.afrl));
150+
// tim8_ch1.output_to_pc6(gpioc.pc6.into_af4());
171151

172152
loop {
173153
asm::wfi();

examples/serial_dma.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,11 @@ fn main() -> ! {
3030
let mut rcc = dp.RCC.constrain();
3131
let clocks = rcc.cfgr.freeze(&mut flash.acr);
3232

33-
let mut gpioa = dp.GPIOA.split(&mut rcc.ahb);
33+
let gpioa = dp.GPIOA.split(&mut rcc.ahb);
3434

3535
let pins = (
36-
gpioa
37-
.pa9
38-
.into_af7_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh),
39-
gpioa
40-
.pa10
41-
.into_af7_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh),
36+
gpioa.pa9.into_af7_push_pull(),
37+
gpioa.pa10.into_af7_push_pull(),
4238
);
4339
let serial = Serial::new(dp.USART1, pins, 9600.Bd(), clocks, &mut rcc.apb2);
4440
let (tx, rx) = serial.split();

examples/serial_echo_rtic.rs

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -54,31 +54,25 @@ mod app {
5454

5555
// Initialize the peripherals
5656
// DIR (the LED that lights up during serial rx)
57-
let mut gpioe = cx.device.GPIOE.split(&mut rcc.ahb);
58-
let mut dir: DirType = gpioe
59-
.pe13
60-
.into_push_pull_output(&mut gpioe.moder, &mut gpioe.otyper);
57+
let gpioe = cx.device.GPIOE.split(&mut rcc.ahb);
58+
let mut dir: DirType = gpioe.pe13.into_push_pull_output();
6159
dir.set_low().unwrap();
6260

6361
// SERIAL
64-
let mut gpioa = cx.device.GPIOA.split(&mut rcc.ahb);
62+
let gpioa = cx.device.GPIOA.split(&mut rcc.ahb);
6563
let mut pins = (
66-
gpioa
67-
// Tx pin
68-
.pa9
69-
// configure this pin to make use of its `USART1_TX` alternative function
70-
// (AF mapping taken from table 14 "Alternate functions for port A" of the datasheet at
71-
// https://www.st.com/en/microcontrollers-microprocessors/stm32f303vc.html)
72-
.into_af7_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh),
73-
gpioa
74-
// Rx pin
75-
.pa10
76-
// configure this pin to make use of its `USART1_RX` alternative function
77-
// (AF mapping taken from table 14 "Alternate functions for port A" of the datasheet at
78-
// https://www.st.com/en/microcontrollers-microprocessors/stm32f303vc.html)
79-
.into_af7_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh),
64+
// Tx pin
65+
// configure this pin to make use of its `USART1_TX` alternative function
66+
// (AF mapping taken from table 14 "Alternate functions for port A" of the datasheet at
67+
// https://www.st.com/en/microcontrollers-microprocessors/stm32f303vc.html)
68+
gpioa.pa9.into_af7_push_pull(),
69+
// Rx pin
70+
// configure this pin to make use of its `USART1_RX` alternative function
71+
// (AF mapping taken from table 14 "Alternate functions for port A" of the datasheet at
72+
// https://www.st.com/en/microcontrollers-microprocessors/stm32f303vc.html)
73+
gpioa.pa10.into_af7_push_pull(),
8074
);
81-
pins.1.internal_pull_up(&mut gpioa.pupdr, true);
75+
pins.1.internal_pull_up(true);
8276
let mut serial: SerialType =
8377
Serial::new(cx.device.USART1, pins, 19200.Bd(), clocks, &mut rcc.apb2);
8478
serial.configure_interrupt(Event::ReceiveDataRegisterNotEmpty, Toggle::On);

examples/spi.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ fn main() -> ! {
2020

2121
let mut flash = dp.FLASH.constrain();
2222
let mut rcc = dp.RCC.constrain();
23-
let mut gpioc = dp.GPIOC.split(&mut rcc.ahb);
23+
let gpioc = dp.GPIOC.split(&mut rcc.ahb);
2424

2525
let clocks = rcc
2626
.cfgr
@@ -30,15 +30,9 @@ fn main() -> ! {
3030
.freeze(&mut flash.acr);
3131

3232
// Configure pins for SPI
33-
let sck = gpioc
34-
.pc10
35-
.into_af6_push_pull(&mut gpioc.moder, &mut gpioc.otyper, &mut gpioc.afrh);
36-
let miso = gpioc
37-
.pc11
38-
.into_af6_push_pull(&mut gpioc.moder, &mut gpioc.otyper, &mut gpioc.afrh);
39-
let mosi = gpioc
40-
.pc12
41-
.into_af6_push_pull(&mut gpioc.moder, &mut gpioc.otyper, &mut gpioc.afrh);
33+
let sck = gpioc.pc10.into_af6_push_pull();
34+
let miso = gpioc.pc11.into_af6_push_pull();
35+
let mosi = gpioc.pc12.into_af6_push_pull();
4236

4337
let mut spi = Spi::new(dp.SPI3, (sck, miso, mosi), 3.MHz(), clocks, &mut rcc.apb1);
4438

examples/toggle.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,9 @@ fn main() -> ! {
1919
let dp = pac::Peripherals::take().unwrap();
2020

2121
let mut rcc = dp.RCC.constrain();
22-
let mut gpioe = dp.GPIOE.split(&mut rcc.ahb);
22+
let gpioe = dp.GPIOE.split(&mut rcc.ahb);
2323

24-
let mut led = gpioe
25-
.pe13
26-
.into_push_pull_output(&mut gpioe.moder, &mut gpioe.otyper);
24+
let mut led = gpioe.pe13.into_push_pull_output();
2725

2826
led.set_low().unwrap();
2927

0 commit comments

Comments
 (0)