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
use core::fmt;
use crate::target_device::DMAC;
use crate::{TriggerSource, TriggerAction, Priority};
#[cfg(feature = "samd5x")]
use crate::{BurstLength, FifoThreshold};
use crate::descriptors::{TransferDescriptor};

#[cfg(feature = "samd5x")]
macro_rules! channel_reg {
    (@arm $reg:ident, $n:literal) => {
        paste::expr! { (&*DMAC::ptr()).[<$reg $n>] }
    };
    ($reg:ident, $n:expr) => {
        match $n {
            0 => channel_reg!(@arm $reg, 0),
            1 => channel_reg!(@arm $reg, 1),
            2 => channel_reg!(@arm $reg, 2),
            3 => channel_reg!(@arm $reg, 3),
            4 => channel_reg!(@arm $reg, 4),
            5 => channel_reg!(@arm $reg, 5),
            6 => channel_reg!(@arm $reg, 6),
            7 => channel_reg!(@arm $reg, 7),
            8 => channel_reg!(@arm $reg, 8),
            9 => channel_reg!(@arm $reg, 9),
            10 => channel_reg!(@arm $reg, 10),
            11 => channel_reg!(@arm $reg, 11),
            12 => channel_reg!(@arm $reg, 12),
            13 => channel_reg!(@arm $reg, 13),
            14 => channel_reg!(@arm $reg, 14),
            15 => channel_reg!(@arm $reg, 15),
            16 => channel_reg!(@arm $reg, 16),
            17 => channel_reg!(@arm $reg, 17),
            18 => channel_reg!(@arm $reg, 18),
            19 => channel_reg!(@arm $reg, 19),
            20 => channel_reg!(@arm $reg, 20),
            21 => channel_reg!(@arm $reg, 21),
            22 => channel_reg!(@arm $reg, 22),
            23 => channel_reg!(@arm $reg, 23),
            24 => channel_reg!(@arm $reg, 24),
            25 => channel_reg!(@arm $reg, 25),
            26 => channel_reg!(@arm $reg, 26),
            27 => channel_reg!(@arm $reg, 27),
            28 => channel_reg!(@arm $reg, 28),
            29 => channel_reg!(@arm $reg, 29),
            30 => channel_reg!(@arm $reg, 30),
            31 => channel_reg!(@arm $reg, 31),
            _ => unreachable!()
        }
    };
}

#[cfg(feature = "samd21")]
macro_rules! channel_reg {
    ($reg:ident, $n:expr) => {
        unsafe {
            (&*DMAC::ptr()).chid.write(|w| w.id().bits($n));
            &(&*DMAC::ptr()).$reg
        }
    };
}

/// Error type for the kinds of errors that can occur during a transaction.
#[derive(Debug)]
pub enum TransactionError {
    /// An invalid descriptor was fetched from memory.
    InvalidDescriptor,
    /// A bus error was detected during a beat transfer.
    TransferError,
    /// The CRC module detected data corruption.
    CRCError,
}

impl fmt::Display for TransactionError {
    fn fmt(&self, w: &mut fmt::Formatter) -> fmt::Result {
        use self::TransactionError::*;
        match self {
            InvalidDescriptor => write!(w, "An invalid descriptor was fetched from memory."),
            TransferError => write!(w, "A bus error was detected during a beat transfer."),
            CRCError => write!(w, "The CRC module detected data corruption.")
        }
    }
}

/// The return value of `Transaction::try_wait()`.
pub enum WaitResult {
    /// The transaction has ended or been aborted.
    Done,
    /// The transaction is suspended.
    Suspended,
    /// The transaction is still ongoing.
    Ongoing,
}

/// DMA channel.
/// 
/// This structure represents a DMA channel. Using the [`start_transfer`] method, you can
/// setup a DMA transfer transaction. You can also configure the DMA channel properties.
/// 
/// [`start_transfer`]: #method.start_transfer
pub struct Channel {
    id: u8,
    first_desc: *mut TransferDescriptor,
    write_back: *mut TransferDescriptor,
}

impl Channel {
    pub(crate) fn new(id: u8, first_desc: *mut TransferDescriptor,
        write_back: *mut TransferDescriptor) -> Channel
    {
        Channel {
            id,
            first_desc,
            write_back,
        }
    }

