From 912d8c33edf93d7a1aff97223abc0f48457a217b Mon Sep 17 00:00:00 2001 From: Andrey Zgarbul Date: Fri, 16 Jul 2021 18:40:14 +0300 Subject: [PATCH 1/8] pac instead of stm32 --- CHANGELOG.md | 1 + examples/adc_dma_rtic.rs | 7 +++---- examples/analog-stopwatch-with-spi-ssd1306.rs | 21 +++++++++---------- examples/blinky-timer-irq.rs | 2 +- examples/delay-syst-blinky.rs | 4 ++-- examples/delay-timer-blinky.rs | 4 ++-- examples/dwt-blinky.rs | 4 ++-- examples/i2s-audio-out-dma.rs | 2 +- examples/pwm-input.rs | 4 ++-- examples/qei.rs | 4 ++-- examples/rng-display.rs | 4 ++-- examples/rtic.rs | 2 +- examples/sd.rs | 5 ++--- examples/serial-9bit.rs | 4 ++-- examples/serial.rs | 4 ++-- examples/ssd1306-image.rs | 4 ++-- .../stopwatch-with-ssd1306-and-interrupts.rs | 17 +++++++-------- examples/timer-periph.rs | 4 ++-- examples/timer-syst.rs | 4 ++-- examples/usb_serial.rs | 4 ++-- src/flash.rs | 4 ++-- src/lib.rs | 1 + src/prelude.rs | 4 ++-- src/pwm_input.rs | 6 +++--- src/rng.rs | 8 +++---- 25 files changed, 63 insertions(+), 65 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 64fffff4..30ca0088 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Added +- Deprecate `stm32` alias. - Temporary change pin mode - More badges in README - `RccBus` & `GetBusFreq` traits. `AHBx`, `APBx` structures. diff --git a/examples/adc_dma_rtic.rs b/examples/adc_dma_rtic.rs index 9c9460fb..9209881d 100644 --- a/examples/adc_dma_rtic.rs +++ b/examples/adc_dma_rtic.rs @@ -12,17 +12,16 @@ use stm32f4xx_hal::{ Adc, Temperature, }, dma::{config::DmaConfig, PeripheralToMemory, Stream0, StreamsTuple, Transfer}, + pac::{self, ADC1, DMA2}, prelude::*, signature::{VtempCal110, VtempCal30}, - stm32, - stm32::{ADC1, DMA2}, }; const POLLING_PERIOD: u32 = 168_000_000 / 2; type DMATransfer = Transfer, Adc, PeripheralToMemory, &'static mut [u16; 2], 0>; -#[rtic::app(device = stm32f4xx_hal::stm32, peripherals = true, monotonic = rtic::cyccnt::CYCCNT)] +#[rtic::app(device = stm32f4xx_hal::pac, peripherals = true, monotonic = rtic::cyccnt::CYCCNT)] const APP: () = { struct Resources { transfer: DMATransfer, @@ -31,7 +30,7 @@ const APP: () = { #[init(schedule=[polling])] fn init(cx: init::Context) -> init::LateResources { - let device: stm32::Peripherals = cx.device; + let device: pac::Peripherals = cx.device; let rcc = device.RCC.constrain(); let _clocks = rcc diff --git a/examples/analog-stopwatch-with-spi-ssd1306.rs b/examples/analog-stopwatch-with-spi-ssd1306.rs index d941943e..0c2736ac 100644 --- a/examples/analog-stopwatch-with-spi-ssd1306.rs +++ b/examples/analog-stopwatch-with-spi-ssd1306.rs @@ -12,11 +12,10 @@ use stm32f4xx_hal as hal; use crate::hal::{ gpio::{gpioa::PA0, Edge, Input, PullDown}, - interrupt, pac, + interrupt, pac, pac, prelude::*, rcc::{Clocks, Rcc}, spi::Spi, - stm32, timer::{CountDownTimer, Event, Timer}, }; @@ -47,7 +46,7 @@ use ssd1306::{prelude::*, Builder}; // Set up global state. It's all mutexed up for concurrency safety. static ELAPSED_MS: Mutex> = Mutex::new(Cell::new(0u32)); static ELAPSED_RESET_MS: Mutex> = Mutex::new(Cell::new(0u32)); -static TIMER_TIM2: Mutex>>> = +static TIMER_TIM2: Mutex>>> = Mutex::new(RefCell::new(None)); static STATE: Mutex> = Mutex::new(Cell::new(StopwatchState::Ready)); static BUTTON: Mutex>>>> = Mutex::new(RefCell::new(None)); @@ -139,10 +138,10 @@ fn main() -> ! { }); // Enable interrupts - stm32::NVIC::unpend(hal::stm32::Interrupt::TIM2); - stm32::NVIC::unpend(hal::stm32::Interrupt::EXTI0); + pac::NVIC::unpend(hal::pac::Interrupt::TIM2); + pac::NVIC::unpend(hal::pac::Interrupt::EXTI0); unsafe { - stm32::NVIC::unmask(hal::stm32::Interrupt::EXTI0); + pac::NVIC::unmask(hal::pac::Interrupt::EXTI0); }; let mut state_led = false; @@ -295,29 +294,29 @@ fn TIM2() { fn stopwatch_start<'cs>(cs: &'cs CriticalSection) { ELAPSED_MS.borrow(cs).replace(0); unsafe { - stm32::NVIC::unmask(hal::stm32::Interrupt::TIM2); + pac::NVIC::unmask(hal::pac::Interrupt::TIM2); } } fn stopwatch_continue<'cs>(_cs: &'cs CriticalSection) { unsafe { - stm32::NVIC::unmask(hal::stm32::Interrupt::TIM2); + pac::NVIC::unmask(hal::pac::Interrupt::TIM2); } } fn stopwatch_stop<'cs>(_cs: &'cs CriticalSection) { - stm32::NVIC::mask(hal::stm32::Interrupt::TIM2); + pac::NVIC::mask(hal::pac::Interrupt::TIM2); } fn stopwatch_reset_start<'cs>(cs: &'cs CriticalSection) { ELAPSED_RESET_MS.borrow(cs).replace(0); unsafe { - stm32::NVIC::unmask(hal::stm32::Interrupt::TIM2); + pac::NVIC::unmask(hal::pac::Interrupt::TIM2); } } fn stopwatch_reset_stop<'cs>(_cs: &'cs CriticalSection) { - stm32::NVIC::mask(hal::stm32::Interrupt::TIM2); + pac::NVIC::mask(hal::pac::Interrupt::TIM2); } // Formatting requires the arrayvec crate diff --git a/examples/blinky-timer-irq.rs b/examples/blinky-timer-irq.rs index a9792f42..7cbfa12b 100644 --- a/examples/blinky-timer-irq.rs +++ b/examples/blinky-timer-irq.rs @@ -12,8 +12,8 @@ use stm32f4xx_hal as hal; use crate::hal::{ gpio::{gpioa, Output, PushPull}, + pac::{interrupt, Interrupt, Peripherals, TIM2}, prelude::*, - stm32::{interrupt, Interrupt, Peripherals, TIM2}, timer::{CountDownTimer, Event, Timer}, }; diff --git a/examples/delay-syst-blinky.rs b/examples/delay-syst-blinky.rs index 87881370..d86ba07e 100644 --- a/examples/delay-syst-blinky.rs +++ b/examples/delay-syst-blinky.rs @@ -11,12 +11,12 @@ use cortex_m; use cortex_m_rt::entry; use stm32f4xx_hal as hal; -use crate::hal::{prelude::*, stm32}; +use crate::hal::{pac, prelude::*}; #[entry] fn main() -> ! { if let (Some(dp), Some(cp)) = ( - stm32::Peripherals::take(), + pac::Peripherals::take(), cortex_m::peripheral::Peripherals::take(), ) { // Set up the LED. On the Nucleo-446RE it's connected to pin PA5. diff --git a/examples/delay-timer-blinky.rs b/examples/delay-timer-blinky.rs index df923c89..6d210cca 100644 --- a/examples/delay-timer-blinky.rs +++ b/examples/delay-timer-blinky.rs @@ -11,12 +11,12 @@ use cortex_m; use cortex_m_rt::entry; use stm32f4xx_hal as hal; -use crate::hal::{prelude::*, stm32}; +use crate::hal::{pac, prelude::*}; #[entry] fn main() -> ! { if let (Some(dp), Some(_cp)) = ( - stm32::Peripherals::take(), + pac::Peripherals::take(), cortex_m::peripheral::Peripherals::take(), ) { // Set up the LED. On the Mini-F4 it's connected to pin PC13. diff --git a/examples/dwt-blinky.rs b/examples/dwt-blinky.rs index 36bff2ea..7849730f 100644 --- a/examples/dwt-blinky.rs +++ b/examples/dwt-blinky.rs @@ -5,8 +5,8 @@ // Halt on panic use crate::hal::{ dwt::{ClockDuration, DwtExt}, + pac, prelude::*, - stm32, }; use cortex_m; use cortex_m_rt::entry; @@ -16,7 +16,7 @@ use stm32f4xx_hal as hal; #[entry] fn main() -> ! { if let (Some(dp), Some(cp)) = ( - stm32::Peripherals::take(), + pac::Peripherals::take(), cortex_m::peripheral::Peripherals::take(), ) { // Set up the LEDs. On the STM32F429I-DISC[O1] they are connected to pin PG13/14. diff --git a/examples/i2s-audio-out-dma.rs b/examples/i2s-audio-out-dma.rs index 7ef8e3f8..d0713018 100644 --- a/examples/i2s-audio-out-dma.rs +++ b/examples/i2s-audio-out-dma.rs @@ -60,8 +60,8 @@ use stm32f4xx_hal::i2c::I2c; use stm32f4xx_hal::i2s::I2s; use stm32f4xx_hal::pac::{interrupt, Interrupt}; use stm32f4xx_hal::pac::{CorePeripherals, Peripherals}; +use stm32f4xx_hal::pac::{DMA1, SPI3}; use stm32f4xx_hal::prelude::*; -use stm32f4xx_hal::stm32::{DMA1, SPI3}; use cs43l22::{Cs43L22, Register}; diff --git a/examples/pwm-input.rs b/examples/pwm-input.rs index 4e0e2f30..cd111291 100644 --- a/examples/pwm-input.rs +++ b/examples/pwm-input.rs @@ -7,11 +7,11 @@ use panic_halt as _; use cortex_m; use cortex_m_rt::entry; -use stm32f4xx_hal::{prelude::*, stm32, timer::Timer}; +use stm32f4xx_hal::{pac, prelude::*, timer::Timer}; #[entry] fn main() -> ! { - if let Some(dp) = stm32::Peripherals::take() { + if let Some(dp) = pac::Peripherals::take() { // Set up the system clock. let rcc = dp.RCC.constrain(); let clocks = rcc.cfgr.freeze(); diff --git a/examples/qei.rs b/examples/qei.rs index b1045642..3ebbc3bf 100644 --- a/examples/qei.rs +++ b/examples/qei.rs @@ -15,11 +15,11 @@ use panic_halt as _; use cortex_m; use cortex_m_rt::entry; use embedded_hal::Direction as RotaryDirection; -use stm32f4xx_hal::{delay::Delay, prelude::*, qei::Qei, stm32}; +use stm32f4xx_hal::{delay::Delay, pac, prelude::*, qei::Qei}; #[entry] fn main() -> ! { - let dp = stm32::Peripherals::take().expect("Failed to get stm32 peripherals"); + let dp = pac::Peripherals::take().expect("Failed to get stm32 peripherals"); let cp = cortex_m::peripheral::Peripherals::take().expect("Failed to get cortex_m peripherals"); // Set up the LED. This is pin C13 on the "black pill" USB C board here: diff --git a/examples/rng-display.rs b/examples/rng-display.rs index 57460317..9cd81561 100644 --- a/examples/rng-display.rs +++ b/examples/rng-display.rs @@ -28,7 +28,7 @@ use embedded_graphics::{ use ssd1306::{prelude::*, Builder, I2CDIBuilder}; -use hal::{i2c::I2c, prelude::*, stm32}; +use hal::{i2c::I2c, pac, prelude::*}; use rand_core::RngCore; use arrayvec::ArrayString; @@ -45,7 +45,7 @@ pub const HINSET_PIX: i32 = 20; #[entry] fn main() -> ! { if let (Some(dp), Some(cp)) = ( - stm32::Peripherals::take(), + pac::Peripherals::take(), cortex_m::peripheral::Peripherals::take(), ) { // Set up the system clock. diff --git a/examples/rtic.rs b/examples/rtic.rs index ba2de2b2..9d52c18c 100644 --- a/examples/rtic.rs +++ b/examples/rtic.rs @@ -12,7 +12,7 @@ use stm32f4xx_hal::{ prelude::*, }; -#[app(device = stm32f4xx_hal::stm32, peripherals = true)] +#[app(device = stm32f4xx_hal::pac, peripherals = true)] const APP: () = { struct Resources { button: PA0>, diff --git a/examples/sd.rs b/examples/sd.rs index 120bfec5..ce6cc684 100644 --- a/examples/sd.rs +++ b/examples/sd.rs @@ -6,15 +6,14 @@ use cortex_m_semihosting::{hprint, hprintln}; use panic_semihosting as _; use stm32f4xx_hal::{ - delay, + delay, pac, prelude::*, sdio::{ClockFreq, Sdio}, - stm32, }; #[entry] fn main() -> ! { - let device = stm32::Peripherals::take().unwrap(); + let device = pac::Peripherals::take().unwrap(); let core = cortex_m::Peripherals::take().unwrap(); let rcc = device.RCC.constrain(); diff --git a/examples/serial-9bit.rs b/examples/serial-9bit.rs index a112e052..de76b2b6 100644 --- a/examples/serial-9bit.rs +++ b/examples/serial-9bit.rs @@ -34,13 +34,13 @@ use panic_halt as _; use cortex_m_rt::entry; use stm32f4xx_hal as hal; -use crate::hal::{block, prelude::*, serial::config::Config, serial::Serial, stm32}; +use crate::hal::{block, pac, prelude::*, serial::config::Config, serial::Serial}; use core::ops::Range; #[entry] fn main() -> ! { - let dp = stm32::Peripherals::take().unwrap(); + let dp = pac::Peripherals::take().unwrap(); let cp = cortex_m::peripheral::Peripherals::take().unwrap(); let gpioa = dp.GPIOA.split(); diff --git a/examples/serial.rs b/examples/serial.rs index fff23427..f5f3e3ab 100644 --- a/examples/serial.rs +++ b/examples/serial.rs @@ -6,13 +6,13 @@ use panic_halt as _; use cortex_m_rt::entry; use stm32f4xx_hal as hal; -use crate::hal::{prelude::*, serial::config::Config, serial::Serial, stm32}; +use crate::hal::{pac, prelude::*, serial::config::Config, serial::Serial}; use core::fmt::Write; // for pretty formatting of the serial output #[entry] fn main() -> ! { - let dp = stm32::Peripherals::take().unwrap(); + let dp = pac::Peripherals::take().unwrap(); let cp = cortex_m::peripheral::Peripherals::take().unwrap(); let gpioa = dp.GPIOA.split(); diff --git a/examples/ssd1306-image.rs b/examples/ssd1306-image.rs index 73458a77..df3c1684 100644 --- a/examples/ssd1306-image.rs +++ b/examples/ssd1306-image.rs @@ -20,12 +20,12 @@ use cortex_m_rt::{entry, exception}; use embedded_graphics::{image::Image, image::ImageRaw, pixelcolor::BinaryColor, prelude::*}; use ssd1306::{prelude::*, Builder, I2CDIBuilder}; -use crate::hal::{i2c::I2c, prelude::*, stm32}; +use crate::hal::{i2c::I2c, pac, prelude::*}; #[entry] fn main() -> ! { if let (Some(dp), Some(_cp)) = ( - stm32::Peripherals::take(), + pac::Peripherals::take(), cortex_m::peripheral::Peripherals::take(), ) { // Set up the system clock. We want to run at 48MHz for this one. diff --git a/examples/stopwatch-with-ssd1306-and-interrupts.rs b/examples/stopwatch-with-ssd1306-and-interrupts.rs index d7262072..12c273c4 100644 --- a/examples/stopwatch-with-ssd1306-and-interrupts.rs +++ b/examples/stopwatch-with-ssd1306-and-interrupts.rs @@ -24,10 +24,9 @@ use crate::hal::{ delay::Delay, gpio::{gpioc::PC13, Edge, Input, PullUp}, i2c::I2c, - interrupt, + interrupt, pac, prelude::*, rcc::{Clocks, Rcc}, - stm32, timer::{CountDownTimer, Event, Timer}, }; use arrayvec::ArrayString; @@ -46,7 +45,7 @@ use ssd1306::{prelude::*, Builder, I2CDIBuilder}; // Set up global state. It's all mutexed up for concurrency safety. static ELAPSED_MS: Mutex> = Mutex::new(Cell::new(0u32)); -static TIMER_TIM2: Mutex>>> = +static TIMER_TIM2: Mutex>>> = Mutex::new(RefCell::new(None)); static STATE: Mutex> = Mutex::new(Cell::new(StopwatchState::Ready)); static BUTTON: Mutex>>>> = Mutex::new(RefCell::new(None)); @@ -60,7 +59,7 @@ enum StopwatchState { #[entry] fn main() -> ! { - if let (Some(mut dp), Some(cp)) = (stm32::Peripherals::take(), cortex_m::Peripherals::take()) { + if let (Some(mut dp), Some(cp)) = (pac::Peripherals::take(), cortex_m::Peripherals::take()) { let rcc = dp.RCC.constrain(); let clocks = setup_clocks(rcc); let gpiob = dp.GPIOB.split(); @@ -98,10 +97,10 @@ fn main() -> ! { }); // Enable interrupts - stm32::NVIC::unpend(hal::stm32::Interrupt::TIM2); - stm32::NVIC::unpend(hal::stm32::Interrupt::EXTI15_10); + pac::NVIC::unpend(hal::pac::Interrupt::TIM2); + pac::NVIC::unpend(hal::pac::Interrupt::EXTI15_10); unsafe { - stm32::NVIC::unmask(hal::stm32::Interrupt::EXTI15_10); + pac::NVIC::unmask(hal::pac::Interrupt::EXTI15_10); }; let mut delay = Delay::new(cp.SYST, &clocks); @@ -201,12 +200,12 @@ fn setup_clocks(rcc: Rcc) -> Clocks { fn stopwatch_start<'cs>(cs: &'cs CriticalSection) { ELAPSED_MS.borrow(cs).replace(0); unsafe { - stm32::NVIC::unmask(hal::stm32::Interrupt::TIM2); + pac::NVIC::unmask(hal::pac::Interrupt::TIM2); } } fn stopwatch_stop<'cs>(_cs: &'cs CriticalSection) { - stm32::NVIC::mask(hal::stm32::Interrupt::TIM2); + pac::NVIC::mask(hal::pac::Interrupt::TIM2); } // Formatting requires the arrayvec crate diff --git a/examples/timer-periph.rs b/examples/timer-periph.rs index 0ed2328d..00e10d26 100644 --- a/examples/timer-periph.rs +++ b/examples/timer-periph.rs @@ -20,11 +20,11 @@ use hal::timer; use hal::timer::Timer; use stm32f4xx_hal as hal; -use crate::hal::{prelude::*, stm32}; +use crate::hal::{pac, prelude::*}; #[entry] fn main() -> ! { - let dp = stm32::Peripherals::take().unwrap(); + let dp = pac::Peripherals::take().unwrap(); let rcc = dp.RCC.constrain(); let clocks = rcc.cfgr.sysclk(24.mhz()).freeze(); diff --git a/examples/timer-syst.rs b/examples/timer-syst.rs index 22a67fa8..c9e91fad 100644 --- a/examples/timer-syst.rs +++ b/examples/timer-syst.rs @@ -20,11 +20,11 @@ use hal::timer; use hal::timer::Timer; use stm32f4xx_hal as hal; -use crate::hal::{prelude::*, stm32}; +use crate::hal::{pac, prelude::*}; #[entry] fn main() -> ! { - let dp = stm32::Peripherals::take().unwrap(); + let dp = pac::Peripherals::take().unwrap(); let cp = cortex_m::peripheral::Peripherals::take().unwrap(); let rcc = dp.RCC.constrain(); let clocks = rcc.cfgr.sysclk(24.mhz()).freeze(); diff --git a/examples/usb_serial.rs b/examples/usb_serial.rs index e778a508..50721ab8 100644 --- a/examples/usb_serial.rs +++ b/examples/usb_serial.rs @@ -7,14 +7,14 @@ use panic_halt as _; use cortex_m_rt::entry; use stm32f4xx_hal::otg_fs::{UsbBus, USB}; -use stm32f4xx_hal::{prelude::*, stm32}; +use stm32f4xx_hal::{pac, prelude::*}; use usb_device::prelude::*; static mut EP_MEMORY: [u32; 1024] = [0; 1024]; #[entry] fn main() -> ! { - let dp = stm32::Peripherals::take().unwrap(); + let dp = pac::Peripherals::take().unwrap(); let rcc = dp.RCC.constrain(); diff --git a/src/flash.rs b/src/flash.rs index 22a06803..367ca255 100644 --- a/src/flash.rs +++ b/src/flash.rs @@ -1,5 +1,5 @@ +use crate::pac::FLASH; use crate::signature::FlashSize; -use crate::stm32::FLASH; use core::{ptr, slice}; /// Flash erase/program error @@ -31,7 +31,7 @@ impl Error { } } -/// Flash methods implemented for `stm32::FLASH` +/// Flash methods implemented for `pac::FLASH` #[allow(clippy::len_without_is_empty)] pub trait FlashExt { /// Memory-mapped address diff --git a/src/lib.rs b/src/lib.rs index 2fedc788..557708d6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -138,6 +138,7 @@ pub mod otg_hs; pub mod rng; #[cfg(feature = "device-selected")] +#[deprecated(since = "0.10.0", note = "Please use pac instead")] pub use pac as stm32; #[cfg(feature = "device-selected")] diff --git a/src/prelude.rs b/src/prelude.rs index 0034f453..540878a1 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -4,7 +4,7 @@ //! helper traits already imported. //! Otherwise the use of peripherals would require the import of the //! corresponding module and the import of the trait, which connects this HAL -//! to the autogenerated svd2rust API in [crate::stm32]. +//! to the autogenerated svd2rust API in [crate::pac]. //! //! # Example //! @@ -13,7 +13,7 @@ //! ``` //! #[entry] //! fn main() -> ! { -//! let dp = stm32::Peripherals::take().unwrap(); +//! let dp = pac::Peripherals::take().unwrap(); //! let gpiog = dp.GPIOG.split(); //! let mut led1 = gpiog.pg13.into_push_pull_output(); //! led1.set_high().unwrap(); diff --git a/src/pwm_input.rs b/src/pwm_input.rs index f32fa0b9..f6cb9811 100644 --- a/src/pwm_input.rs +++ b/src/pwm_input.rs @@ -15,13 +15,13 @@ impl Pins for PC1 where PC1: PinC1 {} /// 2. When the period is captured. the duty cycle will be an observable value. /// An example interrupt handler is provided: /// ``` -/// use stm32f4xx_hal::stm32::TIM8; +/// use stm32f4xx_hal::pac::TIM8; /// use stm32f4xx_hal::timer::Timer; /// use stm32f4xx_hal::pwm_input::PwmInput; /// use stm32f4xx_hal::gpio::gpioc::PC6; /// use stm32f4xx_hal::gpio::Alternate; /// -/// type Monitor = PwmInput>>; +/// type Monitor = PwmInput>>; /// /// fn tim8_cc2(monitor: &Monitor) { /// let duty_clocks = monitor.get_duty_cycle_clocks(); @@ -46,7 +46,7 @@ macro_rules! hal { // Drag the associated TIM object into scope. // Note: its drawn in via the macro to avoid duplicating the feature gate this macro is // expecting to be guarded by. - use crate::stm32::$TIM; + use crate::pac::$TIM; impl Timer<$TIM> { /// Configures this timer for PWM input. Accepts the `best_guess` frequency of the signal diff --git a/src/rng.rs b/src/rng.rs index f777cc50..2c2c777a 100644 --- a/src/rng.rs +++ b/src/rng.rs @@ -12,7 +12,7 @@ //! //! Minimal working example: //! ``` -//! let dp = stm32::Peripherals::take().unwrap(); +//! let dp = pac::Peripherals::take().unwrap(); //! let rcc = dp.RCC.constrain(); //! let clocks = rcc.cfgr.require_pll48clk().freeze(); //! let mut rand_source = dp.RNG.constrain(clocks); @@ -52,12 +52,12 @@ impl From for rand_core::Error { } /// Helper trait to implement the `constrain` method for the -/// [RNG peripheral](crate::stm32::RNG) which is how the [Rng] struct is +/// [RNG peripheral](crate::pac::RNG) which is how the [Rng] struct is /// created. /// /// Usage: /// ``` -/// let dp = stm32::Peripherals::take().unwrap(); +/// let dp = pac::Peripherals::take().unwrap(); /// let rcc = dp.RCC.constrain(); /// let clocks = rcc.cfgr.require_pll48clk().freeze(); /// let mut rand_source = dp.RNG.constrain(clocks); @@ -138,7 +138,7 @@ impl Rng { } } - /// Releases ownership of the [RNG](crate::stm32::RNG) peripheral object + /// Releases ownership of the [RNG](crate::pac::RNG) peripheral object /// (after which `self` can't be used anymore). pub fn release(self) -> RNG { self.rb From d2e17ca89aefd165c47fca8d0ded869e9bb353c0 Mon Sep 17 00:00:00 2001 From: Andrey Zgarbul Date: Fri, 16 Jul 2021 19:20:43 +0300 Subject: [PATCH 2/8] examples --- CHANGELOG.md | 1 + Cargo.toml | 58 ++++++++++++------- examples/analog-stopwatch-with-spi-ssd1306.rs | 2 +- examples/can-send.rs | 2 +- src/pwm_input.rs | 1 + src/timer.rs | 17 +----- 6 files changed, 43 insertions(+), 38 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 30ca0088..34a698f4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Added +- Weaker constrains for examples. - Deprecate `stm32` alias. - Temporary change pin mode - More badges in README diff --git a/Cargo.toml b/Cargo.toml index 99b8e65d..8ecc67d5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -73,6 +73,7 @@ stm32f401 = ["stm32f4/stm32f401", "device-selected", "otg-fs", "sdio", "spi3", "spi4", + "tim2", ] stm32f405 = ["stm32f4/stm32f405", "device-selected", "adc2", "adc3", @@ -86,6 +87,7 @@ stm32f405 = ["stm32f4/stm32f405", "device-selected", "rng", "sdio", "spi3", + "tim2", "tim8", "usart3", "uart4", "uart5", ] stm32f407 = ["stm32f4/stm32f407", "device-selected", @@ -100,6 +102,7 @@ stm32f407 = ["stm32f4/stm32f407", "device-selected", "rng", "sdio", "spi3", + "tim2", "tim8", "usart3", "uart4", "uart5", ] stm32f410 = ["stm32f4/stm32f410", "device-selected", @@ -111,6 +114,7 @@ stm32f411 = ["stm32f4/stm32f411", "device-selected", "i2c3", "otg-fs", "sdio", + "tim2", "spi3", "spi4", "spi5", ] stm32f412 = ["stm32f4/stm32f412", "device-selected", @@ -122,6 +126,7 @@ stm32f412 = ["stm32f4/stm32f412", "device-selected", "rng", "sdio", "spi3", "spi4", "spi5", + "tim2", "tim8", "usart3", ] stm32f413 = ["stm32f4/stm32f413", "device-selected", @@ -135,6 +140,7 @@ stm32f413 = ["stm32f4/stm32f413", "device-selected", "rng", "sdio", "spi3", "spi4", "spi5", + "tim2", "tim8", "usart3", "uart4", "uart5", "uart7", "uart8", "uart9", "uart10", ] stm32f415 = ["stm32f4/stm32f405", "device-selected", @@ -149,6 +155,7 @@ stm32f415 = ["stm32f4/stm32f405", "device-selected", "rng", "sdio", "spi3", + "tim2", "tim8", "usart3", "uart4", "uart5", ] stm32f417 = ["stm32f4/stm32f407", "device-selected", @@ -163,6 +170,7 @@ stm32f417 = ["stm32f4/stm32f407", "device-selected", "rng", "sdio", "spi3", + "tim2", "tim8", "usart3", "uart4", "uart5", ] stm32f423 = ["stm32f4/stm32f413", "device-selected", @@ -176,6 +184,7 @@ stm32f423 = ["stm32f4/stm32f413", "device-selected", "rng", "sdio", "spi3", "spi4", "spi5", + "tim2", "tim8", "usart3", "uart4", "uart5", "uart7", "uart8", "uart9", "uart10", ] stm32f427 = ["stm32f4/stm32f427", "device-selected", @@ -190,6 +199,7 @@ stm32f427 = ["stm32f4/stm32f427", "device-selected", "rng", "sdio", "spi3", "spi4", "spi5", "spi6", + "tim2", "tim8", "usart3", "uart4", "uart5", "uart7", "uart8", ] stm32f429 = ["stm32f4/stm32f429", "device-selected", @@ -204,6 +214,7 @@ stm32f429 = ["stm32f4/stm32f429", "device-selected", "rng", "sdio", "spi3", "spi4", "spi5", "spi6", + "tim2", "tim8", "usart3", "uart4", "uart5", "uart7", "uart8", ] stm32f437 = ["stm32f4/stm32f427", "device-selected", @@ -218,6 +229,7 @@ stm32f437 = ["stm32f4/stm32f427", "device-selected", "rng", "sdio", "spi3", "spi4", "spi5", "spi6", + "tim2", "tim8", "usart3", "uart4", "uart5", "uart7", "uart8", ] stm32f439 = ["stm32f4/stm32f429", "device-selected", @@ -232,6 +244,7 @@ stm32f439 = ["stm32f4/stm32f429", "device-selected", "rng", "sdio", "spi3", "spi4", "spi5", "spi6", + "tim2", "tim8", "usart3", "uart4", "uart5", "uart7", "uart8", ] stm32f446 = ["stm32f4/stm32f446", "device-selected", @@ -244,6 +257,7 @@ stm32f446 = ["stm32f4/stm32f446", "device-selected", "otg-fs", "otg-hs", "spi3", "spi4", + "tim2", "tim8", "usart3", "uart4", "uart5", ] stm32f469 = ["stm32f4/stm32f469", "device-selected", @@ -258,6 +272,7 @@ stm32f469 = ["stm32f4/stm32f469", "device-selected", "rng", "sdio", "spi3", "spi4", "spi5", "spi6", + "tim2", "tim8", "usart3", "uart4", "uart5", "uart7", "uart8", ] stm32f479 = ["stm32f4/stm32f469", "device-selected", @@ -272,6 +287,7 @@ stm32f479 = ["stm32f4/stm32f469", "device-selected", "rng", "sdio", "spi3", "spi4", "spi5", "spi6", + "tim2", "tim8", "usart3", "uart4", "uart5", "uart7", "uart8", ] @@ -308,6 +324,8 @@ spi3 = [] spi4 = [] spi5 = [] spi6 = [] +tim2 = [] +tim8 = [] usart3 = [] uart4 = [] uart5 = [] @@ -327,55 +345,55 @@ opt-level = "s" [[example]] name = "blinky-timer-irq" -required-features = ["rt", "stm32f411"] +required-features = ["rt", "tim2"] # stm32f411 [[example]] name = "usb_serial" -required-features = ["rt", "stm32f401", "usb_fs"] +required-features = ["otg-fs", "usb_fs"] # stm32f401 [[example]] name = "sd" -required-features = ["rt", "stm32f405", "sdio-host"] +required-features = ["gpiod", "sdio", "sdio-host"] # stm32f405 [[example]] name = "delay-syst-blinky" -required-features = ["rt", "stm32f411"] +required-features = ["device-selected"] # stm32f411 [[example]] name = "delay-timer-blinky" -required-features = ["rt", "stm32f411"] +required-features = ["device-selected"] # stm32f411 [[example]] name = "dwt-blinky" -required-features = ["rt", "stm32f429"] +required-features = ["gpiog"] # "stm32f429" [[example]] name = "ssd1306-image" -required-features = ["rt", "stm32f411"] +required-features = ["device-selected"] # stm32f411 [[example]] name = "stopwatch-with-ssd1306-and-interrupts" -required-features = ["rt", "stm32f411"] +required-features = ["rt", "tim2"] # stm32f411 [[example]] name = "analog-stopwatch-with-spi-ssd1306" -required-features = ["rt", "stm32f429"] +required-features = ["rt", "spi4", "tim2", "gpioe", "gpiog"] # stm32f429 [[example]] name = "rng-display" -required-features = ["rt", "stm32f407"] +required-features = ["rng"] # stm32f407 [[example]] name = "qei" -required-features = ["rt", "stm32f411"] +required-features = ["tim2"] # stm32f411 [[example]] name = "can-send" -required-features = ["can", "stm32f405"] +required-features = ["can", "can1", "can2"] # stm32f405 [[example]] name = "i2s-audio-out" -required-features = ["stm32f411", "rt", "i2s"] +required-features = ["stm32f411", "i2s"] [[example]] name = "i2s-audio-out-dma" @@ -383,28 +401,28 @@ required-features = ["stm32f411", "rt", "i2s"] [[example]] name = "rtic" -required-features = ["rt", "stm32f407"] +required-features = ["gpiod"] # stm32f407 [[example]] name = "adc_dma_rtic" -required-features = ["rt", "stm32f401"] +required-features = ["device-selected"] # stm32f401 [[example]] name = "serial-9bit" -required-features = ["rt", "stm32f411"] +required-features = ["gpiod"] # stm32f411 [[example]] name = "st7789-lcd" -required-features = ["rt", "stm32f412", "fsmc_lcd"] +required-features = ["fsmc", "fsmc_lcd"] # stm32f412 [[example]] name = "f413disco_lcd_ferris" -required-features = ["rt", "stm32f413", "fsmc_lcd"] +required-features = ["gpiod", "gpioe", "gpiof", "gpiog", "fsmc", "fsmc_lcd"] # stm32f413 [[example]] name= "pwm-input" -required-features = ["stm32f446"] +required-features = ["tim8"] # stm32f446 [[example]] name = "ist7920_bidi_normal_spi" -required-features = ["rt", "stm32f401"] \ No newline at end of file +required-features = ["rt", "stm32f401"] diff --git a/examples/analog-stopwatch-with-spi-ssd1306.rs b/examples/analog-stopwatch-with-spi-ssd1306.rs index 0c2736ac..aa2dc85f 100644 --- a/examples/analog-stopwatch-with-spi-ssd1306.rs +++ b/examples/analog-stopwatch-with-spi-ssd1306.rs @@ -12,7 +12,7 @@ use stm32f4xx_hal as hal; use crate::hal::{ gpio::{gpioa::PA0, Edge, Input, PullDown}, - interrupt, pac, pac, + interrupt, pac, prelude::*, rcc::{Clocks, Rcc}, spi::Spi, diff --git a/examples/can-send.rs b/examples/can-send.rs index de149f83..22ee4e49 100644 --- a/examples/can-send.rs +++ b/examples/can-send.rs @@ -25,7 +25,7 @@ fn main() -> ! { let gpiob = dp.GPIOB.split(); let mut can1 = { - let rx = gpiob.pb8.into_alternate(); + let rx = gpiob.pb8.into_alternate::<9>(); let tx = gpiob.pb9.into_alternate(); let can = Can::new(dp.CAN1, (tx, rx)); diff --git a/src/pwm_input.rs b/src/pwm_input.rs index f6cb9811..322d1bc6 100644 --- a/src/pwm_input.rs +++ b/src/pwm_input.rs @@ -40,6 +40,7 @@ pub struct PwmInput> { pins: PINS, } +#[cfg(not(feature = "stm32f410"))] macro_rules! hal { ($($TIM:ident: ($bits:ident),)+) => { $( diff --git a/src/timer.rs b/src/timer.rs index 160b8dd1..7c11a2ed 100644 --- a/src/timer.rs +++ b/src/timer.rs @@ -488,22 +488,7 @@ channel_impl!( TIM4, PinC4, PD15, 2; ); -#[cfg(any( - feature = "stm32f405", - feature = "stm32f407", - feature = "stm32f412", - feature = "stm32f413", - feature = "stm32f415", - feature = "stm32f417", - feature = "stm32f423", - feature = "stm32f427", - feature = "stm32f429", - feature = "stm32f437", - feature = "stm32f439", - feature = "stm32f446", - feature = "stm32f469", - feature = "stm32f479" -))] +#[cfg(feature = "tim8")] channel_impl!( TIM8, PinC1, PC6, 3; TIM8, PinC2, PC7, 3; From 348793d13a9a507f9720fde3447e28a87cc2fe7f Mon Sep 17 00:00:00 2001 From: Andrey Zgarbul Date: Sat, 24 Jul 2021 06:18:03 +0300 Subject: [PATCH 3/8] optimize into_alternate in debug --- src/gpio/convert.rs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/gpio/convert.rs b/src/gpio/convert.rs index 722740d8..c36d9d91 100644 --- a/src/gpio/convert.rs +++ b/src/gpio/convert.rs @@ -343,18 +343,17 @@ impl Pin { Assert::::LESS; } let offset = 2 * { N }; - let offset2 = 4 * { N }; - let mode = A as u32; unsafe { - if offset2 < 32 { + if N < 8 { + let offset2 = 4 * { N }; (*Gpio::

