stm32l4xx_ll_dma.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_ll_dma.c
  4. * @author MCD Application Team
  5. * @brief DMA LL module driver.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
  10. *
  11. * Redistribution and use in source and binary forms, with or without modification,
  12. * are permitted provided that the following conditions are met:
  13. * 1. Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  19. * may be used to endorse or promote products derived from this software
  20. * without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  25. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  26. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  27. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  28. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  29. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  30. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. *
  33. ******************************************************************************
  34. */
  35. #if defined(USE_FULL_LL_DRIVER)
  36. /* Includes ------------------------------------------------------------------*/
  37. #include "stm32l4xx_ll_dma.h"
  38. #include "stm32l4xx_ll_bus.h"
  39. #ifdef USE_FULL_ASSERT
  40. #include "stm32_assert.h"
  41. #else
  42. #define assert_param(expr) ((void)0U)
  43. #endif
  44. /** @addtogroup STM32L4xx_LL_Driver
  45. * @{
  46. */
  47. #if defined (DMA1) || defined (DMA2)
  48. /** @defgroup DMA_LL DMA
  49. * @{
  50. */
  51. /* Private types -------------------------------------------------------------*/
  52. /* Private variables ---------------------------------------------------------*/
  53. /* Private constants ---------------------------------------------------------*/
  54. /* Private macros ------------------------------------------------------------*/
  55. /** @addtogroup DMA_LL_Private_Macros
  56. * @{
  57. */
  58. #define IS_LL_DMA_DIRECTION(__VALUE__) (((__VALUE__) == LL_DMA_DIRECTION_PERIPH_TO_MEMORY) || \
  59. ((__VALUE__) == LL_DMA_DIRECTION_MEMORY_TO_PERIPH) || \
  60. ((__VALUE__) == LL_DMA_DIRECTION_MEMORY_TO_MEMORY))
  61. #define IS_LL_DMA_MODE(__VALUE__) (((__VALUE__) == LL_DMA_MODE_NORMAL) || \
  62. ((__VALUE__) == LL_DMA_MODE_CIRCULAR))
  63. #define IS_LL_DMA_PERIPHINCMODE(__VALUE__) (((__VALUE__) == LL_DMA_PERIPH_INCREMENT) || \
  64. ((__VALUE__) == LL_DMA_PERIPH_NOINCREMENT))
  65. #define IS_LL_DMA_MEMORYINCMODE(__VALUE__) (((__VALUE__) == LL_DMA_MEMORY_INCREMENT) || \
  66. ((__VALUE__) == LL_DMA_MEMORY_NOINCREMENT))
  67. #define IS_LL_DMA_PERIPHDATASIZE(__VALUE__) (((__VALUE__) == LL_DMA_PDATAALIGN_BYTE) || \
  68. ((__VALUE__) == LL_DMA_PDATAALIGN_HALFWORD) || \
  69. ((__VALUE__) == LL_DMA_PDATAALIGN_WORD))
  70. #define IS_LL_DMA_MEMORYDATASIZE(__VALUE__) (((__VALUE__) == LL_DMA_MDATAALIGN_BYTE) || \
  71. ((__VALUE__) == LL_DMA_MDATAALIGN_HALFWORD) || \
  72. ((__VALUE__) == LL_DMA_MDATAALIGN_WORD))
  73. #define IS_LL_DMA_NBDATA(__VALUE__) ((__VALUE__) <= 0x0000FFFFU)
  74. #if defined(DMAMUX1)
  75. #define IS_LL_DMA_PERIPHREQUEST(__VALUE__) ((__VALUE__) <= 93U)
  76. #else
  77. #define IS_LL_DMA_PERIPHREQUEST(__VALUE__) (((__VALUE__) == LL_DMA_REQUEST_0) || \
  78. ((__VALUE__) == LL_DMA_REQUEST_1) || \
  79. ((__VALUE__) == LL_DMA_REQUEST_2) || \
  80. ((__VALUE__) == LL_DMA_REQUEST_3) || \
  81. ((__VALUE__) == LL_DMA_REQUEST_4) || \
  82. ((__VALUE__) == LL_DMA_REQUEST_5) || \
  83. ((__VALUE__) == LL_DMA_REQUEST_6) || \
  84. ((__VALUE__) == LL_DMA_REQUEST_7))
  85. #endif /* DMAMUX1 */
  86. #define IS_LL_DMA_PRIORITY(__VALUE__) (((__VALUE__) == LL_DMA_PRIORITY_LOW) || \
  87. ((__VALUE__) == LL_DMA_PRIORITY_MEDIUM) || \
  88. ((__VALUE__) == LL_DMA_PRIORITY_HIGH) || \
  89. ((__VALUE__) == LL_DMA_PRIORITY_VERYHIGH))
  90. #if defined (DMA2)
  91. #if defined (DMA2_Channel6) && defined (DMA2_Channel7)
  92. #define IS_LL_DMA_ALL_CHANNEL_INSTANCE(INSTANCE, CHANNEL) ((((INSTANCE) == DMA1) && \
  93. (((CHANNEL) == LL_DMA_CHANNEL_1) || \
  94. ((CHANNEL) == LL_DMA_CHANNEL_2) || \
  95. ((CHANNEL) == LL_DMA_CHANNEL_3) || \
  96. ((CHANNEL) == LL_DMA_CHANNEL_4) || \
  97. ((CHANNEL) == LL_DMA_CHANNEL_5) || \
  98. ((CHANNEL) == LL_DMA_CHANNEL_6) || \
  99. ((CHANNEL) == LL_DMA_CHANNEL_7))) || \
  100. (((INSTANCE) == DMA2) && \
  101. (((CHANNEL) == LL_DMA_CHANNEL_1) || \
  102. ((CHANNEL) == LL_DMA_CHANNEL_2) || \
  103. ((CHANNEL) == LL_DMA_CHANNEL_3) || \
  104. ((CHANNEL) == LL_DMA_CHANNEL_4) || \
  105. ((CHANNEL) == LL_DMA_CHANNEL_5) || \
  106. ((CHANNEL) == LL_DMA_CHANNEL_6) || \
  107. ((CHANNEL) == LL_DMA_CHANNEL_7))))
  108. #else
  109. #define IS_LL_DMA_ALL_CHANNEL_INSTANCE(INSTANCE, CHANNEL) ((((INSTANCE) == DMA1) && \
  110. (((CHANNEL) == LL_DMA_CHANNEL_1) || \
  111. ((CHANNEL) == LL_DMA_CHANNEL_2) || \
  112. ((CHANNEL) == LL_DMA_CHANNEL_3) || \
  113. ((CHANNEL) == LL_DMA_CHANNEL_4) || \
  114. ((CHANNEL) == LL_DMA_CHANNEL_5) || \
  115. ((CHANNEL) == LL_DMA_CHANNEL_6) || \
  116. ((CHANNEL) == LL_DMA_CHANNEL_7))) || \
  117. (((INSTANCE) == DMA2) && \
  118. (((CHANNEL) == LL_DMA_CHANNEL_1) || \
  119. ((CHANNEL) == LL_DMA_CHANNEL_2) || \
  120. ((CHANNEL) == LL_DMA_CHANNEL_3) || \
  121. ((CHANNEL) == LL_DMA_CHANNEL_4) || \
  122. ((CHANNEL) == LL_DMA_CHANNEL_5))))
  123. #endif
  124. #else
  125. #define IS_LL_DMA_ALL_CHANNEL_INSTANCE(INSTANCE, CHANNEL) ((((INSTANCE) == DMA1) && \
  126. (((CHANNEL) == LL_DMA_CHANNEL_1)|| \
  127. ((CHANNEL) == LL_DMA_CHANNEL_2) || \
  128. ((CHANNEL) == LL_DMA_CHANNEL_3) || \
  129. ((CHANNEL) == LL_DMA_CHANNEL_4) || \
  130. ((CHANNEL) == LL_DMA_CHANNEL_5) || \
  131. ((CHANNEL) == LL_DMA_CHANNEL_6) || \
  132. ((CHANNEL) == LL_DMA_CHANNEL_7))))
  133. #endif
  134. /**
  135. * @}
  136. */
  137. /* Private function prototypes -----------------------------------------------*/
  138. /* Exported functions --------------------------------------------------------*/
  139. /** @addtogroup DMA_LL_Exported_Functions
  140. * @{
  141. */
  142. /** @addtogroup DMA_LL_EF_Init
  143. * @{
  144. */
  145. /**
  146. * @brief De-initialize the DMA registers to their default reset values.
  147. * @param DMAx DMAx Instance
  148. * @param Channel This parameter can be one of the following values:
  149. * @arg @ref LL_DMA_CHANNEL_1
  150. * @arg @ref LL_DMA_CHANNEL_2
  151. * @arg @ref LL_DMA_CHANNEL_3
  152. * @arg @ref LL_DMA_CHANNEL_4
  153. * @arg @ref LL_DMA_CHANNEL_5
  154. * @arg @ref LL_DMA_CHANNEL_6
  155. * @arg @ref LL_DMA_CHANNEL_7
  156. * @arg @ref LL_DMA_CHANNEL_ALL
  157. * @retval An ErrorStatus enumeration value:
  158. * - SUCCESS: DMA registers are de-initialized
  159. * - ERROR: DMA registers are not de-initialized
  160. */
  161. uint32_t LL_DMA_DeInit(DMA_TypeDef *DMAx, uint32_t Channel)
  162. {
  163. DMA_Channel_TypeDef *tmp = (DMA_Channel_TypeDef *)DMA1_Channel1;
  164. ErrorStatus status = SUCCESS;
  165. /* Check the DMA Instance DMAx and Channel parameters*/
  166. assert_param(IS_LL_DMA_ALL_CHANNEL_INSTANCE(DMAx, Channel) || (Channel == LL_DMA_CHANNEL_ALL));
  167. if (Channel == LL_DMA_CHANNEL_ALL)
  168. {
  169. if (DMAx == DMA1)
  170. {
  171. /* Force reset of DMA clock */
  172. LL_AHB1_GRP1_ForceReset(LL_AHB1_GRP1_PERIPH_DMA1);
  173. /* Release reset of DMA clock */
  174. LL_AHB1_GRP1_ReleaseReset(LL_AHB1_GRP1_PERIPH_DMA1);
  175. }
  176. #if defined(DMA2)
  177. else if (DMAx == DMA2)
  178. {
  179. /* Force reset of DMA clock */
  180. LL_AHB1_GRP1_ForceReset(LL_AHB1_GRP1_PERIPH_DMA2);
  181. /* Release reset of DMA clock */
  182. LL_AHB1_GRP1_ReleaseReset(LL_AHB1_GRP1_PERIPH_DMA2);
  183. }
  184. #endif
  185. else
  186. {
  187. status = ERROR;
  188. }
  189. }
  190. else
  191. {
  192. tmp = (DMA_Channel_TypeDef *)(__LL_DMA_GET_CHANNEL_INSTANCE(DMAx, Channel));
  193. /* Disable the selected DMAx_Channely */
  194. CLEAR_BIT(tmp->CCR, DMA_CCR_EN);
  195. /* Reset DMAx_Channely control register */
  196. LL_DMA_WriteReg(tmp, CCR, 0U);
  197. /* Reset DMAx_Channely remaining bytes register */
  198. LL_DMA_WriteReg(tmp, CNDTR, 0U);
  199. /* Reset DMAx_Channely peripheral address register */
  200. LL_DMA_WriteReg(tmp, CPAR, 0U);
  201. /* Reset DMAx_Channely memory address register */
  202. LL_DMA_WriteReg(tmp, CMAR, 0U);
  203. #if defined(DMAMUX1)
  204. /* Reset Request register field for DMAx Channel */
  205. LL_DMA_SetPeriphRequest(DMAx, Channel, LL_DMAMUX_REQUEST_MEM2MEM);
  206. #else
  207. /* Reset Request register field for DMAx Channel */
  208. LL_DMA_SetPeriphRequest(DMAx, Channel, LL_DMA_REQUEST_0);
  209. #endif /* DMAMUX1 */
  210. if (Channel == LL_DMA_CHANNEL_1)
  211. {
  212. /* Reset interrupt pending bits for DMAx Channel1 */
  213. LL_DMA_ClearFlag_GI1(DMAx);
  214. }
  215. else if (Channel == LL_DMA_CHANNEL_2)
  216. {
  217. /* Reset interrupt pending bits for DMAx Channel2 */
  218. LL_DMA_ClearFlag_GI2(DMAx);
  219. }
  220. else if (Channel == LL_DMA_CHANNEL_3)
  221. {
  222. /* Reset interrupt pending bits for DMAx Channel3 */
  223. LL_DMA_ClearFlag_GI3(DMAx);
  224. }
  225. else if (Channel == LL_DMA_CHANNEL_4)
  226. {
  227. /* Reset interrupt pending bits for DMAx Channel4 */
  228. LL_DMA_ClearFlag_GI4(DMAx);
  229. }
  230. else if (Channel == LL_DMA_CHANNEL_5)
  231. {
  232. /* Reset interrupt pending bits for DMAx Channel5 */
  233. LL_DMA_ClearFlag_GI5(DMAx);
  234. }
  235. else if (Channel == LL_DMA_CHANNEL_6)
  236. {
  237. /* Reset interrupt pending bits for DMAx Channel6 */
  238. LL_DMA_ClearFlag_GI6(DMAx);
  239. }
  240. else if (Channel == LL_DMA_CHANNEL_7)
  241. {
  242. /* Reset interrupt pending bits for DMAx Channel7 */
  243. LL_DMA_ClearFlag_GI7(DMAx);
  244. }
  245. else
  246. {
  247. status = ERROR;
  248. }
  249. }
  250. return status;
  251. }
  252. /**
  253. * @brief Initialize the DMA registers according to the specified parameters in DMA_InitStruct.
  254. * @note To convert DMAx_Channely Instance to DMAx Instance and Channely, use helper macros :
  255. * @arg @ref __LL_DMA_GET_INSTANCE
  256. * @arg @ref __LL_DMA_GET_CHANNEL
  257. * @param DMAx DMAx Instance
  258. * @param Channel This parameter can be one of the following values:
  259. * @arg @ref LL_DMA_CHANNEL_1
  260. * @arg @ref LL_DMA_CHANNEL_2
  261. * @arg @ref LL_DMA_CHANNEL_3
  262. * @arg @ref LL_DMA_CHANNEL_4
  263. * @arg @ref LL_DMA_CHANNEL_5
  264. * @arg @ref LL_DMA_CHANNEL_6
  265. * @arg @ref LL_DMA_CHANNEL_7
  266. * @param DMA_InitStruct pointer to a @ref LL_DMA_InitTypeDef structure.
  267. * @retval An ErrorStatus enumeration value:
  268. * - SUCCESS: DMA registers are initialized
  269. * - ERROR: Not applicable
  270. */
  271. uint32_t LL_DMA_Init(DMA_TypeDef *DMAx, uint32_t Channel, LL_DMA_InitTypeDef *DMA_InitStruct)
  272. {
  273. /* Check the DMA Instance DMAx and Channel parameters*/
  274. assert_param(IS_LL_DMA_ALL_CHANNEL_INSTANCE(DMAx, Channel));
  275. /* Check the DMA parameters from DMA_InitStruct */
  276. assert_param(IS_LL_DMA_DIRECTION(DMA_InitStruct->Direction));
  277. assert_param(IS_LL_DMA_MODE(DMA_InitStruct->Mode));
  278. assert_param(IS_LL_DMA_PERIPHINCMODE(DMA_InitStruct->PeriphOrM2MSrcIncMode));
  279. assert_param(IS_LL_DMA_MEMORYINCMODE(DMA_InitStruct->MemoryOrM2MDstIncMode));
  280. assert_param(IS_LL_DMA_PERIPHDATASIZE(DMA_InitStruct->PeriphOrM2MSrcDataSize));
  281. assert_param(IS_LL_DMA_MEMORYDATASIZE(DMA_InitStruct->MemoryOrM2MDstDataSize));
  282. assert_param(IS_LL_DMA_NBDATA(DMA_InitStruct->NbData));
  283. assert_param(IS_LL_DMA_PERIPHREQUEST(DMA_InitStruct->PeriphRequest));
  284. assert_param(IS_LL_DMA_PRIORITY(DMA_InitStruct->Priority));
  285. /*---------------------------- DMAx CCR Configuration ------------------------
  286. * Configure DMAx_Channely: data transfer direction, data transfer mode,
  287. * peripheral and memory increment mode,
  288. * data size alignment and priority level with parameters :
  289. * - Direction: DMA_CCR_DIR and DMA_CCR_MEM2MEM bits
  290. * - Mode: DMA_CCR_CIRC bit
  291. * - PeriphOrM2MSrcIncMode: DMA_CCR_PINC bit
  292. * - MemoryOrM2MDstIncMode: DMA_CCR_MINC bit
  293. * - PeriphOrM2MSrcDataSize: DMA_CCR_PSIZE[1:0] bits
  294. * - MemoryOrM2MDstDataSize: DMA_CCR_MSIZE[1:0] bits
  295. * - Priority: DMA_CCR_PL[1:0] bits
  296. */
  297. LL_DMA_ConfigTransfer(DMAx, Channel, DMA_InitStruct->Direction | \
  298. DMA_InitStruct->Mode | \
  299. DMA_InitStruct->PeriphOrM2MSrcIncMode | \
  300. DMA_InitStruct->MemoryOrM2MDstIncMode | \
  301. DMA_InitStruct->PeriphOrM2MSrcDataSize | \
  302. DMA_InitStruct->MemoryOrM2MDstDataSize | \
  303. DMA_InitStruct->Priority);
  304. /*-------------------------- DMAx CMAR Configuration -------------------------
  305. * Configure the memory or destination base address with parameter :
  306. * - MemoryOrM2MDstAddress: DMA_CMAR_MA[31:0] bits
  307. */
  308. LL_DMA_SetMemoryAddress(DMAx, Channel, DMA_InitStruct->MemoryOrM2MDstAddress);
  309. /*-------------------------- DMAx CPAR Configuration -------------------------
  310. * Configure the peripheral or source base address with parameter :
  311. * - PeriphOrM2MSrcAddress: DMA_CPAR_PA[31:0] bits
  312. */
  313. LL_DMA_SetPeriphAddress(DMAx, Channel, DMA_InitStruct->PeriphOrM2MSrcAddress);
  314. /*--------------------------- DMAx CNDTR Configuration -----------------------
  315. * Configure the peripheral base address with parameter :
  316. * - NbData: DMA_CNDTR_NDT[15:0] bits
  317. */
  318. LL_DMA_SetDataLength(DMAx, Channel, DMA_InitStruct->NbData);
  319. #if defined(DMAMUX1)
  320. /*--------------------------- DMAMUXx CCR Configuration ----------------------
  321. * Configure the DMA request for DMA Channels on DMAMUX Channel x with parameter :
  322. * - PeriphRequest: DMA_CxCR[7:0] bits
  323. */
  324. LL_DMA_SetPeriphRequest(DMAx, Channel, DMA_InitStruct->PeriphRequest);
  325. #else
  326. /*--------------------------- DMAx CSELR Configuration -----------------------
  327. * Configure the DMA request for DMA instance on Channel x with parameter :
  328. * - PeriphRequest: DMA_CSELR[31:0] bits
  329. */
  330. LL_DMA_SetPeriphRequest(DMAx, Channel, DMA_InitStruct->PeriphRequest);
  331. #endif /* DMAMUX1 */
  332. return SUCCESS;
  333. }
  334. /**
  335. * @brief Set each @ref LL_DMA_InitTypeDef field to default value.
  336. * @param DMA_InitStruct Pointer to a @ref LL_DMA_InitTypeDef structure.
  337. * @retval None
  338. */
  339. void LL_DMA_StructInit(LL_DMA_InitTypeDef *DMA_InitStruct)
  340. {
  341. /* Set DMA_InitStruct fields to default values */
  342. DMA_InitStruct->PeriphOrM2MSrcAddress = 0x00000000U;
  343. DMA_InitStruct->MemoryOrM2MDstAddress = 0x00000000U;
  344. DMA_InitStruct->Direction = LL_DMA_DIRECTION_PERIPH_TO_MEMORY;
  345. DMA_InitStruct->Mode = LL_DMA_MODE_NORMAL;
  346. DMA_InitStruct->PeriphOrM2MSrcIncMode = LL_DMA_PERIPH_NOINCREMENT;
  347. DMA_InitStruct->MemoryOrM2MDstIncMode = LL_DMA_MEMORY_NOINCREMENT;
  348. DMA_InitStruct->PeriphOrM2MSrcDataSize = LL_DMA_PDATAALIGN_BYTE;
  349. DMA_InitStruct->MemoryOrM2MDstDataSize = LL_DMA_MDATAALIGN_BYTE;
  350. DMA_InitStruct->NbData = 0x00000000U;
  351. #if defined(DMAMUX1)
  352. DMA_InitStruct->PeriphRequest = LL_DMAMUX_REQUEST_MEM2MEM;
  353. #else
  354. DMA_InitStruct->PeriphRequest = LL_DMA_REQUEST_0;
  355. #endif /* DMAMUX1 */
  356. DMA_InitStruct->Priority = LL_DMA_PRIORITY_LOW;
  357. }
  358. /**
  359. * @}
  360. */
  361. /**
  362. * @}
  363. */
  364. /**
  365. * @}
  366. */
  367. #endif /* DMA1 || DMA2 */
  368. /**
  369. * @}
  370. */
  371. #endif /* USE_FULL_LL_DRIVER */
  372. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/