Skip to content

Do not require registers to be passed to GPIO configuration functions #213

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Deprecate `Toggle` enum and use `Switch` instead for better naming purposes
([#334])
- Add `impl From<Toggle> for Switch` to reduce churn.
- GPIO configuration functions no longer require registers as arguments ([#213])

### Added

- Implement `into_xxx` methods for erased pins ([#213])

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

Expand Down Expand Up @@ -617,6 +622,7 @@ let clocks = rcc
[#220]: https://github.com/stm32-rs/stm32f3xx-hal/pull/220
[#217]: https://github.com/stm32-rs/stm32f3xx-hal/pull/217
[#216]: https://github.com/stm32-rs/stm32f3xx-hal/pull/216
[#213]: https://github.com/stm32-rs/stm32f3xx-hal/pull/213
[#212]: https://github.com/stm32-rs/stm32f3xx-hal/pull/212
[#210]: https://github.com/stm32-rs/stm32f3xx-hal/pull/210
[#208]: https://github.com/stm32-rs/stm32f3xx-hal/pull/208
Expand Down
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,9 @@ fn main() -> ! {
let dp = pac::Peripherals::take().unwrap();

let mut rcc = dp.RCC.constrain();
let mut gpioe = dp.GPIOE.split(&mut rcc.ahb);
let gpioe = dp.GPIOE.split(&mut rcc.ahb);

let mut led = gpioe
.pe13
.into_push_pull_output(&mut gpioe.moder, &mut gpioe.otyper);
let mut led = gpioe.pe13.into_push_pull_output();

loop {
led.toggle().unwrap();
Expand Down
5 changes: 2 additions & 3 deletions codegen/src/codegen/gpio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,11 @@ fn get_port_pac_module(port: &Port, feature: &str) -> &'static str {
fn gen_pin(pin: &gpio::Pin) -> Result<()> {
let nr = pin.number()?;
let reset_mode = get_pin_reset_mode(pin)?;
let afr = if nr < 8 { 'L' } else { 'H' };
let af_numbers = get_pin_af_numbers(pin)?;

println!(
" {} => {{ reset: {}, afr: {}, af: {:?} }},",
nr, reset_mode, afr, af_numbers,
" {} => {{ reset: {}, af: {:?} }},",
nr, reset_mode, af_numbers,
);

Ok(())
Expand Down
4 changes: 2 additions & 2 deletions examples/adc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ fn main() -> ! {

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

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

Expand Down
16 changes: 5 additions & 11 deletions examples/can.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ fn main() -> ! {

let mut flash = dp.FLASH.constrain();
let mut rcc = dp.RCC.constrain();
let mut gpiob = dp.GPIOB.split(&mut rcc.ahb);
let mut gpioa = dp.GPIOA.split(&mut rcc.ahb);
let gpiob = dp.GPIOB.split(&mut rcc.ahb);
let gpioa = dp.GPIOA.split(&mut rcc.ahb);

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

// Configure CAN RX and TX pins (AF9)
let rx = gpioa
.pa11
.into_af_push_pull(&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);
let rx = gpioa.pa11.into_af_push_pull();
let tx = gpioa.pa12.into_af_push_pull();

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

let mut led0 = gpiob
.pb15
.into_push_pull_output(&mut gpiob.moder, &mut gpiob.otyper);
let mut led0 = gpiob.pb15.into_push_pull_output();
led0.set_high().unwrap();

// Watchdog makes sure this gets restarted periodically if nothing happens
Expand Down
12 changes: 5 additions & 7 deletions examples/dac_sine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,11 @@ fn main() -> ! {
// let clocks = rcc.cfgr.freeze(&mut dp.FLASH.constrain().acr);

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

// set up led for blinking loop
let mut ok_led = gpioa
.pa15
.into_push_pull_output(&mut gpioa.moder, &mut gpioa.otyper);
let mut ok_led = gpioa.pa15.into_push_pull_output();

// set up dac1, data is twelve bits, aligned right
let mut dac1 = Dac::new(dp.DAC1, &mut rcc.apb1);
Expand All @@ -38,10 +36,10 @@ fn main() -> ! {
cortex_m::asm::delay(8_000);
}
if led {
ok_led.set_low().unwrap();
ok_led.set_low().ok();
led = false;
} else {
ok_led.set_high().unwrap();
ok_led.set_high().ok();
led = true;
}
}
Expand Down
30 changes: 7 additions & 23 deletions examples/gpio_erased.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,15 @@ fn main() -> ! {
let dp = pac::Peripherals::take().unwrap();

let mut rcc = dp.RCC.constrain();
let mut gpiob = dp.GPIOB.split(&mut rcc.ahb);
let mut gpioc = dp.GPIOC.split(&mut rcc.ahb);
let mut gpiod = dp.GPIOD.split(&mut rcc.ahb);
let gpiob = dp.GPIOB.split(&mut rcc.ahb);
let gpioc = dp.GPIOC.split(&mut rcc.ahb);
let gpiod = dp.GPIOD.split(&mut rcc.ahb);

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

hprintln!("Start scanning pin array");
Expand Down
12 changes: 4 additions & 8 deletions examples/gpio_interrupts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,18 @@ fn main() -> ! {
let mut rcc = device_peripherals.RCC.constrain();
let mut syscfg = device_peripherals.SYSCFG.constrain(&mut rcc.apb2);
let mut exti = device_peripherals.EXTI;
let mut gpioe = device_peripherals.GPIOE.split(&mut rcc.ahb);
let mut gpioa = device_peripherals.GPIOA.split(&mut rcc.ahb);
let gpioe = device_peripherals.GPIOE.split(&mut rcc.ahb);
let gpioa = device_peripherals.GPIOA.split(&mut rcc.ahb);

let mut led = gpioe
.pe9
.into_push_pull_output(&mut gpioe.moder, &mut gpioe.otyper);
let mut led = gpioe.pe9.into_push_pull_output();
// Turn the led on so we know the configuration step occurred.
led.toggle().expect("unable to toggle led in configuration");

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

// Configuring the user button to trigger an interrupt when the button is pressed.
let mut user_button = gpioa
.pa0
.into_pull_down_input(&mut gpioa.moder, &mut gpioa.pupdr);
let mut user_button = gpioa.pa0.into_pull_down_input();
syscfg.select_exti_interrupt_source(&user_button);
user_button.trigger_on_edge(&mut exti, Edge::Rising);
user_button.enable_interrupt(&mut exti);
Expand Down
16 changes: 5 additions & 11 deletions examples/i2c_scanner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,13 @@ fn main() -> ! {
let mut rcc = dp.RCC.constrain();
let clocks = rcc.cfgr.freeze(&mut flash.acr);

let mut gpiob = dp.GPIOB.split(&mut rcc.ahb);
let 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 mut scl = gpiob.pb6.into_af_open_drain();
let mut sda = gpiob.pb7.into_af_open_drain();
scl.internal_pull_up(true);
sda.internal_pull_up(true);
let mut i2c = hal::i2c::I2c::new(
dp.I2C1,
(scl, sda),
Expand Down
54 changes: 17 additions & 37 deletions examples/pwm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,41 +31,21 @@ fn main() -> ! {
let clocks = rcc.cfgr.sysclk(16.MHz()).freeze(&mut flash.acr);

// Prep the pins we need in their correct alternate function
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);
let pa6 = gpioa
.pa6
.into_af_push_pull(&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);

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);
let pb1 = gpiob
.pb1
.into_af_push_pull(&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);
let pb5 = gpiob
.pb5
.into_af_push_pull(&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);
let pb10 = gpiob
.pb10
.into_af_push_pull(&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);
let gpioa = dp.GPIOA.split(&mut rcc.ahb);
let pa4 = gpioa.pa4.into_af_push_pull();
let pa6 = gpioa.pa6.into_af_push_pull();
let pa7 = gpioa.pa7.into_af_push_pull();

let gpiob = dp.GPIOB.split(&mut rcc.ahb);
let pb0 = gpiob.pb0.into_af_push_pull();
let pb1 = gpiob.pb1.into_af_push_pull();
let pb4 = gpiob.pb4.into_af_push_pull();
let pb5 = gpiob.pb5.into_af_push_pull();
let pb8 = gpiob.pb8.into_af_push_pull();
let pb10 = gpiob.pb10.into_af_push_pull();

let gpioc = dp.GPIOC.split(&mut rcc.ahb);
let pc10 = gpioc.pc10.into_af_push_pull();

// TIM3
//
Expand Down Expand Up @@ -114,7 +94,7 @@ fn main() -> ! {
// from the channel.
//
// DOES NOT COMPILE
// pb0.into_af15(&mut gpiob.moder, &mut gpiob.afrl);
// pb0.into_af15();

// TIM2
//
Expand Down Expand Up @@ -167,7 +147,7 @@ fn main() -> ! {
// use).
//
// DOES NOT COMPILE
// tim8_ch1.output_to_pc6(gpioc.pc6.into_af4(&mut gpioc.moder, &mut gpioc.afrl));
// tim8_ch1.output_to_pc6(gpioc.pc6.into_af4());

loop {
asm::wfi();
Expand Down
10 changes: 3 additions & 7 deletions examples/serial_dma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,11 @@ fn main() -> ! {
let mut rcc = dp.RCC.constrain();
let clocks = rcc.cfgr.freeze(&mut flash.acr);

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

let pins = (
gpioa
.pa9
.into_af_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh),
gpioa
.pa10
.into_af_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh),
gpioa.pa9.into_af_push_pull(),
gpioa.pa10.into_af_push_pull(),
);
let serial = Serial::new(dp.USART1, pins, 9600.Bd(), clocks, &mut rcc.apb2);
let (tx, rx) = serial.split();
Expand Down
34 changes: 14 additions & 20 deletions examples/serial_echo_rtic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,31 +54,25 @@ mod app {

// Initialize the peripherals
// DIR (the LED that lights up during serial rx)
let mut gpioe = cx.device.GPIOE.split(&mut rcc.ahb);
let mut dir: DirType = gpioe
.pe13
.into_push_pull_output(&mut gpioe.moder, &mut gpioe.otyper);
let gpioe = cx.device.GPIOE.split(&mut rcc.ahb);
let mut dir: DirType = gpioe.pe13.into_push_pull_output();
dir.set_low().unwrap();

// SERIAL
let mut gpioa = cx.device.GPIOA.split(&mut rcc.ahb);
let gpioa = cx.device.GPIOA.split(&mut rcc.ahb);
let mut 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),
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),
// Tx pin
// 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)
gpioa.pa9.into_af_push_pull(),
// Rx pin
// 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)
gpioa.pa10.into_af_push_pull(),
);
pins.1.internal_pull_up(&mut gpioa.pupdr, true);
pins.1.internal_pull_up(true);
let mut serial: SerialType =
Serial::new(cx.device.USART1, pins, 19200.Bd(), clocks, &mut rcc.apb2);
serial.configure_interrupt(Event::ReceiveDataRegisterNotEmpty, Switch::On);
Expand Down
14 changes: 4 additions & 10 deletions examples/spi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn main() -> ! {

let mut flash = dp.FLASH.constrain();
let mut rcc = dp.RCC.constrain();
let mut gpioc = dp.GPIOC.split(&mut rcc.ahb);
let gpioc = dp.GPIOC.split(&mut rcc.ahb);

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

// Configure pins for SPI
let sck = gpioc
.pc10
.into_af_push_pull(&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);
let mosi = gpioc
.pc12
.into_af_push_pull(&mut gpioc.moder, &mut gpioc.otyper, &mut gpioc.afrh);
let sck = gpioc.pc10.into_af_push_pull();
let miso = gpioc.pc11.into_af_push_pull();
let mosi = gpioc.pc12.into_af_push_pull();

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

Expand Down
6 changes: 2 additions & 4 deletions examples/toggle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@ fn main() -> ! {
let dp = pac::Peripherals::take().unwrap();

let mut rcc = dp.RCC.constrain();
let mut gpioe = dp.GPIOE.split(&mut rcc.ahb);
let gpioe = dp.GPIOE.split(&mut rcc.ahb);

let mut led = gpioe
.pe13
.into_push_pull_output(&mut gpioe.moder, &mut gpioe.otyper);
let mut led = gpioe.pe13.into_push_pull_output();

led.set_low().unwrap();

Expand Down
Loading