stm32f4xx_ll_dma.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_ll_dma.c
  4. * @author MCD Application Team
  5. * @brief DMA LL module driver.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2017 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 "stm32f4xx_ll_dma.h"
  22. #include "stm32f4xx_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 STM32F4xx_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. ((__VALUE__) == LL_DMA_MODE_PFCTRL))
  48. #define IS_LL_DMA_PERIPHINCMODE(__VALUE__) (((__VALUE__) == LL_DMA_PERIPH_INCREMENT) || \
  49. ((__VALUE__) == LL_DMA_PERIPH_NOINCREMENT))
  50. #define IS_LL_DMA_MEMORYINCMODE(__VALUE__) (((__VALUE__) == LL_DMA_MEMORY_INCREMENT) || \
  51. ((__VALUE__) == LL_DMA_MEMORY_NOINCREMENT))
  52. #define IS_LL_DMA_PERIPHDATASIZE(__VALUE__) (((__VALUE__) == LL_DMA_PDATAALIGN_BYTE) || \
  53. ((__VALUE__) == LL_DMA_PDATAALIGN_HALFWORD) || \
  54. ((__VALUE__) == LL_DMA_PDATAALIGN_WORD))
  55. #define IS_LL_DMA_MEMORYDATASIZE(__VALUE__) (((__VALUE__) == LL_DMA_MDATAALIGN_BYTE) || \
  56. ((__VALUE__) == LL_DMA_MDATAALIGN_HALFWORD) || \
  57. ((__VALUE__) == LL_DMA_MDATAALIGN_WORD))
  58. #define IS_LL_DMA_NBDATA(__VALUE__) ((__VALUE__) <= 0x0000FFFFU)
  59. #define IS_LL_DMA_CHANNEL(__VALUE__) (((__VALUE__) == LL_DMA_CHANNEL_0) || \
  60. ((__VALUE__) == LL_DMA_CHANNEL_1) || \
  61. ((__VALUE__) == LL_DMA_CHANNEL_2) || \
  62. ((__VALUE__) == LL_DMA_CHANNEL_3) || \
  63. ((__VALUE__) == LL_DMA_CHANNEL_4) || \
  64. ((__VALUE__) == LL_DMA_CHANNEL_5) || \
  65. ((__VALUE__) == LL_DMA_CHANNEL_6) || \
  66. ((__VALUE__) == LL_DMA_CHANNEL_7))
  67. #define IS_LL_DMA_PRIORITY(__VALUE__) (((__VALUE__) == LL_DMA_PRIORITY_LOW) || \
  68. ((__VALUE__) == LL_DMA_PRIORITY_MEDIUM) || \
  69. ((__VALUE__) == LL_DMA_PRIORITY_HIGH) || \
  70. ((__VALUE__) == LL_DMA_PRIORITY_VERYHIGH))
  71. #define IS_LL_DMA_ALL_STREAM_INSTANCE(INSTANCE, STREAM) ((((INSTANCE) == DMA1) && \
  72. (((STREAM) == LL_DMA_STREAM_0) || \
  73. ((STREAM) == LL_DMA_STREAM_1) || \
  74. ((STREAM) == LL_DMA_STREAM_2) || \
  75. ((STREAM) == LL_DMA_STREAM_3) || \
  76. ((STREAM) == LL_DMA_STREAM_4) || \
  77. ((STREAM) == LL_DMA_STREAM_5) || \
  78. ((STREAM) == LL_DMA_STREAM_6) || \
  79. ((STREAM) == LL_DMA_STREAM_7) || \
  80. ((STREAM) == LL_DMA_STREAM_ALL))) ||\
  81. (((INSTANCE) == DMA2) && \
  82. (((STREAM) == LL_DMA_STREAM_0) || \
  83. ((STREAM) == LL_DMA_STREAM_1) || \
  84. ((STREAM) == LL_DMA_STREAM_2) || \
  85. ((STREAM) == LL_DMA_STREAM_3) || \
  86. ((STREAM) == LL_DMA_STREAM_4) || \
  87. ((STREAM) == LL_DMA_STREAM_5) || \
  88. ((STREAM) == LL_DMA_STREAM_6) || \
  89. ((STREAM) == LL_DMA_STREAM_7) || \
  90. ((STREAM) == LL_DMA_STREAM_ALL))))
  91. #define IS_LL_DMA_FIFO_MODE_STATE(STATE) (((STATE) == LL_DMA_FIFOMODE_DISABLE ) || \
  92. ((STATE) == LL_DMA_FIFOMODE_ENABLE))
  93. #define IS_LL_DMA_FIFO_THRESHOLD(THRESHOLD) (((THRESHOLD) == LL_DMA_FIFOTHRESHOLD_1_4) || \
  94. ((THRESHOLD) == LL_DMA_FIFOTHRESHOLD_1_2) || \
  95. ((THRESHOLD) == LL_DMA_FIFOTHRESHOLD_3_4) || \
  96. ((THRESHOLD) == LL_DMA_FIFOTHRESHOLD_FULL))
  97. #define IS_LL_DMA_MEMORY_BURST(BURST) (((BURST) == LL_DMA_MBURST_SINGLE) || \
  98. ((BURST) == LL_DMA_MBURST_INC4) || \
  99. ((BURST) == LL_DMA_MBURST_INC8) || \
  100. ((BURST) == LL_DMA_MBURST_INC16))
  101. #define IS_LL_DMA_PERIPHERAL_BURST(BURST) (((BURST) == LL_DMA_PBURST_SINGLE) || \
  102. ((BURST) == LL_DMA_PBURST_INC4) || \
  103. ((BURST) == LL_DMA_PBURST_INC8) || \
  104. ((BURST) == LL_DMA_PBURST_INC16))
  105. /**
  106. * @}
  107. */
  108. /* Private function prototypes -----------------------------------------------*/
  109. /* Exported functions --------------------------------------------------------*/
  110. /** @addtogroup DMA_LL_Exported_Functions
  111. * @{
  112. */
  113. /** @addtogroup DMA_LL_EF_Init
  114. * @{
  115. */
  116. /**
  117. * @brief De-initialize the DMA registers to their default reset values.
  118. * @param DMAx DMAx Instance
  119. * @param Stream This parameter can be one of the following values:
  120. * @arg @ref LL_DMA_STREAM_0
  121. * @arg @ref LL_DMA_STREAM_1
  122. * @arg @ref LL_DMA_STREAM_2
  123. * @arg @ref LL_DMA_STREAM_3
  124. * @arg @ref LL_DMA_STREAM_4
  125. * @arg @ref LL_DMA_STREAM_5
  126. * @arg @ref LL_DMA_STREAM_6
  127. * @arg @ref LL_DMA_STREAM_7
  128. * @arg @ref LL_DMA_STREAM_ALL
  129. * @retval An ErrorStatus enumeration value:
  130. * - SUCCESS: DMA registers are de-initialized
  131. * - ERROR: DMA registers are not de-initialized
  132. */
  133. uint32_t LL_DMA_DeInit(DMA_TypeDef *DMAx, uint32_t Stream)
  134. {
  135. DMA_Stream_TypeDef *tmp = (DMA_Stream_TypeDef *)DMA1_Stream0;
  136. ErrorStatus status = SUCCESS;
  137. /* Check the DMA Instance DMAx and Stream parameters*/
  138. assert_param(IS_LL_DMA_ALL_STREAM_INSTANCE(DMAx, Stream));
  139. if (Stream == LL_DMA_STREAM_ALL)
  140. {
  141. if (DMAx == DMA1)
  142. {
  143. /* Force reset of DMA clock */
  144. LL_AHB1_GRP1_ForceReset(LL_AHB1_GRP1_PERIPH_DMA1);
  145. /* Release reset of DMA clock */
  146. LL_AHB1_GRP1_ReleaseReset(LL_AHB1_GRP1_PERIPH_DMA1);
  147. }
  148. else if (DMAx == DMA2)
  149. {
  150. /* Force reset of DMA clock */
  151. LL_AHB1_GRP1_ForceReset(LL_AHB1_GRP1_PERIPH_DMA2);
  152. /* Release reset of DMA clock */
  153. LL_AHB1_GRP1_ReleaseReset(LL_AHB1_GRP1_PERIPH_DMA2);
  154. }
  155. else
  156. {
  157. status = ERROR;
  158. }
  159. }
  160. else
  161. {
  162. /* Disable the selected Stream */
  163. LL_DMA_DisableStream(DMAx,Stream);
  164. /* Get the DMA Stream Instance */
  165. tmp = (DMA_Stream_TypeDef *)(__LL_DMA_GET_STREAM_INSTANCE(DMAx, Stream));
  166. /* Reset DMAx_Streamy configuration register */
  167. LL_DMA_WriteReg(tmp, CR, 0U);
  168. /* Reset DMAx_Streamy remaining bytes register */
  169. LL_DMA_WriteReg(tmp, NDTR, 0U);
  170. /* Reset DMAx_Streamy peripheral address register */
  171. LL_DMA_WriteReg(tmp, PAR, 0U);
  172. /* Reset DMAx_Streamy memory address register */
  173. LL_DMA_WriteReg(tmp, M0AR, 0U);
  174. /* Reset DMAx_Streamy memory address register */
  175. LL_DMA_WriteReg(tmp, M1AR, 0U);
  176. /* Reset DMAx_Streamy FIFO control register */
  177. LL_DMA_WriteReg(tmp, FCR, 0x00000021U);
  178. /* Reset Channel register field for DMAx Stream*/
  179. LL_DMA_SetChannelSelection(DMAx, Stream, LL_DMA_CHANNEL_0);
  180. if(Stream == LL_DMA_STREAM_0)
  181. {
  182. /* Reset the Stream0 pending flags */
  183. DMAx->LIFCR = 0x0000003FU;
  184. }
  185. else if(Stream == LL_DMA_STREAM_1)
  186. {
  187. /* Reset the Stream1 pending flags */
  188. DMAx->LIFCR = 0x00000F40U;
  189. }
  190. else if(Stream == LL_DMA_STREAM_2)
  191. {
  192. /* Reset the Stream2 pending flags */
  193. DMAx->LIFCR = 0x003F0000U;
  194. }
  195. else if(Stream == LL_DMA_STREAM_3)
  196. {
  197. /* Reset the Stream3 pending flags */
  198. DMAx->LIFCR = 0x0F400000U;
  199. }
  200. else if(Stream == LL_DMA_STREAM_4)
  201. {
  202. /* Reset the Stream4 pending flags */
  203. DMAx->HIFCR = 0x0000003FU;
  204. }
  205. else if(Stream == LL_DMA_STREAM_5)
  206. {
  207. /* Reset the Stream5 pending flags */
  208. DMAx->HIFCR = 0x00000F40U;
  209. }
  210. else if(Stream == LL_DMA_STREAM_6)
  211. {
  212. /* Reset the Stream6 pending flags */
  213. DMAx->HIFCR = 0x003F0000U;
  214. }
  215. else if(Stream == LL_DMA_STREAM_7)
  216. {
  217. /* Reset the Stream7 pending flags */
  218. DMAx->HIFCR = 0x0F400000U;
  219. }
  220. else
  221. {
  222. status = ERROR;
  223. }
  224. }
  225. return status;
  226. }
  227. /**
  228. * @brief Initialize the DMA registers according to the specified parameters in DMA_InitStruct.
  229. * @note To convert DMAx_Streamy Instance to DMAx Instance and Streamy, use helper macros :
  230. * @arg @ref __LL_DMA_GET_INSTANCE
  231. * @arg @ref __LL_DMA_GET_STREAM
  232. * @param DMAx DMAx Instance
  233. * @param Stream This parameter can be one of the following values:
  234. * @arg @ref LL_DMA_STREAM_0
  235. * @arg @ref LL_DMA_STREAM_1
  236. * @arg @ref LL_DMA_STREAM_2
  237. * @arg @ref LL_DMA_STREAM_3
  238. * @arg @ref LL_DMA_STREAM_4
  239. * @arg @ref LL_DMA_STREAM_5
  240. * @arg @ref LL_DMA_STREAM_6
  241. * @arg @ref LL_DMA_STREAM_7
  242. * @param DMA_InitStruct pointer to a @ref LL_DMA_InitTypeDef structure.
  243. * @retval An ErrorStatus enumeration value:
  244. * - SUCCESS: DMA registers are initialized
  245. * - ERROR: Not applicable
  246. */
  247. uint32_t LL_DMA_Init(DMA_TypeDef *DMAx, uint32_t Stream, LL_DMA_InitTypeDef *DMA_InitStruct)
  248. {
  249. /* Check the DMA Instance DMAx and Stream parameters*/
  250. assert_param(IS_LL_DMA_ALL_STREAM_INSTANCE(DMAx, Stream));
  251. /* Check the DMA parameters from DMA_InitStruct */
  252. assert_param(IS_LL_DMA_DIRECTION(DMA_InitStruct->Direction));
  253. assert_param(IS_LL_DMA_MODE(DMA_InitStruct->Mode));
  254. assert_param(IS_LL_DMA_PERIPHINCMODE(DMA_InitStruct->PeriphOrM2MSrcIncMode));
  255. assert_param(IS_LL_DMA_MEMORYINCMODE(DMA_InitStruct->MemoryOrM2MDstIncMode));
  256. assert_param(IS_LL_DMA_PERIPHDATASIZE(DMA_InitStruct->PeriphOrM2MSrcDataSize));
  257. assert_param(IS_LL_DMA_MEMORYDATASIZE(DMA_InitStruct->MemoryOrM2MDstDataSize));
  258. assert_param(IS_LL_DMA_NBDATA(DMA_InitStruct->NbData));
  259. assert_param(IS_LL_DMA_CHANNEL(DMA_InitStruct->Channel));
  260. assert_param(IS_LL_DMA_PRIORITY(DMA_InitStruct->Priority));
  261. assert_param(IS_LL_DMA_FIFO_MODE_STATE(DMA_InitStruct->FIFOMode));
  262. /* Check the memory burst, peripheral burst and FIFO threshold parameters only
  263. when FIFO mode is enabled */
  264. if(DMA_InitStruct->FIFOMode != LL_DMA_FIFOMODE_DISABLE)
  265. {
  266. assert_param(IS_LL_DMA_FIFO_THRESHOLD(DMA_InitStruct->FIFOThreshold));
  267. assert_param(IS_LL_DMA_MEMORY_BURST(DMA_InitStruct->MemBurst));
  268. assert_param(IS_LL_DMA_PERIPHERAL_BURST(DMA_InitStruct->PeriphBurst));
  269. }
  270. /*---------------------------- DMAx SxCR Configuration ------------------------
  271. * Configure DMAx_Streamy: data transfer direction, data transfer mode,
  272. * peripheral and memory increment mode,
  273. * data size alignment and priority level with parameters :
  274. * - Direction: DMA_SxCR_DIR[1:0] bits
  275. * - Mode: DMA_SxCR_CIRC bit
  276. * - PeriphOrM2MSrcIncMode: DMA_SxCR_PINC bit
  277. * - MemoryOrM2MDstIncMode: DMA_SxCR_MINC bit
  278. * - PeriphOrM2MSrcDataSize: DMA_SxCR_PSIZE[1:0] bits
  279. * - MemoryOrM2MDstDataSize: DMA_SxCR_MSIZE[1:0] bits
  280. * - Priority: DMA_SxCR_PL[1:0] bits
  281. */
  282. LL_DMA_ConfigTransfer(DMAx, Stream, DMA_InitStruct->Direction | \
  283. DMA_InitStruct->Mode | \
  284. DMA_InitStruct->PeriphOrM2MSrcIncMode | \
  285. DMA_InitStruct->MemoryOrM2MDstIncMode | \
  286. DMA_InitStruct->PeriphOrM2MSrcDataSize | \
  287. DMA_InitStruct->MemoryOrM2MDstDataSize | \
  288. DMA_InitStruct->Priority
  289. );
  290. if(DMA_InitStruct->FIFOMode != LL_DMA_FIFOMODE_DISABLE)
  291. {
  292. /*---------------------------- DMAx SxFCR Configuration ------------------------
  293. * Configure DMAx_Streamy: fifo mode and fifo threshold with parameters :
  294. * - FIFOMode: DMA_SxFCR_DMDIS bit
  295. * - FIFOThreshold: DMA_SxFCR_FTH[1:0] bits
  296. */
  297. LL_DMA_ConfigFifo(DMAx, Stream, DMA_InitStruct->FIFOMode, DMA_InitStruct->FIFOThreshold);
  298. /*---------------------------- DMAx SxCR Configuration --------------------------
  299. * Configure DMAx_Streamy: memory burst transfer with parameters :
  300. * - MemBurst: DMA_SxCR_MBURST[1:0] bits
  301. */
  302. LL_DMA_SetMemoryBurstxfer(DMAx,Stream,DMA_InitStruct->MemBurst);
  303. /*---------------------------- DMAx SxCR Configuration --------------------------
  304. * Configure DMAx_Streamy: peripheral burst transfer with parameters :
  305. * - PeriphBurst: DMA_SxCR_PBURST[1:0] bits
  306. */
  307. LL_DMA_SetPeriphBurstxfer(DMAx,Stream,DMA_InitStruct->PeriphBurst);
  308. }
  309. /*-------------------------- DMAx SxM0AR Configuration --------------------------
  310. * Configure the memory or destination base address with parameter :
  311. * - MemoryOrM2MDstAddress: DMA_SxM0AR_M0A[31:0] bits
  312. */
  313. LL_DMA_SetMemoryAddress(DMAx, Stream, DMA_InitStruct->MemoryOrM2MDstAddress);
  314. /*-------------------------- DMAx SxPAR Configuration ---------------------------
  315. * Configure the peripheral or source base address with parameter :
  316. * - PeriphOrM2MSrcAddress: DMA_SxPAR_PA[31:0] bits
  317. */
  318. LL_DMA_SetPeriphAddress(DMAx, Stream, DMA_InitStruct->PeriphOrM2MSrcAddress);
  319. /*--------------------------- DMAx SxNDTR Configuration -------------------------
  320. * Configure the peripheral base address with parameter :
  321. * - NbData: DMA_SxNDT[15:0] bits
  322. */
  323. LL_DMA_SetDataLength(DMAx, Stream, DMA_InitStruct->NbData);
  324. /*--------------------------- DMA SxCR_CHSEL Configuration ----------------------
  325. * Configure the peripheral base address with parameter :
  326. * - PeriphRequest: DMA_SxCR_CHSEL[2:0] bits
  327. */
  328. LL_DMA_SetChannelSelection(DMAx, Stream, DMA_InitStruct->Channel);
  329. return SUCCESS;
  330. }
  331. /**
  332. * @brief Set each @ref LL_DMA_InitTypeDef field to default value.
  333. * @param DMA_InitStruct Pointer to a @ref LL_DMA_InitTypeDef structure.
  334. * @retval None
  335. */
  336. void LL_DMA_StructInit(LL_DMA_InitTypeDef *DMA_InitStruct)
  337. {
  338. /* Set DMA_InitStruct fields to default values */
  339. DMA_InitStruct->PeriphOrM2MSrcAddress = 0x00000000U;
  340. DMA_InitStruct->MemoryOrM2MDstAddress = 0x00000000U;
  341. DMA_InitStruct->Direction = LL_DMA_DIRECTION_PERIPH_TO_MEMORY;
  342. DMA_InitStruct->Mode = LL_DMA_MODE_NORMAL;
  343. DMA_InitStruct->PeriphOrM2MSrcIncMode = LL_DMA_PERIPH_NOINCREMENT;
  344. DMA_InitStruct->MemoryOrM2MDstIncMode = LL_DMA_MEMORY_NOINCREMENT;
  345. DMA_InitStruct->PeriphOrM2MSrcDataSize = LL_DMA_PDATAALIGN_BYTE;
  346. DMA_InitStruct->MemoryOrM2MDstDataSize = LL_DMA_MDATAALIGN_BYTE;
  347. DMA_InitStruct->NbData = 0x00000000U;
  348. DMA_InitStruct->Channel = LL_DMA_CHANNEL_0;
  349. DMA_InitStruct->Priority = LL_DMA_PRIORITY_LOW;
  350. DMA_InitStruct->FIFOMode = LL_DMA_FIFOMODE_DISABLE;
  351. DMA_InitStruct->FIFOThreshold = LL_DMA_FIFOTHRESHOLD_1_4;
  352. DMA_InitStruct->MemBurst = LL_DMA_MBURST_SINGLE;
  353. DMA_InitStruct->PeriphBurst = LL_DMA_PBURST_SINGLE;
  354. }
  355. /**
  356. * @}
  357. */
  358. /**
  359. * @}
  360. */
  361. /**
  362. * @}
  363. */
  364. #endif /* DMA1 || DMA2 */
  365. /**
  366. * @}
  367. */
  368. #endif /* USE_FULL_LL_DRIVER */
  369. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/