Skip to content
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

Add riscv-peripheral crate #164

Merged
merged 29 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
50c6460
Initial commit
romancardenas Aug 22, 2023
197f2fc
First version of PLIC and (A)CLINT
romancardenas Sep 6, 2023
42f5642
Macro for creating CLINT peripherals
romancardenas Sep 7, 2023
de55f4f
Improve CLINT codegen macro
romancardenas Sep 8, 2023
9067bad
PLIC macro
romancardenas Sep 8, 2023
1459e8f
Create rust.yml
romancardenas Sep 19, 2023
776aaac
Update macros.rs
romancardenas Sep 21, 2023
6d72036
modified macro for documentation
romancardenas Sep 22, 2023
7449010
Docs and CI actions
romancardenas Sep 22, 2023
1509de2
Fix clippy action
romancardenas Sep 22, 2023
90a4035
Added new functions
romancardenas Sep 23, 2023
13deb05
Utility functions now only in macros
romancardenas Sep 27, 2023
012aa79
HAL implementations
romancardenas Nov 3, 2023
147285e
concise implementation of e-h
romancardenas Nov 8, 2023
a74f311
Track DelayNs changes in embedded-hal-1.0.0-rc.2
agowatch Dec 5, 2023
205b197
Merge pull request #2 from purplethunder/embedded-hal-1.0.0-rc.2
romancardenas Dec 5, 2023
51066b5
prepare for riscv
romancardenas Dec 6, 2023
4c72d57
Merge remote-tracking branch 'peripheral/main' into add-peripheral
romancardenas Dec 6, 2023
36a264c
riscv-peripheral crate
romancardenas Dec 7, 2023
82de921
store waker
romancardenas Dec 14, 2023
552063c
Interrupt-driven async HAL implementation
romancardenas Dec 15, 2023
5b012c0
more flexible async implementation
romancardenas Dec 19, 2023
b2183c6
Merge branch 'master' into add-peripheral
romancardenas Jan 10, 2024
e29948c
use e-h-1
romancardenas Jan 10, 2024
89a632f
Merge branch 'master' into add-peripheral
romancardenas Jan 15, 2024
2531e68
MSRV is now 1.75
romancardenas Jan 15, 2024
f4e29c4
async stuff (WIP)
romancardenas Jan 15, 2024
e9cd65d
added example for async delays with CLINT
romancardenas Jan 15, 2024
358a152
tweak CI
romancardenas Feb 13, 2024
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
10 changes: 10 additions & 0 deletions .github/workflows/changelog.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ jobs:
- 'riscv-rt/**'
riscv-pac:
- 'riscv-pac/**'
riscv-peripheral:
- 'riscv-peripheral/**'

- name: Check for CHANGELOG.md (riscv)
if: steps.changes.outputs.riscv == 'true'
Expand All @@ -47,3 +49,11 @@ jobs:
changeLogPath: ./riscv-pac/CHANGELOG.md
skipLabels: 'skip changelog'
missingUpdateErrorMessage: 'Please add a changelog entry in the riscv-pac/CHANGELOG.md file.'

- name: Check for CHANGELOG.md (riscv-peripheral)
if: steps.changes.outputs.riscv-peripheral == 'true'
uses: dangoslen/changelog-enforcer@v3
with:
changeLogPath: ./riscv-peripheral/CHANGELOG.md
skipLabels: 'skip changelog'
missingUpdateErrorMessage: 'Please add a changelog entry in the riscv-peripheral/CHANGELOG.md file.'
61 changes: 61 additions & 0 deletions .github/workflows/riscv-peripheral.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
on:
push:
branches: [ master ]
pull_request:
merge_group:

name: Build check (riscv-peripheral)

jobs:
# We check that the crate builds and links for all the toolchains and targets.
build-riscv:
strategy:
matrix:
# All generated code should be running on stable now, MRSV is 1.61.0
toolchain: [ stable, nightly, 1.61.0 ]
target:
- riscv32i-unknown-none-elf
- riscv32imc-unknown-none-elf
- riscv32imac-unknown-none-elf
- riscv64imac-unknown-none-elf
- riscv64gc-unknown-none-elf
include:
# Nightly is only for reference and allowed to fail
- toolchain: nightly
experimental: true
runs-on: ubuntu-latest
continue-on-error: ${{ matrix.experimental || false }}
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{ matrix.toolchain }}
targets: ${{ matrix.target }}
- name: Build (no features)
run: cargo build --package riscv-peripheral --target ${{ matrix.target }}
- name: Build (all features)
run: cargo build --package riscv-peripheral --target ${{ matrix.target }} --all-features

