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

Commit 05ace9e

Browse files
bors[bot]Brandon Matthews
and
Brandon Matthews
committed
Merge #19
19: Add example of HStdout usage r=japaric a=thenewwazoo Simple enough - I put it at the top since I figure most people will probably be looking for the easy stuff first and the nuts-and-bolts later. Co-authored-by: Brandon Matthews <[email protected]>
2 parents f168b56 + c4a768c commit 05ace9e

File tree

1 file changed

+43
-11
lines changed

1 file changed

+43
-11
lines changed

src/lib.rs

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
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) or the [`write!` macro](https://doc.rust-lang.org/core/macro.write.html), for user-friendly construction and printing of formatted strings.
14+
//!
1015
//! Since semihosting operations are modeled as [system calls][sc], this crate exposes an untyped
1116
//! `syscall!` interface just like the [`sc`] crate does.
1217
//!
@@ -25,22 +30,29 @@
2530
//!
2631
//! # Example
2732
//!
28-
//! This example will show how to print "Hello, world!" on the host.
33+
//! ## Using `hio::HStdout`
2934
//!
30-
//! Target program:
35+
//! This example will demonstrate how to print formatted strings.
3136
//!
32-
//! ```
33-
//! #[macro_use]
37+
//! ```rust
3438
//! extern crate cortex_m_semihosting;
3539
//!
40+
//! use cortex_m_semihosting::hio;
41+
//! use core::fmt::Write;
42+
//!
3643
//! // 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";
44+
//! fn print() -> Result<(), core::fmt::Error> {
45+
//! let mut stdout = match hio::hstdout() {
46+
//! Ok(fd) => fd,
47+
//! Err(()) => return Err(core::fmt::Error),
48+
//! };
4149
//!
42-
//! // Signature: fn write(fd: usize, ptr: *const u8, len: usize) -> usize
43-
//! let r = unsafe { syscall!(WRITE, STDOUT, MSG.as_ptr(), MSG.len()) };
50+
//! let language = "Rust";
51+
//! let ranking = 1;
52+
//!
53+
//! write!(stdout, "{} on embedded is #{}!", language, ranking)?;
54+
//!
55+
//! Ok(())
4456
//! }
4557
//! ```
4658
//!
@@ -93,8 +105,28 @@
93105
//! ``` text
94106
//! # openocd -f $INTERFACE -f $TARGET -l /tmp/openocd.log
95107
//! (..)
96-
//! Hello, world!
108+
//! Rust on embedded is #1!
109+
//! ```
110+
//! ## Using the syscall interface
111+
//!
112+
//! This example will show how to print "Hello, world!" on the host.
113+
//!
114+
//! Target program:
115+
//!
116+
//! ```
117+
//! extern crate cortex_m_semihosting;
118+
//!
119+
//! // This function will be called by the application
120+
//! fn print() {
121+
//! // File descriptor (on the host)
122+
//! const STDOUT: usize = 1; // NOTE the host stdout may not always be fd 1
123+
//! static MSG: &'static [u8] = b"Hello, world!\n";
124+
//!
125+
//! // Signature: fn write(fd: usize, ptr: *const u8, len: usize) -> usize
126+
//! let r = unsafe { syscall!(WRITE, STDOUT, MSG.as_ptr(), MSG.len()) };
127+
//! }
97128
//! ```
129+
//! Output and monitoring proceed as in the above example.
98130
//!
99131
//! # Optional features
100132
//!

0 commit comments

Comments
 (0)