Skip to content

Commit

Permalink
f4xx-hal like gpio
Browse files Browse the repository at this point in the history
  • Loading branch information
burrbull committed Mar 30, 2022
1 parent 565b43c commit 893fb64
Show file tree
Hide file tree
Showing 30 changed files with 1,885 additions and 1,457 deletions.
2 changes: 1 addition & 1 deletion .clippy.toml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
msrv = "1.54"
msrv = "1.59"
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
toolchain: 1.54.0
toolchain: 1.59.0
target: thumbv7em-none-eabihf
override: true
profile: minimal
Expand Down
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## Unreleased

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

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

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

[#316]: https://github.com/stm32-rs/stm32f3xx-hal/pull/316
[#314]: https://github.com/stm32-rs/stm32f3xx-hal/pull/314
[#309]: https://github.com/stm32-rs/stm32f3xx-hal/pull/309
[#308]: https://github.com/stm32-rs/stm32f3xx-hal/pull/308
Expand Down
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
edition = "2018"
edition = "2021"
authors = [
"Dylan Frankland <[email protected]>",
"Sh3Rm4n <[email protected]>",
Expand All @@ -19,7 +19,7 @@ exclude = [
".markdownlint.yml"
]
resolver = "2"
rust-version = "1.54"
rust-version = "1.59"

[workspace]
members = [
Expand All @@ -39,7 +39,7 @@ cortex-m = "0.7.4"
cortex-m-rt = "0.7"
defmt = { version = ">=0.2.3, <0.4.0", optional = true }
embedded-dma = "0.2.0"
embedded-hal = { version = "0.2.5", features = ["unproven"] }
embedded-hal = { version = "0.2.7", features = ["unproven"] }
embedded-time = "0.12.0"
nb = "1.0.0"
paste = "1.0.5"
Expand Down
4 changes: 2 additions & 2 deletions examples/can.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ fn main() -> ! {
// Configure CAN RX and TX pins (AF9)
let rx = gpioa
.pa11
.into_af_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh);
.into_alternate(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh);
let tx = gpioa
.pa12
.into_af_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh);
.into_alternate(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh);

// Initialize the CAN peripheral
// Use loopback mode: No pins need to be assigned to peripheral.
Expand Down
16 changes: 6 additions & 10 deletions examples/gpio_erased.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,29 @@ fn main() -> ! {
let mut gpioc = dp.GPIOC.split(&mut rcc.ahb);
let mut gpiod = dp.GPIOD.split(&mut rcc.ahb);

let mut pin_array: [gpio::PXx<Input>; 4] = [
let mut pin_array: [gpio::EPin<Input>; 4] = [
gpiob
.pb11
.into_floating_input(&mut gpiob.moder, &mut gpiob.pupdr)
.downgrade()
.downgrade(),
.erase(),
gpioc
.pc4
.into_floating_input(&mut gpioc.moder, &mut gpioc.pupdr)
.downgrade()
.downgrade(),
.erase(),
gpiod
.pd3
.into_floating_input(&mut gpiod.moder, &mut gpiod.pupdr)
.downgrade()
.downgrade(),
.erase(),
gpiod
.pd2
.into_floating_input(&mut gpiod.moder, &mut gpiod.pupdr)
.downgrade()
.downgrade(),
.erase(),
];

hprintln!("Start scanning pin array").unwrap();
loop {
for pin in pin_array.iter_mut() {
hprintln!("Value is {}", pin.is_high().unwrap()).unwrap();
hprintln!("Value is {}", pin.is_high()).unwrap();
asm::delay(1_000_000);
}
}
Expand Down
9 changes: 2 additions & 7 deletions examples/gpio_interrupts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fn main() -> ! {
.pe9
.into_push_pull_output(&mut gpioe.moder, &mut gpioe.otyper);
// Turn the led on so we know the configuration step occurred.
led.toggle().expect("unable to toggle led in configuration");
led.toggle();

// Move the ownership of the led to the global LED
cortex_m::interrupt::free(|cs| *LED.borrow(cs).borrow_mut() = Some(led));
Expand Down Expand Up @@ -69,12 +69,7 @@ fn main() -> ! {
fn EXTI0() {
cortex_m::interrupt::free(|cs| {
// Toggle the LED
LED.borrow(cs)
.borrow_mut()
.as_mut()
.unwrap()
.toggle()
.unwrap();
LED.borrow(cs).borrow_mut().as_mut().unwrap().toggle();

// Clear the interrupt pending bit so we don't infinitely call this routine
BUTTON
Expand Down
18 changes: 8 additions & 10 deletions examples/i2c_scanner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,14 @@ fn main() -> ! {
let mut gpiob = dp.GPIOB.split(&mut rcc.ahb);

// Configure I2C1
let mut scl =
gpiob
.pb6
.into_af_open_drain(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrl);
let mut sda =
gpiob
.pb7
.into_af_open_drain(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrl);
scl.internal_pull_up(&mut gpiob.pupdr, true);
sda.internal_pull_up(&mut gpiob.pupdr, true);
let scl = gpiob
.pb6
.into_alternate_open_drain(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrl)
.internal_pull_up(&mut gpiob.pupdr, true);
let sda = gpiob
.pb7
.into_alternate_open_drain(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrl)
.internal_pull_up(&mut gpiob.pupdr, true);
let mut i2c = hal::i2c::I2c::new(
dp.I2C1,
(scl, sda),
Expand Down
20 changes: 10 additions & 10 deletions examples/pwm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,38 +34,38 @@ fn main() -> ! {
let mut gpioa = dp.GPIOA.split(&mut rcc.ahb);
let pa4 = gpioa
.pa4
.into_af_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrl);
.into_alternate(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrl);
let pa6 = gpioa
.pa6
.into_af_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrl);
.into_alternate(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrl);
let pa7 = gpioa
.pa7
.into_af_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrl);
.into_alternate(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrl);

let mut gpiob = dp.GPIOB.split(&mut rcc.ahb);
let pb0 = gpiob
.pb0
.into_af_push_pull(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrl);
.into_alternate(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrl);
let pb1 = gpiob
.pb1
.into_af_push_pull(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrl);
.into_alternate(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrl);
let pb4 = gpiob
.pb4
.into_af_push_pull(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrl);
.into_alternate(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrl);
let pb5 = gpiob
.pb5
.into_af_push_pull(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrl);
.into_alternate(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrl);
let pb8 = gpiob
.pb8
.into_af_push_pull(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrh);
.into_alternate(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrh);
let pb10 = gpiob
.pb10
.into_af_push_pull(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrh);
.into_alternate(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrh);

let mut gpioc = dp.GPIOC.split(&mut rcc.ahb);
let pc10 = gpioc
.pc10
.into_af_push_pull(&mut gpioc.moder, &mut gpioc.otyper, &mut gpioc.afrh);
.into_alternate(&mut gpioc.moder, &mut gpioc.otyper, &mut gpioc.afrh);

// TIM3
//
Expand Down
4 changes: 2 additions & 2 deletions examples/serial_dma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ fn main() -> ! {
let pins = (
gpioa
.pa9
.into_af_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh),
.into_alternate(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh),
gpioa
.pa10
.into_af_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh),
.into_alternate(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh),
);
let serial = Serial::new(dp.USART1, pins, 9600.Bd(), clocks, &mut rcc.apb2);
let (tx, rx) = serial.split();
Expand Down
14 changes: 7 additions & 7 deletions examples/serial_echo_rtic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,27 +58,27 @@ mod app {
let mut dir: DirType = gpioe
.pe13
.into_push_pull_output(&mut gpioe.moder, &mut gpioe.otyper);
dir.set_low().unwrap();
dir.set_low();

// SERIAL
let mut gpioa = cx.device.GPIOA.split(&mut rcc.ahb);
let mut pins = (
let pins = (
gpioa
// Tx pin
.pa9
// configure this pin to make use of its `USART1_TX` alternative function
// (AF mapping taken from table 14 "Alternate functions for port A" of the datasheet at
// https://www.st.com/en/microcontrollers-microprocessors/stm32f303vc.html)
.into_af_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh),
.into_alternate(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh),
gpioa
// Rx pin
.pa10
// configure this pin to make use of its `USART1_RX` alternative function
// (AF mapping taken from table 14 "Alternate functions for port A" of the datasheet at
// https://www.st.com/en/microcontrollers-microprocessors/stm32f303vc.html)
.into_af_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh),
.into_alternate(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh)
.internal_pull_up(&mut gpioa.pupdr, true),
);
pins.1.internal_pull_up(&mut gpioa.pupdr, true);
let mut serial: SerialType =
Serial::new(cx.device.USART1, pins, 19200.Bd(), clocks, &mut rcc.apb2);
serial.configure_interrupt(Event::ReceiveDataRegisterNotEmpty, Toggle::On);
Expand All @@ -99,7 +99,7 @@ mod app {
let dir = cx.local.dir;

if serial.is_event_triggered(Event::ReceiveDataRegisterNotEmpty) {
dir.set_high().unwrap();
dir.set_high();
serial.configure_interrupt(Event::ReceiveDataRegisterNotEmpty, Toggle::Off);
match serial.read() {
Ok(byte) => {
Expand All @@ -116,7 +116,7 @@ mod app {
// and other functions enabled by the "enumset" feature.
let events = serial.triggered_events();
if events.contains(Event::TransmissionComplete) {
dir.set_low().unwrap();
dir.set_low();
let interrupts = {
let mut interrupts = enumset::EnumSet::new();
interrupts.insert(Event::ReceiveDataRegisterNotEmpty);
Expand Down
6 changes: 3 additions & 3 deletions examples/spi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ fn main() -> ! {
// Configure pins for SPI
let sck = gpioc
.pc10
.into_af_push_pull(&mut gpioc.moder, &mut gpioc.otyper, &mut gpioc.afrh);
.into_alternate(&mut gpioc.moder, &mut gpioc.otyper, &mut gpioc.afrh);
let miso = gpioc
.pc11
.into_af_push_pull(&mut gpioc.moder, &mut gpioc.otyper, &mut gpioc.afrh);
.into_alternate(&mut gpioc.moder, &mut gpioc.otyper, &mut gpioc.afrh);
let mosi = gpioc
.pc12
.into_af_push_pull(&mut gpioc.moder, &mut gpioc.otyper, &mut gpioc.afrh);
.into_alternate(&mut gpioc.moder, &mut gpioc.otyper, &mut gpioc.afrh);

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

Expand Down
10 changes: 5 additions & 5 deletions examples/toggle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@ fn main() -> ! {
.pe13
.into_push_pull_output(&mut gpioe.moder, &mut gpioe.otyper);

led.set_low().unwrap();
led.set_low();

loop {
led.toggle().unwrap();
led.toggle();
cortex_m::asm::delay(8_000_000);
// Toggle by hand.
// Uses `StatefulOutputPin` instead of `ToggleableOutputPin`.
// Logically it is the same.
if led.is_set_low().unwrap() {
led.set_high().unwrap();
if led.is_set_low() {
led.set_high();
} else {
led.set_low().unwrap();
led.set_low();
}
cortex_m::asm::delay(8_000_000);
}
Expand Down
12 changes: 6 additions & 6 deletions examples/usb_serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ fn main() -> ! {
let mut led = gpioe
.pe13
.into_push_pull_output(&mut gpioe.moder, &mut gpioe.otyper);
led.set_low().ok(); // Turn off
led.set_low(); // Turn off

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

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

let usb_dm = gpioa
.pa11
.into_af_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh);
let usb_dp = usb_dp.into_af_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh);
.into_alternate(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh);
let usb_dp = usb_dp.into_alternate(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh);

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

match serial.read(&mut buf) {
Ok(count) if count > 0 => {
led.set_high().ok(); // Turn on
led.set_high(); // Turn on

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

led.set_low().ok(); // Turn off
led.set_low(); // Turn off
}
}
Loading

0 comments on commit 893fb64

Please sign in to comment.