::ptr()) .afrl - .modify(|r, w| w.bits((r.bits() & !(0b1111 << offset2)) | (mode << offset2))); + .modify(|r, w| w.bits((r.bits() & !(0b1111 << offset2)) | ((A as u32) << offset2))); } else { - let offset2 = offset2 - 32; + let offset2 = 4 * { N - 8 }; (*Gpio::

::ptr()) .afrh - .modify(|r, w| w.bits((r.bits() & !(0b1111 << offset2)) | (mode << offset2))); + .modify(|r, w| w.bits((r.bits() & !(0b1111 << offset2)) | ((A as u32) << offset2))); } (*Gpio::

::ptr()) .moder From 3dbb54bfd62ff174fd519673141ec1f9f0ef6292 Mon Sep 17 00:00:00 2001 From: Andrey Zgarbul Date: Sat, 24 Jul 2021 07:12:35 +0300 Subject: [PATCH 4/8] PinMode u32 --- src/gpio/convert.rs | 61 +++++++++++++++++++++++---------------------- 1 file changed, 31 insertions(+), 30 deletions(-) diff --git a/src/gpio/convert.rs b/src/gpio/convert.rs index c36d9d91..9dd09ca7 100644 --- a/src/gpio/convert.rs +++ b/src/gpio/convert.rs @@ -346,14 +346,14 @@ impl Pin { unsafe { if N < 8 { let offset2 = 4 * { N }; - (*Gpio::

::ptr()) - .afrl - .modify(|r, w| w.bits((r.bits() & !(0b1111 << offset2)) | ((A as u32) << offset2))); + (*Gpio::

::ptr()).afrl.modify(|r, w| { + w.bits((r.bits() & !(0b1111 << offset2)) | ((A as u32) << offset2)) + }); } else { let offset2 = 4 * { N - 8 }; - (*Gpio::

::ptr()) - .afrh - .modify(|r, w| w.bits((r.bits() & !(0b1111 << offset2)) | ((A as u32) << offset2))); + (*Gpio::

::ptr()).afrh.modify(|r, w| { + w.bits((r.bits() & !(0b1111 << offset2)) | ((A as u32) << offset2)) + }); } (*Gpio::

::ptr()) .moder @@ -599,22 +599,23 @@ impl Pin { /// /// This violates the type state constraints from `MODE`, so callers must /// ensure they use this properly. + #[inline(always)] fn mode(&mut self) { let offset = 2 * N; unsafe { - (*Gpio::

::ptr()).pupdr.modify(|r, w| { - w.bits((r.bits() & !(0b11 << offset)) | (u32::from(M::PUPDR) << offset)) - }); + (*Gpio::

::ptr()) + .pupdr + .modify(|r, w| w.bits((r.bits() & !(0b11 << offset)) | (M::PUPDR << offset))); if let Some(otyper) = M::OTYPER { (*Gpio::

::ptr()) .otyper - .modify(|r, w| w.bits(r.bits() & !(0b1 << N) | (u32::from(otyper) << N))); + .modify(|r, w| w.bits(r.bits() & !(0b1 << N) | (otyper << N))); } - (*Gpio::

::ptr()).moder.modify(|r, w| { - w.bits((r.bits() & !(0b11 << offset)) | (u32::from(M::MODER) << offset)) - }); + (*Gpio::

::ptr()) + .moder + .modify(|r, w| w.bits((r.bits() & !(0b11 << offset)) | (M::MODER << offset))); } } } @@ -721,47 +722,47 @@ pub trait PinMode: crate::Sealed { // They are not part of public API. #[doc(hidden)] - const PUPDR: u8; + const PUPDR: u32; #[doc(hidden)] - const MODER: u8; + const MODER: u32; #[doc(hidden)] - const OTYPER: Option = None; + const OTYPER: Option = None; } impl crate::Sealed for Input {} impl PinMode for Input { - const PUPDR: u8 = 0b00; - const MODER: u8 = 0b00; + const PUPDR: u32 = 0b00; + const MODER: u32 = 0b00; } impl crate::Sealed for Input {} impl PinMode for Input { - const PUPDR: u8 = 0b10; - const MODER: u8 = 0b00; + const PUPDR: u32 = 0b10; + const MODER: u32 = 0b00; } impl crate::Sealed for Input {} impl PinMode for Input { - const PUPDR: u8 = 0b01; - const MODER: u8 = 0b00; + const PUPDR: u32 = 0b01; + const MODER: u32 = 0b00; } impl crate::Sealed for Analog {} impl PinMode for Analog { - const PUPDR: u8 = 0b00; - const MODER: u8 = 0b11; + const PUPDR: u32 = 0b00; + const MODER: u32 = 0b11; } impl crate::Sealed for Output {} impl PinMode for Output { - const PUPDR: u8 = 0b00; - const MODER: u8 = 0b01; - const OTYPER: Option = Some(0b1); + const PUPDR: u32 = 0b00; + const MODER: u32 = 0b01; + const OTYPER: Option = Some(0b1); } impl crate::Sealed for Output {} impl PinMode for Output { - const PUPDR: u8 = 0b00; - const MODER: u8 = 0b01; - const OTYPER: Option = Some(0b0); + const PUPDR: u32 = 0b00; + const MODER: u32 = 0b01; + const OTYPER: Option = Some(0b0); } From cd89a77a93fcef6c766e09788fbbeed8aa8d83fb Mon Sep 17 00:00:00 2001 From: Andrey Zgarbul Date: Sat, 24 Jul 2021 07:30:10 +0300 Subject: [PATCH 5/8] b'A' --- src/gpio.rs | 4 ++-- src/gpio/partially_erased.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/gpio.rs b/src/gpio.rs index 490e6158..1a4dec82 100644 --- a/src/gpio.rs +++ b/src/gpio.rs @@ -238,7 +238,7 @@ impl PinExt for Pin { } #[inline(always)] fn port_id(&self) -> u8 { - P as u8 - 0x41 + P as u8 - b'A' } } @@ -330,7 +330,7 @@ impl Pin { /// This is useful when you want to collect the pins into an array where you /// need all the elements to have the same type pub fn erase(self) -> EPin { - EPin::new(P as u8 - 0x41, N) + EPin::new(P as u8 - b'A', N) } #[deprecated(since = "0.10.0", note = "Please use erase instead")] pub fn downgrade2(self) -> EPin { diff --git a/src/gpio/partially_erased.rs b/src/gpio/partially_erased.rs index 11496b20..fecf76a3 100644 --- a/src/gpio/partially_erased.rs +++ b/src/gpio/partially_erased.rs @@ -24,7 +24,7 @@ impl PinExt for PEPin { } #[inline(always)] fn port_id(&self) -> u8 { - P as u8 - 0x41 + P as u8 - b'A' } } From f0ecbb66c763586777768adb0a147bee9eb1bffd Mon Sep 17 00:00:00 2001 From: Andrey Zgarbul Date: Sat, 24 Jul 2021 18:33:56 +0300 Subject: [PATCH 6/8] pin docs --- CHANGELOG.md | 1 + src/gpio.rs | 54 ++++++++++++++++++++++++++++++++++-- src/gpio/erased.rs | 28 +++++++++++-------- src/gpio/partially_erased.rs | 27 ++++++++++-------- 4 files changed, 84 insertions(+), 26 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 34a698f4..efeff89e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Added +- Simple doc in gpio. - Weaker constrains for examples. - Deprecate `stm32` alias. - Temporary change pin mode diff --git a/src/gpio.rs b/src/gpio.rs index 1a4dec82..9d868329 100644 --- a/src/gpio.rs +++ b/src/gpio.rs @@ -1,4 +1,48 @@ //! General Purpose Input / Output +//! +//! The GPIO pins are organised into groups of 16 pins which can be accessed through the +//! `gpioa`, `gpiob`... modules. To get access to the pins, you first need to convert them into a +//! HAL designed struct from the `pac` struct using the [split](trait.GpioExt.html#tymethod.split) function. +//! ```rust +//! // Acquire the GPIOC peripheral +//! // NOTE: `dp` is the device peripherals from the `PAC` crate +//! let mut gpioa = dp.GPIOA.split(); +//! ``` +//! +//! This gives you a struct containing all the pins `px0..px15`. +//! By default pins are in floating input mode. You can change their modes. +//! For example, to set `pa5` high, you would call +//! +//! ```rust +//! let output = gpioa.pa5.into_push_pull_output(); +//! output.set_high(); +//! ``` +//! +//! ## Modes +//! +//! Each GPIO pin can be set to various modes: +//! +//! - **Alternate**: Pin mode required when the pin is driven by other peripherals +//! - **AlternateOD**: Pin mode required when the pin is driven by other peripherals and has open drain +//! - **Analog**: Analog input to be used with ADC. +//! - Input +//! - **PullUp**: Input connected to high with a weak pull up resistor. Will be high when nothing +//! is connected +//! - **PullDown**: Input connected to high with a weak pull up resistor. Will be low when nothing +//! is connected +//! - **Floating**: Input not pulled to high or low. Will be undefined when nothing is connected +//! - Output +//! - **PushPull**: Output which either drives the pin high or low +//! - **OpenDrain**: Output which leaves the gate floating, or pulls it do ground in drain +//! mode. Can be used as an input in the `open` configuration +//! +//! ## Changing modes +//! The simplest way to change the pin mode is to use the `into_` functions. These return a +//! new struct with the correct mode that you can use the input or output functions on. +//! +//! If you need a more temporary mode change, and can not use the `into_` functions for +//! ownership reasons, you can use the `as_` functions to temporarily change the pin type, do +//! some output or input, and then have it change back once done. use core::convert::Infallible; use core::marker::PhantomData; @@ -10,9 +54,9 @@ use crate::syscfg::SysCfg; mod convert; mod partially_erased; -pub use partially_erased::PEPin; +pub use partially_erased::{PEPin, PartiallyErasedPin}; mod erased; -pub use erased::EPin; +pub use erased::{EPin, ErasedPin}; /// A filler pin type pub struct NoPin; @@ -219,7 +263,11 @@ where } } -/// Pin +/// Generic pin type +/// +/// - `MODE` is one of the pin modes (see [Modes](crate::gpio#modes) section). +/// - `P` is port name: `A` for GPIOA, `B` for GPIOB, etc. +/// - `N` is pin number: from `0` to `15`. pub struct Pin { _mode: PhantomData, } diff --git a/src/gpio/erased.rs b/src/gpio/erased.rs index c33979ae..f6416c63 100644 --- a/src/gpio/erased.rs +++ b/src/gpio/erased.rs @@ -1,13 +1,17 @@ use super::*; +pub type EPin = ErasedPin; + /// Fully erased pin -pub struct EPin { +/// +/// `MODE` is one of the pin modes (see [Modes](crate::gpio#modes) section). +pub struct ErasedPin { // Bits 0-3: Pin, Bits 4-7: Port pin_port: u8, _mode: PhantomData, } -impl PinExt for EPin { +impl PinExt for ErasedPin { type Mode = MODE; #[inline(always)] @@ -20,7 +24,7 @@ impl PinExt for EPin { } } -impl EPin { +impl ErasedPin { pub(crate) fn new(port: u8, pin: u8) -> Self { Self { pin_port: port << 4 | pin, @@ -36,7 +40,7 @@ impl EPin { // - GPIOA register is available on all chips // - all gpio register blocks have the same layout // - consecutive gpio register blocks have the same offset between them, namely 0x0400 - // - EPin::new was called with a valid port + // - ErasedPin::new was called with a valid port // FIXME could be calculated after const_raw_ptr_to_usize_cast stabilization #51910 const GPIO_REGISTER_OFFSET: usize = 0x0400; @@ -49,7 +53,7 @@ impl EPin { } } -impl EPin> { +impl ErasedPin> { #[inline(always)] pub fn set_high(&mut self) { // NOTE(unsafe) atomic write to a stateless register @@ -103,7 +107,7 @@ impl EPin> { } } -impl OutputPin for EPin> { +impl OutputPin for ErasedPin> { type Error = core::convert::Infallible; #[inline(always)] @@ -119,7 +123,7 @@ impl OutputPin for EPin> { } } -impl StatefulOutputPin for EPin> { +impl StatefulOutputPin for ErasedPin> { #[inline(always)] fn is_set_high(&self) -> Result { Ok(self.is_set_high()) @@ -131,7 +135,7 @@ impl StatefulOutputPin for EPin> { } } -impl ToggleableOutputPin for EPin> { +impl ToggleableOutputPin for ErasedPin> { type Error = Infallible; #[inline(always)] @@ -141,7 +145,7 @@ impl ToggleableOutputPin for EPin> { } } -impl EPin> { +impl ErasedPin> { #[inline(always)] pub fn is_high(&self) -> bool { !self.is_low() @@ -153,7 +157,7 @@ impl EPin> { } } -impl InputPin for EPin> { +impl InputPin for ErasedPin> { type Error = core::convert::Infallible; #[inline(always)] @@ -167,7 +171,7 @@ impl InputPin for EPin> { } } -impl EPin> { +impl ErasedPin> { #[inline(always)] pub fn is_high(&self) -> bool { !self.is_low() @@ -179,7 +183,7 @@ impl EPin> { } } -impl InputPin for EPin> { +impl InputPin for ErasedPin> { type Error = core::convert::Infallible; #[inline(always)] diff --git a/src/gpio/partially_erased.rs b/src/gpio/partially_erased.rs index fecf76a3..79f344c1 100644 --- a/src/gpio/partially_erased.rs +++ b/src/gpio/partially_erased.rs @@ -1,12 +1,17 @@ use super::*; +pub type PEPin = PartiallyErasedPin; + /// Partially erased pin -pub struct PEPin { +/// +/// - `MODE` is one of the pin modes (see [Modes](crate::gpio#modes) section). +/// - `P` is port name: `A` for GPIOA, `B` for GPIOB, etc. +pub struct PartiallyErasedPin { i: u8, _mode: PhantomData, } -impl PEPin { +impl PartiallyErasedPin { pub(crate) fn new(i: u8) -> Self { Self { i, @@ -15,7 +20,7 @@ impl PEPin { } } -impl PinExt for PEPin { +impl PinExt for PartiallyErasedPin { type Mode = MODE; #[inline(always)] @@ -28,7 +33,7 @@ impl PinExt for PEPin { } } -impl PEPin, P> { +impl PartiallyErasedPin, P> { #[inline(always)] pub fn set_high(&mut self) { // NOTE(unsafe) atomic write to a stateless register @@ -83,7 +88,7 @@ impl PEPin, P> { } } -impl OutputPin for PEPin, P> { +impl OutputPin for PartiallyErasedPin, P> { type Error = Infallible; #[inline(always)] @@ -99,7 +104,7 @@ impl OutputPin for PEPin, P> { } } -impl StatefulOutputPin for PEPin, P> { +impl StatefulOutputPin for PartiallyErasedPin, P> { #[inline(always)] fn is_set_high(&self) -> Result { Ok(self.is_set_high()) @@ -111,7 +116,7 @@ impl StatefulOutputPin for PEPin, P> { } } -impl ToggleableOutputPin for PEPin, P> { +impl ToggleableOutputPin for PartiallyErasedPin, P> { type Error = Infallible; #[inline(always)] @@ -121,7 +126,7 @@ impl ToggleableOutputPin for PEPin, P> { } } -impl PEPin, P> { +impl PartiallyErasedPin, P> { #[inline(always)] pub fn is_high(&self) -> bool { !self.is_low() @@ -134,7 +139,7 @@ impl PEPin, P> { } } -impl InputPin for PEPin, P> { +impl InputPin for PartiallyErasedPin, P> { type Error = Infallible; #[inline(always)] @@ -148,7 +153,7 @@ impl InputPin for PEPin, P> { } } -impl PEPin, P> { +impl PartiallyErasedPin, P> { #[inline(always)] pub fn is_high(&self) -> bool { !self.is_low() @@ -161,7 +166,7 @@ impl PEPin, P> { } } -impl InputPin for PEPin, P> { +impl InputPin for PartiallyErasedPin, P> { type Error = Infallible; #[inline(always)] From 991cdc3cbdc765fcd5a144d01ff0f684b1c39dd2 Mon Sep 17 00:00:00 2001 From: Andrey Zgarbul Date: Sun, 25 Jul 2021 07:18:39 +0300 Subject: [PATCH 7/8] in_state --- CHANGELOG.md | 3 ++- src/gpio.rs | 55 +++++++++++++++++++++++++++++----------- src/gpio/convert.rs | 61 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 103 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index efeff89e..db6d766e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Added -- Simple doc in gpio. +- Simple docs in gpio. `into__in_state`, `with__in_state` - Weaker constrains for examples. - Deprecate `stm32` alias. - Temporary change pin mode @@ -54,6 +54,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Changed +- [breaking-change] `into_` fns set pin in `Low` state by default - Use manual impls for blocking spi instead of `Default`. - Split `Stream` trait on `Stream` and `StreamISR`. Use const generics for `Stream` and `Channel`. diff --git a/src/gpio.rs b/src/gpio.rs index 9d868329..43e2628e 100644 --- a/src/gpio.rs +++ b/src/gpio.rs @@ -41,7 +41,7 @@ //! new struct with the correct mode that you can use the input or output functions on. //! //! If you need a more temporary mode change, and can not use the `into_` functions for -//! ownership reasons, you can use the `as_` functions to temporarily change the pin type, do +//! ownership reasons, you can use the closure based `with_` functions to temporarily change the pin type, do //! some output or input, and then have it change back once done. use core::convert::Infallible; @@ -386,21 +386,49 @@ impl Pin { } } +impl Pin { + /// Set the output of the pin regardless of its mode. + /// Primarily used to set the output value of the pin + /// before changing its mode to an output to avoid + /// a short spike of an incorrect value + #[inline(always)] + fn _set_state(&mut self, state: PinState) { + match state { + PinState::High => self._set_high(), + PinState::Low => self._set_low(), + } + } + #[inline(always)] + fn _set_high(&mut self) { + // NOTE(unsafe) atomic write to a stateless register + unsafe { (*Gpio::

::ptr()).bsrr.write(|w| w.bits(1 << N)) } + } + #[inline(always)] + fn _set_low(&mut self) { + // NOTE(unsafe) atomic write to a stateless register + unsafe { (*Gpio::

