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;
extern crate cortex_m;
use cortex_m_rt::entry;
use cortex_m_semihosting::hprintln;
// C like API...
mod stm32f40x {
......@@ -57,12 +59,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 mask: u32 = (1 << width) - 1;
let masked_value = value & mask;
let r = self.read() & !(mask << offset);
self.write(r | masked_value << offset);
}
}
#[repr(C)]
#[allow(non_snake_case)]
......@@ -141,27 +148,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);
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
hprintln!("{:?}", t.read()).unwrap();
hprintln!("{:?}", 0b101 << 3);
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 +178,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;
......@@ -192,7 +201,9 @@ fn idle(rcc: &mut RCC, gpioa: &mut GPIOA) {
// alternatively to set the bit high we can
// 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);
......@@ -201,7 +212,9 @@ fn idle(rcc: &mut RCC, gpioa: &mut GPIOA) {
// alternatively to clear the bit we can
// 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);
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment