-
Notifications
You must be signed in to change notification settings - Fork 162
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
riscv: add fallible functions #222
Merged
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
014bc42
riscv: add result module
rmsyn 0d23dc1
riscv: add fallible functions to register macros
rmsyn 5165db3
riscv: add fallible functions to mcounteren
rmsyn 66fa6a4
riscv: add fallible functions to scounteren
rmsyn 56f30bb
riscv: add fallible functions to mcountinhibit
rmsyn 54bdc4e
riscv: add fallible functions to satp
rmsyn 77b692f
riscv: add fallible functions to pmpcfgx
rmsyn 62a417a
riscv-pac: use `Result` type for traits
rmsyn fea2e0b
riscv-peripheral: use `riscv-pac` result types
rmsyn 2b35502
riscv-pac: add field and field-less error variants
rmsyn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
use core::fmt; | ||
|
||
/// Convenience alias for the [Result](core::result::Result) type for the library. | ||
pub type Result<T> = core::result::Result<T, Error>; | ||
|
||
/// Represents error variants for the library. | ||
#[derive(Clone, Copy, Debug, Eq, PartialEq)] | ||
pub enum Error { | ||
/// Attempted out-of-bounds access. | ||
IndexOutOfBounds { | ||
index: usize, | ||
min: usize, | ||
max: usize, | ||
}, | ||
/// Invalid field value. | ||
InvalidValue { | ||
field: &'static str, | ||
value: usize, | ||
bitmask: usize, | ||
}, | ||
/// Invalid value of a register field that does not match any known variants. | ||
InvalidVariant { field: &'static str, value: usize }, | ||
/// Unimplemented function or type. | ||
Unimplemented, | ||
} | ||
|
||
impl fmt::Display for Error { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
match self { | ||
Self::IndexOutOfBounds { index, min, max } => write!( | ||
f, | ||
"out-of-bounds access, index: {index}, min: {min}, max: {max}" | ||
), | ||
Self::InvalidValue { | ||
field, | ||
value, | ||
bitmask, | ||
} => write!( | ||
f, | ||
"invalid {field} field value: {value:#x}, valid bitmask: {bitmask:#x}", | ||
), | ||
Self::InvalidVariant { field, value } => { | ||
write!(f, "invalid {field} field variant: {value:#x}") | ||
} | ||
Self::Unimplemented => write!(f, "unimplemented"), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
//! This is a simple example of how to use the `riscv-peripheral` crate to generate | ||
//! peripheral definitions for a target. | ||
|
||
use riscv_pac::result::{Error, Result}; | ||
use riscv_pac::{HartIdNumber, InterruptNumber, PriorityNumber}; | ||
|
||
#[repr(u16)] | ||
|
@@ -19,9 +20,12 @@ unsafe impl HartIdNumber for HartId { | |
} | ||
|
||
#[inline] | ||
fn from_number(number: u16) -> Result<Self, u16> { | ||
fn from_number(number: u16) -> Result<Self> { | ||
if number > Self::MAX_HART_ID_NUMBER { | ||
Err(number) | ||
Err(Error::InvalidVariant { | ||
field: "hart_id", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adding |
||
value: number as usize, | ||
}) | ||
} else { | ||
// SAFETY: valid context number | ||
Ok(unsafe { core::mem::transmute(number) }) | ||
|
@@ -95,9 +99,12 @@ unsafe impl InterruptNumber for Interrupt { | |
} | ||
|
||
#[inline] | ||
fn from_number(number: u16) -> Result<Self, u16> { | ||
fn from_number(number: u16) -> Result<Self> { | ||
if number == 0 || number > Self::MAX_INTERRUPT_NUMBER { | ||
Err(number) | ||
Err(Error::InvalidVariant { | ||
field: "interrupt", | ||
value: number as usize, | ||
}) | ||
} else { | ||
// SAFETY: valid interrupt number | ||
Ok(unsafe { core::mem::transmute(number) }) | ||
|
@@ -127,9 +134,12 @@ unsafe impl PriorityNumber for Priority { | |
} | ||
|
||
#[inline] | ||
fn from_number(number: u8) -> Result<Self, u8> { | ||
fn from_number(number: u8) -> Result<Self> { | ||
if number > Self::MAX_PRIORITY_NUMBER { | ||
Err(number) | ||
Err(Error::InvalidVariant { | ||
field: "priority", | ||
value: number as usize, | ||
}) | ||
} else { | ||
// SAFETY: valid priority number | ||
Ok(unsafe { core::mem::transmute(number) }) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we could differentiate errors regarding fields of a register and errors regarding the whole register/value. The idea is avoiding defining the
field
in theriscv-peripheral
examples, as I think it is not necessary.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This makes sense to me. The value covers the entire structure, so this separation could be useful in other cases without subfields.