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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
pub use avr_hal_generic::simple_pwm::{IntoPwmPin, Prescaler, PwmPinOps};

#[cfg(any(feature = "attiny85", feature = "attiny84", feature = "attiny88"))]
use crate::port::*;

#[cfg(feature = "attiny84")]
avr_hal_generic::impl_simple_pwm! {
    /// Use `TC0` for PWM (pins `PB2`, `PA7`)
    pub struct Timer0Pwm {
        timer: crate::pac::TC0,
        init: |tim, prescaler| {
            tim.tccr0a.modify(|_r, w| w.wgm0().pwm_fast());
            tim.tccr0b.modify(|_r, w| match prescaler {
                Prescaler::Direct => w.cs0().direct(),
                Prescaler::Prescale8 => w.cs0().prescale_8(),
                Prescaler::Prescale64 => w.cs0().prescale_64(),
                Prescaler::Prescale256 => w.cs0().prescale_256(),
                Prescaler::Prescale1024 => w.cs0().prescale_1024(),
            });
        },
        pins: {
            PB2: {
                ocr: ocr0a,
                into_pwm: |tim| if enable {
                    tim.tccr0a.modify(|_r, w| w.com0a().match_clear());
                } else {
                    tim.tccr0a.modify(|_r, w| w.com0a().disconnected());
                },
            },

            PA7: {
                ocr: ocr0b,
                into_pwm: |tim| if enable {
                    tim.tccr0a.modify(|_r, w| w.com0b().match_clear());
                } else {
                    tim.tccr0a.modify(|_r, w| w.com0b().disconnected());
                },
            },
        },
    }
}

#[cfg(feature = "attiny84")]
avr_hal_generic::impl_simple_pwm! {
    /// Use `TC1` for PWM (pins `PA6`, 'PA5')
    pub struct Timer1Pwm {
        timer: crate::pac::TC1,
        init: |tim, prescaler| {
            tim.tccr1a.modify(|_, w| w.wgm1().bits(0b01));
            tim.tccr1b.modify(|_, w| w.wgm1().bits(0b01));

            tim.tccr1b.modify(|_r, w| match prescaler {
                Prescaler::Direct => w.cs1().direct(),
                Prescaler::Prescale8 => w.cs1().prescale_8(),
                Prescaler::Prescale64 => w.cs1().prescale_64(),
                Prescaler::Prescale256 => w.cs1().prescale_256(),
                Prescaler::Prescale1024 => w.cs1().prescale_1024(),
            });
        },
        pins: {
            PA6: {
                ocr: ocr1a,
                into_pwm: |tim| if enable {
                    tim.tccr1a.modify(|_, w| w.com1a().bits(0b10));
                } else {
                    tim.tccr1a.modify(|_, w| w.com1a().disconnected());
                },
            },

            PA5: {
                ocr: ocr1b,
                into_pwm: |tim| if enable {
                    tim.tccr1a.modify(|_, w| w.com1b().bits(0b10));
                } else {
                    tim.tccr1a.modify(|_, w| w.com1b().disconnected());
                },
            },
        },
    }
}

#[cfg(feature = "attiny85")]
avr_hal_generic::impl_simple_pwm! {
    /// Use `TC0` for PWM (pins `PB0`, `PB1`)
    ///
    /// # Example
    /// ```
    /// let mut timer0 = Timer0Pwm::new(dp.TC0, Prescaler::Prescale64);
    ///
    /// let mut d0 = pins.d0.into_output().into_pwm(&mut timer0);
    /// let mut d1 = pins.d1.into_output().into_pwm(&mut timer0);
    ///
    /// d0.set_duty(128);
    /// d0.enable();
    /// ```
    pub struct Timer0Pwm {
        timer: crate::pac::TC0,
        init: |tim, prescaler| {
            tim.tccr0a.modify(|_r, w| w.wgm0().pwm_fast());
            tim.tccr0b.modify(|_r, w| match prescaler {
                Prescaler::Direct => w.cs0().direct(),
                Prescaler::Prescale8 => w.cs0().prescale_8(),
                Prescaler::Prescale64 => w.cs0().prescale_64(),
                Prescaler::Prescale256 => w.cs0().prescale_256(),
                Prescaler::Prescale1024 => w.cs0().prescale_1024(),
            });
        },
        pins: {
            PB0: {
                ocr: ocr0a,
                into_pwm: |tim| if enable {
                    tim.tccr0a.modify(|_r, w| w.com0a().match_clear());
                } else {
                    tim.tccr0a.modify(|_r, w| w.com0a().disconnected());
                },
            },

            PB1: {
                ocr: ocr0b,
                into_pwm: |tim| if enable {
                    tim.tccr0a.modify(|_r, w| w.com0b().match_clear());
                } else {
                    tim.tccr0a.modify(|_r, w| w.com0b().disconnected());
                },
            },
        },
    }
}

