diff --git a/examples/bare5.rs b/examples/bare5.rs index 9dee1e8b17056684db8a1f4b97a1703dd1c23c04..8dca20d16007961d3ab2482ca753290f228024f7 100644 --- a/examples/bare5.rs +++ b/examples/bare5.rs @@ -9,16 +9,18 @@ #![no_std] #![no_main] -extern crate panic_halt; +extern crate panic_semihosting; extern crate cortex_m; use cortex_m_rt::entry; +use cortex_m_semihosting::{hprint, hprintln}; // C like API... mod stm32f40x { #[allow(dead_code)] use core::{cell, ptr}; + use cortex_m_semihosting::{hprint, hprintln}; #[rustfmt::skip] mod address { pub const PERIPH_BASE: u32 = 0x40000000; @@ -50,19 +52,31 @@ mod stm32f40x { } } - // modify (reads, modifies a field, and writes the volatile cell) - // - // parameters: - // 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 - // } - // } + //modify (reads, modifies a field, and writes the volatile cell) + + //parameters: + //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) { + unsafe{ + let mut mask: u32 = 1; + for i in 0..width{ + mask = mask | 1 << i; + } + mask = mask << offset; + let vaoff = (value << offset); + let vaoffmask = vaoff & mask; + let readinvmask = self.read() & !mask; + let finalval = readinvmask |vaoffmask; + hprintln!("{:#b}",finalval); + self.write(finalval); + } + } + } #[repr(C)] #[allow(non_snake_case)] @@ -140,28 +154,29 @@ 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); -//} +//simple test of Your `modify` +fn test() { + let t:VolatileCell<u32> = unsafe { core::mem::uninitialized() }; + t.write(0); + assert!(t.read() == 0); + hprintln!("{:?}", 123).unwrap(); + 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 +184,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 { continue;