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

/// A clock speed
pub trait Clock {
    /// Frequency of this clock in Hz
    const FREQ: u32;
}

/// 24 MHz Clock
#[derive(ufmt::derive::uDebug, Debug)]
pub struct MHz24;
impl Clock for MHz24 {
    const FREQ: u32 = 24_000_000;
}

/// 20 MHz Clock
#[derive(ufmt::derive::uDebug, Debug)]
pub struct MHz20;
impl Clock for MHz20 {
    const FREQ: u32 = 20_000_000;
}

/// 16 MHz Clock
#[derive(ufmt::derive::uDebug, Debug)]
pub struct MHz16;
impl Clock for MHz16 {
    const FREQ: u32 = 16_000_000;
}

/// 12 MHz Clock
#[derive(ufmt::derive::uDebug, Debug)]
pub struct MHz12;
impl Clock for MHz12 {
    const FREQ: u32 = 12_000_000;
}

/// 10 MHz Clock
#[derive(ufmt::derive::uDebug, Debug)]
pub struct MHz10;
impl Clock for MHz10 {
    const FREQ: u32 = 10_000_000;
}

/// 8 MHz Clock
#[derive(ufmt::derive::uDebug, Debug)]
pub struct MHz8;
impl Clock for MHz8 {
    const FREQ: u32 = 8_000_000;
}

/// 1 MHz Clock
#[derive(ufmt::derive::uDebug, Debug)]
pub struct MHz1;
impl Clock for MHz1 {
    const FREQ: u32 = 1_000_000;
}