Skip to content

Commit a52b4e8

Browse files
committed
Use bare_metal::CriticalSection for GPIO configuration
1 parent b2e2e9d commit a52b4e8

27 files changed

+207
-52
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 `bare_metal::CriticalSection` for GPIO configuration (breaking change)
1314

1415
### Added
1516

examples/adc_values.rs

+15-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ 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 bare_metal::Mutex;
11+
use cortex_m::peripheral::syst::SystClkSource::Core;
1112
use cortex_m_rt::{entry, exception};
1213

1314
use core::{cell::RefCell, fmt::Write};
@@ -25,7 +26,12 @@ fn main() -> ! {
2526
hal::pac::Peripherals::take(),
2627
cortex_m::peripheral::Peripherals::take(),
2728
) {
28-
cortex_m::interrupt::free(move |cs| {
29+
cortex_m::interrupt::free(move |_| {
30+
// SAFETY: We are in a critical section, but the `cortex_m` critical section
31+
// token is not compatible with the `bare_metal` token. Future version of the
32+
// `cortex_m` crate will not supply *any* token to this callback!
33+
let cs = unsafe { &bare_metal::CriticalSection::new() };
34+
2935
let mut flash = p.FLASH;
3036
let mut rcc = p.RCC.configure().sysclk(8.mhz()).freeze(&mut flash);
3137

@@ -60,7 +66,7 @@ fn main() -> ! {
6066
tx.write_str("\n\rThis ADC example will read various values using the ADC and print them out to the serial terminal\r\n").ok();
6167

6268
// Move all components under Mutex supervision
63-
*SHARED.borrow(cs).borrow_mut() = Some(Shared { adc, tx });
69+
*SHARED.borrow(*cs).borrow_mut() = Some(Shared { adc, tx });
6470
});
6571
}
6672

@@ -74,7 +80,12 @@ fn SysTick() {
7480
use core::ops::DerefMut;
7581

7682
// Enter critical section
77-
cortex_m::interrupt::free(|cs| {
83+
cortex_m::interrupt::free(|_| {
84+
// SAFETY: We are in a critical section, but the `cortex_m` critical section
85+
// token is not compatible with the `bare_metal` token. Future version of the
86+
// `cortex_m` crate will not supply *any* token to this callback!
87+
let cs = unsafe { bare_metal::CriticalSection::new() };
88+
7889
// Get access to the Mutex protected shared data
7990
if let Some(ref mut shared) = SHARED.borrow(cs).borrow_mut().deref_mut() {
8091
// Read temperature data from internal sensor using ADC

examples/blinky.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,13 @@ 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 = cortex_m::interrupt::free(|_| {
21+
// SAFETY: We are in a critical section, but the `cortex_m` critical section
22+
// token is not compatible with the `bare_metal` token. Future version of the
23+
// `cortex_m` crate will not supply *any* token to this callback!
24+
let cs = unsafe { &bare_metal::CriticalSection::new() };
25+
gpioa.pa1.into_push_pull_output(cs)
26+
});
2127

2228
loop {
2329
// Turn PA1 on a million times in a row

examples/blinky_adc.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +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) = cortex_m::interrupt::free(move |_| {
21+
// SAFETY: We are in a critical section, but the `cortex_m` critical section
22+
// token is not compatible with the `bare_metal` token. Future version of the
23+
// `cortex_m` crate will not supply *any* token to this callback!
24+
let cs = unsafe { &bare_metal::CriticalSection::new() };
25+
2126
(
2227
// (Re-)configure PA1 as output
2328
gpioa.pa1.into_push_pull_output(cs),

examples/blinky_delay.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@ 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 = cortex_m::interrupt::free(move |_| {
22+
// SAFETY: We are in a critical section, but the `cortex_m` critical section
23+
// token is not compatible with the `bare_metal` token. Future version of the
24+
// `cortex_m` crate will not supply *any* token to this callback!
25+
let cs = unsafe { &bare_metal::CriticalSection::new() };
26+
gpioa.pa1.into_push_pull_output(cs)
27+
});
2228

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

examples/blinky_multiple.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +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) = cortex_m::interrupt::free(move |_| {
22+
// SAFETY: We are in a critical section, but the `cortex_m` critical section
23+
// token is not compatible with the `bare_metal` token. Future version of the
24+
// `cortex_m` crate will not supply *any* token to this callback!
25+
let cs = unsafe { &bare_metal::CriticalSection::new() };
26+
2227
(
2328
// (Re-)configure PA1 as output
2429
gpioa.pa1.into_push_pull_output(cs),

examples/blinky_timer.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,13 @@ 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 = cortex_m::interrupt::free(move |_| {
21+
// SAFETY: We are in a critical section, but the `cortex_m` critical section
22+
// token is not compatible with the `bare_metal` token. Future version of the
23+
// `cortex_m` crate will not supply *any* token to this callback!
24+
let cs = unsafe { &bare_metal::CriticalSection::new() };
25+
gpioa.pa1.into_push_pull_output(cs)
26+
});
2127

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

examples/blinky_timer_irq.rs

+22-6
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ use crate::hal::{
1515

1616
use cortex_m_rt::entry;
1717

18+
use bare_metal::Mutex;
1819
use core::cell::RefCell;
19-
use cortex_m::{interrupt::Mutex, peripheral::Peripherals as c_m_Peripherals};
20+
use cortex_m::peripheral::Peripherals as c_m_Peripherals;
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,24 @@ 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+
cortex_m::interrupt::free(|_| {
40+
// SAFETY: We are in a critical section, but the `cortex_m` critical section
41+
// token is not compatible with the `bare_metal` token. Future version of the
42+
// `cortex_m` crate will not supply *any* token to this callback!
43+
let cs = unsafe { bare_metal::CriticalSection::new() };
44+
3945
// Move LED pin here, leaving a None in its place
4046
GLED.borrow(cs).replace(None).unwrap()
4147
})
4248
});
4349

4450
let int = INT.get_or_insert_with(|| {
45-
cortex_m::interrupt::free(|cs| {
51+
cortex_m::interrupt::free(|_| {
52+
// SAFETY: We are in a critical section, but the `cortex_m` critical section
53+
// token is not compatible with the `bare_metal` token. Future version of the
54+
// `cortex_m` crate will not supply *any* token to this callback!
55+
let cs = unsafe { bare_metal::CriticalSection::new() };
56+
4657
// Move LED pin here, leaving a None in its place
4758
GINT.borrow(cs).replace(None).unwrap()
4859
})
@@ -55,7 +66,12 @@ fn TIM7() {
5566
#[entry]
5667
fn main() -> ! {
5768
if let (Some(mut p), Some(cp)) = (Peripherals::take(), c_m_Peripherals::take()) {
58-
cortex_m::interrupt::free(move |cs| {
69+
cortex_m::interrupt::free(move |_| {
70+
// SAFETY: We are in a critical section, but the `cortex_m` critical section
71+
// token is not compatible with the `bare_metal` token. Future version of the
72+
// `cortex_m` crate will not supply *any* token to this callback!
73+
let cs = unsafe { &bare_metal::CriticalSection::new() };
74+
5975
let mut rcc = p
6076
.RCC
6177
.configure()
@@ -71,7 +87,7 @@ fn main() -> ! {
7187
let led = gpioa.pa5.into_push_pull_output(cs);
7288

7389
// Move the pin into our global storage
74-
*GLED.borrow(cs).borrow_mut() = Some(led);
90+
*GLED.borrow(*cs).borrow_mut() = Some(led);
7591

7692
// Set up a timer expiring after 1s
7793
let mut timer = Timer::tim7(p.TIM7, Hertz(1), &mut rcc);
@@ -80,7 +96,7 @@ fn main() -> ! {
8096
timer.listen(Event::TimeOut);
8197

8298
// Move the timer into our global storage
83-
*GINT.borrow(cs).borrow_mut() = Some(timer);
99+
*GINT.borrow(*cs).borrow_mut() = Some(timer);
84100

85101
// Enable TIM7 IRQ, set prio 1 and clear any pending IRQs
86102
let mut nvic = cp.NVIC;

examples/dac.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,13 @@ 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 = cortex_m::interrupt::free(move |_| {
28+
// SAFETY: We are in a critical section, but the `cortex_m` critical section
29+
// token is not compatible with the `bare_metal` token. Future version of the
30+
// `cortex_m` crate will not supply *any* token to this callback!
31+
let cs = unsafe { &bare_metal::CriticalSection::new() };
32+
gpioa.pa4.into_analog(cs)
33+
});
2834

2935
let mut dac = dac(dp.DAC, pa4, &mut rcc);
3036

examples/flash_systick.rs

+15-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ 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 bare_metal::Mutex;
11+
use cortex_m::{peripheral::syst::SystClkSource::Core, Peripherals};
1112
use cortex_m_rt::{entry, exception};
1213

1314
use core::cell::RefCell;
@@ -19,7 +20,12 @@ 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+
cortex_m::interrupt::free(move |_| {
24+
// SAFETY: We are in a critical section, but the `cortex_m` critical section
25+
// token is not compatible with the `bare_metal` token. Future version of the
26+
// `cortex_m` crate will not supply *any* token to this callback!
27+
let cs = unsafe { &bare_metal::CriticalSection::new() };
28+
2329
let mut rcc = p.RCC.configure().sysclk(48.mhz()).freeze(&mut p.FLASH);
2430

2531
let gpioa = p.GPIOA.split(&mut rcc);
@@ -28,7 +34,7 @@ fn main() -> ! {
2834
let led = gpioa.pa1.into_push_pull_output(cs);
2935

3036
// Transfer GPIO into a shared structure
31-
*GPIO.borrow(cs).borrow_mut() = Some(led);
37+
*GPIO.borrow(*cs).borrow_mut() = Some(led);
3238

3339
let mut syst = cp.SYST;
3440

@@ -62,7 +68,12 @@ fn SysTick() {
6268
static mut STATE: u8 = 1;
6369

6470
// Enter critical section
65-
cortex_m::interrupt::free(|cs| {
71+
cortex_m::interrupt::free(|_| {
72+
// SAFETY: We are in a critical section, but the `cortex_m` critical section
73+
// token is not compatible with the `bare_metal` token. Future version of the
74+
// `cortex_m` crate will not supply *any* token to this callback!
75+
let cs = unsafe { bare_metal::CriticalSection::new() };
76+
6677
// Borrow access to our GPIO pin from the shared structure
6778
if let Some(ref mut led) = *GPIO.borrow(cs).borrow_mut().deref_mut() {
6879
// Check state variable, keep LED off most of the time and turn it on every 10th tick

examples/flash_systick_fancier.rs

+15-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ 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 bare_metal::Mutex;
11+
use cortex_m::{peripheral::syst::SystClkSource::Core, Peripherals};
1112
use cortex_m_rt::{entry, exception};
1213

1314
use core::cell::RefCell;
@@ -22,7 +23,12 @@ 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+
cortex_m::interrupt::free(move |_| {
27+
// SAFETY: We are in a critical section, but the `cortex_m` critical section
28+
// token is not compatible with the `bare_metal` token. Future version of the
29+
// `cortex_m` crate will not supply *any* token to this callback!
30+
let cs = unsafe { &bare_metal::CriticalSection::new() };
31+
2632
let mut rcc = p.RCC.configure().sysclk(48.mhz()).freeze(&mut p.FLASH);
2733

2834
// Get access to individual pins in the GPIO port
@@ -32,7 +38,7 @@ fn main() -> ! {
3238
let led = gpioa.pb3.into_push_pull_output(cs);
3339

3440
// Transfer GPIO into a shared structure
35-
swap(&mut Some(led), &mut GPIO.borrow(cs).borrow_mut());
41+
swap(&mut Some(led), &mut GPIO.borrow(*cs).borrow_mut());
3642

3743
let mut syst = cp.SYST;
3844

@@ -88,7 +94,12 @@ fn SysTick() {
8894
// Otherwise move it out of the Mutex protected shared region into our exception handler
8995
else {
9096
// Enter critical section
91-
cortex_m::interrupt::free(|cs| {
97+
cortex_m::interrupt::free(|_| {
98+
// SAFETY: We are in a critical section, but the `cortex_m` critical section
99+
// token is not compatible with the `bare_metal` token. Future version of the
100+
// `cortex_m` crate will not supply *any* token to this callback!
101+
let cs = unsafe { bare_metal::CriticalSection::new() };
102+
92103
// Swap globally stored data with SysTick private data
93104
swap(LED, &mut GPIO.borrow(cs).borrow_mut());
94105
});

examples/i2c_find_address.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@ 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+
cortex_m::interrupt::free(move |_| {
18+
// SAFETY: We are in a critical section, but the `cortex_m` critical section
19+
// token is not compatible with the `bare_metal` token. Future version of the
20+
// `cortex_m` crate will not supply *any* token to this callback!
21+
let cs = unsafe { &bare_metal::CriticalSection::new() };
22+
1823
let mut flash = p.FLASH;
1924
let mut rcc = p.RCC.configure().freeze(&mut flash);
2025

examples/led_hal_button_irq.rs

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

15-
use cortex_m::{interrupt::Mutex, peripheral::Peripherals as c_m_Peripherals};
15+
use bare_metal::Mutex;
16+
use cortex_m::peripheral::Peripherals as c_m_Peripherals;
1617
use cortex_m_rt::entry;
1718

1819
use core::{cell::RefCell, ops::DerefMut};
@@ -29,7 +30,12 @@ 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+
cortex_m::interrupt::free(move |_| {
34+
// SAFETY: We are in a critical section, but the `cortex_m` critical section
35+
// token is not compatible with the `bare_metal` token. Future version of the
36+
// `cortex_m` crate will not supply *any* token to this callback!
37+
let cs = unsafe { &bare_metal::CriticalSection::new() };
38+
3339
// Enable clock for SYSCFG
3440
let rcc = p.RCC;
3541
rcc.apb2enr.modify(|_, w| w.syscfgen().set_bit());
@@ -64,9 +70,9 @@ fn main() -> ! {
6470
exti.rtsr.modify(|_, w| w.tr1().set_bit());
6571

6672
// Move control over LED and DELAY and EXTI into global mutexes
67-
*LED.borrow(cs).borrow_mut() = Some(led);
68-
*DELAY.borrow(cs).borrow_mut() = Some(delay);
69-
*INT.borrow(cs).borrow_mut() = Some(exti);
73+
*LED.borrow(*cs).borrow_mut() = Some(led);
74+
*DELAY.borrow(*cs).borrow_mut() = Some(delay);
75+
*INT.borrow(*cs).borrow_mut() = Some(exti);
7076

7177
// Enable EXTI IRQ, set prio 1 and clear any pending IRQs
7278
let mut nvic = cp.NVIC;
@@ -88,7 +94,12 @@ fn main() -> ! {
8894
#[interrupt]
8995
fn EXTI0_1() {
9096
// Enter critical section
91-
cortex_m::interrupt::free(|cs| {
97+
cortex_m::interrupt::free(|_| {
98+
// SAFETY: We are in a critical section, but the `cortex_m` critical section
99+
// token is not compatible with the `bare_metal` token. Future version of the
100+
// `cortex_m` crate will not supply *any* token to this callback!
101+
let cs = unsafe { bare_metal::CriticalSection::new() };
102+
92103
// Obtain all Mutex protected resources
93104
if let (&mut Some(ref mut led), &mut Some(ref mut delay), &mut Some(ref mut exti)) = (
94105
LED.borrow(cs).borrow_mut().deref_mut(),

examples/pwm.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#![deny(unsafe_code)]
21
#![no_main]
32
#![no_std]
43

@@ -18,7 +17,12 @@ fn main() -> ! {
1817
let mut rcc = dp.RCC.configure().sysclk(8.mhz()).freeze(&mut dp.FLASH);
1918

2019
let gpioa = dp.GPIOA.split(&mut rcc);
21-
let channels = cortex_m::interrupt::free(move |cs| {
20+
let channels = cortex_m::interrupt::free(move |_| {
21+
// SAFETY: We are in a critical section, but the `cortex_m` critical section
22+
// token is not compatible with the `bare_metal` token. Future version of the
23+
// `cortex_m` crate will not supply *any* token to this callback!
24+
let cs = unsafe { &bare_metal::CriticalSection::new() };
25+
2226
(
2327
gpioa.pa8.into_alternate_af2(cs),
2428
gpioa.pa9.into_alternate_af2(cs),

0 commit comments

Comments
 (0)