Skip to content

Commit 8e91d47

Browse files
committed
Initial commit.
0 parents  commit 8e91d47

23 files changed

+523
-0
lines changed

.cargo/config

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[target.riscv32imac-unknown-none]
2+
runner = 'riscv32-unknown-elf-gdb'
3+
rustflags = [
4+
"-C", "link-arg=-Tmemory.x",
5+
"-C", "link-arg=-Tlink.x",
6+
"-C", "linker=riscv32-unknown-elf-ld",
7+
]

.gdbinit

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
set remotetimeout 240
2+
target extended-remote localhost:3333
3+
4+
define upload
5+
monitor reset halt
6+
monitor flash protect 0 64 last off
7+
load
8+
monitor flash protect 0 64 last on
9+
continue
10+
end
11+
document upload
12+
Upload program to hifive board
13+
end
14+
15+
# Load Rust's GDB pretty printers
16+
python
17+
import os;
18+
import sys;
19+
path = os.environ['TOOLCHAIN'] + '/lib/rustlib/etc'
20+
sys.path.append(path)
21+
22+
gdb.execute('directory %s' % path)
23+
gdb.execute('add-auto-load-safe-path %s' % path)
24+
end

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
/target
3+
**/*.rs.bk

.gitmodules

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[submodule "hifive"]
2+
path = hifive
3+
url = https://github.com/dvc94ch/hifive
4+
[submodule "riscv-rt"]
5+
path = riscv-rt
6+
url = https://github.com/dvc94ch/riscv-rt
7+
[submodule "riscv"]
8+
path = riscv
9+
url = https://github.com/dvc94ch/riscv
10+
[submodule "e310x"]
11+
path = e310x
12+
url = https://github.com/dvc94ch/e310x

Cargo.lock

Lines changed: 89 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[package]
2+
name = "riscv-crates"
3+
version = "0.1.0"
4+
authors = ["David Craven <[email protected]>"]
5+
6+
[workspace]
7+
8+
[dependencies]
9+
embedded-hal = { git = "https://github.com/japaric/embedded-hal", rev = "7d904f515d15fd5fe7ea34e18820ea83e2651fa2" }
10+
nb = { git = "https://github.com/japaric/nb" }
11+
hifive = { path = "hifive" }
12+
13+
[profile.release]
14+
debug = true
15+
lto = true

Makefile

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Examples (uncomment one)
2+
#EXAMPLE := blinky_delay
3+
#EXAMPLE := blinky_clint
4+
#EXAMPLE := blinky_pwm
5+
#EXAMPLE := blinky_plic
6+
#EXAMPLE := hello_world
7+
#EXAMPLE := panicking
8+
#EXAMPLE := pll
9+
10+
# Board crate (uncomment one)
11+
BOARD := hifive
12+
13+
TARGET := riscv32imac-unknown-none
14+
TARGET_DIR := $(abspath ./target/$(TARGET)/debug)
15+
EXAMPLE_DIR := $(TARGET_DIR)/examples
16+
EXAMPLE_BIN := $(EXAMPLE_DIR)/$(EXAMPLE)
17+
OPENOCD_CFG := openocd.cfg
18+
19+
build:
20+
xargo build --examples --target $(TARGET) $(ARGS)
21+
22+
test:
23+
xargo test --all --target $(TARGET) $(ARGS)
24+
25+
clean:
26+
xargo clean $(ARGS)
27+
28+
readelf:
29+
llvm-readelf -a -h -s -r -symbols $(EXAMPLE_BIN) $(ARGS)
30+
31+
objdump:
32+
llvm-objdump -d -S $(EXAMPLE_BIN) $(ARGS)
33+
34+
size:
35+
llvm-size $(EXAMPLE_BIN) $(ARGS)
36+
37+
# .gdbinit adds a upload command to gdb
38+
gdb:
39+
riscv32-unknown-elf-gdb $(EXAMPLE_BIN) $(ARGS)
40+
41+
openocd:
42+
openocd -f $(OPENOCD_CFG) $(ARGS)
43+
44+
upload:
45+
openocd -f $(OPENOCD_CFG) \
46+
-c "flash protect 0 64 last off; program ${EXAMPLE_BIN}; resume 0x20400000; exit"
47+
48+
framedump:
49+
riscv32-unknown-elf-readelf --debug-dump=frames $(EXAMPLE_BIN) $(ARGS)
50+
51+
.PHONY: build clean readelf objdump framedump size gdb openocd spike

Xargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[dependencies]
2+
core = {}
3+
alloc = {}
4+
5+
[dependencies.compiler_builtins]
6+
features = ["mem"]
7+
stage = 1

e310x

Submodule e310x added at fbc8a9a

env.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export RISCV_RUST_TOOLCHAIN=~/repos/riscv-rust
2+
export TOOLCHAIN=$RISCV_RUST_TOOLCHAIN/toolchain
3+
4+
export PATH=/opt/rust/bin:/opt/riscv/bin:~/.cargo/bin:$PATH
5+
export XARGO_RUST_SRC=~/repos/rust/src
6+
export RUST_TARGET_PATH=$PWD

examples/blinky_clint.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#![no_std]
2+
3+
extern crate hifive;
4+
5+
use hifive::prelude::*;
6+
use hifive::{interrupt, led, Blue, Clint, Peripherals, UExt};
7+
8+
fn main() {
9+
let peripherals = hifive::init(115_200);
10+
led::init(peripherals.GPIO0);
11+
12+
let timer = Clint(peripherals.CLINT);
13+
timer.set_timeout(1.s());
14+
15+
unsafe {
16+
interrupt::enable();
17+
}
18+
}
19+
20+
#[no_mangle]
21+
pub fn mtimer_trap_handler(p: &Peripherals) {
22+
Clint(p.CLINT).restart();
23+
Blue::toggle(p.GPIO0);
24+
}

examples/blinky_delay.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#![no_std]
2+
3+
#[macro_use]
4+
extern crate nb;
5+
extern crate hifive;
6+
7+
use hifive::prelude::*;
8+
use hifive::{led, Red, Green, Blue, Clint, UExt};
9+
10+
fn delay(clint: &Clint) {
11+
block!(clint.wait()).unwrap();
12+
clint.restart();
13+
}
14+
15+
fn main() {
16+
let peripherals = hifive::init(115_200);
17+
led::init(peripherals.GPIO0);
18+
19+
let clint = Clint(peripherals.CLINT);
20+
clint.set_timeout(500.ms());
21+
22+
let gpio = peripherals.GPIO0;
23+
loop {
24+
Red::on(gpio);
25+
delay(&clint);
26+
Red::off(gpio);
27+
Green::on(gpio);
28+
delay(&clint);
29+
Green::off(gpio);
30+
Blue::on(gpio);
31+
delay(&clint);
32+
Blue::off(gpio);
33+
}
34+
}

examples/blinky_plic.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#![no_std]
2+
3+
extern crate hifive;
4+
5+
use core::fmt::Write;
6+
use hifive::*;
7+
use hifive::prelude::*;
8+
use hifive::interrupt::Nr;
9+
10+
fn main() {
11+
let p = hifive::init(115_200);
12+
led::init(p.GPIO0);
13+
14+
Red::on(p.GPIO0);
15+
16+
let plic = Plic(p.PLIC);
17+
plic.init();
18+
19+
RtcConf::new().end(p.RTC);
20+
Rtc(p.RTC).set_timeout(500.ms());
21+
22+
plic.set_priority(Interrupt::RTC, Priority::P7);
23+
plic.enable(Interrupt::RTC);
24+
25+
let serial = Serial(p.UART0);
26+
let mut stdout = Port(&serial);
27+
writeln!(stdout, "External interrupts enabled: {}",
28+
csr::mie.read().mext()).unwrap();
29+
let threshold: u32 = plic.get_threshold().into();
30+
writeln!(stdout, "PLIC threshold priority: {}",
31+
threshold).unwrap();
32+
writeln!(stdout, "RTC interrupt number: {}",
33+
Interrupt::RTC.nr()).unwrap();
34+
writeln!(stdout, "RTC interrupt enabled: {}",
35+
plic.is_enabled(Interrupt::RTC)).unwrap();
36+
let priority: u32 = plic.get_priority(Interrupt::RTC).into();
37+
writeln!(stdout, "RTC interrupt priority: {}",
38+
priority).unwrap();
39+
40+
unsafe {
41+
interrupt::enable();
42+
}
43+
}
44+
45+
#[no_mangle]
46+
pub fn plic_trap_handler(p: &Peripherals, intr: &Interrupt) {
47+
match *intr {
48+
Interrupt::RTC => {
49+
Rtc(p.RTC).restart();
50+
Blue::toggle(p.GPIO0);
51+
},
52+
_ => {},
53+
}
54+
}

0 commit comments

Comments
 (0)