esp_hal/ledc/
channel.rs

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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
//! # LEDC channel
//!
//! ## Overview
//! The LEDC Channel module  provides a high-level interface to
//! configure and control individual PWM channels of the LEDC peripheral.
//!
//! ## Configuration
//! The module allows precise and flexible control over LED lighting and other
//! `Pulse-Width Modulation (PWM)` applications by offering configurable duty
//! cycles and frequencies.

use super::timer::{TimerIFace, TimerSpeed};
use crate::{
    gpio::{
        interconnect::{OutputConnection, PeripheralOutput},
        OutputSignal,
    },
    pac::ledc::RegisterBlock,
    peripheral::{Peripheral, PeripheralRef},
    peripherals::LEDC,
};

/// Fade parameter sub-errors
#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum FadeError {
    /// Start duty % out of range
    StartDuty,
    /// End duty % out of range
    EndDuty,
    /// Duty % change from start to end is out of range
    DutyRange,
    /// Duration too long for timer frequency and duty resolution
    Duration,
}

/// Channel errors
#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Error {
    /// Invalid duty % value
    Duty,
    /// Timer not configured
    Timer,
    /// Channel not configured
    Channel,
    /// Fade parameters invalid
    Fade(FadeError),
}

/// Channel number
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Number {
    /// Channel 0
    Channel0 = 0,
    /// Channel 1
    Channel1 = 1,
    /// Channel 2
    Channel2 = 2,
    /// Channel 3
    Channel3 = 3,
    /// Channel 4
    Channel4 = 4,
    /// Channel 5
    Channel5 = 5,
    #[cfg(not(any(esp32c2, esp32c3, esp32c6, esp32h2)))]
    /// Channel 6
    Channel6 = 6,
    #[cfg(not(any(esp32c2, esp32c3, esp32c6, esp32h2)))]
    /// Channel 7
    Channel7 = 7,
}

/// Channel configuration
pub mod config {
    use crate::ledc::timer::{TimerIFace, TimerSpeed};

    #[derive(Debug, Clone, Copy, PartialEq)]
    #[cfg_attr(feature = "defmt", derive(defmt::Format))]
    /// Pin configuration for the LEDC channel.
    pub enum PinConfig {
        /// Push-pull pin configuration.
        PushPull,
        /// Open-drain pin configuration.
        OpenDrain,
    }

    /// Channel configuration
    #[derive(Copy, Clone)]
    pub struct Config<'a, S: TimerSpeed> {
        /// A reference to the timer associated with this channel.
        pub timer: &'a dyn TimerIFace<S>,
        /// The duty cycle percentage (0-100).
        pub duty_pct: u8,
        /// The pin configuration (PushPull or OpenDrain).
        pub pin_config: PinConfig,
    }
}

/// Channel interface
pub trait ChannelIFace<'a, S: TimerSpeed + 'a>
where
    Channel<'a, S>: ChannelHW,
{
    /// Configure channel
    fn configure(&mut self, config: config::Config<'a, S>) -> Result<(), Error>;

    /// Set channel duty HW
    fn set_duty(&self, duty_pct: u8) -> Result<(), Error>;

    /// Start a duty-cycle fade
    fn start_duty_fade(
        &self,
        start_duty_pct: u8,
        end_duty_pct: u8,
        duration_ms: u16,
    ) -> Result<(), Error>;

    /// Check whether a duty-cycle fade is running
    fn is_duty_fade_running(&self) -> bool;
}

/// Channel HW interface
pub trait ChannelHW {
    /// Configure Channel HW except for the duty which is set via
    /// [`Self::set_duty_hw`].
    fn configure_hw(&mut self) -> Result<(), Error>;
    /// Configure the hardware for the channel with a specific pin
    /// configuration.
    fn configure_hw_with_pin_config(&mut self, cfg: config::PinConfig) -> Result<(), Error>;

    /// Set channel duty HW
    fn set_duty_hw(&self, duty: u32);

    /// Start a duty-cycle fade HW
    fn start_duty_fade_hw(
        &self,
        start_duty: u32,
        duty_inc: bool,
        duty_steps: u16,
        cycles_per_step: u16,
        duty_per_cycle: u16,
    );

    /// Check whether a duty-cycle fade is running HW
    fn is_duty_fade_running_hw(&self) -> bool;
}

/// Channel struct
pub struct Channel<'a, S: TimerSpeed> {
    ledc: &'a RegisterBlock,
    timer: Option<&'a dyn TimerIFace<S>>,
    number: Number,
    output_pin: PeripheralRef<'a, OutputConnection>,
}

