diff --git a/examples/bare0.rs b/examples/bare0.rs index d7f0c54ae250e8c113672bce79960560da9124f1..3e97d3c294dd30f46eb9d06e014499cdf196f4cc 100644 --- a/examples/bare0.rs +++ b/examples/bare0.rs @@ -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() ); + 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 read_x() -> u32 { - return unsafe { X }; -} - -fn read_y() -> u32 { - return unsafe { Y }; +fn read_u32(uint: &U32) -> u32 { + return unsafe{ *uint.data.get()}; } -fn write_x(x: u32) { - unsafe { - X = x; - } -} - -fn write_y(y: u32) { - unsafe { - Y = y - } - +fn write_u32(write_to: &U32, value: u32) { + unsafe{ *write_to.data.get() = value }; } // 0. Compile/build the example in debug (dev) mode.