From 2b60666a31c458c0b7509057d22514617e0f3c6d Mon Sep 17 00:00:00 2001 From: Daniel Egger Date: Sun, 13 Jan 2019 14:45:42 +0100 Subject: [PATCH] Simplified examples a bit and fixed/added commentry Signed-off-by: Daniel Egger --- examples/adc_values.rs | 7 ++--- examples/blinky.rs | 14 ++++------ examples/blinky_adc.rs | 18 +++++------- examples/blinky_delay.rs | 13 ++++----- examples/blinky_multiple.rs | 17 +++++------- examples/blinky_timer.rs | 19 ++++++------- examples/flash_systick.rs | 50 ++++++++++++++++------------------ examples/led_hal_button_irq.rs | 19 ++++++------- examples/serial_echo.rs | 13 ++++----- examples/serial_spi_bridge.rs | 12 ++++---- examples/spi_hal_apa102c.rs | 10 ++++--- examples/watchdog.rs | 14 +++++----- 12 files changed, 95 insertions(+), 111 deletions(-) diff --git a/examples/adc_values.rs b/examples/adc_values.rs index a7adcd6..21d6195 100644 --- a/examples/adc_values.rs +++ b/examples/adc_values.rs @@ -6,15 +6,12 @@ use panic_halt; use stm32f0xx_hal as hal; -use crate::hal::prelude::*; -use crate::hal::stm32; +use crate::hal::{prelude::*, stm32}; use cortex_m::{interrupt::Mutex, peripheral::syst::SystClkSource::Core}; use cortex_m_rt::{entry, exception}; -use core::fmt::Write; - -use core::cell::RefCell; +use core::{cell::RefCell, fmt::Write}; struct Shared { adc: hal::adc::Adc, diff --git a/examples/blinky.rs b/examples/blinky.rs index 826bac3..d681c5a 100644 --- a/examples/blinky.rs +++ b/examples/blinky.rs @@ -6,29 +6,27 @@ use panic_halt; use stm32f0xx_hal as hal; -use crate::hal::prelude::*; -use crate::hal::stm32; +use crate::hal::{prelude::*, stm32}; use cortex_m_rt::entry; #[entry] fn main() -> ! { - if let Some(p) = stm32::Peripherals::take() { + if let Some(mut p) = stm32::Peripherals::take() { cortex_m::interrupt::free(move |cs| { - let mut flash = p.FLASH; - let mut rcc = p.RCC.configure().sysclk(8.mhz()).freeze(&mut flash); + let mut rcc = p.RCC.configure().sysclk(8.mhz()).freeze(&mut p.FLASH); let gpioa = p.GPIOA.split(&mut rcc); - /* (Re-)configure PA1 as output */ + // (Re-)configure PA1 as output let mut led = gpioa.pa1.into_push_pull_output(cs); loop { - /* Turn PA1 on a million times in a row */ + // Turn PA1 on a million times in a row for _ in 0..1_000_000 { led.set_high(); } - /* Then turn PA1 off a million times in a row */ + // Then turn PA1 off a million times in a row for _ in 0..1_000_000 { led.set_low(); } diff --git a/examples/blinky_adc.rs b/examples/blinky_adc.rs index 74a698c..1d7a9fa 100644 --- a/examples/blinky_adc.rs +++ b/examples/blinky_adc.rs @@ -6,33 +6,29 @@ use panic_halt; use stm32f0xx_hal as hal; -use crate::hal::delay::Delay; -use crate::hal::prelude::*; -use crate::hal::stm32; - -use crate::hal::adc::Adc; +use crate::hal::{adc::Adc, delay::Delay, prelude::*, stm32}; use cortex_m::peripheral::Peripherals; use cortex_m_rt::entry; #[entry] fn main() -> ! { - if let (Some(p), Some(cp)) = (stm32::Peripherals::take(), Peripherals::take()) { + if let (Some(mut p), Some(cp)) = (stm32::Peripherals::take(), Peripherals::take()) { cortex_m::interrupt::free(move |cs| { - let mut flash = p.FLASH; - let mut rcc = p.RCC.configure().sysclk(8.mhz()).freeze(&mut flash); + let mut rcc = p.RCC.configure().sysclk(8.mhz()).freeze(&mut p.FLASH); let gpioa = p.GPIOA.split(&mut rcc); - /* (Re-)configure PA1 as output */ + // (Re-)configure PA1 as output let mut led = gpioa.pa1.into_push_pull_output(cs); - /* (Re-)configure PA0 as analog in */ + // (Re-)configure PA0 as analog input let mut an_in = gpioa.pa0.into_analog(cs); - /* Get delay provider */ + // Get delay provider let mut delay = Delay::new(cp.SYST, &rcc); + // Get access to the ADC let mut adc = Adc::new(p.ADC, &mut rcc); loop { diff --git a/examples/blinky_delay.rs b/examples/blinky_delay.rs index c8fba45..a7a5b52 100644 --- a/examples/blinky_delay.rs +++ b/examples/blinky_delay.rs @@ -6,26 +6,23 @@ use panic_halt; use stm32f0xx_hal as hal; -use crate::hal::delay::Delay; -use crate::hal::prelude::*; -use crate::hal::stm32; +use crate::hal::{delay::Delay, prelude::*, stm32}; use cortex_m::peripheral::Peripherals; use cortex_m_rt::entry; #[entry] fn main() -> ! { - if let (Some(p), Some(cp)) = (stm32::Peripherals::take(), Peripherals::take()) { + if let (Some(mut p), Some(cp)) = (stm32::Peripherals::take(), Peripherals::take()) { cortex_m::interrupt::free(move |cs| { - let mut flash = p.FLASH; - let mut rcc = p.RCC.configure().sysclk(8.mhz()).freeze(&mut flash); + let mut rcc = p.RCC.configure().sysclk(8.mhz()).freeze(&mut p.FLASH); let gpioa = p.GPIOA.split(&mut rcc); - /* (Re-)configure PA1 as output */ + // (Re-)configure PA1 as output let mut led = gpioa.pa1.into_push_pull_output(cs); - /* Get delay provider */ + // Get delay provider let mut delay = Delay::new(cp.SYST, &rcc); loop { diff --git a/examples/blinky_multiple.rs b/examples/blinky_multiple.rs index 9cb5dd4..141042d 100644 --- a/examples/blinky_multiple.rs +++ b/examples/blinky_multiple.rs @@ -6,33 +6,30 @@ use panic_halt; use stm32f0xx_hal as hal; -use crate::hal::delay::Delay; -use crate::hal::prelude::*; -use crate::hal::stm32; +use crate::hal::{delay::Delay, prelude::*, stm32}; use cortex_m::peripheral::Peripherals; use cortex_m_rt::entry; #[entry] fn main() -> ! { - if let (Some(p), Some(cp)) = (stm32::Peripherals::take(), Peripherals::take()) { + if let (Some(mut p), Some(cp)) = (stm32::Peripherals::take(), Peripherals::take()) { cortex_m::interrupt::free(move |cs| { - let mut flash = p.FLASH; - let mut rcc = p.RCC.configure().sysclk(8.mhz()).freeze(&mut flash); + let mut rcc = p.RCC.configure().sysclk(8.mhz()).freeze(&mut p.FLASH); let gpioa = p.GPIOA.split(&mut rcc); let gpiob = p.GPIOB.split(&mut rcc); - /* (Re-)configure PA1 as output */ + // (Re-)configure PA1 as output let led1 = gpioa.pa1.into_push_pull_output(cs); - /* (Re-)configure PB1 as output */ + // (Re-)configure PB1 as output let led2 = gpiob.pb1.into_push_pull_output(cs); - /* Get delay provider */ + // Get delay provider let mut delay = Delay::new(cp.SYST, &rcc); - /* Store them together */ + // Store them together after erasing the type let mut leds = [led1.downgrade(), led2.downgrade()]; loop { for l in &mut leds { diff --git a/examples/blinky_timer.rs b/examples/blinky_timer.rs index 58a66c2..cf7db6f 100644 --- a/examples/blinky_timer.rs +++ b/examples/blinky_timer.rs @@ -6,30 +6,29 @@ use panic_halt; use stm32f0xx_hal as hal; -use crate::hal::prelude::*; -use crate::hal::stm32; -use crate::hal::time::*; -use crate::hal::timers::*; +use crate::hal::{prelude::*, stm32, time::Hertz, timers::*}; use cortex_m_rt::entry; -use nb::block; #[entry] fn main() -> ! { - if let Some(p) = stm32::Peripherals::take() { + if let Some(mut p) = stm32::Peripherals::take() { cortex_m::interrupt::free(move |cs| { - let mut flash = p.FLASH; - let mut rcc = p.RCC.configure().sysclk(8.mhz()).freeze(&mut flash); + let mut rcc = p.RCC.configure().sysclk(8.mhz()).freeze(&mut p.FLASH); let gpioa = p.GPIOA.split(&mut rcc); - /* (Re-)configure PA1 as output */ + + // (Re-)configure PA1 as output let mut led = gpioa.pa1.into_push_pull_output(cs); + // Set up a timer expiring after 1s let mut timer = Timer::tim1(p.TIM1, Hertz(1), &mut rcc); loop { led.toggle(); - block!(timer.wait()).ok(); + + // Wait for the timer to expire + nb::block!(timer.wait()).ok(); } }); } diff --git a/examples/flash_systick.rs b/examples/flash_systick.rs index 8bd5623..0f5bb36 100644 --- a/examples/flash_systick.rs +++ b/examples/flash_systick.rs @@ -6,49 +6,46 @@ use panic_halt; use stm32f0xx_hal as hal; -use crate::hal::gpio::*; -use crate::hal::prelude::*; -use crate::hal::stm32; +use crate::hal::{gpio::*, prelude::*, stm32}; -use cortex_m::interrupt::Mutex; -use cortex_m::peripheral::syst::SystClkSource::Core; -use cortex_m::peripheral::Peripherals; +use cortex_m::{interrupt::Mutex, peripheral::syst::SystClkSource::Core, Peripherals}; use cortex_m_rt::{entry, exception}; use core::cell::RefCell; use core::ops::DerefMut; +// Mutex protected structure for our shared GPIO pin static GPIO: Mutex>>>> = Mutex::new(RefCell::new(None)); #[entry] fn main() -> ! { - if let (Some(p), Some(cp)) = (stm32::Peripherals::take(), Peripherals::take()) { + if let (Some(mut p), Some(cp)) = (stm32::Peripherals::take(), Peripherals::take()) { cortex_m::interrupt::free(move |cs| { - let mut flash = p.FLASH; - let mut rcc = p.RCC.configure().sysclk(48.mhz()).freeze(&mut flash); + let mut rcc = p.RCC.configure().sysclk(48.mhz()).freeze(&mut p.FLASH); let gpioa = p.GPIOA.split(&mut rcc); - let mut syst = cp.SYST; - - /* (Re-)configure PA1 as output */ + // (Re-)configure PA1 as output let led = gpioa.pa1.into_push_pull_output(cs); + // Transfer GPIO into a shared structure *GPIO.borrow(cs).borrow_mut() = Some(led); - /* Initialise SysTick counter with a defined value */ + let mut syst = cp.SYST; + + // Initialise SysTick counter with a defined value unsafe { syst.cvr.write(1) }; - /* Set source for SysTick counter, here full operating frequency (== 8MHz) */ + // Set source for SysTick counter, here full operating frequency (== 48MHz) syst.set_clock_source(Core); - /* Set reload value, i.e. timer delay 48 MHz/4 Mcounts == 12Hz or 83ms */ + // Set reload value, i.e. timer delay 48 MHz/4 Mcounts == 12Hz or 83ms syst.set_reload(4_000_000 - 1); - /* Start counter */ + // Start counting syst.enable_counter(); - /* Start interrupt generation */ + // Enable interrupt generation syst.enable_interrupt(); }); } @@ -58,28 +55,29 @@ fn main() -> ! { } } -/* Define an exception, i.e. function to call when exception occurs. Here if our SysTick timer - * trips the flash function will be called and the specified stated passed in via argument */ -//, flash, state: u8 = 1); +// Define an exception handler, i.e. function to call when exception occurs. Here, if our SysTick +// timer generates an exception the following handler will be called #[exception] fn SysTick() -> ! { + // Exception handler state variable static mut state: u8 = 1; - /* Enter critical section */ + // Enter critical section cortex_m::interrupt::free(|cs| { + // Borrow access to our GPIO pin from the shared structure if let Some(ref mut led) = *GPIO.borrow(cs).borrow_mut().deref_mut() { - /* Check state variable, keep LED off most of the time and turn it on every 10th tick */ + // Check state variable, keep LED off most of the time and turn it on every 10th tick if *state < 10 { - /* If set turn off the LED */ + // Turn off the LED led.set_low(); - /* And now increment state variable */ + // And now increment state variable *state += 1; } else { - /* If not set, turn on the LED */ + // Turn on the LED led.set_high(); - /* And set new state variable back to 0 */ + // And set new state variable back to 0 *state = 0; } } diff --git a/examples/led_hal_button_irq.rs b/examples/led_hal_button_irq.rs index 60f7669..6e9029f 100644 --- a/examples/led_hal_button_irq.rs +++ b/examples/led_hal_button_irq.rs @@ -6,18 +6,17 @@ use panic_halt; use stm32f0xx_hal as hal; -use crate::hal::delay::Delay; -use crate::hal::gpio::*; -use crate::hal::prelude::*; - -use cortex_m::interrupt::Mutex; -use cortex_m::peripheral::Peripherals as c_m_Peripherals; +use crate::hal::{ + delay::Delay, + gpio::*, + prelude::*, + stm32::{interrupt, Interrupt, Peripherals, EXTI}, +}; + +use cortex_m::{interrupt::Mutex, peripheral::Peripherals as c_m_Peripherals}; use cortex_m_rt::entry; -use crate::hal::stm32::{interrupt, Interrupt, Peripherals, EXTI}; - -use core::cell::RefCell; -use core::ops::DerefMut; +use core::{cell::RefCell, ops::DerefMut}; // Make our LED globally available static LED: Mutex>>>> = Mutex::new(RefCell::new(None)); diff --git a/examples/serial_echo.rs b/examples/serial_echo.rs index bed2a54..d06f7aa 100644 --- a/examples/serial_echo.rs +++ b/examples/serial_echo.rs @@ -6,11 +6,7 @@ use panic_halt; use stm32f0xx_hal as hal; -use crate::hal::prelude::*; -use crate::hal::serial::Serial; -use crate::hal::stm32; - -use nb::block; +use crate::hal::{prelude::*, serial::Serial, stm32}; use cortex_m_rt::entry; @@ -29,8 +25,11 @@ fn main() -> ! { let mut serial = Serial::usart1(p.USART1, (tx, rx), 115_200.bps(), &mut rcc); loop { - let received = block!(serial.read()).unwrap(); - block!(serial.write(received)).ok(); + // Wait for reception of a single byte + let received = nb::block!(serial.read()).unwrap(); + + // Send back previously received byte and wait for completion + nb::block!(serial.write(received)).ok(); } }); } diff --git a/examples/serial_spi_bridge.rs b/examples/serial_spi_bridge.rs index 74050a7..fbf8e82 100644 --- a/examples/serial_spi_bridge.rs +++ b/examples/serial_spi_bridge.rs @@ -6,11 +6,13 @@ use panic_halt; use stm32f0xx_hal as hal; -use crate::hal::prelude::*; -use crate::hal::serial::Serial; -use crate::hal::spi::Spi; -use crate::hal::spi::{Mode, Phase, Polarity}; -use crate::hal::stm32; +use crate::hal::{ + prelude::*, + serial::Serial, + spi::Spi, + spi::{Mode, Phase, Polarity}, + stm32, +}; use nb::block; diff --git a/examples/spi_hal_apa102c.rs b/examples/spi_hal_apa102c.rs index 422e6da..51ebd85 100644 --- a/examples/spi_hal_apa102c.rs +++ b/examples/spi_hal_apa102c.rs @@ -6,10 +6,12 @@ use panic_halt; use stm32f0xx_hal as hal; -use crate::hal::prelude::*; -use crate::hal::spi::Spi; -use crate::hal::spi::{Mode, Phase, Polarity}; -use crate::hal::stm32; +use crate::hal::{ + prelude::*, + spi::Spi, + spi::{Mode, Phase, Polarity}, + stm32, +}; use cortex_m_rt::entry; diff --git a/examples/watchdog.rs b/examples/watchdog.rs index aa3400b..df0e44a 100644 --- a/examples/watchdog.rs +++ b/examples/watchdog.rs @@ -4,19 +4,17 @@ #[allow(unused)] use panic_halt; -use core::fmt::Write; use stm32f0xx_hal as hal; -use crate::hal::delay::Delay; -use crate::hal::prelude::*; -use crate::hal::serial::Serial; -use crate::hal::stm32; -use crate::hal::time::Hertz; -use crate::hal::watchdog::Watchdog; +use crate::hal::{ + delay::Delay, prelude::*, serial::Serial, stm32, time::Hertz, watchdog::Watchdog, +}; use cortex_m::peripheral::Peripherals; use cortex_m_rt::entry; +use core::fmt::Write; + #[entry] fn main() -> ! { if let (Some(p), Some(cp)) = (stm32::Peripherals::take(), Peripherals::take()) { @@ -35,8 +33,10 @@ fn main() -> ! { // Get delay provider let mut delay = Delay::new(cp.SYST, &rcc); + // Configure serial TX pin let tx = gpioa.pa9.into_alternate_af1(cs); + // Obtain a serial peripheral with for unidirectional communication let mut serial = Serial::usart1tx(p.USART1, tx, 115_200.bps(), &mut rcc); serial.write_str("RESET \r\n").ok();