Skip to main content

Module timer

Module timer 

Source
Expand description

The timer and the free-running divider.

divider is free-running and cannot be configured; the timer counts at a rate a program picks and raises an interrupt when it overflows. See https://gbdev.io/pandocs/Timer_and_Divider_Registers.html.

Timer::start hands back the proof that ticks are being counted. Timestamps come from it and cannot outlive it.

let timer = unsafe { Timer::start(rate::Hz256) }.unwrap();
let began = timer.now();
// ...
if began.elapsed() >= Duration::from_ms(5000) {
    expire();
}

Carrying the rate in the type is what settles the arithmetic behind Duration at compile time, and it keeps a span from one rate from being compared against a span from another.

Duration is not core::time::Duration, which holds a u64 of seconds beside a u32 of nanoseconds and does its arithmetic to match; a span here is a u32 of ticks.

§A handler of one’s own

A program that wants work at the tick, a sound driver most often, writes #[gb::rt::interrupt(Timer)] and takes that vector. Nothing reports it, and the count then moves only where the handler calls timer_tick.

The rate is still Timer::start’s to set. The eleven in rate are powers of two; a tempo that falls between them means implementing Rate for a type of one’s own.

Modules§

rate
The rates the timer can be run at.

Structs§

Duration
A span of ticks at rate R.
Instant
A point in the tick count.
Timer
A running timer, and the scope its timestamps belong to.

Traits§

Rate
How often the timer overflows, as a type.

Functions§

divider
The divider, which counts at 16384 Hz whatever the timer is doing.
reset_divider
Reset the divider to zero.
timer_tick
Advance the tick count.