# On MacOS, Ubuntu, and Windows, we run the tests.
build-others:
strategy:
matrix:
os: [ macos-latest, ubuntu-latest, windows-latest ]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@stable
- name: Build (no features)
run: cargo test --package riscv-peripheral
- name: Build (all features)
run: cargo test --package riscv-peripheral --all-features

# Job to check that all the builds succeeded
build-check:
needs:
- build-riscv
- build-others
runs-on: ubuntu-latest
if: always()
steps:
- run: jq --exit-status 'all(.result == "success")' <<< '${{ toJson(needs) }}'
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ resolver = "2"
members = [
"riscv",
"riscv-pac",
"riscv-peripheral",
"riscv-rt",
]
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ This repository contains various crates useful for writing Rust programs on RISC

* [`riscv`]: CPU registers access and intrinsics
* [`riscv-pac`]: Common traits to be implemented by RISC-V PACs
* [`riscv-peripheral`]: Interfaces for standard RISC-V peripherals
* [`riscv-rt`]: Startup code and interrupt handling


Expand All @@ -22,6 +23,8 @@ Conduct][CoC], the maintainer of this crate, the [RISC-V team][team], promises
to intervene to uphold that code of conduct.

[`riscv`]: https://crates.io/crates/riscv
[`riscv-pac`]: https://crates.io/crates/riscv-pac
[`riscv-peripheral`]: https://crates.io/crates/riscv-peripheral
[`riscv-rt`]: https://crates.io/crates/riscv-rt
[team]: https://github.com/rust-embedded/wg#the-risc-v-team
[CoC]: CODE_OF_CONDUCT.md
4 changes: 4 additions & 0 deletions riscv-pac/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Added

- Add `InterruptNumber`, `PriorityNumber`, and `HartIdNumber` traits.

### Changed

