Skip to content
Snippets Groups Projects
Commit 71c9a661 authored by Henrik Theolin's avatar Henrik Theolin
Browse files

bare5_2

parent b81a5d3f
No related branches found
No related tags found
No related merge requests found
...@@ -56,13 +56,17 @@ mod stm32f40x { ...@@ -56,13 +56,17 @@ mod stm32f40x {
// offset (field offset) // offset (field offset)
// width (field width) // width (field width)
// value (new value that the field should take) // value (new value that the field should take)
// impl VolatileCell<u32> {
// impl VolatileCell<u32> { #[inline(always)]
// #[inline(always)] pub fn modify(&self, offset: u8, width: u8, value: u32) {
// pub fn modify(&self, offset: u8, width: u8, value: u32) { let current_value = self.read();
// // your code here 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)] #[repr(C)]
#[allow(non_snake_case)] #[allow(non_snake_case)]
...@@ -141,27 +145,27 @@ fn wait(i: u32) { ...@@ -141,27 +145,27 @@ fn wait(i: u32) {
} }
// simple test of Your `modify` // simple test of Your `modify`
//fn test() { fn test() {
// let t:VolatileCell<u32> = unsafe { core::mem::uninitialized() }; let t:VolatileCell<u32> = unsafe { core::mem::uninitialized() };
// t.write(0); t.write(0);
// assert!(t.read() == 0); assert!(t.read() == 0);
// t.modify(3, 3, 0b10101); t.modify(3, 3, 0b10101);
// // //
// // 10101 // 10101
// // ..0111000 // ..0111000
// // --------- // ---------
// // 000101000 // 000101000
// assert!(t.read() == 0b101 << 3); assert!(t.read() == 0b101 << 3);
// t.modify(4, 3, 0b10001); t.modify(4, 3, 0b10001);
// // 000101000 // 000101000
// // 111 // 111
// // 001 // 001
// // 000011000 // 000011000
// assert!(t.read() == 0b011 << 3); assert!(t.read() == 0b011 << 3);
//if << is used, your code will panic in dev (debug), but not in release mode //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 // system startup, can be hidden from the user
#[entry] #[entry]
...@@ -177,24 +181,18 @@ fn main() -> ! { ...@@ -177,24 +181,18 @@ fn main() -> ! {
// user application // user application
fn idle(rcc: &mut RCC, gpioa: &mut GPIOA) { fn idle(rcc: &mut RCC, gpioa: &mut GPIOA) {
// power on GPIOA // power on GPIOA
let r = rcc.AHB1ENR.read(); // read rcc.AHB1ENR.modify(0, 1, 0b1);
rcc.AHB1ENR.write(r | 1 << (0)); // set enable
// configure PA5 as output // configure PA5 as output
let r = gpioa.MODER.read() & !(0b11 << (5 * 2)); // read and mask gpioa.MODER.modify(5*2, 2, 0b01);
gpioa.MODER.write(r | 0b01 << (5 * 2)); // set output mode
loop { loop {
// set PA5 high // set PA5 high
// gpioa.BSRRH.write(1 << 5); // set bit, output hight (turn on led) gpioa.ODR.modify(5, 1, 0b1);
gpioa.ODR.write(gpioa.ODR.read() | (1 << 5)); //Modify wait by the speedup of a release build.
wait(1_119_982);
wait(10_000);
// set PA5 low // set PA5 low
// gpioa.BSRRL.write(1 << 5); // clear bit, output low (turn off led) gpioa.ODR.modify(5, 1, 0b0);
gpioa.ODR.write(gpioa.ODR.read() & !(1 << 5)); wait(1_119_982);
wait(10_000);
} }
} }
...@@ -227,7 +225,7 @@ fn idle(rcc: &mut RCC, gpioa: &mut GPIOA) { ...@@ -227,7 +225,7 @@ fn idle(rcc: &mut RCC, gpioa: &mut GPIOA) {
// - field width (in bits, u8), // - field width (in bits, u8),
// - and value (u32). // - 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. // Change the code into using your new API.
// //
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment