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

Commit 687f3b5

Browse files
author
Brandon Matthews
committed
Add example of HStdout usage
1 parent f168b56 commit 687f3b5

File tree

1 file changed

+45
-10
lines changed

1 file changed

+45
-10
lines changed

src/lib.rs

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77
//!
88
//! # Interface
99
//!
10+
//! This crate provides implementations of
11+
//! [`core::fmt::Write`](https://doc.rust-lang.org/core/fmt/trait.Write.html), so you can use it,
12+
//! in conjunction with
13+
//! [`core::format_args!`](https://doc.rust-lang.org/core/macro.format_args.html), for user-friendly
14+
//! construction and printing of formatted strings.
15+
//!
1016
//! Since semihosting operations are modeled as [system calls][sc], this crate exposes an untyped
1117
//! `syscall!` interface just like the [`sc`] crate does.
1218
//!
@@ -25,22 +31,30 @@
2531
//!
2632
//! # Example
2733
//!
28-
//! This example will show how to print "Hello, world!" on the host.
34+
//! ## Using `hio::HStdout`
2935
//!
30-
//! Target program:
36+
//! This example will demonstrate how to print formatted strings.
3137
//!
32-
//! ```
38+
//! ```rust
3339
//! #[macro_use]
3440
//! extern crate cortex_m_semihosting;
3541
//!
42+
//! use cortex_m_semihosting::hio;
43+
//! use core::fmt::Write;
44+
//!
3645
//! // This function will be called by the application
37-
//! fn print() {
38-
//! // File descriptor (on the host)
39-
//! const STDOUT: usize = 1; // NOTE the host stdout may not always be fd 1
40-
//! static MSG: &'static [u8] = b"Hello, world!\n";
46+
//! fn print() -> Result<(), core::fmt::Error> {
47+
//! let mut stdout = match hio::hstdout() {
48+
//! Ok(fd) => fd,
49+
//! Err(()) => return Err(core::fmt::Error),
50+
//! };
4151
//!
42-
//! // Signature: fn write(fd: usize, ptr: *const u8, len: usize) -> usize
43-
//! let r = unsafe { syscall!(WRITE, STDOUT, MSG.as_ptr(), MSG.len()) };
52+
//! let language = "Rust";
53+
//! let ranking = 1;
54+
//!
55+
//! stdout.write_fmt(format_args!("{} on embedded is #{}!", language, ranking))?;
56+
//!
57+
//! Ok(())
4458
//! }
4559
//! ```
4660
//!
@@ -93,8 +107,29 @@
93107
//! ``` text
94108
//! # openocd -f $INTERFACE -f $TARGET -l /tmp/openocd.log
95109
//! (..)
96-
//! Hello, world!
110+
//! Rust on embedded is #1!
111+
//! ```
112+
//! ## Using the syscall interface
113+
//!
114+
//! This example will show how to print "Hello, world!" on the host.
115+
//!
116+
//! Target program:
117+
//!
118+
//! ```
119+
//! #[macro_use]
120+
//! extern crate cortex_m_semihosting;
121+
//!
122+
//! // This function will be called by the application
123+
//! fn print() {
124+
//! // File descriptor (on the host)
125+
//! const STDOUT: usize = 1; // NOTE the host stdout may not always be fd 1
126+
//! static MSG: &'static [u8] = b"Hello, world!\n";
127+
//!
128+
//! // Signature: fn write(fd: usize, ptr: *const u8, len: usize) -> usize
129+
//! let r = unsafe { syscall!(WRITE, STDOUT, MSG.as_ptr(), MSG.len()) };
130+
//! }
97131
//! ```
132+
//! Output and monitoring proceed as in the above example.
98133
//!
99134
//! # Optional features
100135
//!

0 commit comments

Comments
 (0)