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.
 
 
 

444 lines
19 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_ll_dma.c
  4. * @author MCD Application Team
  5. * @version V1.7.1
  6. * @date 14-April-2017
  7. * @brief DMA LL module driver.
  8. ******************************************************************************
  9. * @attention
  10. *
  11. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  12. *
  13. * Redistribution and use in source and binary forms, with or without modification,
  14. * are permitted provided that the following conditions are met:
  15. * 1. Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright notice,
  18. * this list of conditions and the following disclaimer in the documentation
  19. * and/or other materials provided with the distribution.
  20. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  21. * may be used to endorse or promote products derived from this software
  22. * without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  27. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  28. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  30. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  31. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  32. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  33. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. ******************************************************************************
  36. */
  37. #if defined(USE_FULL_LL_DRIVER)
  38. /* Includes ------------------------------------------------------------------*/
  39. #include "stm32f4xx_ll_dma.h"
  40. #include "stm32f4xx_ll_bus.h"
  41. #ifdef USE_FULL_ASSERT
  42. #include "stm32_assert.h"
  43. #else
  44. #define assert_param(expr) ((void)0U)
  45. #endif
  46. /** @addtogroup STM32F4xx_LL_Driver
  47. * @{
  48. */
  49. #if defined (DMA1) || defined (DMA2)
  50. /** @defgroup DMA_LL DMA
  51. * @{
  52. */
  53. /* Private types -------------------------------------------------------------*/
  54. /* Private variables ---------------------------------------------------------*/
  55. /* Private constants ---------------------------------------------------------*/
  56. /* Private macros ------------------------------------------------------------*/
  57. /** @addtogroup DMA_LL_Private_Macros
  58. * @{
  59. */
  60. #define IS_LL_DMA_DIRECTION(__VALUE__) (((__VALUE__) == LL_DMA_DIRECTION_PERIPH_TO_MEMORY) || \
  61. ((__VALUE__) == LL_DMA_DIRECTION_MEMORY_TO_PERIPH) || \
  62. ((__VALUE__) == LL_DMA_DIRECTION_MEMORY_TO_MEMORY))
  63. #define IS_LL_DMA_MODE(__VALUE__) (((__VALUE__) == LL_DMA_MODE_NORMAL) || \
  64. ((__VALUE__) == LL_DMA_MODE_CIRCULAR) || \
  65. ((__VALUE__) == LL_DMA_MODE_PFCTRL))
  66. #define IS_LL_DMA_PERIPHINCMODE(__VALUE__) (((__VALUE__) == LL_DMA_PERIPH_INCREMENT) || \
  67. ((__VALUE__) == LL_DMA_PERIPH_NOINCREMENT))
  68. #define IS_LL_DMA_MEMORYINCMODE(__VALUE__) (((__VALUE__) == LL_DMA_MEMORY_INCREMENT) || \
  69. ((__VALUE__) == LL_DMA_MEMORY_NOINCREMENT))
  70. #define IS_LL_DMA_PERIPHDATASIZE(__VALUE__) (((__VALUE__) == LL_DMA_PDATAALIGN_BYTE) || \
  71. ((__VALUE__) == LL_DMA_PDATAALIGN_HALFWORD) || \
  72. ((__VALUE__) == LL_DMA_PDATAALIGN_WORD))
  73. #define IS_LL_DMA_MEMORYDATASIZE(__VALUE__) (((__VALUE__) == LL_DMA_MDATAALIGN_BYTE) || \
  74. ((__VALUE__) == LL_DMA_MDATAALIGN_HALFWORD) || \
  75. ((__VALUE__) == LL_DMA_MDATAALIGN_WORD))
  76. #define IS_LL_DMA_NBDATA(__VALUE__) ((__VALUE__) <= 0x0000FFFFU)
  77. #define IS_LL_DMA_CHANNEL(__VALUE__) (((__VALUE__) == LL_DMA_CHANNEL_0) || \
  78. ((__VALUE__) == LL_DMA_CHANNEL_1) || \
  79. ((__VALUE__) == LL_DMA_CHANNEL_2) || \
  80. ((__VALUE__) == LL_DMA_CHANNEL_3) || \
  81. ((__VALUE__) == LL_DMA_CHANNEL_4) || \
  82. ((__VALUE__) == LL_DMA_CHANNEL_5) || \
  83. ((__VALUE__) == LL_DMA_CHANNEL_6) || \
  84. ((__VALUE__) == LL_DMA_CHANNEL_7))
  85. #define IS_LL_DMA_PRIORITY(__VALUE__) (((__VALUE__) == LL_DMA_PRIORITY_LOW) || \
  86. ((__VALUE__) == LL_DMA_PRIORITY_MEDIUM) || \
  87. ((__VALUE__) == LL_DMA_PRIORITY_HIGH) || \
  88. ((__VALUE__) == LL_DMA_PRIORITY_VERYHIGH))
  89. #define IS_LL_DMA_ALL_STREAM_INSTANCE(INSTANCE, STREAM) ((((INSTANCE) == DMA1) && \
  90. (((STREAM) == LL_DMA_STREAM_0) || \
  91. ((STREAM) == LL_DMA_STREAM_1) || \
  92. ((STREAM) == LL_DMA_STREAM_2) || \
  93. ((STREAM) == LL_DMA_STREAM_3) || \
  94. ((STREAM) == LL_DMA_STREAM_4) || \
  95. ((STREAM) == LL_DMA_STREAM_5) || \
  96. ((STREAM) == LL_DMA_STREAM_6) || \
  97. ((STREAM) == LL_DMA_STREAM_7) || \
  98. ((STREAM) == LL_DMA_STREAM_ALL))) ||\
  99. (((INSTANCE) == DMA2) && \
  100. (((STREAM) == LL_DMA_STREAM_0) || \
  101. ((STREAM) == LL_DMA_STREAM_1) || \
  102. ((STREAM) == LL_DMA_STREAM_2) || \
  103. ((STREAM) == LL_DMA_STREAM_3) || \
  104. ((STREAM) == LL_DMA_STREAM_4) || \
  105. ((STREAM) == LL_DMA_STREAM_5) || \
  106. ((STREAM) == LL_DMA_STREAM_6) || \
  107. ((STREAM) == LL_DMA_STREAM_7) || \
  108. ((STREAM) == LL_DMA_STREAM_ALL))))
  109. #define IS_LL_DMA_FIFO_MODE_STATE(STATE) (((STATE) == LL_DMA_FIFOMODE_DISABLE ) || \
  110. ((STATE) == LL_DMA_FIFOMODE_ENABLE))
  111. #define IS_LL_DMA_FIFO_THRESHOLD(THRESHOLD) (((THRESHOLD) == LL_DMA_FIFOTHRESHOLD_1_4) || \
  112. ((THRESHOLD) == LL_DMA_FIFOTHRESHOLD_1_2) || \
  113. ((THRESHOLD) == LL_DMA_FIFOTHRESHOLD_3_4) || \
  114. ((THRESHOLD) == LL_DMA_FIFOTHRESHOLD_FULL))
  115. #define IS_LL_DMA_MEMORY_BURST(BURST) (((BURST) == LL_DMA_MBURST_SINGLE) || \
  116. ((BURST) == LL_DMA_MBURST_INC4) || \
  117. ((BURST) == LL_DMA_MBURST_INC8) || \
  118. ((BURST) == LL_DMA_MBURST_INC16))
  119. #define IS_LL_DMA_PERIPHERAL_BURST(BURST) (((BURST) == LL_DMA_PBURST_SINGLE) || \
  120. ((BURST) == LL_DMA_PBURST_INC4) || \
  121. ((BURST) == LL_DMA_PBURST_INC8) || \
  122. ((BURST) == LL_DMA_PBURST_INC16))
  123. /**
  124. * @}
  125. */
  126. /* Private function prototypes -----------------------------------------------*/
  127. /* Exported functions --------------------------------------------------------*/
  128. /** @addtogroup DMA_LL_Exported_Functions
  129. * @{
  130. */
  131. /** @addtogroup DMA_LL_EF_Init
  132. * @{
  133. */
  134. /**
  135. * @brief De-initialize the DMA registers to their default reset values.
  136. * @param DMAx DMAx Instance
  137. * @param Stream This parameter can be one of the following values:
  138. * @arg @ref LL_DMA_STREAM_0
  139. * @arg @ref LL_DMA_STREAM_1
  140. * @arg @ref LL_DMA_STREAM_2
  141. * @arg @ref LL_DMA_STREAM_3
  142. * @arg @ref LL_DMA_STREAM_4
  143. * @arg @ref LL_DMA_STREAM_5
  144. * @arg @ref LL_DMA_STREAM_6
  145. * @arg @ref LL_DMA_STREAM_7
  146. * @arg @ref LL_DMA_STREAM_ALL
  147. * @retval An ErrorStatus enumeration value:
  148. * - SUCCESS: DMA registers are de-initialized
  149. * - ERROR: DMA registers are not de-initialized
  150. */
  151. uint32_t LL_DMA_DeInit(DMA_TypeDef *DMAx, uint32_t Stream)
  152. {
  153. DMA_Stream_TypeDef *tmp = (DMA_Stream_TypeDef *)DMA1_Stream0;
  154. ErrorStatus status = SUCCESS;
  155. /* Check the DMA Instance DMAx and Stream parameters*/
  156. assert_param(IS_LL_DMA_ALL_STREAM_INSTANCE(DMAx, Stream));
  157. if (Stream == LL_DMA_STREAM_ALL)
  158. {
  159. if (DMAx == DMA1)
  160. {
  161. /* Force reset of DMA clock */
  162. LL_AHB1_GRP1_ForceReset(LL_AHB1_GRP1_PERIPH_DMA1);
  163. /* Release reset of DMA clock */
  164. LL_AHB1_GRP1_ReleaseReset(LL_AHB1_GRP1_PERIPH_DMA1);
  165. }
  166. else if (DMAx == DMA2)
  167. {
  168. /* Force reset of DMA clock */
  169. LL_AHB1_GRP1_ForceReset(LL_AHB1_GRP1_PERIPH_DMA2);
  170. /* Release reset of DMA clock */
  171. LL_AHB1_GRP1_ReleaseReset(LL_AHB1_GRP1_PERIPH_DMA2);
  172. }
  173. else
  174. {
  175. status = ERROR;
  176. }
  177. }
  178. else
  179. {
  180. /* Disable the selected Stream */
  181. LL_DMA_DisableStream(DMAx,Stream);
  182. /* Get the DMA Stream Instance */
  183. tmp = (DMA_Stream_TypeDef *)(__LL_DMA_GET_STREAM_INSTANCE(DMAx, Stream));
  184. /* Reset DMAx_Streamy configuration register */
  185. LL_DMA_WriteReg(tmp, CR, 0U);
  186. /* Reset DMAx_Streamy remaining bytes register */
  187. LL_DMA_WriteReg(tmp, NDTR, 0U);
  188. /* Reset DMAx_Streamy peripheral address register */
  189. LL_DMA_WriteReg(tmp, PAR, 0U);
  190. /* Reset DMAx_Streamy memory address register */
  191. LL_DMA_WriteReg(tmp, M0AR, 0U);
  192. /* Reset DMAx_Streamy memory address register */
  193. LL_DMA_WriteReg(tmp, M1AR, 0U);
  194. /* Reset DMAx_Streamy FIFO control register */
  195. LL_DMA_WriteReg(tmp, FCR, 0x00000021U);
  196. /* Reset Channel register field for DMAx Stream*/
  197. LL_DMA_SetChannelSelection(DMAx, Stream, LL_DMA_CHANNEL_0);
  198. if(Stream == LL_DMA_STREAM_0)
  199. {
  200. /* Reset the Stream0 pending flags */
  201. DMAx->LIFCR = 0x0000003FU;
  202. }
  203. else if(Stream == LL_DMA_STREAM_1)
  204. {
  205. /* Reset the Stream1 pending flags */
  206. DMAx->LIFCR = 0x00000F40U;
  207. }
  208. else if(Stream == LL_DMA_STREAM_2)
  209. {
  210. /* Reset the Stream2 pending flags */
  211. DMAx->LIFCR = 0x003F0000U;
  212. }
  213. else if(Stream == LL_DMA_STREAM_3)
  214. {
  215. /* Reset the Stream3 pending flags */
  216. DMAx->LIFCR = 0x0F400000U;
  217. }
  218. else if(Stream == LL_DMA_STREAM_4)
  219. {
  220. /* Reset the Stream4 pending flags */
  221. DMAx->HIFCR = 0x0000003FU;
  222. }
  223. else if(Stream == LL_DMA_STREAM_5)
  224. {
  225. /* Reset the Stream5 pending flags */
  226. DMAx->HIFCR = 0x00000F40U;
  227. }
  228. else if(Stream == LL_DMA_STREAM_6)
  229. {
  230. /* Reset the Stream6 pending flags */
  231. DMAx->HIFCR = 0x003F0000U;
  232. }
  233. else if(Stream == LL_DMA_STREAM_7)
  234. {
  235. /* Reset the Stream7 pending flags */
  236. DMAx->HIFCR = 0x0F400000U;
  237. }
  238. else
  239. {
  240. status = ERROR;
  241. }
  242. }
  243. return status;
  244. }
  245. /**
  246. * @brief Initialize the DMA registers according to the specified parameters in DMA_InitStruct.
  247. * @note To convert DMAx_Streamy Instance to DMAx Instance and Streamy, use helper macros :
  248. * @arg @ref __LL_DMA_GET_INSTANCE
  249. * @arg @ref __LL_DMA_GET_STREAM
  250. * @param DMAx DMAx Instance
  251. * @param Stream This parameter can be one of the following values:
  252. * @arg @ref LL_DMA_STREAM_0
  253. * @arg @ref LL_DMA_STREAM_1
  254. * @arg @ref LL_DMA_STREAM_2
  255. * @arg @ref LL_DMA_STREAM_3
  256. * @arg @ref LL_DMA_STREAM_4
  257. * @arg @ref LL_DMA_STREAM_5
  258. * @arg @ref LL_DMA_STREAM_6
  259. * @arg @ref LL_DMA_STREAM_7
  260. * @param DMA_InitStruct pointer to a @ref LL_DMA_InitTypeDef structure.
  261. * @retval An ErrorStatus enumeration value:
  262. * - SUCCESS: DMA registers are initialized
  263. * - ERROR: Not applicable
  264. */
  265. uint32_t LL_DMA_Init(DMA_TypeDef *DMAx, uint32_t Stream, LL_DMA_InitTypeDef *DMA_InitStruct)
  266. {
  267. /* Check the DMA Instance DMAx and Stream parameters*/
  268. assert_param(IS_LL_DMA_ALL_STREAM_INSTANCE(DMAx, Stream));
  269. /* Check the DMA parameters from DMA_InitStruct */
  270. assert_param(IS_LL_DMA_DIRECTION(DMA_InitStruct->Direction));
  271. assert_param(IS_LL_DMA_MODE(DMA_InitStruct->Mode));
  272. assert_param(IS_LL_DMA_PERIPHINCMODE(DMA_InitStruct->PeriphOrM2MSrcIncMode));
  273. assert_param(IS_LL_DMA_MEMORYINCMODE(DMA_InitStruct->MemoryOrM2MDstIncMode));
  274. assert_param(IS_LL_DMA_PERIPHDATASIZE(DMA_InitStruct->PeriphOrM2MSrcDataSize));
  275. assert_param(IS_LL_DMA_MEMORYDATASIZE(DMA_InitStruct->MemoryOrM2MDstDataSize));
  276. assert_param(IS_LL_DMA_NBDATA(DMA_InitStruct->NbData));
  277. assert_param(IS_LL_DMA_CHANNEL(DMA_InitStruct->Channel));
  278. assert_param(IS_LL_DMA_PRIORITY(DMA_InitStruct->Priority));
  279. assert_param(IS_LL_DMA_FIFO_MODE_STATE(DMA_InitStruct->FIFOMode));
  280. /* Check the memory burst, peripheral burst and FIFO threshold parameters only
  281. when FIFO mode is enabled */
  282. if(DMA_InitStruct->FIFOMode != LL_DMA_FIFOMODE_DISABLE)
  283. {
  284. assert_param(IS_LL_DMA_FIFO_THRESHOLD(DMA_InitStruct->FIFOThreshold));
  285. assert_param(IS_LL_DMA_MEMORY_BURST(DMA_InitStruct->MemBurst));
  286. assert_param(IS_LL_DMA_PERIPHERAL_BURST(DMA_InitStruct->PeriphBurst));
  287. }
  288. /*---------------------------- DMAx SxCR Configuration ------------------------
  289. * Configure DMAx_Streamy: data transfer direction, data transfer mode,
  290. * peripheral and memory increment mode,
  291. * data size alignment and priority level with parameters :
  292. * - Direction: DMA_SxCR_DIR[1:0] bits
  293. * - Mode: DMA_SxCR_CIRC bit
  294. * - PeriphOrM2MSrcIncMode: DMA_SxCR_PINC bit
  295. * - MemoryOrM2MDstIncMode: DMA_SxCR_MINC bit
  296. * - PeriphOrM2MSrcDataSize: DMA_SxCR_PSIZE[1:0] bits
  297. * - MemoryOrM2MDstDataSize: DMA_SxCR_MSIZE[1:0] bits
  298. * - Priority: DMA_SxCR_PL[1:0] bits
  299. */
  300. LL_DMA_ConfigTransfer(DMAx, Stream, DMA_InitStruct->Direction | \
  301. DMA_InitStruct->Mode | \
  302. DMA_InitStruct->PeriphOrM2MSrcIncMode | \
  303. DMA_InitStruct->MemoryOrM2MDstIncMode | \
  304. DMA_InitStruct->PeriphOrM2MSrcDataSize | \
  305. DMA_InitStruct->MemoryOrM2MDstDataSize | \
  306. DMA_InitStruct->Priority
  307. );
  308. if(DMA_InitStruct->FIFOMode != LL_DMA_FIFOMODE_DISABLE)
  309. {
  310. /*---------------------------- DMAx SxFCR Configuration ------------------------
  311. * Configure DMAx_Streamy: fifo mode and fifo threshold with parameters :
  312. * - FIFOMode: DMA_SxFCR_DMDIS bit
  313. * - FIFOThreshold: DMA_SxFCR_FTH[1:0] bits
  314. */
  315. LL_DMA_ConfigFifo(DMAx, Stream, DMA_InitStruct->FIFOMode, DMA_InitStruct->FIFOThreshold);
  316. /*---------------------------- DMAx SxCR Configuration --------------------------
  317. * Configure DMAx_Streamy: memory burst transfer with parameters :
  318. * - MemBurst: DMA_SxCR_MBURST[1:0] bits
  319. */
  320. LL_DMA_SetMemoryBurstxfer(DMAx,Stream,DMA_InitStruct->MemBurst);
  321. /*---------------------------- DMAx SxCR Configuration --------------------------
  322. * Configure DMAx_Streamy: peripheral burst transfer with parameters :
  323. * - PeriphBurst: DMA_SxCR_PBURST[1:0] bits
  324. */
  325. LL_DMA_SetPeriphBurstxfer(DMAx,Stream,DMA_InitStruct->PeriphBurst);
  326. }
  327. /*-------------------------- DMAx SxM0AR Configuration --------------------------
  328. * Configure the memory or destination base address with parameter :
  329. * - MemoryOrM2MDstAddress: DMA_SxM0AR_M0A[31:0] bits
  330. */
  331. LL_DMA_SetMemoryAddress(DMAx, Stream, DMA_InitStruct->MemoryOrM2MDstAddress);
  332. /*-------------------------- DMAx SxPAR Configuration ---------------------------
  333. * Configure the peripheral or source base address with parameter :
  334. * - PeriphOrM2MSrcAddress: DMA_SxPAR_PA[31:0] bits
  335. */
  336. LL_DMA_SetPeriphAddress(DMAx, Stream, DMA_InitStruct->PeriphOrM2MSrcAddress);
  337. /*--------------------------- DMAx SxNDTR Configuration -------------------------
  338. * Configure the peripheral base address with parameter :
  339. * - NbData: DMA_SxNDT[15:0] bits
  340. */
  341. LL_DMA_SetDataLength(DMAx, Stream, DMA_InitStruct->NbData);
  342. /*--------------------------- DMA SxCR_CHSEL Configuration ----------------------
  343. * Configure the peripheral base address with parameter :
  344. * - PeriphRequest: DMA_SxCR_CHSEL[2:0] bits
  345. */
  346. LL_DMA_SetChannelSelection(DMAx, Stream, DMA_InitStruct->Channel);
  347. return SUCCESS;
  348. }
  349. /**
  350. * @brief Set each @ref LL_DMA_InitTypeDef field to default value.
  351. * @param DMA_InitStruct Pointer to a @ref LL_DMA_InitTypeDef structure.
  352. * @retval None
  353. */
  354. void LL_DMA_StructInit(LL_DMA_InitTypeDef *DMA_InitStruct)
  355. {
  356. /* Set DMA_InitStruct fields to default values */
  357. DMA_InitStruct->PeriphOrM2MSrcAddress = 0x00000000U;
  358. DMA_InitStruct->MemoryOrM2MDstAddress = 0x00000000U;
  359. DMA_InitStruct->Direction = LL_DMA_DIRECTION_PERIPH_TO_MEMORY;
  360. DMA_InitStruct->Mode = LL_DMA_MODE_NORMAL;
  361. DMA_InitStruct->PeriphOrM2MSrcIncMode = LL_DMA_PERIPH_NOINCREMENT;
  362. DMA_InitStruct->MemoryOrM2MDstIncMode = LL_DMA_MEMORY_NOINCREMENT;
  363. DMA_InitStruct->PeriphOrM2MSrcDataSize = LL_DMA_PDATAALIGN_BYTE;
  364. DMA_InitStruct->MemoryOrM2MDstDataSize = LL_DMA_MDATAALIGN_BYTE;
  365. DMA_InitStruct->NbData = 0x00000000U;
  366. DMA_InitStruct->Channel = LL_DMA_CHANNEL_0;
  367. DMA_InitStruct->Priority = LL_DMA_PRIORITY_LOW;
  368. DMA_InitStruct->FIFOMode = LL_DMA_FIFOMODE_DISABLE;
  369. DMA_InitStruct->FIFOThreshold = LL_DMA_FIFOTHRESHOLD_1_4;
  370. DMA_InitStruct->MemBurst = LL_DMA_MBURST_SINGLE;
  371. DMA_InitStruct->PeriphBurst = LL_DMA_PBURST_SINGLE;
  372. }
  373. /**
  374. * @}
  375. */
  376. /**
  377. * @}
  378. */
  379. /**
  380. * @}
  381. */
  382. #endif /* DMA1 || DMA2 */
  383. /**
  384. * @}
  385. */
  386. #endif /* USE_FULL_LL_DRIVER */
  387. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/