Skip to content

Commit 459486a

Browse files
committed
update to new HAL
1 parent 615c802 commit 459486a

File tree

14 files changed

+220
-182
lines changed

14 files changed

+220
-182
lines changed

.cargo/config

-8
This file was deleted.

.cargo/config.toml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[target.'cfg(all(target_arch = "riscv32", target_os = "none"))']
2+
runner = "qemu-system-riscv32 -machine sifive_e,revb=true -nographic -kernel"
3+
# runner = "riscv64-unknown-elf-gdb -q -x gdb_init"
4+
rustflags = [
5+
"-C", "link-arg=-Thifive1-link.x",
6+
]
7+
8+
[build]
9+
target = "riscv32imac-unknown-none-elf"

.vscode/settings.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
{
2-
"cortex-debug.variableUseNaturalFormat": true
3-
}
2+
"cortex-debug.variableUseNaturalFormat": true,
3+
"rust-analyzer.cargo.features": ["board-redv"],
4+
"rust-analyzer.check.allTargets": false,
5+
}

Cargo.toml

+4-8
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ categories = ["embedded", "hardware-support", "no-std"]
77
description = "Board support crate for HiFive1 and LoFive boards"
88
keywords = ["riscv", "register", "peripheral"]
99
license = "ISC"
10-
edition = "2018"
10+
edition = "2021"
1111

1212
[dependencies]
1313
e310x-hal = {git = "https://github.com/greenlsi/e310x-hal"}
1414
embedded-hal = "0.2.5"
15-
riscv = "0.6.0"
16-
riscv-rt = "0.11.0"
15+
riscv = { git = "https://github.com/rust-embedded/riscv", branch = "riscv-pac-only", features = ["critical-section-single-hart"]} # TODO use crates.io
16+
riscv-rt = { git = "https://github.com/rust-embedded/riscv", branch = "riscv-pac-only" } # TODO use crates.io
1717
nb = "1.0.0"
1818
panic-halt = "0.2.0"
1919

@@ -23,11 +23,7 @@ board-hifive1-revb = ["e310x-hal/g002"]
2323
board-redv = ["e310x-hal/g002"]
2424
board-lofive = []
2525
board-lofive-r1 = ["e310x-hal/g002"]
26-
v-extern = ["e310x-hal/v-extern"]
27-
28-
[[example]]
29-
name = "virq"
30-
required-features = ["v-extern"]
26+
v-trap = ["e310x-hal/v-trap"]
3127

3228
[package.metadata.docs.rs]
3329
features = ['board-hifive1-revb']

assemble.sh

-11
This file was deleted.

bin/flash.a

-2.17 KB
Binary file not shown.

build.rs

-6
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,4 @@ fn main() {
4444

4545
fs::copy("hifive1-link.x", out_dir.join("hifive1-link.x")).unwrap();
4646
println!("cargo:rerun-if-changed=hifive1-link.x");
47-
48-
// Copy library with flash setup code
49-
let name = env::var("CARGO_PKG_NAME").unwrap();
50-
fs::copy("bin/flash.a", out_dir.join(format!("lib{}.a", name))).unwrap();
51-
println!("cargo:rustc-link-lib=static={}", name);
52-
println!("cargo:rerun-if-changed=bin/flash.a");
5347
}

examples/gpio4.rs

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
//! Demonstration on how to configure the GPIO4 interrupt on HiFive boards.
2+
//! You must connect a button to pin 12 (GPIO4) and ground to test this example.
3+
4+
#![no_main]
5+
#![no_std]
6+
7+
extern crate panic_halt;
8+
9+
use hifive1::hal::e310x::PLIC;
10+
use hifive1::{hal::prelude::*, hal::DeviceResources, pin, sprintln};
11+
12+
use riscv::register::mstatus;
13+
use riscv_rt::entry;
14+
15+
/* Handler for the GPIO0 interrupt */
16+
#[riscv_rt::external_interrupt(ExternalInterrupt::GPIO4)]
17+
fn gpio4_handler() {
18+
sprintln!("We reached the GPIO4 interrupt!");
19+
/* Clear the GPIO pending interrupt */
20+
let gpio_block = unsafe { hifive1::hal::e310x::Gpio0::steal() };
21+
gpio_block.fall_ip().write(|w| w.pin4().set_bit());
22+
}
23+
24+
/* Code adapted from https://github.com/riscv-rust/riscv-rust-quickstart/blob/interrupt-test/examples/interrupt.rs*/
25+
#[entry]
26+
fn main() -> ! {
27+
/* Get the ownership of the device resources singleton */
28+
let resources = DeviceResources::take().unwrap();
29+
let peripherals = resources.peripherals;
30+
31+
/* Configure system clock */
32+
let sysclock = hifive1::configure_clocks(peripherals.PRCI, peripherals.AONCLK, 64.mhz().into());
33+
/* Get the board pins */
34+
let gpio = resources.pins;
35+
36+
/* Configure stdout for debugging */
37+
hifive1::stdout::configure(
38+
peripherals.UART0,
39+
pin!(gpio, uart0_tx),
40+
pin!(gpio, uart0_rx),
41+
115_200.bps(),
42+
sysclock,
43+
);
44+
45+
sprintln!("Configuring GPIO...");
46+
/* Set GPIO4 (pin 12) as input */
47+
// let gpio4 = pin!(gpio, dig12);
48+
gpio.pin4.into_pull_up_input();
49+
//let input = gpio4.into_pull_up_input();
50+
51+
sprintln!("Configuring priorities...");
52+
/* Set interrupt source priority */
53+
let priorities = PLIC::priorities();
54+
unsafe { priorities.set_priority(ExternalInterrupt::GPIO4, Priority::P7) };
55+
56+
let gpio_block = unsafe { hifive1::hal::e310x::Gpio0::steal() };
57+
unsafe {
58+
/* Clear pending interrupts from previous states */
59+
gpio_block.fall_ie().write(|w| w.bits(0x00000000));
60+
gpio_block.rise_ie().write(|w| w.bits(0x00000000));
61+
gpio_block.fall_ip().write(|w| w.bits(0xffffffff));
62+
gpio_block.rise_ip().write(|w| w.bits(0xffffffff));
63+
}
64+
gpio_block.fall_ie().write(|w| w.pin4().set_bit());
65+
gpio_block.rise_ie().write(|w| w.pin4().clear_bit());
66+
67+
/* Activate global interrupts (mie bit) */
68+
let ctx = PLIC::ctx0();
69+
unsafe {
70+
ctx.threshold().set_threshold(Priority::P1);
71+
ctx.enables().enable(ExternalInterrupt::GPIO4);
72+
mstatus::set_mie();
73+
PLIC::enable();
74+
}
75+
loop {
76+
riscv::asm::wfi();
77+
}
78+
}

