Skip to content

Commit b30fe24

Browse files
committed
Utilize LDREX/STREX in GPIO configure functions
1 parent 231c959 commit b30fe24

File tree

15 files changed

+681
-797
lines changed

15 files changed

+681
-797
lines changed

asm/asm.a

2.91 KB
Binary file not shown.

asm/asm.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#![feature(asm)]
2+
#![no_std]
3+
#![crate_type = "staticlib"]
4+
5+
/// Perform volatile atomic `*ptr = *ptr & !bic | or;`
6+
#[no_mangle]
7+
pub unsafe extern "C" fn volatile_atomic_bic_or(ptr: *mut u32, bic: u32, or: u32) {
8+
let mut res: u32;
9+
while {
10+
asm!(
11+
"ldrex {0}, [{1}]",
12+
"bics {0}, {2}",
13+
"orrs {0}, {3}",
14+
"strex {0}, {0}, [{1}]",
15+
out(reg) res,
16+
in(reg) ptr,
17+
in(reg) bic,
18+
in(reg) or,
19+
options(nostack),
20+
);
21+
res != 0
22+
} {}
23+
}
24+
25+
#[panic_handler]
26+
fn panic(_: &core::panic::PanicInfo) -> ! {
27+
loop {} // Now empty loop is safe for LLVM >=12!
28+
}

