summaryrefslogtreecommitdiff
path: root/src/hal/common.rs
diff options
context:
space:
mode:
authorJosh Rahm <joshuarahm@gmail.com>2022-12-13 16:45:49 -0700
committerJosh Rahm <joshuarahm@gmail.com>2022-12-13 16:45:49 -0700
commit540e0316aa425a8fbd126d31350dbe51fca92791 (patch)
treeabc938b9b9f21b7d2357a18272d84c263d767142 /src/hal/common.rs
parent8bb2d168044213ce9bd31a19efb6bab90f5c9722 (diff)
downloadstm32l4-rust-master.tar.gz
stm32l4-rust-master.tar.bz2
stm32l4-rust-master.zip
Experimental Gpio.HEADmaster
This rapidly blinks the sysled on the stm32. This shows the ability to manipulate memory mapped registers.
Diffstat (limited to 'src/hal/common.rs')
-rw-r--r--src/hal/common.rs25
1 files changed, 25 insertions, 0 deletions
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()
+ }
+}