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
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
//! Remote Control (RMT) module driver.
//!
//! The RMT (Remote Control) module driver can be used to send infrared remote control
//! signals. Due to flexibility of RMT module, the driver can also be used to generate or receive
//! many other types of signals.
//!
//! This module is an abstraction around the [IDF RMT](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/rmt.html)
//! implementation. It is recommended to read before using this module.
//!
//! This is implementation currently supports transmission only.
//!
//! Not supported:
//! * Interrupts.
//! * Receiving.
//! * Change of config after initialisation.
//!
//! # Example
//!
//! ```
//! // Prepare the config.
//! let config = Config::new().clock_divider(1);
//!
//! // Retrieve the output pin and channel from peripherals.
//! let peripherals = Peripherals::take().unwrap();
//! let channel = peripherals.rmt.channel0;
//! let pin = peripherals.pins.gpio18;
//!
//! // Create an RMT transmitter.
//! let tx = TxRmtDriver::new(channel, pin, &config)?;
//!
//! // Prepare signal pulse signal to be sent.
//! let low = Pulse::new(PinState::Low, PulseTicks::new(10)?);
//! let high = Pulse::new(PinState::High, PulseTicks::new(10)?);
//! let mut signal = FixedLengthSignal::<2>::new();
//! signal.set(0, &(low, high))?;
//! signal.set(1, &(high, low))?;
//!
//! // Transmit the signal.
//! tx.start(signal)?;
//!```
//!
//! See the `examples/` folder of this repository for more.
//!
//! # Loading pulses
//! There are two ways of preparing pulse signal. [FixedLengthSignal] and [VariableLengthSignal]. These
//! implement the [Signal] trait.
//!
//! [FixedLengthSignal] lives on the stack and must have the items set in pairs of [Pulse]s. This is
//! due to the internal implementation of RMT, and const generics limitations.
//!
//! [VariableLengthSignal] allows you to use the heap and incrementally add pulse items without knowing the size
//! ahead of time.

use core::cell::UnsafeCell;
use core::marker::PhantomData;
use core::time::Duration;
use core::{ptr, slice};

#[cfg(feature = "alloc")]
extern crate alloc;

use esp_idf_sys::*;

use crate::gpio::InputPin;
use crate::gpio::OutputPin;
use crate::interrupt::InterruptType;
use crate::peripheral::Peripheral;
use crate::units::Hertz;

use config::ReceiveConfig;
use config::TransmitConfig;

pub use chip::*;

// Might not always be available in the generated `esp-idf-sys` bindings
const ERR_ERANGE: esp_err_t = 34;
const ERR_EOVERFLOW: esp_err_t = 139;

pub type RmtTransmitConfig = config::TransmitConfig;
pub type RmtReceiveConfig = config::ReceiveConfig;

/// A `Low` (0) or `High` (1) state for a pin.
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum PinState {
    Low,
    High,
}

impl From<u32> for PinState {
    fn from(state: u32) -> Self {
        if state == 0 {
            Self::Low
        } else {
            Self::High
        }
    }
}

/// A `Pulse` contains a pin state and a tick count, used in creating a [`Signal`].
///
/// The real time duration of a tick depends on the [`Config::clock_divider`] setting.
///
/// You can create a `Pulse` with a [`Duration`] by using [`Pulse::new_with_duration`].
///
/// # Example
/// ```rust
/// # use esp_idf_hal::rmt::PulseTicks;
/// let pulse = Pulse::new(PinState::High, PulseTicks::new(32));
/// ```
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct Pulse {
    pub ticks: PulseTicks,
    pub pin_state: PinState,
}

impl Pulse {
    pub const fn zero() -> Self {
        Self::new(PinState::Low, PulseTicks::zero())
    }

    pub const fn new(pin_state: PinState, ticks: PulseTicks) -> Self {
        Pulse { pin_state, ticks }
    }

    /// Create a [`Pulse`] using a [`Duration`].
    ///
    /// The ticks frequency, which depends on the clock divider set on the channel within a
    /// [`Transmit`]. To get the frequency for the `ticks_hz` argument, use [`Transmit::counter_clock()`].
    ///
    /// # Example
    /// ```
    /// # use esp_idf_sys::EspError;
    /// # use esp_idf_hal::gpio::Output;
    /// # use esp_idf_hal::rmt::Channel::Channel0;
    /// # fn example() -> Result<(), EspError> {
    /// # let peripherals = Peripherals::take()?;
    /// # let led = peripherals.pins.gpio18.into_output()?;
    /// # let channel = peripherals.rmt.channel0;
    /// # let config = Config::new()?;
    /// let tx = Transmit::new(led, channel, &config)?;
    /// let ticks_hz = tx.counter_clock()?;
    /// let pulse = Pulse::new_with_duration(ticks_hz, PinState::High, Duration::from_nanos(500))?;
    /// # }
    /// ```
    pub fn new_with_duration(
        ticks_hz: Hertz,
        pin_state: PinState,
        duration: &Duration,
    ) -> Result<Self, EspError> {
        let ticks = PulseTicks::new_with_duration(ticks_hz, duration)?;
        Ok(Self::new(pin_state, ticks))
    }
}

impl Default for Pulse {
    fn default() -> Self {
        Self::zero()
    }
}

