stm32h7xx_ll_dma.c 18 KB

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