Skip to content

Commit 7a890f8

Browse files
authored
Merge pull request #18 from rust-osdev/pr15-follow-up
Improvements to new MMIO support
2 parents c1578b3 + 7cbfacf commit 7a890f8

File tree

5 files changed

+69
-38
lines changed

5 files changed

+69
-38
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,6 @@ pre-release-replacements = [
2424
{ file="Changelog.md", search="# Unreleased", replace="# Unreleased\n\n# {{version}} – {{date}}", exactly=1 },
2525
]
2626
pre-release-commit-message = "Release version {{version}}"
27+
28+
[package.metadata.docs.rs]
29+
rustdoc-args = ["--cfg", "docsrs"]

README.md

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@
22

33
[![Build Status](https://github.com/rust-osdev/uart_16550/workflows/Build/badge.svg)](https://github.com/rust-osdev/uart_16550/actions?query=workflow%3ABuild) [![Docs.rs Badge](https://docs.rs/uart_16550/badge.svg)](https://docs.rs/uart_16550/)
44

5-
Minimal support for uart_16550 serial and memory mapped I/O.
5+
Minimal support for [serial communication](https://en.wikipedia.org/wiki/Asynchronous_serial_communication) through [UART](https://en.wikipedia.org/wiki/Universal_asynchronous_receiver-transmitter) devices, which are compatible to the [16550 UART](https://en.wikipedia.org/wiki/16550_UART). This crate supports port-mapped and memory mapped UARTS.
66

77
## Usage
88

9-
### With usual serial port
9+
Depending on the system architecture, the UART can be either accessed through [port-mapped I/O](https://wiki.osdev.org/Port_IO) or [memory-mapped I/O](https://en.wikipedia.org/wiki/Memory-mapped_I/O).
10+
11+
### With port-mappd I/O
12+
13+
The UART is accessed through port-mapped I/O on architectures such as `x86_64`. On these architectures, the [`SerialPort`](https://docs.rs/uart_16550/~0.2/uart_16550/struct.SerialPort.html) type can be used:
14+
1015

1116
```rust
1217
use uart_16550::SerialPort;
@@ -25,6 +30,8 @@ let data = serial_port.receive();
2530

2631
### With memory mapped serial port
2732

33+
Most other architectures, such as [RISC-V](https://en.wikipedia.org/wiki/RISC-V), use memory-mapped I/O for accessing the UARTs. On these architectures, the [`MmioSerialPort`](https://docs.rs/uart_16550/~0.2/uart_16550/struct.MmioSerialPort.html) type can be used:
34+
2835
```rust
2936
use uart_16550::MmioSerialPort;
3037

@@ -40,10 +47,6 @@ serial_port.send(42);
4047
let data = serial_port.receive();
4148
```
4249

43-
## License
44-
45-
Licensed under the MIT license ([LICENSE](LICENSE) or <http://opensource.org/licenses/MIT>).
46-
4750
## Crate Feature Flags
4851

4952
* `nightly`: This is the default.
@@ -53,3 +56,7 @@ Licensed under the MIT license ([LICENSE](LICENSE) or <http://opensource.org/lic
5356

5457
This needs to have the [compile-time requirements](https://github.com/alexcrichton/cc-rs#compile-time-requirements) of the `cc` crate installed on your system.
5558
It was currently only tested on Linux and MacOS.
59+
60+
## License
61+
62+
Licensed under the MIT license ([LICENSE](LICENSE) or <http://opensource.org/licenses/MIT>).

src/lib.rs

Lines changed: 48 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,47 @@
1-
//! Minimal support for uart_16550 serial I/O.
1+
//! Minimal support for
2+
//! [serial communication](https://en.wikipedia.org/wiki/Asynchronous_serial_communication)
3+
//! through [UART](https://en.wikipedia.org/wiki/Universal_asynchronous_receiver-transmitter)
4+
//! devices, which are compatible to the [16550 UART](https://en.wikipedia.org/wiki/16550_UART).
25
//!
3-
//! # Usage
4-
5-
#![cfg_attr(
6-
target_arch = "x86_64",
7-
doc = "
8-
## With usual serial port
9-
```no_run
10-
use uart_16550::SerialPort;
11-
12-
const SERIAL_IO_PORT: u16 = 0x3F8;
13-
14-
let mut serial_port = unsafe { SerialPort::new(SERIAL_IO_PORT) };
15-
serial_port.init();
16-
17-
// Now the serial port is ready to be used. To send a byte:
18-
serial_port.send(42);
19-
20-
// To receive a byte:
21-
let data = serial_port.receive();
22-
```
23-
"
24-
)]
25-
26-
//! ## With memory mapped serial port
6+
//! This crate supports port-mapped and memory mapped UARTS.
7+
//!
8+
//! ## Usage
9+
//!
10+
//! Depending on the system architecture, the UART can be either accessed through
11+
//! [port-mapped I/O](https://wiki.osdev.org/Port_IO) or
12+
//! [memory-mapped I/O](https://en.wikipedia.org/wiki/Memory-mapped_I/O).
13+
//!
14+
//! ### With port-mappd I/O
15+
//!
16+
//! The UART is accessed through port-mapped I/O on architectures such as `x86_64`.
17+
//! On these architectures, the [`SerialPort`] type can be used:
18+
//!
19+
//!
20+
//! ```no_run
21+
//! # #[cfg(target_arch = "x86_64")]
22+
//! # fn main() {
23+
//! use uart_16550::SerialPort;
24+
//!
25+
//! const SERIAL_IO_PORT: u16 = 0x3F8;
26+
//!
27+
//! let mut serial_port = unsafe { SerialPort::new(SERIAL_IO_PORT) };
28+
//! serial_port.init();
29+
//!
30+
//! // Now the serial port is ready to be used. To send a byte:
31+
//! serial_port.send(42);
32+
//!
33+
//! // To receive a byte:
34+
//! let data = serial_port.receive();
35+
//! # }
36+
//! # #[cfg(not(target_arch = "x86_64"))]
37+
//! # fn main() {}
38+
//! ```
39+
//!
40+
//! ### With memory mapped serial port
41+
//!
42+
//! Most other architectures, such as [RISC-V](https://en.wikipedia.org/wiki/RISC-V), use
43+
//! memory-mapped I/O for accessing the UARTs. On these architectures, the [`MmioSerialPort`]
44+
//! type can be used:
2745
//!
2846
//! ```no_run
2947
//! use uart_16550::MmioSerialPort;
@@ -39,9 +57,11 @@ let data = serial_port.receive();
3957
//! // To receive a byte:
4058
//! let data = serial_port.receive();
4159
//! ```
60+
4261
#![no_std]
4362
#![warn(missing_docs)]
4463
#![cfg_attr(feature = "nightly", feature(const_ptr_offset))]
64+
#![cfg_attr(docsrs, feature(doc_cfg))]
4565

4666
#[cfg(not(any(feature = "stable", feature = "nightly")))]
4767
compile_error!("Either the `stable` or `nightly` feature must be enabled");
@@ -57,14 +77,14 @@ macro_rules! wait_for {
5777
}
5878

5979
/// Memory mapped implementation
60-
pub mod mmio;
80+
mod mmio;
6181
#[cfg(target_arch = "x86_64")]
6282
/// Port asm commands implementation
63-
pub mod x86_64;
83+
mod port;
6484

6585
pub use crate::mmio::MmioSerialPort;
6686
#[cfg(target_arch = "x86_64")]
67-
pub use crate::x86_64::SerialPort;
87+
pub use crate::port::SerialPort;
6888

6989
bitflags! {
7090
/// Interrupt enable flags

src/mmio.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use core::{
55

66
use crate::LineStsFlags;
77

8-
/// An interface to a serial port that allows sending out individual bytes.
8+
/// A memory-mapped UART.
99
pub struct MmioSerialPort {
1010
data: AtomicPtr<u8>,
1111
int_en: AtomicPtr<u8>,
@@ -16,7 +16,7 @@ pub struct MmioSerialPort {
1616
}
1717

1818
impl MmioSerialPort {
19-
/// Creates a new serial port interface on the given memory mapped address.
19+
/// Creates a new UART interface on the given memory mapped address.
2020
///
2121
/// This function is unsafe because the caller must ensure that the given base address
2222
/// really points to a serial port device.
@@ -46,7 +46,7 @@ impl MmioSerialPort {
4646
}
4747
}
4848

49-
/// Initializes the serial port.
49+
/// Initializes the memory-mapped UART.
5050
///
5151
/// The default configuration of [38400/8-N-1](https://en.wikipedia.org/wiki/8-N-1) is used.
5252
pub fn init(&mut self) {

src/x86_64.rs renamed to src/port.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ use x86_64::instructions::port::{Port, PortReadOnly, PortWriteOnly};
44

55
use crate::LineStsFlags;
66

7-
/// An interface to a serial port that allows sending out individual bytes.
7+
/// A port-mapped UART.
8+
#[cfg_attr(docsrs, doc(cfg(target_arch = "x86_64")))]
89
pub struct SerialPort {
910
data: Port<u8>,
1011
int_en: PortWriteOnly<u8>,

0 commit comments

Comments
 (0)