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
//! # Two-wire Automotive Interface (TWAI)
//!
//! ## Overview
//! The TWAI peripheral driver provides functions and structs specifically
//! designed for the Two-Wire Automotive Interface (TWAI) on ESP chips. TWAI is
//! a communication protocol commonly used in automotive applications for
//! sending and receiving messages between electronic control units (ECUs) in a
//! vehicle.
//!
//! The driver enables you to configure and utilize the TWAI module on ESP
//! chips. It offers functions for initializing the TWAI peripheral, setting up
//! the timing parameters, configuring acceptance filters, handling interrupts,
//! and transmitting/receiving messages on the TWAI bus.
//!
//! This driver manages the ISO 11898-1 (CAN Specification 2.0) compatible TWAI
//! controllers. It supports Standard Frame Format (11-bit) and Extended Frame
//! Format (29-bit) frame identifiers.
//!
//! ## Example
//! ```no_run
//! // Use GPIO pins 2 and 3 to connect to the respective pins on the CAN
//! // transceiver.
//! let can_tx_pin = io.pins.gpio2;
//! let can_rx_pin = io.pins.gpio3;
//!
//! // The speed of the CAN bus.
//! const CAN_BAUDRATE: twai::BaudRate = twai::BaudRate::B1000K;
//!
//! // Begin configuring the TWAI peripheral. The peripheral is in a reset like
//! // state that prevents transmission but allows configuration.
//! let mut can_config = twai::TwaiConfiguration::new(
//!     peripherals.TWAI0,
//!     can_tx_pin,
//!     can_rx_pin,
//!     &clocks,
//!     CAN_BAUDRATE,
//! );
//!
//! // Partially filter the incoming messages to reduce overhead of receiving undesired messages
//! const FILTER: twai::filter::SingleStandardFilter =
//!     twai::filter::SingleStandardFilter::new(b"xxxxxxxxxx0", b"x", [b"xxxxxxxx", b"xxxxxxxx"]);
//! can_config.set_filter(FILTER);
//!
//! // Start the peripheral. This locks the configuration settings of the peripheral
//! // and puts it into operation mode, allowing packets to be sent and
//! // received.
//! let mut can = can_config.start();
//!
//! loop {
//!     // Wait for a frame to be received.
//!     let frame = block!(can.receive()).unwrap();
//!
//!     println!("Received a frame:");
//!
//!     // Print different messages based on the frame id type.
//!     match frame.id() {
//!         Id::Standard(id) => {
//!             println!("\tStandard Id: {:?}", id);
//!         }
//!         Id::Extended(id) => {
//!             println!("\tExtended Id: {:?}", id);
//!         }
//!     }
//!
//!     // Print out the frame data or the requested data length code for a remote
//!     // transmission request frame.
//!     if frame.is_data_frame() {
//!         println!("\tData: {:?}", frame.data());
//!     } else {
//!         println!("\tRemote Frame. Data Length Code: {}", frame.dlc());
//!     }
//!
//!     // Transmit the frame back.
//!     let _result = block!(can.transmit(&frame)).unwrap();
//! }
//! ```

#[cfg(feature = "eh1")]
use embedded_can::{nb::Can, Error, ErrorKind, ExtendedId, Frame, Id, StandardId};
#[cfg(not(feature = "eh1"))]
use embedded_hal::can::{Can, Error, ErrorKind, ExtendedId, Frame, Id, StandardId};
use fugit::HertzU32;

use self::filter::{Filter, FilterType};
use crate::{
    clock::Clocks,
    gpio::{InputPin, InputSignal, OutputPin, OutputSignal},
    peripheral::{Peripheral, PeripheralRef},
    peripherals::twai0::RegisterBlock,
    system::{self, PeripheralClockControl},
};

pub mod filter;

/// Structure backing the embedded_hal::can::Frame/embedded_can::Frame trait.
#[derive(Clone, Copy, Debug)]
pub struct EspTwaiFrame {
    id: Id,
    dlc: usize,
    data: [u8; 8],
    is_remote: bool,
}