/// Number of ticks, restricting the range to 0 to 32,767.
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct PulseTicks(u16);

impl PulseTicks {
    const MAX: u16 = 32767;

    pub const fn zero() -> Self {
        Self(0)
    }

    /// Use the maximum value of 32767.
    pub const fn max() -> Self {
        Self(Self::MAX)
    }

    /// Needs to be unsigned 15 bits: 0-32767 inclusive, otherwise an [ESP_ERR_INVALID_ARG] is
    /// returned.
    pub fn new(ticks: u16) -> Result<Self, EspError> {
        if ticks > Self::MAX {
            Err(EspError::from_infallible::<ESP_ERR_INVALID_ARG>())
        } else {
            Ok(Self(ticks))
        }
    }

    /// Convert a `Duration` into `PulseTicks`.
    ///
    /// See `Pulse::new_with_duration()` for details.
    pub fn new_with_duration(ticks_hz: Hertz, duration: &Duration) -> Result<Self, EspError> {
        Self::new(duration_to_ticks(ticks_hz, duration)?)
    }

    pub fn ticks(&self) -> u16 {
        self.0
    }

    pub fn duration(&self, ticks_hz: Hertz) -> Result<Duration, EspError> {
        ticks_to_duration(ticks_hz, self.ticks())
    }
}

impl Default for PulseTicks {
    fn default() -> Self {
        Self::zero()
    }
}

/// A utility to convert a duration into ticks, depending on the clock ticks.
pub fn duration_to_ticks(ticks_hz: Hertz, duration: &Duration) -> Result<u16, EspError> {
    let ticks = duration
        .as_nanos()
        .checked_mul(u32::from(ticks_hz) as u128)
        .ok_or_else(|| EspError::from(ERR_EOVERFLOW).unwrap())?
        / 1_000_000_000;

    u16::try_from(ticks).map_err(|_| EspError::from(ERR_EOVERFLOW).unwrap())
}

/// A utility to convert ticks into duration, depending on the clock ticks.
pub fn ticks_to_duration(ticks_hz: Hertz, ticks: u16) -> Result<Duration, EspError> {
    let duration = 1_000_000_000_u128
        .checked_mul(ticks as u128)
        .ok_or_else(|| EspError::from(ERR_EOVERFLOW).unwrap())?
        / u32::from(ticks_hz) as u128;

    u64::try_from(duration)
        .map(Duration::from_nanos)
        .map_err(|_| EspError::from(ERR_EOVERFLOW).unwrap())
}

pub type TxRmtConfig = config::TransmitConfig;
pub type RxRmtConfig = config::ReceiveConfig;

/// Types used for configuring the [`rmt`][crate::rmt] module.
///
/// [`Config`] is used when creating a [`Transmit`][crate::rmt::Transmit] instance.
///
/// # Example
/// ```
/// # use esp_idf_hal::units::FromValueType;
/// let carrier = CarrierConfig::new()
///     .duty_percent(DutyPercent::new(50)?)
///     .frequency(611.Hz());
///
/// let config = Config::new()
///     .carrier(Some(carrier))
///     .looping(Loop::Endless)
///     .clock_divider(255);
///
/// ```
pub mod config {
    use enumset::EnumSet;
    use esp_idf_sys::{EspError, ESP_ERR_INVALID_ARG};

    use super::PinState;
    use crate::{
        interrupt::InterruptType,
        units::{FromValueType, Hertz},
    };

    /// A percentage from 0 to 100%, used to specify the duty percentage in [`CarrierConfig`].
    #[derive(Debug, Copy, Clone, Eq, PartialEq)]
    pub struct DutyPercent(pub(super) u8);

    impl DutyPercent {
        /// Must be between 0 and 100, otherwise an error is returned.
        pub fn new(v: u8) -> Result<Self, EspError> {
            if v > 100 {
                Err(EspError::from_infallible::<ESP_ERR_INVALID_ARG>())
            } else {
                Ok(Self(v))
            }
        }
    }

    /// Configuration for when enabling a carrier frequency.
    #[derive(Debug, Copy, Clone, Eq, PartialEq)]
    pub struct CarrierConfig {
        /// Frequency of the carrier in Hz.
        pub frequency: Hertz,

        /// Level of the RMT output, when the carrier is applied.
        pub carrier_level: PinState,

        /// Duty cycle of the carrier signal in percent (%).
        pub duty_percent: DutyPercent,
    }

    impl CarrierConfig {
        pub fn new() -> Self {
            Self {
                frequency: 38.kHz().into(),
                carrier_level: PinState::High,
                duty_percent: DutyPercent(33),
            }
        }

        #[must_use]
        pub fn frequency(mut self, hz: Hertz) -> Self {
            self.frequency = hz;
            self
        }

        #[must_use]
        pub fn carrier_level(mut self, state: PinState) -> Self {
            self.carrier_level = state;
            self
        }

        #[must_use]
        pub fn duty_percent(mut self, duty: DutyPercent) -> Self {
            self.duty_percent = duty;
            self
        }
    }

    impl Default for CarrierConfig {
        /// Defaults from `<https://github.com/espressif/esp-idf/blob/master/components/driver/include/driver/rmt.h#L101>`
        fn default() -> Self {
            Self::new()
        }
    }

