Skip to content

Commit 2b60666

Browse files
committed
Simplified examples a bit and fixed/added commentry
Signed-off-by: Daniel Egger <[email protected]>
1 parent 0ffbce7 commit 2b60666

12 files changed

+95
-111
lines changed

examples/adc_values.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,12 @@ use panic_halt;
66

77
use stm32f0xx_hal as hal;
88

9-
use crate::hal::prelude::*;
10-
use crate::hal::stm32;
9+
use crate::hal::{prelude::*, stm32};
1110

1211
use cortex_m::{interrupt::Mutex, peripheral::syst::SystClkSource::Core};
1312
use cortex_m_rt::{entry, exception};
1413

15-
use core::fmt::Write;
16-
17-
use core::cell::RefCell;
14+
use core::{cell::RefCell, fmt::Write};
1815

1916
struct Shared {
2017
adc: hal::adc::Adc,

examples/blinky.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,27 @@ use panic_halt;
66

77
use stm32f0xx_hal as hal;
88

9-
use crate::hal::prelude::*;
10-
use crate::hal::stm32;
9+
use crate::hal::{prelude::*, stm32};
1110

1211
use cortex_m_rt::entry;
1312

1413
#[entry]
1514
fn main() -> ! {
16-
if let Some(p) = stm32::Peripherals::take() {
15+
if let Some(mut p) = stm32::Peripherals::take() {
1716
cortex_m::interrupt::free(move |cs| {
18-
let mut flash = p.FLASH;
19-
let mut rcc = p.RCC.configure().sysclk(8.mhz()).freeze(&mut flash);
17+
let mut rcc = p.RCC.configure().sysclk(8.mhz()).freeze(&mut p.FLASH);
2018

2119
let gpioa = p.GPIOA.split(&mut rcc);
2220

23-
/* (Re-)configure PA1 as output */
21+
// (Re-)configure PA1 as output
2422
let mut led = gpioa.pa1.into_push_pull_output(cs);
2523

2624
loop {
27-
/* Turn PA1 on a million times in a row */
25+
// Turn PA1 on a million times in a row
2826
for _ in 0..1_000_000 {
2927
led.set_high();
3028
}
31-
/* Then turn PA1 off a million times in a row */
29+
// Then turn PA1 off a million times in a row
3230
for _ in 0..1_000_000 {
3331
led.set_low();
3432
}

examples/blinky_adc.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,29 @@ use panic_halt;
66

77
use stm32f0xx_hal as hal;
88

9-
use crate::hal::delay::Delay;
10-
use crate::hal::prelude::*;
11-
use crate::hal::stm32;
12-
13-
use crate::hal::adc::Adc;
9+
use crate::hal::{adc::Adc, delay::Delay, prelude::*, stm32};
1410

1511
use cortex_m::peripheral::Peripherals;
1612
use cortex_m_rt::entry;
1713

1814
#[entry]
1915
fn main() -> ! {
20-
if let (Some(p), Some(cp)) = (stm32::Peripherals::take(), Peripherals::take()) {
16+
if let (Some(mut p), Some(cp)) = (stm32::Peripherals::take(), Peripherals::take()) {
2117
cortex_m::interrupt::free(move |cs| {
22-
let mut flash = p.FLASH;
23-
let mut rcc = p.RCC.configure().sysclk(8.mhz()).freeze(&mut flash);
18+
let mut rcc = p.RCC.configure().sysclk(8.mhz()).freeze(&mut p.FLASH);
2419

2520
let gpioa = p.GPIOA.split(&mut rcc);
2621

27-
/* (Re-)configure PA1 as output */
22+
// (Re-)configure PA1 as output
2823
let mut led = gpioa.pa1.into_push_pull_output(cs);
2924

30-
/* (Re-)configure PA0 as analog in */
25+
// (Re-)configure PA0 as analog input
3126
let mut an_in = gpioa.pa0.into_analog(cs);
3227

33-
/* Get delay provider */
28+
// Get delay provider
3429
let mut delay = Delay::new(cp.SYST, &rcc);
3530

31+
// Get access to the ADC
3632
let mut adc = Adc::new(p.ADC, &mut rcc);
3733

3834
loop {

examples/blinky_delay.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,23 @@ use panic_halt;
66

77
use stm32f0xx_hal as hal;
88

9-
use crate::hal::delay::Delay;
10-
use crate::hal::prelude::*;
11-
use crate::hal::stm32;
9+
use crate::hal::{delay::Delay, prelude::*, stm32};
1210

1311
use cortex_m::peripheral::Peripherals;
1412
use cortex_m_rt::entry;
1513

1614
#[entry]
1715
fn main() -> ! {
18-
if let (Some(p), Some(cp)) = (stm32::Peripherals::take(), Peripherals::take()) {
16+
if let (Some(mut p), Some(cp)) = (stm32::Peripherals::take(), Peripherals::take()) {
1917
cortex_m::interrupt::free(move |cs| {
20-
let mut flash = p.FLASH;
21-
let mut rcc = p.RCC.configure().sysclk(8.mhz()).freeze(&mut flash);
18+
let mut rcc = p.RCC.configure().sysclk(8.mhz()).freeze(&mut p.FLASH);
2219

2320
let gpioa = p.GPIOA.split(&mut rcc);
2421

25-
/* (Re-)configure PA1 as output */
22+
// (Re-)configure PA1 as output
2623
let mut led = gpioa.pa1.into_push_pull_output(cs);
2724

28-
/* Get delay provider */
25+
// Get delay provider
2926
let mut delay = Delay::new(cp.SYST, &rcc);
3027

3128
loop {

examples/blinky_multiple.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,30 @@ use panic_halt;
66

77
use stm32f0xx_hal as hal;
88

9-
use crate::hal::delay::Delay;
10-
use crate::hal::prelude::*;
11-
use crate::hal::stm32;
9+
use crate::hal::{delay::Delay, prelude::*, stm32};
1210

1311
use cortex_m::peripheral::Peripherals;
1412
use cortex_m_rt::entry;
1513

1614
#[entry]
1715
fn main() -> ! {
18-
if let (Some(p), Some(cp)) = (stm32::Peripherals::take(), Peripherals::take()) {
16+
if let (Some(mut p), Some(cp)) = (stm32::Peripherals::take(), Peripherals::take()) {
1917
cortex_m::interrupt::free(move |cs| {
20-
let mut flash = p.FLASH;
21-
let mut rcc = p.RCC.configure().sysclk(8.mhz()).freeze(&mut flash);
18+
let mut rcc = p.RCC.configure().sysclk(8.mhz()).freeze(&mut p.FLASH);
2219

2320
let gpioa = p.GPIOA.split(&mut rcc);
2421
let gpiob = p.GPIOB.split(&mut rcc);
2522

26-
/* (Re-)configure PA1 as output */
23+
// (Re-)configure PA1 as output
2724
let led1 = gpioa.pa1.into_push_pull_output(cs);
2825

29-
/* (Re-)configure PB1 as output */
26+
// (Re-)configure PB1 as output
3027
let led2 = gpiob.pb1.into_push_pull_output(cs);
3128

32-
/* Get delay provider */
29+
// Get delay provider
3330
let mut delay = Delay::new(cp.SYST, &rcc);
3431

35-
/* Store them together */
32+
// Store them together after erasing the type
3633
let mut leds = [led1.downgrade(), led2.downgrade()];
3734
loop {
3835
for l in &mut leds {

examples/blinky_timer.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,29 @@ use panic_halt;
66

77
use stm32f0xx_hal as hal;
88

9-
use crate::hal::prelude::*;
10-
use crate::hal::stm32;
11-
use crate::hal::time::*;
12-
use crate::hal::timers::*;
9+
use crate::hal::{prelude::*, stm32, time::Hertz, timers::*};
1310

1411
use cortex_m_rt::entry;
15-
use nb::block;
1612

1713
#[entry]
1814
fn main() -> ! {
19-
if let Some(p) = stm32::Peripherals::take() {
15+
if let Some(mut p) = stm32::Peripherals::take() {
2016
cortex_m::interrupt::free(move |cs| {
21-
let mut flash = p.FLASH;
22-
let mut rcc = p.RCC.configure().sysclk(8.mhz()).freeze(&mut flash);
17+
let mut rcc = p.RCC.configure().sysclk(8.mhz()).freeze(&mut p.FLASH);
2318

2419
let gpioa = p.GPIOA.split(&mut rcc);
25-
/* (Re-)configure PA1 as output */
20+
21+
// (Re-)configure PA1 as output
2622
let mut led = gpioa.pa1.into_push_pull_output(cs);
2723

24+
// Set up a timer expiring after 1s
2825
let mut timer = Timer::tim1(p.TIM1, Hertz(1), &mut rcc);
2926

3027
loop {
3128
led.toggle();
32-
block!(timer.wait()).ok();
29+
30+
// Wait for the timer to expire
31+
nb::block!(timer.wait()).ok();
3332
}
3433
});
3534
}

examples/flash_systick.rs

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,49 +6,46 @@ use panic_halt;
66

77
use stm32f0xx_hal as hal;
88

9-
use crate::hal::gpio::*;
10-
use crate::hal::prelude::*;
11-
use crate::hal::stm32;
9+
use crate::hal::{gpio::*, prelude::*, stm32};
1210

13-
use cortex_m::interrupt::Mutex;
14-
use cortex_m::peripheral::syst::SystClkSource::Core;
15-
use cortex_m::peripheral::Peripherals;
11+
use cortex_m::{interrupt::Mutex, peripheral::syst::SystClkSource::Core, Peripherals};
1612
use cortex_m_rt::{entry, exception};
1713

1814
use core::cell::RefCell;
1915
use core::ops::DerefMut;
2016

17+
// Mutex protected structure for our shared GPIO pin
2118
static GPIO: Mutex<RefCell<Option<gpioa::PA1<Output<PushPull>>>>> = Mutex::new(RefCell::new(None));
2219

2320
#[entry]
2421
fn main() -> ! {
25-
if let (Some(p), Some(cp)) = (stm32::Peripherals::take(), Peripherals::take()) {
22+
if let (Some(mut p), Some(cp)) = (stm32::Peripherals::take(), Peripherals::take()) {
2623
cortex_m::interrupt::free(move |cs| {
27-
let mut flash = p.FLASH;
28-
let mut rcc = p.RCC.configure().sysclk(48.mhz()).freeze(&mut flash);
24+
let mut rcc = p.RCC.configure().sysclk(48.mhz()).freeze(&mut p.FLASH);
2925

3026
let gpioa = p.GPIOA.split(&mut rcc);
3127

32-
let mut syst = cp.SYST;
33-
34-
/* (Re-)configure PA1 as output */
28+
// (Re-)configure PA1 as output
3529
let led = gpioa.pa1.into_push_pull_output(cs);
3630

31+
// Transfer GPIO into a shared structure
3732
*GPIO.borrow(cs).borrow_mut() = Some(led);
3833

39-
/* Initialise SysTick counter with a defined value */
34+
let mut syst = cp.SYST;
35+
36+
// Initialise SysTick counter with a defined value
4037
unsafe { syst.cvr.write(1) };
4138

42-
/* Set source for SysTick counter, here full operating frequency (== 8MHz) */
39+
// Set source for SysTick counter, here full operating frequency (== 48MHz)
4340
syst.set_clock_source(Core);
4441

45-
/* Set reload value, i.e. timer delay 48 MHz/4 Mcounts == 12Hz or 83ms */
42+
// Set reload value, i.e. timer delay 48 MHz/4 Mcounts == 12Hz or 83ms
4643
syst.set_reload(4_000_000 - 1);
4744

48-
/* Start counter */
45+
// Start counting
4946
syst.enable_counter();
5047

51-
/* Start interrupt generation */
48+
// Enable interrupt generation
5249
syst.enable_interrupt();
5350
});
5451
}
@@ -58,28 +55,29 @@ fn main() -> ! {
5855
}
5956
}
6057

61-
/* Define an exception, i.e. function to call when exception occurs. Here if our SysTick timer
62-
* trips the flash function will be called and the specified stated passed in via argument */
63-
//, flash, state: u8 = 1);
58+
// Define an exception handler, i.e. function to call when exception occurs. Here, if our SysTick
59+
// timer generates an exception the following handler will be called
6460
#[exception]
6561
fn SysTick() -> ! {
62+
// Exception handler state variable
6663
static mut state: u8 = 1;
6764

68-
/* Enter critical section */
65+
// Enter critical section
6966
cortex_m::interrupt::free(|cs| {
67+
// Borrow access to our GPIO pin from the shared structure
7068
if let Some(ref mut led) = *GPIO.borrow(cs).borrow_mut().deref_mut() {
71-
/* Check state variable, keep LED off most of the time and turn it on every 10th tick */
69+
// Check state variable, keep LED off most of the time and turn it on every 10th tick
7270
if *state < 10 {
73-
/* If set turn off the LED */
71+
// Turn off the LED
7472
led.set_low();
7573

76-
/* And now increment state variable */
74+
// And now increment state variable
7775
*state += 1;
7876
} else {
79-
/* If not set, turn on the LED */
77+
// Turn on the LED
8078
led.set_high();
8179

82-
/* And set new state variable back to 0 */
80+
// And set new state variable back to 0
8381
*state = 0;
8482
}
8583
}

examples/led_hal_button_irq.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,17 @@ use panic_halt;
66

77
use stm32f0xx_hal as hal;
88

9-
use crate::hal::delay::Delay;
10-
use crate::hal::gpio::*;
11-
use crate::hal::prelude::*;
12-
13-
use cortex_m::interrupt::Mutex;
14-
use cortex_m::peripheral::Peripherals as c_m_Peripherals;
9+
use crate::hal::{
10+
delay::Delay,
11+
gpio::*,
12+
prelude::*,
13+
stm32::{interrupt, Interrupt, Peripherals, EXTI},
14+
};
15+
16+
use cortex_m::{interrupt::Mutex, peripheral::Peripherals as c_m_Peripherals};
1517
use cortex_m_rt::entry;
1618

17-
use crate::hal::stm32::{interrupt, Interrupt, Peripherals, EXTI};
18-
19-
use core::cell::RefCell;
20-
use core::ops::DerefMut;
19+
use core::{cell::RefCell, ops::DerefMut};
2120

2221
// Make our LED globally available
2322
static LED: Mutex<RefCell<Option<gpioa::PA1<Output<PushPull>>>>> = Mutex::new(RefCell::new(None));

examples/serial_echo.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,7 @@ use panic_halt;
66

77
use stm32f0xx_hal as hal;
88

9-
use crate::hal::prelude::*;
10-
use crate::hal::serial::Serial;
11-
use crate::hal::stm32;
12-
13-
use nb::block;
9+
use crate::hal::{prelude::*, serial::Serial, stm32};
1410

1511
use cortex_m_rt::entry;
1612

@@ -29,8 +25,11 @@ fn main() -> ! {
2925
let mut serial = Serial::usart1(p.USART1, (tx, rx), 115_200.bps(), &mut rcc);
3026

3127
loop {
32-
let received = block!(serial.read()).unwrap();
33-
block!(serial.write(received)).ok();
28+
// Wait for reception of a single byte
29+
let received = nb::block!(serial.read()).unwrap();
30+
31+
// Send back previously received byte and wait for completion
32+
nb::block!(serial.write(received)).ok();
3433
}
3534
});
3635
}

examples/serial_spi_bridge.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ use panic_halt;
66

77
use stm32f0xx_hal as hal;
88

9-
use crate::hal::prelude::*;
10-
use crate::hal::serial::Serial;
11-
use crate::hal::spi::Spi;
12-
use crate::hal::spi::{Mode, Phase, Polarity};
13-
use crate::hal::stm32;
9+
use crate::hal::{
10+
prelude::*,
11+
serial::Serial,
12+
spi::Spi,
13+
spi::{Mode, Phase, Polarity},
14+
stm32,
15+
};
1416

1517
use nb::block;
1618

0 commit comments

Comments
 (0)