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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
//! API for Pulse-Width Modulation (PWM)
//!
//! The Vorago VA416xx devices use the TIM peripherals to perform PWM related tasks.
//!
//! ## Examples
//!
//! - [PWM example](https://egit.irs.uni-stuttgart.de/rust/va416xx-rs/src/branch/main/examples/simple/examples/pwm.rs)
use core::convert::Infallible;
use core::marker::PhantomData;

use crate::pac;
use crate::{clock::Clocks, gpio::DynPinId};
pub use crate::{gpio::PinId, time::Hertz, timer::*};

const DUTY_MAX: u16 = u16::MAX;

pub struct PwmBase {
    clock: Hertz,
    /// For PWMB, this is the upper limit
    current_duty: u16,
    /// For PWMA, this value will not be used
    current_lower_limit: u16,
    current_period: Hertz,
    current_rst_val: u32,
}

enum StatusSelPwm {
    PwmA = 3,
    PwmB = 4,
}

pub struct PwmA {}
pub struct PwmB {}

//==================================================================================================
// Common
//==================================================================================================

macro_rules! pwm_common_func {
    () => {
        #[inline]
        fn enable_pwm_a(&mut self) {
            self.reg
                .reg()
                .ctrl()
                .modify(|_, w| unsafe { w.status_sel().bits(StatusSelPwm::PwmA as u8) });
        }

        #[inline]
        fn enable_pwm_b(&mut self) {
            self.reg
                .reg()
                .ctrl()
                .modify(|_, w| unsafe { w.status_sel().bits(StatusSelPwm::PwmB as u8) });
        }

        #[inline]
        pub fn get_period(&self) -> Hertz {
            self.pwm_base.current_period
        }

        #[inline]
        pub fn set_period(&mut self, period: impl Into<Hertz>) {
            self.pwm_base.current_period = period.into();
            // Avoid division by 0
            if self.pwm_base.current_period.raw() == 0 {
                return;
            }
            self.pwm_base.current_rst_val =
                self.pwm_base.clock.raw() / self.pwm_base.current_period.raw();
            self.reg
                .reg()
                .rst_value()
                .write(|w| unsafe { w.bits(self.pwm_base.current_rst_val) });
        }

        #[inline]
        pub fn disable(&mut self) {
            self.reg.reg().ctrl().modify(|_, w| w.enable().clear_bit());
        }

        #[inline]
        pub fn enable(&mut self) {
            self.reg.reg().ctrl().modify(|_, w| w.enable().set_bit());
        }

        #[inline]
        pub fn period(&self) -> Hertz {
            self.pwm_base.current_period
        }

        #[inline(always)]
        pub fn duty(&self) -> u16 {
            self.pwm_base.current_duty
        }
    };
}

macro_rules! pwmb_func {
    () => {
        pub fn pwmb_lower_limit(&self) -> u16 {
            self.pwm_base.current_lower_limit
        }

        pub fn pwmb_upper_limit(&self) -> u16 {
            self.pwm_base.current_duty
        }

        /// Set the lower limit for PWMB
        ///
        /// The PWM signal will be 1 as long as the current RST counter is larger than
        /// the lower limit. For example, with a lower limit of 0.5 and and an upper limit
        /// of 0.7, Only a fixed period between 0.5 * period and 0.7 * period will be in a high
        /// state
        pub fn set_pwmb_lower_limit(&mut self, duty: u16) {
            self.pwm_base.current_lower_limit = duty;
            let pwmb_val: u64 = (self.pwm_base.current_rst_val as u64
                * self.pwm_base.current_lower_limit as u64)
                / DUTY_MAX as u64;
            self.reg
                .reg()
                .pwmb_value()
                .write(|w| unsafe { w.bits(pwmb_val as u32) });
        }

        /// Set the higher limit for PWMB
        ///
        /// The PWM signal will be 1 as long as the current RST counter is smaller than
        /// the higher limit. For example, with a lower limit of 0.5 and and an upper limit
        /// of 0.7, Only a fixed period between 0.5 * period and 0.7 * period will be in a high
        /// state
        pub fn set_pwmb_upper_limit(&mut self, duty: u16) {
            self.pwm_base.current_duty = duty;
            let pwma_val: u64 = (self.pwm_base.current_rst_val as u64
                * self.pwm_base.current_duty as u64)
                / DUTY_MAX as u64;
            self.reg
                .reg()
                .pwma_value()
                .write(|w| unsafe { w.bits(pwma_val as u32) });
        }
    };
}

