Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add implementation for critical-section 1.0 for single-core chips.
  • Loading branch information
Dirbaio committed Aug 21, 2022
commit 8429f8c4ed4446abe8a00e860d59364df2b1bfd2
10 changes: 9 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ jobs:
run: cargo check --target riscv64imac-unknown-none-elf
- name: Run CI script for riscv64gc-unknown-none-elf under ${{ matrix.rust }}
run: cargo check --target riscv64gc-unknown-none-elf
- name: Run CI script for x86_64-unknown-linux-gnu under ${{ matrix.rust }} with critical-section-single-core
run: cargo check --target x86_64-unknown-linux-gnu --features critical-section-single-core
- name: Run CI script for riscv32imac-unknown-none-elf under ${{ matrix.rust }} with critical-section-single-core
run: cargo check --target riscv32imac-unknown-none-elf --features critical-section-single-core
- name: Run CI script for riscv64imac-unknown-none-elf under ${{ matrix.rust }} with critical-section-single-core
run: cargo check --target riscv64imac-unknown-none-elf --features critical-section-single-core
- name: Run CI script for riscv64gc-unknown-none-elf under ${{ matrix.rust }} with critical-section-single-core
run: cargo check --target riscv64gc-unknown-none-elf --features critical-section-single-core

# On macOS and Windows, we at least make sure that the crate builds and links.
build-other:
Expand All @@ -56,4 +64,4 @@ jobs:
toolchain: stable
override: true
- name: Build crate for host OS
run: cargo build
run: cargo build --features critical-section-single-core
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

### Added

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

### Fixed

- Fix `asm::delay()` to ensure count register is always reloaded
Expand Down
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ targets = [
"riscv64imac-unknown-none-elf", "riscv64gc-unknown-none-elf",
]

[features]
critical-section-single-core = ["critical-section/restore-state-bool"]

[dependencies]
bare-metal = "1.0.0"
bit_field = "0.10.0"
critical-section = "1.1.0"
embedded-hal = "0.2.6"
22 changes: 22 additions & 0 deletions src/critical_section.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use critical_section::{set_impl, Impl, RawRestoreState};

use crate::interrupt;
use crate::register::mstatus;

struct SingleCoreCriticalSection;
set_impl!(SingleCoreCriticalSection);

unsafe impl Impl for SingleCoreCriticalSection {
unsafe fn acquire() -> RawRestoreState {
let was_active = mstatus::read().mie();
interrupt::disable();
was_active
}

unsafe fn release(was_active: RawRestoreState) {
// Only re-enable interrupts if they were enabled before the critical section.
if was_active {
interrupt::enable()
}
}
}
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@ pub mod register;

#[macro_use]
mod macros;

#[cfg(all(riscv, feature = "critical-section-single-core"))]
mod critical_section;