impl<'a, S: TimerSpeed> Channel<'a, S> {
    /// Return a new channel
    pub fn new(
        number: Number,
        output_pin: impl Peripheral<P = impl PeripheralOutput> + 'a,
    ) -> Self {
        crate::into_mapped_ref!(output_pin);
        let ledc = LEDC::regs();
        Channel {
            ledc,
            timer: None,
            number,
            output_pin,
        }
    }
}

impl<'a, S: TimerSpeed> ChannelIFace<'a, S> for Channel<'a, S>
where
    Channel<'a, S>: ChannelHW,
{
    /// Configure channel
    fn configure(&mut self, config: config::Config<'a, S>) -> Result<(), Error> {
        self.timer = Some(config.timer);

        self.set_duty(config.duty_pct)?;
        self.configure_hw_with_pin_config(config.pin_config)?;

        Ok(())
    }

    /// Set duty % of channel
    fn set_duty(&self, duty_pct: u8) -> Result<(), Error> {
        let duty_exp;
        if let Some(timer) = self.timer {
            if let Some(timer_duty) = timer.duty() {
                duty_exp = timer_duty as u32;
            } else {
                return Err(Error::Timer);
            }
        } else {
            return Err(Error::Channel);
        }

        let duty_range = 2u32.pow(duty_exp);
        let duty_value = (duty_range * duty_pct as u32) / 100;

        if duty_pct > 100u8 {
            // duty_pct greater than 100%
            return Err(Error::Duty);
        }

        self.set_duty_hw(duty_value);

        Ok(())
    }

    /// Start a duty fade from one % to another.
    ///
    /// There's a constraint on the combination of timer frequency, timer PWM
    /// duty resolution (the bit count), the fade "range" (abs(start-end)), and
    /// the duration:
    ///
    /// frequency * duration / ((1<<bit_count) * abs(start-end)) < 1024
    ///
    /// Small percentage changes, long durations, coarse PWM resolutions (that
    /// is, low bit counts), and high timer frequencies will all be more likely
    /// to fail this requirement.  If it does fail, this function will return
    /// an error Result.
    fn start_duty_fade(
        &self,
        start_duty_pct: u8,
        end_duty_pct: u8,
        duration_ms: u16,
    ) -> Result<(), Error> {
        let duty_exp;
        let frequency;
        if start_duty_pct > 100u8 {
            return Err(Error::Fade(FadeError::StartDuty));
        }
        if end_duty_pct > 100u8 {
            return Err(Error::Fade(FadeError::EndDuty));
        }
        if let Some(timer) = self.timer {
            if let Some(timer_duty) = timer.duty() {
                if timer.frequency() > 0 {
                    duty_exp = timer_duty as u32;
                    frequency = timer.frequency();
                } else {
                    return Err(Error::Timer);
                }
            } else {
                return Err(Error::Timer);
            }
        } else {
            return Err(Error::Channel);
        }

        let duty_range = (1u32 << duty_exp) - 1;
        let start_duty_value = (duty_range * start_duty_pct as u32) / 100;
        let end_duty_value = (duty_range * end_duty_pct as u32) / 100;

        // NB: since we do the multiplication first here, there's no loss of
        // precision from using milliseconds instead of (e.g.) nanoseconds.
        let pwm_cycles = (duration_ms as u32) * frequency / 1000;

        let abs_duty_diff = end_duty_value.abs_diff(start_duty_value);
        let duty_steps: u32 = u16::try_from(abs_duty_diff).unwrap_or(65535).into();
        // This conversion may fail if duration_ms is too big, and if either
        // duty_steps gets truncated, or the fade is over a short range of duty
        // percentages, so it's too small.  Returning an Err in either case is
        // fine: shortening the duration_ms will sort things out.
        let cycles_per_step: u16 = (pwm_cycles / duty_steps)
            .try_into()
            .map_err(|_| Error::Fade(FadeError::Duration))
            .and_then(|res| {
                if res > 1023 {
                    Err(Error::Fade(FadeError::Duration))
                } else {
                    Ok(res)
                }
            })?;
        // This can't fail unless abs_duty_diff is bigger than 65536*65535-1,
        // and so duty_steps gets truncated.  But that requires duty_exp to be
        // at least 32, and the hardware only supports up to 20.  Still, handle
        // it in case something changes in the future.
        let duty_per_cycle: u16 = (abs_duty_diff / duty_steps)
            .try_into()
            .map_err(|_| Error::Fade(FadeError::DutyRange))?;

        self.start_duty_fade_hw(
            start_duty_value,
            end_duty_value > start_duty_value,
            duty_steps.try_into().unwrap(),
            cycles_per_step,
            duty_per_cycle,
        );

        Ok(())
    }

    fn is_duty_fade_running(&self) -> bool {
        self.is_duty_fade_running_hw()
    }
}

mod ehal1 {
    use embedded_hal::pwm::{self, ErrorKind, ErrorType, SetDutyCycle};

    use super::{Channel, ChannelHW, Error};
    use crate::ledc::timer::TimerSpeed;

    impl pwm::Error for Error {
        fn kind(&self) -> pwm::ErrorKind {
            ErrorKind::Other
        }
    }

    impl<S: TimerSpeed> ErrorType for Channel<'_, S> {
        type Error = Error;
    }

    impl<'a, S: TimerSpeed> SetDutyCycle for Channel<'a, S>
    where
        Channel<'a, S>: ChannelHW,
    {
        fn max_duty_cycle(&self) -> u16 {
            let duty_exp;

            if let Some(timer_duty) = self.timer.and_then(|timer| timer.duty()) {
                duty_exp = timer_duty as u32;
            } else {
                return 0;
            }

            let duty_range = 2u32.pow(duty_exp);

            duty_range as u16
        }

        fn set_duty_cycle(&mut self, mut duty: u16) -> Result<(), Self::Error> {
            let max = self.max_duty_cycle();
            duty = if duty > max { max } else { duty };
            self.set_duty_hw(duty.into());
            Ok(())
        }
    }
}

impl<S: crate::ledc::timer::TimerSpeed> Channel<'_, S> {
    #[cfg(esp32)]
    fn set_channel(&mut self, timer_number: u8) {
        if S::IS_HS {
            let ch = self.ledc.hsch(self.number as usize);
            ch.hpoint().write(|w| unsafe { w.hpoint().bits(0x0) });
            ch.conf0()
                .modify(|_, w| unsafe { w.sig_out_en().set_bit().timer_sel().bits(timer_number) });
        } else {
            let ch = self.ledc.lsch(self.number as usize);
            ch.hpoint().write(|w| unsafe { w.hpoint().bits(0x0) });
            ch.conf0()
                .modify(|_, w| unsafe { w.sig_out_en().set_bit().timer_sel().bits(timer_number) });
        }
        self.start_duty_without_fading();
    }
    #[cfg(not(esp32))]
    fn set_channel(&mut self, timer_number: u8) {
        {
            let ch = self.ledc.ch(self.number as usize);
            ch.hpoint().write(|w| unsafe { w.hpoint().bits(0x0) });
            ch.conf0().modify(|_, w| {
                w.sig_out_en().set_bit();
                unsafe { w.timer_sel().bits(timer_number) }
            });
        }

        // this is needed to make low duty-resolutions / high frequencies work
        #[cfg(any(esp32h2, esp32c6))]
        self.ledc
            .ch_gamma_wr_addr(self.number as usize)
            .write(|w| unsafe { w.bits(0) });

        self.start_duty_without_fading();
    }

    #[cfg(esp32)]
    fn start_duty_without_fading(&self) {
        if S::IS_HS {
            self.ledc
                .hsch(self.number as usize)
                .conf1()
                .write(|w| unsafe {
                    w.duty_start().set_bit();
                    w.duty_inc().set_bit();
                    w.duty_num().bits(0x1);
                    w.duty_cycle().bits(0x1);
                    w.duty_scale().bits(0x0)
                });
        } else {
            self.ledc
                .lsch(self.number as usize)
                .conf1()
                .write(|w| unsafe {
                    w.duty_start().set_bit();
                    w.duty_inc().set_bit();
                    w.duty_num().bits(0x1);
                    w.duty_cycle().bits(0x1);
                    w.duty_scale().bits(0x0)
                });
        }
    }
    #[cfg(any(esp32c6, esp32h2))]
    fn start_duty_without_fading(&self) {
        let cnum = self.number as usize;
        self.ledc
            .ch(cnum)
            .conf1()
            .write(|w| w.duty_start().set_bit());
        self.ledc.ch_gamma_wr(cnum).write(|w| {
            w.ch_gamma_duty_inc().set_bit();
            unsafe {
                w.ch_gamma_duty_num().bits(0x1);
                w.ch_gamma_duty_cycle().bits(0x1);
                w.ch_gamma_scale().bits(0x0)
            }
        });
    }
    #[cfg(not(any(esp32, esp32c6, esp32h2)))]
    fn start_duty_without_fading(&self) {
        self.ledc.ch(self.number as usize).conf1().write(|w| {
            w.duty_start().set_bit();
            w.duty_inc().set_bit();
            unsafe {
                w.duty_num().bits(0x1);
                w.duty_cycle().bits(0x1);
                w.duty_scale().bits(0x0)
            }
        });
    }

    #[cfg(esp32)]
    fn start_duty_fade_inner(
        &self,
        duty_inc: bool,
        duty_steps: u16,
        cycles_per_step: u16,
        duty_per_cycle: u16,
    ) {
        if S::IS_HS {
            self.ledc
                .hsch(self.number as usize)
                .conf1()
                .write(|w| unsafe {
                    w.duty_start()
                        .set_bit()
                        .duty_inc()
                        .variant(duty_inc)
                        .duty_num() // count of incs before stopping
                        .bits(duty_steps)
                        .duty_cycle() // overflows between incs
                        .bits(cycles_per_step)
                        .duty_scale()
                        .bits(duty_per_cycle)
                });
        } else {
            self.ledc
                .lsch(self.number as usize)
                .conf1()
                .write(|w| unsafe {
                    w.duty_start()
                        .set_bit()
                        .duty_inc()
                        .variant(duty_inc)
                        .duty_num() // count of incs before stopping
                        .bits(duty_steps)
                        .duty_cycle() // overflows between incs
                        .bits(cycles_per_step)
                        .duty_scale()
                        .bits(duty_per_cycle)
                });
        }
    }

    #[cfg(any(esp32c6, esp32h2))]
    fn start_duty_fade_inner(
        &self,
        duty_inc: bool,
        duty_steps: u16,
        cycles_per_step: u16,
        duty_per_cycle: u16,
    ) {
        let cnum = self.number as usize;
        self.ledc
            .ch(cnum)
            .conf1()
            .write(|w| w.duty_start().set_bit());
        self.ledc.ch_gamma_wr(cnum).write(|w| unsafe {
            w.ch_gamma_duty_inc()
                .variant(duty_inc)
                .ch_gamma_duty_num() // count of incs before stopping
                .bits(duty_steps)
                .ch_gamma_duty_cycle() // overflows between incs
                .bits(cycles_per_step)
                .ch_gamma_scale()
                .bits(duty_per_cycle)
        });
        self.ledc
            .ch_gamma_wr_addr(cnum)
            .write(|w| unsafe { w.ch_gamma_wr_addr().bits(0) });
        self.ledc
            .ch_gamma_conf(cnum)
            .write(|w| unsafe { w.ch_gamma_entry_num().bits(0x1) });
    }

    #[cfg(not(any(esp32, esp32c6, esp32h2)))]
    fn start_duty_fade_inner(
        &self,
        duty_inc: bool,
        duty_steps: u16,
        cycles_per_step: u16,
        duty_per_cycle: u16,
    ) {
        self.ledc
            .ch(self.number as usize)
            .conf1()
            .write(|w| unsafe {
                w.duty_start().set_bit();
                w.duty_inc().variant(duty_inc);
                // count of incs before stopping
                w.duty_num().bits(duty_steps);
                // overflows between incs
                w.duty_cycle().bits(cycles_per_step);
                w.duty_scale().bits(duty_per_cycle)
            });
    }

    #[cfg(esp32)]
    fn update_channel(&self) {
        if !S::IS_HS {
            self.ledc
                .lsch(self.number as usize)
                .conf0()
                .modify(|_, w| w.para_up().set_bit());
        }
    }
    #[cfg(not(esp32))]
    fn update_channel(&self) {
        self.ledc
            .ch(self.number as usize)
            .conf0()
            .modify(|_, w| w.para_up().set_bit());
    }
}

