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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
//! Timers
//!
//! # PWM
//! `atmega32u4_hal` currently only implements timers for PWM.  Different uses
//! might get added later on.  To configure a timer for PWM, create a new corresponding
//! `Timer#Pwm` object:
//!
//! ```
//! let dp = atmega32u4::Peripherals::take().unwrap();
//! let mut pwm4 = atmega32u4_hal::timer::Timer4Pwm::new(dp.TIMER4);
//! ```
//!
//! Next up, convert your pin into a PWM output.  You can only configure PWM for pins
//! already configured as outputs:
//!
//! ```
//! let mut pin = portc.pc7.into_output(&mut portc.ddr).into_pwm(&mut pwm4);
//! ```
//!
//! ## Pins supporting PWM
//! Only the following pins support PWM:
//!
//! | Timer                | Channel | Port                | Pin   |
//! |----------------------|---------|---------------------|-------|
//! | [atmega32u4::TIMER0] | `OC0A`  | [atmega32u4::PORTB] | `PB7` |
//! | [atmega32u4::TIMER0] | `OC0B`  | [atmega32u4::PORTD] | `PD0` |
//! | [atmega32u4::TIMER1] | `OC1A`  | [atmega32u4::PORTB] | `PB5` |
//! | [atmega32u4::TIMER1] | `OC1B`  | [atmega32u4::PORTB] | `PB6` |
//! | [atmega32u4::TIMER1] | `OC1C`  | [atmega32u4::PORTB] | `PB7` |
//! | [atmega32u4::TIMER3] | `OC3A`  | [atmega32u4::PORTC] | `PC6` |
//! | [atmega32u4::TIMER4] | `OC4A`  | [atmega32u4::PORTC] | `PC7` |
//! | [atmega32u4::TIMER4] | `OC4B`  | [atmega32u4::PORTC] | `PB6` |
//! | [atmega32u4::TIMER4] | `OC4D`  | [atmega32u4::PORTD] | `PD7` |
//!
//! # Example
//! ```
//! let dp = atmega32u4::Peripherals::take().unwrap();
//!
//! // According to the manual, PC7(D13) is connected to Timer/Counter4
//! let mut pwm4 = atmega32u4_hal::timer::Timer4Pwm::new(dp.TIMER4);
//!
//! // Split portc into 8 pins
//! let mut portc = dp.PORTC.split();
//!
//! // First make the pin an output, then enable the PWM timer
//! let mut pin = portc.pc7.into_output(&mut portc.ddr).into_pwm(&mut pwm4);
//!
//! // Set a duty cycle
//! pin.set_duty(pin.get_max_duty() / 2);
//! ```
use core::marker;
use hal;
use atmega32u4;
use port;

macro_rules! timer_impl {
    (
        Info: ($Timer:ident, $TIMER:ident, $tim:ident),
        Init: $init:block,
        Pins: [
            $(|$port:ident, $PIN:ident, $pwm:ident| ($ocr:ident, $setup:block),)+
        ]
    ) => {
        /// PWM Timer
        pub struct $Timer {
            $tim: atmega32u4::$TIMER,
        }

        impl $Timer {
            /// Initialize this PWM timer
            ///
            /// *Note*: Right now, once a timer is configured for PWM, it can't be used for
            /// anything else afterwards.
            pub fn new($tim: atmega32u4::$TIMER) -> $Timer {
                $init

                $Timer {
                    $tim: $tim,
                }
            }
        }

        $(
            impl port::$port::$PIN<port::mode::io::Output> {
                /// Make this pin a PWM pin
                ///
                /// Pin needs to be an output pin to be turned into a PWM pin.
                pub fn into_pwm(
                    self,
                    $pwm: &mut $Timer,
                ) -> port::$port::$PIN<port::mode::Pwm<$Timer>> {
                    $setup

                    port::$port::$PIN {
                        _mode: marker::PhantomData,
                    }
                }
            }

            impl hal::PwmPin for port::$port::$PIN<port::mode::Pwm<$Timer>> {
                type Duty = u8;

                fn disable(&mut self) {
                    unimplemented!()
                }

                fn enable(&mut self) {
                    unimplemented!()
                }

                fn get_duty(&self) -> Self::Duty {
                    unsafe { (&*atmega32u4::$TIMER::ptr()) }.$ocr.read().bits()
                }

                fn get_max_duty(&self) -> Self::Duty {
                    ::core::u8::MAX
                }

                fn set_duty(&mut self, duty: Self::Duty) {
                    unsafe { (&*atmega32u4::$TIMER::ptr()) }.$ocr.write(|w| w.bits(duty));
                }
            }
        )+
    }
}

// Timer0
timer_impl! {
    Info: (Timer0Pwm, TIMER0, tim),
    Init: {
        // Fast PWM Mode
        tim.tccr_a.modify(|_, w| w.wgm0().pwm_fast());
        // Enable Timer
        tim.tccr_b.modify(|_, w| w.cs().io_64());
    },
    Pins: [
        |portb, PB7, pwm| (ocr_a, {
            // Use OCR_A as Duty Cycle
            pwm.tim.tccr_a.modify(|_, w| w.com_a().match_clear());
        }),
        |portd, PD0, pwm| (ocr_b, {
            // Use OCR_B as Duty Cycle
            pwm.tim.tccr_a.modify(|_, w| w.com_b().match_clear());
        }),
    ]
}

