diff --git a/examples/bare5.rs b/examples/bare5.rs index 7491e04e4c2f78e7f4ecaa4ac1b898b011f7e7f6..70c384be60e8923281e91f09d6332749482c8acf 100644 --- a/examples/bare5.rs +++ b/examples/bare5.rs @@ -57,12 +57,17 @@ mod stm32f40x { // width (field width) // value (new value that the field should take) // - // impl VolatileCell<u32> { - // #[inline(always)] - // pub fn modify(&self, offset: u8, width: u8, value: u32) { - // // your code here - // } - // } + impl VolatileCell<u32> { + #[inline(always)] + pub fn modify(&self, offset: u8, width: u8, value: u32) { + let currentVal = self.read(); + let b = 0b11111111; + let mask = !( (b << offset + width) | !(b << offset) ); + let maskedVal = mask & (value << offset); + let modVal = (currentVal & !mask) | maskedVal; + self.write(modVal); + } + } #[repr(C)] #[allow(non_snake_case)] @@ -141,27 +146,27 @@ fn wait(i: u32) { } // simple test of Your `modify` -//fn test() { -// let t:VolatileCell<u32> = unsafe { core::mem::uninitialized() }; -// t.write(0); -// assert!(t.read() == 0); -// t.modify(3, 3, 0b10101); -// // -// // 10101 -// // ..0111000 -// // --------- -// // 000101000 -// assert!(t.read() == 0b101 << 3); -// t.modify(4, 3, 0b10001); -// // 000101000 -// // 111 -// // 001 -// // 000011000 -// assert!(t.read() == 0b011 << 3); +fn test() { +let t:VolatileCell<u32> = unsafe { core::mem::uninitialized() }; +t.write(0); +assert!(t.read() == 0); +t.modify(3, 3, 0b10101); +// +// 10101 +// ..0111000 +// --------- +// 000101000 +assert!(t.read() == 0b101 << 3); +t.modify(4, 3, 0b10001); +// 000101000 +// 111 +// 001 +// 000011000 +assert!(t.read() == 0b011 << 3); // if << is used, your code will panic in dev (debug), but not in release mode -// t.modify(32, 3, 1); -//} +t.modify(32, 3, 1); +} // system startup, can be hidden from the user #[entry]