impl<S> ChannelHW for Channel<'_, S>
where
    S: crate::ledc::timer::TimerSpeed,
{
    /// Configure Channel HW
    fn configure_hw(&mut self) -> Result<(), Error> {
        self.configure_hw_with_pin_config(config::PinConfig::PushPull)
    }
    fn configure_hw_with_pin_config(&mut self, cfg: config::PinConfig) -> Result<(), Error> {
        if let Some(timer) = self.timer {
            if !timer.is_configured() {
                return Err(Error::Timer);
            }

            match cfg {
                config::PinConfig::PushPull => self.output_pin.set_to_push_pull_output(),
                config::PinConfig::OpenDrain => self.output_pin.set_to_open_drain_output(),
            };

            let timer_number = timer.number() as u8;

            self.set_channel(timer_number);
            self.update_channel();

            #[cfg(esp32)]
            let signal = if S::IS_HS {
                #[cfg(not(any(esp32c2, esp32c3, esp32c6, esp32h2)))]
                match self.number {
                    Number::Channel0 => OutputSignal::LEDC_HS_SIG0,
                    Number::Channel1 => OutputSignal::LEDC_HS_SIG1,
                    Number::Channel2 => OutputSignal::LEDC_HS_SIG2,
                    Number::Channel3 => OutputSignal::LEDC_HS_SIG3,
                    Number::Channel4 => OutputSignal::LEDC_HS_SIG4,
                    Number::Channel5 => OutputSignal::LEDC_HS_SIG5,
                    Number::Channel6 => OutputSignal::LEDC_HS_SIG6,
                    Number::Channel7 => OutputSignal::LEDC_HS_SIG7,
                }
            } else {
                match self.number {
                    Number::Channel0 => OutputSignal::LEDC_LS_SIG0,
                    Number::Channel1 => OutputSignal::LEDC_LS_SIG1,
                    Number::Channel2 => OutputSignal::LEDC_LS_SIG2,
                    Number::Channel3 => OutputSignal::LEDC_LS_SIG3,
                    Number::Channel4 => OutputSignal::LEDC_LS_SIG4,
                    Number::Channel5 => OutputSignal::LEDC_LS_SIG5,
                    #[cfg(not(any(esp32c2, esp32c3, esp32c6, esp32h2)))]
                    Number::Channel6 => OutputSignal::LEDC_LS_SIG6,
                    #[cfg(not(any(esp32c2, esp32c3, esp32c6, esp32h2)))]
                    Number::Channel7 => OutputSignal::LEDC_LS_SIG7,
                }
            };
            #[cfg(not(esp32))]
            let signal = match self.number {
                Number::Channel0 => OutputSignal::LEDC_LS_SIG0,
                Number::Channel1 => OutputSignal::LEDC_LS_SIG1,
                Number::Channel2 => OutputSignal::LEDC_LS_SIG2,
                Number::Channel3 => OutputSignal::LEDC_LS_SIG3,
                Number::Channel4 => OutputSignal::LEDC_LS_SIG4,
                Number::Channel5 => OutputSignal::LEDC_LS_SIG5,
                #[cfg(not(any(esp32c2, esp32c3, esp32c6, esp32h2)))]
                Number::Channel6 => OutputSignal::LEDC_LS_SIG6,
                #[cfg(not(any(esp32c2, esp32c3, esp32c6, esp32h2)))]
                Number::Channel7 => OutputSignal::LEDC_LS_SIG7,
            };

            signal.connect_to(&mut self.output_pin);
        } else {
            return Err(Error::Timer);
        }

        Ok(())
    }

    /// Set duty in channel HW
    #[cfg(esp32)]
    fn set_duty_hw(&self, duty: u32) {
        if S::IS_HS {
            self.ledc
                .hsch(self.number as usize)
                .duty()
                .write(|w| unsafe { w.duty().bits(duty << 4) });
        } else {
            self.ledc
                .lsch(self.number as usize)
                .duty()
                .write(|w| unsafe { w.duty().bits(duty << 4) });
        }
        self.start_duty_without_fading();
        self.update_channel();
    }

    /// Set duty in channel HW
    #[cfg(not(esp32))]
    fn set_duty_hw(&self, duty: u32) {
        self.ledc
            .ch(self.number as usize)
            .duty()
            .write(|w| unsafe { w.duty().bits(duty << 4) });
        self.start_duty_without_fading();
        self.update_channel();
    }

    /// Start a duty-cycle fade HW
    #[cfg(esp32)]
    fn start_duty_fade_hw(
        &self,
        start_duty: u32,
        duty_inc: bool,
        duty_steps: u16,
        cycles_per_step: u16,
        duty_per_cycle: u16,
    ) {
        if S::IS_HS {
            self.ledc
                .hsch(self.number as usize)
                .duty()
                .write(|w| unsafe { w.duty().bits(start_duty << 4) });
            self.ledc
                .int_clr()
                .write(|w| w.duty_chng_end_hsch(self.number as u8).clear_bit_by_one());
        } else {
            self.ledc
                .lsch(self.number as usize)
                .duty()
                .write(|w| unsafe { w.duty().bits(start_duty << 4) });
            self.ledc
                .int_clr()
                .write(|w| w.duty_chng_end_lsch(self.number as u8).clear_bit_by_one());
        }
        self.start_duty_fade_inner(duty_inc, duty_steps, cycles_per_step, duty_per_cycle);
        self.update_channel();
    }

    /// Start a duty-cycle fade HW
    #[cfg(not(esp32))]
    fn start_duty_fade_hw(
        &self,
        start_duty: u32,
        duty_inc: bool,
        duty_steps: u16,
        cycles_per_step: u16,
        duty_per_cycle: u16,
    ) {
        self.ledc
            .ch(self.number as usize)
            .duty()
            .write(|w| unsafe { w.duty().bits(start_duty << 4) });
        self.ledc
            .int_clr()
            .write(|w| w.duty_chng_end_ch(self.number as u8).clear_bit_by_one());
        self.start_duty_fade_inner(duty_inc, duty_steps, cycles_per_step, duty_per_cycle);
        self.update_channel();
    }

    #[cfg(esp32)]
    fn is_duty_fade_running_hw(&self) -> bool {
        let reg = self.ledc.int_raw().read();
        if S::IS_HS {
            reg.duty_chng_end_hsch(self.number as u8).bit_is_clear()
        } else {
            reg.duty_chng_end_lsch(self.number as u8).bit_is_clear()
        }
    }

    #[cfg(not(esp32))]
    fn is_duty_fade_running_hw(&self) -> bool {
        self.ledc
            .int_raw()
            .read()
            .duty_chng_end_ch(self.number as u8)
            .bit_is_clear()
    }
}