//==================================================================================================
// Strongly typed PWM pin
//==================================================================================================

pub struct PwmPin<Pin: TimPin, Tim: ValidTim, Mode = PwmA> {
    reg: TimAndPinRegister<Pin, Tim>,
    pwm_base: PwmBase,
    mode: PhantomData<Mode>,
}

impl<Pin: TimPin, Tim: ValidTim, Mode> PwmPin<Pin, Tim, Mode>
where
    (Pin, Tim): ValidTimAndPin<Pin, Tim>,
{
    /// Create a new stronlgy typed PWM pin
    pub fn new(
        pin_and_tim: (Pin, Tim),
        sys_cfg: &mut pac::Sysconfig,
        clocks: &Clocks,
        initial_period: impl Into<Hertz> + Copy,
    ) -> Self {
        let mut pin = PwmPin {
            pwm_base: PwmBase {
                current_duty: 0,
                current_lower_limit: 0,
                current_period: initial_period.into(),
                current_rst_val: 0,
                clock: Tim::clock(clocks),
            },
            reg: unsafe { TimAndPinRegister::new(pin_and_tim.0, pin_and_tim.1) },
            mode: PhantomData,
        };
        sys_cfg
            .tim_clk_enable()
            .modify(|r, w| unsafe { w.bits(r.bits() | pin.reg.mask_32()) });
        pin.enable_pwm_a();
        pin.set_period(initial_period);
        pin
    }

    pub fn release(self) -> (Pin, Tim) {
        self.reg.release()
    }

    pwm_common_func!();
}

impl<Pin: TimPin, Tim: ValidTim> From<PwmPin<Pin, Tim, PwmA>> for PwmPin<Pin, Tim, PwmB>
where
    (Pin, Tim): ValidTimAndPin<Pin, Tim>,
{
    fn from(other: PwmPin<Pin, Tim, PwmA>) -> Self {
        let mut pwmb = Self {
            reg: other.reg,
            pwm_base: other.pwm_base,
            mode: PhantomData,
        };
        pwmb.enable_pwm_b();
        pwmb
    }
}

impl<PIN: TimPin, TIM: ValidTim> From<PwmPin<PIN, TIM, PwmB>> for PwmPin<PIN, TIM, PwmA>
where
    (PIN, TIM): ValidTimAndPin<PIN, TIM>,
{
    fn from(other: PwmPin<PIN, TIM, PwmB>) -> Self {
        let mut pwmb = Self {
            reg: other.reg,
            pwm_base: other.pwm_base,
            mode: PhantomData,
        };
        pwmb.enable_pwm_a();
        pwmb
    }
}

impl<Pin: TimPin, Tim: ValidTim> PwmPin<Pin, Tim, PwmA>
where
    (Pin, Tim): ValidTimAndPin<Pin, Tim>,
{
    pub fn pwma(
        tim_and_pin: (Pin, Tim),
        sys_cfg: &mut pac::Sysconfig,
        clocks: &Clocks,
        initial_period: impl Into<Hertz> + Copy,
    ) -> Self {
        let mut pin: PwmPin<Pin, Tim, PwmA> =
            Self::new(tim_and_pin, sys_cfg, clocks, initial_period);
        pin.enable_pwm_a();
        pin
    }
}

impl<Pin: TimPin, Tim: ValidTim> PwmPin<Pin, Tim, PwmB>
where
    (Pin, Tim): ValidTimAndPin<Pin, Tim>,
{
    pub fn pwmb(
        tim_and_pin: (Pin, Tim),
        sys_cfg: &mut pac::Sysconfig,
        clocks: &Clocks,
        initial_period: impl Into<Hertz> + Copy,
    ) -> Self {
        let mut pin: PwmPin<Pin, Tim, PwmB> =
            Self::new(tim_and_pin, sys_cfg, clocks, initial_period);
        pin.enable_pwm_b();
        pin
    }
}

//==================================================================================================
// Reduced PWM pin
//==================================================================================================

/// Reduced version where type information is deleted
pub struct ReducedPwmPin<Mode = PwmA> {
    reg: TimDynRegister,
    pwm_base: PwmBase,
    pin_id: DynPinId,
    mode: PhantomData<Mode>,
}

impl<PIN: TimPin, TIM: ValidTim> From<PwmPin<PIN, TIM>> for ReducedPwmPin<PwmA> {
    fn from(pwm_pin: PwmPin<PIN, TIM>) -> Self {
        ReducedPwmPin {
            reg: TimDynRegister::from(pwm_pin.reg),
            pwm_base: pwm_pin.pwm_base,
            pin_id: PIN::DYN,
            mode: PhantomData,
        }
    }
}

impl<MODE> ReducedPwmPin<MODE> {
    pwm_common_func!();
}

impl From<ReducedPwmPin<PwmA>> for ReducedPwmPin<PwmB> {
    fn from(other: ReducedPwmPin<PwmA>) -> Self {
        let mut pwmb = Self {
            reg: other.reg,
            pwm_base: other.pwm_base,
            pin_id: other.pin_id,
            mode: PhantomData,
        };
        pwmb.enable_pwm_b();
        pwmb
    }
}

impl From<ReducedPwmPin<PwmB>> for ReducedPwmPin<PwmA> {
    fn from(other: ReducedPwmPin<PwmB>) -> Self {
        let mut pwmb = Self {
            reg: other.reg,
            pwm_base: other.pwm_base,
            pin_id: other.pin_id,
            mode: PhantomData,
        };
        pwmb.enable_pwm_a();
        pwmb
    }
}

//==================================================================================================
// PWMB implementations
//==================================================================================================

impl<PIN: TimPin, TIM: ValidTim> PwmPin<PIN, TIM, PwmB>
where
    (PIN, TIM): ValidTimAndPin<PIN, TIM>,
{
    pwmb_func!();
}

impl ReducedPwmPin<PwmB> {
    pwmb_func!();
}

//==================================================================================================
// Embedded HAL implementation: PWMA only
//==================================================================================================

impl<Pin: TimPin, Tim: ValidTim> embedded_hal::pwm::ErrorType for PwmPin<Pin, Tim> {
    type Error = Infallible;
}

impl embedded_hal::pwm::ErrorType for ReducedPwmPin {
    type Error = Infallible;
}

impl embedded_hal::pwm::SetDutyCycle for ReducedPwmPin {
    #[inline]
    fn max_duty_cycle(&self) -> u16 {
        DUTY_MAX
    }

    #[inline]
    fn set_duty_cycle(&mut self, duty: u16) -> Result<(), Self::Error> {
        self.pwm_base.current_duty = duty;
        let pwma_val: u64 = (self.pwm_base.current_rst_val as u64
            * (DUTY_MAX as u64 - self.pwm_base.current_duty as u64))
            / DUTY_MAX as u64;
        self.reg
            .reg()
            .pwma_value()
            .write(|w| unsafe { w.bits(pwma_val as u32) });
        Ok(())
    }
}

impl<Pin: TimPin, Tim: ValidTim> embedded_hal::pwm::SetDutyCycle for PwmPin<Pin, Tim> {
    #[inline]
    fn max_duty_cycle(&self) -> u16 {
        DUTY_MAX
    }

    #[inline]
    fn set_duty_cycle(&mut self, duty: u16) -> Result<(), Self::Error> {
        self.pwm_base.current_duty = duty;
        let pwma_val: u64 = (self.pwm_base.current_rst_val as u64
            * (DUTY_MAX as u64 - self.pwm_base.current_duty as u64))
            / DUTY_MAX as u64;
        self.reg
            .reg()
            .pwma_value()
            .write(|w| unsafe { w.bits(pwma_val as u32) });
        Ok(())
    }
}

/// Get the corresponding u16 duty cycle from a percent value ranging between 0.0 and 1.0.
///
/// Please note that this might load a lot of floating point code because this processor does not
/// have a FPU
pub fn get_duty_from_percent(percent: f32) -> u16 {
    if percent > 1.0 {
        DUTY_MAX
    } else if percent <= 0.0 {
        0
    } else {
        (percent * DUTY_MAX as f32) as u16
    }
}