- Update `README.md`
6 changes: 3 additions & 3 deletions riscv-pac/README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
[![crates.io](https://img.shields.io/crates/d/riscv.svg)](https://crates.io/crates/riscv)
[![crates.io](https://img.shields.io/crates/v/riscv.svg)](https://crates.io/crates/riscv)
[![crates.io](https://img.shields.io/crates/d/riscv-pac.svg)](https://crates.io/crates/riscv-pac)
[![crates.io](https://img.shields.io/crates/v/riscv-pac.svg)](https://crates.io/crates/riscv-pac)

# `riscv-pac`

> Target-specific traits to be implemented by PACs

This project is developed and maintained by the [RISC-V team][team].

## [Documentation](https://docs.rs/crate/riscv)
## [Documentation](https://docs.rs/crate/riscv-pac)

## Minimum Supported Rust Version (MSRV)

Expand Down
12 changes: 12 additions & 0 deletions riscv-peripheral/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Change Log

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

### Added

- Add `ACLINT`, `CLINT`, and `PLIC` structs
22 changes: 22 additions & 0 deletions riscv-peripheral/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[package]
name = "riscv-peripheral"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
embedded-hal = "1.0.0-rc.2"
# embedded-hal-async = { version = "1.0.0-rc.1", optional = true }
riscv = { path = "../riscv", version = "0.10" }
riscv-pac = { path = "../riscv-pac", version = "0.1.0" }

[features]
# hal-async = ["embedded-hal-async"]

[package.metadata.docs.rs]
default-target = "riscv64imac-unknown-none-elf"
targets = [
"riscv32i-unknown-none-elf", "riscv32imc-unknown-none-elf", "riscv32imac-unknown-none-elf",
"riscv64imac-unknown-none-elf", "riscv64gc-unknown-none-elf",
]
40 changes: 40 additions & 0 deletions riscv-peripheral/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
[![crates.io](https://img.shields.io/crates/d/riscv-peripheral.svg)](https://crates.io/crates/riscv-peripheral)
[![crates.io](https://img.shields.io/crates/v/riscv-peripheral.svg)](https://crates.io/crates/riscv-peripheral)

# `riscv-peripheral`

> Interfaces for standard RISC-V peripherals

This project is developed and maintained by the [RISC-V team][team].

## [Documentation](https://docs.rs/crate/riscv-peripheral)

## Minimum Supported Rust Version (MSRV)

This crate is guaranteed to compile on stable Rust 1.61 and up. It *might*
compile with older versions but that may change in any new patch release.

## License

Copyright 2023-2024s [RISC-V team][team]

Permission to use, copy, modify, and/or distribute this software for any purpose
with or without fee is hereby granted, provided that the above copyright notice
and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
THIS SOFTWARE.

## Code of Conduct

Contribution to this crate is organized under the terms of the [Rust Code of
Conduct][CoC], the maintainer of this crate, the [RISC-V team][team], promises
to intervene to uphold that code of conduct.

[CoC]: CODE_OF_CONDUCT.md
[team]: https://github.com/rust-embedded/wg#the-risc-v-team
152 changes: 152 additions & 0 deletions riscv-peripheral/examples/e310x.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
//! Peripheral definitions for the E310x chip.
//! This is a simple example of how to use the `riscv-peripheral` crate to generate
//! peripheral definitions for a target.

use riscv_pac::{HartIdNumber, InterruptNumber, PriorityNumber};

#[repr(u16)]
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum HartId {
H0 = 0,
}

unsafe impl HartIdNumber for HartId {
const MAX_HART_ID_NUMBER: u16 = 0;

#[inline]
fn number(self) -> u16 {
self as _
}

#[inline]
fn from_number(number: u16) -> Result<Self, u16> {
if number > Self::MAX_HART_ID_NUMBER {
Err(number)
} else {
// SAFETY: valid context number
Ok(unsafe { core::mem::transmute(number) })
}
}
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[repr(u16)]
pub enum Interrupt {
WATCHDOG = 1,
RTC = 2,
UART0 = 3,
UART1 = 4,
QSPI0 = 5,
QSPI1 = 6,
QSPI2 = 7,
GPIO0 = 8,
GPIO1 = 9,
GPIO2 = 10,
GPIO3 = 11,
GPIO4 = 12,
GPIO5 = 13,
GPIO6 = 14,
GPIO7 = 15,
GPIO8 = 16,
GPIO9 = 17,
GPIO10 = 18,
GPIO11 = 19,
GPIO12 = 20,
GPIO13 = 21,
GPIO14 = 22,
GPIO15 = 23,
GPIO16 = 24,
GPIO17 = 25,
GPIO18 = 26,
GPIO19 = 27,
GPIO20 = 28,
GPIO21 = 29,
GPIO22 = 30,
GPIO23 = 31,
GPIO24 = 32,
GPIO25 = 33,
GPIO26 = 34,
GPIO27 = 35,
GPIO28 = 36,
GPIO29 = 37,
GPIO30 = 38,
GPIO31 = 39,
PWM0CMP0 = 40,
PWM0CMP1 = 41,
PWM0CMP2 = 42,
PWM0CMP3 = 43,
PWM1CMP0 = 44,
PWM1CMP1 = 45,
PWM1CMP2 = 46,
PWM1CMP3 = 47,
PWM2CMP0 = 48,
PWM2CMP1 = 49,
PWM2CMP2 = 50,
PWM2CMP3 = 51,
I2C0 = 52,
}

unsafe impl InterruptNumber for Interrupt {
const MAX_INTERRUPT_NUMBER: u16 = 52;

#[inline]
fn number(self) -> u16 {
self as _
}

#[inline]
fn from_number(number: u16) -> Result<Self, u16> {
if number == 0 || number > Self::MAX_INTERRUPT_NUMBER {
Err(number)
} else {
// SAFETY: valid interrupt number
Ok(unsafe { core::mem::transmute(number) })
}
}
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[repr(u8)]
pub enum Priority {
P0 = 0,
P1 = 1,
P2 = 2,
P3 = 3,
P4 = 4,
P5 = 5,
P6 = 6,
P7 = 7,
}

unsafe impl PriorityNumber for Priority {
const MAX_PRIORITY_NUMBER: u8 = 7;

#[inline]
fn number(self) -> u8 {
self as _
}

#[inline]
fn from_number(number: u8) -> Result<Self, u8> {
if number > Self::MAX_PRIORITY_NUMBER {
Err(number)
} else {
// SAFETY: valid priority number
Ok(unsafe { core::mem::transmute(number) })
}
}
}

riscv_peripheral::clint_codegen!(
base 0x0200_0000,
freq 32_768,
mtimecmps [mtimecmp0=(HartId::H0,"`H0`")],
msips [msip0=(HartId::H0,"`H0`")],
);

riscv_peripheral::plic_codegen!(
base 0x0C00_0000,
ctxs [ctx0=(HartId::H0,"`H0`")],
);

fn main() {}
Loading