    /// Configuration setting for looping a signal.
    #[derive(Copy, Clone, Debug, PartialEq, Eq)]
    pub enum Loop {
        None,
        Endless,
        #[cfg(any(
            all(not(esp_idf_version_major = "4"), not(esp_idf_version_major = "5")),
            all(esp_idf_version_major = "5", not(esp_idf_version_minor = "0")),
            not(esp32)
        ))]
        Count(u32),
    }

    /// Used when creating a [`Transmit`][crate::rmt::Transmit] instance.
    #[derive(Debug, Clone)]
    pub struct TransmitConfig {
        pub clock_divider: u8,
        pub mem_block_num: u8,
        pub carrier: Option<CarrierConfig>,
        // TODO: `loop` is taken. Maybe can change to `repeat` even though it doesn't match the IDF.
        pub looping: Loop,

        /// Enable and set the signal level on the output if idle.
        pub idle: Option<PinState>,

        /// Channel can work during APB clock scaling.
        ///
        /// When set, RMT channel will take REF_TICK or XTAL as source clock. The benefit is, RMT
        /// channel can continue work even when APB clock is changing.
        pub aware_dfs: bool,

        pub intr_flags: EnumSet<InterruptType>,
    }

    impl TransmitConfig {
        pub fn new() -> Self {
            Self {
                aware_dfs: false,
                mem_block_num: 1,
                clock_divider: 80,
                looping: Loop::None,
                carrier: None,
                idle: Some(PinState::Low),
                intr_flags: EnumSet::<InterruptType>::empty(),
            }
        }

        #[must_use]
        pub fn aware_dfs(mut self, enable: bool) -> Self {
            self.aware_dfs = enable;
            self
        }

        #[must_use]
        pub fn mem_block_num(mut self, mem_block_num: u8) -> Self {
            self.mem_block_num = mem_block_num;
            self
        }

        #[must_use]
        pub fn clock_divider(mut self, divider: u8) -> Self {
            self.clock_divider = divider;
            self
        }

        #[must_use]
        pub fn looping(mut self, looping: Loop) -> Self {
            self.looping = looping;
            self
        }

        #[must_use]
        pub fn carrier(mut self, carrier: Option<CarrierConfig>) -> Self {
            self.carrier = carrier;
            self
        }

        #[must_use]
        pub fn idle(mut self, idle: Option<PinState>) -> Self {
            self.idle = idle;
            self
        }

        #[must_use]
        pub fn intr_flags(mut self, flags: EnumSet<InterruptType>) -> Self {
            self.intr_flags = flags;
            self
        }
    }

    impl Default for TransmitConfig {
        /// Defaults from `<https://github.com/espressif/esp-idf/blob/master/components/driver/include/driver/rmt.h#L101>`
        fn default() -> Self {
            Self::new()
        }
    }

    /// Used when creating a [`Receive`][crate::rmt::Receive] instance.
    #[derive(Debug, Clone)]
    pub struct ReceiveConfig {
        pub clock_divider: u8,
        pub mem_block_num: u8,
        pub idle_threshold: u16,
        pub filter_ticks_thresh: u8,
        pub filter_en: bool,
        pub carrier: Option<CarrierConfig>,
        pub intr_flags: EnumSet<InterruptType>,
    }

    impl ReceiveConfig {
        pub fn new() -> Self {
            Self::default()
        }

        #[must_use]
        pub fn clock_divider(mut self, divider: u8) -> Self {
            self.clock_divider = divider;
            self
        }

        #[must_use]
        pub fn mem_block_num(mut self, mem_block_num: u8) -> Self {
            self.mem_block_num = mem_block_num;
            self
        }

        #[must_use]
        pub fn idle_threshold(mut self, threshold: u16) -> Self {
            self.idle_threshold = threshold;
            self
        }

        #[must_use]
        pub fn filter_ticks_thresh(mut self, threshold: u8) -> Self {
            self.filter_ticks_thresh = threshold;
            self
        }

        #[must_use]
        pub fn filter_en(mut self, enable: bool) -> Self {
            self.filter_en = enable;
            self
        }

        #[must_use]
        pub fn carrier(mut self, carrier: Option<CarrierConfig>) -> Self {
            self.carrier = carrier;
            self
        }

        #[must_use]
        pub fn intr_flags(mut self, flags: EnumSet<InterruptType>) -> Self {
            self.intr_flags = flags;
            self
        }
    }

    impl Default for ReceiveConfig {
        /// Defaults from `<https://github.com/espressif/esp-idf/blob/master/components/driver/include/driver/rmt.h#L110>`
        fn default() -> Self {
            Self {
                clock_divider: 80,        // one microsecond clock period
                mem_block_num: 1, // maximum of 448 rmt items can be captured (mem_block_num=0 will have max 512 rmt items)
                idle_threshold: 12000, // 1.2 milliseconds, pulse greater than this will generate interrupt
                filter_ticks_thresh: 100, // 100 microseconds, pulses less than this will be ignored
                filter_en: true,
                carrier: None,
                intr_flags: EnumSet::<InterruptType>::empty(),
            }
        }
    }
}

/// The RMT transmitter driver.
///
/// Use [`TxRmtDriver::start()`] or [`TxRmtDriver::start_blocking()`] to transmit pulses.
///
/// See the [rmt module][crate::rmt] for more information.

