em_dma.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /***************************************************************************//**
  2. * @file
  3. * @brief Direct memory access (DMA) API
  4. * @author Energy Micro AS
  5. * @version 3.0.0
  6. *******************************************************************************
  7. * @section License
  8. * <b>(C) Copyright 2012 Energy Micro AS, http://www.energymicro.com</b>
  9. *******************************************************************************
  10. *
  11. * Permission is granted to anyone to use this software for any purpose,
  12. * including commercial applications, and to alter it and redistribute it
  13. * freely, subject to the following restrictions:
  14. *
  15. * 1. The origin of this software must not be misrepresented; you must not
  16. * claim that you wrote the original software.
  17. * 2. Altered source versions must be plainly marked as such, and must not be
  18. * misrepresented as being the original software.
  19. * 3. This notice may not be removed or altered from any source distribution.
  20. *
  21. * DISCLAIMER OF WARRANTY/LIMITATION OF REMEDIES: Energy Micro AS has no
  22. * obligation to support this Software. Energy Micro AS is providing the
  23. * Software "AS IS", with no express or implied warranties of any kind,
  24. * including, but not limited to, any implied warranties of merchantability
  25. * or fitness for any particular purpose or warranties against infringement
  26. * of any proprietary rights of a third party.
  27. *
  28. * Energy Micro AS will not be liable for any consequential, incidental, or
  29. * special damages, or any other relief, or for any claim by any third party,
  30. * arising from your use of this Software.
  31. *
  32. ******************************************************************************/
  33. #ifndef __EM_DMA_H
  34. #define __EM_DMA_H
  35. #include <stdio.h>
  36. #include <stdbool.h>
  37. #include "em_part.h"
  38. #ifdef __cplusplus
  39. extern "C" {
  40. #endif
  41. /***************************************************************************//**
  42. * @addtogroup EM_Library
  43. * @{
  44. ******************************************************************************/
  45. /***************************************************************************//**
  46. * @addtogroup DMA
  47. * @{
  48. ******************************************************************************/
  49. /*******************************************************************************
  50. ******************************** ENUMS ************************************
  51. ******************************************************************************/
  52. /**
  53. * Amount source/destination address should be incremented for each data
  54. * transfer.
  55. */
  56. typedef enum
  57. {
  58. dmaDataInc1 = _DMA_CTRL_SRC_INC_BYTE, /**< Increment address 1 byte. */
  59. dmaDataInc2 = _DMA_CTRL_SRC_INC_HALFWORD, /**< Increment address 2 bytes. */
  60. dmaDataInc4 = _DMA_CTRL_SRC_INC_WORD, /**< Increment address 4 bytes. */
  61. dmaDataIncNone = _DMA_CTRL_SRC_INC_NONE /**< Do not increment address. */
  62. } DMA_DataInc_TypeDef;
  63. /** Data sizes (in number of bytes) to be read/written by DMA transfer. */
  64. typedef enum
  65. {
  66. dmaDataSize1 = _DMA_CTRL_SRC_SIZE_BYTE, /**< 1 byte DMA transfer size. */
  67. dmaDataSize2 = _DMA_CTRL_SRC_SIZE_HALFWORD, /**< 2 byte DMA transfer size. */
  68. dmaDataSize4 = _DMA_CTRL_SRC_SIZE_WORD /**< 4 byte DMA transfer size. */
  69. } DMA_DataSize_TypeDef;
  70. /** Type of DMA transfer. */
  71. typedef enum
  72. {
  73. /** Basic DMA cycle. */
  74. dmaCycleCtrlBasic = _DMA_CTRL_CYCLE_CTRL_BASIC,
  75. /** Auto-request DMA cycle. */
  76. dmaCycleCtrlAuto = _DMA_CTRL_CYCLE_CTRL_AUTO,
  77. /** Ping-pong DMA cycle. */
  78. dmaCycleCtrlPingPong = _DMA_CTRL_CYCLE_CTRL_PINGPONG,
  79. /** Memory scatter-gather DMA cycle. */
  80. dmaCycleCtrlMemScatterGather = _DMA_CTRL_CYCLE_CTRL_MEM_SCATTER_GATHER,
  81. /** Peripheral scatter-gather DMA cycle. */
  82. dmaCycleCtrlPerScatterGather = _DMA_CTRL_CYCLE_CTRL_PER_SCATTER_GATHER
  83. } DMA_CycleCtrl_TypeDef;
  84. /** Number of transfers before controller does new arbitration. */
  85. typedef enum
  86. {
  87. dmaArbitrate1 = _DMA_CTRL_R_POWER_1, /**< Arbitrate after 1 DMA transfer. */
  88. dmaArbitrate2 = _DMA_CTRL_R_POWER_2, /**< Arbitrate after 2 DMA transfers. */
  89. dmaArbitrate4 = _DMA_CTRL_R_POWER_4, /**< Arbitrate after 4 DMA transfers. */
  90. dmaArbitrate8 = _DMA_CTRL_R_POWER_8, /**< Arbitrate after 8 DMA transfers. */
  91. dmaArbitrate16 = _DMA_CTRL_R_POWER_16, /**< Arbitrate after 16 DMA transfers. */
  92. dmaArbitrate32 = _DMA_CTRL_R_POWER_32, /**< Arbitrate after 32 DMA transfers. */
  93. dmaArbitrate64 = _DMA_CTRL_R_POWER_64, /**< Arbitrate after 64 DMA transfers. */
  94. dmaArbitrate128 = _DMA_CTRL_R_POWER_128, /**< Arbitrate after 128 DMA transfers. */
  95. dmaArbitrate256 = _DMA_CTRL_R_POWER_256, /**< Arbitrate after 256 DMA transfers. */
  96. dmaArbitrate512 = _DMA_CTRL_R_POWER_512, /**< Arbitrate after 512 DMA transfers. */
  97. dmaArbitrate1024 = _DMA_CTRL_R_POWER_1024 /**< Arbitrate after 1024 DMA transfers. */
  98. } DMA_ArbiterConfig_TypeDef;
  99. /*******************************************************************************
  100. ******************************* STRUCTS ***********************************
  101. ******************************************************************************/
  102. /**
  103. * @brief
  104. * DMA interrupt callback function pointer.
  105. * @details
  106. * Parameters:
  107. * @li channel - The DMA channel the callback function is invoked for.
  108. * @li primary - Indicates if callback is invoked for completion of primary
  109. * (true) or alternate (false) descriptor. This is mainly useful for
  110. * ping-pong DMA cycles, in order to know which descriptor to refresh.
  111. * @li user - User definable reference that may be used to pass information
  112. * to be used by the callback handler. If used, the referenced data must be
  113. * valid at the point when the interrupt handler invokes the callback.
  114. * If callback changes any data in the provided user structure, remember
  115. * that those changes are done in interrupt context, and proper protection
  116. * of data may be required.
  117. */
  118. typedef void (*DMA_FuncPtr_TypeDef)(unsigned int channel, bool primary, void *user);
  119. /**
  120. * @brief
  121. * Callback structure that can be used to define DMA complete actions.
  122. * @details
  123. * A reference to this structure is only stored in the primary descriptor
  124. * for a channel (if callback feature is used). If callback is required
  125. * for both primary and alternate descriptor completion, this must be
  126. * handled by one common callback, using the provided 'primary' parameter
  127. * with the callback function.
  128. */
  129. typedef struct
  130. {
  131. /**
  132. * Pointer to callback function to invoke when DMA transfer cycle done.
  133. * Notice that this function is invoked in interrupt context, and therefore
  134. * should be short and non-blocking.
  135. */
  136. DMA_FuncPtr_TypeDef cbFunc;
  137. /** User defined pointer to provide with callback function. */
  138. void *userPtr;
  139. /**
  140. * For internal use only: Indicates if next callback applies to primary
  141. * or alternate descriptor completion. Mainly useful for ping-pong DMA
  142. * cycles. Set this value to 0 prior to configuring callback handling.
  143. */
  144. uint8_t primary;
  145. } DMA_CB_TypeDef;
  146. /** Configuration structure for a channel. */
  147. typedef struct
  148. {
  149. /**
  150. * Select if channel priority is in the high or default priority group
  151. * with respect to arbitration. Within a priority group, lower numbered
  152. * channels have higher priority than higher numbered channels.
  153. */
  154. bool highPri;
  155. /**
  156. * Select if interrupt shall be enabled for channel (triggering interrupt
  157. * handler when dma_done signal is asserted). It should normally be
  158. * enabled if using the callback feature for a channel, and disabled if
  159. * not using the callback feature.
  160. */
  161. bool enableInt;
  162. /**
  163. * Channel control specifying the source of DMA signals. If accessing
  164. * peripherals, use one of the DMAREQ_nnn defines available for the
  165. * peripheral. Set it to 0 for memory-to-memory DMA cycles.
  166. */
  167. uint32_t select;
  168. /**
  169. * @brief
  170. * User definable callback handling configuration.
  171. * @details
  172. * Please refer to structure definition for details. The callback
  173. * is invoked when the specified DMA cycle is complete (when dma_done
  174. * signal asserted). The callback is invoked in interrupt context,
  175. * and should be efficient and non-blocking. Set to NULL to not
  176. * use the callback feature.
  177. * @note
  178. * The referenced structure is used by the interrupt handler, and must
  179. * be available until no longer used. Thus, in most cases it should
  180. * not be located on the stack.
  181. */
  182. DMA_CB_TypeDef *cb;
  183. } DMA_CfgChannel_TypeDef;
  184. /**
  185. * Configuration structure for primary or alternate descriptor
  186. * (not used for scatter-gather DMA cycles).
  187. */
  188. typedef struct
  189. {
  190. /** Destination increment size for each DMA transfer */
  191. DMA_DataInc_TypeDef dstInc;
  192. /** Source increment size for each DMA transfer */
  193. DMA_DataInc_TypeDef srcInc;
  194. /** DMA transfer unit size. */
  195. DMA_DataSize_TypeDef size;
  196. /**
  197. * Arbitration rate, ie number of DMA transfers done before rearbitration
  198. * takes place.
  199. */
  200. DMA_ArbiterConfig_TypeDef arbRate;
  201. /**
  202. * HPROT signal state, please refer to reference manual, DMA chapter for
  203. * further details. Normally set to 0 if protection is not an issue.
  204. * The following bits are available:
  205. * @li bit 0 - HPROT[1] control for source read accesses,
  206. * privileged/non-privileged access
  207. * @li bit 3 - HPROT[1] control for destination write accesses,
  208. * privileged/non-privileged access
  209. */
  210. uint8_t hprot;
  211. } DMA_CfgDescr_TypeDef;
  212. #if defined(_EFM32_GIANT_FAMILY)
  213. /**
  214. * Configuration structure for loop mode
  215. */
  216. typedef struct
  217. {
  218. /** Enable repeated loop */
  219. bool enable;
  220. /** Width of transfer, reload value for nMinus1 */
  221. uint16_t nMinus1;
  222. } DMA_CfgLoop_TypeDef;
  223. /**
  224. * Configuration structure for rectangular copy
  225. */
  226. typedef struct
  227. {
  228. /** DMA channel destination stride (width of destination image, distance between lines) */
  229. uint16_t dstStride;
  230. /** DMA channel source stride (width of source image, distance between lines) */
  231. uint16_t srcStride;
  232. /** 2D copy height */
  233. uint16_t height;
  234. } DMA_CfgRect_TypeDef;
  235. #endif
  236. /** Configuration structure for alternate scatter-gather descriptor. */
  237. typedef struct
  238. {
  239. /** Pointer to location to transfer data from. */
  240. void *src;
  241. /** Pointer to location to transfer data to. */
  242. void *dst;
  243. /** Destination increment size for each DMA transfer */
  244. DMA_DataInc_TypeDef dstInc;
  245. /** Source increment size for each DMA transfer */
  246. DMA_DataInc_TypeDef srcInc;
  247. /** DMA transfer unit size. */
  248. DMA_DataSize_TypeDef size;
  249. /**
  250. * Arbitration rate, ie number of DMA transfers done before rearbitration
  251. * takes place.
  252. */
  253. DMA_ArbiterConfig_TypeDef arbRate;
  254. /** Number of DMA transfers minus 1 to do. Must be <= 1023. */
  255. uint16_t nMinus1;
  256. /**
  257. * HPROT signal state, please refer to reference manual, DMA chapter for
  258. * further details. Normally set to 0 if protection is not an issue.
  259. * The following bits are available:
  260. * @li bit 0 - HPROT[1] control for source read accesses,
  261. * privileged/non-privileged access
  262. * @li bit 3 - HPROT[1] control for destination write accesses,
  263. * privileged/non-privileged access
  264. */
  265. uint8_t hprot;
  266. /** Specify if a memory or peripheral scatter-gather DMA cycle. Notice
  267. * that this parameter should be the same for all alternate
  268. * descriptors.
  269. * @li true - this is a peripheral scatter-gather cycle
  270. * @li false - this is a memory scatter-gather cycle
  271. */
  272. bool peripheral;
  273. } DMA_CfgDescrSGAlt_TypeDef;
  274. /** DMA init structure */
  275. typedef struct
  276. {
  277. /**
  278. * HPROT signal state when accessing the primary/alternate
  279. * descriptors. Normally set to 0 if protection is not an issue.
  280. * The following bits are available:
  281. * @li bit 0 - HPROT[1] control for descriptor accesses (ie when
  282. * the DMA controller accesses the channel control block itself),
  283. * privileged/non-privileged access
  284. */
  285. uint8_t hprot;
  286. /**
  287. * Pointer to the controlblock in memory holding descriptors (channel
  288. * control data structures). This memory must be properly aligned
  289. * according to requirements.
  290. *
  291. * Alignment requirements are
  292. * a) 5 bits base requirement, bits [4:0]
  293. * b) Add the number of bits needed to represent the wanted number
  294. * of channels
  295. * c) Align structure with this number of bits set to zero
  296. *
  297. * Examples: 4 channels, 5 + 2 (channels 0 to 3) = 7 bits
  298. * 7 bit alignment, 64 byte address alignment
  299. * 8 channels, 5 + 3 (channels 0 to 7) = 8 bits
  300. * 8 bit alignment, 256 byte address alignment
  301. * 12 channels, 5 + 4 (channels 0 to 11) = 9 bits
  302. * 9 bit alignment, 512 byte address alignment
  303. *
  304. * Please refer to the reference manual, DMA chapter for more details.
  305. *
  306. * It is possible to provide a smaller memory block, only covering
  307. * those channels actually used, if not all available channels are used.
  308. * Ie, if only using 4 channels (0-3), both primary and alternate
  309. * structures, then only 16*2*4 = 128 bytes must be provided. This
  310. * implementation has however no check if later exceeding such a limit
  311. * by configuring for instance channel 4, in which case memory overwrite
  312. * of some other data will occur.
  313. */
  314. DMA_DESCRIPTOR_TypeDef *controlBlock;
  315. } DMA_Init_TypeDef;
  316. /*******************************************************************************
  317. ***************************** PROTOTYPES **********************************
  318. ******************************************************************************/
  319. void DMA_ActivateAuto(unsigned int channel,
  320. bool primary,
  321. void *dst,
  322. void *src,
  323. unsigned int nMinus1);
  324. void DMA_ActivateBasic(unsigned int channel,
  325. bool primary,
  326. bool useBurst,
  327. void *dst,
  328. void *src,
  329. unsigned int nMinus1);
  330. void DMA_ActivatePingPong(unsigned int channel,
  331. bool useBurst,
  332. void *primDst,
  333. void *primSrc,
  334. unsigned int primNMinus1,
  335. void *altDst,
  336. void *altSrc,
  337. unsigned int altNMinus1);
  338. void DMA_ActivateScatterGather(unsigned int channel,
  339. bool useBurst,
  340. DMA_DESCRIPTOR_TypeDef *altDescr,
  341. unsigned int count);
  342. void DMA_CfgChannel(unsigned int channel, DMA_CfgChannel_TypeDef *cfg);
  343. void DMA_CfgDescr(unsigned int channel,
  344. bool primary,
  345. DMA_CfgDescr_TypeDef *cfg);
  346. #if defined(_EFM32_GIANT_FAMILY)
  347. void DMA_CfgLoop(unsigned int channel, DMA_CfgLoop_TypeDef *cfg);
  348. void DMA_CfgRect(unsigned int channel, DMA_CfgRect_TypeDef *cfg);
  349. /***************************************************************************//**
  350. * @brief
  351. * Clear Loop configuration for channel
  352. *
  353. * @param[in] channel
  354. * Channel to reset loop configuration for
  355. ******************************************************************************/
  356. __STATIC_INLINE void DMA_ResetLoop(unsigned int channel)
  357. {
  358. /* Clean loop copy operation */
  359. switch(channel)
  360. {
  361. case 0:
  362. DMA->LOOP0 = _DMA_LOOP0_RESETVALUE;
  363. break;
  364. case 1:
  365. DMA->LOOP1 = _DMA_LOOP1_RESETVALUE;
  366. break;
  367. default:
  368. break;
  369. }
  370. }
  371. /***************************************************************************//**
  372. * @brief
  373. * Clear Rect/2D DMA configuration for channel
  374. *
  375. * @param[in] channel
  376. * Channel to reset loop configuration for
  377. ******************************************************************************/
  378. __STATIC_INLINE void DMA_ResetRect(unsigned int channel)
  379. {
  380. (void) channel;
  381. /* Clear rect copy operation */
  382. DMA->RECT0 = _DMA_RECT0_RESETVALUE;
  383. }
  384. #endif
  385. void DMA_CfgDescrScatterGather(DMA_DESCRIPTOR_TypeDef *descr,
  386. unsigned int indx,
  387. DMA_CfgDescrSGAlt_TypeDef *cfg);
  388. bool DMA_ChannelEnabled(unsigned int channel);
  389. void DMA_Init(DMA_Init_TypeDef *init);
  390. void DMA_IRQHandler(void);
  391. void DMA_RefreshPingPong(unsigned int channel,
  392. bool primary,
  393. bool useBurst,
  394. void *dst,
  395. void *src,
  396. unsigned int nMinus1,
  397. bool last);
  398. void DMA_Reset(void);
  399. /** @} (end addtogroup DMA) */
  400. /** @} (end addtogroup EM_Library) */
  401. #ifdef __cplusplus
  402. }
  403. #endif
  404. #endif /* __EM_DMA_H */