diff options
| author | Ian McIntyre <ianpmcintyre@gmail.com> | 2022-09-22 13:49:23 -0400 |
|---|---|---|
| committer | Ian McIntyre <ianpmcintyre@gmail.com> | 2022-12-07 09:42:54 -0500 |
| commit | a5850ab6d6c508fc3351fd86646bcf3fb1b69103 (patch) | |
| tree | 9e7aad64be5ac096b45096ffbf5cb0f3cb371346 /tests | |
| parent | 96bfa91e3bf81bc215524e8c5c02ee951339f702 (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 'tests')
| -rw-r--r-- | tests/pathological.rs | 70 |
1 files changed, 70 insertions, 0 deletions
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); +} |