pub struct TxRmtDriver<'d> {
    channel: u8,
    _p: PhantomData<&'d mut ()>,
}

impl<'d> TxRmtDriver<'d> {
    /// Initialise the rmt module with the specified pin, channel and configuration.
    ///
    /// To uninstall the driver just drop it.
    ///
    /// Internally this calls `rmt_config()` and `rmt_driver_install()`.
    pub fn new<C: RmtChannel>(
        _channel: impl Peripheral<P = C> + 'd,
        pin: impl Peripheral<P = impl OutputPin> + 'd,
        config: &TransmitConfig,
    ) -> Result<Self, EspError> {
        crate::into_ref!(pin);

        let mut flags = 0;
        if config.aware_dfs {
            flags |= RMT_CHANNEL_FLAGS_AWARE_DFS;
        }

        let carrier_en = config.carrier.is_some();
        let carrier = config.carrier.unwrap_or_default();

        let sys_config = rmt_config_t {
            rmt_mode: rmt_mode_t_RMT_MODE_TX,
            channel: C::channel(),
            gpio_num: pin.pin(),
            clk_div: config.clock_divider,
            mem_block_num: config.mem_block_num,
            flags,
            __bindgen_anon_1: rmt_config_t__bindgen_ty_1 {
                tx_config: rmt_tx_config_t {
                    carrier_en,
                    carrier_freq_hz: carrier.frequency.into(),
                    carrier_level: carrier.carrier_level as u32,
                    carrier_duty_percent: carrier.duty_percent.0,
                    idle_output_en: config.idle.is_some(),
                    idle_level: config.idle.map(|i| i as u32).unwrap_or(0),
                    loop_en: config.looping != config::Loop::None,
                    #[cfg(any(
                        all(not(esp_idf_version_major = "4"), not(esp_idf_version_major = "5")),
                        all(esp_idf_version_major = "5", not(esp_idf_version_minor = "0")),
                        not(esp32)
                    ))]
                    loop_count: match config.looping {
                        config::Loop::Count(count) if count > 0 && count < 1024 => count,
                        _ => 0,
                    },
                },
            },
        };

        unsafe {
            esp!(rmt_config(&sys_config))?;
            esp!(rmt_driver_install(
                C::channel(),
                0,
                InterruptType::to_native(config.intr_flags) as _
            ))?;
        }

        Ok(Self {
            channel: C::channel() as _,
            _p: PhantomData,
        })
    }

    /// Get speed of the channel’s internal counter clock.
    ///
    /// This calls [rmt_get_counter_clock()][rmt_get_counter_clock]
    /// internally. It is used for calculating the number of ticks per second for pulses.
    ///
    /// See [Pulse::new_with_duration()].
    ///
    /// [rmt_get_counter_clock]: https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/peripherals/rmt.html#_CPPv421rmt_get_counter_clock13rmt_channel_tP8uint32_t
    pub fn counter_clock(&self) -> Result<Hertz, EspError> {
        let mut ticks_hz: u32 = 0;
        esp!(unsafe { rmt_get_counter_clock(self.channel(), &mut ticks_hz) })?;
        Ok(ticks_hz.into())
    }

    /// Start sending the given signal without blocking.
    ///
    /// `signal` is captured for safety so that the user can't change the data while transmitting.
    pub fn start<S>(&mut self, signal: S) -> Result<(), EspError>
    where
        S: Signal,
    {
        self.write_items(&signal, false)
    }

    /// Start sending the given signal while blocking.
    pub fn start_blocking<S>(&mut self, signal: &S) -> Result<(), EspError>
    where
        S: Signal + ?Sized,
    {
        self.write_items(signal, true)
    }

    fn write_items<S>(&mut self, signal: &S, block: bool) -> Result<(), EspError>
    where
        S: Signal + ?Sized,
    {
        let items = signal.as_slice();
        esp!(unsafe { rmt_write_items(self.channel(), items.as_ptr(), items.len() as i32, block) })
    }

    /// Transmit all items in `iter` without blocking.
    ///
    /// Note that this requires `iter` to be [`Box`]ed for an allocation free version see [`Self::start_iter_blocking`].
    ///
    /// ### Warning
    ///
    /// Iteration of `iter` happens inside an interrupt handler so beware of side-effects
    /// that don't work in interrupt handlers. Iteration must also be fast so that there
    /// are no time-gaps between successive transmissions where the perhipheral has to
    /// wait for items. This can cause weird behavior and can be counteracted with
    /// increasing [`Config::mem_block_num`] or making iteration more efficient.
    #[cfg(feature = "alloc")]
    pub fn start_iter<T>(&mut self, iter: T) -> Result<(), EspError>
    where
        T: Iterator<Item = Symbol> + Send + 'static,
    {
        let iter = alloc::boxed::Box::new(UnsafeCell::new(iter));
        unsafe {
            esp!(rmt_translator_init(
                self.channel(),
                Some(Self::translate_iterator::<T, true>),
            ))?;

            esp!(rmt_write_sample(
                self.channel(),
                alloc::boxed::Box::leak(iter) as *const _ as _,
                1,
                false
            ))
        }
    }

    /// Transmit all items in `iter`, blocking until all items are transmitted.
    ///
    /// This method does not require any allocations since the thread is paused until all
    /// items are transmitted. The iterator lives on the stack and will be dropped after
    /// all items are written and before this method returns.
    ///
    /// ### Warning
    ///
    /// Iteration of `iter` happens inside an interrupt handler so beware of side-effects
    /// that don't work in interrupt handlers. Iteration must also be fast so that there
    /// are no time-gaps between successive transmissions where the perhipheral has to
    /// wait for items. This can cause weird behavior and can be counteracted with
    /// increasing [`Config::mem_block_num`] or making iteration more efficient.

    pub fn start_iter_blocking<T>(&mut self, iter: T) -> Result<(), EspError>
    where
        T: Iterator<Item = Symbol> + Send,
    {
        let iter = UnsafeCell::new(iter);
        unsafe {
            // TODO: maybe use a separate struct so that we don't have to do this when
            // transmitting the same iterator type.
            esp!(rmt_translator_init(
                self.channel(),
                Some(Self::translate_iterator::<T, false>),
            ))?;
            esp!(rmt_write_sample(
                self.channel(),
                &iter as *const _ as _,
                24,
                true
            ))
        }
    }

    /// The translator that turns an iterator into `rmt_item32_t` elements. Most of the
    /// magic happens here.
    ///
    /// The general idea is that we can fill a buffer (`dest`) of `rmt_item32_t` items of
    /// length `wanted_num` with the items that we get from the iterator. Then we can tell
    /// the peripheral driver how many items we filled in by setting `item_num`. The
    /// driver will call this function over-and-over until `translated_size` is equal to
    /// `src_size` so when the iterator returns [`None`] we set `translated_size` to
    /// `src_size` to signal that there are no more items to translate.
    ///
    /// The compiler will generate this function for every different call to
    /// [`Self::start_iter_blocking`] and [`Self::start_iter`] with different iterator
    /// types because of the type parameter. This is done to avoid the double indirection
    /// that we'd have to do when using a trait object since references to trait objects
    /// are fat-pointers (2 `usize` wide) and we only get a narrow pointer (`src`).
    /// Using a trait object has the addional overhead that every call to `Iterator::next`
    /// would also be indirect (through the `vtable`) and couldn't be inlined.
    unsafe extern "C" fn translate_iterator<T, const DEALLOC_ITER: bool>(
        src: *const core::ffi::c_void,
        mut dest: *mut rmt_item32_t,
        src_size: usize,
        wanted_num: usize,
        translated_size: *mut usize,
        item_num: *mut usize,
    ) where
        T: Iterator<Item = Symbol>,
    {
        // An `UnsafeCell` is needed here because we're casting a `*const` to a `*mut`.
        // Safe because this is the only existing reference.
        let iter = &mut *UnsafeCell::raw_get(src as *const UnsafeCell<T>);

        let mut i = 0;
        let finished = loop {
            if i >= wanted_num {
                break 0;
            }

            if let Some(item) = iter.next() {
                *dest = item.0;
                dest = dest.add(1);
                i += 1;
            } else {
                // Only deallocate the iter if the const generics argument is `true`
                // otherwise we could be deallocating stack memory.
                #[cfg(feature = "alloc")]
                if DEALLOC_ITER {
                    drop(alloc::boxed::Box::from_raw(iter));
                }
                break src_size;
            }
        };

        *item_num = i;
        *translated_size = finished;
    }

    /// Stop transmitting.
    pub fn stop(&mut self) -> Result<(), EspError> {
        esp!(unsafe { rmt_tx_stop(self.channel()) })
    }

    pub fn set_looping(&mut self, looping: config::Loop) -> Result<(), EspError> {
        esp!(unsafe { rmt_set_tx_loop_mode(self.channel(), looping != config::Loop::None) })?;

        #[cfg(not(any(esp32, esp32c2)))]
        esp!(unsafe {
            rmt_set_tx_loop_count(
                self.channel(),
                match looping {
                    config::Loop::Count(count) if count > 0 && count < 1024 => count,
                    _ => 0,
                },
            )
        })?;

        Ok(())
    }

    pub fn channel(&self) -> rmt_channel_t {
        self.channel as _
    }
}