impl EspTwaiFrame {
    /// Make a new frame from an id, pointer to the TWAI_DATA_x_REG registers,
    /// and the length of the data payload (dlc).
    ///
    /// # Safety
    /// This is unsafe because it directly accesses peripheral registers.
    unsafe fn new_from_data_registers(
        id: impl Into<Id>,
        registers: *const u32,
        dlc: usize,
    ) -> Self {
        let mut data: [u8; 8] = [0; 8];

        // Copy the data from the memory mapped peripheral into actual memory.
        copy_from_data_register(&mut data[..dlc], registers);

        Self {
            id: id.into(),
            data,
            dlc,
            is_remote: false,
        }
    }
}

impl Frame for EspTwaiFrame {
    fn new(id: impl Into<Id>, data: &[u8]) -> Option<Self> {
        // CAN2.0 frames cannot contain more than 8 bytes of data.
        if data.len() > 8 {
            return None;
        }

        let mut d: [u8; 8] = [0; 8];
        let (left, _unused) = d.split_at_mut(data.len());
        left.clone_from_slice(data);

        Some(EspTwaiFrame {
            id: id.into(),
            data: d,
            dlc: data.len(),
            is_remote: false,
        })
    }

    fn new_remote(id: impl Into<Id>, dlc: usize) -> Option<Self> {
        // CAN2.0 frames cannot have more than 8 bytes.
        if dlc > 8 {
            return None;
        }
        Some(EspTwaiFrame {
            id: id.into(),
            data: [0; 8],
            dlc,
            is_remote: true,
        })
    }

    fn is_extended(&self) -> bool {
        match self.id {
            Id::Standard(_) => false,
            Id::Extended(_) => true,
        }
    }

    fn is_remote_frame(&self) -> bool {
        self.is_remote
    }

    fn id(&self) -> Id {
        self.id
    }

    fn dlc(&self) -> usize {
        self.dlc
    }

    fn data(&self) -> &[u8] {
        // Remote frames do not contain data, yet have a value for the dlc so return
        // an empty slice for remote frames.
        match self.is_remote_frame() {
            true => &[],
            false => &self.data[0..self.dlc],
        }
    }
}

/// The underlying timings for the TWAI peripheral.
pub struct TimingConfig {
    pub baud_rate_prescaler: u16,
    pub sync_jump_width: u8,
    pub tseg_1: u8,
    pub tseg_2: u8,
    pub triple_sample: bool,
}

/// A selection of pre-determined baudrates for the TWAI driver.
/// Currently these timings are sourced from the ESP IDF C driver which assumes
/// an APB clock of 80MHz.
pub enum BaudRate {
    B125K,
    B250K,
    B500K,
    B1000K,
    Custom(TimingConfig),
}

