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
// Copyright 2021 The gd32f1x0-hal authors.
//
// SPDX-License-Identifier: MIT OR Apache-2.0

//! # Serial Communication (USART)
//!
//! This module contains the functions to utilize the USART (Universal
//! synchronous asynchronous receiver transmitter).

use crate::dma::{
    self, CircBuffer, CircReadDma, Priority, ReadDma, Receive, RxDma, Transfer, TransferPayload,
    Transmit, TxDma, Width, WriteDma, R, W,
};
use crate::gpio::gpioa::{PA10, PA14, PA15, PA2, PA3, PA9};
use crate::gpio::gpiob::{PB6, PB7};
use crate::gpio::{Alternate, AF0, AF1};
use crate::pac::{self, usart0, usart0::ctl1::Stb, Usart0};
use crate::rcu::{sealed::RcuBus, Clocks, Enable, GetBusFreq, Reset};
use crate::time::{Bps, U32Ext};
use core::convert::Infallible;
use core::fmt;
use core::hint::spin_loop;
use core::marker::PhantomData;
use core::ops::Deref;
use core::sync::atomic::{self, Ordering};
use embedded_dma::{ReadBuffer, WriteBuffer};
use embedded_io::{ErrorKind, ErrorType, Read, ReadReady, Write, WriteReady};

/// Serial error
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
    /// Framing error
    Framing,
    /// Noise error
    Noise,
    /// RX buffer overrun
    Overrun,
    /// Parity check error
    Parity,
}

impl From<Infallible> for Error {
    fn from(e: Infallible) -> Self {
        match e {}
    }
}

impl embedded_io::Error for Error {
    fn kind(&self) -> ErrorKind {
        match self {
            Self::Framing | Self::Noise | Self::Parity => ErrorKind::InvalidData,
            Self::Overrun => ErrorKind::Other,
        }
    }
}

pub enum Parity {
    ParityNone,
    ParityEven,
    ParityOdd,
}

pub type StopBits = Stb;

pub struct Config {
    pub baudrate: Bps,
    pub parity: Parity,
    pub stopbits: StopBits,
}

impl Config {
    pub fn baudrate(mut self, baudrate: Bps) -> Self {
        self.baudrate = baudrate;
        self
    }

    pub fn parity_none(mut self) -> Self {
        self.parity = Parity::ParityNone;
        self
    }

    pub fn parity_even(mut self) -> Self {
        self.parity = Parity::ParityEven;
        self
    }

    pub fn parity_odd(mut self) -> Self {
        self.parity = Parity::ParityOdd;
        self
    }

    pub fn stopbits(mut self, stopbits: StopBits) -> Self {
        self.stopbits = stopbits;
        self
    }
}

impl Default for Config {
    fn default() -> Config {
        Config {
            baudrate: 115_200_u32.bps(),
            parity: Parity::ParityNone,
            stopbits: StopBits::Stop1,
        }
    }
}

/// Interrupt event
pub enum Event {
    /// New data has been received
    Rbne,
    /// New data can be sent
    Tbe,
    /// Idle line state detected
    Idle,
}

pub trait TxPin<USART> {}
pub trait RxPin<USART> {}

impl TxPin<Usart0> for PA9<Alternate<AF1>> {}
impl RxPin<Usart0> for PA10<Alternate<AF1>> {}
impl TxPin<Usart0> for PB6<Alternate<AF0>> {}
impl RxPin<Usart0> for PB7<Alternate<AF0>> {}

// Two USARTs
#[cfg(any(
    feature = "gd32f130x6",
    feature = "gd32f130x8",
    feature = "gd32f150x6",
    feature = "gd32f150x8",
    feature = "gd32f170x6",
    feature = "gd32f170x8",
    feature = "gd32f190x6",
    feature = "gd32f190x8",
))]
mod pins {
    use super::*;
    use crate::gpio::gpiob::PB0;
    use crate::gpio::{gpioa::PA8, AF4};
    use crate::pac::Usart1;

    impl TxPin<Usart1> for PA2<Alternate<AF1>> {}
    impl RxPin<Usart1> for PA3<Alternate<AF1>> {}
    impl TxPin<Usart1> for PA8<Alternate<AF4>> {}
    impl RxPin<Usart1> for PB0<Alternate<AF4>> {}
    impl TxPin<Usart1> for PA14<Alternate<AF1>> {}
    impl RxPin<Usart1> for PA15<Alternate<AF1>> {}
}