impl<'d> Drop for TxRmtDriver<'d> {
    /// Stop transmitting and release the driver.
    fn drop(&mut self) {
        self.stop().unwrap();
        esp!(unsafe { rmt_driver_uninstall(self.channel()) }).unwrap();
    }
}

unsafe impl<'d> Send for TxRmtDriver<'d> {}

/// Symbols
///
/// Represents a single pulse cycle symbol comprised of mark (high)
/// and space (low) periods in either order or a fixed level if both
/// halves have the same [`PinState`]. This is just a newtype over the
/// IDF's `rmt_item32_t` or `rmt_symbol_word_t` type.
#[derive(Clone, Copy)]
pub struct Symbol(rmt_item32_t);

impl Symbol {
    /// Create a symbol from a pair of half-cycles.
    pub fn new(level0: Pulse, level1: Pulse) -> Self {
        let item = rmt_item32_t {
            __bindgen_anon_1: rmt_item32_t__bindgen_ty_1 { val: 0 },
        };
        let mut this = Self(item);
        this.update(level0, level1);
        this
    }

    /// Mutate this symbol to store a different pair of half-cycles.
    pub fn update(&mut self, level0: Pulse, level1: Pulse) {
        // SAFETY: We're overriding all 32 bits, so it doesn't matter what was here before.
        let inner = unsafe { &mut self.0.__bindgen_anon_1.__bindgen_anon_1 };
        inner.set_level0(level0.pin_state as u32);
        inner.set_duration0(level0.ticks.0 as u32);
        inner.set_level1(level1.pin_state as u32);
        inner.set_duration1(level1.ticks.0 as u32);
    }
}

