Skip to content

Commit 5198f0d

Browse files
authored
Use critical section instead of interrupt::free (#350)
1 parent 22de3d0 commit 5198f0d

File tree

6 files changed

+16
-9
lines changed

6 files changed

+16
-9
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
4444
implementation, but it is hard to maintain the current implementation
4545
and not easy to verify if it is really a safe implementation.
4646
It is also not consistent with the rest of the crates API. ([#352])
47+
- Use [critical-section] crate instead of `interrupt_free`, which is not always
48+
sound. ([#350])
49+
50+
[critical-section]: https://github.com/rust-embedded/critical-section
4751

4852
## [v0.9.2] - 2023-02-20
4953

@@ -611,6 +615,7 @@ let clocks = rcc
611615

612616
[#352]: https://github.com/stm32-rs/stm32f3xx-hal/pull/352
613617
[#351]: https://github.com/stm32-rs/stm32f3xx-hal/pull/351
618+
[#350]: https://github.com/stm32-rs/stm32f3xx-hal/pull/350
614619
[#345]: https://github.com/stm32-rs/stm32f3xx-hal/pull/345
615620
[#346]: https://github.com/stm32-rs/stm32f3xx-hal/pull/346
616621
[#347]: https://github.com/stm32-rs/stm32f3xx-hal/pull/347

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ bxcan = { version = "0.7.0", optional = true }
3131
cfg-if = "1.0.0"
3232
cortex-m = "0.7.4"
3333
cortex-m-rt = "0.7.3"
34+
critical-section = "1.1.2"
3435
defmt = { version = ">=0.2.3, <0.4.0", optional = true }
3536
embedded-dma = "0.2.0"
3637
embedded-hal = { version = "0.2.5", features = ["unproven"] }

examples/adc.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ use panic_probe as _;
99
use core::cell::RefCell;
1010

1111
use cortex_m::asm;
12-
use cortex_m::interrupt::Mutex;
1312
use cortex_m_rt::entry;
13+
use critical_section::Mutex;
1414

1515
use embedded_hal::adc::OneShot;
1616
use stm32f3xx_hal::{
@@ -99,7 +99,7 @@ fn main() -> ! {
9999
// Start a timer which fires regularly to wake up from `asm::wfi`
100100
timer.start(500.milliseconds());
101101
// Put the timer in the global context.
102-
cortex_m::interrupt::free(|cs| {
102+
critical_section::with(|cs| {
103103
TIMER.borrow(cs).replace(Some(timer));
104104
});
105105

@@ -133,7 +133,7 @@ fn main() -> ! {
133133
#[interrupt]
134134
fn TIM2() {
135135
// Just handle the pending interrupt event.
136-
cortex_m::interrupt::free(|cs| {
136+
critical_section::with(|cs| {
137137
TIMER
138138
// Unlock resource for use in critical section
139139
.borrow(cs)

examples/gpio_interrupts.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ use core::cell::RefCell;
55

66
use panic_semihosting as _;
77

8-
use cortex_m::{asm, interrupt::Mutex, peripheral::NVIC};
8+
use cortex_m::{asm, peripheral::NVIC};
99
use cortex_m_rt::entry;
10+
use critical_section::Mutex;
1011

1112
use stm32f3xx_hal::{
1213
gpio::{self, Edge, Input, Output, PushPull},
@@ -38,7 +39,7 @@ fn main() -> ! {
3839
led.toggle().expect("unable to toggle led in configuration");
3940

4041
// Move the ownership of the led to the global LED
41-
cortex_m::interrupt::free(|cs| *LED.borrow(cs).borrow_mut() = Some(led));
42+
critical_section::with(|cs| *LED.borrow(cs).borrow_mut() = Some(led));
4243

4344
// Configuring the user button to trigger an interrupt when the button is pressed.
4445
let mut user_button = gpioa
@@ -50,7 +51,7 @@ fn main() -> ! {
5051
let interrupt_num = user_button.interrupt(); // hal::pac::Interrupt::EXTI0
5152

5253
// Moving ownership to the global BUTTON so we can clear the interrupt pending bit.
53-
cortex_m::interrupt::free(|cs| *BUTTON.borrow(cs).borrow_mut() = Some(user_button));
54+
critical_section::with(|cs| *BUTTON.borrow(cs).borrow_mut() = Some(user_button));
5455

5556
unsafe { NVIC::unmask(interrupt_num) };
5657

@@ -67,7 +68,7 @@ fn main() -> ! {
6768
// This may be called more than once per button press from the user since the button may not be debounced.
6869
#[interrupt]
6970
fn EXTI0() {
70-
cortex_m::interrupt::free(|cs| {
71+
critical_section::with(|cs| {
7172
// Toggle the LED
7273
LED.borrow(cs)
7374
.borrow_mut()

src/adc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ where
133133
{
134134
/// Releases the common ADC peripheral
135135
pub fn free(self, _adcs: &<ADC as CommonInstance>::Childs) -> ADC {
136-
cortex_m::interrupt::free(|_| {
136+
critical_section::with(|_| {
137137
// SAFETY: Guaranteed to be the only instance left, which has control over the
138138
// `ADC`perpherals, and criticala section ensure that no race condition happens
139139
// on the `Bus` peripheral.

src/usb.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ unsafe impl<Dm: DmPin + Send, Dp: DpPin + Send> UsbPeripheral for Peripheral<Dm,
9797
fn enable() {
9898
// SAFETY: the cricitcal section ensures, that the RCC access to enable the USB peripheral
9999
// is mutually exclusive
100-
cortex_m::interrupt::free(|_| unsafe {
100+
critical_section::with(|_| unsafe {
101101
// Enable USB peripheral
102102
USB::enable_unchecked();
103103
// Reset USB peripheral

0 commit comments

Comments
 (0)