Skip to content

Commit

Permalink
Add implementation for critical-section 1.0.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dirbaio committed Aug 10, 2022
1 parent e0bfe3a commit e30b3b2
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Expand Up @@ -29,6 +29,6 @@ jobs:
toolchain: ${{ matrix.rust }}
override: true
- name: Run tests
run: cargo test --all
run: cargo test --all --features critical-section-single-core

# FIXME: test on macOS and Windows
1 change: 1 addition & 0 deletions .github/workflows/clippy.yml
Expand Up @@ -23,3 +23,4 @@ jobs:
- uses: actions-rs/clippy-check@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
args: --features critical-section-single-core
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

- Added `critical-section-single-core` feature which provides an implementation for the `critical_section` crate for single-core systems, based on disabling all interrupts. (#448)

## [v0.7.5] - 2022-05-15

### Deprecated
Expand Down
2 changes: 2 additions & 0 deletions Cargo.toml
Expand Up @@ -17,6 +17,7 @@ links = "cortex-m" # prevent multiple versions of this crate to be linked toget

[dependencies]
bare-metal = { version = "0.2.4", features = ["const-fn"] }
critical-section = "1.0.0-alpha.2"
volatile-register = "0.2.0"
bitfield = "0.13.2"
embedded-hal = "0.2.4"
Expand All @@ -32,6 +33,7 @@ cm7-r0p1 = ["cm7"]
inline-asm = []
linker-plugin-lto = []
std = []
critical-section-single-core = ["critical-section/restore-state-bool"]

[workspace]
members = ["xtask", "cortex-m-semihosting", "panic-semihosting", "panic-itm"]
Expand Down
27 changes: 27 additions & 0 deletions src/critical_section.rs
@@ -0,0 +1,27 @@
#[cfg(all(cortex_m, feature = "critical-section-single-core"))]
mod single_core_critical_section {
use critical_section::{set_impl, Impl, RawRestoreState};

use crate::interrupt;
use crate::register::primask;

struct SingleCoreCriticalSection;
set_impl!(SingleCoreCriticalSection);

unsafe impl Impl for SingleCoreCriticalSection {
unsafe fn acquire() -> RawRestoreState {
let active = primask::read().is_active();
if active {
interrupt::disable();
}
active
}

unsafe fn release(was_active: RawRestoreState) {
// Only re-enable interrupts if they were enabled before the critical section.
if was_active {
interrupt::enable()
}
}
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Expand Up @@ -90,6 +90,7 @@ mod macros;
pub mod asm;
#[cfg(armv8m)]
pub mod cmse;
mod critical_section;
pub mod delay;
pub mod interrupt;
#[cfg(all(not(armv6m), not(armv8m_base)))]
Expand Down
7 changes: 7 additions & 0 deletions xtask/tests/ci.rs
Expand Up @@ -32,6 +32,13 @@ fn build(package: &str, target: &str, features: &[&str]) {
cargo.args(&["--features", *feat]);
}

// A `critical_section` implementation is always needed.
if package == "cortex-m" {
cargo.args(&["--features", "critical-section-single-core"]);
} else {
cargo.args(&["--features", "cortex-m/critical-section-single-core"]);
}

// Cargo features don't work right when invoked from the workspace root, so change to the
// package's directory when necessary.
if package != "cortex-m" {
Expand Down

0 comments on commit e30b3b2

Please sign in to comment.