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
//! Bidirectional DMA transfers

use core::sync::atomic::{compiler_fence, Ordering};

use super::{
    single_channel::{ChannelConfig, SingleChannel},
    Pace, ReadTarget, WriteTarget,
};

/// DMA configuration for sending and receiving data simultaneously
pub struct Config<CH1, CH2, FROM, BIDI, TO>
where
    CH1: SingleChannel,
    CH2: SingleChannel,
    FROM: ReadTarget,
    BIDI: ReadTarget + WriteTarget,
    TO: WriteTarget,
{
    ch: (CH1, CH2),
    from: FROM,
    bidi: BIDI,
    to: TO,
    from_pace: Pace,
    to_pace: Pace,
    bswap: bool,
}

impl<CH1, CH2, FROM, BIDI, TO, WORD> Config<CH1, CH2, FROM, BIDI, TO>
where
    CH1: SingleChannel,
    CH2: SingleChannel,
    FROM: ReadTarget<ReceivedWord = WORD>,
    BIDI: ReadTarget<ReceivedWord = WORD> + WriteTarget<TransmittedWord = WORD>,
    TO: WriteTarget<TransmittedWord = WORD>,
{
    /// Create a DMA configuration for sending and receiving data simultaneously
    pub fn new(ch: (CH1, CH2), from: FROM, bidi: BIDI, to: TO) -> Config<CH1, CH2, FROM, BIDI, TO> {
        Config {
            ch,
            from,
            bidi,
            to,
            bswap: false,
            from_pace: Pace::PreferSink,
            to_pace: Pace::PreferSink,
        }
    }

    #[allow(clippy::wrong_self_convention)]
    /// Set the transfer pacing for the DMA transfer from the source
    pub fn from_pace(&mut self, pace: Pace) {
        self.from_pace = pace;
    }

    #[allow(clippy::wrong_self_convention)]
    /// Set the transfer pacing for the DMA transfer to the target
    pub fn to_pace(&mut self, pace: Pace) {
        self.to_pace = pace;
    }

    /// Enable/disable byteswapping for the DMA transfers, default value is false.
    ///
    /// For byte data, this has no effect. For halfword data, the two bytes of
    /// each halfword are swapped. For word data, the four bytes of each word
    /// are swapped to reverse order.
    ///
    /// This is a convenient way to change the (half-)words' byte endianness on the fly.
    pub fn bswap(&mut self, bswap: bool) {
        self.bswap = bswap;
    }

    /// Start the DMA transfer
    pub fn start(mut self) -> Transfer<CH1, CH2, FROM, BIDI, TO> {
        cortex_m::asm::dsb();
        compiler_fence(Ordering::SeqCst);

        // Configure the DMA channel and start it.
        self.ch.0.config(
            &self.from,
            &mut self.bidi,
            self.from_pace,
            self.bswap,
            None,
            false,
        );
        self.ch.1.config(
            &self.bidi,
            &mut self.to,
            self.to_pace,
            self.bswap,
            None,
            false,
        );
        self.ch.0.start_both(&mut self.ch.1);

        Transfer {
            ch: self.ch,
            from: self.from,
            bidi: self.bidi,
            to: self.to,
        }
    }
}

/// Instance of a bidirectional DMA transfer
pub struct Transfer<CH1, CH2, FROM, BIDI, TO>
where
    CH1: SingleChannel,
    CH2: SingleChannel,
    FROM: ReadTarget,
    BIDI: ReadTarget + WriteTarget,
    TO: WriteTarget,
{
    ch: (CH1, CH2),
    from: FROM,
    bidi: BIDI,
    to: TO,
}

impl<CH1, CH2, FROM, BIDI, TO, WORD> Transfer<CH1, CH2, FROM, BIDI, TO>
where
    CH1: SingleChannel,
    CH2: SingleChannel,
    FROM: ReadTarget<ReceivedWord = WORD>,
    BIDI: ReadTarget<ReceivedWord = WORD> + WriteTarget<TransmittedWord = WORD>,
    TO: WriteTarget<TransmittedWord = WORD>,
{
    /// Check if an interrupt is pending for either channel and clear the corresponding pending bit
    pub fn check_irq0(&mut self) -> bool {
        let a = self.ch.0.check_irq0();
        let b = self.ch.1.check_irq0();
        a | b
    }

    /// Check if an interrupt is pending for either channel and clear the corresponding pending bit
    pub fn check_irq1(&mut self) -> bool {
        let a = self.ch.0.check_irq1();
        let b = self.ch.1.check_irq1();
        a | b
    }

    /// Check if the transfer is completed
    pub fn is_done(&self) -> bool {
        let a = self.ch.1.ch().ch_ctrl_trig().read().busy().bit_is_set();
        let b = self.ch.0.ch().ch_ctrl_trig().read().busy().bit_is_set();
        !(a | b)
    }

    /// Block until transfer is complete
    pub fn wait(self) -> ((CH1, CH2), FROM, BIDI, TO) {
        while !self.is_done() {}

        // Make sure that memory contents reflect what the user intended.
        cortex_m::asm::dsb();
        compiler_fence(Ordering::SeqCst);

        // TODO: Use a tuple type?
        ((self.ch.0, self.ch.1), self.from, self.bidi, self.to)
    }
}