From 7037ac0d66493978a3aa0b04752edd2d4288cacb Mon Sep 17 00:00:00 2001
From: Per <Per Lindgren>
Date: Sun, 21 Jan 2018 18:36:29 +0100
Subject: [PATCH] itm

---
 .settings/com.github.rustdt.ide.core.prefs |  2 +-
 Cargo.toml                                 |  8 ++++-
 Makefile                                   |  2 +-
 examples/bare0.rs                          | 36 +++++++++++++++++-----
 examples/bare2.rs                          |  1 +
 5 files changed, 38 insertions(+), 11 deletions(-)

diff --git a/.settings/com.github.rustdt.ide.core.prefs b/.settings/com.github.rustdt.ide.core.prefs
index cee1592..7262c58 100644
--- a/.settings/com.github.rustdt.ide.core.prefs
+++ b/.settings/com.github.rustdt.ide.core.prefs
@@ -1,4 +1,4 @@
-build_targets=<?xml version\="1.0" encoding\="UTF-8" standalone\="no"?>\n<build_targets xmlns\="com.github.rustdt.ide.core">\n<target auto_enabled\="false" config\="build" n_enabled\="false" version2\="true">\n<command_invocation append_env\="true" command_arguments\=" xargo build --example bare0&\#10;">\n<env_vars/>\n</command_invocation>\n</target>\n<target auto_enabled\="false" config\="check" n_enabled\="false" version2\="true">\n<command_invocation append_env\="true" command_arguments\="xargo build --example bare0&\#10;">\n<env_vars/>\n</command_invocation>\n</target>\n<target auto_enabled\="false" config\="clean" n_enabled\="false" version2\="true"/>\n</build_targets>\n
+build_targets=<?xml version\="1.0" encoding\="UTF-8" standalone\="no"?>\n<build_targets xmlns\="com.github.rustdt.ide.core">\n<target auto_enabled\="false" config\="build" n_enabled\="false" version2\="true">\n<command_invocation append_env\="true" command_arguments\="xargo build --examples --message-format\=json">\n<env_vars/>\n</command_invocation>\n</target>\n<target auto_enabled\="false" config\="check" n_enabled\="false" version2\="true">\n<command_invocation append_env\="true" command_arguments\="xargo check --examples --message-format\=json&\#10;">\n<env_vars/>\n</command_invocation>\n</target>\n<target auto_enabled\="false" config\="clean" n_enabled\="false" version2\="true"/>\n</build_targets>\n
 eclipse.preferences.version=1
 format_onSave=true
 racer_path=/home/pln/.cargo/bin/racer
diff --git a/Cargo.toml b/Cargo.toml
index 4fa2c4f..587c1ed 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -13,10 +13,16 @@ version = "0.1.0"
 
 [dependencies]
 cortex-m-rtfm = "0.2.2"
-cortex-m = "0.3.1"
+#cortex-m = "0.3.1"
 rtfm-core = "0.1.0"
 cortex-m-semihosting = "0.2.0"
 
+[dependencies.cortex-m]
+version = "0.3.2"
+git = "https://gitlab.henriktjader.com/pln/cortex-m-fork.git"
+#path = "../cortex-m"
+branch = "itm"
+
 [dependencies.cortex-m-debug]
 version = "0.1.0"
 git = "https://gitlab.henriktjader.com/pln/cortex-m-debug.git"
diff --git a/Makefile b/Makefile
index f328ca8..519294b 100644
--- a/Makefile
+++ b/Makefile
@@ -1,5 +1,5 @@
 
-TARGETS = bare0 bare1 bare2 bare3 bare4 bare5 bare6
+TARGETS = bare0 bare1 bare2 bare3 bare4 bare5 bare6 clk_out_itm
 DIR = target/thumbv7em-none-eabihf/debug/examples/
 ELFS = $(addprefix $(DIR), $(TARGETS))
 EELFS = $(addsuffix .elf, $(ELFS))
diff --git a/examples/bare0.rs b/examples/bare0.rs
index cb4eb7e..e562f8b 100644
--- a/examples/bare0.rs
+++ b/examples/bare0.rs
@@ -8,23 +8,43 @@
 
 // Minimal runtime / startup for Cortex-M microcontrollers
 extern crate cortex_m_rt;
+use core::cell::UnsafeCell;
 
 const X_INIT: u32 = 10;
 
-static mut X: u32 = X_INIT;
-static mut Y: u32 = 0;
+struct U32 {
+    data: UnsafeCell<u32>,
+}
+unsafe impl Sync for U32 {}
+
+static X: U32 = U32 {
+    data: UnsafeCell::new(X_INIT),
+};
+
+static Y: U32 = U32 {
+    data: UnsafeCell::new(0),
+};
+
+impl U32 {
+    fn read(&self) -> u32 {
+        unsafe { *self.data.get() }
+    }
+
+    fn write(&self, v: u32) {
+        unsafe { *self.data.get() = v }
+    }
+}
 
 #[inline(never)]
 fn main() {
-    let mut x = unsafe { X };
+    let mut x = X.read();
 
     loop {
         x += 1;
-        unsafe {
-            X += 1;
-            Y = X;
-            assert!(x == X && X == Y);
-        }
+
+        X.write(X.read() + 1);
+        Y.write(X.read());
+        assert!(x == X.read() && X.read() == Y.read());
     }
 }
 // 1. run the program in the debugger,
diff --git a/examples/bare2.rs b/examples/bare2.rs
index bf49b92..bb417c3 100644
--- a/examples/bare2.rs
+++ b/examples/bare2.rs
@@ -19,6 +19,7 @@ fn wait(i: u32) {
 
 fn main() {
     ipln!("Start");
+    // sprintln!("Start");
 
     // a "mutable" (changeable) variable
     let mut s = 0;
-- 
GitLab