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:
parent
1f315da903
commit
bc21369dcb
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
|
||||
// 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) -> ! {
|
||||
|
Loading…
x
Reference in New Issue
Block a user