From a5850ab6d6c508fc3351fd86646bcf3fb1b69103 Mon Sep 17 00:00:00 2001 From: Ian McIntyre Date: Thu, 22 Sep 2022 13:49:23 -0400 Subject: 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; 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. --- tests/pathological.rs | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 tests/pathological.rs (limited to 'tests') 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; 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); +} -- cgit v1.2.3