impl BaudRate {
    /// Convert the BaudRate into the timings that the peripheral needs.
    // These timings are copied from the ESP IDF C driver.
    // #define TWAI_TIMING_CONFIG_25KBITS()    {.brp = 128, .tseg_1 = 16, .tseg_2 =
    // 8, .sjw = 3, .triple_sampling = false}
    // #define TWAI_TIMING_CONFIG_50KBITS()    {.brp = 80, .tseg_1 = 15, .tseg_2 =
    // 4, .sjw = 3, .triple_sampling = false}
    // #define TWAI_TIMING_CONFIG_100KBITS()   {.brp = 40, .tseg_1 = 15, .tseg_2 =
    // 4, .sjw = 3, .triple_sampling = false}
    // #define TWAI_TIMING_CONFIG_125KBITS()   {.brp = 32, .tseg_1 = 15, .tseg_2 =
    // 4, .sjw = 3, .triple_sampling = false}
    // #define TWAI_TIMING_CONFIG_250KBITS()   {.brp = 16, .tseg_1 = 15, .tseg_2 =
    // 4, .sjw = 3, .triple_sampling = false}
    // #define TWAI_TIMING_CONFIG_500KBITS()   {.brp = 8, .tseg_1 = 15, .tseg_2 = 4,
    // .sjw = 3, .triple_sampling = false} #define TWAI_TIMING_CONFIG_800KBITS()
    // {.brp = 4, .tseg_1 = 16, .tseg_2 = 8, .sjw = 3, .triple_sampling = false}
    // #define TWAI_TIMING_CONFIG_1MBITS()     {.brp = 4, .tseg_1 = 15, .tseg_2 = 4,
    // .sjw = 3, .triple_sampling = false}
    const fn timing(self) -> TimingConfig {
        match self {
            Self::B125K => TimingConfig {
                baud_rate_prescaler: 32,
                sync_jump_width: 3,
                tseg_1: 15,
                tseg_2: 4,
                triple_sample: false,
            },
            Self::B250K => TimingConfig {
                baud_rate_prescaler: 16,
                sync_jump_width: 3,
                tseg_1: 15,
                tseg_2: 4,
                triple_sample: false,
            },
            Self::B500K => TimingConfig {
                baud_rate_prescaler: 8,
                sync_jump_width: 3,
                tseg_1: 15,
                tseg_2: 4,
                triple_sample: false,
            },
            Self::B1000K => TimingConfig {
                baud_rate_prescaler: 4,
                sync_jump_width: 3,
                tseg_1: 15,
                tseg_2: 4,
                triple_sample: false,
            },
            Self::Custom(timing_config) => timing_config,
        }
    }
}

/// An inactive TWAI peripheral in the "Reset"/configuration state.
pub struct TwaiConfiguration<'d, T> {
    peripheral: PeripheralRef<'d, T>,
}