#[cfg(feature = "attiny85")]
avr_hal_generic::impl_simple_pwm! {
    /// Use `TC1` for PWM (pins `PB4`)
    ///
    /// # Example
    /// ```
    /// let mut timer1 = Timer1Pwm::new(dp.TC1, Prescaler::Prescale64);
    ///
    /// let mut d4 = pins.d4.into_output().into_pwm(&mut timer1);
    ///
    /// d4.set_duty(128);
    /// d4.enable();
    /// ```
    pub struct Timer1Pwm {
        timer: crate::pac::TC1,
        init: |tim, prescaler| {
            tim.gtccr.modify(|_, w| w.pwm1b().bit(true));

            tim.tccr1.modify(|_r, w| match prescaler {
                Prescaler::Direct => w.cs1().direct(),
                Prescaler::Prescale8 => w.cs1().prescale_8(),
                Prescaler::Prescale64 => w.cs1().prescale_64(),
                Prescaler::Prescale256 => w.cs1().prescale_256(),
                Prescaler::Prescale1024 => w.cs1().prescale_1024(),
            });
        },
        pins: {
            PB4: {
                ocr: ocr1b,
                into_pwm: |tim| if enable {
                    tim.gtccr.modify(|_, w| w.com1b().bits(0b10));
                } else {
                    tim.gtccr.modify(|_, w| w.com1b().disconnected());
                },
            },
        },
    }
}

#[cfg(feature = "attiny88")]
avr_hal_generic::impl_simple_pwm! {
    /// Use `TC1` for PWM (pins `PB1`, 'PB2')
    ///
    /// # Example
    /// ```
    /// let mut timer1 = Timer1Pwm::new(dp.TC1, Prescaler::Prescale64);
    ///
    /// let mut d9 = pins.d9.into_output().into_pwm(&mut timer1);
    /// let mut d10 = pins.d10.into_output().into_pwm(&mut timer1);
    ///
    /// d9.set_duty(128);
    /// d9.enable();
    /// ```
    pub struct Timer1Pwm {
        timer: crate::pac::TC1,
        init: |tim, prescaler| {
            tim.tccr1a.modify(|_, w| w.wgm1().bits(0b01));
            tim.tccr1b.modify(|_, w| w.wgm1().bits(0b01));

            tim.tccr1b.modify(|_r, w| match prescaler {
                Prescaler::Direct => w.cs1().direct(),
                Prescaler::Prescale8 => w.cs1().prescale_8(),
                Prescaler::Prescale64 => w.cs1().prescale_64(),
                Prescaler::Prescale256 => w.cs1().prescale_256(),
                Prescaler::Prescale1024 => w.cs1().prescale_1024(),
            });
        },
        pins: {
            PB1: {
                ocr: ocr1a,
                into_pwm: |tim| if enable {
                    tim.tccr1a.modify(|_, w| w.com1a().bits(0b10));
                } else {
                    tim.tccr1a.modify(|_, w| w.com1a().disconnected());
                },
            },

            PB2: {
                ocr: ocr1b,
                into_pwm: |tim| if enable {
                    tim.tccr1a.modify(|_, w| w.com1b().bits(0b10));
                } else {
                    tim.tccr1a.modify(|_, w| w.com1b().disconnected());
                },
            },
        },
    }
}