// Only one USART
#[cfg(any(
    feature = "gd32f130x4",
    feature = "gd32f150x4",
    feature = "gd32f170x4",
    feature = "gd32f190x4"
))]
mod pins {
    use super::*;

    impl TxPin<Usart0> for PA2<Alternate<AF1>> {}
    impl RxPin<Usart0> for PA3<Alternate<AF1>> {}
    impl TxPin<Usart0> for PA14<Alternate<AF1>> {}
    impl RxPin<Usart0> for PA15<Alternate<AF1>> {}
}

/// Serial abstraction
pub struct Serial<USART, TXPIN, RXPIN> {
    usart: USART,
    pins: (TXPIN, RXPIN),
}

/// Serial receiver
pub struct Rx<USART> {
    usart: *const usart0::RegisterBlock,
    _instance: PhantomData<USART>,
}

unsafe impl<USART> Send for Rx<USART> {}

/// Serial transmitter
pub struct Tx<USART> {
    usart: *const usart0::RegisterBlock,
    _instance: PhantomData<USART>,
}

unsafe impl<USART> Send for Tx<USART> {}

impl<
        USART: RcuBus + Enable + Reset + Deref<Target = usart0::RegisterBlock>,
        TXPIN: TxPin<USART>,
        RXPIN: RxPin<USART>,
    > Serial<USART, TXPIN, RXPIN>
where
    USART::Bus: GetBusFreq,
{
    /// Configures the USART and creates a new Serial instance.
    pub fn usart(
        usart: USART,
        pins: (TXPIN, RXPIN),
        config: Config,
        clocks: Clocks,
        bus: &mut USART::Bus,
    ) -> Self {
        usart.enable_configure(config, clocks, bus);

        // Enable transmitter, receiver and the USART as a whole.
        usart
            .ctl0()
            .modify(|_, w| w.ten().enabled().ren().enabled().uen().enabled());

        Self { usart, pins }
    }

    /// Separates the serial struct into separate channel objects for sending (Tx) and receiving (Rx).
    pub fn split(self) -> (Tx<USART>, Rx<USART>) {
        (
            Tx {
                usart: &*self.usart,
                _instance: PhantomData,
            },
            Rx {
                usart: &*self.usart,
                _instance: PhantomData,
            },
        )
    }
}

impl<
        USART: RcuBus + Enable + Reset + Deref<Target = usart0::RegisterBlock>,
        TXPIN: TxPin<USART>,
    > Serial<USART, TXPIN, ()>
where
    USART::Bus: GetBusFreq,
{
    /// Configures the USART and creates a new TX-only Serial instance.
    pub fn usart_tx(
        usart: USART,
        txpin: TXPIN,
        config: Config,
        clocks: Clocks,
        bus: &mut USART::Bus,
    ) -> Self {
        usart.enable_configure(config, clocks, bus);

        // Enable transmitter and the USART as a whole.
        usart
            .ctl0()
            .modify(|_, w| w.ten().enabled().uen().enabled());

        Self {
            usart,
            pins: (txpin, ()),
        }
    }

    /// Erase the pin.
    pub fn downgrade_tx(self) -> Tx<USART> {
        Tx {
            usart: &*self.usart,
            _instance: PhantomData,
        }
    }
}

impl<
        USART: RcuBus + Enable + Reset + Deref<Target = usart0::RegisterBlock>,
        RXPIN: RxPin<USART>,
    > Serial<USART, (), RXPIN>
where
    USART::Bus: GetBusFreq,
{
    /// Configures the USART and creates a new RX-only Serial instance.
    pub fn usart_rx(
        usart: USART,
        rxpin: RXPIN,
        config: Config,
        clocks: Clocks,
        bus: &mut USART::Bus,
    ) -> Self {
        usart.enable_configure(config, clocks, bus);

        // Enable receiver and the USART as a whole.
        usart
            .ctl0()
            .modify(|_, w| w.ren().enabled().uen().enabled());

        Self {
            usart,
            pins: ((), rxpin),
        }
    }

    /// Erase the pin.
    pub fn downgrade_rx(self) -> Rx<USART> {
        Rx {
            usart: &*self.usart,
            _instance: PhantomData,
        }
    }
}

impl<USART: Deref<Target = usart0::RegisterBlock>, TXPIN, RXPIN> Serial<USART, TXPIN, RXPIN> {
    pub fn release(self) -> (USART, (TXPIN, RXPIN)) {
        (self.usart, self.pins)
    }