impl<'d, T> TwaiConfiguration<'d, T>
where
    T: Instance,
{
    pub fn new<TX: OutputPin, RX: InputPin>(
        peripheral: impl Peripheral<P = T> + 'd,
        tx_pin: impl Peripheral<P = TX> + 'd,
        rx_pin: impl Peripheral<P = RX> + 'd,
        clocks: &Clocks,
        baud_rate: BaudRate,
    ) -> Self {
        // Enable the peripheral clock for the TWAI peripheral.
        PeripheralClockControl::enable(T::SYSTEM_PERIPHERAL);

        // Set up the GPIO pins.
        crate::into_ref!(tx_pin, rx_pin);
        tx_pin.connect_peripheral_to_output(T::OUTPUT_SIGNAL);
        rx_pin.connect_input_to_peripheral(T::INPUT_SIGNAL);

        crate::into_ref!(peripheral);
        let mut cfg = TwaiConfiguration { peripheral };

        cfg.set_baud_rate(baud_rate, clocks);

        cfg
    }

    /// Set the bitrate of the bus.
    ///
    /// Note: The timings currently assume a APB_CLK of 80MHz.
    fn set_baud_rate(&mut self, baud_rate: BaudRate, clocks: &Clocks) {
        // TWAI is clocked from the APB_CLK according to Table 6-4 [ESP32C3 Reference Manual](https://www.espressif.com/sites/default/files/documentation/esp32-c3_technical_reference_manual_en.pdf)
        // Included timings are all for 80MHz so assert that we are running at 80MHz.
        assert!(clocks.apb_clock == HertzU32::MHz(80));

        // Unpack the baud rate timings and convert them to the values needed for the
        // register. Many of the registers have a minimum value of 1 which is
        // represented by having zero bits set, therefore many values need to
        // have 1 subtracted from them before being stored into the register.
        let timing = baud_rate.timing();

        let prescale = (timing.baud_rate_prescaler / 2) - 1;
        let sjw = timing.sync_jump_width - 1;
        let tseg_1 = timing.tseg_1 - 1;
        let tseg_2 = timing.tseg_2 - 1;
        let triple_sample = timing.triple_sample;

        #[cfg(esp32)]
        let prescale = prescale as u8;

        // Set up the prescaler and sync jump width.
        self.peripheral
            .register_block()
            .bus_timing_0()
            .modify(|_, w| {
                w.baud_presc()
                    .variant(prescale)
                    .sync_jump_width()
                    .variant(sjw)
            });

        // Set up the time segment 1, time segment 2, and triple sample.
        self.peripheral
            .register_block()
            .bus_timing_1()
            .modify(|_, w| {
                w.time_seg1()
                    .variant(tseg_1)
                    .time_seg2()
                    .variant(tseg_2)
                    .time_samp()
                    .bit(triple_sample)
            });
    }

    /// Set up the acceptance filter on the device.
    ///
    /// NOTE: On a bus with mixed 11-bit and 29-bit packet id's, you may
    /// experience an 11-bit filter match against a 29-bit frame and vice
    /// versa. Your application should check the id again once a frame has
    /// been received to make sure it is the expected value.
    ///
    /// [ESP32C3 Reference Manual](https://www.espressif.com/sites/default/files/documentation/esp32-c3_technical_reference_manual_en.pdf#subsubsection.29.4.6)
    pub fn set_filter(&mut self, filter: impl Filter) {
        // Set or clear the rx filter mode bit depending on the filter type.
        let filter_mode_bit = filter.filter_type() == FilterType::Single;
        self.peripheral
            .register_block()
            .mode()
            .modify(|_, w| w.rx_filter_mode().bit(filter_mode_bit));

        // Convert the filter into values for the registers and store them to the
        // registers.
        let registers = filter.to_registers();

        // Copy the filter to the peripheral.
        unsafe {
            copy_to_data_register(
                self.peripheral.register_block().data_0().as_ptr(),
                &registers,
            );
        }
    }

    /// Set the error warning threshold.
    ///
    /// In the case when any of an error counter value exceeds the threshold, or
    /// all the error counter values are below the threshold, an error
    /// warning interrupt will be triggered (given the enable signal is
    /// valid).
    pub fn set_error_warning_limit(&mut self, limit: u8) {
        self.peripheral
            .register_block()
            .err_warning_limit()
            .write(|w| w.err_warning_limit().variant(limit));
    }

    /// Put the peripheral into Operation Mode, allowing the transmission and
    /// reception of packets using the new object.
    pub fn start(self) -> Twai<'d, T> {
        // Put the peripheral into operation mode by clearing the reset mode bit.
        self.peripheral
            .register_block()
            .mode()
            .modify(|_, w| w.reset_mode().clear_bit());

        Twai {
            peripheral: self.peripheral,
        }
    }
}

/// An active TWAI peripheral in Normal Mode.
///
/// In this mode, the TWAI controller can transmit and receive messages
/// including error signals (such as error and overload frames).
pub struct Twai<'d, T> {
    peripheral: PeripheralRef<'d, T>,
}

