Module avr_oxide::devices::masterclock

source ·
Expand description

A simple master clock device designed to produce regular, frequent clock tick events.

§Usage

The frequency, in Hz, is passed as a constant parameter when the device is constructed using the using() constructor methods. Also passed is the underlying AVR hardware timer device which it should use to generate the clock.


  let master_clock = StaticWrap::new(OxideMasterClock::with_timer::<20>(avr_oxide::hardware::timer::tcb0::instance()));

  // An event handler every time the master clock ticks
  master_clock.borrow().on_tick(Box::new(move |_timerid, _duration|{
    // Do something 20 times every second
  }));

 supervisor.listen(master_clock.borrow());
 supervisor.run();

§Delay Events

The MasterClock device can be used to efficiently schedule events which should be triggered in the future. A closure can be passed to the after_delay() method, which will be executed after the given duration has elapsed. Internally, WallClock uses a Delay Queue implementation, meaning there is no limit to the number of such events which may be scheduled (other than memory to allocate the queue elements.)


  let master_clock = StaticWrap::new(OxideMasterClock::with_timer::<20>(avr_oxide::hardware::timer::tcb0::instance()));

  master_clock.borrow().after_delay(Duration::from_millis(100), Box::new(move |_timerid|{
    // Do something after 100ms
  }));

 supervisor.listen(master_clock.borrow());
 supervisor.run();

§Blocking API

A blocking wait() API is also provided, which will block the calling thread for the given duration. Note that the clock must be running - i.e. the supervisor.listen_handle() method was called already - before you use the wait() method, or you can expect to block forever, and that this method depends on the main supervisor to be running. In other words, this must be used in threads you have spawn()ed, not the main thread.


 let master_clock = StaticWrap::new(OxideMasterClock::with_timer::<20>(avr_oxide::hardware::timer::tcb0::instance()));

 {
   let master_clock = master_clock.borrow();
   avr_oxide::thread::spawn(move||{
     master_clock.wait(Duration::from_millis(2500));
     0
   });
 }

 supervisor.listen(master_clock.borrow());
 supervisor.run();

§Pre-Emptive Multithreading

A MasterClock instance is required to schedule thread context switches pre-emptively. By default, any MasterClock instance will trigger context switches at its configured frequency. This can be disabled with the disable_preemption() method if not desired.

§Features

For accurate frequency calculations, this depends on various constants defined in the avr_oxide::deviceconsts::clock module. These depend on the correct AVRoxide clockspeed feature being enabled in your Cargo.toml.

CPU FeatureClockspeed features (pick 1)
atmega480916MHz, 20MHz
atmega328p16MHz

§Maximum Frequency

The maximum frequency for which you can create a MasterClock is limited by the maximum frequency of the Oxide internal tick. This is determined by the CPU frequency and the power-saving feature you have built AVRoxide with, as follows:

FrequencyPower-saving featureMaximum MasterClock frequency
16MHzNone1000 Hz
power_med500 Hz
power_low500 Hz
20MHzNone1000 Hz
power_med500 Hz
power_low500 Hz

Structs§

Traits§

  • Trait implemented by devices which can run code after a delay has passed.
  • Trait implemented by devices which can run code every time the clock ticks.

Trait Aliases§