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

bare0_6

parent c08dd88b
No related branches found
No related tags found
No related merge requests found
......@@ -18,45 +18,43 @@ extern crate panic_halt;
// Minimal runtime / startup for Cortex-M microcontrollers
use cortex_m_rt::entry;
use core::cell::UnsafeCell;
struct U32 {
data: UnsafeCell<u32>,
}
// a constant (cannot be changed at run-time)
const X_INIT: u32 = 0xffffffff;
// global mutabale variables (changed using unsafe code)
static mut X: u32 = X_INIT;
static mut Y: u32 = 0;
static X: U32 = U32 {
data: UnsafeCell::new(X_INIT),
};
static Y: U32 = U32 {
data: UnsafeCell::new(0),
};
unsafe impl Sync for U32 {}
#[entry]
fn main() -> ! {
// local mutabale variable (changed in safe code)
let mut x = read_x();
let mut x = read_u32(&X);
loop {
x = x.wrapping_add(1); // <- place breakpoint here (3)
write_x(read_x().wrapping_add(1));
write_y(read_x());
assert!(x == read_x() && read_x() == read_y() );
}
}
fn read_x() -> u32 {
return unsafe { X };
}
fn read_y() -> u32 {
return unsafe { Y };
}
fn write_x(x: u32) {
unsafe {
X = x;
write_u32(&X, read_u32(&X).wrapping_add(1) );
write_u32(&Y, read_u32(&X));
assert!(x == read_u32(&X) && read_u32(&X) == read_u32(&Y) );
}
}
fn write_y(y: u32) {
unsafe {
Y = y
fn read_u32(uint: &U32) -> u32 {
return unsafe{ *uint.data.get()};
}
fn write_u32(write_to: &U32, value: u32) {
unsafe{ *write_to.data.get() = value };
}
// 0. Compile/build the example in debug (dev) mode.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment