Skip to content

Commit 2b3864c

Browse files
committed
Replace interrupt::free with critical_section::with.
1 parent 0d94e3b commit 2b3864c

File tree

8 files changed

+38
-32
lines changed

8 files changed

+38
-32
lines changed

.github/workflows/clippy.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ jobs:
2323
- uses: actions-rs/clippy-check@v1
2424
with:
2525
token: ${{ secrets.GITHUB_TOKEN }}
26-
args: --all
26+
args: --all --features single-core-critical-section

cortex-m-semihosting/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ no-semihosting = []
2121

2222
[dependencies]
2323
cortex-m = { path = "..", version = ">= 0.5.8, < 0.8" }
24+
critical-section = "0.2"

cortex-m-semihosting/src/export.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@
22
33
use core::fmt::{self, Write};
44

5-
use cortex_m::interrupt;
6-
75
use crate::hio::{self, HostStream};
86

97
static mut HSTDOUT: Option<HostStream> = None;
108

119
pub fn hstdout_str(s: &str) {
12-
let _result = interrupt::free(|| unsafe {
10+
let _result = critical_section::with(|_| unsafe {
1311
if HSTDOUT.is_none() {
1412
HSTDOUT = Some(hio::hstdout()?);
1513
}
@@ -19,7 +17,7 @@ pub fn hstdout_str(s: &str) {
1917
}
2018

2119
pub fn hstdout_fmt(args: fmt::Arguments) {
22-
let _result = interrupt::free(|| unsafe {
20+
let _result = critical_section::with(|_| unsafe {
2321
if HSTDOUT.is_none() {
2422
HSTDOUT = Some(hio::hstdout()?);
2523
}
@@ -31,7 +29,7 @@ pub fn hstdout_fmt(args: fmt::Arguments) {
3129
static mut HSTDERR: Option<HostStream> = None;
3230

3331
pub fn hstderr_str(s: &str) {
34-
let _result = interrupt::free(|| unsafe {
32+
let _result = critical_section::with(|_| unsafe {
3533
if HSTDERR.is_none() {
3634
HSTDERR = Some(hio::hstderr()?);
3735
}
@@ -41,7 +39,7 @@ pub fn hstderr_str(s: &str) {
4139
}
4240

4341
pub fn hstderr_fmt(args: fmt::Arguments) {
44-
let _result = interrupt::free(|| unsafe {
42+
let _result = critical_section::with(|_| unsafe {
4543
if HSTDERR.is_none() {
4644
HSTDERR = Some(hio::hstderr()?);
4745
}

src/critical_section.rs

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,32 @@
1-
use crate::interrupt;
2-
use crate::register::primask::{self, Primask};
1+
#[cfg(all(cortex_m, feature = "single-core-critical-section"))]
2+
mod single_core_critical_section {
3+
use crate::interrupt;
4+
use crate::register::primask::{self, Primask};
35

4-
struct CriticalSection;
5-
critical_section::custom_impl!(CriticalSection);
6+
struct CriticalSection;
7+
critical_section::custom_impl!(CriticalSection);
68

7-
const TOKEN_IGNORE: u8 = 0;
8-
const TOKEN_REENABLE: u8 = 1;
9+
const TOKEN_IGNORE: u8 = 0;
10+
const TOKEN_REENABLE: u8 = 1;
911

10-
unsafe impl critical_section::Impl for CriticalSection {
11-
unsafe fn acquire() -> u8 {
12-
match primask::read() {
13-
Primask::Active => {
14-
interrupt::disable();
15-
TOKEN_REENABLE
12+
unsafe impl critical_section::Impl for CriticalSection {
13+
unsafe fn acquire() -> u8 {
14+
match primask::read() {
15+
Primask::Active => {
16+
interrupt::disable();
17+
TOKEN_REENABLE
18+
}
19+
Primask::Inactive => TOKEN_IGNORE,
1620
}
17-
Primask::Inactive => TOKEN_IGNORE,
1821
}
19-
}
2022

21-
unsafe fn release(token: u8) {
22-
// Only re-enable interrupts if they were enabled before the critical section.
23-
if token == TOKEN_REENABLE {
24-
interrupt::enable()
23+
unsafe fn release(token: u8) {
24+
// Only re-enable interrupts if they were enabled before the critical section.
25+
if token == TOKEN_REENABLE {
26+
interrupt::enable()
27+
}
2528
}
2629
}
2730
}
31+
32+
pub use critical_section::with;

src/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,11 @@ mod macros;
4949
pub mod asm;
5050
#[cfg(armv8m)]
5151
pub mod cmse;
52-
#[cfg(all(cortex_m, feature = "single-core-critical-section"))]
53-
mod critical_section;
52+
// This is only public so the `singleton` macro does not require depending on
53+
// the `critical-section` crate separately.
54+
#[doc(hidden)]
55+
#[cfg(feature = "critical-section")]
56+
pub mod critical_section;
5457
pub mod delay;
5558
pub mod interrupt;
5659
#[cfg(all(not(armv6m), not(armv8m_base)))]

src/macros.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ macro_rules! iprintln {
6262
#[macro_export]
6363
macro_rules! singleton {
6464
($name:ident: $ty:ty = $expr:expr) => {
65-
$crate::interrupt::free(|| {
65+
$crate::critical_section::with(|| {
6666
// this is a tuple of a MaybeUninit and a bool because using an Option here is
6767
// problematic: Due to niche-optimization, an Option could end up producing a non-zero
6868
// initializer value which would move the entire static from `.bss` into `.data`...

src/peripheral/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757
//!
5858
//! - ARMv7-M Architecture Reference Manual (Issue E.b) - Chapter B3
5959
60-
use crate::interrupt;
6160
use core::marker::PhantomData;
6261
use core::ops;
6362

@@ -164,7 +163,7 @@ impl Peripherals {
164163
/// Returns all the core peripherals *once*
165164
#[inline]
166165
pub fn take() -> Option<Self> {
167-
interrupt::free(|| {
166+
critical_section::with(|_| {
168167
if unsafe { TAKEN } {
169168
None
170169
} else {

src/peripheral/sau.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ impl SAU {
162162
/// This function is executed under a critical section to prevent having inconsistent results.
163163
#[inline]
164164
pub fn set_region(&mut self, region_number: u8, region: SauRegion) -> Result<(), SauError> {
165-
interrupt::free(|| {
165+
critical_section::with(|| {
166166
let base_address = region.base_address;
167167
let limit_address = region.limit_address;
168168
let attribute = region.attribute;
@@ -215,7 +215,7 @@ impl SAU {
215215
/// This function is executed under a critical section to prevent having inconsistent results.
216216
#[inline]
217217
pub fn get_region(&mut self, region_number: u8) -> Result<SauRegion, SauError> {
218-
interrupt::free(|| {
218+
critical_section::with(|| {
219219
if region_number >= self.region_numbers() {
220220
Err(SauError::RegionNumberTooBig)
221221
} else {

0 commit comments

Comments
 (0)