Skip to content

Commit a02b816

Browse files
bors[bot]japaric
andcommitted
Merge #25
25: Global singletons r=therealprof a=japaric This PR depends on #24 r? @rust-embedded/resources @jamesmunns this is the global singleton pattern I mentioned some time ago on IRC Co-authored-by: Jorge Aparicio <jorge@japaric.io>
2 parents 2ba145c + ba1ceb2 commit a02b816

File tree

14 files changed

+318
-0
lines changed

14 files changed

+318
-0
lines changed

ci/script.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,21 @@ main() {
194194
popd
195195

196196
popd
197+
198+
# # Logging with symbols
199+
pushd singleton
200+
201+
pushd app
202+
diff dev.out \
203+
<(cargo run | xxd -p)
204+
diff dev.objdump \
205+
<(cargo objdump --bin app -- -t | grep '\.log')
206+
diff release.objdump \
207+
<(cargo objdump --bin app --release -- -t | grep LOGGER)
208+
edition_check
209+
popd
210+
211+
popd
197212
}
198213

199214
# checks that 2018 idioms are being used

ci/singleton/app/.cargo

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../logging/app2/.cargo

ci/singleton/app/Cargo.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[package]
2+
authors = ["Jorge Aparicio <jorge@japaric.io>"]
3+
edition = "2018"
4+
name = "app"
5+
version = "0.1.0"
6+
7+
[profile.release]
8+
codegen-units = 1
9+
lto = true
10+
11+
[dependencies]
12+
cortex-m = "0.5.7"
13+
cortex-m-semihosting = "0.3.1"
14+
log = { path = "../log" }
15+
rt = { path = "../rt" }

ci/singleton/app/dev.objdump

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
00000001 l .log 00000001 Goodbye
2+
00000000 l .log 00000001 Hello, world!

ci/singleton/app/dev.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0001

ci/singleton/app/release.objdump

Whitespace-only changes.

ci/singleton/app/src/main.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#![no_main]
2+
#![no_std]
3+
4+
use cortex_m::interrupt;
5+
use cortex_m_semihosting::{
6+
debug,
7+
hio::{self, HStdout},
8+
};
9+
10+
use log::{global_logger, log, GlobalLog};
11+
use rt::entry;
12+
13+
struct Logger;
14+
15+
global_logger!(Logger);
16+
17+
entry!(main);
18+
19+
fn main() -> ! {
20+
log!("Hello, world!");
21+
22+
log!("Goodbye");
23+
24+
debug::exit(debug::EXIT_SUCCESS);
25+
26+
loop {}
27+
}
28+
29+
impl GlobalLog for Logger {
30+
fn log(&self, address: u8) {
31+
// we use a critical section (`interrupt::free`) to make the access to the
32+
// `static mut` variable interrupt safe which is required for memory safety
33+
interrupt::free(|_| unsafe {
34+
static mut HSTDOUT: Option<HStdout> = None;
35+
36+
// lazy initialization
37+
if HSTDOUT.is_none() {
38+
HSTDOUT = Some(hio::hstdout()?);
39+
}
40+
41+
let hstdout = HSTDOUT.as_mut().unwrap();
42+
43+
hstdout.write_all(&[address])
44+
}).ok(); // `.ok()` = ignore errors
45+
}
46+
}

ci/singleton/log/Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "log"
3+
version = "0.1.0"
4+
authors = ["Jorge Aparicio <jorge@japaric.io>"]
5+
edition = "2018"
6+
7+
[dependencies]

ci/singleton/log/build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../logging/log/build.rs

ci/singleton/log/log.x

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../logging/log/log.x

0 commit comments

Comments
 (0)