diff options
| -rw-r--r-- | src/lib.rs | 40 | ||||
| -rw-r--r-- | tests/pathological.rs | 70 |
2 files changed, 90 insertions, 20 deletions
@@ -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); }}; } diff --git a/tests/pathological.rs b/tests/pathological.rs new file mode 100644 index 0000000..d463e29 --- /dev/null +++ b/tests/pathological.rs @@ -0,0 +1,70 @@ +//! Testing corner cases. + +#![allow(non_upper_case_globals, non_snake_case)] // Macro conventions. + +use ral_registers as ral; + +mod periph { + #[repr(C)] + pub struct RegisterBlock { + /// Multi-dimensional arrays. + #[allow(clippy::type_complexity)] // Intentionally complex type. + pub DEEP_LEARNING: [[[[[[[[ral_registers::RWRegister<u32>; 1]; 2]; 3]; 4]; 5]; 6]; 7]; 8], + } + + pub mod DEEP_LEARNING { + pub mod GRADIENT { + pub const offset: u32 = 3; + pub const mask: u32 = 0x1F << offset; + pub mod R {} + pub mod W {} + pub mod RW {} + } + } + + pub struct ResetValues { + pub DEEP_LEARNING: u32, + } + + pub mod INST { + pub const reset: super::ResetValues = super::ResetValues { DEEP_LEARNING: 42 }; + } +} + +fn register_block() -> periph::RegisterBlock { + // Safety: bitpattern of zero is fine. + use std::mem::MaybeUninit; + unsafe { MaybeUninit::zeroed().assume_init() } +} + +#[test] +fn read_deep_array() { + let rb = register_block(); + rb.DEEP_LEARNING[7][6][5][4][3][2][1][0].write(u32::MAX); + let gradient = ral::read_reg!(periph, &rb, DEEP_LEARNING[7][6][5][4][3][2][1][0], GRADIENT); + assert_eq!(gradient, 0x1F); +} + +#[test] +fn write_deep_array() { + let rb = register_block(); + ral::write_reg!(periph, &rb, DEEP_LEARNING[7][6][5][4][3][2][1][0], 23); + assert_eq!(rb.DEEP_LEARNING[7][6][5][4][3][2][1][0].read(), 23); +} + +#[test] +fn modify_deep_array() { + let rb = register_block(); + ral::modify_reg!(periph, &rb, DEEP_LEARNING[7][6][5][4][3][2][1][0], GRADIENT: 42); + assert_eq!( + rb.DEEP_LEARNING[7][6][5][4][3][2][1][0].read(), + (42 & 0x1F) << 3 + ); +} + +#[test] +fn reset_deep_array() { + let rb = register_block(); + ral::reset_reg!(periph, &rb, INST, DEEP_LEARNING[7][6][5][4][3][2][1][0]); + assert_eq!(rb.DEEP_LEARNING[7][6][5][4][3][2][1][0].read(), 42); +} |
