aboutsummaryrefslogtreecommitdiff
path: root/src/lib.rs
diff options
context:
space:
mode:
authorIan McIntyre <ianpmcintyre@gmail.com>2022-09-22 13:49:23 -0400
committerIan McIntyre <ianpmcintyre@gmail.com>2022-12-07 09:42:54 -0500
commita5850ab6d6c508fc3351fd86646bcf3fb1b69103 (patch)
tree9e7aad64be5ac096b45096ffbf5cb0f3cb371346 /src/lib.rs
parent96bfa91e3bf81bc215524e8c5c02ee951339f702 (diff)
Add macro support for register arrays
I'm experimenting with a RAL code generator that collapses contiguous register arrays. The generated code would resemble pub struct RegisterBlock { pub MY_ARRAY: [RWRegister<u32>; 3], } and an individual register would be addressed like ral::read_reg!(ral::my_mod, my_inst, MY_ARRAY[1]); This commit extends the four macros so that we can specify an array offset. We simply need to match zero or more `[N]` patterns, where `N` is some expression that produces an array offset. The included test case shows that the approach should support multi-dimensional arrays.
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs40
1 files changed, 20 insertions, 20 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 52401d6..c65e94b 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -275,11 +275,11 @@ impl<T: Copy> UnsafeWORegister<T> {
/// and the macro brings such constants into scope and then dereferences the provided reference.
#[macro_export]
macro_rules! write_reg {
- ( $periph:path, $instance:expr, $reg:ident, $( $field:ident : $value:expr ),+ ) => {{
+ ( $periph:path, $instance:expr, $reg:ident $([$offset:expr])*, $( $field:ident : $value:expr ),+ ) => {{
#[allow(unused_imports)]
use $periph::{*};
#[allow(unused_imports)]
- (*$instance).$reg.write(
+ (*$instance).$reg $([$offset])*.write(
$({
use $periph::{$reg::$field::{W::*, RW::*}};
($value << { use $periph::{$reg::$field::offset}; offset })
@@ -287,10 +287,10 @@ macro_rules! write_reg {
}) | *
);
}};
- ( $periph:path, $instance:expr, $reg:ident, $value:expr ) => {{
+ ( $periph:path, $instance:expr, $reg:ident $([$offset:expr])*, $value:expr ) => {{
#[allow(unused_imports)]
use $periph::{*};
- (*$instance).$reg.write($value);
+ (*$instance).$reg $([$offset])*.write($value);
}};
}
@@ -408,12 +408,12 @@ macro_rules! write_reg {
/// and the macro brings such constants into scope and then dereferences the provided reference.
#[macro_export]
macro_rules! modify_reg {
- ( $periph:path, $instance:expr, $reg:ident, $( $field:ident : $value:expr ),+ ) => {{
+ ( $periph:path, $instance:expr, $reg:ident $([$offset:expr])*, $( $field:ident : $value:expr ),+ ) => {{
#[allow(unused_imports)]
use $periph::{*};
#[allow(unused_imports)]
- (*$instance).$reg.write(
- ((*$instance).$reg.read() & !( $({ use $periph::{$reg::$field::mask}; mask }) | * ))
+ (*$instance).$reg $([$offset])*.write(
+ ((*$instance).$reg $([$offset])*.read() & !( $({ use $periph::{$reg::$field::mask}; mask }) | * ))
| $({
use $periph::{$reg::$field::{W::*, RW::*}};
($value << { use $periph::{$reg::$field::offset}; offset })
@@ -421,10 +421,10 @@ macro_rules! modify_reg {
}) | *
);
}};
- ( $periph:path, $instance:expr, $reg:ident, $fn:expr ) => {{
+ ( $periph:path, $instance:expr, $reg:ident $([$offset:expr])*, $fn:expr ) => {{
#[allow(unused_imports)]
use $periph::{*};
- (*$instance).$reg.write($fn((*$instance).$reg.read()));
+ (*$instance).$reg $([$offset])*.write($fn((*$instance).$reg $([$offset])*.read()));
}};
}
@@ -527,27 +527,27 @@ macro_rules! modify_reg {
/// and the macro brings such constants into scope and then dereferences the provided reference.
#[macro_export]
macro_rules! read_reg {
- ( $periph:path, $instance:expr, $reg:ident, $( $field:ident ),+ ) => {{
+ ( $periph:path, $instance:expr, $reg:ident $([$offset:expr])*, $( $field:ident ),+ ) => {{
#[allow(unused_imports)]
use $periph::{*};
- let val = ((*$instance).$reg.read());
+ let val = ((*$instance).$reg $([$offset])*.read());
( $({
#[allow(unused_imports)]
use $periph::{$reg::$field::{mask, offset, R::*, RW::*}};
(val & mask) >> offset
}) , *)
}};
- ( $periph:path, $instance:expr, $reg:ident, $field:ident $($cmp:tt)* ) => {{
+ ( $periph:path, $instance:expr, $reg:ident $([$offset:expr])*, $field:ident $($cmp:tt)* ) => {{
#[allow(unused_imports)]
use $periph::{*};
#[allow(unused_imports)]
use $periph::{$reg::$field::{mask, offset, R::*, RW::*}};
- (((*$instance).$reg.read() & mask) >> offset) $($cmp)*
+ (((*$instance).$reg $([$offset])*.read() & mask) >> offset) $($cmp)*
}};
- ( $periph:path, $instance:expr, $reg:ident ) => {{
+ ( $periph:path, $instance:expr, $reg:ident $([$offset:expr])* ) => {{
#[allow(unused_imports)]
use $periph::{*};
- ((*$instance).$reg.read())
+ ((*$instance).$reg $([$offset])*.read())
}};
}
@@ -624,20 +624,20 @@ macro_rules! read_reg {
/// `GPIOA` they are not the same thing.
#[macro_export]
macro_rules! reset_reg {
- ( $periph:path, $instance:expr, $instancemod:path, $reg:ident, $( $field:ident ),+ ) => {{
+ ( $periph:path, $instance:expr, $instancemod:path, $reg:ident $([$offset:expr])*, $( $field:ident ),+ ) => {{
#[allow(unused_imports)]
use $periph::{*};
use $periph::{$instancemod::{reset}};
#[allow(unused_imports)]
- (*$instance).$reg.write({
+ (*$instance).$reg $([$offset])*.write({
let resetmask: u32 = $({ use $periph::{$reg::$field::mask}; mask }) | *;
- ((*$instance).$reg.read() & !resetmask) | (reset.$reg & resetmask)
+ ((*$instance).$reg $([$offset])*.read() & !resetmask) | (reset.$reg & resetmask)
});
}};
- ( $periph:path, $instance:expr, $instancemod:path, $reg:ident ) => {{
+ ( $periph:path, $instance:expr, $instancemod:path, $reg:ident $([$offset:expr])*) => {{
#[allow(unused_imports)]
use $periph::{*};
use $periph::{$instancemod::{reset}};
- (*$instance).$reg.write(reset.$reg);
+ (*$instance).$reg $([$offset])*.write(reset.$reg);
}};
}