Skip to content
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

Infallible Pin #341

Closed
wants to merge 2 commits into from
Closed
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Added

- `InputPin`, `OutputPin`, `StatefulOutputPin`, `ToggleableOutputPin` traits.
Remove `embedded_hal` gpio traits from prelude.
- Support for OpenDrain pin configuration on SPI CLK and MOSI pins
- LSB/MSB bit format selection for `SPI`
- Support for CAN peripherals with the `bxcan` crate
Expand Down
7 changes: 3 additions & 4 deletions examples/blinky.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ use panic_halt as _;
use nb::block;

use cortex_m_rt::entry;
use embedded_hal::digital::v2::OutputPin;
use stm32f1xx_hal::{pac, prelude::*, timer::Timer};
use stm32f1xx_hal::{gpio::infallible::*, pac, prelude::*, timer::Timer};

#[entry]
fn main() -> ! {
Expand Down Expand Up @@ -45,8 +44,8 @@ fn main() -> ! {
// Wait for the timer to trigger an update and change the state of the LED
loop {
block!(timer.wait()).unwrap();
led.set_high().unwrap();
led.set_high();
block!(timer.wait()).unwrap();
led.set_low().unwrap();
led.set_low();
}
}
7 changes: 3 additions & 4 deletions examples/blinky_generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ use panic_halt as _;
use nb::block;

use cortex_m_rt::entry;
use embedded_hal::digital::v2::OutputPin;
use stm32f1xx_hal::{pac, prelude::*, timer::Timer};
use stm32f1xx_hal::{gpio::infallible::*, pac, prelude::*, timer::Timer};

#[entry]
fn main() -> ! {
Expand Down Expand Up @@ -39,11 +38,11 @@ fn main() -> ! {
loop {
block!(timer.wait()).unwrap();
for led in leds.iter_mut() {
led.set_high().unwrap();
led.set_high();
}
block!(timer.wait()).unwrap();
for led in leds.iter_mut() {
led.set_low().unwrap();
led.set_low();
}
}
}
7 changes: 3 additions & 4 deletions examples/blinky_rtc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@

use panic_halt as _;

use stm32f1xx_hal::{pac, prelude::*, rtc::Rtc};
use stm32f1xx_hal::{gpio::infallible::*, pac, prelude::*, rtc::Rtc};

use cortex_m_rt::entry;
use embedded_hal::digital::v2::OutputPin;
use nb::block;

#[entry]
Expand Down Expand Up @@ -43,10 +42,10 @@ fn main() -> ! {
rtc.set_alarm(5);
block!(rtc.wait_alarm()).unwrap();
if led_on {
led.set_low().unwrap();
led.set_low();
led_on = false;
} else {
led.set_high().unwrap();
led.set_high();
led_on = true;
}
}
Expand Down
3 changes: 1 addition & 2 deletions examples/blinky_rtcalarm_irq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use panic_halt as _;
use stm32f1xx_hal as hal;

use crate::hal::{
gpio::{gpioc, Output, PushPull},
gpio::{gpioc, infallible::*, Output, PushPull},
pac::{interrupt, Interrupt, Peripherals, EXTI},
prelude::*,
rtc::Rtc,
Expand All @@ -24,7 +24,6 @@ use crate::hal::{
use core::cell::RefCell;
use cortex_m::{asm::wfi, interrupt::Mutex};
use cortex_m_rt::entry;
use embedded_hal::digital::v2::OutputPin;

// A type definition for the GPIO pin to be used for our LED
type LEDPIN = gpioc::PC13<Output<PushPull>>;
Expand Down
3 changes: 1 addition & 2 deletions examples/blinky_timer_irq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use panic_halt as _;
use stm32f1xx_hal as hal;

use crate::hal::{
gpio::{gpioc, Output, PushPull},
gpio::{gpioc, infallible::*, Output, PushPull},
pac::{interrupt, Interrupt, Peripherals, TIM2},
prelude::*,
timer::{CountDownTimer, Event, Timer},
Expand All @@ -24,7 +24,6 @@ use crate::hal::{
use core::cell::RefCell;
use cortex_m::{asm::wfi, interrupt::Mutex};
use cortex_m_rt::entry;
use embedded_hal::digital::v2::OutputPin;

// NOTE You can uncomment 'hprintln' here and in the code below for a bit more
// verbosity at runtime, at the cost of throwing off the timing of the blink
Expand Down
5 changes: 2 additions & 3 deletions examples/can-loopback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ use bxcan::{
use panic_halt as _;

use cortex_m_rt::entry;
use embedded_hal::digital::v2::OutputPin;
use nb::block;
use stm32f1xx_hal::{can::Can, pac, prelude::*};
use stm32f1xx_hal::{can::Can, gpio::infallible::*, pac, prelude::*};

#[entry]
fn main() -> ! {
Expand Down Expand Up @@ -111,7 +110,7 @@ fn main() -> ! {

let mut gpiob = dp.GPIOB.split(&mut rcc.apb2);
let mut led = gpiob.pb9.into_push_pull_output(&mut gpiob.crh);
led.set_high().unwrap();
led.set_high();

loop {}
}
7 changes: 3 additions & 4 deletions examples/delay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
use panic_halt as _;

use cortex_m_rt::entry;
use embedded_hal::digital::v2::OutputPin;
use stm32f1xx_hal::{delay::Delay, pac, prelude::*};
use stm32f1xx_hal::{delay::Delay, gpio::infallible::*, pac, prelude::*};

#[entry]
fn main() -> ! {
Expand All @@ -34,9 +33,9 @@ fn main() -> ! {
let mut delay = Delay::new(cp.SYST, clocks);

loop {
led.set_high().unwrap();
led.set_high();
delay.delay_ms(1_000_u16);
led.set_low().unwrap();
led.set_low();
delay.delay_ms(1_000_u16);
}
}
4 changes: 2 additions & 2 deletions examples/exti.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use core::mem::MaybeUninit;
use cortex_m_rt::entry;
use pac::interrupt;
use stm32f1xx_hal::gpio::*;
use stm32f1xx_hal::{pac, prelude::*};
use stm32f1xx_hal::{gpio::infallible::*, pac, prelude::*};

// These two are owned by the ISR. main() may only access them during the initialization phase,
// where the interrupt is not yet enabled (i.e. no concurrent accesses can occur).
Expand All @@ -29,7 +29,7 @@ fn EXTI9_5() {
let int_pin = unsafe { &mut *INT_PIN.as_mut_ptr() };

if int_pin.check_interrupt() {
led.toggle().unwrap();
led.toggle();

// if we don't clear this bit, the ISR would trigger indefinitely
int_pin.clear_interrupt_pending_bit();
Expand Down
21 changes: 4 additions & 17 deletions examples/led.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
use panic_halt as _;

use cortex_m_rt::entry;
use embedded_hal::digital::v2::OutputPin;
use stm32f1xx_hal::{pac, prelude::*};
use stm32f1xx_hal::{gpio::infallible::*, pac, prelude::*};

#[entry]
fn main() -> ! {
Expand All @@ -27,25 +26,13 @@ fn main() -> ! {
let mut gpioc = p.GPIOC.split(&mut rcc.apb2);

#[cfg(feature = "stm32f100")]
gpioc
.pc9
.into_push_pull_output(&mut gpioc.crh)
.set_high()
.unwrap();
gpioc.pc9.into_push_pull_output(&mut gpioc.crh).set_high();

#[cfg(feature = "stm32f101")]
gpioc
.pc9
.into_push_pull_output(&mut gpioc.crh)
.set_high()
.unwrap();
gpioc.pc9.into_push_pull_output(&mut gpioc.crh).set_high();

#[cfg(any(feature = "stm32f103", feature = "stm32f105", feature = "stm32f107"))]
gpioc
.pc13
.into_push_pull_output(&mut gpioc.crh)
.set_low()
.unwrap();
gpioc.pc13.into_push_pull_output(&mut gpioc.crh).set_low();

loop {}
}
6 changes: 3 additions & 3 deletions examples/mfrc522.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use panic_itm as _;
use cortex_m::iprintln;

use cortex_m_rt::entry;
use embedded_hal::digital::{v1_compat::OldOutputPin, v2::OutputPin};
use embedded_hal::digital::v1_compat::OldOutputPin;
use mfrc522::Mfrc522;
use stm32f1xx_hal::{pac, prelude::*, spi::Spi};
use stm32f1xx_hal::{gpio::infallible::*, pac, prelude::*, spi::Spi};

#[entry]
fn main() -> ! {
Expand Down Expand Up @@ -42,7 +42,7 @@ fn main() -> ! {
let mut mfrc522 = Mfrc522::new(spi, OldOutputPin::from(nss)).unwrap();

let mut led = gpioc.pc13.into_push_pull_output(&mut gpioc.crh);
led.set_high().unwrap();
led.set_high();

loop {
if let Ok(atqa) = mfrc522.reqa() {
Expand Down
16 changes: 10 additions & 6 deletions examples/multi_mode_gpio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ use nb::block;

use cortex_m_rt::entry;
use cortex_m_semihosting::hprintln;
use embedded_hal::digital::v2::{InputPin, OutputPin};
use stm32f1xx_hal::{gpio::State, pac, prelude::*, timer::Timer};
use stm32f1xx_hal::{
gpio::{infallible::*, State},
pac,
prelude::*,
timer::Timer,
};

#[entry]
fn main() -> ! {
Expand Down Expand Up @@ -37,16 +41,16 @@ fn main() -> ! {
// Wait for the timer to trigger an update and change the state of the LED
loop {
block!(timer.wait()).unwrap();
hprintln!("{}", pin.is_high().unwrap()).unwrap();
hprintln!("{}", pin.is_high()).unwrap();
pin.as_push_pull_output(&mut gpioc.crh, |out| {
out.set_high().unwrap();
out.set_high();
block!(timer.wait()).unwrap();
out.set_low().unwrap();
out.set_low();
block!(timer.wait()).unwrap();
});
pin.as_push_pull_output_with_state(&mut gpioc.crh, State::High, |out| {
block!(timer.wait()).unwrap();
out.set_low().unwrap();
out.set_low();
block!(timer.wait()).unwrap();
});
}
Expand Down
8 changes: 4 additions & 4 deletions examples/nojtag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use panic_halt as _;

use cortex_m_rt::entry;
use stm32f1xx_hal::{pac, prelude::*};
use stm32f1xx_hal::{gpio::infallible::*, pac, prelude::*};

#[entry]
fn main() -> ! {
Expand All @@ -25,9 +25,9 @@ fn main() -> ! {
let mut pb4 = pb4.into_push_pull_output(&mut gpiob.crl);

loop {
pa15.toggle().unwrap();
pb3.toggle().unwrap();
pb4.toggle().unwrap();
pa15.toggle();
pb3.toggle();
pb4.toggle();
cortex_m::asm::delay(8_000_000);
}
}
6 changes: 3 additions & 3 deletions examples/timer-interrupt-rtic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use panic_halt as _;

use rtic::app;

use embedded_hal::digital::v2::OutputPin;
use stm32f1xx_hal::{
gpio::infallible::*,
gpio::{gpioc::PC13, Output, PushPull, State},
pac,
prelude::*,
Expand Down Expand Up @@ -85,10 +85,10 @@ const APP: () = {

if *cx.resources.led_state {
// Uses resources managed by rtic to turn led off (on bluepill)
cx.resources.led.set_high().unwrap();
cx.resources.led.set_high();
*cx.resources.led_state = false;
} else {
cx.resources.led.set_low().unwrap();
cx.resources.led.set_low();
*cx.resources.led_state = true;
}
*COUNT += 1;
Expand Down
9 changes: 6 additions & 3 deletions examples/usb_serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@ extern crate panic_semihosting;

use cortex_m::asm::delay;
use cortex_m_rt::entry;
use embedded_hal::digital::v2::OutputPin;
use stm32f1xx_hal::usb::{Peripheral, UsbBus};
use stm32f1xx_hal::{prelude::*, stm32};
use stm32f1xx_hal::{
gpio::infallible::*,
pac,
prelude::*,
usb::{Peripheral, UsbBus},
};
use usb_device::prelude::*;
use usbd_serial::{SerialPort, USB_CLASS_CDC};

Expand Down
10 changes: 6 additions & 4 deletions examples/usb_serial_interrupt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ extern crate panic_semihosting;

use cortex_m::asm::{delay, wfi};
use cortex_m_rt::entry;
use embedded_hal::digital::v2::OutputPin;
use stm32f1xx_hal::pac::{interrupt, Interrupt};
use stm32f1xx_hal::usb::{Peripheral, UsbBus, UsbBusType};
use stm32f1xx_hal::{prelude::*, stm32};
use stm32f1xx_hal::{
gpio::infallible::*,
pac::{self, interrupt, Interrupt},
prelude::*,
usb::{Peripheral, UsbBus, UsbBusType},
};
use usb_device::{bus::UsbBusAllocator, prelude::*};
use usbd_serial::{SerialPort, USB_CLASS_CDC};

Expand Down
10 changes: 6 additions & 4 deletions examples/usb_serial_rtic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
extern crate panic_semihosting;

use cortex_m::asm::delay;
use embedded_hal::digital::v2::OutputPin;
use rtic::app;
use stm32f1xx_hal::prelude::*;
use stm32f1xx_hal::usb::{Peripheral, UsbBus, UsbBusType};
use stm32f1xx_hal::{
gpio::infallible::*,
prelude::*,
usb::{Peripheral, UsbBus, UsbBusType},
};
use usb_device::bus;
use usb_device::prelude::*;
use usbd_serial::{SerialPort, USB_CLASS_CDC};
Expand Down Expand Up @@ -45,7 +47,7 @@ const APP: () = {
// This forced reset is needed only for development, without it host
// will not reset your device when you upload new firmware.
let mut usb_dp = gpioa.pa12.into_push_pull_output(&mut gpioa.crh);
usb_dp.set_low().unwrap();
usb_dp.set_low();
delay(clocks.sysclk().0 / 100);

let usb_dm = gpioa.pa11;
Expand Down
Loading