Skip to content

Compatibility with riscv v0.14.0, riscv-peripheral v0.3.0, and riscv-rt v0.15.0 #42

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 12 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,16 @@ members = [
"e310x",
"e310x-hal",
"hifive1",
"hifive1-examples",
]
default-members = [
"e310x",
"e310x-hal",
"hifive1",
]

[workspace.dependencies]
critical-section = "1.2.0"
riscv = { git = "https://github.com/rust-embedded/riscv.git", branch = "post-init" }
riscv-peripheral = { git = "https://github.com/rust-embedded/riscv.git", branch = "post-init" }
riscv-rt = { git = "https://github.com/rust-embedded/riscv.git", branch = "post-init", features = ["single-hart", "no-interrupts"] }
3 changes: 3 additions & 0 deletions e310x-hal/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

### Changed
- Update `e310x` dependency and adapt code

## [v0.12.0] - 2024-12-10

### Changed
Expand Down
4 changes: 2 additions & 2 deletions e310x-hal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ embedded-hal-nb = "1.0.0"
embedded-io = "0.6.1"
e310x = { path = "../e310x", version = "0.12.0", features = ["rt", "critical-section"] }
nb = "1.0.0"
portable-atomic = { version = "1.9", default-features = false}
riscv = { version = "0.12.1", features = ["critical-section-single-hart"] }
portable-atomic = { version = "1.9", default-features = false }
riscv = { workspace = true, features = ["critical-section-single-hart"] }

[features]
g002 = ["e310x/g002"]
Expand Down
6 changes: 3 additions & 3 deletions e310x-hal/src/clock.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Clock configuration
use crate::time::Hertz;
use e310x::{Aonclk as AONCLK, Prci as PRCI, CLINT};
use e310x::{Aonclk as AONCLK, Clint, Prci as PRCI};
use riscv::interrupt;
use riscv::register::mcycle;