    /// Return the channel ID.
    pub fn id(&self) -> u8 {
        self.id
    }

    ///  Configure whether the channel continues to run in standby.
    #[cfg(feature = "samd5x")]
    pub fn set_run_standby(&mut self, run_standby: bool) {
        channel_reg!(chctrla, self.id).modify(|_, w| w.runstdby().bit(run_standby));
    }

    /// Return true if the channel continues to run in standby.
    #[cfg(feature = "samd5x")]
    pub fn get_run_standby(&self) -> bool {
        channel_reg!(chctrla, self.id).read().runstdby().bit()
    }

    /// Configure how many beats are in a burst.
    #[cfg(feature = "samd5x")]
    pub fn set_burst_length(&mut self, burst_len: BurstLength) {
        channel_reg!(chctrla, self.id).modify(|_, w| w.burstlen().bits(burst_len as u8));
    }

    /// Get the length of a burst in beats.
    #[cfg(feature = "samd5x")]
    pub fn get_burst_length(&self) -> BurstLength {
        channel_reg!(chctrla, self.id).read().burstlen().variant().into()
    }

    /// Set the trigger action for the channel.
    pub fn set_trigger_action(&mut self, trig_act: TriggerAction) {
        #[cfg(feature = "samd5x")]
        let reg = channel_reg!(chctrla, self.id);
        #[cfg(feature = "samd21")]
        let reg = channel_reg!(chctrlb, self.id);
        reg.modify(|_, w| unsafe { w.trigact().bits(trig_act as u8) });
    }

    /// Get the trigger action for the channel.
    pub fn get_trigger_action(&self) -> TriggerAction {
        #[cfg(feature = "samd5x")]
        let reg = channel_reg!(chctrla, self.id);
        #[cfg(feature = "samd21")]
        let reg = channel_reg!(chctrlb, self.id);
        reg.read().trigact().variant().into()
    }

    /// Set threshold for when destination writes occur.
    #[cfg(feature = "samd5x")]
    pub fn set_fifo_threshold(&mut self, fifo_threshold: FifoThreshold) {
        channel_reg!(chctrla, self.id).modify(|_, w| w.threshold().bits(fifo_threshold as u8));
    }

    /// Get the threshold for when destination writes will occur.
    #[cfg(feature = "samd5x")]
    pub fn get_fifi_threshold(&self) -> FifoThreshold {
        channel_reg!(chctrla, self.id).read().threshold().variant().into()
    }

    /// Set the source trigger for the DMA Channel.
    pub fn set_source(&mut self, source: TriggerSource) {
        #[cfg(feature = "samd5x")]
        let reg = channel_reg!(chctrla, self.id);

        #[cfg(feature = "samd21")]
        let reg = channel_reg!(chctrlb, self.id);
        reg.modify(|_, w| unsafe { w.trigsrc().bits(source as u8)});
    }

    /// Get the trigger source for the channel.
    pub fn get_source(&self) -> TriggerSource {
        #[cfg(feature = "samd5x")]
        let reg = channel_reg!(chctrla, self.id);

        #[cfg(feature = "samd21")]
        let reg = channel_reg!(chctrlb, self.id);
        reg.read().trigsrc().variant().into()
    }

    /// Set the priority level of the channel.
    pub fn set_priority(&mut self, priority: Priority) {
        #[cfg(feature = "samd5x")]
        channel_reg!(chprilvl, self.id).write(|w| unsafe { w.prilvl().bits(priority as u8) });
        #[cfg(feature = "samd21")]
        channel_reg!(chctrlb, self.id).modify(|_, w| w.lvl().bits(priority as u8))
    }

    /// Get channel priority level.
    pub fn get_priority(&self) -> Priority {
        #[cfg(feature = "samd5x")]
        return channel_reg!(chprilvl, self.id).read().prilvl().variant().into();
        #[cfg(feature = "samd21")]
        return channel_reg!(chctrlb, self.id).read().lvl().variant().into();
    }

    pub fn get_first_descriptor(&self) -> &mut TransferDescriptor {
        unsafe { &mut *self.first_desc }
    }

    /// Read descriptor from the Write-back Address of this channel.
    /// 
    /// # Safety
    /// 
    /// The write-back address of a DMA channel is volatile, as the DMA engine can change it at any time.
    /// As such, reading and writing to this descriptor is unsafe. Act cautiously.
    pub fn get_writeback_descriptor(&mut self) -> *mut TransferDescriptor {
        self.write_back 
    }

