aboutsummaryrefslogtreecommitdiff
path: root/cortex-m-semihosting/src
diff options
context:
space:
mode:
authorGravatar Mara Bos <m-ou.se@m-ou.se> 2020-10-14 00:50:01 +0100
committerGravatar Adam Greig <adam@adamgreig.com> 2020-10-14 00:50:01 +0100
commit320881e0b57d16a4f768a7c8543571e26113e904 (patch)
treedd59b1f1eec5f8a5038ec4b34fc0e94982c9cc0c /cortex-m-semihosting/src
parent432e5f527430394570d1d07454e26cc41d5b2936 (diff)
downloadcortex-m-320881e0b57d16a4f768a7c8543571e26113e904.tar.gz
cortex-m-320881e0b57d16a4f768a7c8543571e26113e904.tar.zst
cortex-m-320881e0b57d16a4f768a7c8543571e26113e904.zip
Merge HStdout and HStderr into the same type
This commit copies https://github.com/rust-embedded/cortex-m-semihosting/pull/41 into the cortex-m repository.
Diffstat (limited to 'cortex-m-semihosting/src')
-rw-r--r--cortex-m-semihosting/src/export.rs6
-rw-r--r--cortex-m-semihosting/src/hio.rs39
-rw-r--r--cortex-m-semihosting/src/lib.rs8
3 files changed, 15 insertions, 38 deletions
diff --git a/cortex-m-semihosting/src/export.rs b/cortex-m-semihosting/src/export.rs
index c188ab0..0bbd09f 100644
--- a/cortex-m-semihosting/src/export.rs
+++ b/cortex-m-semihosting/src/export.rs
@@ -4,9 +4,9 @@ use core::fmt::{self, Write};
use cortex_m::interrupt;
-use crate::hio::{self, HStderr, HStdout};
+use crate::hio::{self, HostStream};
-static mut HSTDOUT: Option<HStdout> = None;
+static mut HSTDOUT: Option<HostStream> = None;
pub fn hstdout_str(s: &str) {
let _result = interrupt::free(|_| unsafe {
@@ -28,7 +28,7 @@ pub fn hstdout_fmt(args: fmt::Arguments) {
});
}
-static mut HSTDERR: Option<HStderr> = None;
+static mut HSTDERR: Option<HostStream> = None;
pub fn hstderr_str(s: &str) {
let _result = interrupt::free(|_| unsafe {
diff --git a/cortex-m-semihosting/src/hio.rs b/cortex-m-semihosting/src/hio.rs
index 61ac749..b0ca2fb 100644
--- a/cortex-m-semihosting/src/hio.rs
+++ b/cortex-m-semihosting/src/hio.rs
@@ -3,64 +3,45 @@
use core::{fmt, slice};
use crate::nr;
-/// Host's standard error
+/// A byte stream to the host (e.g., host's stdout or stderr).
#[derive(Clone, Copy)]
-pub struct HStderr {
+pub struct HostStream {
fd: usize,
}
-impl HStderr {
+impl HostStream {
/// Attempts to write an entire `buffer` into this sink
pub fn write_all(&mut self, buffer: &[u8]) -> Result<(), ()> {
write_all(self.fd, buffer)
}
}
-impl fmt::Write for HStderr {
- fn write_str(&mut self, s: &str) -> fmt::Result {
- self.write_all(s.as_bytes()).map_err(|_| fmt::Error)
- }
-}
-
-/// Host's standard output
-#[derive(Clone, Copy)]
-pub struct HStdout {
- fd: usize,
-}
-
-impl HStdout {
- /// Attempts to write an entire `buffer` into this sink
- pub fn write_all(&mut self, buffer: &[u8]) -> Result<(), ()> {
- write_all(self.fd, buffer)
- }
-}
-
-impl fmt::Write for HStdout {
+impl fmt::Write for HostStream {
fn write_str(&mut self, s: &str) -> fmt::Result {
self.write_all(s.as_bytes()).map_err(|_| fmt::Error)
}
}
/// Construct a new handle to the host's standard error.
-pub fn hstderr() -> Result<HStderr, ()> {
+pub fn hstderr() -> Result<HostStream, ()> {
// There is actually no stderr access in ARM Semihosting documentation. Use
// convention used in libgloss.
// See: libgloss/arm/syscalls.c, line 139.
// https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;a=blob;f=libgloss/arm/syscalls.c#l139
- open(":tt\0", nr::open::W_APPEND).map(|fd| HStderr { fd })
+ open(":tt\0", nr::open::W_APPEND)
}
/// Construct a new handle to the host's standard output.
-pub fn hstdout() -> Result<HStdout, ()> {
- open(":tt\0", nr::open::W_TRUNC).map(|fd| HStdout { fd })
+pub fn hstdout() -> Result<HostStream, ()> {
+ open(":tt\0", nr::open::W_TRUNC)
}
-fn open(name: &str, mode: usize) -> Result<usize, ()> {
+fn open(name: &str, mode: usize) -> Result<HostStream, ()> {
let name = name.as_bytes();
match unsafe { syscall!(OPEN, name.as_ptr(), mode, name.len() - 1) } as
isize {
-1 => Err(()),
- fd => Ok(fd as usize),
+ fd => Ok(HostStream { fd: fd as usize }),
}
}
diff --git a/cortex-m-semihosting/src/lib.rs b/cortex-m-semihosting/src/lib.rs
index b70dea0..de52ae1 100644
--- a/cortex-m-semihosting/src/lib.rs
+++ b/cortex-m-semihosting/src/lib.rs
@@ -25,7 +25,7 @@
//!
//! # Example
//!
-//! ## Using `hio::HStdout`
+//! ## Using `hio::hstdout`
//!
//! This example will demonstrate how to print formatted strings.
//!
@@ -35,11 +35,7 @@
//!
//! // This function will be called by the application
//! fn print() -> Result<(), core::fmt::Error> {
-//! let mut stdout = match hio::hstdout() {
-//! Ok(fd) => fd,
-//! Err(()) => return Err(core::fmt::Error),
-//! };
-//!
+//! let mut stdout = hio::hstdout().map_err(|_| core::fmt::Error)?;
//! let language = "Rust";
//! let ranking = 1;
//!