// Timer1
timer_impl! {
    Info: (Timer1Pwm, TIMER1, tim),
    Init: {
        tim.tccr_a.modify(|_, w| unsafe { w.wgm0().bits(0b01) });
        tim.tccr_b.modify(|_, w| unsafe { w.wgm2().bits(0b01)}.cs().io_64());
    },
    Pins: [
        |portb, PB5, pwm| (ocr_a_l, {
            // Use OCR_A as Duty Cycle
            pwm.tim.tccr_a.modify(|_, w| w.com_a().match_clear());
        }),
        |portb, PB6, pwm| (ocr_b_l, {
            // Use OCR_B as Duty Cycle
            pwm.tim.tccr_a.modify(|_, w| w.com_b().match_clear());
        }),
        //////////////////////////////////////////////////////////////////
        // The following can be used instead of Timer0.ocr_a:
        //
        // |portb, PB7, pwm, dc| (ocr_c_l, {
        //     // Use OCR_C as Duty Cycle
        //     pwm.tim.tccr_a.modify(|_, w| w.com_c().match_clear());
        // }),
    ]
}

// Manual second implementation
impl port::portb::PB7<port::mode::io::Output> {
    /// Make this pin  a PWM pin, but using Timer1 instead of Timer0
    ///
    /// `PB7` can be PWM'd by both Timer0 and Timer1.
    pub fn into_pwm1(self, pwm: &mut Timer1Pwm) -> port::portb::PB7<port::mode::Pwm<Timer1Pwm>> {
        pwm.tim.tccr_a.modify(|_, w| w.com_c().match_clear());

        port::portb::PB7 { _mode: marker::PhantomData }
    }
}

impl hal::PwmPin for port::portb::PB7<port::mode::Pwm<Timer1Pwm>> {
    type Duty = u8;

    fn disable(&mut self) {
        unimplemented!()
    }

    fn enable(&mut self) {
        unimplemented!()
    }

    fn get_duty(&self) -> Self::Duty {
        unsafe { (&*atmega32u4::TIMER1::ptr()) }
            .ocr_c_l
            .read()
            .bits()
    }

    fn get_max_duty(&self) -> Self::Duty {
        ::core::u8::MAX
    }

    fn set_duty(&mut self, duty: Self::Duty) {
        unsafe { (&*atmega32u4::TIMER1::ptr()) }.ocr_c_l.write(
            |w| {
                w.bits(duty)
            },
        );
    }
}

// Timer3
timer_impl! {
    Info: (Timer3Pwm, TIMER3, tim),
    Init: {
        tim.tccr_a.modify(|_, w| unsafe { w.wgm0().bits(0b01) });
        tim.tccr_b.modify(|_, w| unsafe { w.wgm2().bits(0b01) }.cs().io_64());
    },
    Pins: [
        |portc, PC6, pwm| (ocr_a_l, {
            // Use OCR_A as Duty Cycle
            pwm.tim.tccr_a.modify(|_, w| w.com_a().match_clear());
        }),
    ]
}

// Timer4
timer_impl! {
    Info: (Timer4Pwm, TIMER4, tim),
    Init: {
        // Prescale/64
        tim.tccr_b.modify(|_, w| w.cs().clk_64());
        // Set WGM to Phase-Correct PWM Mode
        tim.tccr_d.modify(|_, w| unsafe { w.wgm().bits(0b01) });
    },
    Pins: [
        |portc, PC7, pwm| (ocr_a, {
            // Use OCR_A as Duty Cycle
            // Enable PWM for OCR_A
            pwm.tim.tccr_a.modify(|_, w| w.com_a().match_clear().pwm_a().set_bit());
        }),
        |portd, PD7, pwm| (ocr_d, {
            // Use OCR_D as Duty Cycle
            // Enable PWM for OCR_D
            pwm.tim.tccr_c.modify(|_, w| w.com_d().match_clear().pwm_d().set_bit());
        }),
    ]
}

// Manual second implementation
impl port::portb::PB6<port::mode::io::Output> {
    /// Make this pin a PWM pin, but using Timer4 instead of Timer1
    ///
    /// `PB6` can be PWM'd by both Timer1 and Timer4.
    pub fn into_pwm4(self, pwm: &mut Timer4Pwm) -> port::portb::PB6<port::mode::Pwm<Timer4Pwm>> {
        pwm.tim.tccr_a.modify(|_, w| {
            w.com_b().match_clear().pwm_b().set_bit()
        });

        port::portb::PB6 { _mode: marker::PhantomData }
    }
}

impl hal::PwmPin for port::portb::PB6<port::mode::Pwm<Timer4Pwm>> {
    type Duty = u8;

    fn disable(&mut self) {
        unimplemented!()
    }

    fn enable(&mut self) {
        unimplemented!()
    }

    fn get_duty(&self) -> Self::Duty {
        unsafe { (&*atmega32u4::TIMER4::ptr()) }.ocr_b.read().bits()
    }

    fn get_max_duty(&self) -> Self::Duty {
        ::core::u8::MAX
    }

    fn set_duty(&mut self, duty: Self::Duty) {
        unsafe { (&*atmega32u4::TIMER4::ptr()) }.ocr_b.write(|w| {
            w.bits(duty)
        });
    }
}