# RTC driver

The QEMU aarch64 virt machine has a [PL031][1] real-time clock at 0x9010000. For
this exercise, you should write a driver for it.

1. Use it to print the current time to the serial console. You can use the
   [`chrono`][2] crate for date/time formatting.
2. Use the match register and raw interrupt status to busy-wait until a given
   time, e.g. 3 seconds in the future. (Call [`core::hint::spin_loop`][3] inside
   the loop.)
3. _Extension if you have time:_ Enable and handle the interrupt generated by
   the RTC match. You can use the driver provided in the [`arm-gic`][4] crate to
   configure the Arm Generic Interrupt Controller.
   - Use the RTC interrupt, which is wired to the GIC as `IntId::spi(2)`.
   - Once the interrupt is enabled, you can put the core to sleep via
     `arm_gic::wfi()`, which will cause the core to sleep until it receives an
     interrupt.

Download the [exercise template](../../comprehensive-rust-exercises.zip) and
look in the `rtc` directory for the following files.

_src/main.rs_:

<!-- File src/main.rs -->
<!-- mdbook-xgettext: skip -->

```rust,compile_fail
{{#include rtc/src/main.rs:top}}

{{#include rtc/src/main.rs:imports}}

{{#include rtc/src/main.rs:main}}

    // TODO: Create instance of RTC driver and print current time.

    // TODO: Wait for 3 seconds.

{{#include rtc/src/main.rs:main_end}}
```

_src/exceptions.rs_ (you should only need to change this for the 3rd part of the
exercise):

<!-- File src/exceptions.rs -->
<!-- mdbook-xgettext: skip -->

```rust,compile_fail
{{#include rtc/src/exceptions.rs}}
```

_src/logger.rs_ (you shouldn't need to change this):

<!-- File src/logger.rs -->
<!-- mdbook-xgettext: skip -->

```rust,compile_fail
{{#include rtc/src/logger.rs}}
```

_Cargo.toml_ (you shouldn't need to change this):

<!-- File Cargo.toml -->
<!-- mdbook-xgettext: skip -->

```toml
{{#include rtc/Cargo.toml}}
```

_build.rs_ (you shouldn't need to change this):

<!-- File build.rs -->
<!-- mdbook-xgettext: skip -->

```ld
{{#include rtc/build.rs}}
```

_memory.ld_ (you shouldn't need to change this):

<!-- File memory.ld -->
<!-- mdbook-xgettext: skip -->

```ld
{{#include rtc/memory.ld}}
```

_Makefile_ (you shouldn't need to change this):

<!-- File Makefile -->
<!-- mdbook-xgettext: skip -->

```makefile
{{#include rtc/Makefile}}
```

_.cargo/config.toml_ (you shouldn't need to change this):

<!-- File .cargo/config.toml -->
<!-- mdbook-xgettext: skip -->

```toml
{{#include rtc/.cargo/config.toml}}
```

Run the code in QEMU with `make qemu`.

[1]: https://developer.arm.com/documentation/ddi0224/c
[2]: https://crates.io/crates/chrono
[3]: https://doc.rust-lang.org/core/hint/fn.spin_loop.html
[4]: https://docs.rs/arm-gic/