/// Signal storage for [`Transmit`] in a format ready for the RMT driver.
pub trait Signal {
    fn as_slice(&self) -> &[rmt_item32_t];
}

impl Signal for Symbol {
    fn as_slice(&self) -> &[rmt_item32_t] {
        slice::from_ref(&self.0)
    }
}

impl Signal for [rmt_item32_t] {
    fn as_slice(&self) -> &[rmt_item32_t] {
        self
    }
}

/// Stack based signal storage for an RMT signal.
///
/// Use this if you know the length of the pulses ahead of time and prefer to use the stack.
///
/// Internally RMT uses pairs of pulses as part of its data structure. This implementation
/// you need to [`set`][FixedLengthSignal::set()] a two [`Pulse`]s for each index.
///
/// ```rust
/// # use esp_idf_hal::rmt::FixedLengthSignal;
/// let p1 = Pulse::new(PinState::High, PulseTicks::new(10));
/// let p2 = Pulse::new(PinState::Low, PulseTicks::new(11));
/// let p3 = Pulse::new(PinState::High, PulseTicks::new(12));
/// let p4 = Pulse::new(PinState::Low, PulseTicks::new(13));
///
/// let mut s = FixedLengthSignal::new();
/// s.set(0, &(p1, p2));
/// s.set(1, &(p3, p4));
/// ```
#[derive(Clone)]
pub struct FixedLengthSignal<const N: usize>([rmt_item32_t; N]);

#[cfg(all(esp_idf_version_major = "4", esp32))]
#[allow(non_camel_case_types)]
type rmt_item32_t__bindgen_ty_1 = rmt_item32_s__bindgen_ty_1;
#[cfg(all(esp_idf_version_major = "4", esp32))]
#[allow(non_camel_case_types)]
#[allow(dead_code)]
type rmt_item32_t__bindgen_ty_1__bindgen_ty_1 = rmt_item32_s__bindgen_ty_1__bindgen_ty_1;

impl<const N: usize> FixedLengthSignal<N> {
    /// Creates a new array of size `<N>`, where the number of pulses is `N * 2`.
    pub fn new() -> Self {
        Self(
            [rmt_item32_t {
                __bindgen_anon_1: rmt_item32_t__bindgen_ty_1 {
                    // Quick way to set all 32 bits to zero, instead of using `__bindgen_anon_1`.
                    val: 0,
                },
            }; N],
        )
    }

    /// Set a pair of [`Pulse`]s at a position in the array.
    pub fn set(&mut self, index: usize, pair: &(Pulse, Pulse)) -> Result<(), EspError> {
        let item = self
            .0
            .get_mut(index)
            .ok_or_else(|| EspError::from(ERR_ERANGE).unwrap())?;

        let mut symbol = Symbol(*item);
        symbol.update(pair.0, pair.1);
        *item = symbol.0;
        Ok(())
    }
}

impl<const N: usize> Signal for FixedLengthSignal<N> {
    fn as_slice(&self) -> &[rmt_item32_t] {
        &self.0
    }
}

impl<const N: usize> Default for FixedLengthSignal<N> {
    fn default() -> Self {
        Self::new()
    }
}

// TODO: impl<const N: usize> From<&[Pulse; N]> for StackSignal<{ (N + 1) / 2 }> {
// Implementing this caused the compiler to crash!

/// `Vec` heap based storage for an RMT signal.
///
/// Use this for when you don't know the final size of your signal data.
///
/// # Example
/// ```rust
/// let mut signal = VariableLengthSignal::new();
/// signal.push(Pulse::new(PinState::High, PulseTicks::new(10)));
/// signal.push(Pulse::new(PinState::Low, PulseTicks::new(9)));
/// ```

#[cfg(feature = "alloc")]
#[derive(Clone, Default)]
pub struct VariableLengthSignal {
    items: alloc::vec::Vec<rmt_item32_t>,

    // Items contain two pulses. Track if we're adding a new pulse to the first one (true) or if
    // we're changing the second one (false).
    next_item_is_new: bool,
}

#[cfg(feature = "alloc")]
impl VariableLengthSignal {
    pub const fn new() -> Self {
        Self {
            items: alloc::vec::Vec::new(),
            next_item_is_new: true,
        }
    }

    /// Create a new [`VariableLengthSignal`] with a a given capacity. This is
    /// more efficent than not specifying the capacity with `new( )` as the
    /// memory manager only needs to allocate the underlying array once.
    ///
    /// - `capacity` is the number of [`Pulse`]s which can be pushes before reallocating
    pub fn with_capacity(capacity: usize) -> Self {
        // half the size, rounding up, because each entry in the [`Vec`] holds upto 2 pulses each
        let vec_size = (capacity + 1) / 2;
        Self {
            items: alloc::vec::Vec::with_capacity(vec_size),
            next_item_is_new: true,
        }
    }

