Skip to content

Commit 4c72d57

Browse files
committed
Merge remote-tracking branch 'peripheral/main' into add-peripheral
2 parents c579937 + 51066b5 commit 4c72d57

File tree

24 files changed

+2487
-0
lines changed

24 files changed

+2487
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
on:
2+
push:
3+
branches: [ main ]
4+
pull_request:
5+
merge_group:
6+
7+
name: Lints compliance check
8+
9+
env:
10+
CLIPPY_PARAMS: -W clippy::all -W clippy::pedantic -W clippy::nursery -W clippy::cargo
11+
12+
jobs:
13+
clippy:
14+
strategy:
15+
matrix:
16+
toolchain: [ stable, nightly ]
17+
cargo_flags: [ --all-features, --no-default-features ]
18+
include:
19+
# Nightly is only for reference and allowed to fail
20+
- toolchain: nightly
21+
experimental: true
22+
# async traits are still not supported in stable
23+
- toolchain: stable
24+
cargo_flags: --all-features
25+
experimental: true
26+
runs-on: ubuntu-latest
27+
continue-on-error: ${{ matrix.experimental || false }}
28+
steps:
29+
- uses: actions/checkout@v3
30+
- uses: dtolnay/rust-toolchain@master
31+
with:
32+
toolchain: ${{ matrix.toolchain }}
33+
components: clippy
34+
- name: Run clippy
35+
run: cargo clippy --all ${{ matrix.cargo_flags }} -- -D warnings
36+
37+
# Job to check that all the lint checks succeeded
38+
clippy-check:
39+
needs:
40+
- clippy
41+
runs-on: ubuntu-latest
42+
if: always()
43+
steps:
44+
- run: jq --exit-status 'all(.result == "success")' <<< '${{ toJson(needs) }}'
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: Rust
2+
3+
on:
4+
push:
5+
branches: [ "main" ]
6+
pull_request:
7+
branches: [ "main" ]
8+
9+
env:
10+
CARGO_TERM_COLOR: always
11+
12+
jobs:
13+
build:
14+
15+
runs-on: ubuntu-latest
16+
17+
steps:
18+
- uses: actions/checkout@v3
19+
- name: Build
20+
run: cargo build --verbose
21+
- name: Run tests
22+
run: cargo test --verbose
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
on:
2+
push:
3+
branches: [ main ]
4+
pull_request:
5+
merge_group:
6+
7+
name: Code formatting check
8+
9+
jobs:
10+
rustfmt:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v3
14+
- uses: dtolnay/rust-toolchain@stable
15+
with:
16+
components: rustfmt
17+
- name: Run Rustfmt
18+
run: cargo fmt --all -- --check --verbose

