diff --git a/CHANGELOG.md b/CHANGELOG.md index 939ecfb9..795edcb2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,6 +44,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/). implementation, but it is hard to maintain the current implementation and not easy to verify if it is really a safe implementation. It is also not consistent with the rest of the crates API. ([#352]) +- Use [critical-section] crate instead of `interrupt_free`, which is not always + sound. ([#350]) + +[critical-section]: https://github.com/rust-embedded/critical-section ## [v0.9.2] - 2023-02-20 @@ -611,6 +615,7 @@ let clocks = rcc [#352]: https://github.com/stm32-rs/stm32f3xx-hal/pull/352 [#351]: https://github.com/stm32-rs/stm32f3xx-hal/pull/351 +[#350]: https://github.com/stm32-rs/stm32f3xx-hal/pull/350 [#345]: https://github.com/stm32-rs/stm32f3xx-hal/pull/345 [#346]: https://github.com/stm32-rs/stm32f3xx-hal/pull/346 [#347]: https://github.com/stm32-rs/stm32f3xx-hal/pull/347 diff --git a/Cargo.toml b/Cargo.toml index 55b64fd8..e2ce040d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,6 +31,7 @@ bxcan = { version = "0.7.0", optional = true } cfg-if = "1.0.0" cortex-m = "0.7.4" cortex-m-rt = "0.7.3" +critical-section = "1.1.2" defmt = { version = ">=0.2.3, <0.4.0", optional = true } embedded-dma = "0.2.0" embedded-hal = { version = "0.2.5", features = ["unproven"] } diff --git a/examples/adc.rs b/examples/adc.rs index 7e017454..9c43d069 100644 --- a/examples/adc.rs +++ b/examples/adc.rs @@ -9,8 +9,8 @@ use panic_probe as _; use core::cell::RefCell; use cortex_m::asm; -use cortex_m::interrupt::Mutex; use cortex_m_rt::entry; +use critical_section::Mutex; use embedded_hal::adc::OneShot; use stm32f3xx_hal::{ @@ -99,7 +99,7 @@ fn main() -> ! { // Start a timer which fires regularly to wake up from `asm::wfi` timer.start(500.milliseconds()); // Put the timer in the global context. - cortex_m::interrupt::free(|cs| { + critical_section::with(|cs| { TIMER.borrow(cs).replace(Some(timer)); }); @@ -133,7 +133,7 @@ fn main() -> ! { #[interrupt] fn TIM2() { // Just handle the pending interrupt event. - cortex_m::interrupt::free(|cs| { + critical_section::with(|cs| { TIMER // Unlock resource for use in critical section .borrow(cs) diff --git a/examples/gpio_interrupts.rs b/examples/gpio_interrupts.rs index c835ec5f..bf0701bf 100644 --- a/examples/gpio_interrupts.rs +++ b/examples/gpio_interrupts.rs @@ -5,8 +5,9 @@ use core::cell::RefCell; use panic_semihosting as _; -use cortex_m::{asm, interrupt::Mutex, peripheral::NVIC}; +use cortex_m::{asm, peripheral::NVIC}; use cortex_m_rt::entry; +use critical_section::Mutex; use stm32f3xx_hal::{ gpio::{self, Edge, Input, Output, PushPull}, @@ -38,7 +39,7 @@ fn main() -> ! { led.toggle().expect("unable to toggle led in configuration"); // Move the ownership of the led to the global LED - cortex_m::interrupt::free(|cs| *LED.borrow(cs).borrow_mut() = Some(led)); + critical_section::with(|cs| *LED.borrow(cs).borrow_mut() = Some(led)); // Configuring the user button to trigger an interrupt when the button is pressed. let mut user_button = gpioa @@ -50,7 +51,7 @@ fn main() -> ! { let interrupt_num = user_button.interrupt(); // hal::pac::Interrupt::EXTI0 // Moving ownership to the global BUTTON so we can clear the interrupt pending bit. - cortex_m::interrupt::free(|cs| *BUTTON.borrow(cs).borrow_mut() = Some(user_button)); + critical_section::with(|cs| *BUTTON.borrow(cs).borrow_mut() = Some(user_button)); unsafe { NVIC::unmask(interrupt_num) }; @@ -67,7 +68,7 @@ fn main() -> ! { // This may be called more than once per button press from the user since the button may not be debounced. #[interrupt] fn EXTI0() { - cortex_m::interrupt::free(|cs| { + critical_section::with(|cs| { // Toggle the LED LED.borrow(cs) .borrow_mut() diff --git a/src/adc.rs b/src/adc.rs index e2b1b645..d74e1eb8 100644 --- a/src/adc.rs +++ b/src/adc.rs @@ -133,7 +133,7 @@ where { /// Releases the common ADC peripheral pub fn free(self, _adcs: &::Childs) -> ADC { - cortex_m::interrupt::free(|_| { + critical_section::with(|_| { // SAFETY: Guaranteed to be the only instance left, which has control over the // `ADC`perpherals, and criticala section ensure that no race condition happens // on the `Bus` peripheral. diff --git a/src/usb.rs b/src/usb.rs index b40e4b4a..f0aef6a8 100644 --- a/src/usb.rs +++ b/src/usb.rs @@ -97,7 +97,7 @@ unsafe impl UsbPeripheral for Peripheral