stm32f1xx_ll_dma.c 13 KB

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