Using it

Let’s write a small program using our driver to write to the serial console, and echo incoming bytes.

  1. #![no_main]
  2. #![no_std]
  3. mod exceptions;
  4. mod pl011;
  5. use crate::pl011::Uart;
  6. use core::fmt::Write;
  7. use core::panic::PanicInfo;
  8. use log::error;
  9. use smccc::psci::system_off;
  10. use smccc::Hvc;
  11. /// Base address of the primary PL011 UART.
  12. const PL011_BASE_ADDRESS: *mut u32 = 0x900_0000 as _;
  13. // SAFETY: There is no other global function of this name.
  14. #[unsafe(no_mangle)]
  15. extern "C" fn main(x0: u64, x1: u64, x2: u64, x3: u64) {
  16.     // SAFETY: `PL011_BASE_ADDRESS` is the base address of a PL011 device, and
  17.     // nothing else accesses that address range.
  18.     let mut uart = unsafe { Uart::new(PL011_BASE_ADDRESS) };
  19.     writeln!(uart, "main({x0:#x}, {x1:#x}, {x2:#x}, {x3:#x})").unwrap();
  20.     loop {
  21.         if let Some(byte) = uart.read_byte() {
  22.             uart.write_byte(byte);
  23.             match byte {
  24.                 b'\r' => {
  25.                     uart.write_byte(b'\n');
  26.                 }
  27.                 b'q' => break,
  28.                 _ => continue,
  29.             }
  30.         }
  31.     }
  32.     writeln!(uart, "\n\nBye!").unwrap();
  33.     system_off::<Hvc>().unwrap();
  34. }
  • As in the inline assembly example, this main function is called from our entry point code in entry.S. See the speaker notes there for details.
  • Run the example in QEMU with make qemu under src/bare-metal/aps/examples.