::ptr()).bsrr.write(|w| w.bits(1 << (16 + N))) } + } + #[inline(always)] + fn _is_set_low(&self) -> bool { + // NOTE(unsafe) atomic read with no side effects + unsafe { (*Gpio::

::ptr()).odr.read().bits() & (1 << N) == 0 } + } + #[inline(always)] + fn _is_low(&self) -> bool { + // NOTE(unsafe) atomic read with no side effects + unsafe { (*Gpio::

::ptr()).idr.read().bits() & (1 << N) == 0 } + } +} + impl Pin, P, N> { #[inline(always)] pub fn set_high(&mut self) { - // NOTE(unsafe) atomic write to a stateless register - unsafe { (*Gpio::

::ptr()).bsrr.write(|w| w.bits(1 << { N })) } + self._set_high() } #[inline(always)] pub fn set_low(&mut self) { - // NOTE(unsafe) atomic write to a stateless register - unsafe { - (*Gpio::

::ptr()) - .bsrr - .write(|w| w.bits(1 << ({ N } + 16))) - } + self._set_low() } #[inline(always)] @@ -427,8 +455,7 @@ impl Pin, P, N> { #[inline(always)] pub fn is_set_low(&self) -> bool { - // NOTE(unsafe) atomic read with no side effects - unsafe { (*Gpio::

