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
use crate::{
    peripherals::ctimer::Ctimer,
    time::Microseconds,
    traits::wg,
    typestates::init_state,
};

pub struct Pwm <TIMER>
where
    TIMER: Ctimer<init_state::Enabled>,
{
    timer: TIMER,
}

impl <TIMER> Pwm <TIMER>
where TIMER: Ctimer<init_state::Enabled> {

    pub fn new(timer: TIMER) -> Self {

        // Match should reset and stop timer, and generate interrupt.
        timer.mcr.modify(|_, w| {
            w
            .mr3i().set_bit()
            .mr3r().set_bit()
            .mr3s().clear_bit()
        });

        timer.pwmc.modify(|_,w|
            w.
            pwmen3().clear_bit()
        );

        // Set max duty cycle to 3rd match register (256 timer counts per pwm period)
        timer.mr[3].write(|w| unsafe { w.bits(0xff) });

        timer.mr[0].write(|w| unsafe{ w.bits(0x0) });
        timer.mr[1].write(|w| unsafe{ w.bits(0x0) });
        timer.mr[2].write(|w| unsafe{ w.bits(0x0) });

        timer.mcr.modify(|_,w| {
            w
            .mr0i().set_bit()
            .mr0r().clear_bit()
            .mr0s().clear_bit()

            .mr1i().set_bit()
            .mr1r().clear_bit()
            .mr1s().clear_bit()

            .mr2i().set_bit()
            .mr2r().clear_bit()
            .mr2s().clear_bit()
        });
        timer.pwmc.modify(|_,w|
            w
            .pwmen0().set_bit()
            .pwmen1().set_bit()
            .pwmen2().set_bit()
        );

        // No divsion necessary (1MHz / 256 ~= 4kHz at LED)
        timer.pr.write(|w| unsafe {w.bits(0)});

        // Start timer
        timer.tcr.write(|w| {
            w
            .crst().clear_bit()
            .cen().set_bit()
        });

        Self {
            timer: timer,
        }
    }

    pub fn release(self) -> TIMER {
        self.timer
    }

    /// Increase maximum value for the duty cycle.
    pub fn scale_max_duty_by(&mut self, duty: u32) {
        self.timer.mr[3].write(|w| unsafe { w.bits(0xff * duty) });
    }

}
//pin: & Pin<impl PinId, state::Analog<direction::Input>>

impl<TIMER> wg::Pwm for Pwm<TIMER>
where TIMER: Ctimer<init_state::Enabled>
{
    type Channel = u8;
    type Time = Microseconds;
    type Duty = u16;

    fn enable(&mut self, channel: Self::Channel) {
        match channel {
            0|1|2 => {

            }
            _ => {
                panic!("Cannot use channel outside 0-2 for PWM.");
            }
        }
    }

    fn disable(&mut self, channel: Self::Channel) {
        match channel {
            0 => {
                self.timer.mcr.modify(|_,w| {
                    w
                    .mr0i().clear_bit()
                    .mr0r().clear_bit()
                    .mr0s().clear_bit()
                });
                self.timer.pwmc.modify(|_,w|
                    w.
                    pwmen0().clear_bit()
                );
            }
            1 => {
                self.timer.mcr.modify(|_,w| {
                    w
                    .mr1i().clear_bit()
                    .mr1r().clear_bit()
                    .mr1s().clear_bit()
                });
                self.timer.pwmc.modify(|_,w|
                    w.
                    pwmen1().clear_bit()
                );
            }
            2 => {
                self.timer.mcr.modify(|_,w| {
                    w
                    .mr2i().clear_bit()
                    .mr2r().clear_bit()
                    .mr2s().clear_bit()
                });
                self.timer.pwmc.modify(|_,w|
                    w.
                    pwmen2().clear_bit()
                );
            }
            _ => {
                panic!("Cannot use channel outside 0-2 for PWM.");
            }
        }
    }

    fn get_period(&self) -> Self::Time {
        Microseconds(1_000_000 / self.get_max_duty() as u32)
    }

    fn set_period<P>(&mut self, _period: P)
    where
        P: Into<Self::Time>
    {
        panic!("Currently period is fixed.");
    }

    fn get_duty(&self, channel: Self::Channel) -> Self::Duty {
        self.timer.mr[channel as usize].read().bits() as Self::Duty
    }

    fn get_max_duty(&self) -> Self::Duty {
        self.timer.mr[3].read().bits() as Self::Duty
    }

    fn set_duty(&mut self, channel: Self::Channel, duty: Self::Duty) {
        self.timer.mr[channel as usize].write(|w| unsafe { w.bits(duty as u32) });
    }

}