1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-02-13 00:16:11 +02:00

Add example of using UART driver.

This commit is contained in:
Andrew Walbran 2023-03-13 16:09:30 +00:00
parent 1f315da903
commit bc21369dcb
2 changed files with 25 additions and 0 deletions

View 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}}
```

View File

@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
// ANCHOR: main
#![no_main]
#![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) };
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();
}
// ANCHOR_END: main
#[panic_handler]
fn panic(info: &PanicInfo) -> ! {