    /// Enable an interrupt event.
    pub fn listen(&mut self, event: Event) {
        match event {
            Event::Rbne => self.usart.ctl0().modify(|_, w| w.rbneie().enabled()),
            Event::Tbe => self.usart.ctl0().modify(|_, w| w.tbeie().enabled()),
            Event::Idle => self.usart.ctl0().modify(|_, w| w.idleie().enabled()),
        }
    }

    /// Disable an interrupt event.
    pub fn unlisten(&mut self, event: Event) {
        match event {
            Event::Rbne => self.usart.ctl0().modify(|_, w| w.rbneie().disabled()),
            Event::Tbe => self.usart.ctl0().modify(|_, w| w.tbeie().disabled()),
            Event::Idle => self.usart.ctl0().modify(|_, w| w.idleie().disabled()),
        }
    }
}

impl<USART: Deref<Target = usart0::RegisterBlock>> Rx<USART> {
    /// Enable the RBNE interrupt.
    pub fn listen(&mut self) {
        unsafe { &*self.usart }
            .ctl0()
            .modify(|_, w| w.rbneie().enabled());
    }

    /// Disable the RBNE interrupt.
    pub fn unlisten(&mut self) {
        unsafe { &*self.usart }
            .ctl0()
            .modify(|_, w| w.rbneie().disabled());
    }
}

impl<USART: Deref<Target = usart0::RegisterBlock>> Tx<USART> {
    /// Enable the TBE interrupt.
    pub fn listen(&mut self) {
        unsafe { &*self.usart }
            .ctl0()
            .modify(|_, w| w.tbeie().enabled());
    }

    /// Disable the TBE interrupt.
    pub fn unlisten(&mut self) {
        unsafe { &*self.usart }
            .ctl0()
            .modify(|_, w| w.tbeie().disabled());
    }
}

// Implement writing traits if the USART has a TX pin assigned.
impl<USART: Deref<Target = usart0::RegisterBlock>, TXPIN: TxPin<USART>, RXPIN> Write
    for Serial<USART, TXPIN, RXPIN>
{
    fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
        self.usart.write_buffer(buf)
    }

    fn flush(&mut self) -> Result<(), Self::Error> {
        nb::block!(self.usart.flush())?;
        Ok(())
    }
}

impl<USART: Deref<Target = usart0::RegisterBlock>, TXPIN: TxPin<USART>, RXPIN> WriteReady
    for Serial<USART, TXPIN, RXPIN>
{
    fn write_ready(&mut self) -> Result<bool, Self::Error> {
        Ok(self.usart.write_ready())
    }
}

#[cfg(feature = "embedded-hal-02")]
impl<USART: Deref<Target = usart0::RegisterBlock>, TXPIN: TxPin<USART>, RXPIN>
    embedded_hal_02::serial::Write<u8> for Serial<USART, TXPIN, RXPIN>
{
    type Error = Infallible;

    fn flush(&mut self) -> nb::Result<(), Self::Error> {
        self.usart.flush()
    }

    fn write(&mut self, byte: u8) -> nb::Result<(), Self::Error> {
        self.usart.write(byte)
    }
}

impl<USART> ErrorType for Tx<USART> {
    type Error = Error;
}

impl<USART: Deref<Target = usart0::RegisterBlock>> Write for Tx<USART> {
    fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
        unsafe { &*self.usart }.write_buffer(buf)
    }

    fn flush(&mut self) -> Result<(), Self::Error> {
        nb::block!(unsafe { &*self.usart }.flush())?;
        Ok(())
    }
}

impl<USART: Deref<Target = usart0::RegisterBlock>> WriteReady for Tx<USART> {
    fn write_ready(&mut self) -> Result<bool, Self::Error> {
        Ok(unsafe { &*self.usart }.write_ready())
    }
}

#[cfg(feature = "embedded-hal-02")]
impl<USART: Deref<Target = usart0::RegisterBlock>> embedded_hal_02::serial::Write<u8>
    for Tx<USART>
{
    type Error = Infallible;

    fn flush(&mut self) -> nb::Result<(), Self::Error> {
        unsafe { &*self.usart }.flush()
    }

    fn write(&mut self, byte: u8) -> nb::Result<(), Self::Error> {
        unsafe { &*self.usart }.write(byte)
    }
}

