Skip to content

Commit 01c1dad

Browse files
committed
f4xx-hal like gpio
1 parent 565b43c commit 01c1dad

30 files changed

+1886
-1457
lines changed

.clippy.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
msrv = "1.54"
1+
msrv = "1.59"

.github/workflows/ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ jobs:
5757
- uses: actions/checkout@v2
5858
- uses: actions-rs/toolchain@v1
5959
with:
60-
toolchain: 1.54.0
60+
toolchain: 1.59.0
6161
target: thumbv7em-none-eabihf
6262
override: true
6363
profile: minimal

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1414

1515
## Unreleased
1616

17-
No changes.
17+
- Use const generics for pins. The MSRV was bumped to 1.59 ([#316])
1818

1919
## [v0.9.0] - 2022-03-06
2020

@@ -554,6 +554,7 @@ let clocks = rcc
554554
[defmt]: https://github.com/knurling-rs/defmt
555555
[filter]: https://defmt.ferrous-systems.com/filtering.html
556556

557+
[#316]: https://github.com/stm32-rs/stm32f3xx-hal/pull/316
557558
[#314]: https://github.com/stm32-rs/stm32f3xx-hal/pull/314
558559
[#309]: https://github.com/stm32-rs/stm32f3xx-hal/pull/309
559560
[#308]: https://github.com/stm32-rs/stm32f3xx-hal/pull/308

Cargo.toml

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[package]
2-
edition = "2018"
2+
edition = "2021"
33
authors = [
44
"Dylan Frankland <[email protected]>",
55
"Sh3Rm4n <[email protected]>",
@@ -19,7 +19,7 @@ exclude = [
1919
".markdownlint.yml"
2020
]
2121
resolver = "2"
22-
rust-version = "1.54"
22+
rust-version = "1.59"
2323

2424
[workspace]
2525
members = [
@@ -39,7 +39,7 @@ cortex-m = "0.7.4"
3939
cortex-m-rt = "0.7"
4040
defmt = { version = ">=0.2.3, <0.4.0", optional = true }
4141
embedded-dma = "0.2.0"
42-
embedded-hal = { version = "0.2.5", features = ["unproven"] }
42+
embedded-hal = { version = "0.2.7", features = ["unproven"] }
4343
embedded-time = "0.12.0"
4444
nb = "1.0.0"
4545
paste = "1.0.5"

examples/can.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ fn main() -> ! {
4343
// Configure CAN RX and TX pins (AF9)
4444
let rx = gpioa
4545
.pa11
46-
.into_af_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh);
46+
.into_alternate(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh);
4747
let tx = gpioa
4848
.pa12
49-
.into_af_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh);
49+
.into_alternate(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh);
5050

5151
// Initialize the CAN peripheral
5252
// Use loopback mode: No pins need to be assigned to peripheral.

examples/gpio_erased.rs

+6-10
Original file line numberDiff line numberDiff line change
@@ -23,33 +23,29 @@ fn main() -> ! {
2323
let mut gpioc = dp.GPIOC.split(&mut rcc.ahb);
2424
let mut gpiod = dp.GPIOD.split(&mut rcc.ahb);
2525

26-
let mut pin_array: [gpio::PXx<Input>; 4] = [
26+
let mut pin_array: [gpio::EPin<Input>; 4] = [
2727
gpiob
2828
.pb11
2929
.into_floating_input(&mut gpiob.moder, &mut gpiob.pupdr)
30-
.downgrade()
31-
.downgrade(),
30+
.erase(),
3231
gpioc
3332
.pc4
3433
.into_floating_input(&mut gpioc.moder, &mut gpioc.pupdr)
35-
.downgrade()
36-
.downgrade(),
34+
.erase(),
3735
gpiod
3836
.pd3
3937
.into_floating_input(&mut gpiod.moder, &mut gpiod.pupdr)
40-
.downgrade()
41-
.downgrade(),
38+
.erase(),
4239
gpiod
4340
.pd2
4441
.into_floating_input(&mut gpiod.moder, &mut gpiod.pupdr)
45-
.downgrade()
46-
.downgrade(),
42+
.erase(),
4743
];
4844

4945
hprintln!("Start scanning pin array").unwrap();
5046
loop {
5147
for pin in pin_array.iter_mut() {
52-
hprintln!("Value is {}", pin.is_high().unwrap()).unwrap();
48+
hprintln!("Value is {}", pin.is_high()).unwrap();
5349
asm::delay(1_000_000);
5450
}
5551
}

examples/gpio_interrupts.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ fn main() -> ! {
3535
.pe9
3636
.into_push_pull_output(&mut gpioe.moder, &mut gpioe.otyper);
3737
// Turn the led on so we know the configuration step occurred.
38-
led.toggle().expect("unable to toggle led in configuration");
38+
led.toggle();
3939

4040
// Move the ownership of the led to the global LED
4141
cortex_m::interrupt::free(|cs| *LED.borrow(cs).borrow_mut() = Some(led));
@@ -69,12 +69,7 @@ fn main() -> ! {
6969
fn EXTI0() {
7070
cortex_m::interrupt::free(|cs| {
7171
// Toggle the LED
72-
LED.borrow(cs)
73-
.borrow_mut()
74-
.as_mut()
75-
.unwrap()
76-
.toggle()
77-
.unwrap();
72+
LED.borrow(cs).borrow_mut().as_mut().unwrap().toggle();
7873

7974
// Clear the interrupt pending bit so we don't infinitely call this routine
8075
BUTTON

examples/i2c_scanner.rs

+8-10
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,14 @@ fn main() -> ! {
2828
let mut 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 scl = gpiob
32+
.pb6
33+
.into_alternate_open_drain(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrl)
34+
.internal_pull_up(&mut gpiob.pupdr, true);
35+
let sda = gpiob
36+
.pb7
37+
.into_alternate_open_drain(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrl)
38+
.internal_pull_up(&mut gpiob.pupdr, true);
4139
let mut i2c = hal::i2c::I2c::new(
4240
dp.I2C1,
4341
(scl, sda),

examples/pwm.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -34,38 +34,38 @@ fn main() -> ! {
3434
let mut gpioa = dp.GPIOA.split(&mut rcc.ahb);
3535
let pa4 = gpioa
3636
.pa4
37-
.into_af_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrl);
37+
.into_alternate(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrl);
3838
let pa6 = gpioa
3939
.pa6
40-
.into_af_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrl);
40+
.into_alternate(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrl);
4141
let pa7 = gpioa
4242
.pa7
43-
.into_af_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrl);
43+
.into_alternate(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrl);
4444

4545
let mut gpiob = dp.GPIOB.split(&mut rcc.ahb);
4646
let pb0 = gpiob
4747
.pb0
48-
.into_af_push_pull(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrl);
48+
.into_alternate(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrl);
4949
let pb1 = gpiob
5050
.pb1
51-
.into_af_push_pull(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrl);
51+
.into_alternate(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrl);
5252
let pb4 = gpiob
5353
.pb4
54-
.into_af_push_pull(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrl);
54+
.into_alternate(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrl);
5555
let pb5 = gpiob
5656
.pb5
57-
.into_af_push_pull(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrl);
57+
.into_alternate(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrl);
5858
let pb8 = gpiob
5959
.pb8
60-
.into_af_push_pull(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrh);
60+
.into_alternate(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrh);
6161
let pb10 = gpiob
6262
.pb10
63-
.into_af_push_pull(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrh);
63+
.into_alternate(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrh);
6464

6565
let mut gpioc = dp.GPIOC.split(&mut rcc.ahb);
6666
let pc10 = gpioc
6767
.pc10
68-
.into_af_push_pull(&mut gpioc.moder, &mut gpioc.otyper, &mut gpioc.afrh);
68+
.into_alternate(&mut gpioc.moder, &mut gpioc.otyper, &mut gpioc.afrh);
6969

7070
// TIM3
7171
//

examples/serial_dma.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ fn main() -> ! {
3535
let pins = (
3636
gpioa
3737
.pa9
38-
.into_af_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh),
38+
.into_alternate(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh),
3939
gpioa
4040
.pa10
41-
.into_af_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh),
41+
.into_alternate(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh),
4242
);
4343
let serial = Serial::new(dp.USART1, pins, 9600.Bd(), clocks, &mut rcc.apb2);
4444
let (tx, rx) = serial.split();

examples/serial_echo_rtic.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -58,27 +58,27 @@ mod app {
5858
let mut dir: DirType = gpioe
5959
.pe13
6060
.into_push_pull_output(&mut gpioe.moder, &mut gpioe.otyper);
61-
dir.set_low().unwrap();
61+
dir.set_low();
6262

6363
// SERIAL
6464
let mut gpioa = cx.device.GPIOA.split(&mut rcc.ahb);
65-
let mut pins = (
65+
let pins = (
6666
gpioa
6767
// Tx pin
6868
.pa9
6969
// configure this pin to make use of its `USART1_TX` alternative function
7070
// (AF mapping taken from table 14 "Alternate functions for port A" of the datasheet at
7171
// https://www.st.com/en/microcontrollers-microprocessors/stm32f303vc.html)
72-
.into_af_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh),
72+
.into_alternate(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh),
7373
gpioa
7474
// Rx pin
7575
.pa10
7676
// configure this pin to make use of its `USART1_RX` alternative function
7777
// (AF mapping taken from table 14 "Alternate functions for port A" of the datasheet at
7878
// https://www.st.com/en/microcontrollers-microprocessors/stm32f303vc.html)
79-
.into_af_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh),
79+
.into_alternate(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh)
80+
.internal_pull_up(&mut gpioa.pupdr, true),
8081
);
81-
pins.1.internal_pull_up(&mut gpioa.pupdr, true);
8282
let mut serial: SerialType =
8383
Serial::new(cx.device.USART1, pins, 19200.Bd(), clocks, &mut rcc.apb2);
8484
serial.configure_interrupt(Event::ReceiveDataRegisterNotEmpty, Toggle::On);
@@ -99,7 +99,7 @@ mod app {
9999
let dir = cx.local.dir;
100100

101101
if serial.is_event_triggered(Event::ReceiveDataRegisterNotEmpty) {
102-
dir.set_high().unwrap();
102+
dir.set_high();
103103
serial.configure_interrupt(Event::ReceiveDataRegisterNotEmpty, Toggle::Off);
104104
match serial.read() {
105105
Ok(byte) => {
@@ -116,7 +116,7 @@ mod app {
116116
// and other functions enabled by the "enumset" feature.
117117
let events = serial.triggered_events();
118118
if events.contains(Event::TransmissionComplete) {
119-
dir.set_low().unwrap();
119+
dir.set_low();
120120
let interrupts = {
121121
let mut interrupts = enumset::EnumSet::new();
122122
interrupts.insert(Event::ReceiveDataRegisterNotEmpty);

examples/spi.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ fn main() -> ! {
3232
// Configure pins for SPI
3333
let sck = gpioc
3434
.pc10
35-
.into_af_push_pull(&mut gpioc.moder, &mut gpioc.otyper, &mut gpioc.afrh);
35+
.into_alternate(&mut gpioc.moder, &mut gpioc.otyper, &mut gpioc.afrh);
3636
let miso = gpioc
3737
.pc11
38-
.into_af_push_pull(&mut gpioc.moder, &mut gpioc.otyper, &mut gpioc.afrh);
38+
.into_alternate(&mut gpioc.moder, &mut gpioc.otyper, &mut gpioc.afrh);
3939
let mosi = gpioc
4040
.pc12
41-
.into_af_push_pull(&mut gpioc.moder, &mut gpioc.otyper, &mut gpioc.afrh);
41+
.into_alternate(&mut gpioc.moder, &mut gpioc.otyper, &mut gpioc.afrh);
4242

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

examples/toggle.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,18 @@ fn main() -> ! {
2525
.pe13
2626
.into_push_pull_output(&mut gpioe.moder, &mut gpioe.otyper);
2727

28-
led.set_low().unwrap();
28+
led.set_low();
2929

3030
loop {
31-
led.toggle().unwrap();
31+
led.toggle();
3232
cortex_m::asm::delay(8_000_000);
3333
// Toggle by hand.
3434
// Uses `StatefulOutputPin` instead of `ToggleableOutputPin`.
3535
// Logically it is the same.
36-
if led.is_set_low().unwrap() {
37-
led.set_high().unwrap();
36+
if led.is_set_low() {
37+
led.set_high();
3838
} else {
39-
led.set_low().unwrap();
39+
led.set_low();
4040
}
4141
cortex_m::asm::delay(8_000_000);
4242
}

examples/usb_serial.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ fn main() -> ! {
3939
let mut led = gpioe
4040
.pe13
4141
.into_push_pull_output(&mut gpioe.moder, &mut gpioe.otyper);
42-
led.set_low().ok(); // Turn off
42+
led.set_low(); // Turn off
4343

4444
let mut gpioa = dp.GPIOA.split(&mut rcc.ahb);
4545

@@ -50,13 +50,13 @@ fn main() -> ! {
5050
let mut usb_dp = gpioa
5151
.pa12
5252
.into_push_pull_output(&mut gpioa.moder, &mut gpioa.otyper);
53-
usb_dp.set_low().ok();
53+
usb_dp.set_low();
5454
delay(clocks.sysclk().0 / 100);
5555

5656
let usb_dm = gpioa
5757
.pa11
58-
.into_af_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh);
59-
let usb_dp = usb_dp.into_af_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh);
58+
.into_alternate(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh);
59+
let usb_dp = usb_dp.into_alternate(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh);
6060

6161
let usb = Peripheral {
6262
usb: dp.USB,
@@ -83,7 +83,7 @@ fn main() -> ! {
8383

8484
match serial.read(&mut buf) {
8585
Ok(count) if count > 0 => {
86-
led.set_high().ok(); // Turn on
86+
led.set_high(); // Turn on
8787

8888
// Echo back in upper case
8989
for c in buf[0..count].iter_mut() {
@@ -105,6 +105,6 @@ fn main() -> ! {
105105
_ => {}
106106
}
107107

108-
led.set_low().ok(); // Turn off
108+
led.set_low(); // Turn off
109109
}
110110
}

0 commit comments

Comments
 (0)