3
3
pub use paste:: paste;
4
4
5
5
/// Macro to create interfaces to CLINT peripherals in PACs.
6
- /// The resulting struct will be named `CLINT`, and will provide safe access to the CLINT registers.
6
+ /// The resulting struct will provide safe access to the CLINT registers.
7
7
///
8
- /// This macro expects 3 different argument types:
8
+ /// This macro expects 5 different argument types:
9
9
///
10
+ /// - Visibility (**MANDATORY**): visibility of the `fn new()` function for creating a new CLINT.
11
+ /// It can be ``, `pub`, `pub(crate)`, or `pub(super)`. If empty, the function will be private.
12
+ /// - Peripheral name (**MANDATORY**): name of the resulting CLINT peripheral.
10
13
/// - Base address (**MANDATORY**): base address of the CLINT peripheral of the target.
11
14
/// - MTIME Frequency (**MANDATORY**): clock frequency (in Hz) of the `MTIME` register.
12
15
/// - HART map (**OPTIONAL**): a list of HART IDs and their corresponding numbers.
@@ -15,17 +18,17 @@ pub use paste::paste;
15
18
///
16
19
/// # Example
17
20
///
18
- /// ## Mandatory fields only
21
+ /// ## Mandatory fields only, public `fn new()` function
19
22
///
20
23
/// ```
21
- /// riscv_peripheral::clint_codegen!(base 0x0200_0000, mtime_freq 32_768,); // do not forget the ending comma!
24
+ /// riscv_peripheral::clint_codegen!(pub CLINT, base 0x0200_0000, mtime_freq 32_768);
22
25
///
23
- /// let clint = CLINT::new(); // Create a new CLINT peripheral
26
+ /// let clint = CLINT::new(); // Create a new CLINT peripheral (new is public)
24
27
/// let mswi = clint.mswi(); // MSWI peripheral
25
28
/// let mtimer = clint.mtimer(); // MTIMER peripheral
26
29
/// ```
27
30
///
28
- /// ## Base address and per-HART mtimecmp registers
31
+ /// ## Base address and per-HART mtimecmp registers, private `fn new()` function
29
32
///
30
33
/// ```
31
34
/// use riscv_pac::result::{Error, Result};
@@ -36,12 +39,13 @@ pub use paste::paste;
36
39
/// pub enum HartId { H0 = 0, H1 = 1, H2 = 2 }
37
40
///
38
41
/// riscv_peripheral::clint_codegen!(
42
+ /// Clint,
39
43
/// base 0x0200_0000,
40
44
/// mtime_freq 32_768,
41
- /// harts [HartId::H0 => 0, HartId::H1 => 1, HartId::H2 => 2], // do not forget the ending comma!
45
+ /// harts [HartId::H0 => 0, HartId::H1 => 1, HartId::H2 => 2]
42
46
/// );
43
47
///
44
- /// let clint = CLINT ::new(); // Create a new CLINT peripheral
48
+ /// let clint = Clint ::new(); // Create a new CLINT peripheral (new is private)
45
49
/// let mswi = clint.mswi(); // MSWI peripheral
46
50
/// let mtimer = clint.mtimer(); // MTIMER peripheral
47
51
///
@@ -55,97 +59,87 @@ pub use paste::paste;
55
59
/// ```
56
60
#[ macro_export]
57
61
macro_rules! clint_codegen {
58
- ( ) => {
59
- #[ allow( unused_imports) ]
60
- use CLINT as _; // assert that the CLINT struct is defined
61
- } ;
62
- ( base $addr: literal, mtime_freq $freq: literal, $( $tail: tt) * ) => {
62
+ ( $vis: vis $name: ident, base $addr: literal, mtime_freq $freq: literal) => {
63
63
/// CLINT peripheral
64
64
#[ allow( clippy:: upper_case_acronyms) ]
65
65
#[ derive( Clone , Copy , Debug , Eq , PartialEq ) ]
66
- pub struct CLINT ( $crate:: aclint:: CLINT <Self >) ;
66
+ pub struct $name ( $crate:: aclint:: CLINT <Self >) ;
67
67
68
- impl CLINT {
68
+ impl $name {
69
69
/// Creates a new `CLINT` peripheral.
70
70
#[ inline]
71
- pub const fn new( ) -> Self {
71
+ $vis const fn new( ) -> Self {
72
72
Self ( $crate:: aclint:: CLINT :: new( ) )
73
73
}
74
74
}
75
75
76
- unsafe impl $crate:: aclint:: Clint for CLINT {
76
+ unsafe impl $crate:: aclint:: Clint for $name {
77
77
const BASE : usize = $addr;
78
78
const MTIME_FREQ : usize = $freq;
79
79
}
80
80
81
- impl core:: ops:: Deref for CLINT {
81
+ impl core:: ops:: Deref for $name {
82
82
type Target = $crate:: aclint:: CLINT <Self >;
83
83
84
84
#[ inline]
85
85
fn deref( & self ) -> & Self :: Target {
86
86
& self . 0
87
87
}
88
88
}
89
-
90
- impl core:: ops:: DerefMut for CLINT {
91
- #[ inline]
92
- fn deref_mut( & mut self ) -> & mut Self :: Target {
93
- & mut self . 0
94
- }
95
- }
96
-
97
- $crate:: clint_codegen!( $( $tail) * ) ;
98
89
} ;
99
- ( harts [ $( $hart: expr => $num: literal) ,+] , $( $tail: tt) * ) => {
90
+ ( $vis: vis $name: ident, base $addr: literal, mtime_freq $freq: literal, harts [ $( $hart: expr => $num: literal) ,+] ) => {
91
+ $crate:: clint_codegen!( $vis $name, base $addr, mtime_freq $freq) ;
100
92
$crate:: macros:: paste! {
101
- impl CLINT {
93
+ impl $name {
102
94
$(
103
- #[ doc = "Returns the `msip` register for HART [` " ]
104
- #[ doc = stringify!( $hart ) ]
105
- #[ doc = "`] ." ]
95
+ #[ doc = "Returns the `msip` register for HART " ]
96
+ #[ doc = stringify!( $num ) ]
97
+ #[ doc = "." ]
106
98
#[ inline]
107
99
pub fn [ <msip $num>] ( & self ) -> $crate:: aclint:: mswi:: MSIP {
108
100
self . mswi( ) . msip( $hart)
109
101
}
110
- #[ doc = "Returns the `mtimecmp` register for HART [` " ]
111
- #[ doc = stringify!( $hart ) ]
112
- #[ doc = "`] ." ]
102
+ #[ doc = "Returns the `mtimecmp` register for HART " ]
103
+ #[ doc = stringify!( $num ) ]
104
+ #[ doc = "." ]
113
105
#[ inline]
114
106
pub fn [ <mtimecmp $num>] ( & self ) -> $crate:: aclint:: mtimer:: MTIMECMP {
115
107
self . mtimer( ) . mtimecmp( $hart)
116
108
}
117
109
) *
118
110
}
119
111
}
120
- $crate:: clint_codegen!( $( $tail) * ) ;
121
112
} ;
122
113
}
123
114
124
115
/// Macro to create interfaces to PLIC peripherals in PACs.
125
116
/// The resulting struct will be named `PLIC`, and will provide safe access to the PLIC registers.
126
117
///
127
- /// This macro expects 2 different argument types:
118
+ /// This macro expects 4 different argument types:
128
119
///
120
+ /// - Visibility (**MANDATORY**): visibility of the `fn new()` function for creating a new PLIC.
121
+ /// It can be ``, `pub`, `pub(crate)`, or `pub(super)`. If empty, the function will be private.
122
+ /// - Peripheral name (**MANDATORY**): name of the resulting PLIC peripheral.
129
123
/// - Base address (**MANDATORY**): base address of the PLIC peripheral of the target.
130
124
/// - HART map (**OPTIONAL**): a list of HART IDs and their corresponding numbers.
131
125
///
132
126
/// Check the examples below for more details about the usage and syntax of this macro.
133
127
///
134
128
/// # Example
135
129
///
136
- /// ## Base address only
130
+ /// ## Base address only, public `fn new()` function
137
131
///
138
132
/// ```
139
133
/// use riscv_peripheral::clint_codegen;
140
134
///
141
- /// riscv_peripheral::plic_codegen!(base 0x0C00_0000,); // do not forget the ending comma!
135
+ /// riscv_peripheral::plic_codegen!(pub PLIC, base 0x0C00_0000);
142
136
///
143
- /// let plic = PLIC::new(); // Create a new PLIC peripheral
137
+ /// let plic = PLIC::new(); // Create a new PLIC peripheral (new is public)
144
138
/// let priorities = plic.priorities(); // Priorities registers
145
139
/// let pendings = plic.pendings(); // Pendings registers
146
140
/// ```
147
141
///
148
- /// ## Base address and per-HART context proxies
142
+ /// ## Base address and per-HART context proxies, private `fn new()` function
149
143
///
150
144
/// ```
151
145
/// use riscv_pac::result::{Error, Result};
@@ -156,11 +150,12 @@ macro_rules! clint_codegen {
156
150
/// pub enum HartId { H0 = 0, H1 = 1, H2 = 2 }
157
151
///
158
152
/// riscv_peripheral::plic_codegen!(
153
+ /// Plic,
159
154
/// base 0x0C00_0000,
160
- /// harts [HartId::H0 => 0, HartId::H1 => 1, HartId::H2 => 2], // do not forget the ending comma!
155
+ /// harts [HartId::H0 => 0, HartId::H1 => 1, HartId::H2 => 2]
161
156
/// );
162
157
///
163
- /// let plic = PLIC ::new(); // Create a new PLIC peripheral
158
+ /// let plic = Plic ::new(); // Create a new PLIC peripheral (new is private)
164
159
/// let ctx0 = plic.ctx0(); // Context proxy for HART 0
165
160
/// let ctx1 = plic.ctx1(); // Context proxy for HART 1
166
161
/// let ctx2 = plic.ctx2(); // Context proxy for HART 2
@@ -171,60 +166,47 @@ macro_rules! clint_codegen {
171
166
/// ```
172
167
#[ macro_export]
173
168
macro_rules! plic_codegen {
174
- ( ) => {
175
- #[ allow( unused_imports) ]
176
- use PLIC as _; // assert that the PLIC struct is defined
177
- } ;
178
- ( base $addr: literal, $( $tail: tt) * ) => {
169
+ ( $vis: vis $name: ident, base $addr: literal) => {
179
170
/// PLIC peripheral
180
171
#[ allow( clippy:: upper_case_acronyms) ]
181
172
#[ derive( Clone , Copy , Debug , Eq , PartialEq ) ]
182
- pub struct PLIC ( $crate:: plic:: PLIC <Self >) ;
173
+ pub struct $name ( $crate:: plic:: PLIC <Self >) ;
183
174
184
- impl PLIC {
185
- /// Creates a new `CLINT ` peripheral.
175
+ impl $name {
176
+ /// Creates a new `PLIC ` peripheral.
186
177
#[ inline]
187
- pub const fn new( ) -> Self {
178
+ $vis const fn new( ) -> Self {
188
179
Self ( $crate:: plic:: PLIC :: new( ) )
189
180
}
190
181
}
191
182
192
- unsafe impl $crate:: plic:: Plic for PLIC {
183
+ unsafe impl $crate:: plic:: Plic for $name {
193
184
const BASE : usize = $addr;
194
185
}
195
186
196
- impl core:: ops:: Deref for PLIC {
187
+ impl core:: ops:: Deref for $name {
197
188
type Target = $crate:: plic:: PLIC <Self >;
198
189
199
190
#[ inline]
200
191
fn deref( & self ) -> & Self :: Target {
201
192
& self . 0
202
193
}
203
194
}
204
-
205
- impl core:: ops:: DerefMut for PLIC {
206
- #[ inline]
207
- fn deref_mut( & mut self ) -> & mut Self :: Target {
208
- & mut self . 0
209
- }
210
- }
211
-
212
- $crate:: plic_codegen!( $( $tail) * ) ;
213
195
} ;
214
- ( harts [ $( $hart: expr => $num: literal) ,+] , $( $tail: tt) * ) => {
196
+ ( $vis: vis $name: ident, base $addr: literal, harts [ $( $hart: expr => $num: literal) ,+] ) => {
197
+ $crate:: plic_codegen!( $vis $name, base $addr) ;
215
198
$crate:: macros:: paste! {
216
- impl PLIC {
199
+ impl $name {
217
200
$(
218
- #[ doc = "Returns a PLIC context proxy for context of HART [` " ]
219
- #[ doc = stringify!( $hart ) ]
220
- #[ doc = "`] ." ]
201
+ #[ doc = "Returns a PLIC context proxy for context of HART " ]
202
+ #[ doc = stringify!( $num ) ]
203
+ #[ doc = "." ]
221
204
#[ inline]
222
205
pub fn [ <ctx $num>] ( & self ) -> $crate:: plic:: CTX <Self > {
223
206
self . ctx( $hart)
224
207
}
225
208
) *
226
209
}
227
210
}
228
- $crate:: plic_codegen!( $( $tail) * ) ;
229
211
} ;
230
212
}
0 commit comments