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
//! # Pulse Width Modulation Interface
//!
//! You can use the `PWM` with these [Pwm] instances
//!
//! # PWM0 - 8 bit period and duty
//! - Channel 1: Pin 9 IOF1
//! - Channel 2: Pin 10 IOF1
//! - Channel 3: Pin 11 IOF1
//!
//! # PWM1 - 16 bit period and duty
//! - Channel 1: Pin 3 IOF1
//! - Channel 2: Pin 5 IOF1
//! - Channel 3: Pin 6 IOF1
//!
//! # PWM2 - 16 bit period and duty
//! - Channel 1: Pin 17 IOF1
//! - Channel 2: Pin 18 IOF1
//! - Channel 3: Pin 19 IOF1

use core::marker::PhantomData;
use core::ops::Deref;

use e310x::{pwm0, PWM0, PWM1, PWM2};

/// PWM comparator index
#[derive(Copy, Clone)]
pub enum CmpIndex {
    /// PWM comparator 1
    Cmp1,
    /// PWM comparator 1
    Cmp2,
    /// PWM comparator 1
    Cmp3,
}

/// PWM pin - DO NOT IMPLEMENT THIS TRAIT
pub trait Pin<PWM> {
    #[doc(hidden)]
    const CMP_INDEX: CmpIndex;
}

mod pwm0_impl {
    use super::{CmpIndex, Pin, PWM0};
    use crate::gpio::{gpio0, NoInvert, IOF1};

    impl Pin<PWM0> for gpio0::Pin1<IOF1<NoInvert>> {
        const CMP_INDEX: CmpIndex = CmpIndex::Cmp1;
    }

    impl Pin<PWM0> for gpio0::Pin2<IOF1<NoInvert>> {
        const CMP_INDEX: CmpIndex = CmpIndex::Cmp2;
    }

    impl Pin<PWM0> for gpio0::Pin3<IOF1<NoInvert>> {
        const CMP_INDEX: CmpIndex = CmpIndex::Cmp3;
    }
}

mod pwm1_impl {
    use super::{CmpIndex, Pin, PWM1};
    use crate::gpio::{gpio0, NoInvert, IOF1};

    impl Pin<PWM1> for gpio0::Pin19<IOF1<NoInvert>> {
        const CMP_INDEX: CmpIndex = CmpIndex::Cmp1;
    }

    impl Pin<PWM1> for gpio0::Pin21<IOF1<NoInvert>> {
        const CMP_INDEX: CmpIndex = CmpIndex::Cmp2;
    }

    impl Pin<PWM1> for gpio0::Pin22<IOF1<NoInvert>> {
        const CMP_INDEX: CmpIndex = CmpIndex::Cmp3;
    }
}

mod pwm2_impl {
    use super::{CmpIndex, Pin, PWM2};
    use crate::gpio::{gpio0, NoInvert, IOF1};

    impl Pin<PWM2> for gpio0::Pin11<IOF1<NoInvert>> {
        const CMP_INDEX: CmpIndex = CmpIndex::Cmp1;
    }

    impl Pin<PWM2> for gpio0::Pin12<IOF1<NoInvert>> {
        const CMP_INDEX: CmpIndex = CmpIndex::Cmp2;
    }

    impl Pin<PWM2> for gpio0::Pin13<IOF1<NoInvert>> {
        const CMP_INDEX: CmpIndex = CmpIndex::Cmp3;
    }
}

/// PWM channel
pub struct Channel<PWM> {
    _pwm: PhantomData<PWM>,
    cmp_index: CmpIndex,
}

impl<PWM> Channel<PWM> {
    /// Constructs a PWM channel from a PWM pin for use with [Pwm]
    pub fn from<PIN>(_: PIN) -> Channel<PWM>
    where
        PIN: Pin<PWM>,
    {
        Channel {
            _pwm: PhantomData,
            cmp_index: PIN::CMP_INDEX,
        }
    }
}

impl<PWM> Clone for Channel<PWM> {
    fn clone(&self) -> Self {
        Self {
            _pwm: self._pwm.clone(),
            cmp_index: self.cmp_index.clone(),
        }
    }
}

impl<PWM> Copy for Channel<PWM> {}

