Back to Comprehensive Rust

RTC driver

src/exercises/bare-metal/rtc.md

latest3.0 KB
Original Source
<!-- Copyright 2023 Google LLC SPDX-License-Identifier: CC-BY-4.0 -->

RTC driver

The QEMU aarch64 virt machine has a PL031 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 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 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 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 and look in the rtc directory for the following files.

src/main.rs:

<!-- File src/main.rs --> <!-- mdbook-xgettext: skip -->
rust,compile_fail
# // Copyright 2023 Google LLC
# // SPDX-License-Identifier: Apache-2.0
#
{{#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
# // Copyright 2023 Google LLC
# // SPDX-License-Identifier: Apache-2.0
#
{{#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
# // Copyright 2023 Google LLC
# // SPDX-License-Identifier: Apache-2.0
#
{{#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.