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

add hprint macros #27

Merged
merged 1 commit into from
Nov 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ This project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

## [v0.3.2] - 2018-11-04

### Added

- Added a family of `hprint` macros for printing to the host standard output /
error via globally shared `HStdout` / `HStderr` handles .

## [v0.3.1] - 2018-08-27

### Changed
Expand Down
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ license = "MIT OR Apache-2.0"
name = "cortex-m-semihosting"
readme = "README.md"
repository = "https://github.com/japaric/cortex-m-semihosting"
version = "0.3.1"
version = "0.3.2"

[features]
inline-asm = []

[dependencies]
cortex-m = "0.5.8"
51 changes: 51 additions & 0 deletions src/export.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//! IMPLEMENTATION DETAILS USED BY MACROS

use core::fmt::{self, Write};

use cortex_m::interrupt;

use hio::{self, HStderr, HStdout};

static mut HSTDOUT: Option<HStdout> = None;

pub fn hstdout_str(s: &str) -> Result<(), ()> {
interrupt::free(|_| unsafe {
if HSTDOUT.is_none() {
HSTDOUT = Some(hio::hstdout()?);
}

HSTDOUT.as_mut().unwrap().write_str(s).map_err(drop)
})
}

pub fn hstdout_fmt(args: fmt::Arguments) -> Result<(), ()> {
interrupt::free(|_| unsafe {
if HSTDOUT.is_none() {
HSTDOUT = Some(hio::hstdout()?);
}

HSTDOUT.as_mut().unwrap().write_fmt(args).map_err(drop)
})
}

static mut HSTDERR: Option<HStderr> = None;

pub fn hstderr_str(s: &str) -> Result<(), ()> {
interrupt::free(|_| unsafe {
if HSTDERR.is_none() {
HSTDERR = Some(hio::hstderr()?);
}

HSTDERR.as_mut().unwrap().write_str(s).map_err(drop)
})
}

pub fn hstderr_fmt(args: fmt::Arguments) -> Result<(), ()> {
interrupt::free(|_| unsafe {
if HSTDERR.is_none() {
HSTDERR = Some(hio::hstderr()?);
}

HSTDERR.as_mut().unwrap().write_fmt(args).map_err(drop)
})
}
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,14 @@
#![deny(warnings)]
#![no_std]

extern crate cortex_m;

#[macro_use]
mod macros;

pub mod debug;
#[doc(hidden)]
pub mod export;
pub mod hio;
pub mod nr;

Expand Down
58 changes: 58 additions & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,61 @@ macro_rules! syscall1 {
$crate::syscall1($crate::nr::$nr, $a1 as usize)
};
}

/// Macro for printing to the HOST standard output
///
/// This macro returns a `Result<(), ()>` value
#[macro_export]
macro_rules! hprint {
($s:expr) => {
$crate::export::hstdout_str($s)
};
($($tt:tt)*) => {
$crate::export::hstdout_fmt(format_args!($($tt)*))
};
}

/// Macro for printing to the HOST standard output, with a newline.
///
/// This macro returns a `Result<(), ()>` value
#[macro_export]
macro_rules! hprintln {
() => {
$crate::export::hstdout_str("\n")
};
($s:expr) => {
$crate::export::hstdout_str(concat!($s, "\n"))
};
($s:expr, $($tt:tt)*) => {
$crate::export::hstdout_fmt(format_args!(concat!($s, "\n"), $($tt)*))
};
}

/// Macro for printing to the HOST standard error
///
/// This macro returns a `Result<(), ()>` value
#[macro_export]
macro_rules! heprint {
($s:expr) => {
$crate::export::hstderr_str($s)
};
($($tt:tt)*) => {
$crate::export::hstderr_fmt(format_args!($($tt)*))
};
}

/// Macro for printing to the HOST standard error, with a newline.
///
/// This macro returns a `Result<(), ()>` value
#[macro_export]
macro_rules! heprintln {
() => {
$crate::export::hstderr_str("\n")
};
($s:expr) => {
$crate::export::hstderr_str(concat!($s, "\n"))
};
($s:expr, $($tt:tt)*) => {
$crate::export::hstderr_fmt(format_args!(concat!($s, "\n"), $($tt)*))
};
}