build.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
use std::{env, fs::copy, path::PathBuf};
2+
3+
fn main() {
4+
copy_asm_lib();
5+
println!("cargo:rerun-if-changed=build.rs");
6+
}
7+
8+
fn copy_asm_lib() {
9+
let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());
10+
let name = env::var("CARGO_PKG_NAME").unwrap();
11+
copy("asm/asm.a", out_dir.join(format!("lib{}.a", name))).unwrap();
12+
println!("cargo:rustc-link-lib=static={}", name);
13+
println!("cargo:rustc-link-search={}", out_dir.display());
14+
println!("cargo:rerun-if-changed=asm/asm.a");
15+
}

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 gpio_a = dp.GPIOA.split(&mut rcc.ahb);
36-
let mut adc1_in1_pin = gpio_a.pa0.into_analog(&mut gpio_a.moder, &mut gpio_a.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
@@ -26,8 +26,8 @@ fn main() -> ! {
2626

2727
let mut flash = dp.FLASH.constrain();
2828
let mut rcc = dp.RCC.constrain();
29-
let mut gpiob = dp.GPIOB.split(&mut rcc.ahb);
30-
let mut gpioa = dp.GPIOA.split(&mut rcc.ahb);
29+
let gpiob = dp.GPIOB.split(&mut rcc.ahb);
30+
let gpioa = dp.GPIOA.split(&mut rcc.ahb);
3131

3232
let _clocks = rcc
3333
.cfgr
@@ -38,12 +38,8 @@ fn main() -> ! {
3838
.freeze(&mut flash.acr);
3939

4040
// Configure CAN RX and TX pins (AF9)
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);
41+
let rx = gpioa.pa11.into_af9_push_pull();
42+
let tx = gpioa.pa12.into_af9_push_pull();
4743

4844
// Initialize the CAN peripheral
4945
let can = Can::new(dp.CAN, rx, tx, &mut rcc.apb1);
@@ -53,9 +49,7 @@ fn main() -> ! {
5349

5450
let (mut tx, mut rx0, _rx1) = can.split();
5551

56-
let mut led0 = gpiob
57-
.pb15
58-
.into_push_pull_output(&mut gpiob.moder, &mut gpiob.otyper);
52+
let mut led0 = gpiob.pb15.into_push_pull_output();
5953
led0.set_high().unwrap();
6054

6155
let filter = CanFilter::from_mask(0b100, ID as u32);

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
@@ -29,22 +29,18 @@ fn main() -> ! {
2929
let mut rcc = device_peripherals.RCC.constrain();
3030
let mut syscfg = device_peripherals.SYSCFG.constrain(&mut rcc.apb2);
3131
let mut exti = device_peripherals.EXTI;
32-
let mut gpioe = device_peripherals.GPIOE.split(&mut rcc.ahb);
33-
let mut gpioa = device_peripherals.GPIOA.split(&mut rcc.ahb);
32+
let gpioe = device_peripherals.GPIOE.split(&mut rcc.ahb);
33+
let gpioa = device_peripherals.GPIOA.split(&mut rcc.ahb);
3434

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

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

4442
// Configuring the user button to trigger an interrupt when the button is pressed.
45-
let mut user_button = gpioa
46-
.pa0
47-
.into_pull_down_input(&mut gpioa.moder, &mut gpioa.pupdr);
43+
let mut user_button = gpioa.pa0.into_pull_down_input();
4844
user_button.make_interrupt_source(&mut syscfg);
4945
user_button.trigger_on_edge(&mut exti, Edge::Rising);
5046
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: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -31,41 +31,21 @@ fn main() -> ! {
3131
let clocks = rcc.cfgr.sysclk(16u32.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
//

examples/serial_dma.rs

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

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

2424
let pins = (
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),
25+
gpioa.pa9.into_af7_push_pull(),
26+
gpioa.pa10.into_af7_push_pull(),
3127
);
3228
let serial = Serial::usart1(dp.USART1, pins, 9600.Bd(), clocks, &mut rcc.apb2);
3329
let (tx, rx) = serial.split();

examples/spi.rs

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

2323
let mut flash = dp.FLASH.constrain();
2424
let mut rcc = dp.RCC.constrain();
25-
let mut gpioa = dp.GPIOA.split(&mut rcc.ahb);
25+
let gpioa = dp.GPIOA.split(&mut rcc.ahb);
2626

2727
let clocks = rcc
2828
.cfgr
@@ -32,15 +32,9 @@ fn main() -> ! {
3232
.freeze(&mut flash.acr);
3333

3434
// Configure pins for SPI
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);
35+
let sck = gpioa.pa5.into_af5_push_pull();
36+
let miso = gpioa.pa6.into_af5_push_pull();
37+
let mosi = gpioa.pa7.into_af5_push_pull();
4438

4539
let spi_mode = Mode {
4640
polarity: Polarity::IdleLow,

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

examples/usb_serial.rs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,29 +35,22 @@ fn main() -> ! {
3535
assert!(clocks.usbclk_valid());
3636

3737
// Configure the on-board LED (LD10, south red)
38-
let mut gpioe = dp.GPIOE.split(&mut rcc.ahb);
39-
let mut led = gpioe
40-
.pe13
41-
.into_push_pull_output(&mut gpioe.moder, &mut gpioe.otyper);
38+
let gpioe = dp.GPIOE.split(&mut rcc.ahb);
39+
let mut led = gpioe.pe13.into_push_pull_output();
4240
led.set_low().ok(); // Turn off
4341

44-
let mut gpioa = dp.GPIOA.split(&mut rcc.ahb);
42+
let gpioa = dp.GPIOA.split(&mut rcc.ahb);
4543

4644
// F3 Discovery board has a pull-up resistor on the D+ line.
4745
// Pull the D+ pin down to send a RESET condition to the USB bus.
4846
// This forced reset is needed only for development, without it host
4947
// will not reset your device when you upload new firmware.
50-
let mut usb_dp = gpioa
51-
.pa12
52-
.into_push_pull_output(&mut gpioa.moder, &mut gpioa.otyper);
48+
let mut usb_dp = gpioa.pa12.into_push_pull_output();
5349
usb_dp.set_low().ok();
5450
delay(clocks.sysclk().0 / 100);
5551

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);
52+
let usb_dm = gpioa.pa11.into_af14_push_pull();
53+
let usb_dp = usb_dp.into_af14_push_pull();
6154

6255
let usb = Peripheral {
6356
usb: dp.USB,

0 commit comments

Comments
 (0)