11//! mvien register
22
33use crate :: bits:: { bf_extract, bf_insert} ;
4+ use riscv_pac:: result:: { Error , Result } ;
45use riscv_pac:: InterruptNumber ;
56
67#[ cfg( target_arch = "riscv32" ) ]
@@ -49,29 +50,45 @@ impl Mvien {
4950 }
5051
5152 /// Check if a specific core interrupt source is enabled.
53+ ///
54+ /// Returns `Error` if the interrupt number is invalid.
5255 #[ inline]
53- pub fn is_enabled < I : InterruptNumber > ( & self , interrupt : I ) -> bool {
56+ pub fn is_enabled < I : InterruptNumber > ( & self , interrupt : I ) -> Result < bool > {
5457 let n = interrupt. number ( ) ;
55- Self :: is_valid_interrupt ( n) && bf_extract ( self . bits , n, 1 ) != 0
58+ if Self :: is_valid_interrupt ( n) {
59+ Ok ( bf_extract ( self . bits , n, 1 ) != 0 )
60+ } else {
61+ Err ( Error :: InvalidVariant ( n) )
62+ }
5663 }
5764
5865 /// Enable a specific core interrupt source.
66+ ///
67+ /// Returns `Error` if the interrupt number is invalid.
5968 #[ inline]
60- pub fn enable < I : InterruptNumber > ( & mut self , interrupt : I ) {
69+ pub fn enable < I : InterruptNumber > ( & mut self , interrupt : I ) -> Result < ( ) > {
6170 let n = interrupt. number ( ) ;
6271
6372 if Self :: is_valid_interrupt ( n) {
6473 self . bits = bf_insert ( self . bits , n, 1 , 1 ) ;
74+ Ok ( ( ) )
75+ } else {
76+ Err ( Error :: InvalidVariant ( n) )
6577 }
6678 }
6779
6880 /// Disable a specific core interrupt source.
81+ ///
82+ /// Returns `Error` if the interrupt number is invalid.
6983 #[ inline]
70- pub fn disable < I : InterruptNumber > ( & mut self , interrupt : I ) {
84+ pub fn disable < I : InterruptNumber > ( & mut self , interrupt : I ) -> Result < ( ) > {
7185 let n = interrupt. number ( ) ;
7286
7387 if Self :: is_valid_interrupt ( n) {
7488 self . bits = bf_insert ( self . bits , n, 1 , 0 ) ;
89+ Ok ( ( ) )
90+ } else {
91+ Err ( Error :: InvalidVariant ( n) )
7592 }
7693 }
7794}
@@ -94,15 +111,14 @@ read_composite_csr!(super::mvienh::read().bits(), read().bits());
94111#[ cfg( test) ]
95112mod tests {
96113 use super :: * ;
97- use riscv_pac:: result:: { Error , Result } ;
98114
99115 /// Represents a custom set of virtual interrupts.
100116 ///
101117 /// NOTE: a real implementation may want to enumerate the valid virtual interrupt variants.
102118 #[ derive( Clone , Copy , Debug , Eq , PartialEq ) ]
103119 pub struct VirtualInterrupt ( usize ) ;
104120
105- /// SAFETY: `Interrupt ` represents the standard RISC-V interrupts
121+ /// SAFETY: `VirtualInterrupt ` represents the virtual RISC-V interrupts
106122 unsafe impl InterruptNumber for VirtualInterrupt {
107123 const MAX_INTERRUPT_NUMBER : usize = Mvien :: MAX_INTERRUPT ;
108124
@@ -132,13 +148,13 @@ mod tests {
132148 ( 0 ..=VirtualInterrupt :: MAX_INTERRUPT_NUMBER )
133149 . filter_map ( |n| VirtualInterrupt :: from_number ( n) . ok ( ) )
134150 . for_each ( |int| {
135- assert ! ( ! m. is_enabled( int) ) ;
151+ assert_eq ! ( m. is_enabled( int) , Ok ( false ) ) ;
136152
137- m. enable ( int) ;
138- assert ! ( m. is_enabled( int) ) ;
153+ assert ! ( m. enable( int) . is_ok ( ) ) ;
154+ assert_eq ! ( m. is_enabled( int) , Ok ( true ) ) ;
139155
140- m. disable ( int) ;
141- assert ! ( ! m. is_enabled( int) ) ;
156+ assert ! ( m. disable( int) . is_ok ( ) ) ;
157+ assert_eq ! ( m. is_enabled( int) , Ok ( false ) ) ;
142158 } ) ;
143159 }
144160}
0 commit comments