Skip to content

Commit 8429f8c

Browse files
committed
Add implementation for critical-section 1.0 for single-core chips.
1 parent d75714a commit 8429f8c

File tree

5 files changed

+42
-1
lines changed

5 files changed

+42
-1
lines changed

.github/workflows/ci.yaml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ jobs:
3838
run: cargo check --target riscv64imac-unknown-none-elf
3939
- name: Run CI script for riscv64gc-unknown-none-elf under ${{ matrix.rust }}
4040
run: cargo check --target riscv64gc-unknown-none-elf
41+
- name: Run CI script for x86_64-unknown-linux-gnu under ${{ matrix.rust }} with critical-section-single-core
42+
run: cargo check --target x86_64-unknown-linux-gnu --features critical-section-single-core
43+
- name: Run CI script for riscv32imac-unknown-none-elf under ${{ matrix.rust }} with critical-section-single-core
44+
run: cargo check --target riscv32imac-unknown-none-elf --features critical-section-single-core
45+
- name: Run CI script for riscv64imac-unknown-none-elf under ${{ matrix.rust }} with critical-section-single-core
46+
run: cargo check --target riscv64imac-unknown-none-elf --features critical-section-single-core
47+
- name: Run CI script for riscv64gc-unknown-none-elf under ${{ matrix.rust }} with critical-section-single-core
48+
run: cargo check --target riscv64gc-unknown-none-elf --features critical-section-single-core
4149

4250
# On macOS and Windows, we at least make sure that the crate builds and links.
4351
build-other:
@@ -56,4 +64,4 @@ jobs:
5664
toolchain: stable
5765
override: true
5866
- name: Build crate for host OS
59-
run: cargo build
67+
run: cargo build --features critical-section-single-core

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- Added `critical-section-single-core` feature which provides an implementation for the `critical_section` crate for single-core systems, based on disabling all interrupts.
13+
1014
### Fixed
1115

1216
- Fix `asm::delay()` to ensure count register is always reloaded

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ targets = [
1717
"riscv64imac-unknown-none-elf", "riscv64gc-unknown-none-elf",
1818
]
1919

20+
[features]
21+
critical-section-single-core = ["critical-section/restore-state-bool"]
22+
2023
[dependencies]
2124
bare-metal = "1.0.0"
2225
bit_field = "0.10.0"
26+
critical-section = "1.1.0"
2327
embedded-hal = "0.2.6"

src/critical_section.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use critical_section::{set_impl, Impl, RawRestoreState};
2+
3+
use crate::interrupt;
4+
use crate::register::mstatus;
5+
6+
struct SingleCoreCriticalSection;
7+
set_impl!(SingleCoreCriticalSection);
8+
9+
unsafe impl Impl for SingleCoreCriticalSection {
10+
unsafe fn acquire() -> RawRestoreState {
11+
let was_active = mstatus::read().mie();
12+
interrupt::disable();
13+
was_active
14+
}
15+
16+
unsafe fn release(was_active: RawRestoreState) {
17+
// Only re-enable interrupts if they were enabled before the critical section.
18+
if was_active {
19+
interrupt::enable()
20+
}
21+
}
22+
}

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,6 @@ pub mod register;
2222

2323
#[macro_use]
2424
mod macros;
25+
26+
#[cfg(all(riscv, feature = "critical-section-single-core"))]
27+
mod critical_section;

0 commit comments

Comments
 (0)