---
title: "Timer in Rust"
canonical_url: https://developer.repebble.com/pebbleos/docs/samples/rust/kernel/timer/
last_updated: 2026-11-18
sdk_version: 4.44
---
# Timer in Rust

Source: https://developer.repebble.com/pebbleos/docs/samples/rust/kernel/timer/

The [timer sample](https://developer.repebble.com/pebbleos/docs/samples/kernel/timer.md) written in [Rust](https://developer.repebble.com/pebbleos/docs/development/build_system/rust.md), on the `pebbleos` crate.

`Timer<T>` holds the data its function gets, in place of the C structure that embeds the timer; what the interrupt and `main()` share is atomic.

Run:

`pbl samples samples/rust/kernel/timer`

Boards:

every board

## Sources

src/lib.rs

```
#![no\_std]use core::sync::atomic::{AtomicU32, Ordering};use core::time::Duration;use pebbleos::sync::Semaphore;use pebbleos::thread;use pebbleos::time::Instant;use pebbleos::timer::Timer;use pebbleos::{Result, println};const BLINKS: u32 = 5;const BLINK\_PERIOD: Duration = Duration::from\_millis(20);// What the timer acts on, which its function gets through it.struct Blinker {done: Semaphore,blinks: AtomicU32,last\_ms: AtomicU32,}static BLINKER: Timer\<Blinker\> = Timer::new(blink,Blinker {done: Semaphore::new(0, 1),blinks: AtomicU32::new(0),last\_ms: AtomicU32::new(0),},);// Runs in the tick interrupt: keep it short, and never block.fn blink(timer: &'static Timer\<Blinker\>) {let blinks = timer.blinks.fetch\_add(1, Ordering::Relaxed) + 1;timer.last\_ms.store(Instant::now().as\_millis() as u32, Ordering::Relaxed);// Timers are one-shot: one that repeats starts itself again.if blinks \< BLINKS {timer.start(BLINK\_PERIOD);} else {timer.done.give();}}#[pebbleos::main]fn main() -\> Result\<()\> {let start\_ms = Instant::now().as\_millis() as u32;BLINKER.start(BLINK\_PERIOD);BLINKER.done.take(Duration::from\_secs(1))?;let elapsed = BLINKER.last\_ms.load(Ordering::Relaxed) - start\_ms;println!("main: {} blinks, the last one {} {} ms after the start",BLINKER.blinks.load(Ordering::Relaxed),if elapsed \>= BLINKS \* 20 {"at least"} else {"less than"},BLINKS \* 20);BLINKER.blinks.store(0, Ordering::Relaxed);BLINKER.start(Duration::from\_secs(10));thread::sleep(Duration::from\_millis(10));println!("main: started for 10 s, active: {}, more than 5 s left: {}",yes\_no(BLINKER.is\_active()),yes\_no(BLINKER.remaining() \> Duration::from\_secs(5)));BLINKER.stop();println!("main: stopped, active: {}", yes\_no(BLINKER.is\_active()));thread::sleep(Duration::from\_millis(20));println!("main: stopped before it expired, {} blinks since",BLINKER.blinks.load(Ordering::Relaxed));println!("Sample done");Ok(())}fn yes\_no(b: bool) -\> &'static str {if b { "yes" } else { "no" }}
```

## Output

```
main: 5 blinks, the last one at least 100 ms after the start
main: started for 10 s, active: yes, more than 5 s left: yes
main: stopped, active: no
main: stopped before it expired, 0 blinks since
Sample done
```

Generated from PebbleOS [7753e801d](https://github.com/coredevices/pebbleos/commit/7753e801d93d20f2d37534ce2fa69d9952015237). This page is maintained in the pebbleos repository: [docs/samples/rust/kernel/timer.md](https://github.com/coredevices/pebbleos/blob/7753e801d93d20f2d37534ce2fa69d9952015237/docs/samples/rust/kernel/timer.md).
