Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use critical section instead of interrupt::free #350

Merged
merged 1 commit into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
6 changes: 3 additions & 3 deletions examples/adc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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));
});

Expand Down Expand Up @@ -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)
Expand Down
9 changes: 5 additions & 4 deletions examples/gpio_interrupts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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
Expand All @@ -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) };

Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion src/adc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ where
{
/// Releases the common ADC peripheral
pub fn free(self, _adcs: &<ADC as CommonInstance>::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.
Expand Down
2 changes: 1 addition & 1 deletion src/usb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ unsafe impl<Dm: DmPin + Send, Dp: DpPin + Send> UsbPeripheral for Peripheral<Dm,
fn enable() {
// SAFETY: the cricitcal section ensures, that the RCC access to enable the USB peripheral
// is mutually exclusive
cortex_m::interrupt::free(|_| unsafe {
critical_section::with(|_| unsafe {
// Enable USB peripheral
USB::enable_unchecked();
// Reset USB peripheral
Expand Down
Loading