impl<'d, T> Twai<'d, T>
where
    T: Instance,
{
    /// Stop the peripheral, putting it into reset mode and enabling
    /// reconfiguration.
    pub fn stop(self) -> TwaiConfiguration<'d, T> {
        // Put the peripheral into reset/configuration mode by setting the reset mode
        // bit.
        self.peripheral
            .register_block()
            .mode()
            .modify(|_, w| w.reset_mode().set_bit());

        TwaiConfiguration {
            peripheral: self.peripheral,
        }
    }

    pub fn receive_error_count(&self) -> u8 {
        self.peripheral
            .register_block()
            .rx_err_cnt()
            .read()
            .rx_err_cnt()
            .bits()
    }

    pub fn transmit_error_count(&self) -> u8 {
        self.peripheral
            .register_block()
            .tx_err_cnt()
            .read()
            .tx_err_cnt()
            .bits()
    }

    /// Check if the controller is in a bus off state.
    pub fn is_bus_off(&self) -> bool {
        self.peripheral
            .register_block()
            .status()
            .read()
            .bus_off_st()
            .bit_is_set()
    }

    /// Get the number of messages that the peripheral has available in the
    /// receive FIFO.
    ///
    /// Note that this may not be the number of valid messages in the receive
    /// FIFO due to fifo overflow/overrun.
    pub fn num_available_messages(&self) -> u8 {
        self.peripheral
            .register_block()
            .rx_message_cnt()
            .read()
            .rx_message_counter()
            .bits()
    }

    /// Clear the receive FIFO, discarding any valid, partial, or invalid
    /// packets.
    ///
    /// This is typically used to clear an overrun receive FIFO.
    ///
    /// TODO: Not sure if this needs to be guarded against Bus Off or other
    /// error states.
    pub fn clear_receive_fifo(&self) {
        while self.num_available_messages() > 0 {
            self.release_receive_fifo();
        }
    }

    /// Release the message in the buffer. This will decrement the received
    /// message counter and prepare the next message in the FIFO for
    /// reading.
    fn release_receive_fifo(&self) {
        self.peripheral
            .register_block()
            .cmd()
            .write(|w| w.release_buf().set_bit());
    }
}

#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum EspTwaiError {
    BusOff,
    EmbeddedHAL(ErrorKind),
}

impl Error for EspTwaiError {
    fn kind(&self) -> ErrorKind {
        match self {
            Self::BusOff => ErrorKind::Other,
            Self::EmbeddedHAL(kind) => *kind,
        }
    }
}

/// Copy data from multiple TWAI_DATA_x_REG registers, packing the source into
/// the destination.
///
/// # Safety
/// This function is marked unsafe because it reads arbitrarily from
/// memory-mapped registers. Specifically, this function is used with the
/// TWAI_DATA_x_REG registers which has different results based on the mode of
/// the peripheral.
#[inline(always)]
unsafe fn copy_from_data_register(dest: &mut [u8], src: *const u32) {
    for (i, dest) in dest.iter_mut().enumerate() {
        // Perform a volatile read to avoid compiler optimizations.
        *dest = src.add(i).read_volatile() as u8;
    }
}

/// Copy data to multiple TWAI_DATA_x_REG registers, unpacking the source into
/// the destination.
///
/// # Safety
/// This function is marked unsafe because it writes arbitrarily to
/// memory-mapped registers. Specifically, this function is used with the
/// TWAI_DATA_x_REG registers which has different results based on the mode of
/// the peripheral.
#[inline(always)]
unsafe fn copy_to_data_register(dest: *mut u32, src: &[u8]) {
    for (i, src) in src.iter().enumerate() {
        // Perform a volatile write to avoid compiler optimizations.
        dest.add(i).write_volatile(*src as u32);
    }
}