impl<USART: Deref<Target = usart0::RegisterBlock>, TXPIN: TxPin<USART>, RXPIN> fmt::Write
    for Serial<USART, TXPIN, RXPIN>
{
    fn write_str(&mut self, s: &str) -> fmt::Result {
        self.write_all(s.as_bytes()).map_err(|_| core::fmt::Error)
    }
}

impl<USART: Deref<Target = usart0::RegisterBlock>> fmt::Write for Tx<USART> {
    fn write_str(&mut self, s: &str) -> fmt::Result {
        self.write_all(s.as_bytes()).map_err(|_| core::fmt::Error)
    }
}

impl<USART, TXPIN, RXPIN> ErrorType for Serial<USART, TXPIN, RXPIN> {
    type Error = Error;
}

// Implement reading traits if the USART has an RX pin assigned.
impl<USART: Deref<Target = usart0::RegisterBlock>, TXPIN, RXPIN: RxPin<USART>> Read
    for Serial<USART, TXPIN, RXPIN>
{
    fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
        self.usart.read_buffer(buf)
    }
}

impl<USART: Deref<Target = usart0::RegisterBlock>, TXPIN, RXPIN: RxPin<USART>> ReadReady
    for Serial<USART, TXPIN, RXPIN>
{
    fn read_ready(&mut self) -> Result<bool, Self::Error> {
        Ok(self.usart.read_ready())
    }
}

impl<USART> ErrorType for Rx<USART> {
    type Error = Error;
}

impl<USART: Deref<Target = usart0::RegisterBlock>> Read for Rx<USART> {
    fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
        unsafe { &*self.usart }.read_buffer(buf)
    }
}

impl<USART: Deref<Target = usart0::RegisterBlock>> ReadReady for Rx<USART> {
    fn read_ready(&mut self) -> Result<bool, Self::Error> {
        Ok(unsafe { &*self.usart }.read_ready())
    }
}

#[cfg(feature = "embedded-hal-02")]
impl<USART: Deref<Target = usart0::RegisterBlock>, TXPIN, RXPIN: RxPin<USART>>
    embedded_hal_02::serial::Read<u8> for Serial<USART, TXPIN, RXPIN>
{
    type Error = Error;

    fn read(&mut self) -> nb::Result<u8, Error> {
        self.usart.read()
    }
}

#[cfg(feature = "embedded-hal-02")]
impl<USART: Deref<Target = usart0::RegisterBlock>> embedded_hal_02::serial::Read<u8> for Rx<USART> {
    type Error = Error;

    fn read(&mut self) -> nb::Result<u8, Error> {
        unsafe { &*self.usart }.read()
    }
}

trait UsartConfigExt {
    type Bus;

    fn enable_configure(&self, config: Config, clocks: Clocks, bus: &mut Self::Bus);
}

impl<USART: RcuBus + Enable + Reset + Deref<Target = usart0::RegisterBlock>> UsartConfigExt
    for USART
where
    USART::Bus: GetBusFreq,
{
    type Bus = USART::Bus;

    /// Enable, reset and configure the USART.
    fn enable_configure(&self, config: Config, clocks: Clocks, bus: &mut Self::Bus) {
        // Enable clock for USART, and reset it.
        USART::enable(bus);
        USART::reset(bus);

        // Configure baud rate.
        let baud_rate_ratio = <USART as RcuBus>::Bus::get_frequency(&clocks).0 / config.baudrate.0;
        assert!((16..=0xFFFF).contains(&baud_rate_ratio));
        self.baud().write(|w| unsafe { w.bits(baud_rate_ratio) });

        // Configure parity. Note that the parity bit counts towards the word length, so we have to
        // increase it to 9 bits if parity is enabled so as to still get 8 data bits.
        match config.parity {
            Parity::ParityNone => {
                self.ctl0().modify(|_, w| w.pcen().disabled().wl().bit8());
            }
            Parity::ParityEven => {
                self.ctl0()
                    .modify(|_, w| w.pcen().enabled().wl().bit9().pm().even());
            }
            Parity::ParityOdd => {
                self.ctl0()
                    .modify(|_, w| w.pcen().enabled().wl().bit9().pm().odd());
            }
        }

        // Configure stop bits.
        self.ctl1().modify(|_, w| w.stb().variant(config.stopbits));
    }
}

