Skip to content

Commit 6fc7a46

Browse files
Update skip-data-init example
1 parent 6899a12 commit 6fc7a46

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

cortex-m-rt/examples/skip-data-init.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,23 @@
2020
#![no_main]
2121
#![no_std]
2222

23-
use panic_halt as _;
24-
23+
use core::sync::atomic::{AtomicU32, Ordering};
2524
use cortex_m_rt::entry;
25+
use cortex_m_semihosting::hprintln;
26+
use panic_halt as _;
2627

27-
static mut COUNTER: u32 = 42;
28+
static COUNTER: AtomicU32 = AtomicU32::new(42);
2829

2930
#[entry]
3031
fn main() -> ! {
31-
unsafe {
32-
COUNTER += 1;
32+
let prev = COUNTER.fetch_add(1, Ordering::Relaxed);
33+
let new = COUNTER.load(Ordering::Relaxed);
34+
35+
hprintln!("Previous counter value: {}", prev);
36+
hprintln!("New counter value: {}", new);
37+
38+
if prev != 42 || new != 43 {
39+
panic!("Unexpected COUNTER value! Data section may not be initialized.");
3340
}
3441

3542
loop {

0 commit comments

Comments
 (0)