From 540e0316aa425a8fbd126d31350dbe51fca92791 Mon Sep 17 00:00:00 2001 From: Josh Rahm Date: Tue, 13 Dec 2022 16:45:49 -0700 Subject: Experimental Gpio. This rapidly blinks the sysled on the stm32. This shows the ability to manipulate memory mapped registers. --- src/hal/common.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 src/hal/common.rs (limited to 'src/hal/common.rs') diff --git a/src/hal/common.rs b/src/hal/common.rs new file mode 100644 index 0000000..9b07c4e --- /dev/null +++ b/src/hal/common.rs @@ -0,0 +1,25 @@ +/* + * Sets bits in the register pointed to by 'reg'. + * + * reg: raw pointer to the register to manipulate. + * mask: the bits to change in the register. The binary of mask should follow + * the regex 0*1*0* (i.e. all the 1's should be contiguous). + * val: The value to write to the bits referenced by 'mask' + */ +pub fn regset(reg: *mut u32, mask: u32, val: u32) -> () { + unsafe { + reg.write_volatile((reg.read_volatile() & !mask) | (val << mask.trailing_zeros())); + } +} + +/* + * Returns bits in the register pointed to by 'reg'. + * + * reg: raw pointer to the register to read + * mask: the bits to retrieve. + */ +pub fn regget(reg: *mut u32, mask: u32) -> u32 { + unsafe { + (reg.read_volatile() & mask) >> mask.trailing_zeros() + } +} -- cgit