riscv-peripheral/.gitignore

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Generated by Cargo
2+
# will have compiled files and executables
3+
debug/
4+
target/
5+
6+
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
7+
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
8+
Cargo.lock
9+
10+
# These are backup files generated by rustfmt
11+
**/*.rs.bk
12+
13+
# MSVC Windows builds of rustc generate these, which store debugging information
14+
*.pdb
15+
16+
.DS_Store
17+
.vscode/

riscv-peripheral/Cargo.toml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[package]
2+
name = "riscv-peripheral"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
9+
embedded-hal = "1.0.0-rc.2"
10+
# embedded-hal-async = { version = "1.0.0-rc.1", optional = true }
11+
riscv = { git = "https://github.com/rust-embedded/riscv", branch = "master" }
12+
13+
[features]
14+
# hal-async = ["embedded-hal-async"]
15+
16+
[package.metadata.docs.rs]
17+
default-target = "riscv64imac-unknown-none-elf"
18+
targets = [
19+
"riscv32i-unknown-none-elf", "riscv32imc-unknown-none-elf", "riscv32imac-unknown-none-elf",
20+
"riscv64imac-unknown-none-elf", "riscv64gc-unknown-none-elf",
21+
]

riscv-peripheral/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# `riscv-peripheral`
2+
3+
> Standard RISC-V peripherals for embedded systems written in Rust
4+
5+
## Minimum Supported Rust Version (MSRV)
6+
7+
This crate is guaranteed to compile on stable Rust 1.61 and up. It *might*
8+
compile with older versions but that may change in any new patch release.

riscv-peripheral/examples/e310x.rs

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
use riscv_peripheral::{
2+
aclint::HartIdNumber,
3+
plic::{ContextNumber, InterruptNumber, PriorityNumber},
4+
};
5+
6+
#[repr(u16)]
7+
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
8+
pub enum HartId {
9+
H0 = 0,
10+
}
11+
12+
unsafe impl HartIdNumber for HartId {
13+
const MAX_HART_ID_NUMBER: u16 = 0;
14+
15+
#[inline]
16+
fn number(self) -> u16 {
17+
self as _
18+
}
19+
20+
#[inline]
21+
fn from_number(number: u16) -> Result<Self, u16> {
22+
if number > Self::MAX_HART_ID_NUMBER {
23+
Err(number)
24+
} else {
25+
// SAFETY: valid context number
26+
Ok(unsafe { core::mem::transmute(number) })
27+
}
28+
}
29+
}
30+
31+
unsafe impl ContextNumber for HartId {
32+
const MAX_CONTEXT_NUMBER: u16 = 0;
33+
34+
#[inline]
35+
fn number(self) -> u16 {
36+
self as _
37+
}
38+
39+
#[inline]
40+
fn from_number(number: u16) -> Result<Self, u16> {
41+
if number > Self::MAX_CONTEXT_NUMBER {
42+
Err(number)
43+
} else {
44+
// SAFETY: valid context number
45+
Ok(unsafe { core::mem::transmute(number) })
46+
}
47+
}
48+
}
49+
50+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
51+
#[repr(u16)]
52+
pub enum Interrupt {
53+
WATCHDOG = 1,
54+
RTC = 2,
55+
UART0 = 3,
56+
UART1 = 4,
57+
QSPI0 = 5,
58+
QSPI1 = 6,
59+
QSPI2 = 7,
60+
GPIO0 = 8,
61+
GPIO1 = 9,
62+
GPIO2 = 10,
63+
GPIO3 = 11,
64+
GPIO4 = 12,
65+
GPIO5 = 13,
66+
GPIO6 = 14,
67+
GPIO7 = 15,
68+
GPIO8 = 16,
69+
GPIO9 = 17,
70+
GPIO10 = 18,
71+
GPIO11 = 19,
72+
GPIO12 = 20,
73+
GPIO13 = 21,
74+
GPIO14 = 22,
75+
GPIO15 = 23,
76+
GPIO16 = 24,
77+
GPIO17 = 25,
78+
GPIO18 = 26,
79+
GPIO19 = 27,
80+
GPIO20 = 28,
81+
GPIO21 = 29,
82+
GPIO22 = 30,
83+
GPIO23 = 31,
84+
GPIO24 = 32,
85+
GPIO25 = 33,
86+
GPIO26 = 34,
87+
GPIO27 = 35,
88+
GPIO28 = 36,
89+
GPIO29 = 37,
90+
GPIO30 = 38,
91+
GPIO31 = 39,
92+
PWM0CMP0 = 40,
93+
PWM0CMP1 = 41,
94+
PWM0CMP2 = 42,
95+
PWM0CMP3 = 43,
96+
PWM1CMP0 = 44,
97+
PWM1CMP1 = 45,
98+
PWM1CMP2 = 46,
99+
PWM1CMP3 = 47,
100+
PWM2CMP0 = 48,
101+
PWM2CMP1 = 49,
102+
PWM2CMP2 = 50,
103+
PWM2CMP3 = 51,
104+
I2C0 = 52,
105+
}
106+
107+
unsafe impl InterruptNumber for Interrupt {
108+
const MAX_INTERRUPT_NUMBER: u16 = 52;
109+
110+
#[inline]
111+
fn number(self) -> u16 {
112+
self as _
113+
}
114+
115+
#[inline]
116+
fn from_number(number: u16) -> Result<Self, u16> {
117+
if number == 0 || number > Self::MAX_INTERRUPT_NUMBER {
118+
Err(number)
119+
} else {
120+
// SAFETY: valid interrupt number
121+
Ok(unsafe { core::mem::transmute(number) })
122+
}
123+
}
124+
}
125+
126+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
127+
#[repr(u8)]
128+
pub enum Priority {
129+
P0 = 0,
130+
P1 = 1,
131+
P2 = 2,
132+
P3 = 3,
133+
P4 = 4,
134+
P5 = 5,
135+
P6 = 6,
136+
P7 = 7,
137+
}
138+
139+
unsafe impl PriorityNumber for Priority {
140+
const MAX_PRIORITY_NUMBER: u8 = 7;
141+
142+
#[inline]
143+
fn number(self) -> u8 {
144+
self as _
145+
}
146+
147+
#[inline]
148+
fn from_number(number: u8) -> Result<Self, u8> {
149+
if number > Self::MAX_PRIORITY_NUMBER {
150+
Err(number)
151+
} else {
152+
// SAFETY: valid priority number
153+
Ok(unsafe { core::mem::transmute(number) })
154+
}
155+
}
156+
}
157+
158+
riscv_peripheral::clint_codegen!(
159+
base 0x0200_0000,
160+
freq 32_768,
161+
mtimecmps [mtimecmp0=(HartId::H0,"`H0`")],
162+
msips [msip0=(HartId::H0,"`H0`")],
163+
);
164+
165+
fn main() {}

0 commit comments

Comments
 (0)