Skip to content

Commit c579937

Browse files
authored
Merge pull request #162 from rust-embedded/riscv-pac
riscv-pac crate
2 parents eb02f31 + fd0ecf0 commit c579937

File tree

7 files changed

+163
-1
lines changed

7 files changed

+163
-1
lines changed

.github/workflows/changelog.yaml

+10
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ jobs:
2121
- 'riscv/**'
2222
riscv-rt:
2323
- 'riscv-rt/**'
24+
riscv-pac:
25+
- 'riscv-pac/**'
2426
2527
- name: Check for CHANGELOG.md (riscv)
2628
if: steps.changes.outputs.riscv == 'true'
@@ -37,3 +39,11 @@ jobs:
3739
changeLogPath: ./riscv-rt/CHANGELOG.md
3840
skipLabels: 'skip changelog'
3941
missingUpdateErrorMessage: 'Please add a changelog entry in the riscv-rt/CHANGELOG.md file.'
42+
43+
- name: Check for CHANGELOG.md (riscv-pac)
44+
if: steps.changes.outputs.riscv-pac == 'true'
45+
uses: dangoslen/changelog-enforcer@v3
46+
with:
47+
changeLogPath: ./riscv-pac/CHANGELOG.md
48+
skipLabels: 'skip changelog'
49+
missingUpdateErrorMessage: 'Please add a changelog entry in the riscv-pac/CHANGELOG.md file.'

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
resolver = "2"
33
members = [
44
"riscv",
5+
"riscv-pac",
56
"riscv-rt",
67
]

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
This repository contains various crates useful for writing Rust programs on RISC-V microcontrollers:
44

5-
* [`riscv`]: CPU peripheral access and intrinsics
5+
* [`riscv`]: CPU registers access and intrinsics
6+
* [`riscv-pac`]: Common traits to be implemented by RISC-V PACs
67
* [`riscv-rt`]: Startup code and interrupt handling
78

89

