avr_hal_generic/clock.rs
1//! Core clock speed management
2//!
3//! AVR microcontrollers support different core clock speeds. Peripheral drivers need to know
4//! about this speed to calculate timing parameters. To make this as efficient as possible, the
5//! clock speed is tracked as a compile-time constant. This means peripheral drivers can do
6//! compile-time calculation of timing parameters.
7//!
8//! # How To Use
9//! If you are using `arduino-hal`, there is nothing you need to do - the core clock speed is
10//! defined in `arduino-hal` as `arduino_hal::DefaultClock` and the const-generic parameters of all
11//! peripheral drivers are preset to this value.
12//!
13//! If you are using a MCU HAL like `atmega-hal` or `attiny-hal`, you need to take care of clock
14//! speed management manually. The best way to do this is as follows:
15//!
16//! - Define a "constant" for your core clock speed in the crate root:
17//! ```ignore
18//! type CoreClock = atmega_hal::clock::MHz16;
19//! ```
20//! - Define aliases for peripheral driver types based on this clock:
21//! ```ignore
22//! type Adc = atmega_hal::adc::Adc<crate::CoreClock>;
23//! type I2c = atmega_hal::i2c::I2c<crate::CoreClock>;
24//! ```
25
26/// A clock speed
27pub trait Clock {
28 /// Frequency of this clock in Hz
29 const FREQ: u32;
30}
31
32/// 24 MHz Clock
33#[derive(ufmt::derive::uDebug, Debug)]
34pub struct MHz24;
35impl Clock for MHz24 {
36 const FREQ: u32 = 24_000_000;
37}
38
39/// 20 MHz Clock
40#[derive(ufmt::derive::uDebug, Debug)]
41pub struct MHz20;
42impl Clock for MHz20 {
43 const FREQ: u32 = 20_000_000;
44}
45
46/// 16 MHz Clock
47#[derive(ufmt::derive::uDebug, Debug)]
48pub struct MHz16;
49impl Clock for MHz16 {
50 const FREQ: u32 = 16_000_000;
51}
52
53/// 12 MHz Clock
54#[derive(ufmt::derive::uDebug, Debug)]
55pub struct MHz12;
56impl Clock for MHz12 {
57 const FREQ: u32 = 12_000_000;
58}
59
60/// 10 MHz Clock
61#[derive(ufmt::derive::uDebug, Debug)]
62pub struct MHz10;
63impl Clock for MHz10 {
64 const FREQ: u32 = 10_000_000;
65}
66
67/// 8 MHz Clock
68#[derive(ufmt::derive::uDebug, Debug)]
69pub struct MHz8;
70impl Clock for MHz8 {
71 const FREQ: u32 = 8_000_000;
72}
73
74/// 1 MHz Clock
75#[derive(ufmt::derive::uDebug, Debug)]
76pub struct MHz1;
77impl Clock for MHz1 {
78 const FREQ: u32 = 1_000_000;
79}