    /// Enable the DMA channel.
    /// 
    /// After this call, this channel will be a part of the DMA arbitration scheme (if its corresponding priority level 
    /// is active), and trigger events will cause the transaction to start from the first descriptor.
    pub fn enable(&self) {
        channel_reg!(chctrla, self.id).modify(|_, w| w.enable().set_bit());
    }

    /// Reset the DMA channel. This will set all channel registers to their reset values.
    /// 
    /// If the channel is still enabled (or is in the process of being disabled), this command will be ignored.
    pub fn reset(&mut self) {
        channel_reg!(chctrla, self.id).modify(|_, w| w.swrst().set_bit());
    }

    /// Manually trigger the channel.
    /// 
    /// # Interrupt Safety
    /// This method is not interrupt-safe, and could lead to lost updates.
    /// 
    /// It is the responsibility of the caller to ensure that the call-site is in an interrupt-free section.
    pub fn trigger(&mut self) {
        unsafe {&*DMAC::ptr()}.swtrigctrl.modify(|r, w| unsafe {
            w.bits(r.bits() | (1 << self.id))
        })
    }

    /// Suspend the ongoing transaction. Returns `true` if the command was successfully written, `false` if another 
    /// command is ongoing.
    /// 
    /// This call returns immediately, but the suspend operation won't complete until the ongoing burst transfer 
    /// completes.
    pub fn suspend(&self) -> bool {
        let mut ongoing = true;
        channel_reg!(chctrlb, self.id).modify(|r, w| {
            if r.cmd().is_noact() {
                w.cmd().suspend();
                ongoing = false;
            }
            w
        });
        ongoing
    }

    /// Resume the ongoing transaction. Returns `true` if command was successfully written, `false` if another command 
    /// is ongoing.
    pub fn resume(&self) -> bool {
        let mut ongoing = true;
        channel_reg!(chctrlb, self.id).modify(|r, w| {
            if r.cmd().is_noact() {
                w.cmd().resume();
                ongoing = false;
            }
            w
        });
        ongoing
    }

    /// Disable the channel. This aborts any ongoing transaction.
    /// 
    /// This call returns immediately, but the transaction will not be aborted until
    /// the ongoing burst transfer completes.
    pub fn disable(&mut self) {
        channel_reg!(chctrla, self.id).modify(|_, w| w.enable().clear_bit());
    }

    /// Returns `true` if a transfer is pending on the channel.
    /// Returns `false` if a channel trigger action is completed, a bus error is detected, or the channel is disabled.
    pub fn is_pending(&self) -> bool {
        channel_reg!(chstatus, self.id).read().pend().bit()
    }

    /// Returns `true` if the channel has started a transfer.
    /// Returns `false` if a channel trigger action is started, a bus error is detected, or the channel is disabled.
    pub fn is_busy(&self) -> bool {
        channel_reg!(chstatus, self.id).read().busy().bit()
    }

    /// Poll the channel to determine the status of the transaction.
    /// 
    /// This will read and reset the interrupt flag registers of the channel to determine the status of the channel.
    /// Therefore it is expected to be called in the appropriate interrupt vector.
    /// 
    /// Any non-error state will return `Ok(WaitResult)`.
    /// Any errors will be returned as `Err(TransactionError)`.
    pub fn poll_status(&self) -> Result<WaitResult, TransactionError> {
        let intflag = channel_reg!(chintflag, self.id).read();
        let status = channel_reg!(chstatus, self.id).read();
        channel_reg!(chintflag, self.id).reset();

        if channel_reg!(chctrla, self.id).read().enable().bit_is_set() {
            if intflag.terr().bit_is_set() {
                #[cfg(feature = "samd5x")]
                if status.crcerr().bit_is_set() {
                    return Err(TransactionError::CRCError);
                }
                return Err(TransactionError::TransferError);
            } else {
                return Ok(WaitResult::Done);
            }
        }

        if intflag.susp().bit_is_set() {
            if status.ferr().bit_is_set() {
                return Err(TransactionError::InvalidDescriptor);
            } else {
                return Ok(WaitResult::Suspended)
            }
        }

        Ok(WaitResult::Ongoing)
    } 
}