1 /***************************************************************************//**
\r
3 * @brief Direct memory access (DMA) API
\r
5 *******************************************************************************
\r
7 * <b>(C) Copyright 2014 Silicon Labs, http://www.silabs.com</b>
\r
8 *******************************************************************************
\r
10 * Permission is granted to anyone to use this software for any purpose,
\r
11 * including commercial applications, and to alter it and redistribute it
\r
12 * freely, subject to the following restrictions:
\r
14 * 1. The origin of this software must not be misrepresented; you must not
\r
15 * claim that you wrote the original software.
\r
16 * 2. Altered source versions must be plainly marked as such, and must not be
\r
17 * misrepresented as being the original software.
\r
18 * 3. This notice may not be removed or altered from any source distribution.
\r
20 * DISCLAIMER OF WARRANTY/LIMITATION OF REMEDIES: Silicon Labs has no
\r
21 * obligation to support this Software. Silicon Labs is providing the
\r
22 * Software "AS IS", with no express or implied warranties of any kind,
\r
23 * including, but not limited to, any implied warranties of merchantability
\r
24 * or fitness for any particular purpose or warranties against infringement
\r
25 * of any proprietary rights of a third party.
\r
27 * Silicon Labs will not be liable for any consequential, incidental, or
\r
28 * special damages, or any other relief, or for any claim by any third party,
\r
29 * arising from your use of this Software.
\r
31 ******************************************************************************/
\r
35 #ifndef __SILICON_LABS_EM_DMA_H_
\r
36 #define __SILICON_LABS_EM_DMA_H_
\r
38 #include "em_device.h"
\r
39 #if defined( DMA_PRESENT )
\r
42 #include <stdbool.h>
\r
48 /***************************************************************************//**
\r
49 * @addtogroup EM_Library
\r
51 ******************************************************************************/
\r
53 /***************************************************************************//**
\r
56 ******************************************************************************/
\r
58 /*******************************************************************************
\r
59 ******************************** ENUMS ************************************
\r
60 ******************************************************************************/
\r
63 * Amount source/destination address should be incremented for each data
\r
68 dmaDataInc1 = _DMA_CTRL_SRC_INC_BYTE, /**< Increment address 1 byte. */
\r
69 dmaDataInc2 = _DMA_CTRL_SRC_INC_HALFWORD, /**< Increment address 2 bytes. */
\r
70 dmaDataInc4 = _DMA_CTRL_SRC_INC_WORD, /**< Increment address 4 bytes. */
\r
71 dmaDataIncNone = _DMA_CTRL_SRC_INC_NONE /**< Do not increment address. */
\r
72 } DMA_DataInc_TypeDef;
\r
75 /** Data sizes (in number of bytes) to be read/written by DMA transfer. */
\r
78 dmaDataSize1 = _DMA_CTRL_SRC_SIZE_BYTE, /**< 1 byte DMA transfer size. */
\r
79 dmaDataSize2 = _DMA_CTRL_SRC_SIZE_HALFWORD, /**< 2 byte DMA transfer size. */
\r
80 dmaDataSize4 = _DMA_CTRL_SRC_SIZE_WORD /**< 4 byte DMA transfer size. */
\r
81 } DMA_DataSize_TypeDef;
\r
84 /** Type of DMA transfer. */
\r
87 /** Basic DMA cycle. */
\r
88 dmaCycleCtrlBasic = _DMA_CTRL_CYCLE_CTRL_BASIC,
\r
89 /** Auto-request DMA cycle. */
\r
90 dmaCycleCtrlAuto = _DMA_CTRL_CYCLE_CTRL_AUTO,
\r
91 /** Ping-pong DMA cycle. */
\r
92 dmaCycleCtrlPingPong = _DMA_CTRL_CYCLE_CTRL_PINGPONG,
\r
93 /** Memory scatter-gather DMA cycle. */
\r
94 dmaCycleCtrlMemScatterGather = _DMA_CTRL_CYCLE_CTRL_MEM_SCATTER_GATHER,
\r
95 /** Peripheral scatter-gather DMA cycle. */
\r
96 dmaCycleCtrlPerScatterGather = _DMA_CTRL_CYCLE_CTRL_PER_SCATTER_GATHER
\r
97 } DMA_CycleCtrl_TypeDef;
\r
100 /** Number of transfers before controller does new arbitration. */
\r
103 dmaArbitrate1 = _DMA_CTRL_R_POWER_1, /**< Arbitrate after 1 DMA transfer. */
\r
104 dmaArbitrate2 = _DMA_CTRL_R_POWER_2, /**< Arbitrate after 2 DMA transfers. */
\r
105 dmaArbitrate4 = _DMA_CTRL_R_POWER_4, /**< Arbitrate after 4 DMA transfers. */
\r
106 dmaArbitrate8 = _DMA_CTRL_R_POWER_8, /**< Arbitrate after 8 DMA transfers. */
\r
107 dmaArbitrate16 = _DMA_CTRL_R_POWER_16, /**< Arbitrate after 16 DMA transfers. */
\r
108 dmaArbitrate32 = _DMA_CTRL_R_POWER_32, /**< Arbitrate after 32 DMA transfers. */
\r
109 dmaArbitrate64 = _DMA_CTRL_R_POWER_64, /**< Arbitrate after 64 DMA transfers. */
\r
110 dmaArbitrate128 = _DMA_CTRL_R_POWER_128, /**< Arbitrate after 128 DMA transfers. */
\r
111 dmaArbitrate256 = _DMA_CTRL_R_POWER_256, /**< Arbitrate after 256 DMA transfers. */
\r
112 dmaArbitrate512 = _DMA_CTRL_R_POWER_512, /**< Arbitrate after 512 DMA transfers. */
\r
113 dmaArbitrate1024 = _DMA_CTRL_R_POWER_1024 /**< Arbitrate after 1024 DMA transfers. */
\r
114 } DMA_ArbiterConfig_TypeDef;
\r
117 /*******************************************************************************
\r
118 ******************************* STRUCTS ***********************************
\r
119 ******************************************************************************/
\r
123 * DMA interrupt callback function pointer.
\r
126 * @li channel - The DMA channel the callback function is invoked for.
\r
127 * @li primary - Indicates if callback is invoked for completion of primary
\r
128 * (true) or alternate (false) descriptor. This is mainly useful for
\r
129 * ping-pong DMA cycles, in order to know which descriptor to refresh.
\r
130 * @li user - User definable reference that may be used to pass information
\r
131 * to be used by the callback handler. If used, the referenced data must be
\r
132 * valid at the point when the interrupt handler invokes the callback.
\r
133 * If callback changes any data in the provided user structure, remember
\r
134 * that those changes are done in interrupt context, and proper protection
\r
135 * of data may be required.
\r
137 typedef void (*DMA_FuncPtr_TypeDef)(unsigned int channel, bool primary, void *user);
\r
142 * Callback structure that can be used to define DMA complete actions.
\r
144 * A reference to this structure is only stored in the primary descriptor
\r
145 * for a channel (if callback feature is used). If callback is required
\r
146 * for both primary and alternate descriptor completion, this must be
\r
147 * handled by one common callback, using the provided 'primary' parameter
\r
148 * with the callback function.
\r
153 * Pointer to callback function to invoke when DMA transfer cycle done.
\r
154 * Notice that this function is invoked in interrupt context, and therefore
\r
155 * should be short and non-blocking.
\r
157 DMA_FuncPtr_TypeDef cbFunc;
\r
159 /** User defined pointer to provide with callback function. */
\r
163 * For internal use only: Indicates if next callback applies to primary
\r
164 * or alternate descriptor completion. Mainly useful for ping-pong DMA
\r
165 * cycles. Set this value to 0 prior to configuring callback handling.
\r
171 /** Configuration structure for a channel. */
\r
175 * Select if channel priority is in the high or default priority group
\r
176 * with respect to arbitration. Within a priority group, lower numbered
\r
177 * channels have higher priority than higher numbered channels.
\r
182 * Select if interrupt shall be enabled for channel (triggering interrupt
\r
183 * handler when dma_done signal is asserted). It should normally be
\r
184 * enabled if using the callback feature for a channel, and disabled if
\r
185 * not using the callback feature.
\r
190 * Channel control specifying the source of DMA signals. If accessing
\r
191 * peripherals, use one of the DMAREQ_nnn defines available for the
\r
192 * peripheral. Set it to 0 for memory-to-memory DMA cycles.
\r
198 * User definable callback handling configuration.
\r
200 * Please refer to structure definition for details. The callback
\r
201 * is invoked when the specified DMA cycle is complete (when dma_done
\r
202 * signal asserted). The callback is invoked in interrupt context,
\r
203 * and should be efficient and non-blocking. Set to NULL to not
\r
204 * use the callback feature.
\r
206 * The referenced structure is used by the interrupt handler, and must
\r
207 * be available until no longer used. Thus, in most cases it should
\r
208 * not be located on the stack.
\r
210 DMA_CB_TypeDef *cb;
\r
211 } DMA_CfgChannel_TypeDef;
\r
215 * Configuration structure for primary or alternate descriptor
\r
216 * (not used for scatter-gather DMA cycles).
\r
220 /** Destination increment size for each DMA transfer */
\r
221 DMA_DataInc_TypeDef dstInc;
\r
223 /** Source increment size for each DMA transfer */
\r
224 DMA_DataInc_TypeDef srcInc;
\r
226 /** DMA transfer unit size. */
\r
227 DMA_DataSize_TypeDef size;
\r
230 * Arbitration rate, ie number of DMA transfers done before rearbitration
\r
233 DMA_ArbiterConfig_TypeDef arbRate;
\r
236 * HPROT signal state, please refer to reference manual, DMA chapter for
\r
237 * further details. Normally set to 0 if protection is not an issue.
\r
238 * The following bits are available:
\r
239 * @li bit 0 - HPROT[1] control for source read accesses,
\r
240 * privileged/non-privileged access
\r
241 * @li bit 3 - HPROT[1] control for destination write accesses,
\r
242 * privileged/non-privileged access
\r
245 } DMA_CfgDescr_TypeDef;
\r
248 #if defined( _DMA_LOOP0_MASK ) && defined( _DMA_LOOP1_MASK )
\r
250 * Configuration structure for loop mode
\r
254 /** Enable repeated loop */
\r
256 /** Width of transfer, reload value for nMinus1 */
\r
258 } DMA_CfgLoop_TypeDef;
\r
262 #if defined( _DMA_RECT0_MASK )
\r
264 * Configuration structure for rectangular copy
\r
268 /** DMA channel destination stride (width of destination image, distance between lines) */
\r
269 uint16_t dstStride;
\r
270 /** DMA channel source stride (width of source image, distance between lines) */
\r
271 uint16_t srcStride;
\r
272 /** 2D copy height */
\r
274 } DMA_CfgRect_TypeDef;
\r
278 /** Configuration structure for alternate scatter-gather descriptor. */
\r
281 /** Pointer to location to transfer data from. */
\r
284 /** Pointer to location to transfer data to. */
\r
287 /** Destination increment size for each DMA transfer */
\r
288 DMA_DataInc_TypeDef dstInc;
\r
290 /** Source increment size for each DMA transfer */
\r
291 DMA_DataInc_TypeDef srcInc;
\r
293 /** DMA transfer unit size. */
\r
294 DMA_DataSize_TypeDef size;
\r
297 * Arbitration rate, ie number of DMA transfers done before rearbitration
\r
300 DMA_ArbiterConfig_TypeDef arbRate;
\r
302 /** Number of DMA transfers minus 1 to do. Must be <= 1023. */
\r
306 * HPROT signal state, please refer to reference manual, DMA chapter for
\r
307 * further details. Normally set to 0 if protection is not an issue.
\r
308 * The following bits are available:
\r
309 * @li bit 0 - HPROT[1] control for source read accesses,
\r
310 * privileged/non-privileged access
\r
311 * @li bit 3 - HPROT[1] control for destination write accesses,
\r
312 * privileged/non-privileged access
\r
316 /** Specify if a memory or peripheral scatter-gather DMA cycle. Notice
\r
317 * that this parameter should be the same for all alternate
\r
319 * @li true - this is a peripheral scatter-gather cycle
\r
320 * @li false - this is a memory scatter-gather cycle
\r
323 } DMA_CfgDescrSGAlt_TypeDef;
\r
326 /** DMA init structure */
\r
330 * HPROT signal state when accessing the primary/alternate
\r
331 * descriptors. Normally set to 0 if protection is not an issue.
\r
332 * The following bits are available:
\r
333 * @li bit 0 - HPROT[1] control for descriptor accesses (ie when
\r
334 * the DMA controller accesses the channel control block itself),
\r
335 * privileged/non-privileged access
\r
340 * Pointer to the controlblock in memory holding descriptors (channel
\r
341 * control data structures). This memory must be properly aligned
\r
342 * at a 256 bytes. I.e. the 8 least significant bits must be zero.
\r
344 * Please refer to the reference manual, DMA chapter for more details.
\r
346 * It is possible to provide a smaller memory block, only covering
\r
347 * those channels actually used, if not all available channels are used.
\r
348 * Ie, if only using 4 channels (0-3), both primary and alternate
\r
349 * structures, then only 16*2*4 = 128 bytes must be provided. This
\r
350 * implementation has however no check if later exceeding such a limit
\r
351 * by configuring for instance channel 4, in which case memory overwrite
\r
352 * of some other data will occur.
\r
354 DMA_DESCRIPTOR_TypeDef *controlBlock;
\r
355 } DMA_Init_TypeDef;
\r
358 /*******************************************************************************
\r
359 ***************************** PROTOTYPES **********************************
\r
360 ******************************************************************************/
\r
362 void DMA_ActivateAuto(unsigned int channel,
\r
366 unsigned int nMinus1);
\r
367 void DMA_ActivateBasic(unsigned int channel,
\r
372 unsigned int nMinus1);
\r
373 void DMA_ActivatePingPong(unsigned int channel,
\r
377 unsigned int primNMinus1,
\r
380 unsigned int altNMinus1);
\r
381 void DMA_ActivateScatterGather(unsigned int channel,
\r
383 DMA_DESCRIPTOR_TypeDef *altDescr,
\r
384 unsigned int count);
\r
385 void DMA_CfgChannel(unsigned int channel, DMA_CfgChannel_TypeDef *cfg);
\r
386 void DMA_CfgDescr(unsigned int channel,
\r
388 DMA_CfgDescr_TypeDef *cfg);
\r
389 #if defined( _DMA_LOOP0_MASK ) && defined( _DMA_LOOP1_MASK )
\r
390 void DMA_CfgLoop(unsigned int channel, DMA_CfgLoop_TypeDef *cfg);
\r
393 #if defined( _DMA_RECT0_MASK )
\r
394 void DMA_CfgRect(unsigned int channel, DMA_CfgRect_TypeDef *cfg);
\r
397 #if defined( _DMA_LOOP0_MASK ) && defined( _DMA_LOOP1_MASK )
\r
398 /***************************************************************************//**
\r
400 * Clear Loop configuration for channel
\r
402 * @param[in] channel
\r
403 * Channel to reset loop configuration for
\r
404 ******************************************************************************/
\r
405 __STATIC_INLINE void DMA_ResetLoop(unsigned int channel)
\r
407 /* Clean loop copy operation */
\r
411 DMA->LOOP0 = _DMA_LOOP0_RESETVALUE;
\r
414 DMA->LOOP1 = _DMA_LOOP1_RESETVALUE;
\r
423 #if defined( _DMA_RECT0_MASK )
\r
424 /***************************************************************************//**
\r
426 * Clear Rect/2D DMA configuration for channel
\r
428 * @param[in] channel
\r
429 * Channel to reset loop configuration for
\r
430 ******************************************************************************/
\r
431 __STATIC_INLINE void DMA_ResetRect(unsigned int channel)
\r
435 /* Clear rect copy operation */
\r
436 DMA->RECT0 = _DMA_RECT0_RESETVALUE;
\r
439 void DMA_CfgDescrScatterGather(DMA_DESCRIPTOR_TypeDef *descr,
\r
441 DMA_CfgDescrSGAlt_TypeDef *cfg);
\r
442 void DMA_ChannelEnable(unsigned int channel, bool enable);
\r
443 bool DMA_ChannelEnabled(unsigned int channel);
\r
444 void DMA_Init(DMA_Init_TypeDef *init);
\r
445 void DMA_IRQHandler(void);
\r
446 void DMA_RefreshPingPong(unsigned int channel,
\r
451 unsigned int nMinus1,
\r
453 void DMA_Reset(void);
\r
455 /** @} (end addtogroup DMA) */
\r
456 /** @} (end addtogroup EM_Library) */
\r
462 #endif /* defined( DMA_PRESENT ) */
\r
463 #endif /* __SILICON_LABS_EM_DMA_H_ */
\r