    /// Add [`Pulse`]s to the end of the signal.
    pub fn push<'p, I>(&mut self, pulses: I) -> Result<(), EspError>
    where
        I: IntoIterator<Item = &'p Pulse>,
    {
        for pulse in pulses {
            if self.next_item_is_new {
                let mut inner_item = rmt_item32_t__bindgen_ty_1__bindgen_ty_1::default();

                inner_item.set_level0(pulse.pin_state as u32);
                inner_item.set_duration0(pulse.ticks.0 as u32);

                let item = rmt_item32_t {
                    __bindgen_anon_1: rmt_item32_t__bindgen_ty_1 {
                        __bindgen_anon_1: inner_item,
                    },
                };

                self.items.push(item);
            } else {
                // There should be at least one item in the vec.
                let len = self.items.len();
                let item = self.items.get_mut(len - 1).unwrap();

                // SAFETY: This item was previously populated with the same union field.
                let inner = unsafe { &mut item.__bindgen_anon_1.__bindgen_anon_1 };

                inner.set_level1(pulse.pin_state as u32);
                inner.set_duration1(pulse.ticks.0 as u32);
            }

            self.next_item_is_new = !self.next_item_is_new;
        }

        Ok(())
    }

    /// Delete all pulses.
    pub fn clear(&mut self) {
        self.next_item_is_new = true;
        self.items.clear();
    }
}

#[cfg(feature = "alloc")]
impl Signal for VariableLengthSignal {
    fn as_slice(&self) -> &[rmt_item32_t] {
        &self.items
    }
}

#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum Receive {
    Read(usize),
    Overflow(usize),
    Timeout,
}

/// The RMT receiver.
///
/// Use [`RxRmtDriver::start()`] to receive pulses.
///
/// See the [rmt module][crate::rmt] for more information.
pub struct RxRmtDriver<'d> {
    channel: u8,
    next_ringbuf_item: Option<(*mut rmt_item32_t, usize)>,
    _p: PhantomData<&'d mut ()>,
}

impl<'d> RxRmtDriver<'d> {
    /// Initialise the rmt module with the specified pin, channel and configuration.
    ///
    /// To uninstall the driver just drop it.
    ///
    /// Internally this calls `rmt_config()` and `rmt_driver_install()`.

    pub fn new<C: RmtChannel>(
        _channel: impl Peripheral<P = C> + 'd,
        pin: impl Peripheral<P = impl InputPin> + 'd,
        config: &ReceiveConfig,
        ring_buf_size: usize,
    ) -> Result<Self, EspError> {
        crate::into_ref!(pin);

        #[cfg(not(any(esp32, esp32c2)))]
        let carrier_en = config.carrier.is_some();

        #[cfg(not(any(esp32, esp32c2)))]
        let carrier = config.carrier.unwrap_or_default();

        let sys_config = rmt_config_t {
            rmt_mode: rmt_mode_t_RMT_MODE_RX,
            channel: C::channel(),
            gpio_num: pin.pin(),
            clk_div: config.clock_divider,
            mem_block_num: config.mem_block_num,
            flags: 0,
            __bindgen_anon_1: rmt_config_t__bindgen_ty_1 {
                rx_config: rmt_rx_config_t {
                    idle_threshold: config.idle_threshold,
                    filter_ticks_thresh: config.filter_ticks_thresh,
                    filter_en: config.filter_en,
                    #[cfg(not(any(esp32, esp32c2)))]
                    rm_carrier: carrier_en,
                    #[cfg(not(any(esp32, esp32c2)))]
                    carrier_freq_hz: carrier.frequency.into(),
                    #[cfg(not(any(esp32, esp32c2)))]
                    carrier_level: carrier.carrier_level as u32,
                    #[cfg(not(any(esp32, esp32c2)))]
                    carrier_duty_percent: carrier.duty_percent.0,
                },
            },
        };

        unsafe {
            esp!(rmt_config(&sys_config))?;
            esp!(rmt_driver_install(
                C::channel(),
                ring_buf_size * 4,
                InterruptType::to_native(config.intr_flags) as _
            ))?;
        }

        Ok(Self {
            channel: C::channel() as _,
            next_ringbuf_item: None,
            _p: PhantomData,
        })
    }

    pub fn channel(&self) -> rmt_channel_t {
        self.channel as _
    }

    /// Start receiving
    pub fn start(&self) -> Result<(), EspError> {
        esp!(unsafe { rmt_rx_start(self.channel(), true) })
    }

    /// Stop receiving
    pub fn stop(&self) -> Result<(), EspError> {
        esp!(unsafe { rmt_rx_stop(self.channel()) })
    }

    pub fn receive(
        &mut self,
        buf: &mut [(Pulse, Pulse)],
        ticks_to_wait: TickType_t,
    ) -> Result<Receive, EspError> {
        if let Some(items) = self.fetch_ringbuf_next_item(ticks_to_wait)? {
            if items.len() <= buf.len() {
                for (index, item) in items.iter().enumerate() {
                    let item = unsafe { item.__bindgen_anon_1.__bindgen_anon_1 };

                    buf[index] = (
                        Pulse::new(
                            item.level0().into(),
                            PulseTicks::new(item.duration0().try_into().unwrap()).unwrap(),
                        ),
                        Pulse::new(
                            item.level1().into(),
                            PulseTicks::new(item.duration1().try_into().unwrap()).unwrap(),
                        ),
                    );
                }

                let len = items.len();

                self.return_ringbuf_item()?;

                Ok(Receive::Read(len))
            } else {
                Ok(Receive::Overflow(items.len()))
            }
        } else {
            Ok(Receive::Timeout)
        }
    }

