dma.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /**
  2. * @file
  3. * @brief Direct Memory Access (DMA) driver function prototypes and data types.
  4. */
  5. /* ****************************************************************************
  6. * Copyright (C) 2017 Maxim Integrated Products, Inc., All Rights Reserved.
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a
  9. * copy of this software and associated documentation files (the "Software"),
  10. * to deal in the Software without restriction, including without limitation
  11. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  12. * and/or sell copies of the Software, and to permit persons to whom the
  13. * Software is furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included
  16. * in all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  19. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  21. * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
  22. * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  23. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  24. * OTHER DEALINGS IN THE SOFTWARE.
  25. *
  26. * Except as contained in this notice, the name of Maxim Integrated
  27. * Products, Inc. shall not be used except as stated in the Maxim Integrated
  28. * Products, Inc. Branding Policy.
  29. *
  30. * The mere transfer of this software does not imply any licenses
  31. * of trade secrets, proprietary technology, copyrights, patents,
  32. * trademarks, maskwork rights, or any other form of intellectual
  33. * property whatsoever. Maxim Integrated Products, Inc. retains all
  34. * ownership rights.
  35. *
  36. * $Date: 2019-07-01 11:06:19 -0500 (Mon, 01 Jul 2019) $
  37. * $Revision: 44383 $
  38. *
  39. *************************************************************************** */
  40. #ifndef _DMA_H_
  41. #define _DMA_H_
  42. /* **** Includes **** */
  43. #include "mxc_config.h"
  44. #include "dma_regs.h"
  45. #ifdef __cplusplus
  46. extern "C" {
  47. #endif
  48. /**
  49. * @defgroup dma Direct Memory Access (DMA)
  50. * @ingroup periphlibs
  51. * @{
  52. */
  53. /* **** Definitions **** */
  54. /**
  55. * Enumeration for the DMA Channel's priority level.
  56. */
  57. typedef enum {
  58. DMA_PRIO_HIGH = MXC_S_DMA_CFG_PRI_HIGH, /**< High Priority */
  59. DMA_PRIO_MEDHIGH = MXC_S_DMA_CFG_PRI_MEDHIGH, /**< Medium High Priority */
  60. DMA_PRIO_MEDLOW = MXC_S_DMA_CFG_PRI_MEDLOW, /**< Medium Low Priority */
  61. DMA_PRIO_LOW = MXC_S_DMA_CFG_PRI_LOW, /**< Low Priority */
  62. } dma_priority_t;
  63. /** @brief DMA request select */
  64. typedef enum {
  65. DMA_REQSEL_MEMTOMEM = MXC_S_DMA_CFG_REQSEL_MEMTOMEM, /**< Memory to Memory DMA Request Selection */
  66. DMA_REQSEL_SPI0RX = MXC_S_DMA_CFG_REQSEL_SPI0RX, /**< SPI0 Receive DMA Request Selection */
  67. DMA_REQSEL_SPI1RX = MXC_S_DMA_CFG_REQSEL_SPI1RX, /**< SPI1 Receive DMA Request Selection */
  68. DMA_REQSEL_UART0RX = MXC_S_DMA_CFG_REQSEL_UART0RX, /**< UART0 Receive DMA Request Selection */
  69. DMA_REQSEL_UART1RX = MXC_S_DMA_CFG_REQSEL_UART1RX, /**< UART1 Receive DMA Request Selection */
  70. DMA_REQSEL_I2C0RX = MXC_S_DMA_CFG_REQSEL_I2C0RX, /**< I2C0 Receive DMA Request Selection */
  71. DMA_REQSEL_I2C1RX = MXC_S_DMA_CFG_REQSEL_I2C1RX, /**< I2C1 Receive DMA Request Selection */
  72. DMA_REQSEL_SPI0TX = MXC_S_DMA_CFG_REQSEL_SPI0TX, /**< SPI0 Transmit DMA Request Selection */
  73. DMA_REQSEL_SPI1TX = MXC_S_DMA_CFG_REQSEL_SPI1TX, /**< SPI1 Transmit DMA Request Selection */
  74. DMA_REQSEL_UART0TX = MXC_S_DMA_CFG_REQSEL_UART0TX, /**< UART0 Transmit DMA Request Selection */
  75. DMA_REQSEL_UART1TX = MXC_S_DMA_CFG_REQSEL_UART1TX, /**< UART1 Transmit DMA Request Selection */
  76. DMA_REQSEL_I2C0TX = MXC_S_DMA_CFG_REQSEL_I2C0TX, /**< I2C0 Transmit DMA Request Selection */
  77. DMA_REQSEL_I2C1TX = MXC_S_DMA_CFG_REQSEL_I2C1TX, /**< I2C1 Transmit DMA Request Selection */
  78. } dma_reqsel_t;
  79. /** @brief Enumeration for the DMA prescaler */
  80. typedef enum {
  81. DMA_PRESCALE_DISABLE = MXC_S_DMA_CFG_PSSEL_DIS, /**< Prescaler disabled */
  82. DMA_PRESCALE_DIV256 = MXC_S_DMA_CFG_PSSEL_DIV256, /**< Divide by 256 */
  83. DMA_PRESCALE_DIV64K = MXC_S_DMA_CFG_PSSEL_DIV64K, /**< Divide by 65,536 */
  84. DMA_PRESCALE_DIV16M = MXC_S_DMA_CFG_PSSEL_DIV16M, /**< Divide by 16,777,216 */
  85. } dma_prescale_t;
  86. /** @brief Enumeration for the DMA timeout value */
  87. typedef enum {
  88. DMA_TIMEOUT_4_CLK = MXC_S_DMA_CFG_TOSEL_TO4, /**< DMA timeout of 4 clocks */
  89. DMA_TIMEOUT_8_CLK = MXC_S_DMA_CFG_TOSEL_TO8, /**< DMA timeout of 8 clocks */
  90. DMA_TIMEOUT_16_CLK = MXC_S_DMA_CFG_TOSEL_TO16, /**< DMA timeout of 16 clocks */
  91. DMA_TIMEOUT_32_CLK = MXC_S_DMA_CFG_TOSEL_TO32, /**< DMA timeout of 32 clocks */
  92. DMA_TIMEOUT_64_CLK = MXC_S_DMA_CFG_TOSEL_TO64, /**< DMA timeout of 64 clocks */
  93. DMA_TIMEOUT_128_CLK = MXC_S_DMA_CFG_TOSEL_TO128, /**< DMA timeout of 128 clocks */
  94. DMA_TIMEOUT_256_CLK = MXC_S_DMA_CFG_TOSEL_TO256, /**< DMA timeout of 256 clocks */
  95. DMA_TIMEOUT_512_CLK = MXC_S_DMA_CFG_TOSEL_TO512, /**< DMA timeout of 512 clocks */
  96. } dma_timeout_t;
  97. /** @brief DMA transfer data width */
  98. typedef enum {
  99. /* Using the '_V_' define instead of the '_S_' since these same values will be used to
  100. specify the DSTWD also. The API functions will shift the value the correct amount
  101. prior to writing the cfg register. */
  102. DMA_WIDTH_BYTE = MXC_V_DMA_CFG_SRCWD_BYTE, /**< DMA transfer in bytes */
  103. DMA_WIDTH_HALFWORD = MXC_V_DMA_CFG_SRCWD_HALFWORD, /**< DMA transfer in 16-bit half-words */
  104. DMA_WIDTH_WORD = MXC_V_DMA_CFG_SRCWD_WORD, /**< DMA transfer in 32-bit words */
  105. } dma_width_t;
  106. /** @brief Convenience defines for options */
  107. #define DMA_FALSE 0 /**< Define for passing 0 to DMA functions */
  108. #define DMA_TRUE 1 /**< Define for passing 1 to DMA functions */
  109. /* **** Function Prototypes **** */
  110. /**
  111. * @brief Initialize DMA resources
  112. * @details This initialization is required before using the DMA driver functions.
  113. * @return #E_NO_ERROR if successful
  114. */
  115. int DMA_Init(void);
  116. /**
  117. * @brief Request DMA channel
  118. * @details Returns a handle to the first free DMA channel, which can be used via API calls
  119. * or direct access to channel registers using the DMA_GetCHRegs(int ch) function.
  120. * @return Non-negative channel handle (inclusive of zero).
  121. * @return #E_NONE_AVAIL All channels in use.
  122. * @return #E_BAD_STATE DMA is not initialized, call DMA_Init() first.
  123. * @return #E_BUSY DMA is currently busy (locked), try again later.
  124. */
  125. int DMA_AcquireChannel(void);
  126. /**
  127. * @brief Release DMA channel
  128. * @details Stops any DMA operation on the channel and returns it to the pool of free channels.
  129. *
  130. * @param ch channel handle to release
  131. *
  132. * @return #E_BAD_PARAM if an unused or invalid channel handle, #E_NO_ERROR otherwise
  133. */
  134. int DMA_ReleaseChannel(int ch);
  135. /**
  136. * @brief Configure the DMA channel
  137. * @details Configures the channel, which was previously requested by DMA_Getchannel()
  138. *
  139. * @param ch The channel to configure
  140. * @param prio The channel's priority
  141. * @param reqsel Select the DMA request line
  142. * @param reqwait_en The enable delay before request
  143. * @param tosel The transfer timer timeout select
  144. * @param pssel The transfer timer prescale select
  145. * @param srcwd The size of the read transactions
  146. * @param srcinc_en Enable auto-increment source pointer
  147. * @param dstwd The size of write transactions
  148. * @param dstinc_en Enable auto-increment destination pointer
  149. * @param burst_size The number of bytes transferred in one transaction
  150. * @param chdis_inten The channel disable interrupt enable
  151. * @param ctz_inten The count-to-zero interrupt enable
  152. *
  153. * @return #E_BAD_PARAM if an unused or invalid channel handle
  154. * @return #E_NO_ERROR otherwise
  155. */
  156. int DMA_ConfigChannel(int ch,
  157. dma_priority_t prio,
  158. dma_reqsel_t reqsel, unsigned int reqwait_en,
  159. dma_timeout_t tosel, dma_prescale_t pssel,
  160. dma_width_t srcwd, unsigned int srcinc_en,
  161. dma_width_t dstwd, unsigned int dstinc_en,
  162. unsigned int burst_size, unsigned int chdis_inten,
  163. unsigned int ctz_inten);
  164. /**
  165. * @brief Set channel source, destination, and count for transfer
  166. * @param ch channel handle
  167. * @param src_addr source address (*)
  168. * @param dst_addr destination address (*)
  169. * @param count number of bytes to transfer
  170. * @details This function is used to set the source and destination addresses and the number
  171. * of bytes to transfer using the channel, @p ch.
  172. * @note Unless the channel request select is #DMA_REQSEL_MEMTOMEM,
  173. * either src_addr or dst_addr will be ignored by the DMA engine.
  174. * In these cases, the address is a don't-care. See the User's
  175. * Guide for more information.
  176. * @return #E_BAD_PARAM if an unused or invalid channel handle
  177. * @return #E_NO_ERROR otherwise
  178. */
  179. int DMA_SetSrcDstCnt(int ch,
  180. void *src_addr,
  181. void *dst_addr,
  182. unsigned int count);
  183. /**
  184. * @brief Set channel reload values
  185. * @param ch channel handle
  186. * @param src_addr_reload source address
  187. * @param dst_addr_reload destination address
  188. * @param count_reload number of bytes to transfer
  189. * @details This function will set the values which will be loaded after the
  190. * channel count register reaches zero. After enabling, call with
  191. * count_reload set to zero to disable reload.
  192. * @return #E_BAD_PARAM if an unused or invalid channel handle
  193. * @return #E_NO_ERROR otherwise
  194. */
  195. int DMA_SetReload(int ch,
  196. void *src_addr_reload,
  197. void *dst_addr_reload,
  198. unsigned int count_reload);
  199. /**
  200. * @brief Set channel interrupt callback
  201. * @param ch channel handle
  202. * @param callback Pointer to a function to call when the channel
  203. * interrupt flag is set and interrupts are enabled or
  204. * when DMA is shutdown by the driver.
  205. * @details Configures the channel interrupt callback. The @p callback
  206. * function is called for two conditions:
  207. * -# When the channel's interrupt flag is set and DMA interrupts
  208. * are enabled.
  209. * -# If the driver calls the DMA_Shutdown() function. The
  210. * callback function prototype is:
  211. * @code
  212. * void callback_fn(int ch, int reason);
  213. * @endcode
  214. * @p ch indicates the channel that generated the callback, @p
  215. * reason is either #E_NO_ERROR for a DMA interrupt or #E_SHUTDOWN
  216. * if the DMA is being shutdown.
  217. *
  218. * @return #E_BAD_PARAM if an unused or invalid channel handle
  219. * @return #E_NO_ERROR otherwise
  220. */
  221. int DMA_SetCallback(int ch, void (*callback)(int, int));
  222. /**
  223. * @brief Enable channel interrupt
  224. * @param ch channel handle
  225. * @return #E_BAD_PARAM if an unused or invalid channel handle
  226. * @return #E_NO_ERROR otherwise
  227. */
  228. int DMA_EnableInterrupt(int ch);
  229. /**
  230. * @brief Disable channel interrupt
  231. * @param ch channel handle
  232. * @return #E_BAD_PARAM if an unused or invalid channel handle
  233. * @return #E_NO_ERROR otherwise
  234. */
  235. int DMA_DisableInterrupt(int ch);
  236. /**
  237. * @brief Read channel interrupt flags
  238. * @param ch channel handle
  239. * @param fl flags to get
  240. * @return #E_BAD_PARAM if an unused or invalid channel handle
  241. * @return #E_NO_ERROR otherwise
  242. */
  243. int DMA_GetFlags(int ch, unsigned int *fl);
  244. /**
  245. * @brief Clear channel interrupt flags
  246. * @param ch channel handle
  247. * @return #E_BAD_PARAM if an unused or invalid channel handle
  248. * @return #E_NO_ERROR otherwise
  249. */
  250. int DMA_ClearFlags(int ch);
  251. /**
  252. * @brief Start transfer
  253. * @param ch channel handle
  254. * @details Start the DMA channel transfer, assumes that DMA_SetSrcDstCnt() has been called beforehand.
  255. * @return #E_BAD_PARAM if an unused or invalid channel handle
  256. * @return #E_NO_ERROR otherwise
  257. */
  258. int DMA_Start(int ch);
  259. /**
  260. * @brief Stop DMA transfer, irrespective of status (complete or in-progress)
  261. * @param ch channel handle
  262. * @return #E_BAD_PARAM if an unused or invalid channel handle
  263. * @return #E_NO_ERROR otherwise
  264. */
  265. int DMA_Stop(int ch);
  266. /**
  267. * @brief Get a pointer to the DMA channel registers
  268. * @param ch channel handle
  269. * @details If direct access to DMA channel registers is required, this
  270. * function can be used on a channel handle returned by DMA_AcquireChannel().
  271. * @return NULL if an unused or invalid channel handle, or a valid pointer otherwise
  272. */
  273. mxc_dma_ch_regs_t *DMA_GetCHRegs(int ch);
  274. /**
  275. * @brief Interrupt handler function
  276. * @param ch channel handle
  277. * @details Call this function as the ISR for each DMA channel under driver control.
  278. * Interrupt flags for channel ch will be automatically cleared before return.
  279. * @return NULL if an unused or invalid channel handle, or a valid pointer otherwise
  280. */
  281. void DMA_Handler(int ch);
  282. /**@} end of group dma */
  283. #ifdef __cplusplus
  284. }
  285. #endif
  286. #endif /* _DMA_H_ */