From 71c9a66129f2cf925cb743470350d450442cb6e0 Mon Sep 17 00:00:00 2001 From: "henthe-5@student.ltu.se" <henthe-5@student.ltu.se> Date: Fri, 22 Feb 2019 13:04:21 +0100 Subject: [PATCH] bare5_2 --- examples/bare5.rs | 84 +++++++++++++++++++++++------------------------ 1 file changed, 41 insertions(+), 43 deletions(-) diff --git a/examples/bare5.rs b/examples/bare5.rs index be4f637..89215af 100644 --- a/examples/bare5.rs +++ b/examples/bare5.rs @@ -56,13 +56,17 @@ mod stm32f40x { // offset (field offset) // 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 current_value = self.read(); + let b = 0b11111111; + let mask = !( (b << offset + width) | !(b << offset) ); + let masked_value = mask & (value << offset); + let modified_value = (current_value & !mask) | masked_value; + self.write(modified_value); + } + } #[repr(C)] #[allow(non_snake_case)] @@ -141,27 +145,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); - -// if << is used, your code will panic in dev (debug), but not in release mode -// t.modify(32, 3, 1); -//} +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); +} // system startup, can be hidden from the user #[entry] @@ -169,7 +173,7 @@ fn main() -> ! { let rcc = unsafe { &mut *RCC::get() }; // get the reference to RCC in memory let gpioa = unsafe { &mut *GPIOA::get() }; // get the reference to GPIOA in memory - // test(); // uncomment to run test + //test(); // uncomment to run test idle(rcc, gpioa); loop {} } @@ -177,24 +181,18 @@ fn main() -> ! { // user application fn idle(rcc: &mut RCC, gpioa: &mut GPIOA) { // power on GPIOA - let r = rcc.AHB1ENR.read(); // read - rcc.AHB1ENR.write(r | 1 << (0)); // set enable - + rcc.AHB1ENR.modify(0, 1, 0b1); // configure PA5 as output - let r = gpioa.MODER.read() & !(0b11 << (5 * 2)); // read and mask - gpioa.MODER.write(r | 0b01 << (5 * 2)); // set output mode - + gpioa.MODER.modify(5*2, 2, 0b01); loop { // set PA5 high - // gpioa.BSRRH.write(1 << 5); // set bit, output hight (turn on led) - gpioa.ODR.write(gpioa.ODR.read() | (1 << 5)); - - wait(10_000); + gpioa.ODR.modify(5, 1, 0b1); + //Modify wait by the speedup of a release build. + wait(1_119_982); // set PA5 low - // gpioa.BSRRL.write(1 << 5); // clear bit, output low (turn off led) - gpioa.ODR.write(gpioa.ODR.read() & !(1 << 5)); - wait(10_000); + gpioa.ODR.modify(5, 1, 0b0); + wait(1_119_982); } } @@ -227,7 +225,7 @@ fn idle(rcc: &mut RCC, gpioa: &mut GPIOA) { // - field width (in bits, u8), // - and value (u32). // -// Implement and check that running `test` gives you expected behavior. +// Implement and` gives you expected behavior. // // Change the code into using your new API. // -- GitLab