You've already forked comprehensive-rust
mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-06-26 18:51:00 +02:00
Add example of using UART driver.
This commit is contained in:
8
src/bare-metal/aps/better-uart/using.md
Normal file
8
src/bare-metal/aps/better-uart/using.md
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
# Using it
|
||||||
|
|
||||||
|
Let's write a small program using our driver to write to the serial console, and echo incoming
|
||||||
|
bytes.
|
||||||
|
|
||||||
|
```rust,editable,compile_fail
|
||||||
|
{{#include ../examples/src/main.rs:main}}
|
||||||
|
```
|
@ -12,6 +12,7 @@
|
|||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
|
// ANCHOR: main
|
||||||
#![no_main]
|
#![no_main]
|
||||||
#![no_std]
|
#![no_std]
|
||||||
|
|
||||||
@ -34,8 +35,24 @@ extern "C" fn main(x0: u64, x1: u64, x2: u64, x3: u64) {
|
|||||||
let mut uart = unsafe { Uart::new(PL011_BASE_ADDRESS as *mut u32) };
|
let mut uart = unsafe { Uart::new(PL011_BASE_ADDRESS as *mut u32) };
|
||||||
|
|
||||||
writeln!(uart, "main({:#x}, {:#x}, {:#x}, {:#x})", x0, x1, x2, x3).unwrap();
|
writeln!(uart, "main({:#x}, {:#x}, {:#x}, {:#x})", x0, x1, x2, x3).unwrap();
|
||||||
|
|
||||||
|
loop {
|
||||||
|
if let Some(b) = uart.read_byte() {
|
||||||
|
uart.write_byte(b);
|
||||||
|
match b {
|
||||||
|
b'\r' => {
|
||||||
|
uart.write_byte(b'\n');
|
||||||
|
}
|
||||||
|
b'q' => break,
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
writeln!(uart, "Bye!").unwrap();
|
||||||
system_off().unwrap();
|
system_off().unwrap();
|
||||||
}
|
}
|
||||||
|
// ANCHOR_END: main
|
||||||
|
|
||||||
#[panic_handler]
|
#[panic_handler]
|
||||||
fn panic(info: &PanicInfo) -> ! {
|
fn panic(info: &PanicInfo) -> ! {
|
||||||
|
Reference in New Issue
Block a user