Skip to content
Snippets Groups Projects
Commit 19008d4f authored by Anton Grahn's avatar Anton Grahn
Browse files

bare5_2

parent cbafba1b
No related branches found
No related tags found
No related merge requests found
......@@ -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;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment