|
7 | 7 | //!
|
8 | 8 | //! # Interface
|
9 | 9 | //!
|
| 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 | +//! |
10 | 15 | //! Since semihosting operations are modeled as [system calls][sc], this crate exposes an untyped
|
11 | 16 | //! `syscall!` interface just like the [`sc`] crate does.
|
12 | 17 | //!
|
|
25 | 30 | //!
|
26 | 31 | //! # Example
|
27 | 32 | //!
|
28 |
| -//! This example will show how to print "Hello, world!" on the host. |
| 33 | +//! ## Using `hio::HStdout` |
29 | 34 | //!
|
30 |
| -//! Target program: |
| 35 | +//! This example will demonstrate how to print formatted strings. |
31 | 36 | //!
|
32 |
| -//! ``` |
33 |
| -//! #[macro_use] |
| 37 | +//! ```rust |
34 | 38 | //! extern crate cortex_m_semihosting;
|
35 | 39 | //!
|
| 40 | +//! use cortex_m_semihosting::hio; |
| 41 | +//! use core::fmt::Write; |
| 42 | +//! |
36 | 43 | //! // 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 | +//! }; |
41 | 49 | //!
|
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(()) |
44 | 56 | //! }
|
45 | 57 | //! ```
|
46 | 58 | //!
|
|
93 | 105 | //! ``` text
|
94 | 106 | //! # openocd -f $INTERFACE -f $TARGET -l /tmp/openocd.log
|
95 | 107 | //! (..)
|
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 | +//! } |
97 | 128 | //! ```
|
| 129 | +//! Output and monitoring proceed as in the above example. |
98 | 130 | //!
|
99 | 131 | //! # Optional features
|
100 | 132 | //!
|
|
0 commit comments