examples/mtimer.rs

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//! This example demonstrates how to configure the CLINT to generate
2+
//! periodic interrupts using the machine timer.
3+
4+
#![no_main]
5+
#![no_std]
6+
7+
extern crate panic_halt;
8+
9+
use hifive1::{
10+
configure_clocks,
11+
hal::{e310x::CLINT, prelude::*, DeviceResources},
12+
pin, sprintln,
13+
};
14+
15+
const PERIOD_MS: u64 = 1000;
16+
const FREQUENCY_HZ: u64 = 32768;
17+
const CLINT_TICKS_PER_MS: u64 = PERIOD_MS * FREQUENCY_HZ / 1000;
18+
19+
/// Handler for the machine timer interrupt (handled by the CLINT)
20+
#[riscv_rt::core_interrupt(CoreInterrupt::MachineTimer)]
21+
fn mtimer_handler() {
22+
sprintln!("MTIMER interrupt!");
23+
CLINT::mtimecmp0().modify(|f| *f += CLINT_TICKS_PER_MS);
24+
}
25+
26+
#[riscv_rt::entry]
27+
fn main() -> ! {
28+
/* Get the ownership of the device resources singleton */
29+
let resources = DeviceResources::take().unwrap();
30+
let peripherals = resources.peripherals;
31+
32+
/* Configure system clock */
33+
let sysclock = configure_clocks(peripherals.PRCI, peripherals.AONCLK, 64.mhz().into());
34+
35+
/* Configure stdout for printing via UART */
36+
let gpio = resources.pins;
37+
hifive1::stdout::configure(
38+
peripherals.UART0,
39+
pin!(gpio, uart0_tx),
40+
pin!(gpio, uart0_rx),
41+
115_200.bps(),
42+
sysclock,
43+
);
44+
45+
sprintln!("Configuring CLINT...");
46+
CLINT::mtimer_disable();
47+
let mtimer = CLINT::mtimer();
48+
let (mtimecmp, mtime) = (mtimer.mtimecmp0, mtimer.mtime);
49+
mtime.write(0);
50+
mtimecmp.write(CLINT_TICKS_PER_MS);
51+
52+
sprintln!("Enabling interrupts...");
53+
unsafe {
54+
riscv::interrupt::enable();
55+
CLINT::mtimer_enable();
56+
}
57+
loop {
58+
sprintln!("Sleeping...");
59+
riscv::asm::wfi();
60+
}
61+
}

examples/virq.rs

-89
This file was deleted.

flash.S

-45
This file was deleted.

src/clock.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use e310x_hal::{
44
clock::{AonExt, Clocks, PrciExt},
5-
e310x::{AONCLK, PRCI},
5+
e310x::{Aonclk, Prci},
66
time::Hertz,
77
};
88

@@ -15,7 +15,7 @@ use e310x_hal::{
1515
///
1616
/// For HiFive1 and HiFive1 Rev B boards external oscillators are enabled for
1717
/// both high-frequency and low-frequency clocks.
18-
pub fn configure(prci: PRCI, aonclk: AONCLK, target_coreclk: Hertz) -> Clocks {
18+
pub fn configure(prci: Prci, aonclk: Aonclk, target_coreclk: Hertz) -> Clocks {
1919
let coreclk = prci.constrain();
2020
let coreclk = coreclk
2121
.use_external(Hertz(16_000_000))
@@ -32,7 +32,7 @@ pub fn configure(prci: PRCI, aonclk: AONCLK, target_coreclk: Hertz) -> Clocks {
3232
///
3333
/// For the LoFive and LoFive R1 boards, external oscillator is enabled for
3434
/// high-frequency clock. For low-frequency clock internal oscillator is used.
35-
pub fn configure(prci: PRCI, aonclk: AONCLK, target_coreclk: Hertz) -> Clocks {
35+
pub fn configure(prci: Prci, aonclk: Aonclk, target_coreclk: Hertz) -> Clocks {
3636
let coreclk = prci.constrain();
3737
let coreclk = coreclk
3838
.use_external(Hertz(16_000_000))

0 commit comments

Comments
 (0)