Skip to content

Commit 7124498

Browse files
committed
Use critical_section::CriticalSection for GPIO configuration
1 parent b2e2e9d commit 7124498

29 files changed

+89
-81
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1010
### Changed
1111

1212
- Updated the `cast` dependency from 0.2 to 0.3
13+
- Use `critical_section::CriticalSection` for GPIO configuration (breaking change)
1314

1415
### Added
1516

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ targets = ["thumbv6m-none-eabi"]
3333
bare-metal = { version = "1.0.0" }
3434
cast = "0.3"
3535
cortex-m = "0.7"
36+
critical-section = "1.0"
3637
embedded-hal = { version = "0.2", features = ["unproven"] }
3738
stm32f0 = "0.14"
3839
nb = "1"

examples/adc_values.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ use stm32f0xx_hal as hal;
77

88
use crate::hal::{pac, prelude::*};
99

10-
use cortex_m::{interrupt::Mutex, peripheral::syst::SystClkSource::Core};
10+
use cortex_m::peripheral::syst::SystClkSource::Core;
1111
use cortex_m_rt::{entry, exception};
12+
use critical_section::Mutex;
1213

1314
use core::{cell::RefCell, fmt::Write};
1415

@@ -25,7 +26,7 @@ fn main() -> ! {
2526
hal::pac::Peripherals::take(),
2627
cortex_m::peripheral::Peripherals::take(),
2728
) {
28-
cortex_m::interrupt::free(move |cs| {
29+
critical_section::with(move |cs| {
2930
let mut flash = p.FLASH;
3031
let mut rcc = p.RCC.configure().sysclk(8.mhz()).freeze(&mut flash);
3132

@@ -46,8 +47,8 @@ fn main() -> ! {
4647
syst.enable_interrupt();
4748

4849
// USART1 at PA9 (TX) and PA10(RX)
49-
let tx = gpioa.pa9.into_alternate_af1(cs);
50-
let rx = gpioa.pa10.into_alternate_af1(cs);
50+
let tx = gpioa.pa9.into_alternate_af1(&cs);
51+
let rx = gpioa.pa10.into_alternate_af1(&cs);
5152

5253
// Initialiase UART
5354
let (mut tx, _) =
@@ -74,7 +75,7 @@ fn SysTick() {
7475
use core::ops::DerefMut;
7576

7677
// Enter critical section
77-
cortex_m::interrupt::free(|cs| {
78+
critical_section::with(|cs| {
7879
// Get access to the Mutex protected shared data
7980
if let Some(ref mut shared) = SHARED.borrow(cs).borrow_mut().deref_mut() {
8081
// Read temperature data from internal sensor using ADC

examples/blinky.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ fn main() -> ! {
1717
let gpioa = p.GPIOA.split(&mut rcc);
1818

1919
// (Re-)configure PA1 as output
20-
let mut led = cortex_m::interrupt::free(|cs| gpioa.pa1.into_push_pull_output(cs));
20+
let mut led = critical_section::with(|cs| gpioa.pa1.into_push_pull_output(&cs));
2121

2222
loop {
2323
// Turn PA1 on a million times in a row

examples/blinky_adc.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ fn main() -> ! {
1717

1818
let gpioa = p.GPIOA.split(&mut rcc);
1919

20-
let (mut led, mut an_in) = cortex_m::interrupt::free(move |cs| {
20+
let (mut led, mut an_in) = critical_section::with(move |cs| {
2121
(
2222
// (Re-)configure PA1 as output
23-
gpioa.pa1.into_push_pull_output(cs),
23+
gpioa.pa1.into_push_pull_output(&cs),
2424
// (Re-)configure PA0 as analog input
25-
gpioa.pa0.into_analog(cs),
25+
gpioa.pa0.into_analog(&cs),
2626
)
2727
});
2828

examples/blinky_delay.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ fn main() -> ! {
1818
let gpioa = p.GPIOA.split(&mut rcc);
1919

2020
// (Re-)configure PA1 as output
21-
let mut led = cortex_m::interrupt::free(move |cs| gpioa.pa1.into_push_pull_output(cs));
21+
let mut led = critical_section::with(move |cs| gpioa.pa1.into_push_pull_output(&cs));
2222

2323
// Get delay provider
2424
let mut delay = Delay::new(cp.SYST, &rcc);

examples/blinky_multiple.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ fn main() -> ! {
1818
let gpioa = p.GPIOA.split(&mut rcc);
1919
let gpiob = p.GPIOB.split(&mut rcc);
2020

21-
let (led1, led2) = cortex_m::interrupt::free(move |cs| {
21+
let (led1, led2) = critical_section::with(move |cs| {
2222
(
2323
// (Re-)configure PA1 as output
24-
gpioa.pa1.into_push_pull_output(cs),
24+
gpioa.pa1.into_push_pull_output(&cs),
2525
// (Re-)configure PB1 as output
26-
gpiob.pb1.into_push_pull_output(cs),
26+
gpiob.pb1.into_push_pull_output(&cs),
2727
)
2828
});
2929

examples/blinky_timer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ fn main() -> ! {
1717
let gpioa = p.GPIOA.split(&mut rcc);
1818

1919
// (Re-)configure PA1 as output
20-
let mut led = cortex_m::interrupt::free(move |cs| gpioa.pa1.into_push_pull_output(cs));
20+
let mut led = critical_section::with(move |cs| gpioa.pa1.into_push_pull_output(&cs));
2121

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

examples/blinky_timer_irq.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ use crate::hal::{
1616
use cortex_m_rt::entry;
1717

1818
use core::cell::RefCell;
19-
use cortex_m::{interrupt::Mutex, peripheral::Peripherals as c_m_Peripherals};
19+
use cortex_m::peripheral::Peripherals as c_m_Peripherals;
20+
use critical_section::Mutex;
2021

2122
// A type definition for the GPIO pin to be used for our LED
2223
type LEDPIN = gpioa::PA5<Output<PushPull>>;
@@ -35,14 +36,14 @@ fn TIM7() {
3536
static mut INT: Option<Timer<TIM7>> = None;
3637

3738
let led = LED.get_or_insert_with(|| {
38-
cortex_m::interrupt::free(|cs| {
39+
critical_section::with(|cs| {
3940
// Move LED pin here, leaving a None in its place
4041
GLED.borrow(cs).replace(None).unwrap()
4142
})
4243
});
4344

4445
let int = INT.get_or_insert_with(|| {
45-
cortex_m::interrupt::free(|cs| {
46+
critical_section::with(|cs| {
4647
// Move LED pin here, leaving a None in its place
4748
GINT.borrow(cs).replace(None).unwrap()
4849
})
@@ -55,7 +56,7 @@ fn TIM7() {
5556
#[entry]
5657
fn main() -> ! {
5758
if let (Some(mut p), Some(cp)) = (Peripherals::take(), c_m_Peripherals::take()) {
58-
cortex_m::interrupt::free(move |cs| {
59+
critical_section::with(move |cs| {
5960
let mut rcc = p
6061
.RCC
6162
.configure()
@@ -68,7 +69,7 @@ fn main() -> ! {
6869
let gpioa = p.GPIOA.split(&mut rcc);
6970

7071
// (Re-)configure PA5 as output
71-
let led = gpioa.pa5.into_push_pull_output(cs);
72+
let led = gpioa.pa5.into_push_pull_output(&cs);
7273

7374
// Move the pin into our global storage
7475
*GLED.borrow(cs).borrow_mut() = Some(led);

examples/dac.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ fn main() -> ! {
2424
let mut rcc = dp.RCC.configure().sysclk(8.mhz()).freeze(&mut dp.FLASH);
2525
let gpioa = dp.GPIOA.split(&mut rcc);
2626

27-
let pa4 = cortex_m::interrupt::free(move |cs| gpioa.pa4.into_analog(cs));
27+
let pa4 = critical_section::with(move |cs| gpioa.pa4.into_analog(&cs));
2828

2929
let mut dac = dac(dp.DAC, pa4, &mut rcc);
3030

0 commit comments

Comments
 (0)