Skip to content
Snippets Groups Projects
Commit 1efbfa56 authored by Gustav Hansson's avatar Gustav Hansson
Browse files

bare5_2

parent 8cfdde2e
No related branches found
No related tags found
No related merge requests found
...@@ -13,6 +13,8 @@ extern crate panic_halt; ...@@ -13,6 +13,8 @@ extern crate panic_halt;
extern crate cortex_m; extern crate cortex_m;
use cortex_m_rt::entry; use cortex_m_rt::entry;
use cortex_m_semihosting::hprintln;
// C like API... // C like API...
mod stm32f40x { mod stm32f40x {
...@@ -57,12 +59,17 @@ mod stm32f40x { ...@@ -57,12 +59,17 @@ mod stm32f40x {
// 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) {
// // your code here let mask: u32 = (1 << width) - 1;
// } let masked_value = value & mask;
// }
let r = self.read() & !(mask << offset);
self.write(r | masked_value << offset);
}
}
#[repr(C)] #[repr(C)]
#[allow(non_snake_case)] #[allow(non_snake_case)]
...@@ -141,27 +148,29 @@ fn wait(i: u32) { ...@@ -141,27 +148,29 @@ 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); hprintln!("{:?}", t.read()).unwrap();
// t.modify(4, 3, 0b10001); hprintln!("{:?}", 0b101 << 3);
// // 000101000 assert!(t.read() == 0b101 << 3);
// // 111 t.modify(4, 3, 0b10001);
// // 001 // 000101000
// // 000011000 // 111
// assert!(t.read() == 0b011 << 3); // 001
// 000011000
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]
...@@ -169,7 +178,7 @@ fn main() -> ! { ...@@ -169,7 +178,7 @@ fn main() -> ! {
let rcc = unsafe { &mut *RCC::get() }; // get the reference to RCC in memory 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 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); idle(rcc, gpioa);
loop { loop {
continue; continue;
...@@ -192,7 +201,9 @@ fn idle(rcc: &mut RCC, gpioa: &mut GPIOA) { ...@@ -192,7 +201,9 @@ fn idle(rcc: &mut RCC, gpioa: &mut GPIOA) {
// alternatively to set the bit high we can // alternatively to set the bit high we can
// read the value, or with PA5 (bit 5) and write back // read the value, or with PA5 (bit 5) and write back
gpioa.ODR.write(gpioa.ODR.read() | (1 << 5)); // gpioa.ODR.write(gpioa.ODR.read() | (1 << 5));
gpioa.ODR.modify(5, 1, 1);
wait(10_000); wait(10_000);
...@@ -201,7 +212,9 @@ fn idle(rcc: &mut RCC, gpioa: &mut GPIOA) { ...@@ -201,7 +212,9 @@ fn idle(rcc: &mut RCC, gpioa: &mut GPIOA) {
// alternatively to clear the bit we can // alternatively to clear the bit we can
// read the value, mask out PA5 (bit 5) and write back // read the value, mask out PA5 (bit 5) and write back
gpioa.ODR.write(gpioa.ODR.read() & !(1 << 5)); // gpioa.ODR.write(gpioa.ODR.read() & !(1 << 5));
gpioa.ODR.modify(5, 1, 0);
wait(10_000); wait(10_000);
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment