You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

424 lines
17 KiB

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