5050//!
5151//! # Overview
5252//!
53- //! Use [`Uart16550Tty`] for a quick start. For more fine-grained low-level
54- //! control, please have a look at [`Uart16550`] instead.
53+ //! Use [`Uart16550Tty`] for a quick start to write to a terminal via your
54+ //! serial connection. For more fine-grained low-level control, please have a
55+ //! look at [`Uart16550`] instead. The following examples show usage of the
56+ //! latter.
5557//!
56- //! # Example (Minimalistic)
58+ //! # Example (Minimal - x86 Port IO)
59+ //!
60+ #![ cfg_attr(
61+ any( target_arch = "x86" , target_arch = "x86_64" ) ,
62+ doc = "```rust,no_run"
63+ ) ]
64+ #![ cfg_attr(
65+ not( any( target_arch = "x86" , target_arch = "x86_64" ) ) ,
66+ doc = "```rust,ignore"
67+ ) ]
68+ //! use uart_16550::{Config, Uart16550};
69+ //!
70+ //! // SAFETY: The port is valid and we have exclusive access.
71+ //! let mut uart = unsafe { Uart16550::new_port(0x3f8).unwrap() };
72+ //! uart.init(Config::default()).expect("should init device successfully");
73+ //! uart.send_bytes_exact(b"hello world!");
74+ //! ```
75+ //!
76+ //! # Example (Minimal - MMIO)
5777//!
5878//! ```rust,no_run
59- //! use uart_16550::{Config, Uart16550Tty};
60- //! use core::fmt::Write;
79+ //! use uart_16550::{Config, Uart16550};
6180//!
6281//! // SAFETY: The address is valid and we have exclusive access.
63- //! let mut uart = unsafe { Uart16550Tty ::new_mmio(0x1000 as *mut _, 4, Config::default()).expect("should initialize device" ) };
64- //! // ^ or `new_port(0x3f8, Config::default())`
65- //! uart.write_str( "hello world\nhow's it going? ");
82+ //! let mut uart = unsafe { Uart16550 ::new_mmio(0x1000 as *mut _, 4).unwrap( ) };
83+ //! uart.init( Config::default()).expect("should init device successfully");
84+ //! uart.send_bytes_exact(b "hello world! ");
6685//! ```
6786//!
68- //! See [`Uart16550Tty`] for more details.
69- //!
70- //! # Example (More low-level control)
87+ //! # Example (Recommended)
7188//!
7289//! ```rust,no_run
7390//! use uart_16550::{Config, Uart16550};
7794//! // ^ or `new_port(0x3f8)`
7895//! uart.init(Config::default()).expect("should init device successfully");
7996//! uart.test_loopback().expect("should have working loopback mode");
97+ //! // Note: Might fail on real hardware with some null-modem cables
8098//! uart.check_connected().expect("should have physically connected receiver");
8199//! uart.send_bytes_exact(b"hello world!");
82100//! ```
@@ -146,7 +164,7 @@ pub use crate::error::*;
146164pub use crate :: tty:: * ;
147165
148166use crate :: backend:: { Backend , MmioAddress , MmioBackend } ;
149- #[ cfg( any( target_arch = "x86" , target_arch = "x86_64" ) ) ]
167+ #[ cfg( any( target_arch = "x86" , target_arch = "x86_64" , doc ) ) ]
150168use crate :: backend:: { PioBackend , PortIoAddress } ;
151169use crate :: spec:: registers:: { DLL , DLM , FCR , IER , ISR , LCR , LSR , MCR , MSR , SPR , offsets} ;
152170use crate :: spec:: { FIFO_SIZE , NUM_REGISTERS , calc_baud_rate, calc_divisor} ;
@@ -171,16 +189,34 @@ mod tty;
171189/// on the underlying hardware.
172190///
173191/// This type is generic over x86 port I/O and MMIO via the corresponding
174- /// constructors (`Uart16550::new_port()` and [`Uart16550::new_mmio()`].
192+ /// constructors [`Uart16550::new_port()`] and [`Uart16550::new_mmio()`].
193+ /// The following examples show usage of the latter.
194+ ///
195+ /// # Example (Minimal - x86 Port IO)
196+ ///
197+ #[ cfg_attr(
198+ any( target_arch = "x86" , target_arch = "x86_64" ) ,
199+ doc = "```rust,no_run"
200+ ) ]
201+ #[ cfg_attr(
202+ not( any( target_arch = "x86" , target_arch = "x86_64" ) ) ,
203+ doc = "```rust,ignore"
204+ ) ]
205+ /// use uart_16550::{Config, Uart16550};
175206///
176- /// # Example (Minimal)
207+ /// // SAFETY: The port is valid and we have exclusive access.
208+ /// let mut uart = unsafe { Uart16550::new_port(0x3f8).unwrap() };
209+ /// uart.init(Config::default()).expect("should init device successfully");
210+ /// uart.send_bytes_exact(b"hello world!");
211+ /// ```
212+ ///
213+ /// # Example (Minimal - MMIO)
177214///
178215/// ```rust,no_run
179216/// use uart_16550::{Config, Uart16550};
180217///
181218/// // SAFETY: The address is valid and we have exclusive access.
182219/// let mut uart = unsafe { Uart16550::new_mmio(0x1000 as *mut _, 4).unwrap() };
183- /// // ^ or `new_port(0x3f8)`
184220/// uart.init(Config::default()).expect("should init device successfully");
185221/// uart.send_bytes_exact(b"hello world!");
186222/// ```
@@ -195,6 +231,7 @@ mod tty;
195231/// // ^ or `new_port(0x3f8)`
196232/// uart.init(Config::default()).expect("should init device successfully");
197233/// uart.test_loopback().expect("should have working loopback mode");
234+ /// // Note: Might fail on real hardware with some null-modem cables
198235/// uart.check_connected().expect("should have physically connected receiver");
199236/// uart.send_bytes_exact(b"hello world!");
200237/// ```
@@ -221,15 +258,16 @@ mod tty;
221258/// - [`Uart16550::send_bytes_exact`]: Transmit all bytes, looping until the
222259/// entire buffer has been written.
223260/// - [`Uart16550::receive_bytes_exact`]: Receive bytes until the provided
224- /// buffer is completely filled.
261+ /// buffer is filled.
225262///
226263/// These methods spin until completion.
227264///
228265/// # MMIO and Port I/O
229266///
230267/// Uart 16550 devices are typically mapped via port I/O on x86 and via MMIO on
231- /// other platforms. The constructors `new_port()` and `new_mmio()` create an
232- /// instance of a device with the corresponding backend.
268+ /// other platforms. The constructors [`Uart16550::new_port()`] and
269+ /// [`Uart16550::new_mmio()`] create an instance of a device with the
270+ /// corresponding backend.
233271///
234272/// # Hints for Usage on Real Hardware
235273///
@@ -248,7 +286,7 @@ pub struct Uart16550<B: Backend> {
248286 config : Config ,
249287}
250288
251- #[ cfg( any( target_arch = "x86" , target_arch = "x86_64" ) ) ]
289+ #[ cfg( any( target_arch = "x86" , target_arch = "x86_64" , doc ) ) ]
252290impl Uart16550 < PioBackend > {
253291 /// Creates a new [`Uart16550`] backed by x86 port I/O.
254292 ///
@@ -955,7 +993,7 @@ mod tests {
955993 #[ test]
956994 fn constructors ( ) {
957995 // SAFETY: We just test the constructor but do not access the device.
958- #[ cfg( any( target_arch = "x86" , target_arch = "x86_64" ) ) ]
996+ #[ cfg( any( target_arch = "x86" , target_arch = "x86_64" , doc ) ) ]
959997 unsafe {
960998 assert2:: assert!( let Ok ( _) = Uart16550 :: new_port( 0x3f8 ) ) ;
961999 assert2:: assert!( let Ok ( _) = Uart16550 :: new_port( u16 :: MAX - NUM_REGISTERS as u16 ) ) ;
0 commit comments