Skip to content
This repository was archived by the owner on Dec 2, 2020. It is now read-only.

Commit 7e554f9

Browse files
committed
add hprint macros
1 parent 4d81af0 commit 7e554f9

File tree

5 files changed

+124
-1
lines changed

5 files changed

+124
-1
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ This project adheres to [Semantic Versioning](http://semver.org/).
55

66
## [Unreleased]
77

8+
## [v0.3.2] - 2018-11-04
9+
10+
### Added
11+
12+
- Added a family of `hprint` macros for printing to the host standard output /
13+
error via globally shared `HStdout` / `HStderr` handles .
14+
815
## [v0.3.1] - 2018-08-27
916

1017
### Changed

Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ license = "MIT OR Apache-2.0"
77
name = "cortex-m-semihosting"
88
readme = "README.md"
99
repository = "https://github.com/japaric/cortex-m-semihosting"
10-
version = "0.3.1"
10+
version = "0.3.2"
1111

1212
[features]
1313
inline-asm = []
14+
15+
[dependencies]
16+
cortex-m = "0.5.8"

src/export.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//! IMPLEMENTATION DETAILS USED BY MACROS
2+
3+
use core::fmt::{self, Write};
4+
5+
use cortex_m::interrupt;
6+
7+
use hio::{self, HStderr, HStdout};
8+
9+
static mut HSTDOUT: Option<HStdout> = None;
10+
11+
pub fn hstdout_str(s: &str) -> Result<(), ()> {
12+
interrupt::free(|_| unsafe {
13+
if HSTDOUT.is_none() {
14+
HSTDOUT = Some(hio::hstdout()?);
15+
}
16+
17+
HSTDOUT.as_mut().unwrap().write_str(s).map_err(drop)
18+
})
19+
}
20+
21+
pub fn hstdout_fmt(args: fmt::Arguments) -> Result<(), ()> {
22+
interrupt::free(|_| unsafe {
23+
if HSTDOUT.is_none() {
24+
HSTDOUT = Some(hio::hstdout()?);
25+
}
26+
27+
HSTDOUT.as_mut().unwrap().write_fmt(args).map_err(drop)
28+
})
29+
}
30+
31+
static mut HSTDERR: Option<HStderr> = None;
32+
33+
pub fn hstderr_str(s: &str) -> Result<(), ()> {
34+
interrupt::free(|_| unsafe {
35+
if HSTDERR.is_none() {
36+
HSTDERR = Some(hio::hstderr()?);
37+
}
38+
39+
HSTDERR.as_mut().unwrap().write_str(s).map_err(drop)
40+
})
41+
}
42+
43+
pub fn hstderr_fmt(args: fmt::Arguments) -> Result<(), ()> {
44+
interrupt::free(|_| unsafe {
45+
if HSTDERR.is_none() {
46+
HSTDERR = Some(hio::hstderr()?);
47+
}
48+
49+
HSTDERR.as_mut().unwrap().write_fmt(args).map_err(drop)
50+
})
51+
}

src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,14 @@
147147
#![deny(warnings)]
148148
#![no_std]
149149

150+
extern crate cortex_m;
151+
150152
#[macro_use]
151153
mod macros;
152154

153155
pub mod debug;
156+
#[doc(hidden)]
157+
pub mod export;
154158
pub mod hio;
155159
pub mod nr;
156160

src/macros.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,61 @@ macro_rules! syscall1 {
2727
$crate::syscall1($crate::nr::$nr, $a1 as usize)
2828
};
2929
}
30+
31+
/// Macro for printing to the HOST standard output
32+
///
33+
/// This macro returns a `Result<(), ()>` value
34+
#[macro_export]
35+
macro_rules! hprint {
36+
($s:expr) => {
37+
$crate::export::hstdout_str($s)
38+
};
39+
($($tt:tt)*) => {
40+
$crate::export::hstdout_fmt(format_args!($($tt)*))
41+
};
42+
}
43+
44+
/// Macro for printing to the HOST standard output, with a newline.
45+
///
46+
/// This macro returns a `Result<(), ()>` value
47+
#[macro_export]
48+
macro_rules! hprintln {
49+
() => {
50+
$crate::export::hstdout_str("\n")
51+
};
52+
($s:expr) => {
53+
$crate::export::hstdout_str(concat!($s, "\n"))
54+
};
55+
($s:expr, $($tt:tt)*) => {
56+
$crate::export::hstdout_fmt(format_args!(concat!($s, "\n"), $($tt)*))
57+
};
58+
}
59+
60+
/// Macro for printing to the HOST standard error
61+
///
62+
/// This macro returns a `Result<(), ()>` value
63+
#[macro_export]
64+
macro_rules! heprint {
65+
($s:expr) => {
66+
$crate::export::hstderr_str($s)
67+
};
68+
($($tt:tt)*) => {
69+
$crate::export::hstderr_fmt(format_args!($($tt)*))
70+
};
71+
}
72+
73+
/// Macro for printing to the HOST standard error, with a newline.
74+
///
75+
/// This macro returns a `Result<(), ()>` value
76+
#[macro_export]
77+
macro_rules! heprintln {
78+
() => {
79+
$crate::export::hstderr_str("\n")
80+
};
81+
($s:expr) => {
82+
$crate::export::hstderr_str(concat!($s, "\n"))
83+
};
84+
($s:expr, $($tt:tt)*) => {
85+
$crate::export::hstderr_fmt(format_args!(concat!($s, "\n"), $($tt)*))
86+
};
87+
}

0 commit comments

Comments
 (0)