Skip to content

Commit

Permalink
Merge pull request #4 from vapor-keeb/main
Browse files Browse the repository at this point in the history
fix(Critical Section): rewrite critical section
  • Loading branch information
andelf authored Oct 3, 2024
2 parents db70c90 + ae63d58 commit 9073610
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 17 deletions.
7 changes: 3 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ documentation = "https://docs.rs/qingke"
homepage = "https://github.com/ch32-rs/qingke"
categories = ["embedded", "no-std", "hardware-support"]
license = "MIT/Apache-2.0"
version = "0.2.1" # for rt and macros
version = "0.3.0" # for rt and macros
edition = "2021"

[package]
name = "qingke"
# version.workspace = true
version = "0.2.0"
version.workspace = true
edition.workspace = true
authors.workspace = true
repository.workspace = true
Expand All @@ -30,7 +29,7 @@ readme = "README.md"
bit_field = "0.10.2"
riscv = "0.11.1"
critical-section = { version = "1.1.3", features = [
"restore-state-u8",
"restore-state-bool",
], optional = true }
defmt = { version = "0.3.8", optional = true }

Expand Down
4 changes: 2 additions & 2 deletions qingke-rt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ v4 = []
highcode = []

[dependencies]
qingke-rt-macros = { path = "./macros", version = "=0.2.1" }
qingke = { path = "../", version = "=0.2.0" }
qingke-rt-macros = { path = "./macros", version = "0.3" }
qingke = { path = "../", version = "0.3" }

[package.metadata.docs.rs]
targets = ["riscv32imac-unknown-none-elf"]
2 changes: 2 additions & 0 deletions qingke-rt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ unsafe extern "C" fn qingke_setup_interrupts() {

#[cfg(not(feature = "highcode"))]
mtvec::write(0x00000000, TrapMode::VectoredAddress);

qingke::pfic::wfi_to_wfe(true);
}

#[doc(hidden)]
Expand Down
11 changes: 4 additions & 7 deletions src/critical_section_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,13 @@ set_impl!(SingleHartCriticalSection);

unsafe impl Impl for SingleHartCriticalSection {
unsafe fn acquire() -> RawRestoreState {
let irq_state = gintenr::read();
if irq_state & 0x08 != 0 {
gintenr::write(irq_state & (!0x08));
}
irq_state as u8 // as the high bits are reserved 0x0.
(gintenr::set_disable() & 0x8) != 0
}

unsafe fn release(irq_state: RawRestoreState) {
// Only re-enable interrupts if they were enabled before the critical section.
let irq_state = irq_state as usize;
gintenr::write(gintenr::read() | (irq_state & 0x08));
if irq_state {
gintenr::set_enable();
}
}
}
13 changes: 13 additions & 0 deletions src/pfic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,16 @@ pub unsafe fn disable_vtf(channel: u8) {
let val = ptr::read_volatile(PFIC_VTFADDRR0.offset(channel as isize));
ptr::write_volatile(PFIC_VTFADDRR0.offset(channel as isize), val & 0xFFFF_FFFE);
}

pub unsafe fn wfi_to_wfe(v: bool) {
critical_section::with(|_| {
let mut val = ptr::read_volatile(PFIC_SCTLR);
// 0x8 is WFITOWFE bit
if v {
val |= 0x8;
} else {
val &= !0x8;
}
ptr::write_volatile(PFIC_SCTLR, val);
});
}
13 changes: 9 additions & 4 deletions src/register/gintenr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,20 @@ pub fn read() -> usize {

#[inline]
pub unsafe fn write(bits: usize) {
asm!("csrs 0x800, {}", in(reg) bits);
asm!("csrw 0x800, {}", in(reg) bits);
}

#[inline]
pub unsafe fn set_enable() {
write(0x08)
let mask = 0x8;
asm!("csrs 0x800, {}", in(reg) mask);
}

#[inline]
pub unsafe fn set_disable() {
write(0x00)
/// Disable interrupt and return the old `GINTENR` value
pub fn set_disable() -> usize {
let prev: usize;
let mask = 0x8usize;
unsafe { asm!("csrrc {}, 0x800, {}", out(reg) prev, in(reg) mask) };
prev
}

0 comments on commit 9073610

Please sign in to comment.