impl<T> Can for Twai<'_, T>
where
    T: Instance,
{
    type Frame = EspTwaiFrame;
    type Error = EspTwaiError;
    /// Transmit a frame.
    ///
    /// Because of how the TWAI registers are set up, we have to do some
    /// assembly of bytes. Note that these registers serve a filter
    /// configuration role when the device is in configuration mode so
    /// patching the svd files to improve this may be non-trivial.
    ///
    /// [ESP32C3 Reference Manual](https://www.espressif.com/sites/default/files/documentation/esp32-c3_technical_reference_manual_en.pdf#subsubsection.29.4.4.2)
    ///
    /// NOTE: TODO: This may not work if using the self reception/self test
    /// functionality. See notes 1 and 2 in the "Frame Identifier" section
    /// of the reference manual.
    fn transmit(&mut self, frame: &Self::Frame) -> nb::Result<Option<Self::Frame>, Self::Error> {
        let status = self.peripheral.register_block().status().read();

        // Check that the peripheral is not in a bus off state.
        if status.bus_off_st().bit_is_set() {
            return nb::Result::Err(nb::Error::Other(EspTwaiError::BusOff));
        }
        // Check that the peripheral is not already transmitting a packet.
        if !status.tx_buf_st().bit_is_set() {
            return nb::Result::Err(nb::Error::WouldBlock);
        }

        // Assemble the frame information into the data_0 byte.
        let frame_format: u8 = frame.is_extended() as u8;
        let rtr_bit: u8 = frame.is_remote_frame() as u8;
        let dlc_bits: u8 = frame.dlc() as u8 & 0b1111;

        let data_0: u8 = frame_format << 7 | rtr_bit << 6 | dlc_bits;

        self.peripheral
            .register_block()
            .data_0()
            .write(|w| w.tx_byte_0().variant(data_0));

        // Assemble the identifier information of the packet.
        match frame.id() {
            Id::Standard(id) => {
                let id = id.as_raw();

                self.peripheral
                    .register_block()
                    .data_1()
                    .write(|w| w.tx_byte_1().variant((id >> 3) as u8));

                self.peripheral
                    .register_block()
                    .data_2()
                    .write(|w| w.tx_byte_2().variant((id << 5) as u8));
            }
            Id::Extended(id) => {
                let id = id.as_raw();

                self.peripheral
                    .register_block()
                    .data_1()
                    .write(|w| w.tx_byte_1().variant((id >> 21) as u8));
                self.peripheral
                    .register_block()
                    .data_2()
                    .write(|w| w.tx_byte_2().variant((id >> 13) as u8));
                self.peripheral
                    .register_block()
                    .data_3()
                    .write(|w| w.tx_byte_3().variant((id >> 5) as u8));
                self.peripheral
                    .register_block()
                    .data_4()
                    .write(|w| w.tx_byte_4().variant((id << 3) as u8));
            }
        }

        // Store the data portion of the packet into the transmit buffer.
        if frame.is_data_frame() {
            match frame.id() {
                Id::Standard(_) => unsafe {
                    copy_to_data_register(
                        self.peripheral.register_block().data_3().as_ptr(),
                        frame.data(),
                    )
                },
                Id::Extended(_) => unsafe {
                    copy_to_data_register(
                        self.peripheral.register_block().data_5().as_ptr(),
                        frame.data(),
                    )
                },
            }
        } else {
            // Is RTR frame, so no data is included.
        }

        // Set the transmit request command, this will lock the transmit buffer until
        // the transmission is complete or aborted.
        self.peripheral
            .register_block()
            .cmd()
            .write(|w| w.tx_req().set_bit());

        // Success in readying packet for transmit. No packets can be replaced in the
        // transmit buffer so return None in accordance with the
        // embedded-can/embedded-hal trait.
        nb::Result::Ok(None)
    }

    /// Return a received frame if there are any available.
    fn receive(&mut self) -> nb::Result<Self::Frame, Self::Error> {
        let status = self.peripheral.register_block().status().read();

        // Check that the peripheral is not in a bus off state.
        if status.bus_off_st().bit_is_set() {
            return nb::Result::Err(nb::Error::Other(EspTwaiError::BusOff));
        }

        // Check that we actually have packets to receive.
        if !status.rx_buf_st().bit_is_set() {
            return nb::Result::Err(nb::Error::WouldBlock);
        }

        // Check if the packet in the receive buffer is valid or overrun.
        if status.miss_st().bit_is_set() {
            return nb::Result::Err(nb::Error::Other(EspTwaiError::EmbeddedHAL(
                ErrorKind::Overrun,
            )));
        }

        // Read the frame information and extract the frame id format and dlc.
        let data_0 = self
            .peripheral
            .register_block()
            .data_0()
            .read()
            .tx_byte_0()
            .bits();

        let is_standard_format = data_0 & 0b1 << 7 == 0;
        let is_data_frame = data_0 & 0b1 << 6 == 0;
        let dlc = (data_0 & 0b1111) as usize;

        // Read the payload from the packet and construct a frame.
        let frame = if is_standard_format {
            // Frame uses standard 11 bit id.
            let data_1 = self
                .peripheral
                .register_block()
                .data_1()
                .read()
                .tx_byte_1()
                .bits();

            let data_2 = self
                .peripheral
                .register_block()
                .data_2()
                .read()
                .tx_byte_2()
                .bits();

            let raw_id: u16 = ((data_1 as u16) << 3) | ((data_2 as u16) >> 5);

            let id = StandardId::new(raw_id).unwrap();

            if is_data_frame {
                // Create a new frame from the contents of the appropriate TWAI_DATA_x_REG
                // registers.
                unsafe {
                    EspTwaiFrame::new_from_data_registers(
                        id,
                        self.peripheral.register_block().data_3().as_ptr(),
                        dlc,
                    )
                }
            } else {
                EspTwaiFrame::new_remote(id, dlc).unwrap()
            }
        } else {
            // Frame uses extended 29 bit id.
            let data_1 = self
                .peripheral
                .register_block()
                .data_1()
                .read()
                .tx_byte_1()
                .bits();

            let data_2 = self
                .peripheral
                .register_block()
                .data_2()
                .read()
                .tx_byte_2()
                .bits();

            let data_3 = self
                .peripheral
                .register_block()
                .data_3()
                .read()
                .tx_byte_3()
                .bits();

            let data_4 = self
                .peripheral
                .register_block()
                .data_4()
                .read()
                .tx_byte_4()
                .bits();

            let raw_id: u32 = (data_1 as u32) << 21
                | (data_2 as u32) << 13
                | (data_3 as u32) << 5
                | (data_4 as u32) >> 3;

            let id = ExtendedId::new(raw_id).unwrap();

            if is_data_frame {
                unsafe {
                    EspTwaiFrame::new_from_data_registers(
                        id,
                        self.peripheral.register_block().data_5().as_ptr(),
                        dlc,
                    )
                }
            } else {
                EspTwaiFrame::new_remote(id, dlc).unwrap()
            }
        };

        // Release the packet we read from the FIFO, allowing the peripheral to prepare
        // the next packet.
        self.release_receive_fifo();

        nb::Result::Ok(frame)
    }
}

