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

+1
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

+1
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

+6-5
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

+1-1
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

+3-3
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

+1-1
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

+3-3
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

+1-1
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

+6-5
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

+1-1
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

examples/flash_systick.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ use stm32f0xx_hal as hal;
77

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

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

1314
use core::cell::RefCell;
1415
use core::ops::DerefMut;
@@ -19,13 +20,13 @@ static GPIO: Mutex<RefCell<Option<gpioa::PA1<Output<PushPull>>>>> = Mutex::new(R
1920
#[entry]
2021
fn main() -> ! {
2122
if let (Some(mut p), Some(cp)) = (pac::Peripherals::take(), Peripherals::take()) {
22-
cortex_m::interrupt::free(move |cs| {
23+
critical_section::with(move |cs| {
2324
let mut rcc = p.RCC.configure().sysclk(48.mhz()).freeze(&mut p.FLASH);
2425

2526
let gpioa = p.GPIOA.split(&mut rcc);
2627

2728
// (Re-)configure PA1 as output
28-
let led = gpioa.pa1.into_push_pull_output(cs);
29+
let led = gpioa.pa1.into_push_pull_output(&cs);
2930

3031
// Transfer GPIO into a shared structure
3132
*GPIO.borrow(cs).borrow_mut() = Some(led);
@@ -62,7 +63,7 @@ fn SysTick() {
6263
static mut STATE: u8 = 1;
6364

6465
// Enter critical section
65-
cortex_m::interrupt::free(|cs| {
66+
critical_section::with(|cs| {
6667
// Borrow access to our GPIO pin from the shared structure
6768
if let Some(ref mut led) = *GPIO.borrow(cs).borrow_mut().deref_mut() {
6869
// Check state variable, keep LED off most of the time and turn it on every 10th tick

examples/flash_systick_fancier.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ use stm32f0xx_hal as hal;
77

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

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

1314
use core::cell::RefCell;
1415
use core::mem::swap;
@@ -22,14 +23,14 @@ static GPIO: Mutex<RefCell<Option<LEDPIN>>> = Mutex::new(RefCell::new(None));
2223
#[entry]
2324
fn main() -> ! {
2425
if let (Some(mut p), Some(cp)) = (pac::Peripherals::take(), Peripherals::take()) {
25-
cortex_m::interrupt::free(move |cs| {
26+
critical_section::with(move |cs| {
2627
let mut rcc = p.RCC.configure().sysclk(48.mhz()).freeze(&mut p.FLASH);
2728

2829
// Get access to individual pins in the GPIO port
2930
let gpioa = p.GPIOB.split(&mut rcc);
3031

3132
// (Re-)configure the pin connected to our LED as output
32-
let led = gpioa.pb3.into_push_pull_output(cs);
33+
let led = gpioa.pb3.into_push_pull_output(&cs);
3334

3435
// Transfer GPIO into a shared structure
3536
swap(&mut Some(led), &mut GPIO.borrow(cs).borrow_mut());
@@ -88,7 +89,7 @@ fn SysTick() {
8889
// Otherwise move it out of the Mutex protected shared region into our exception handler
8990
else {
9091
// Enter critical section
91-
cortex_m::interrupt::free(|cs| {
92+
critical_section::with(|cs| {
9293
// Swap globally stored data with SysTick private data
9394
swap(LED, &mut GPIO.borrow(cs).borrow_mut());
9495
});

examples/i2c_find_address.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ use cortex_m_rt::entry;
1414
#[entry]
1515
fn main() -> ! {
1616
if let Some(p) = pac::Peripherals::take() {
17-
cortex_m::interrupt::free(move |cs| {
17+
critical_section::with(move |cs| {
1818
let mut flash = p.FLASH;
1919
let mut rcc = p.RCC.configure().freeze(&mut flash);
2020

2121
let gpiob = p.GPIOB.split(&mut rcc);
2222

2323
// Configure pins for I2C
24-
let sda = gpiob.pb7.into_alternate_af1(cs);
25-
let scl = gpiob.pb8.into_alternate_af1(cs);
24+
let sda = gpiob.pb7.into_alternate_af1(&cs);
25+
let scl = gpiob.pb8.into_alternate_af1(&cs);
2626

2727
// Configure I2C with 100kHz rate
2828
let mut i2c = I2c::i2c1(p.I2C1, (scl, sda), 100.khz(), &mut rcc);

examples/led_hal_button_irq.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ use crate::hal::{
1212
prelude::*,
1313
};
1414

15-
use cortex_m::{interrupt::Mutex, peripheral::Peripherals as c_m_Peripherals};
15+
use cortex_m::peripheral::Peripherals as c_m_Peripherals;
1616
use cortex_m_rt::entry;
17+
use critical_section::Mutex;
1718

1819
use core::{cell::RefCell, ops::DerefMut};
1920

@@ -29,7 +30,7 @@ static INT: Mutex<RefCell<Option<EXTI>>> = Mutex::new(RefCell::new(None));
2930
#[entry]
3031
fn main() -> ! {
3132
if let (Some(p), Some(cp)) = (Peripherals::take(), c_m_Peripherals::take()) {
32-
cortex_m::interrupt::free(move |cs| {
33+
critical_section::with(move |cs| {
3334
// Enable clock for SYSCFG
3435
let rcc = p.RCC;
3536
rcc.apb2enr.modify(|_, w| w.syscfgen().set_bit());
@@ -43,10 +44,10 @@ fn main() -> ! {
4344
let exti = p.EXTI;
4445

4546
// Configure PB1 as input (button)
46-
let _ = gpiob.pb1.into_pull_down_input(cs);
47+
let _ = gpiob.pb1.into_pull_down_input(&cs);
4748

4849
// Configure PA1 as output (LED)
49-
let mut led = gpioa.pa1.into_push_pull_output(cs);
50+
let mut led = gpioa.pa1.into_push_pull_output(&cs);
5051

5152
// Turn off LED
5253
led.set_low().ok();
@@ -88,7 +89,7 @@ fn main() -> ! {
8889
#[interrupt]
8990
fn EXTI0_1() {
9091
// Enter critical section
91-
cortex_m::interrupt::free(|cs| {
92+
critical_section::with(|cs| {
9293
// Obtain all Mutex protected resources
9394
if let (&mut Some(ref mut led), &mut Some(ref mut delay), &mut Some(ref mut exti)) = (
9495
LED.borrow(cs).borrow_mut().deref_mut(),

examples/pwm.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ fn main() -> ! {
1818
let mut rcc = dp.RCC.configure().sysclk(8.mhz()).freeze(&mut dp.FLASH);
1919

2020
let gpioa = dp.GPIOA.split(&mut rcc);
21-
let channels = cortex_m::interrupt::free(move |cs| {
21+
let channels = critical_section::with(move |cs| {
2222
(
23-
gpioa.pa8.into_alternate_af2(cs),
24-
gpioa.pa9.into_alternate_af2(cs),
23+
gpioa.pa8.into_alternate_af2(&cs),
24+
gpioa.pa9.into_alternate_af2(&cs),
2525
)
2626
});
2727

examples/pwm_complementary.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ fn main() -> ! {
1818
let mut rcc = dp.RCC.configure().sysclk(8.mhz()).freeze(&mut dp.FLASH);
1919

2020
let gpioa = dp.GPIOA.split(&mut rcc);
21-
let channels = cortex_m::interrupt::free(move |cs| {
21+
let channels = critical_section::with(move |cs| {
2222
(
23-
gpioa.pa8.into_alternate_af2(cs), // on TIM1_CH1
24-
gpioa.pa7.into_alternate_af2(cs), // on TIM1_CH1N
23+
gpioa.pa8.into_alternate_af2(&cs), // on TIM1_CH1
24+
gpioa.pa7.into_alternate_af2(&cs), // on TIM1_CH1N
2525
)
2626
});
2727

examples/serial_echo.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ fn main() -> ! {
1717

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

20-
let (tx, rx) = cortex_m::interrupt::free(move |cs| {
20+
let (tx, rx) = critical_section::with(move |cs| {
2121
(
22-
gpioa.pa9.into_alternate_af1(cs),
23-
gpioa.pa10.into_alternate_af1(cs),
22+
gpioa.pa9.into_alternate_af1(&cs),
23+
gpioa.pa10.into_alternate_af1(&cs),
2424
)
2525
});
2626

examples/serial_spi_bridge.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ fn main() -> ! {
3636

3737
let gpioa = p.GPIOA.split(&mut rcc);
3838

39-
let (sck, miso, mosi, tx, rx) = cortex_m::interrupt::free(move |cs| {
39+
let (sck, miso, mosi, tx, rx) = critical_section::with(move |cs| {
4040
(
4141
// SPI pins
42-
gpioa.pa5.into_alternate_af0(cs),
43-
gpioa.pa6.into_alternate_af0(cs),
44-
gpioa.pa7.into_alternate_af0(cs),
42+
gpioa.pa5.into_alternate_af0(&cs),
43+
gpioa.pa6.into_alternate_af0(&cs),
44+
gpioa.pa7.into_alternate_af0(&cs),
4545
// USART pins
46-
gpioa.pa9.into_alternate_af1(cs),
47-
gpioa.pa10.into_alternate_af1(cs),
46+
gpioa.pa9.into_alternate_af1(&cs),
47+
gpioa.pa10.into_alternate_af1(&cs),
4848
)
4949
});
5050

0 commit comments

Comments
 (0)