trait UsartReadWrite {
    fn read(&mut self) -> nb::Result<u8, Error>;
    fn read_ready(&self) -> bool;
    fn flush(&mut self) -> nb::Result<(), Infallible>;
    fn write(&mut self, byte: u8) -> nb::Result<(), Infallible>;
    fn write_ready(&self) -> bool;

    /// Reads into the given buffer, blocking until at least one byte is available.
    fn read_buffer(&mut self, buf: &mut [u8]) -> Result<usize, Error> {
        let mut bytes_read = 0;
        while bytes_read < buf.len() {
            match self.read() {
                Ok(b) => {
                    buf[bytes_read] = b;
                    bytes_read += 1;
                }
                Err(nb::Error::WouldBlock) => {
                    if bytes_read > 0 {
                        break;
                    }
                    spin_loop();
                }
                Err(nb::Error::Other(e)) => {
                    return Err(e);
                }
            }
        }
        Ok(bytes_read)
    }

    /// Writes at least one byte from the given buffer, blocking if necessary.
    fn write_buffer(&mut self, buf: &[u8]) -> Result<usize, Error> {
        let mut bytes_written = 0;
        while bytes_written < buf.len() {
            match self.write(buf[bytes_written]) {
                Ok(()) => {
                    bytes_written += 1;
                }
                Err(nb::Error::WouldBlock) => {
                    if bytes_written > 0 {
                        break;
                    }
                    spin_loop();
                }
                Err(nb::Error::Other(_)) => {
                    unreachable!()
                }
            }
        }
        Ok(bytes_written)
    }
}

impl<USART: Deref<Target = usart0::RegisterBlock>> UsartReadWrite for USART {
    fn read(&mut self) -> nb::Result<u8, Error> {
        let status = self.stat().read();

        if status.perr().bit_is_set() {
            self.intc().write(|w| w.pec().clear());
            Err(nb::Error::Other(Error::Parity))
        } else if status.ferr().bit_is_set() {
            self.intc().write(|w| w.fec().clear());
            Err(nb::Error::Other(Error::Framing))
        } else if status.nerr().bit_is_set() {
            self.intc().write(|w| w.nec().clear());
            Err(nb::Error::Other(Error::Noise))
        } else if status.orerr().bit_is_set() {
            self.intc().write(|w| w.orec().clear());
            // Discard the previous received byte.
            self.cmd().write(|w| w.rxfcmd().discard());
            Err(nb::Error::Other(Error::Overrun))
        } else if status.rbne().bit_is_set() {
            Ok(self.rdata().read().rdata().bits() as u8)
        } else {
            Err(nb::Error::WouldBlock)
        }
    }

    fn read_ready(&self) -> bool {
        self.stat().read().rbne().bit_is_set()
    }

    fn flush(&mut self) -> nb::Result<(), Infallible> {
        let status = self.stat().read();
        if status.tc().bit_is_set() {
            Ok(())
        } else {
            Err(nb::Error::WouldBlock)
        }
    }

    fn write(&mut self, byte: u8) -> nb::Result<(), Infallible> {
        let status = self.stat().read();
        if status.tbe().bit_is_set() {
            self.tdata()
                .write(|w| unsafe { w.tdata().bits(byte.into()) });
            Ok(())
        } else {
            Err(nb::Error::WouldBlock)
        }
    }

    fn write_ready(&self) -> bool {
        self.stat().read().tbe().bit_is_set()
    }
}