::ptr()).odr.read().bits() & (1 << { N }) == 0 } + self._is_set_low() } #[inline(always)] @@ -487,8 +514,7 @@ impl Pin, P, N> { #[inline(always)] pub fn is_low(&self) -> bool { - // NOTE(unsafe) atomic read with no side effects - unsafe { (*Gpio::

::ptr()).idr.read().bits() & (1 << { N }) == 0 } + self._is_low() } } @@ -514,8 +540,7 @@ impl Pin, P, N> { #[inline(always)] pub fn is_low(&self) -> bool { - // NOTE(unsafe) atomic read with no side effects - unsafe { (*Gpio::

::ptr()).idr.read().bits() & (1 << { N }) == 0 } + self._is_low() } } diff --git a/src/gpio/convert.rs b/src/gpio/convert.rs index 9dd09ca7..008f043e 100644 --- a/src/gpio/convert.rs +++ b/src/gpio/convert.rs @@ -578,13 +578,38 @@ impl Pin { } /// Configures the pin to operate as an open drain output pin + /// Initial state will be low. pub fn into_open_drain_output(mut self) -> Pin, P, N> { self.mode::>(); Pin::new() } + /// Configures the pin to operate as an open-drain output pin. + /// `initial_state` specifies whether the pin should be initially high or low. + pub fn into_open_drain_output_in_state( + mut self, + initial_state: PinState, + ) -> Pin, P, N> { + self._set_state(initial_state); + self.mode::>(); + Pin::new() + } + /// Configures the pin to operate as an push pull output pin + /// Initial state will be low. pub fn into_push_pull_output(mut self) -> Pin, P, N> { + self._set_low(); + self.mode::>(); + Pin::new() + } + + /// Configures the pin to operate as an push-pull output pin. + /// `initial_state` specifies whether the pin should be initially high or low. + pub fn into_push_pull_output_in_state( + mut self, + initial_state: PinState, + ) -> Pin, P, N> { + self._set_state(initial_state); self.mode::>(); Pin::new() } @@ -685,6 +710,8 @@ where /// /// The closure `f` is called with the reconfigured pin. After it returns, /// the pin will be configured back. + /// The value of the pin after conversion is undefined. If you + /// want to control it, use `with_open_drain_output_in_state` pub fn with_open_drain_output( &mut self, f: impl FnOnce(&mut Pin, P, N>) -> R, @@ -692,16 +719,50 @@ where self.with_mode(f) } + /// Temporarily configures this pin as an open drain output . + /// + /// The closure `f` is called with the reconfigured pin. After it returns, + /// the pin will be configured back. + /// Note that the new state is set slightly before conversion + /// happens. This can cause a short output glitch if switching + /// between output modes + pub fn with_open_drain_output_in_state( + &mut self, + state: PinState, + f: impl FnOnce(&mut Pin, P, N>) -> R, + ) -> R { + self._set_state(state); + self.with_mode(f) + } + /// Temporarily configures this pin as a push-pull output. /// /// The closure `f` is called with the reconfigured pin. After it returns, /// the pin will be configured back. + /// The value of the pin after conversion is undefined. If you + /// want to control it, use `with_push_pull_output_in_state` pub fn with_push_pull_output( &mut self, f: impl FnOnce(&mut Pin, P, N>) -> R, ) -> R { self.with_mode(f) } + + /// Temporarily configures this pin as a push-pull output. + /// + /// The closure `f` is called with the reconfigured pin. After it returns, + /// the pin will be configured back. + /// Note that the new state is set slightly before conversion + /// happens. This can cause a short output glitch if switching + /// between output modes + pub fn with_push_pull_output_in_state( + &mut self, + state: PinState, + f: impl FnOnce(&mut Pin, P, N>) -> R, + ) -> R { + self._set_state(state); + self.with_mode(f) + } } struct ResetMode<'a, ORIG: PinMode, const P: char, const N: u8> { From 2e69f41970e10cf67b5aa619f744ac0826a3d385 Mon Sep 17 00:00:00 2001 From: Andrey Zgarbul Date: Tue, 27 Jul 2021 11:57:30 +0300 Subject: [PATCH 8/8] clippy --- Cargo.toml | 2 +- examples/analog-stopwatch-with-spi-ssd1306.rs | 24 +++++++++---------- examples/blinky-timer-irq.rs | 6 ++--- examples/delay-syst-blinky.rs | 2 +- examples/delay-timer-blinky.rs | 2 +- examples/dwt-blinky.rs | 2 +- examples/pwm-input.rs | 1 - examples/qei.rs | 1 - examples/rng-display.rs | 1 + examples/ssd1306-image.rs | 5 ++-- .../stopwatch-with-ssd1306-and-interrupts.rs | 16 ++++++------- src/spi.rs | 3 ++- 12 files changed, 32 insertions(+), 33 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 8ecc67d5..42a1531c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -425,4 +425,4 @@ required-features = ["tim8"] # stm32f446 [[example]] name = "ist7920_bidi_normal_spi" -required-features = ["rt", "stm32f401"] +required-features = ["device-selected"] diff --git a/examples/analog-stopwatch-with-spi-ssd1306.rs b/examples/analog-stopwatch-with-spi-ssd1306.rs index aa2dc85f..4652c747 100644 --- a/examples/analog-stopwatch-with-spi-ssd1306.rs +++ b/examples/analog-stopwatch-with-spi-ssd1306.rs @@ -168,11 +168,10 @@ fn main() -> ! { led4.set_low(); } StopwatchState::Running => { + led4.set_low(); if state_led { - led4.set_low(); led3.set_high(); } else { - led4.set_low(); led3.set_low(); } } @@ -208,13 +207,12 @@ fn main() -> ! { } fn setup_clocks(rcc: Rcc) -> Clocks { - return rcc - .cfgr + rcc.cfgr .hclk(180.mhz()) .sysclk(180.mhz()) .pclk1(45.mhz()) .pclk2(90.mhz()) - .freeze(); + .freeze() } #[interrupt] @@ -291,31 +289,31 @@ fn TIM2() { }); } -fn stopwatch_start<'cs>(cs: &'cs CriticalSection) { +fn stopwatch_start(cs: &CriticalSection) { ELAPSED_MS.borrow(cs).replace(0); unsafe { pac::NVIC::unmask(hal::pac::Interrupt::TIM2); } } -fn stopwatch_continue<'cs>(_cs: &'cs CriticalSection) { +fn stopwatch_continue(_cs: &CriticalSection) { unsafe { pac::NVIC::unmask(hal::pac::Interrupt::TIM2); } } -fn stopwatch_stop<'cs>(_cs: &'cs CriticalSection) { +fn stopwatch_stop(_cs: &CriticalSection) { pac::NVIC::mask(hal::pac::Interrupt::TIM2); } -fn stopwatch_reset_start<'cs>(cs: &'cs CriticalSection) { +fn stopwatch_reset_start(cs: &CriticalSection) { ELAPSED_RESET_MS.borrow(cs).replace(0); unsafe { pac::NVIC::unmask(hal::pac::Interrupt::TIM2); } } -fn stopwatch_reset_stop<'cs>(_cs: &'cs CriticalSection) { +fn stopwatch_reset_stop(_cs: &CriticalSection) { pac::NVIC::mask(hal::pac::Interrupt::TIM2); } @@ -332,15 +330,15 @@ fn format_elapsed(buf: &mut ArrayString<[u8; 10]>, elapsed: u32) { } fn elapsed_to_ms(elapsed: u32) -> u32 { - return elapsed % 1000; + elapsed % 1000 } fn elapsed_to_s(elapsed: u32) -> u32 { - return (elapsed - elapsed_to_ms(elapsed)) % 60000 / 1000; + (elapsed - elapsed_to_ms(elapsed)) % 60000 / 1000 } fn elapsed_to_m(elapsed: u32) -> u32 { - return elapsed / 60000; + elapsed / 60000 } /// Convert a polar coordinate (angle/distance) into an (X, Y) coordinate centered around `CENTER` diff --git a/examples/blinky-timer-irq.rs b/examples/blinky-timer-irq.rs index 7cbfa12b..9f6c6731 100644 --- a/examples/blinky-timer-irq.rs +++ b/examples/blinky-timer-irq.rs @@ -30,10 +30,10 @@ use embedded_hal::timer::CountDown; // A type definition for the GPIO pin to be used for our LED // For the onboard nucleo LED, use gpioa::PA5 or gpiob::PB13 depending your model -type LEDPIN = gpioa::PA5>; +type LedPin = gpioa::PA5>; // Make LED pin globally available -static G_LED: Mutex>> = Mutex::new(RefCell::new(None)); +static G_LED: Mutex>> = Mutex::new(RefCell::new(None)); // Make timer interrupt registers globally available static G_TIM: Mutex>>> = Mutex::new(RefCell::new(None)); @@ -42,7 +42,7 @@ static G_TIM: Mutex>>> = Mutex::new(RefCell: // This specific interrupt will "trip" when the timer TIM2 times out #[interrupt] fn TIM2() { - static mut LED: Option = None; + static mut LED: Option = None; static mut TIM: Option> = None; let led = LED.get_or_insert_with(|| { diff --git a/examples/delay-syst-blinky.rs b/examples/delay-syst-blinky.rs index d86ba07e..dd320032 100644 --- a/examples/delay-syst-blinky.rs +++ b/examples/delay-syst-blinky.rs @@ -1,13 +1,13 @@ //! Demonstrate the use of a blocking `Delay` using the SYST (sysclock) timer. #![deny(unsafe_code)] +#![allow(clippy::empty_loop)] #![no_main] #![no_std] // Halt on panic use panic_halt as _; // panic handler -use cortex_m; use cortex_m_rt::entry; use stm32f4xx_hal as hal; diff --git a/examples/delay-timer-blinky.rs b/examples/delay-timer-blinky.rs index 6d210cca..001681b9 100644 --- a/examples/delay-timer-blinky.rs +++ b/examples/delay-timer-blinky.rs @@ -1,13 +1,13 @@ //! Demonstrate the use of a blocking `Delay` using TIM5 general-purpose timer. #![deny(unsafe_code)] +#![allow(clippy::empty_loop)] #![no_main] #![no_std] // Halt on panic use panic_halt as _; // panic handler -use cortex_m; use cortex_m_rt::entry; use stm32f4xx_hal as hal; diff --git a/examples/dwt-blinky.rs b/examples/dwt-blinky.rs index 7849730f..adbfa485 100644 --- a/examples/dwt-blinky.rs +++ b/examples/dwt-blinky.rs @@ -1,3 +1,4 @@ +#![allow(clippy::empty_loop)] #![deny(unsafe_code)] #![no_main] #![no_std] @@ -8,7 +9,6 @@ use crate::hal::{ pac, prelude::*, }; -use cortex_m; use cortex_m_rt::entry; use panic_halt as _; use stm32f4xx_hal as hal; diff --git a/examples/pwm-input.rs b/examples/pwm-input.rs index cd111291..9b3b8eac 100644 --- a/examples/pwm-input.rs +++ b/examples/pwm-input.rs @@ -5,7 +5,6 @@ // Halt on panic use panic_halt as _; -use cortex_m; use cortex_m_rt::entry; use stm32f4xx_hal::{pac, prelude::*, timer::Timer}; diff --git a/examples/qei.rs b/examples/qei.rs index 3ebbc3bf..055965e7 100644 --- a/examples/qei.rs +++ b/examples/qei.rs @@ -12,7 +12,6 @@ // Halt on panic use panic_halt as _; -use cortex_m; use cortex_m_rt::entry; use embedded_hal::Direction as RotaryDirection; use stm32f4xx_hal::{delay::Delay, pac, prelude::*, qei::Qei}; diff --git a/examples/rng-display.rs b/examples/rng-display.rs index 9cd81561..6c4fb078 100644 --- a/examples/rng-display.rs +++ b/examples/rng-display.rs @@ -10,6 +10,7 @@ //! Note that this example requires the `--release` build flag because it is too //! large to fit in the default `memory.x` file provided with this crate. +#![allow(clippy::empty_loop)] #![no_std] #![no_main] diff --git a/examples/ssd1306-image.rs b/examples/ssd1306-image.rs index df3c1684..212ca103 100644 --- a/examples/ssd1306-image.rs +++ b/examples/ssd1306-image.rs @@ -9,6 +9,7 @@ //! //! Note that `--release` is required to fix link errors for smaller devices. +#![allow(clippy::empty_loop)] #![no_std] #![no_main] @@ -87,7 +88,7 @@ fn main() -> ! { /// Helper function - what rotation flips the screen upside down from /// the rotation we're in now? fn get_next_rotation(rotation: DisplayRotation) -> DisplayRotation { - return match rotation { + match rotation { DisplayRotation::Rotate0 => DisplayRotation::Rotate180, DisplayRotation::Rotate180 => DisplayRotation::Rotate0, @@ -95,7 +96,7 @@ fn get_next_rotation(rotation: DisplayRotation) -> DisplayRotation { // reset to 0 degrees landscape. On most SSD1306 displays, this means down is towards // the flat flex coming out of the display (and up is towards the breakout board pins). _ => DisplayRotation::Rotate0, - }; + } } #[exception] diff --git a/examples/stopwatch-with-ssd1306-and-interrupts.rs b/examples/stopwatch-with-ssd1306-and-interrupts.rs index 12c273c4..1a858e75 100644 --- a/examples/stopwatch-with-ssd1306-and-interrupts.rs +++ b/examples/stopwatch-with-ssd1306-and-interrupts.rs @@ -14,6 +14,7 @@ //! //! Video of this example running: https://imgur.com/a/lQTQFLy +#![allow(clippy::empty_loop)] #![no_std] #![no_main] @@ -188,23 +189,22 @@ fn EXTI15_10() { } fn setup_clocks(rcc: Rcc) -> Clocks { - return rcc - .cfgr + rcc.cfgr .hclk(48.mhz()) .sysclk(48.mhz()) .pclk1(24.mhz()) .pclk2(24.mhz()) - .freeze(); + .freeze() } -fn stopwatch_start<'cs>(cs: &'cs CriticalSection) { +fn stopwatch_start(cs: &CriticalSection) { ELAPSED_MS.borrow(cs).replace(0); unsafe { pac::NVIC::unmask(hal::pac::Interrupt::TIM2); } } -fn stopwatch_stop<'cs>(_cs: &'cs CriticalSection) { +fn stopwatch_stop(_cs: &CriticalSection) { pac::NVIC::mask(hal::pac::Interrupt::TIM2); } @@ -221,13 +221,13 @@ fn format_elapsed(buf: &mut ArrayString<[u8; 10]>, elapsed: u32) { } fn elapsed_to_ms(elapsed: u32) -> u32 { - return elapsed % 1000; + elapsed % 1000 } fn elapsed_to_s(elapsed: u32) -> u32 { - return (elapsed - elapsed_to_ms(elapsed)) % 60000 / 1000; + (elapsed - elapsed_to_ms(elapsed)) % 60000 / 1000 } fn elapsed_to_m(elapsed: u32) -> u32 { - return elapsed / 60000; + elapsed / 60000 } diff --git a/src/spi.rs b/src/spi.rs index 750d8908..f03a1f67 100644 --- a/src/spi.rs +++ b/src/spi.rs @@ -681,7 +681,8 @@ where }); Error::Crc.into() } else if sr.txe().bit_is_set() { - return Ok(self.send_u8(byte)); + self.send_u8(byte); + return Ok(()); } else { nb::Error::WouldBlock })