Skip to content

Commit dab8708

Browse files
Piroro-hsSh3Rm4n
authored andcommitted
Utilize LDREX/STREX in GPIO configure functions
1 parent ad2f0b9 commit dab8708

27 files changed

+929
-1033
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
2222
- Deprecate `Toggle` enum and use `Switch` instead for better naming purposes
2323
([#334])
2424
- Add `impl From<Toggle> for Switch` to reduce churn.
25+
- GPIO configuration functions no longer require registers as arguments ([#213])
26+
27+
### Added
28+
29+
- Implement `into_xxx` methods for erased pins ([#213])
2530

2631
## [v0.9.1] - 2022-09-07
2732

@@ -617,6 +622,7 @@ let clocks = rcc
617622
[#220]: https://github.com/stm32-rs/stm32f3xx-hal/pull/220
618623
[#217]: https://github.com/stm32-rs/stm32f3xx-hal/pull/217
619624
[#216]: https://github.com/stm32-rs/stm32f3xx-hal/pull/216
625+
[#213]: https://github.com/stm32-rs/stm32f3xx-hal/pull/213
620626
[#212]: https://github.com/stm32-rs/stm32f3xx-hal/pull/212
621627
[#210]: https://github.com/stm32-rs/stm32f3xx-hal/pull/210
622628
[#208]: https://github.com/stm32-rs/stm32f3xx-hal/pull/208

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
@@ -87,8 +87,8 @@ fn main() -> ! {
8787

8888
// Set up pin PA0 as analog pin.
8989
// This pin is connected to the user button on the stm32f3discovery board.
90-
let mut gpioa = dp.GPIOA.split(&mut rcc.ahb);
91-
let mut analog_pin = gpioa.pa0.into_analog(&mut gpioa.moder, &mut gpioa.pupdr);
90+
let gpioa = dp.GPIOA.split(&mut rcc.ahb);
91+
let mut analog_pin = gpioa.pa0.into_analog();
9292

9393
let mut timer = timer::Timer::new(dp.TIM2, clocks, &mut rcc.apb1);
9494

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_af_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh);
47-
let tx = gpioa
48-
.pa12
49-
.into_af_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh);
44+
let rx = gpioa.pa11.into_af_push_pull();
45+
let tx = gpioa.pa12.into_af_push_pull();
5046

5147
// Initialize the CAN peripheral
5248
// Use loopback mode: No pins need to be assigned to peripheral.
@@ -68,9 +64,7 @@ fn main() -> ! {
6864
// Sync to the bus and start normal operation.
6965
block!(can.enable_non_blocking()).ok();
7066

71-
let mut led0 = gpiob
72-
.pb15
73-
.into_push_pull_output(&mut gpiob.moder, &mut gpiob.otyper);
67+
let mut led0 = gpiob.pb15.into_push_pull_output();
7468
led0.set_high().unwrap();
7569

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

examples/dac_sine.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,11 @@ fn main() -> ! {
1919
// let clocks = rcc.cfgr.freeze(&mut dp.FLASH.constrain().acr);
2020

2121
// Set up pin PA4 as analog pin.
22-
let mut gpioa = dp.GPIOA.split(&mut rcc.ahb);
23-
let _dac1_out1 = gpioa.pa4.into_analog(&mut gpioa.moder, &mut gpioa.pupdr);
22+
let gpioa = dp.GPIOA.split(&mut rcc.ahb);
23+
let _dac1_out1 = gpioa.pa4.into_analog();
2424

2525
// set up led for blinking loop
26-
let mut ok_led = gpioa
27-
.pa15
28-
.into_push_pull_output(&mut gpioa.moder, &mut gpioa.otyper);
26+
let mut ok_led = gpioa.pa15.into_push_pull_output();
2927

3028
// set up dac1, data is twelve bits, aligned right
3129
let mut dac1 = Dac::new(dp.DAC1, &mut rcc.apb1);
@@ -38,10 +36,10 @@ fn main() -> ! {
3836
cortex_m::asm::delay(8_000);
3937
}
4038
if led {
41-
ok_led.set_low().unwrap();
39+
ok_led.set_low().ok();
4240
led = false;
4341
} else {
44-
ok_led.set_high().unwrap();
42+
ok_led.set_high().ok();
4543
led = true;
4644
}
4745
}

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");

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_af_open_drain(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrl);
35-
let mut sda =
36-
gpiob
37-
.pb7
38-
.into_af_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_af_open_drain();
32+
let mut sda = gpiob.pb7.into_af_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_af_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrl);
38-
let pa6 = gpioa
39-
.pa6
40-
.into_af_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrl);
41-
let pa7 = gpioa
42-
.pa7
43-
.into_af_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_af_push_pull(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrl);
49-
let pb1 = gpiob
50-
.pb1
51-
.into_af_push_pull(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrl);
52-
let pb4 = gpiob
53-
.pb4
54-
.into_af_push_pull(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrl);
55-
let pb5 = gpiob
56-
.pb5
57-
.into_af_push_pull(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrl);
58-
let pb8 = gpiob
59-
.pb8
60-
.into_af_push_pull(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrh);
61-
let pb10 = gpiob
62-
.pb10
63-
.into_af_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_af_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_af_push_pull();
36+
let pa6 = gpioa.pa6.into_af_push_pull();
37+
let pa7 = gpioa.pa7.into_af_push_pull();
38+
39+
let gpiob = dp.GPIOB.split(&mut rcc.ahb);
40+
let pb0 = gpiob.pb0.into_af_push_pull();
41+
let pb1 = gpiob.pb1.into_af_push_pull();
42+
let pb4 = gpiob.pb4.into_af_push_pull();
43+
let pb5 = gpiob.pb5.into_af_push_pull();
44+
let pb8 = gpiob.pb8.into_af_push_pull();
45+
let pb10 = gpiob.pb10.into_af_push_pull();
46+
47+
let gpioc = dp.GPIOC.split(&mut rcc.ahb);
48+
let pc10 = gpioc.pc10.into_af_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();

0 commit comments

Comments
 (0)