macro_rules! serialdma {
    ($(
        $USARTX:ty: (
            $RxDmaX:ident,
            $TxDmaX:ident,
            $dmarxch:ty,
            $dmatxch:ty,
        ),
    )+) => {
        $(
            pub type $RxDmaX = RxDma<Rx<$USARTX>, $dmarxch>;
            pub type $TxDmaX = TxDma<Tx<$USARTX>, $dmatxch>;

            impl Receive for $RxDmaX {
                type RxChannel = $dmarxch;
                type TransmittedWord = u8;
            }

            impl Transmit for $TxDmaX {
                type TxChannel = $dmatxch;
                type ReceivedWord = u8;
            }

            impl TransferPayload for $RxDmaX {
                fn start(&mut self) {
                    self.channel.start();
                }

                fn stop(&mut self) {
                    self.channel.stop();
                }
            }

            impl TransferPayload for $TxDmaX {
                fn start(&mut self) {
                    self.channel.start();
                }

                fn stop(&mut self) {
                    self.channel.stop();
                }
            }

            impl Rx<$USARTX> {
                pub fn with_dma(self, channel: $dmarxch) -> $RxDmaX {
                    RxDma {
                        payload: self,
                        channel,
                    }
                }
            }

            impl Tx<$USARTX> {
                pub fn with_dma(self, channel: $dmatxch) -> $TxDmaX {
                    TxDma {
                        payload: self,
                        channel,
                    }
                }
            }

            impl $RxDmaX {
                pub fn split(mut self) -> (Rx<$USARTX>, $dmarxch) {
                    self.stop();
                    let RxDma { payload, channel } = self;
                    (payload, channel)
                }
            }

            impl $TxDmaX {
                pub fn split(mut self) -> (Tx<$USARTX>, $dmatxch) {
                    self.stop();
                    let TxDma { payload, channel } = self;
                    (payload, channel)
                }
            }

            impl<B> CircReadDma<B, u8> for $RxDmaX
            where
                &'static mut [B; 2]: WriteBuffer<Word = u8>,
                B: 'static,
            {
                fn circ_read(mut self, mut buffer: &'static mut [B; 2]) -> CircBuffer<B, Self> {
                    // NOTE(unsafe) We own the buffer now and we won't call other `&mut` on it
                    // until the end of the transfer.
                    let (ptr, len) = unsafe { buffer.write_buffer() };
                    self.channel
                        .set_peripheral_address(unsafe { &(*<$USARTX>::ptr()).rdata() as *const _ as u32 }, false);
                    self.channel.set_memory_address(ptr as u32, true);
                    self.channel.set_transfer_length(len);

                    atomic::compiler_fence(Ordering::Release);

                    self.channel
                        .configure_from_peripheral(Priority::Medium, Width::Bits8, Width::Bits8, true);

                    self.start();

                    CircBuffer::new(buffer, self)
                }
            }

            impl<B> ReadDma<B, u8> for $RxDmaX
            where
                B: WriteBuffer<Word = u8>,
            {
                fn read(mut self, mut buffer: B) -> Transfer<W, B, Self> {
                    // NOTE(unsafe) We own the buffer now and we won't call other `&mut` on it
                    // until the end of the transfer.
                    let (ptr, len) = unsafe { buffer.write_buffer() };
                    self.channel
                        .set_peripheral_address(unsafe { &(*<$USARTX>::ptr()).rdata() as *const _ as u32 }, false);
                    self.channel.set_memory_address(ptr as u32, true);
                    self.channel.set_transfer_length(len);

                    atomic::compiler_fence(Ordering::Release);
                    self.channel
                        .configure_from_peripheral(Priority::Medium, Width::Bits8, Width::Bits8, false);
                    self.start();

                    Transfer::w(buffer, self)
                }
            }

            impl<B> WriteDma<B, u8> for $TxDmaX
            where
                B: ReadBuffer<Word = u8>,
            {
                fn write(mut self, buffer: B) -> Transfer<R, B, Self> {
                    // Clear transmission complete bit.
                    unsafe { &*self.payload.usart }
                        .intc()
                        .write(|w| w.tcc().clear());

                    // NOTE(unsafe) We own the buffer now and we won't call other `&mut` on it
                    // until the end of the transfer.
                    let (ptr, len) = unsafe { buffer.read_buffer() };

                    self.channel
                        .set_peripheral_address(unsafe { &(*<$USARTX>::ptr()).tdata() as *const _ as u32 }, false);

                    self.channel.set_memory_address(ptr as u32, true);
                    self.channel.set_transfer_length(len);

                    atomic::compiler_fence(Ordering::Release);

                    self.channel
                        .configure_to_peripheral(Priority::Medium, Width::Bits8, Width::Bits8, false);
                    self.start();

                    Transfer::r(buffer, self)
                }
            }
        )+
    }
}

serialdma! {
    pac::Usart0: (
        RxDma0,
        TxDma0,
        dma::C2,
        dma::C1,
    ),
}

#[cfg(any(
    feature = "gd32f130x6",
    feature = "gd32f130x8",
    feature = "gd32f150x6",
    feature = "gd32f150x8",
    feature = "gd32f170x6",
    feature = "gd32f170x8",
    feature = "gd32f190x6",
    feature = "gd32f190x8",
))]
serialdma! {
    pac::Usart1: (
        RxDma1,
        TxDma1,
        dma::C4,
        dma::C3,
    ),
}