    fn fetch_ringbuf_next_item(
        &mut self,
        ticks_to_wait: TickType_t,
    ) -> Result<Option<&[rmt_item32_t]>, EspError> {
        if let Some((rmt_items, length)) = self.next_ringbuf_item {
            Ok(Some(unsafe {
                core::slice::from_raw_parts(rmt_items, length)
            }))
        } else {
            let mut ringbuf_handle = ptr::null_mut();
            esp!(unsafe { rmt_get_ringbuf_handle(self.channel(), &mut ringbuf_handle) })?;

            let mut length = 0;
            let rmt_items: *mut rmt_item32_t = unsafe {
                xRingbufferReceive(ringbuf_handle.cast(), &mut length, ticks_to_wait).cast()
            };

            if rmt_items.is_null() {
                Ok(None)
            } else {
                let length = length / 4;
                self.next_ringbuf_item = Some((rmt_items, length));

                Ok(Some(unsafe {
                    core::slice::from_raw_parts(rmt_items, length)
                }))
            }
        }
    }

    fn return_ringbuf_item(&mut self) -> Result<(), EspError> {
        let mut ringbuf_handle = ptr::null_mut();
        esp!(unsafe { rmt_get_ringbuf_handle(self.channel(), &mut ringbuf_handle) })?;

        if let Some((rmt_items, _)) = self.next_ringbuf_item.take() {
            unsafe {
                vRingbufferReturnItem(ringbuf_handle, rmt_items.cast());
            }
        } else {
            unreachable!();
        }

        Ok(())
    }
}

impl<'d> Drop for RxRmtDriver<'d> {
    /// Stop receiving and release the driver.
    fn drop(&mut self) {
        self.stop().unwrap();
        esp!(unsafe { rmt_driver_uninstall(self.channel()) }).unwrap();
    }
}

unsafe impl<'d> Send for RxRmtDriver<'d> {}

mod chip {
    use esp_idf_sys::*;

    /// RMT peripheral channel.
    pub trait RmtChannel {
        fn channel() -> rmt_channel_t;
    }

    macro_rules! impl_channel {
        ($instance:ident: $channel:expr) => {
            crate::impl_peripheral!($instance);

            impl RmtChannel for $instance {
                fn channel() -> rmt_channel_t {
                    $channel
                }
            }
        };
    }

    // SOC_RMT_CHANNELS_PER_GROUP defines how many channels there are.

    impl_channel!(CHANNEL0: rmt_channel_t_RMT_CHANNEL_0);
    impl_channel!(CHANNEL1: rmt_channel_t_RMT_CHANNEL_1);
    impl_channel!(CHANNEL2: rmt_channel_t_RMT_CHANNEL_2);
    impl_channel!(CHANNEL3: rmt_channel_t_RMT_CHANNEL_3);
    #[cfg(any(esp32, esp32s3))]
    impl_channel!(CHANNEL4: rmt_channel_t_RMT_CHANNEL_4);
    #[cfg(any(esp32, esp32s3))]
    impl_channel!(CHANNEL5: rmt_channel_t_RMT_CHANNEL_5);
    #[cfg(any(esp32, esp32s3))]
    impl_channel!(CHANNEL6: rmt_channel_t_RMT_CHANNEL_6);
    #[cfg(any(esp32, esp32s3))]
    impl_channel!(CHANNEL7: rmt_channel_t_RMT_CHANNEL_7);

    pub struct RMT {
        pub channel0: CHANNEL0,
        pub channel1: CHANNEL1,
        pub channel2: CHANNEL2,
        pub channel3: CHANNEL3,
        #[cfg(any(esp32, esp32s3))]
        pub channel4: CHANNEL4,
        #[cfg(any(esp32, esp32s3))]
        pub channel5: CHANNEL5,
        #[cfg(any(esp32, esp32s3))]
        pub channel6: CHANNEL6,
        #[cfg(any(esp32, esp32s3))]
        pub channel7: CHANNEL7,
    }

    impl RMT {
        /// Creates a new instance of the RMT peripheral. Typically one wants
        /// to use the instance [`rmt`](crate::peripherals::Peripherals::rmt) from
        /// the device peripherals obtained via
        /// [`peripherals::Peripherals::take()`](crate::peripherals::Peripherals::take()).
        ///
        /// # Safety
        ///
        /// It is safe to instantiate the RMT peripheral exactly one time.
        /// Care has to be taken that this has not already been done elsewhere.
        pub unsafe fn new() -> Self {
            Self {
                channel0: CHANNEL0::new(),
                channel1: CHANNEL1::new(),
                channel2: CHANNEL2::new(),
                channel3: CHANNEL3::new(),
                #[cfg(any(esp32, esp32s3))]
                channel4: CHANNEL4::new(),
                #[cfg(any(esp32, esp32s3))]
                channel5: CHANNEL5::new(),
                #[cfg(any(esp32, esp32s3))]
                channel6: CHANNEL6::new(),
                #[cfg(any(esp32, esp32s3))]
                channel7: CHANNEL7::new(),
            }
        }
    }
}