stm32g0xx_ll_dma.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /**
  2. ******************************************************************************
  3. * @file stm32g0xx_ll_dma.c
  4. * @author MCD Application Team
  5. * @brief DMA LL module driver.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2018 STMicroelectronics.
  10. * All rights reserved.</center></h2>
  11. *
  12. * This software component is licensed by ST under BSD 3-Clause license,
  13. * the "License"; You may not use this file except in compliance with the
  14. * License. You may obtain a copy of the License at:
  15. * opensource.org/licenses/BSD-3-Clause
  16. *
  17. ******************************************************************************
  18. */
  19. #if defined(USE_FULL_LL_DRIVER)
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "stm32g0xx_ll_dma.h"
  22. #include "stm32g0xx_ll_bus.h"
  23. #ifdef USE_FULL_ASSERT
  24. #include "stm32_assert.h"
  25. #else
  26. #define assert_param(expr) ((void)0U)
  27. #endif
  28. /** @addtogroup STM32G0xx_LL_Driver
  29. * @{
  30. */
  31. #if defined (DMA1) || defined (DMA2)
  32. /** @defgroup DMA_LL DMA
  33. * @{
  34. */
  35. /* Private types -------------------------------------------------------------*/
  36. /* Private variables ---------------------------------------------------------*/
  37. /* Private constants ---------------------------------------------------------*/
  38. /* Private macros ------------------------------------------------------------*/
  39. /** @addtogroup DMA_LL_Private_Macros
  40. * @{
  41. */
  42. #define IS_LL_DMA_DIRECTION(__VALUE__) (((__VALUE__) == LL_DMA_DIRECTION_PERIPH_TO_MEMORY) || \
  43. ((__VALUE__) == LL_DMA_DIRECTION_MEMORY_TO_PERIPH) || \
  44. ((__VALUE__) == LL_DMA_DIRECTION_MEMORY_TO_MEMORY))
  45. #define IS_LL_DMA_MODE(__VALUE__) (((__VALUE__) == LL_DMA_MODE_NORMAL) || \
  46. ((__VALUE__) == LL_DMA_MODE_CIRCULAR))
  47. #define IS_LL_DMA_PERIPHINCMODE(__VALUE__) (((__VALUE__) == LL_DMA_PERIPH_INCREMENT) || \
  48. ((__VALUE__) == LL_DMA_PERIPH_NOINCREMENT))
  49. #define IS_LL_DMA_MEMORYINCMODE(__VALUE__) (((__VALUE__) == LL_DMA_MEMORY_INCREMENT) || \
  50. ((__VALUE__) == LL_DMA_MEMORY_NOINCREMENT))
  51. #define IS_LL_DMA_PERIPHDATASIZE(__VALUE__) (((__VALUE__) == LL_DMA_PDATAALIGN_BYTE) || \
  52. ((__VALUE__) == LL_DMA_PDATAALIGN_HALFWORD) || \
  53. ((__VALUE__) == LL_DMA_PDATAALIGN_WORD))
  54. #define IS_LL_DMA_MEMORYDATASIZE(__VALUE__) (((__VALUE__) == LL_DMA_MDATAALIGN_BYTE) || \
  55. ((__VALUE__) == LL_DMA_MDATAALIGN_HALFWORD) || \
  56. ((__VALUE__) == LL_DMA_MDATAALIGN_WORD))
  57. #define IS_LL_DMA_NBDATA(__VALUE__) ((__VALUE__) <= 0x0000FFFFU)
  58. #define IS_LL_DMA_PERIPHREQUEST(__VALUE__) ((__VALUE__) <= LL_DMAMUX_MAX_REQ)
  59. #define IS_LL_DMA_PRIORITY(__VALUE__) (((__VALUE__) == LL_DMA_PRIORITY_LOW) || \
  60. ((__VALUE__) == LL_DMA_PRIORITY_MEDIUM) || \
  61. ((__VALUE__) == LL_DMA_PRIORITY_HIGH) || \
  62. ((__VALUE__) == LL_DMA_PRIORITY_VERYHIGH))
  63. #if defined(DMA2)
  64. #define IS_LL_DMA_ALL_CHANNEL_INSTANCE(INSTANCE, CHANNEL) ((((INSTANCE) == DMA1) && \
  65. (((CHANNEL) == LL_DMA_CHANNEL_1) || \
  66. ((CHANNEL) == LL_DMA_CHANNEL_2) || \
  67. ((CHANNEL) == LL_DMA_CHANNEL_3) || \
  68. ((CHANNEL) == LL_DMA_CHANNEL_4) || \
  69. ((CHANNEL) == LL_DMA_CHANNEL_5) || \
  70. ((CHANNEL) == LL_DMA_CHANNEL_6) || \
  71. ((CHANNEL) == LL_DMA_CHANNEL_7))) || \
  72. (((INSTANCE) == DMA2) && \
  73. (((CHANNEL) == LL_DMA_CHANNEL_1) || \
  74. ((CHANNEL) == LL_DMA_CHANNEL_2) || \
  75. ((CHANNEL) == LL_DMA_CHANNEL_3) || \
  76. ((CHANNEL) == LL_DMA_CHANNEL_4) || \
  77. ((CHANNEL) == LL_DMA_CHANNEL_5))))
  78. #else /* DMA1 */
  79. #if defined(DMA1_Channel7)
  80. #define IS_LL_DMA_ALL_CHANNEL_INSTANCE(INSTANCE, CHANNEL) ((((INSTANCE) == DMA1) && \
  81. (((CHANNEL) == LL_DMA_CHANNEL_1) || \
  82. ((CHANNEL) == LL_DMA_CHANNEL_2) || \
  83. ((CHANNEL) == LL_DMA_CHANNEL_3) || \
  84. ((CHANNEL) == LL_DMA_CHANNEL_4) || \
  85. ((CHANNEL) == LL_DMA_CHANNEL_5) || \
  86. ((CHANNEL) == LL_DMA_CHANNEL_6) || \
  87. ((CHANNEL) == LL_DMA_CHANNEL_7))))
  88. #else
  89. #define IS_LL_DMA_ALL_CHANNEL_INSTANCE(INSTANCE, CHANNEL) ((((INSTANCE) == DMA1) && \
  90. (((CHANNEL) == LL_DMA_CHANNEL_1) || \
  91. ((CHANNEL) == LL_DMA_CHANNEL_2) || \
  92. ((CHANNEL) == LL_DMA_CHANNEL_3) || \
  93. ((CHANNEL) == LL_DMA_CHANNEL_4) || \
  94. ((CHANNEL) == LL_DMA_CHANNEL_5))))
  95. #endif
  96. #endif /* DMA2 */
  97. /**
  98. * @}
  99. */
  100. /* Private function prototypes -----------------------------------------------*/
  101. /* Exported functions --------------------------------------------------------*/
  102. /** @addtogroup DMA_LL_Exported_Functions
  103. * @{
  104. */
  105. /** @addtogroup DMA_LL_EF_Init
  106. * @{
  107. */
  108. /**
  109. * @brief De-initialize the DMA registers to their default reset values.
  110. * @param DMAx DMAx Instance
  111. * @param Channel This parameter can be one of the following values:
  112. * @arg @ref LL_DMA_CHANNEL_1
  113. * @arg @ref LL_DMA_CHANNEL_2
  114. * @arg @ref LL_DMA_CHANNEL_3
  115. * @arg @ref LL_DMA_CHANNEL_4
  116. * @arg @ref LL_DMA_CHANNEL_5
  117. * @arg @ref LL_DMA_CHANNEL_6
  118. * @arg @ref LL_DMA_CHANNEL_7
  119. * @arg @ref LL_DMA_CHANNEL_ALL
  120. * @retval An ErrorStatus enumeration value:
  121. * - SUCCESS: DMA registers are de-initialized
  122. * - ERROR: DMA registers are not de-initialized
  123. */
  124. ErrorStatus LL_DMA_DeInit(DMA_TypeDef *DMAx, uint32_t Channel)
  125. {
  126. ErrorStatus status = SUCCESS;
  127. /* Check the DMA Instance DMAx and Channel parameters*/
  128. assert_param(IS_LL_DMA_ALL_CHANNEL_INSTANCE(DMAx, Channel) || (Channel == LL_DMA_CHANNEL_ALL));
  129. if (Channel == LL_DMA_CHANNEL_ALL)
  130. {
  131. if (DMAx == DMA1)
  132. {
  133. /* Force reset of DMA clock */
  134. LL_AHB1_GRP1_ForceReset(LL_AHB1_GRP1_PERIPH_DMA1);
  135. /* Release reset of DMA clock */
  136. LL_AHB1_GRP1_ReleaseReset(LL_AHB1_GRP1_PERIPH_DMA1);
  137. }
  138. #if defined(DMA2)
  139. else if (DMAx == DMA2)
  140. {
  141. /* Force reset of DMA clock */
  142. LL_AHB1_GRP1_ForceReset(LL_AHB1_GRP1_PERIPH_DMA2);
  143. /* Release reset of DMA clock */
  144. LL_AHB1_GRP1_ReleaseReset(LL_AHB1_GRP1_PERIPH_DMA2);
  145. }
  146. #endif
  147. else
  148. {
  149. status = ERROR;
  150. }
  151. }
  152. else
  153. {
  154. DMA_Channel_TypeDef *tmp;
  155. tmp = (DMA_Channel_TypeDef *)(__LL_DMA_GET_CHANNEL_INSTANCE(DMAx, Channel));
  156. /* Disable the selected DMAx_Channely */
  157. CLEAR_BIT(tmp->CCR, DMA_CCR_EN);
  158. /* Reset DMAx_Channely control register */
  159. WRITE_REG(tmp->CCR, 0U);
  160. /* Reset DMAx_Channely remaining bytes register */
  161. WRITE_REG(tmp->CNDTR, 0U);
  162. /* Reset DMAx_Channely peripheral address register */
  163. WRITE_REG(tmp->CPAR, 0U);
  164. /* Reset DMAx_Channely memory address register */
  165. WRITE_REG(tmp->CMAR, 0U);
  166. /* Reset Request register field for DMAx Channel */
  167. LL_DMA_SetPeriphRequest(DMAx, Channel, LL_DMAMUX_REQ_MEM2MEM);
  168. if (Channel == LL_DMA_CHANNEL_1)
  169. {
  170. /* Reset interrupt pending bits for DMAx Channel1 */
  171. LL_DMA_ClearFlag_GI1(DMAx);
  172. }
  173. else if (Channel == LL_DMA_CHANNEL_2)
  174. {
  175. /* Reset interrupt pending bits for DMAx Channel2 */
  176. LL_DMA_ClearFlag_GI2(DMAx);
  177. }
  178. else if (Channel == LL_DMA_CHANNEL_3)
  179. {
  180. /* Reset interrupt pending bits for DMAx Channel3 */
  181. LL_DMA_ClearFlag_GI3(DMAx);
  182. }
  183. else if (Channel == LL_DMA_CHANNEL_4)
  184. {
  185. /* Reset interrupt pending bits for DMAx Channel4 */
  186. LL_DMA_ClearFlag_GI4(DMAx);
  187. }
  188. else if (Channel == LL_DMA_CHANNEL_5)
  189. {
  190. /* Reset interrupt pending bits for DMAx Channel5 */
  191. LL_DMA_ClearFlag_GI5(DMAx);
  192. }
  193. #if defined(DMA1_Channel6)
  194. else if (Channel == LL_DMA_CHANNEL_6)
  195. {
  196. /* Reset interrupt pending bits for DMAx Channel6 */
  197. LL_DMA_ClearFlag_GI6(DMAx);
  198. }
  199. #endif
  200. #if defined(DMA1_Channel7)
  201. else if (Channel == LL_DMA_CHANNEL_7)
  202. {
  203. /* Reset interrupt pending bits for DMAx Channel7 */
  204. LL_DMA_ClearFlag_GI7(DMAx);
  205. }
  206. #endif
  207. else
  208. {
  209. status = ERROR;
  210. }
  211. }
  212. return status;
  213. }
  214. /**
  215. * @brief Initialize the DMA registers according to the specified parameters in DMA_InitStruct.
  216. * @note To convert DMAx_Channely Instance to DMAx Instance and Channely, use helper macros :
  217. * @arg @ref __LL_DMA_GET_INSTANCE
  218. * @arg @ref __LL_DMA_GET_CHANNEL
  219. * @param DMAx DMAx Instance
  220. * @param Channel This parameter can be one of the following values:
  221. * @arg @ref LL_DMA_CHANNEL_1
  222. * @arg @ref LL_DMA_CHANNEL_2
  223. * @arg @ref LL_DMA_CHANNEL_3
  224. * @arg @ref LL_DMA_CHANNEL_4
  225. * @arg @ref LL_DMA_CHANNEL_5
  226. * @arg @ref LL_DMA_CHANNEL_6
  227. * @arg @ref LL_DMA_CHANNEL_7
  228. * @param DMA_InitStruct pointer to a @ref LL_DMA_InitTypeDef structure.
  229. * @retval An ErrorStatus enumeration value:
  230. * - SUCCESS: DMA registers are initialized
  231. * - ERROR: Not applicable
  232. */
  233. ErrorStatus LL_DMA_Init(DMA_TypeDef *DMAx, uint32_t Channel, LL_DMA_InitTypeDef *DMA_InitStruct)
  234. {
  235. /* Check the DMA Instance DMAx and Channel parameters*/
  236. assert_param(IS_LL_DMA_ALL_CHANNEL_INSTANCE(DMAx, Channel));
  237. /* Check the DMA parameters from DMA_InitStruct */
  238. assert_param(IS_LL_DMA_DIRECTION(DMA_InitStruct->Direction));
  239. assert_param(IS_LL_DMA_MODE(DMA_InitStruct->Mode));
  240. assert_param(IS_LL_DMA_PERIPHINCMODE(DMA_InitStruct->PeriphOrM2MSrcIncMode));
  241. assert_param(IS_LL_DMA_MEMORYINCMODE(DMA_InitStruct->MemoryOrM2MDstIncMode));
  242. assert_param(IS_LL_DMA_PERIPHDATASIZE(DMA_InitStruct->PeriphOrM2MSrcDataSize));
  243. assert_param(IS_LL_DMA_MEMORYDATASIZE(DMA_InitStruct->MemoryOrM2MDstDataSize));
  244. assert_param(IS_LL_DMA_NBDATA(DMA_InitStruct->NbData));
  245. assert_param(IS_LL_DMA_PERIPHREQUEST(DMA_InitStruct->PeriphRequest));
  246. assert_param(IS_LL_DMA_PRIORITY(DMA_InitStruct->Priority));
  247. /*---------------------------- DMAx CCR Configuration ------------------------
  248. * Configure DMAx_Channely: data transfer direction, data transfer mode,
  249. * peripheral and memory increment mode,
  250. * data size alignment and priority level with parameters :
  251. * - Direction: DMA_CCR_DIR and DMA_CCR_MEM2MEM bits
  252. * - Mode: DMA_CCR_CIRC bit
  253. * - PeriphOrM2MSrcIncMode: DMA_CCR_PINC bit
  254. * - MemoryOrM2MDstIncMode: DMA_CCR_MINC bit
  255. * - PeriphOrM2MSrcDataSize: DMA_CCR_PSIZE[1:0] bits
  256. * - MemoryOrM2MDstDataSize: DMA_CCR_MSIZE[1:0] bits
  257. * - Priority: DMA_CCR_PL[1:0] bits
  258. */
  259. LL_DMA_ConfigTransfer(DMAx, Channel, DMA_InitStruct->Direction | \
  260. DMA_InitStruct->Mode | \
  261. DMA_InitStruct->PeriphOrM2MSrcIncMode | \
  262. DMA_InitStruct->MemoryOrM2MDstIncMode | \
  263. DMA_InitStruct->PeriphOrM2MSrcDataSize | \
  264. DMA_InitStruct->MemoryOrM2MDstDataSize | \
  265. DMA_InitStruct->Priority);
  266. /*-------------------------- DMAx CMAR Configuration -------------------------
  267. * Configure the memory or destination base address with parameter :
  268. * - MemoryOrM2MDstAddress: DMA_CMAR_MA[31:0] bits
  269. */
  270. LL_DMA_SetMemoryAddress(DMAx, Channel, DMA_InitStruct->MemoryOrM2MDstAddress);
  271. /*-------------------------- DMAx CPAR Configuration -------------------------
  272. * Configure the peripheral or source base address with parameter :
  273. * - PeriphOrM2MSrcAddress: DMA_CPAR_PA[31:0] bits
  274. */
  275. LL_DMA_SetPeriphAddress(DMAx, Channel, DMA_InitStruct->PeriphOrM2MSrcAddress);
  276. /*--------------------------- DMAx CNDTR Configuration -----------------------
  277. * Configure the peripheral base address with parameter :
  278. * - NbData: DMA_CNDTR_NDT[15:0] bits
  279. */
  280. LL_DMA_SetDataLength(DMAx, Channel, DMA_InitStruct->NbData);
  281. /*--------------------------- DMAMUXx CCR Configuration ----------------------
  282. * Configure the DMA request for DMA Channels on DMAMUX Channel x with parameter :
  283. * - PeriphRequest: DMA_CxCR[7:0] bits
  284. */
  285. LL_DMA_SetPeriphRequest(DMAx, Channel, DMA_InitStruct->PeriphRequest);
  286. return SUCCESS;
  287. }
  288. /**
  289. * @brief Set each @ref LL_DMA_InitTypeDef field to default value.
  290. * @param DMA_InitStruct Pointer to a @ref LL_DMA_InitTypeDef structure.
  291. * @retval None
  292. */
  293. void LL_DMA_StructInit(LL_DMA_InitTypeDef *DMA_InitStruct)
  294. {
  295. /* Set DMA_InitStruct fields to default values */
  296. DMA_InitStruct->PeriphOrM2MSrcAddress = 0x00000000U;
  297. DMA_InitStruct->MemoryOrM2MDstAddress = 0x00000000U;
  298. DMA_InitStruct->Direction = LL_DMA_DIRECTION_PERIPH_TO_MEMORY;
  299. DMA_InitStruct->Mode = LL_DMA_MODE_NORMAL;
  300. DMA_InitStruct->PeriphOrM2MSrcIncMode = LL_DMA_PERIPH_NOINCREMENT;
  301. DMA_InitStruct->MemoryOrM2MDstIncMode = LL_DMA_MEMORY_NOINCREMENT;
  302. DMA_InitStruct->PeriphOrM2MSrcDataSize = LL_DMA_PDATAALIGN_BYTE;
  303. DMA_InitStruct->MemoryOrM2MDstDataSize = LL_DMA_MDATAALIGN_BYTE;
  304. DMA_InitStruct->NbData = 0x00000000U;
  305. DMA_InitStruct->PeriphRequest = LL_DMAMUX_REQ_MEM2MEM;
  306. DMA_InitStruct->Priority = LL_DMA_PRIORITY_LOW;
  307. }
  308. /**
  309. * @}
  310. */
  311. /**
  312. * @}
  313. */
  314. /**
  315. * @}
  316. */
  317. #endif /* DMA1 || DMA2 */
  318. /**
  319. * @}
  320. */
  321. #endif /* USE_FULL_LL_DRIVER */
  322. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/