1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
|
// Copyright 2018 Adam Greig
// Copyright 2025 Ian McIntyre
// See LICENSE-APACHE and LICENSE-MIT for license details.
//! This crate contains an MMIO abstraction that uses macros to read,
//! modify, and write fields in registers.
//!
//! See the [README](https://github.com/adamgreig/ral-registers/blob/master/README.md)
//! for further details.
#![no_std]
use core::ptr::NonNull;
/// Describes a type that acts as a peripheral instance.
///
/// If you're not sure how to implement this, try using [`Instance`].
///
/// # Safety
///
/// This can only be implemented atop "smart pointer" objects that
/// produce the same raw pointer to a static register block.
pub unsafe trait Inst {}
/// Safety: any shared reference to an instance can similarly provide
/// access to the underlying static object.
unsafe impl<I> Inst for &'_ I where I: Inst {}
/// Safety: see shared reference documentation. If a shared
/// reference can provide this access, then so can an exclusive
/// reference.
unsafe impl<I> Inst for &'_ mut I where I: Inst {}
/// A pointer to a peripheral instance.
///
/// This is transparently a `NonNull<T>`. Like a `NonNull<T>`,
/// the type is `Copy` and `Clone`, so it may be shared throughout
/// a single execution context.
#[derive(PartialEq, Eq)]
#[repr(transparent)]
pub struct Instance<T: ?Sized>(NonNull<T>);
/// Safety: the constructor's safety contract enforces the
/// trait's safety requirements.
unsafe impl<T: ?Sized> Inst for Instance<T> {}
impl<T: ?Sized> Instance<T> {
/// Create an instance that points to a peripheral register block.
///
/// # Safety
///
/// The pointer must not be NULL. The pointer is expected to point
/// to static memory that represents a block of MMIO registers.
/// The layout of `T` must be correct for the base address.
#[inline(always)]
#[must_use]
pub const unsafe fn new_unchecked(ptr: *mut T) -> Self {
// Safety: caller claims the pointer isn't NULL.
Self(unsafe { NonNull::new_unchecked(ptr) })
}
/// Access the inner pointer.
#[inline(always)]
#[must_use]
pub const fn as_ptr(self) -> *mut T {
self.0.as_ptr()
}
}
impl<T: ?Sized> Clone for Instance<T> {
#[inline(always)]
fn clone(&self) -> Self {
*self
}
}
impl<T: ?Sized> Copy for Instance<T> {}
impl<T> core::fmt::Debug for Instance<T> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let ptr = self.0.as_ptr() as usize;
f.debug_tuple("Instance")
.field(&format_args!("{ptr:#X}"))
.finish()
}
}
/// Check expressions that may be unsafe.
///
/// Users are not expected to pass non-Copy expressions into
/// this expansion.
#[macro_export]
#[doc(hidden)]
macro_rules! __expand_unsafe {
( $( $expr:expr, )+ ) => { if false { $( let _ = $expr; )* } };
}
#[doc(hidden)]
#[allow(non_snake_case)]
pub const fn must_be_ral_registers_Inst<I: Inst>(_: &I) {}
/// Write to a register.
#[macro_export]
macro_rules! write_reg {
( $(::)? $($periph:ident)::+, $instance:expr, $( $(.)? $reg:ident $([$offset:expr])* )+, $( $field:ident : $value:expr ),+ $(,)? ) => {{
use $($periph)::+ $( :: $reg )* as __register;
$crate::write_reg!($($periph)::+, $instance, $(. $reg $([$offset])* )*,
$({
#[allow(unused_imports)]
use __register::{$field::vals::{*}};
const { assert!(__register::$field::access.is_write()); }
($value << __register::$field::offset)
& __register::$field::mask
}) | *
);
}};
( $(::)? $($periph:ident)::+, $instance:expr, $( $(.)? $reg:ident $([$offset:expr])* )+, $value:expr ) => {{
#[allow(unused_imports)]
use $($periph)::+ $( :: $reg )* as __register;
const { assert!(__register::access.is_write()); }
$crate::must_be_ral_registers_Inst(&$instance);
$crate::__expand_unsafe!($instance, $( $( $offset ,)* )*);
#[allow(unused_unsafe)]
unsafe { (&raw mut (*$instance.as_ptr())$(. $reg $([$offset])* )*).write_volatile($value) }
}};
}
/// Modify a register.
#[macro_export]
macro_rules! modify_reg {
( $(::)? $($periph:ident)::+, $instance:expr, $( $(.)? $reg:ident $([$offset:expr])* )+, $( $field:ident : $value:expr ),+ $(,)? ) => {{
$crate::__expand_unsafe!($instance, $( $( $offset ,)* )*);
$crate::must_be_ral_registers_Inst(&$instance);
use $($periph)::+ $( :: $reg )* as __register;
const { assert!(__register::access.is_read_write()); }
#[allow(unused_unsafe)]
unsafe {
(&raw mut (*$instance.as_ptr())$(. $reg $([$offset])* )*).write_volatile(
(&raw const (*$instance.as_ptr())$(. $reg $([$offset])* )*).read_volatile() & const { !( $(__register::$field::mask) | * ) }
| $({
#[allow(unused_imports)]
use __register::{$field::vals::{*}};
const { assert!(__register::$field::access.is_read_write()); }
($value << __register::$field::offset )
& __register::$field::mask
}) | *
)
}
}};
( $(::)? $($periph:ident)::+, $instance:expr, $( $(.)? $reg:ident $([$offset:expr])* )+, $fn:expr ) => {{
#[allow(unused_imports)]
use $($periph)::+ $( :: $reg )* as __register;
const { assert!(__register::access.is_read_write()); }
$crate::__expand_unsafe!($instance, $( $( $offset ,)* )*);
$crate::must_be_ral_registers_Inst(&$instance);
#[allow(unused_unsafe)]
unsafe {
(&raw mut (*$instance.as_ptr())$(. $reg $([$offset])* )*).write_volatile(
$fn((&raw const (*$instance.as_ptr())$(. $reg $([$offset])* )*).read_volatile())
)
}
}};
}
/// Read the value from a register.
#[macro_export]
macro_rules! read_reg {
( $(::)? $($periph:ident)::+, $instance:expr, $( $(.)? $reg:ident $([$offset:expr])* )+, $( $field:ident ),+ $(,)? ) => {{
let __value = $crate::read_reg!($($periph)::+, $instance, $(. $reg $([$offset])* )*);
use $($periph)::+ $( :: $reg )* as __register;
( $({
#[allow(unused_imports)]
use __register::$field::vals::{*};
const { assert!(__register::$field::access.is_read()); }
(__value & __register::$field::mask) >> __register::$field::offset
}) , *)
}};
( $(::)? $($periph:ident)::+, $instance:expr, $( $(.)? $reg:ident $([$offset:expr])* )+, $field:ident $($cmp:tt)* ) => {{
use $($periph)::+ $( :: $reg )* as __register;
#[allow(unused_imports)]
use __register::$field::vals::{*};
const { assert!(__register::$field::access.is_read()); }
(($crate::read_reg!($($periph)::+, $instance, $(. $reg $([$offset])* )* ) & __register::$field::mask) >> __register::$field::offset) $($cmp)*
}};
( $(::)? $($periph:ident)::+, $instance:expr, $( $(.)? $reg:ident $([$offset:expr])* )+ ) => {{
#[allow(unused_imports)]
use $($periph)::+ $( :: $reg )* as __register;
const { assert!(__register::access.is_read()); }
$crate::__expand_unsafe!($instance, $( $( $offset ,)* )*);
$crate::must_be_ral_registers_Inst(&$instance);
#[allow(unused_unsafe)]
unsafe {
(&raw const (*$instance.as_ptr())$(. $reg $([$offset])* )*).read_volatile()
}
}};
}
/// Access for registers and fields.
pub enum Access {
/// Read-only access.
RO,
/// Write-only access.
WO,
/// Read-write access.
RW,
}
impl Access {
/// Returns `true` if the access is read.
#[inline(always)]
#[must_use]
pub const fn is_read(self) -> bool {
matches!(self, Self::RO | Self::RW)
}
/// Returns `true` if the access is write.
#[inline(always)]
#[must_use]
pub const fn is_write(self) -> bool {
matches!(self, Self::WO | Self::RW)
}
/// Returns `true` if the access is read and write.
#[inline(always)]
#[must_use]
pub const fn is_read_write(self) -> bool {
matches!(self, Self::RW)
}
}
#[macro_export]
macro_rules! register {
(
$(#[$register_meta:meta])*
$vis:vis $register_name:ident <$ty:ty> $register_access:ident [$(
$(#[$field_meta:meta])*
$field_name:ident start($start_bit:expr) width($width:expr) $field_access:ident {$(
$(#[$enum_meta:meta])*
$enum_name:ident = $enum_val:expr $(,)?
)*}
)*]
) => {
$(#[$register_meta])*
#[allow(non_snake_case, non_upper_case_globals)]
$vis mod $register_name {
#[doc(hidden)]
pub const access: $crate::Access = $crate::Access::$register_access;
$(
$(#[$field_meta])*
#[allow(non_snake_case)]
pub mod $field_name {
#[doc(hidden)]
pub const offset: $ty = $start_bit;
#[doc(hidden)]
pub const mask: $ty = ((1 << $width) - 1) << $start_bit;
#[doc(hidden)]
pub const access: $crate::Access = $crate::Access::$field_access;
#[doc(hidden)]
pub mod vals {
$(
$(#[$enum_meta])*
pub const $enum_name: $ty = $enum_val;
)*
}
#[doc(inline)]
pub use self::vals::*;
}
)*
}
};
}
#[macro_export]
macro_rules! instances {
(unsafe { $(
$(#[$doc:meta])*
$vis:vis $name:ident<$block:ty> = $base_address:expr;
)* }) => {$(
$(#[$doc])*
#[allow(non_snake_case)]
#[inline(always)]
$vis const unsafe fn $name() -> $crate::Instance<$block> {
// Safety: Macro caller swears that the $base_address points
// to the given kind of register block.
unsafe { $crate::Instance::new_unchecked($base_address as _) }
}
)*};
}
|