3
3
pub use crate :: interrupt:: Trap ;
4
4
pub use riscv_pac:: { CoreInterruptNumber , ExceptionNumber , InterruptNumber } ; // re-export useful riscv-pac traits
5
5
6
- /// scause register
7
- # [ derive ( Clone , Copy ) ]
8
- pub struct Scause {
9
- bits : usize ,
6
+ read_write_csr ! {
7
+ /// scause register
8
+ Scause : 0x142 ,
9
+ mask : usize :: MAX ,
10
10
}
11
11
12
- impl Scause {
13
- /// Returns the contents of the register as raw bits
14
- #[ inline]
15
- pub fn bits ( & self ) -> usize {
16
- self . bits
17
- }
12
+ #[ cfg( target_arch = "riscv32" ) ]
13
+ read_write_csr_field ! {
14
+ Scause ,
15
+ /// Returns the type of the trap:
16
+ ///
17
+ /// - `true`: an interrupt caused the trap
18
+ /// - `false`: an exception caused the trap
19
+ interrupt: 31 ,
20
+ }
18
21
22
+ #[ cfg( not( target_arch = "riscv32" ) ) ]
23
+ read_write_csr_field ! {
24
+ Scause ,
25
+ /// Returns the type of the trap:
26
+ ///
27
+ /// - `true`: an interrupt caused the trap
28
+ /// - `false`: an exception caused the trap
29
+ interrupt: 63 ,
30
+ }
31
+
32
+ #[ cfg( target_arch = "riscv32" ) ]
33
+ read_write_csr_field ! {
34
+ Scause ,
19
35
/// Returns the code field
20
- #[ inline]
21
- pub fn code ( & self ) -> usize {
22
- self . bits & !( 1 << ( usize:: BITS as usize - 1 ) )
23
- }
36
+ code: [ 0 : 30 ] ,
37
+ }
24
38
39
+ #[ cfg( not( target_arch = "riscv32" ) ) ]
40
+ read_write_csr_field ! {
41
+ Scause ,
42
+ /// Returns the code field
43
+ code: [ 0 : 62 ] ,
44
+ }
45
+
46
+ impl Scause {
25
47
/// Returns the trap cause represented by this register.
26
48
///
27
49
/// # Note
@@ -40,25 +62,16 @@ impl Scause {
40
62
/// Is trap cause an interrupt.
41
63
#[ inline]
42
64
pub fn is_interrupt ( & self ) -> bool {
43
- self . bits & ( 1 << ( usize :: BITS as usize - 1 ) ) != 0
65
+ self . interrupt ( )
44
66
}
45
67
46
68
/// Is trap cause an exception.
47
69
#[ inline]
48
70
pub fn is_exception ( & self ) -> bool {
49
- !self . is_interrupt ( )
71
+ !self . interrupt ( )
50
72
}
51
73
}
52
74
53
- read_csr_as ! ( Scause , 0x142 ) ;
54
- write_csr ! ( 0x142 ) ;
55
-
56
- /// Writes the CSR
57
- #[ inline]
58
- pub unsafe fn write ( bits : usize ) {
59
- _write ( bits)
60
- }
61
-
62
75
/// Set supervisor cause register to corresponding cause.
63
76
#[ inline]
64
77
pub unsafe fn set < I : CoreInterruptNumber , E : ExceptionNumber > ( cause : Trap < I , E > ) {
0 commit comments