1 /**
2  * \file
3  *
4  * \brief SAM Direct Memory Access Controller Driver
5  *
6  * Copyright (C) 2014-2016 Atmel Corporation. All rights reserved.
7  *
8  * \asf_license_start
9  *
10  * \page License
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions are met:
14  *
15  * 1. Redistributions of source code must retain the above copyright notice,
16  *    this list of conditions and the following disclaimer.
17  *
18  * 2. Redistributions in binary form must reproduce the above copyright notice,
19  *    this list of conditions and the following disclaimer in the documentation
20  *    and/or other materials provided with the distribution.
21  *
22  * 3. The name of Atmel may not be used to endorse or promote products derived
23  *    from this software without specific prior written permission.
24  *
25  * 4. This software may only be redistributed and used in connection with an
26  *    Atmel microcontroller product.
27  *
28  * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
29  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
30  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
31  * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
32  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
36  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
37  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38  * POSSIBILITY OF SUCH DAMAGE.
39  *
40  * \asf_license_stop
41  *
42  */
43 /*
44  * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
45  */
46 #ifndef DMA_H_INCLUDED
47 #define DMA_H_INCLUDED
48 
49 #ifdef __cplusplus
50 extern "C" {
51 #endif
52 
53 /**
54  * \defgroup asfdoc_sam0_dma_group SAM Direct Memory Access Controller (DMAC) Driver
55  *
56  * This driver for Atmel&reg; | SMART ARM®-based microcontrollers provides an interface for the configuration
57  * and management of the Direct Memory Access Controller(DMAC) module within
58  * the device. The DMAC can transfer data between memories and peripherals, and
59  * thus off-load these tasks from the CPU. The module supports peripheral to
60  * peripheral, peripheral to memory, memory to peripheral, and memory to memory
61  * transfers.
62  *
63  * The following peripheral is used by the DMAC Driver:
64  * - DMAC (Direct Memory Access Controller)
65  *
66  * The following devices can use this module:
67  *  - Atmel | SMART SAM D21
68  *  - Atmel | SMART SAM R21
69  *  - Atmel | SMART SAM D09/D10/D11
70  *  - Atmel | SMART SAM L21/L22
71  *  - Atmel | SMART SAM DA1
72  *  - Atmel | SMART SAM C20/C21
73  *  - Atmel | SMART SAM HA1
74  *  - Atmel | SMART SAM R30
75  *
76  * The outline of this documentation is as follows:
77  * - \ref asfdoc_sam0_dma_prerequisites
78  * - \ref asfdoc_sam0_dma_module_overview
79  * - \ref asfdoc_sam0_dma_special_considerations
80  * - \ref asfdoc_sam0_dma_extra_info
81  * - \ref asfdoc_sam0_dma_examples
82  * - \ref asfdoc_sam0_dma_api_overview
83  *
84  *
85  * \section asfdoc_sam0_dma_prerequisites Prerequisites
86  *
87  * There are no prerequisites for this module.
88  *
89  *
90  * \section asfdoc_sam0_dma_module_overview Module Overview
91  *
92  * SAM devices with DMAC enables high data transfer rates with minimum
93  * CPU intervention and frees up CPU time. With access to all peripherals,
94  * the DMAC can handle automatic transfer of data to/from modules.
95  * It supports static and incremental addressing for both source and
96  * destination.
97  *
98  * The DMAC when used with Event System or peripheral triggers, provides a
99  * considerable advantage by reducing the power consumption and performing
100  * data transfer in the background.
101  * For example, if the ADC is configured to generate an event, it can trigger
102  * the DMAC to transfer the data into another peripheral or SRAM.
103  * The CPU can remain in sleep during this time to reduce the power consumption.
104  *
105  * <table>
106  *    <tr>
107  *      <th>Device</th>
108  *      <th>Dma channel number</th>
109  *    </tr>
110  *    <tr>
111  *      <td>SAM D21/R21/C20/C21</td>
112  *      <td>12</td>
113  *    </tr>
114  *    <tr>
115  *      <td>SAM D09/D10/D11</td>
116  *      <td>6</td>
117  *    </tr>
118  *    <tr>
119  *      <td>SAM L21,SAMR30</td>
120  *      <td>16</td>
121  *    </tr>
122  * </table>
123  * The DMA channel operation can be suspended at any time by software, by events
124  * from event system, or after selectable descriptor execution. The operation
125  * can be resumed by software or by events from the event system.
126  * The DMAC driver for SAM supports four types of transfers such as
127  * peripheral to peripheral, peripheral to memory, memory to peripheral, and
128  * memory to memory.
129  *
130  * The basic transfer unit is a beat, which is defined as a single bus access.
131  * There can be multiple beats in a single block transfer and multiple block
132  * transfers in a DMA transaction.
133  * DMA transfer is based on descriptors, which holds transfer properties
134  * such as the source and destination addresses, transfer counter, and other
135  * additional transfer control information.
136  * The descriptors can be static or linked. When static, a single block transfer
137  * is performed. When linked, a number of transfer descriptors can be used to
138  * enable multiple block transfers within a single DMA transaction.
139  *
140  * The implementation of the DMA driver is based on the idea that the DMA channel
141  * is a finite resource of entities with the same abilities. A DMA channel resource
142  * is able to move a defined set of data from a source address to destination
143  * address triggered by a transfer trigger. On the SAM devices there are 12
144  * DMA resources available for allocation. Each of these DMA resources can trigger
145  * interrupt callback routines and peripheral events.
146  * The other main features are:
147  *
148  * - Selectable transfer trigger source
149  *  - Software
150  *  - Event System
151  *  - Peripheral
152  * - Event input and output is supported for the four lower channels
153  * - Four level channel priority
154  * - Optional interrupt generation on transfer complete, channel error, or channel suspend
155  * - Supports multi-buffer or circular buffer mode by linking multiple descriptors
156  * - Beat size configurable as 8-bit, 16-bit, or 32-bit
157  *
158  * A simplified block diagram of the DMA Resource can be seen in
159  * \ref asfdoc_sam0_dma_module_block_diagram "the figure below".
160  *
161  * \anchor asfdoc_sam0_dma_module_block_diagram
162  * \dot
163  * digraph overview {
164  * splines = false;
165  * rankdir=LR;
166  *
167  * mux1 [label="Transfer Trigger", shape=box];
168  *
169  * dma [label="DMA Channel", shape=polygon, sides=6, orientation=60, style=filled, fillcolor=darkolivegreen1, height=1, width=1];
170  * descriptor [label="Transfer Descriptor", shape=box, style=filled, fillcolor=lightblue];
171  *
172  * mux1 -> dma;
173  * descriptor -> dma;
174  *
175  * interrupt [label="Interrupt", shape=box];
176  * events [label="Events", shape=box];
177  *
178  * dma:e -> interrupt:w;
179  * dma:e -> events:w;
180  *
181  * {rank=same; descriptor dma}
182  *
183  * }
184  * \enddot
185  *
186  * \subsection asfdoc_sam0_dma_features Driver Feature Macro Definition
187  * <table>
188  *  <tr>
189  *    <th>Driver Feature Macro</th>
190  *    <th>Supported devices</th>
191  *  </tr>
192  *  <tr>
193  *    <td>FEATURE_DMA_CHANNEL_STANDBY</td>
194  *    <td>SAM L21/L22/C20/C21/R30</td>
195  *  </tr>
196  * </table>
197  * \note The specific features are only available in the driver when the
198  * selected device supports those features.
199  *
200  * \subsection asfdoc_sam0_dma_module_overview_dma_transf_term Terminology Used in DMAC Transfers
201  *
202  *   <table border="0" cellborder="1" cellspacing="0" >
203  *    <tr>
204  *        <th> Name </th> <th> Description </th>
205  *    </tr>
206  *    <tr>
207  *     <td > Beat </td>
208  *     <td > It is a single bus access by the DMAC.
209  *           Configurable as 8-bit, 16-bit, or 32-bit.
210  *     </td>
211  *    </tr>
212  *    <tr>
213  *     <td > Burst </td>
214  *     <td> It is a transfer of n-beats (n=1,4,8,16).
215  *          For the DMAC module in SAM, the burst size is one beat.
216  *          Arbitration takes place each time a burst transfer is completed.
217  *     </td>
218  *    </tr>
219  *    <tr>
220  *     <td > Block transfer </td>
221  *     <td>  A single block transfer is a configurable number of (1 to 64k)
222  *           beat transfers
223  *     </td>
224  *    </tr>
225  *   </table>
226  *
227  * \subsection asfdoc_sam0_dma_module_overview_dma_channels DMA Channels
228  * The DMAC in each device consists of several DMA channels, which
229  * along with the transfer descriptors defines the data transfer properties.
230  * - The transfer control descriptor defines the source and destination
231  * addresses, source and destination address increment settings, the
232  * block transfer count, and event output condition selection
233  * - Dedicated channel registers control the peripheral trigger source,
234  * trigger mode settings, event input actions, and channel priority level
235  * settings
236  *
237  * With a successful DMA resource allocation, a dedicated
238  * DMA channel will be assigned. The channel will be occupied until the
239  * DMA resource is freed. A DMA resource handle is used to identify the specific
240  * DMA resource.
241  * When there are multiple channels with active requests, the arbiter prioritizes
242  * the channels requesting access to the bus.
243  *
244  * \subsection asfdoc_sam0_dma_module_overview_dma_trigger DMA Triggers
245  * DMA transfer can be started only when a DMA transfer request is acknowledged/granted by the arbiter. A
246  * transfer request can be triggered from software, peripheral, or an event. There
247  * are dedicated source trigger selections for each DMA channel usage.
248 
249  *
250  * \subsection asfdoc_sam0_dma_module_overview_dma_transfer_descriptor DMA Transfer Descriptor
251  * The transfer descriptor resides in the SRAM and
252  * defines these channel properties.
253  *   <table border="0" cellborder="1" cellspacing="0" >
254  *    <tr>
255  *        <th> Field name </th> <th> Field width </th>
256  *    </tr>
257  *    <tr>
258  *     <td > Descriptor Next Address </td> <td > 32 bits </td>
259  *    </tr>
260  *    <tr>
261  *     <td > Destination Address </td> <td> 32 bits </td>
262  *    </tr>
263  *    <tr>
264  *     <td > Source Address </td> <td> 32 bits </td>
265  *    </tr>
266  *    <tr>
267  *     <td > Block Transfer Counter </td> <td> 16 bits </td>
268  *    </tr>
269  *    <tr>
270  *     <td > Block Transfer Control </td> <td> 16 bits </td>
271  *    </tr>
272  *   </table>
273  *
274  * Before starting a transfer, at least one descriptor should be configured.
275  * After a successful allocation of a DMA channel, the transfer descriptor can
276  * be added with a call to \ref dma_add_descriptor(). If there is a transfer
277  * descriptor already allocated to the DMA resource, the descriptor will
278  * be linked to the next descriptor address.
279  *
280  * \subsection asfdoc_sam0_dma_module_overview_dma_output DMA Interrupts/Events
281  * Both an interrupt callback and an peripheral event can be triggered by the
282  * DMA transfer. Three types of callbacks are supported by the DMA driver:
283  * transfer complete, channel suspend, and transfer error. Each of these callback
284  * types can be registered and enabled for each channel independently through
285  * the DMA driver API.
286  *
287  * The DMAC module can also generate events on transfer complete. Event
288  * generation is enabled through the DMA channel, event channel configuration,
289  * and event user multiplexing is done through the events driver.
290  *
291  * The DMAC can generate events in the below cases:
292  *
293  * - When a block transfer is complete
294  *
295  * - When each beat transfer within a block transfer is complete
296  *
297  * \section asfdoc_sam0_dma_special_considerations Special Considerations
298  *
299  * There are no special considerations for this module.
300  *
301  *
302  * \section asfdoc_sam0_dma_extra_info Extra Information
303  *
304  * For extra information, see \ref asfdoc_sam0_dma_extra. This includes:
305  * - \ref asfdoc_sam0_dma_extra_acronyms
306  * - \ref asfdoc_sam0_dma_extra_dependencies
307  * - \ref asfdoc_sam0_dma_extra_errata
308  * - \ref asfdoc_sam0_dma_extra_history
309  *
310  *
311  * \section asfdoc_sam0_dma_examples Examples
312  *
313  * For a list of examples related to this driver, see
314  * \ref asfdoc_sam0_dma_exqsg.
315  *
316  *
317  * \section asfdoc_sam0_dma_api_overview API Overview
318  * @{
319  */
320 
321 #include <compiler.h>
322 #include "conf_dma.h"
323 
324 #if (SAML21) || (SAML22) || (SAMC20) || (SAMC21) || (SAMR30) || defined(__DOXYGEN__)
325 #define FEATURE_DMA_CHANNEL_STANDBY
326 #endif
327 
328 /** DMA invalid channel number. */
329 #define DMA_INVALID_CHANNEL        0xff
330 
331 /** ExInitial description section. */
332 extern DmacDescriptor descriptor_section[CONF_MAX_USED_CHANNEL_NUM];
333 
334 /* DMA channel interrup flag. */
335 extern uint8_t g_chan_interrupt_flag[CONF_MAX_USED_CHANNEL_NUM];
336 
337 /** DMA priority level. */
338 enum dma_priority_level {
339 	/** Priority level 0. */
340 	DMA_PRIORITY_LEVEL_0,
341 	/** Priority level 1. */
342 	DMA_PRIORITY_LEVEL_1,
343 	/** Priority level 2. */
344 	DMA_PRIORITY_LEVEL_2,
345 	/** Priority level 3. */
346 	DMA_PRIORITY_LEVEL_3,
347 };
348 
349 /** DMA input actions. */
350 enum dma_event_input_action {
351 	/** No action. */
352 	DMA_EVENT_INPUT_NOACT,
353 	/** Normal transfer and periodic transfer trigger. */
354 	DMA_EVENT_INPUT_TRIG,
355 	/** Conditional transfer trigger. */
356 	DMA_EVENT_INPUT_CTRIG,
357 	/** Conditional block transfer. */
358 	DMA_EVENT_INPUT_CBLOCK,
359 	/** Channel suspend operation. */
360 	DMA_EVENT_INPUT_SUSPEND,
361 	/** Channel resume operation. */
362 	DMA_EVENT_INPUT_RESUME,
363 	/** Skip next block suspend action. */
364 	DMA_EVENT_INPUT_SSKIP,
365 };
366 
367 /**
368  * Address increment step size. These bits select the address increment step
369  * size. The setting apply to source or destination address, depending on
370  * STEPSEL setting.
371  */
372 enum dma_address_increment_stepsize {
373 	/** The address is incremented by (beat size * 1). */
374 	DMA_ADDRESS_INCREMENT_STEP_SIZE_1 = 0,
375 	/** The address is incremented by (beat size * 2). */
376 	DMA_ADDRESS_INCREMENT_STEP_SIZE_2,
377 	/** The address is incremented by (beat size * 4). */
378 	DMA_ADDRESS_INCREMENT_STEP_SIZE_4,
379 	/** The address is incremented by (beat size * 8). */
380 	DMA_ADDRESS_INCREMENT_STEP_SIZE_8,
381 	/** The address is incremented by (beat size * 16). */
382 	DMA_ADDRESS_INCREMENT_STEP_SIZE_16,
383 	/** The address is incremented by (beat size * 32). */
384 	DMA_ADDRESS_INCREMENT_STEP_SIZE_32,
385 	/** The address is incremented by (beat size * 64). */
386 	DMA_ADDRESS_INCREMENT_STEP_SIZE_64,
387 	/** The address is incremented by (beat size * 128). */
388 	DMA_ADDRESS_INCREMENT_STEP_SIZE_128,
389 };
390 
391 /**
392  * DMA step selection. This bit determines whether the step size setting
393  * is applied to source or destination address.
394  */
395 enum dma_step_selection {
396 	/** Step size settings apply to the destination address. */
397 	DMA_STEPSEL_DST = 0,
398 	/** Step size settings apply to the source address. */
399 	DMA_STEPSEL_SRC,
400 };
401 
402 /** The basic transfer unit in DMAC is a beat, which is defined as a
403  *  single bus access. Its size is configurable and applies to both read
404  *  and write. */
405 enum dma_beat_size {
406 	/** 8-bit access. */
407 	DMA_BEAT_SIZE_BYTE = 0,
408 	/** 16-bit access. */
409 	DMA_BEAT_SIZE_HWORD,
410 	/** 32-bit access. */
411 	DMA_BEAT_SIZE_WORD,
412 };
413 
414 /**
415  * Block action definitions.
416  */
417 enum dma_block_action {
418 	/** No action. */
419 	DMA_BLOCK_ACTION_NOACT = 0,
420 	/** Channel in normal operation and sets transfer complete interrupt flag
421 	 *  after block transfer. */
422 	DMA_BLOCK_ACTION_INT,
423 	/** Trigger channel suspend after block transfer and sets channel
424 	 *  suspend interrupt flag once the channel is suspended. */
425 	DMA_BLOCK_ACTION_SUSPEND,
426 	/** Sets transfer complete interrupt flag after a block transfer and
427 	 *  trigger channel suspend. The channel suspend interrupt flag will be set
428 	 *  once the channel is suspended. */
429 	DMA_BLOCK_ACTION_BOTH,
430 };
431 
432 /** Event output selection. */
433 enum dma_event_output_selection {
434 	/** Event generation disable. */
435 	DMA_EVENT_OUTPUT_DISABLE = 0,
436 	/** Event strobe when block transfer complete. */
437 	DMA_EVENT_OUTPUT_BLOCK,
438 	/** Event output reserved. */
439 	DMA_EVENT_OUTPUT_RESERVED,
440 	/** Event strobe when beat transfer complete. */
441 	DMA_EVENT_OUTPUT_BEAT,
442 };
443 
444 /** DMA trigger action type. */
445 enum dma_transfer_trigger_action{
446 	/** Perform a block transfer when triggered. */
447 	DMA_TRIGGER_ACTION_BLOCK = DMAC_CHCTRLB_TRIGACT_BLOCK_Val,
448 	/** Perform a beat transfer when triggered. */
449 	DMA_TRIGGER_ACTION_BEAT = DMAC_CHCTRLB_TRIGACT_BEAT_Val,
450 	/** Perform a transaction when triggered. */
451 	DMA_TRIGGER_ACTION_TRANSACTION = DMAC_CHCTRLB_TRIGACT_TRANSACTION_Val,
452 };
453 
454 /**
455  * Callback types for DMA callback driver.
456  */
457 enum dma_callback_type {
458 	/** Callback for any of transfer errors. A transfer error is flagged
459      *	if a bus error is detected during an AHB access or when the DMAC
460 	 *  fetches an invalid descriptor. */
461 	DMA_CALLBACK_TRANSFER_ERROR,
462 	/** Callback for transfer complete. */
463 	DMA_CALLBACK_TRANSFER_DONE,
464 	/** Callback for channel suspend. */
465 	DMA_CALLBACK_CHANNEL_SUSPEND,
466 	/** Number of available callbacks. */
467 	DMA_CALLBACK_N,
468 };
469 
470 /**
471  * DMA transfer descriptor configuration. When the source or destination address
472  * increment is enabled, the addresses stored into the configuration structure
473  * must correspond to the end of the transfer.
474  *
475  */
476 struct dma_descriptor_config {
477 	/** Descriptor valid flag used to identify whether a descriptor is
478 	    valid or not */
479 	bool descriptor_valid;
480 	/** This is used to generate an event on specific transfer action in
481 	    a channel. Supported only in four lower channels. */
482 	enum dma_event_output_selection event_output_selection;
483 	/** Action taken when a block transfer is completed */
484 	enum dma_block_action block_action;
485 	/** Beat size is configurable as 8-bit, 16-bit, or 32-bit */
486 	enum dma_beat_size beat_size;
487 	/** Used for enabling the source address increment */
488 	bool src_increment_enable;
489 	/** Used for enabling the destination address increment */
490 	bool dst_increment_enable;
491 	/** This bit selects whether the source or destination address is
492 	    using the step size settings */
493 	enum dma_step_selection step_selection;
494 	/** The step size for source/destination address increment.
495 	    The next address is calculated
496 	    as next_addr = addr + (2^step_size * beat size). */
497 	enum dma_address_increment_stepsize step_size;
498 	/** It is the number of beats in a block. This count value is
499 	 * decremented by one after each beat data transfer. */
500 	uint16_t block_transfer_count;
501 	/** Transfer source address */
502 	uint32_t source_address;
503 	/** Transfer destination address */
504 	uint32_t destination_address;
505 	/** Set to zero for static descriptors. This must have a valid memory
506 	    address for linked descriptors. */
507 	uint32_t next_descriptor_address;
508 };
509 
510 /** Configurations for DMA events. */
511 struct dma_events_config {
512 	/** Event input actions */
513 	enum dma_event_input_action input_action;
514 	/** Enable DMA event output */
515 	bool event_output_enable;
516 };
517 
518 /** DMA configurations for transfer. */
519 struct dma_resource_config {
520 	/** DMA transfer priority */
521 	enum dma_priority_level priority;
522 	/**DMA peripheral trigger index */
523 	uint8_t peripheral_trigger;
524 	/** DMA trigger action */
525 	enum dma_transfer_trigger_action trigger_action;
526 #ifdef FEATURE_DMA_CHANNEL_STANDBY
527 	/** Keep DMA channel enabled in standby sleep mode if true */
528 	bool run_in_standby;
529 #endif
530 	/** DMA events configurations */
531 	struct dma_events_config event_config;
532 };
533 
534 /** Forward definition of the DMA resource. */
535 struct dma_resource;
536 /** Type definition for a DMA resource callback function. */
537 typedef void (*dma_callback_t)(struct dma_resource *const resource);
538 
539 /** Structure for DMA transfer resource. */
540 struct dma_resource {
541 	/** Allocated DMA channel ID */
542 	uint8_t channel_id;
543 	/** Array of callback functions for DMA transfer job */
544 	dma_callback_t callback[DMA_CALLBACK_N];
545 	/** Bit mask for enabled callbacks */
546 	uint8_t callback_enable;
547 	/** Status of the last job */
548 	volatile enum status_code job_status;
549 	/** Transferred data size */
550 	uint32_t transfered_size;
551 	/** DMA transfer descriptor */
552 	DmacDescriptor* descriptor;
553 };
554 
555 /**
556  * \brief Get DMA resource status.
557  *
558  * \param[in] resource Pointer to the DMA resource
559  *
560  * \return Status of the DMA resource.
561  */
dma_get_job_status(struct dma_resource * resource)562 static inline enum status_code dma_get_job_status(struct dma_resource *resource)
563 {
564 	Assert(resource);
565 
566 	return resource->job_status;
567 }
568 
569 /**
570  * \brief Check if the given DMA resource is busy.
571  *
572  * \param[in] resource Pointer to the DMA resource
573  *
574  * \return Status which indicates whether the DMA resource is busy.
575  *
576  * \retval true The DMA resource has an on-going transfer
577  * \retval false The DMA resource is not busy
578  */
dma_is_busy(struct dma_resource * resource)579 static inline bool dma_is_busy(struct dma_resource *resource)
580 {
581 	Assert(resource);
582 
583 	return (resource->job_status == STATUS_BUSY);
584 }
585 
586 /**
587  * \brief Enable a callback function for a dedicated DMA resource.
588  *
589  * \param[in] resource Pointer to the DMA resource
590  * \param[in] type Callback function type
591  *
592  */
dma_enable_callback(struct dma_resource * resource,enum dma_callback_type type)593 static inline void dma_enable_callback(struct dma_resource *resource,
594 		enum dma_callback_type type)
595 {
596 	Assert(resource);
597 
598 	resource->callback_enable |= 1 << type;
599 	g_chan_interrupt_flag[resource->channel_id] |= (1UL << type);
600 }
601 
602 /**
603  * \brief Disable a callback function for a dedicated DMA resource.
604  *
605  * \param[in] resource Pointer to the DMA resource
606  * \param[in] type Callback function type
607  *
608  */
dma_disable_callback(struct dma_resource * resource,enum dma_callback_type type)609 static inline void dma_disable_callback(struct dma_resource *resource,
610 		enum dma_callback_type type)
611 {
612 	Assert(resource);
613 
614 	resource->callback_enable &= ~(1 << type);
615 	g_chan_interrupt_flag[resource->channel_id] &= (~(1UL << type) & DMAC_CHINTENSET_MASK);
616 	DMAC->CHINTENCLR.reg = (1UL << type);
617 }
618 
619 /**
620  * \brief Register a callback function for a dedicated DMA resource.
621  *
622  * There are three types of callback functions, which can be registered:
623  * - Callback for transfer complete
624  * - Callback for transfer error
625  * - Callback for channel suspend
626  *
627  * \param[in] resource Pointer to the DMA resource
628  * \param[in] callback Pointer to the callback function
629  * \param[in] type Callback function type
630  *
631  */
dma_register_callback(struct dma_resource * resource,dma_callback_t callback,enum dma_callback_type type)632 static inline void dma_register_callback(struct dma_resource *resource,
633 		dma_callback_t callback, enum dma_callback_type type)
634 {
635 	Assert(resource);
636 
637 	resource->callback[type] = callback;
638 }
639 
640 /**
641  * \brief Unregister a callback function for a dedicated DMA resource.
642  *
643  * There are three types of callback functions:
644  * - Callback for transfer complete
645  * - Callback for transfer error
646  * - Callback for channel suspend
647  *
648  * The application can unregister any of the callback functions which
649  * are already registered and are no longer needed.
650  *
651  * \param[in] resource Pointer to the DMA resource
652  * \param[in] type Callback function type
653  *
654  */
dma_unregister_callback(struct dma_resource * resource,enum dma_callback_type type)655 static inline void dma_unregister_callback(struct dma_resource *resource,
656 		enum dma_callback_type type)
657 {
658 	Assert(resource);
659 
660 	resource->callback[type] = NULL;
661 }
662 
663 /**
664  * \brief Will set a software trigger for resource.
665  *
666  * This function is used to set a software trigger on the DMA channel
667  * associated with resource. If a trigger is already pending no new trigger
668  * will be generated for the channel.
669  *
670  * \param[in] resource Pointer to the DMA resource
671  */
dma_trigger_transfer(struct dma_resource * resource)672 static inline void dma_trigger_transfer(struct dma_resource *resource) {
673 	Assert(resource);
674 
675 	DMAC->SWTRIGCTRL.reg |= (1 << resource->channel_id);
676 }
677 
678 /**
679  * \brief Initializes DMA transfer configuration with predefined default values.
680  *
681  * This function will initialize a given DMA descriptor configuration structure to
682  * a set of known default values. This function should be called on
683  * any new instance of the configuration structure before being
684  * modified by the user application.
685  *
686  * The default configuration is as follows:
687  *  \li Set the descriptor as valid
688  *  \li Disable event output
689  *  \li No block action
690  *  \li Set beat size as byte
691  *  \li Enable source increment
692  *  \li Enable destination increment
693  *  \li Step size is applied to the destination address
694  *  \li Address increment is beat size multiplied by 1
695  *  \li Default transfer size is set to 0
696  *  \li Default source address is set to NULL
697  *  \li Default destination address is set to NULL
698  *  \li Default next descriptor not available
699  * \param[out] config Pointer to the configuration
700  *
701  */
dma_descriptor_get_config_defaults(struct dma_descriptor_config * config)702 static inline void dma_descriptor_get_config_defaults(struct dma_descriptor_config *config)
703 {
704 	Assert(config);
705 
706 	/* Set descriptor as valid */
707 	config->descriptor_valid = true;
708 	/* Disable event output */
709 	config->event_output_selection = DMA_EVENT_OUTPUT_DISABLE;
710 	/* No block action */
711 	config->block_action = DMA_BLOCK_ACTION_NOACT;
712 	/* Set beat size to one byte */
713 	config->beat_size = DMA_BEAT_SIZE_BYTE;
714 	/* Enable source increment */
715 	config->src_increment_enable = true;
716 	/* Enable destination increment */
717 	config->dst_increment_enable = true;
718 	/* Step size is applied to the destination address */
719 	config->step_selection = DMA_STEPSEL_DST;
720 	/* Address increment is beat size multiplied by 1*/
721 	config->step_size = DMA_ADDRESS_INCREMENT_STEP_SIZE_1;
722 	/* Default transfer size is set to 0 */
723 	config->block_transfer_count = 0;
724 	/* Default source address is set to NULL */
725 	config->source_address = (uint32_t)NULL;
726 	/* Default destination address is set to NULL */
727 	config->destination_address = (uint32_t)NULL;
728 	/** Next descriptor address set to 0 */
729 	config->next_descriptor_address = 0;
730 }
731 
732 /**
733  * \brief Update DMA descriptor.
734  *
735  * This function can update the descriptor of an allocated DMA resource.
736  *
737  */
dma_update_descriptor(struct dma_resource * resource,DmacDescriptor * descriptor)738 static inline void dma_update_descriptor(struct dma_resource *resource,
739 		DmacDescriptor* descriptor)
740 {
741 	Assert(resource);
742 
743 	resource->descriptor = descriptor;
744 }
745 
746 /**
747  * \brief Reset DMA descriptor.
748  *
749  * This function will clear the DESCADDR register of an allocated DMA resource.
750  *
751  */
dma_reset_descriptor(struct dma_resource * resource)752 static inline void dma_reset_descriptor(struct dma_resource *resource)
753 {
754 	Assert(resource);
755 
756 	resource->descriptor = NULL;
757 }
758 
759 void dma_get_config_defaults(struct dma_resource_config *config);
760 enum status_code dma_allocate(struct dma_resource *resource,
761 		struct dma_resource_config *config);
762 enum status_code dma_free(struct dma_resource *resource);
763 enum status_code dma_start_transfer_job(struct dma_resource *resource);
764 void dma_abort_job(struct dma_resource *resource);
765 void dma_suspend_job(struct dma_resource *resource);
766 void dma_resume_job(struct dma_resource *resource);
767 void dma_descriptor_create(DmacDescriptor* descriptor,
768 	struct dma_descriptor_config *config);
769 enum status_code dma_add_descriptor(struct dma_resource *resource,
770 		DmacDescriptor* descriptor);
771 
772 /** @} */
773 
774 /**
775  * \page asfdoc_sam0_dma_extra Extra Information for DMAC Driver
776  *
777  * \section asfdoc_sam0_dma_extra_acronyms Acronyms
778  * Below is a table listing the acronyms used in this module, along with their
779  * intended meanings.
780  *
781  * <table>
782  *   <tr>
783  *     <th>Acronym</th>
784  *     <th>Description</th>
785  *   </tr>
786  *   <tr>
787  *     <td>DMA</td>
788  *     <td>Direct Memory Access</td>
789  *   </tr>
790  *   <tr>
791  *     <td>DMAC</td>
792  *     <td>Direct Memory Access Controller </td>
793  *   </tr>
794  *   <tr>
795  *     <td>CPU</td>
796  *     <td>Central Processing Unit</td>
797  *   </tr>
798  * </table>
799  *
800  *
801  * \section asfdoc_sam0_dma_extra_dependencies Dependencies
802  * This driver has the following dependencies:
803  *
804  * - \ref asfdoc_sam0_system_clock_group "System Clock Driver"
805  *
806  *
807  * \section asfdoc_sam0_dma_extra_errata Errata
808  * There are no errata related to this driver.
809  *
810  *
811  * \section asfdoc_sam0_dma_extra_history Module History
812  * An overview of the module history is presented in the table below, with
813  * details on the enhancements and fixes made to the module since its first
814  * release. The current version of this corresponds to the newest version in
815  * the table.
816  *
817  * <table>
818  *   <tr>
819  *     <th>Changelog</th>
820  *   </tr>
821  *   <tr>
822  *     <td>Add SAM C21 support</td>
823  *   </tr>
824  *   <tr>
825  *     <td>Add SAM L21 support</td>
826  *   </tr>
827  *   <tr>
828  *     <td>Add SAM R30 support</td>
829  *   </tr>
830  *   <tr>
831  *     <td>Initial Release</td>
832  *   </tr>
833  * </table>
834  */
835 
836  /**
837  * \page asfdoc_sam0_dma_exqsg Examples for DMAC Driver
838  *
839  * This is a list of the available Quick Start Guides (QSGs) and example
840  * applications for \ref asfdoc_sam0_dma_group. QSGs are simple examples with
841  * step-by-step instructions to configure and use this driver in a selection of
842  * use cases. Note that a QSG can be compiled as a standalone application or be
843  * added to the user application.
844  *
845  * - \subpage asfdoc_sam0_dma_basic_use_case
846  *
847  * \note More DMA usage examples are available in peripheral QSGs.
848  * A quick start guide for TC/TCC
849  * shows the usage of DMA event trigger; SERCOM SPI/USART/I<SUP>2</SUP>C has example for
850  * DMA transfer from peripheral to memory or from memory to peripheral;
851  * ADC/DAC shows peripheral to peripheral transfer.
852  *
853  * \page asfdoc_sam0_dma_document_revision_history Document Revision History
854  *
855  * <table>
856  *    <tr>
857  *        <th>Doc. Rev.</th>
858  *        <th>Date</th>
859  *        <th>Comments</th>
860  *    </tr>
861  *    <tr>
862  *        <td>42257C</td>
863  *        <td>12/2015</td>
864  *        <td>Added suppport for SAM L21/L22, SAM C21, SAM D09, SAMR30 and SAM DA1</td>
865  *    </tr>
866  *    <tr>
867  *        <td>42257B</td>
868  *        <td>12/2014</td>
869  *        <td>Added support for SAM R21 and SAM D10/D11</td>
870  *    </tr>
871  *    <tr>
872  *        <td>42257A</td>
873  *        <td>02/2014</td>
874  *        <td>Initial release</td>
875  *    </tr>
876  * </table>
877  */
878 
879 #ifdef __cplusplus
880 }
881 #endif
882 
883 #endif /* DMA_H_INCLUDED */
884