pub trait Instance {
    const SYSTEM_PERIPHERAL: system::Peripheral;

    const INPUT_SIGNAL: InputSignal;
    const OUTPUT_SIGNAL: OutputSignal;

    fn register_block(&self) -> &RegisterBlock;
}

#[cfg(any(esp32c3, esp32s3))]
impl Instance for crate::peripherals::TWAI0 {
    const SYSTEM_PERIPHERAL: system::Peripheral = system::Peripheral::Twai0;

    const INPUT_SIGNAL: InputSignal = InputSignal::TWAI_RX;
    const OUTPUT_SIGNAL: OutputSignal = OutputSignal::TWAI_TX;

    #[inline(always)]
    fn register_block(&self) -> &RegisterBlock {
        self
    }
}

#[cfg(esp32c6)]
impl Instance for crate::peripherals::TWAI0 {
    const SYSTEM_PERIPHERAL: system::Peripheral = system::Peripheral::Twai0;

    const INPUT_SIGNAL: InputSignal = InputSignal::TWAI0_RX;
    const OUTPUT_SIGNAL: OutputSignal = OutputSignal::TWAI0_TX;

    #[inline(always)]
    fn register_block(&self) -> &RegisterBlock {
        self
    }
}

#[cfg(esp32c6)]
impl Instance for crate::peripherals::TWAI1 {
    const SYSTEM_PERIPHERAL: system::Peripheral = system::Peripheral::Twai1;

    const INPUT_SIGNAL: InputSignal = InputSignal::TWAI1_RX;
    const OUTPUT_SIGNAL: OutputSignal = OutputSignal::TWAI1_TX;

    #[inline(always)]
    fn register_block(&self) -> &RegisterBlock {
        self
    }
}