Expand Down Expand Up @@ -290,7 +290,7 @@ impl CoreClk {
// Need to wait 100 us
// RTC is running at 32kHz.
// So wait 4 ticks of RTC.
let mtime = CLINT::mtimer().mtime;
let mtime = unsafe { Clint::steal() }.mtimer().mtime();
let time = mtime.read() + 4;
while mtime.read() < time {}
// Now it is safe to check for PLL Lock
Expand Down Expand Up @@ -384,7 +384,7 @@ impl Clocks {

/// Measure the coreclk frequency by counting the number of aonclk ticks.
fn _measure_coreclk(&self, min_ticks: u64) -> Hertz {
let mtime = CLINT::mtimer().mtime;
let mtime = unsafe { Clint::steal() }.mtimer().mtime();
interrupt::free(|| {
// Don't start measuring until we see an mtime tick
while mtime.read() == mtime.read() {}
Expand Down
2 changes: 1 addition & 1 deletion e310x-hal/src/core/counters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub struct PerformanceCounters {
}

impl PerformanceCounters {
pub(crate) fn new() -> Self {
pub(crate) const fn new() -> Self {
Self {
mcycle: MCYCLE,
minstret: MINSTRET,
Expand Down
12 changes: 9 additions & 3 deletions e310x-hal/src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,23 @@

pub mod counters;

pub use e310x::{CLINT, PLIC};
use e310x::{Clint, Plic};

/// Core peripherals
pub struct CorePeripherals {
/// Core Local Interruptor (CLINT)
pub clint: Clint,
/// Platform-Level Interrupt Controller (PLIC)
pub plic: Plic,
/// Performance counters
pub counters: counters::PerformanceCounters,
}

impl CorePeripherals {
pub(crate) fn new() -> Self {
pub(crate) const fn new(clint: Clint, plic: Plic) -> Self {
Self {
clint,
plic,
counters: counters::PerformanceCounters::new(),
}
}
Expand All @@ -23,6 +29,6 @@ impl CorePeripherals {
///
/// Using this function may break the guarantees of the singleton pattern.
pub unsafe fn steal() -> Self {
Self::new()
Self::new(Clint::steal(), Plic::steal())
}
}
13 changes: 7 additions & 6 deletions e310x-hal/src/delay.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! # Delays

use crate::clock::Clocks;
use e310x::CLINT;
use e310x::Clint;
use embedded_hal::delay::DelayNs;
use riscv::register::mip;

Expand All @@ -22,7 +22,7 @@ impl DelayNs for Delay {
fn delay_ns(&mut self, ns: u32) {
let ticks = (ns as u64) * TICKS_PER_SECOND / 1_000_000_000;

let mtime = CLINT::mtimer().mtime;
let mtime = unsafe { Clint::steal() }.mtimer().mtime();
let t = mtime.read() + ticks;
while mtime.read() < t {}
}
Expand All @@ -45,12 +45,13 @@ impl Sleep {
impl DelayNs for Sleep {
fn delay_ns(&mut self, ns: u32) {
let ticks = (ns as u64) * u64::from(self.clock_freq) / 1_000_000_000;
let t = CLINT::mtimer().mtime.read() + ticks;
let clint = unsafe { e310x::Clint::steal() };
let t = clint.mtimer().mtime().read() + ticks;

CLINT::mtimecmp0().write(t);
clint.mtimecmp0().write(t);

// Enable timer interrupt
unsafe { CLINT::mtimer_enable() };
unsafe { clint.mtimer().enable() };

// Wait For Interrupt will put CPU to sleep until an interrupt hits
// in our case when internal timer mtime value >= mtimecmp value
Expand All @@ -66,6 +67,6 @@ impl DelayNs for Sleep {
}

// Clear timer interrupt
CLINT::mtimer_disable();
clint.mtimer().disable();
}
}
2 changes: 1 addition & 1 deletion e310x-hal/src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ impl From<Peripherals> for DeviceResources {
};

DeviceResources {
core_peripherals: CorePeripherals::new(),
core_peripherals: CorePeripherals::new(p.clint, p.plic),
peripherals,
pins: p.gpio0.into(),
}
Expand Down
2 changes: 1 addition & 1 deletion e310x/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

- The I2C0 code is now gated under the `g002` feature
- Regenerate code with `svd2rust` 0.36.1
- Use `riscv` v0.13.0 and `riscv-rt` v0.14.0
- Use `riscv` v0.14.0, `riscv-peripheral` v0.3.0, and `riscv-rt` v0.15.0
- In vectored mode, align `mtvec` to 64 bytes

## [v0.12.0] - 2024-12-10
Expand Down
8 changes: 4 additions & 4 deletions e310x/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ rust-version = "1.76"
edition = "2021"

[dependencies]
critical-section = { version = "1.2.0", optional = true }
riscv = "0.13.0"
riscv-peripheral = "0.2.0"
riscv-rt = { version = "0.14.0", features = ["no-interrupts"], optional = true }
critical-section = { workspace = true, optional = true }
riscv = { workspace = true }
riscv-peripheral = { workspace = true }
riscv-rt = { workspace = true, optional = true }
vcell = "0.1.3"

[features]
Expand Down
4 changes: 3 additions & 1 deletion e310x/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ fn main() {
.unwrap();
println!("cargo:rustc-link-search={}", out.display());
println!("cargo:rerun-if-changed=device.x");
println!("cargo:rustc-env=RISCV_MTVEC_ALIGN={}", 64usize);
println!("cargo:rustc-env=RISCV_RT_BASE_ISA=rv32i");
println!("cargo:rerun-if-env-changed=RISCV_RT_BASE_ISA");
println!("cargo:rustc-env=RISCV_MTVEC_ALIGN=64");
println!("cargo:rerun-if-env-changed=RISCV_MTVEC_ALIGN");
}
println!("cargo:rerun-if-changed=build.rs");
Expand Down
4 changes: 2 additions & 2 deletions e310x/settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ riscv_config:

clint:
name: "CLINT"
freq: 32768
async_delay: false
mtime_freq: 32768

plic:
name: "PLIC"
core_interrupt: "MachineExternal"
hart_id: "H0"

base_isa: "rv32i"
mtvec_align: 64
8 changes: 4 additions & 4 deletions e310x/src/aonclk/lfrosccfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,22 @@ impl R {
impl W {
#[doc = "Bits 0:5"]
#[inline(always)]
pub fn div(&mut self) -> DivW<LfrosccfgSpec> {
pub fn div(&mut self) -> DivW<'_, LfrosccfgSpec> {
DivW::new(self, 0)
}
#[doc = "Bits 16:20"]
#[inline(always)]
pub fn trim(&mut self) -> TrimW<LfrosccfgSpec> {
pub fn trim(&mut self) -> TrimW<'_, LfrosccfgSpec> {
TrimW::new(self, 16)
}
#[doc = "Bit 30"]
#[inline(always)]
pub fn enable(&mut self) -> EnableW<LfrosccfgSpec> {
pub fn enable(&mut self) -> EnableW<'_, LfrosccfgSpec> {
EnableW::new(self, 30)
}
#[doc = "Bit 31"]
#[inline(always)]
pub fn ready(&mut self) -> ReadyW<LfrosccfgSpec> {
pub fn ready(&mut self) -> ReadyW<'_, LfrosccfgSpec> {
ReadyW::new(self, 31)
}
}
Expand Down
Loading