riscv-pac/CHANGELOG.md

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Change Log
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
The format is based on [Keep a Changelog](http://keepachangelog.com/)
6+
and this project adheres to [Semantic Versioning](http://semver.org/).
7+
8+
## [Unreleased]
9+
10+
### Added
11+
12+
- Add `InterruptNumber`, `PriorityNumber`, and `HartIdNumber` traits.

riscv-pac/Cargo.toml

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[package]
2+
name = "riscv-pac"
3+
version = "0.1.0"
4+
edition = "2021"
5+
rust-version = "1.60"
6+
repository = "https://github.com/rust-embedded/riscv"
7+
authors = ["The RISC-V Team <[email protected]>"]
8+
categories = ["embedded", "hardware-support", "no-std"]
9+
description = "Low level access to RISC-V processors"
10+
documentation = "https://docs.rs/riscv-pac"
11+
keywords = ["riscv", "register", "peripheral"]
12+
license = "ISC"
13+
14+
[package.metadata.docs.rs]
15+
default-target = "riscv64imac-unknown-none-elf"
16+
targets = [
17+
"riscv32i-unknown-none-elf", "riscv32imc-unknown-none-elf", "riscv32imac-unknown-none-elf",
18+
"riscv64imac-unknown-none-elf", "riscv64gc-unknown-none-elf",
19+
]

riscv-pac/README.md

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
[![crates.io](https://img.shields.io/crates/d/riscv.svg)](https://crates.io/crates/riscv)
2+
[![crates.io](https://img.shields.io/crates/v/riscv.svg)](https://crates.io/crates/riscv)
3+
4+
# `riscv-pac`
5+
6+
> Target-specific traits to be implemented by PACs
7+
8+
This project is developed and maintained by the [RISC-V team][team].
9+
10+
## [Documentation](https://docs.rs/crate/riscv)
11+
12+
## Minimum Supported Rust Version (MSRV)
13+
14+
This crate is guaranteed to compile on stable Rust 1.60 and up. It *might*
15+
compile with older versions but that may change in any new patch release.
16+
17+
## License
18+
19+
Copyright 2023-2024s [RISC-V team][team]
20+
21+
Permission to use, copy, modify, and/or distribute this software for any purpose
22+
with or without fee is hereby granted, provided that the above copyright notice
23+
and this permission notice appear in all copies.
24+
25+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
26+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
27+
FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
28+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
29+
OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
30+
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
31+
THIS SOFTWARE.
32+
33+
## Code of Conduct
34+
35+
Contribution to this crate is organized under the terms of the [Rust Code of
36+
Conduct][CoC], the maintainer of this crate, the [RISC-V team][team], promises
37+
to intervene to uphold that code of conduct.
38+
39+
[CoC]: CODE_OF_CONDUCT.md
40+
[team]: https://github.com/rust-embedded/wg#the-risc-v-team

riscv-pac/src/lib.rs

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#![no_std]
2+
3+
/// Trait for enums of target-specific external interrupt numbers.
4+
///
5+
/// This trait should be implemented by a peripheral access crate (PAC)
6+
/// on its enum of available external interrupts for a specific device.
7+
/// Each variant must convert to a `u16` of its interrupt number.
8+
///
9+
/// # Safety
10+
///
11+
/// * This trait must only be implemented on a PAC of a RISC-V target.
12+
/// * This trait must only be implemented on enums of external interrupts.
13+
/// * Each enum variant must represent a distinct value (no duplicates are permitted),
14+
/// * Each enum variant must always return the same value (do not change at runtime).
15+
/// * All the interrupt numbers must be less than or equal to `MAX_INTERRUPT_NUMBER`.
16+
/// * `MAX_INTERRUPT_NUMBER` must coincide with the highest allowed interrupt number.
17+
pub unsafe trait InterruptNumber: Copy {
18+
/// Highest number assigned to an interrupt source.
19+
const MAX_INTERRUPT_NUMBER: u16;
20+
21+
/// Converts an interrupt source to its corresponding number.
22+
fn number(self) -> u16;
23+
24+
/// Tries to convert a number to a valid interrupt source.
25+
/// If the conversion fails, it returns an error with the number back.
26+
fn from_number(value: u16) -> Result<Self, u16>;
27+
}
28+
29+
/// Trait for enums of priority levels.
30+
///
31+
/// This trait should be implemented by a peripheral access crate (PAC)
32+
/// on its enum of available priority numbers for a specific device.
33+
/// Each variant must convert to a `u8` of its priority level.
34+
///
35+
/// # Safety
36+
///
37+
/// * This trait must only be implemented on a PAC of a RISC-V target.
38+
/// * This trait must only be implemented on enums of priority levels.
39+
/// * Each enum variant must represent a distinct value (no duplicates are permitted).
40+
/// * Each enum variant must always return the same value (do not change at runtime).
41+
/// * All the priority level numbers must be less than or equal to `MAX_PRIORITY_NUMBER`.
42+
/// * `MAX_PRIORITY_NUMBER` must coincide with the highest allowed priority number.
43+
pub unsafe trait PriorityNumber: Copy {
44+
/// Number assigned to the highest priority level.
45+
const MAX_PRIORITY_NUMBER: u8;
46+
47+
/// Converts a priority level to its corresponding number.
48+
fn number(self) -> u8;
49+
50+
/// Tries to convert a number to a valid priority level.
51+
/// If the conversion fails, it returns an error with the number back.
52+
fn from_number(value: u8) -> Result<Self, u8>;
53+
}
54+
55+
/// Trait for enums of HART identifiers.
56+
///
57+
/// This trait should be implemented by a peripheral access crate (PAC)
58+
/// on its enum of available HARTs for a specific device.
59+
/// Each variant must convert to a `u16` of its HART ID number.
60+
///
61+
/// # Safety
62+
///
63+
/// * This trait must only be implemented on a PAC of a RISC-V target.
64+
/// * This trait must only be implemented on enums of HART IDs.
65+
/// * Each enum variant must represent a distinct value (no duplicates are permitted),
66+
/// * Each anum variant must always return the same value (do not change at runtime).
67+
/// * All the HART ID numbers must be less than or equal to `MAX_HART_ID_NUMBER`.
68+
/// * `MAX_HART_ID_NUMBER` must coincide with the highest allowed HART ID number.
69+
pub unsafe trait HartIdNumber: Copy {
70+
/// Highest number assigned to a context.
71+
const MAX_HART_ID_NUMBER: u16;
72+
73+
/// Converts a HART ID to its corresponding number.
74+
fn number(self) -> u16;
75+
76+
/// Tries to convert a number to a valid HART ID.
77+
/// If the conversion fails, it returns an error with the number back.
78+
fn from_number(value: u16) -> Result<Self, u16>;
79+
}

0 commit comments

Comments
 (0)