#[doc(hidden)]
pub trait PwmX: Deref<Target = pwm0::RegisterBlock> {
    type CmpWidth: Ord;
    fn bits_from_cmp_width(other: Self::CmpWidth) -> u32;
    fn bits_into_cmp_width(other: u32) -> Self::CmpWidth;
}

macro_rules! pwmx_impl {
    ($PWM:ident,$CMP_WIDTH:ident) => {
        impl PwmX for $PWM {
            type CmpWidth = $CMP_WIDTH;
            fn bits_from_cmp_width(other: Self::CmpWidth) -> u32 {
                other as u32
            }
            fn bits_into_cmp_width(other: u32) -> Self::CmpWidth {
                other as Self::CmpWidth
            }
        }
    };
}

pwmx_impl!(PWM0, u8);
pwmx_impl!(PWM1, u16);
pwmx_impl!(PWM2, u16);

/// PWM abstraction
///
/// # Notes
///
/// [PWM0] has a max period of 255, as it only has an 8 bit comparison register,
/// the rest of them have a max value of 2^16 as they have 16 bit registers.
pub struct Pwm<PWM> {
    pwm: PWM,
}

impl<PWM: PwmX> Pwm<PWM> {
    /// Configures a PWM device
    pub fn new(pwm: PWM) -> Self {
        pwm.cfg.reset();
        pwm.cfg.write(|w| {
            w.zerocmp()
                .set_bit()
                .enalways()
                .set_bit()
                .deglitch()
                .set_bit()
        });
        pwm.cmp0.reset();
        pwm.cmp1.reset();
        pwm.cmp2.reset();
        pwm.cmp3.reset();
        Self { pwm }
    }
}

impl<PWM: PwmX> embedded_hal::Pwm for Pwm<PWM> {
    type Channel = Channel<PWM>;

    type Time = PWM::CmpWidth;

    type Duty = PWM::CmpWidth;

    fn enable(&mut self, channel: Self::Channel) {
        match channel.cmp_index {
            CmpIndex::Cmp1 => self.pwm.cmp1.write(|w| unsafe { w.bits(u32::MAX) }),
            CmpIndex::Cmp2 => self.pwm.cmp2.write(|w| unsafe { w.bits(u32::MAX) }),
            CmpIndex::Cmp3 => self.pwm.cmp3.write(|w| unsafe { w.bits(u32::MAX) }),
        }
    }

    fn disable(&mut self, channel: Self::Channel) {
        match channel.cmp_index {
            CmpIndex::Cmp1 => self.pwm.cmp1.reset(),
            CmpIndex::Cmp2 => self.pwm.cmp2.reset(),
            CmpIndex::Cmp3 => self.pwm.cmp3.reset(),
        }
    }

    fn get_period(&self) -> Self::Time {
        PWM::bits_into_cmp_width(self.pwm.cmp0.read().bits())
    }

    fn get_duty(&self, channel: Self::Channel) -> Self::Duty {
        let duty = match channel.cmp_index {
            CmpIndex::Cmp1 => self.pwm.cmp1.read().bits(),
            CmpIndex::Cmp2 => self.pwm.cmp2.read().bits(),
            CmpIndex::Cmp3 => self.pwm.cmp3.read().bits(),
        };
        PWM::bits_into_cmp_width(duty)
    }

    fn get_max_duty(&self) -> Self::Duty {
        self.get_period()
    }

    fn set_duty(&mut self, channel: Self::Channel, duty: Self::Duty) {
        let duty = PWM::bits_from_cmp_width(duty.min(self.get_max_duty()));
        match channel.cmp_index {
            CmpIndex::Cmp1 => self.pwm.cmp1.write(|w| unsafe { w.bits(duty) }),
            CmpIndex::Cmp2 => self.pwm.cmp2.write(|w| unsafe { w.bits(duty) }),
            CmpIndex::Cmp3 => self.pwm.cmp3.write(|w| unsafe { w.bits(duty) }),
        }
    }

    fn set_period<P>(&mut self, period: P)
    where
        P: Into<Self::Time>,
    {
        let period = PWM::bits_from_cmp_width(period.into());
        self.pwm.count.reset();
        self.pwm.cmp0.write(|w| unsafe { w.bits(period) });
    }
}