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.
 
 
 

1119 lines
34 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32wbxx_hal_dma.c
  4. * @author MCD Application Team
  5. * @brief DMA HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the Direct Memory Access (DMA) peripheral:
  8. * + Initialization and de-initialization functions
  9. * + IO operation functions
  10. * + Peripheral State and errors functions
  11. @verbatim
  12. ==============================================================================
  13. ##### How to use this driver #####
  14. ==============================================================================
  15. [..]
  16. (#) Enable and configure the peripheral to be connected to the DMA Channel
  17. (except for internal SRAM / FLASH memories: no initialization is
  18. necessary). Please refer to the Reference manual for connection between peripherals
  19. and DMA requests.
  20. (#) For a given Channel, program the required configuration through the following parameters:
  21. Channel request, Transfer Direction, Source and Destination data formats,
  22. Circular or Normal mode, Channel Priority level, Source and Destination Increment mode
  23. using HAL_DMA_Init() function.
  24. Prior to HAL_DMA_Init the peripheral clock shall be enabled for both DMA & DMAMUX
  25. thanks to:
  26. (##) DMA1 or DMA2: __HAL_RCC_DMA1_CLK_ENABLE() or __HAL_RCC_DMA2_CLK_ENABLE() ;
  27. (##) DMAMUX1: __HAL_RCC_DMAMUX1_CLK_ENABLE();
  28. (#) Use HAL_DMA_GetState() function to return the DMA state and HAL_DMA_GetError() in case of error
  29. detection.
  30. (#) Use HAL_DMA_Abort() function to abort the current transfer
  31. -@- In Memory-to-Memory transfer mode, Circular mode is not allowed.
  32. *** Polling mode IO operation ***
  33. =================================
  34. [..]
  35. (+) Use HAL_DMA_Start() to start DMA transfer after the configuration of Source
  36. address and destination address and the Length of data to be transferred
  37. (+) Use HAL_DMA_PollForTransfer() to poll for the end of current transfer, in this
  38. case a fixed Timeout can be configured by User depending from his application.
  39. *** Interrupt mode IO operation ***
  40. ===================================
  41. [..]
  42. (+) Configure the DMA interrupt priority using HAL_NVIC_SetPriority()
  43. (+) Enable the DMA IRQ handler using HAL_NVIC_EnableIRQ()
  44. (+) Use HAL_DMA_Start_IT() to start DMA transfer after the configuration of
  45. Source address and destination address and the Length of data to be transferred.
  46. In this case the DMA interrupt is configured
  47. (+) Use HAL_DMA_IRQHandler() called under DMA_IRQHandler() Interrupt subroutine
  48. (+) At the end of data transfer HAL_DMA_IRQHandler() function is executed and user can
  49. add his own function to register callbacks with HAL_DMA_RegisterCallback().
  50. *** DMA HAL driver macros list ***
  51. =============================================
  52. [..]
  53. Below the list of macros in DMA HAL driver.
  54. (+) __HAL_DMA_ENABLE: Enable the specified DMA Channel.
  55. (+) __HAL_DMA_DISABLE: Disable the specified DMA Channel.
  56. (+) __HAL_DMA_GET_FLAG: Get the DMA Channel pending flags.
  57. (+) __HAL_DMA_CLEAR_FLAG: Clear the DMA Channel pending flags.
  58. (+) __HAL_DMA_ENABLE_IT: Enable the specified DMA Channel interrupts.
  59. (+) __HAL_DMA_DISABLE_IT: Disable the specified DMA Channel interrupts.
  60. (+) __HAL_DMA_GET_IT_SOURCE: Check whether the specified DMA Channel interrupt is enabled or not.
  61. [..]
  62. (@) You can refer to the DMA HAL driver header file for more useful macros
  63. @endverbatim
  64. ******************************************************************************
  65. * @attention
  66. *
  67. * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
  68. * All rights reserved.</center></h2>
  69. *
  70. * This software component is licensed by ST under BSD 3-Clause license,
  71. * the "License"; You may not use this file except in compliance with the
  72. * License. You may obtain a copy of the License at:
  73. * opensource.org/licenses/BSD-3-Clause
  74. *
  75. ******************************************************************************
  76. */
  77. /* Includes ------------------------------------------------------------------*/
  78. #include "stm32wbxx_hal.h"
  79. /** @addtogroup STM32WBxx_HAL_Driver
  80. * @{
  81. */
  82. /** @defgroup DMA DMA
  83. * @brief DMA HAL module driver
  84. * @{
  85. */
  86. #ifdef HAL_DMA_MODULE_ENABLED
  87. /* Private typedef -----------------------------------------------------------*/
  88. /* Private define ------------------------------------------------------------*/
  89. /* Private macro -------------------------------------------------------------*/
  90. /* Private variables ---------------------------------------------------------*/
  91. /* Private function prototypes -----------------------------------------------*/
  92. /** @defgroup DMA_Private_Functions DMA Private Functions
  93. * @{
  94. */
  95. static void DMA_SetConfig(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength);
  96. static void DMA_CalcDMAMUXChannelBaseAndMask(DMA_HandleTypeDef *hdma);
  97. static void DMA_CalcDMAMUXRequestGenBaseAndMask(DMA_HandleTypeDef *hdma);
  98. /**
  99. * @}
  100. */
  101. /* Exported functions ---------------------------------------------------------*/
  102. /** @defgroup DMA_Exported_Functions DMA Exported Functions
  103. * @{
  104. */
  105. /** @defgroup DMA_Exported_Functions_Group1 Initialization and de-initialization functions
  106. * @brief Initialization and de-initialization functions
  107. *
  108. @verbatim
  109. ===============================================================================
  110. ##### Initialization and de-initialization functions #####
  111. ===============================================================================
  112. [..]
  113. This section provides functions allowing to initialize the DMA Channel source
  114. and destination addresses, incrementation and data sizes, transfer direction,
  115. circular/normal mode selection, memory-to-memory mode selection and Channel priority value.
  116. [..]
  117. The HAL_DMA_Init() function follows the DMA configuration procedures as described in
  118. reference manual.
  119. @endverbatim
  120. * @{
  121. */
  122. /**
  123. * @brief Initialize the DMA according to the specified
  124. * parameters in the DMA_InitTypeDef and initialize the associated handle.
  125. * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
  126. * the configuration information for the specified DMA Channel.
  127. * @retval HAL status
  128. */
  129. HAL_StatusTypeDef HAL_DMA_Init(DMA_HandleTypeDef *hdma)
  130. {
  131. uint32_t tmp;
  132. /* Check the DMA handle allocation */
  133. if (hdma == NULL)
  134. {
  135. return HAL_ERROR;
  136. }
  137. /* Check the parameters */
  138. assert_param(IS_DMA_ALL_INSTANCE(hdma->Instance));
  139. assert_param(IS_DMA_DIRECTION(hdma->Init.Direction));
  140. assert_param(IS_DMA_PERIPHERAL_INC_STATE(hdma->Init.PeriphInc));
  141. assert_param(IS_DMA_MEMORY_INC_STATE(hdma->Init.MemInc));
  142. assert_param(IS_DMA_PERIPHERAL_DATA_SIZE(hdma->Init.PeriphDataAlignment));
  143. assert_param(IS_DMA_MEMORY_DATA_SIZE(hdma->Init.MemDataAlignment));
  144. assert_param(IS_DMA_MODE(hdma->Init.Mode));
  145. assert_param(IS_DMA_PRIORITY(hdma->Init.Priority));
  146. assert_param(IS_DMA_ALL_REQUEST(hdma->Init.Request));
  147. #if defined(DMA2)
  148. /* Compute the channel index */
  149. if ((uint32_t)(hdma->Instance) < (uint32_t)(DMA2_Channel1))
  150. {
  151. /* DMA1 */
  152. hdma->ChannelIndex = (((uint32_t)hdma->Instance - (uint32_t)DMA1_Channel1) / ((uint32_t)DMA1_Channel2 - (uint32_t)DMA1_Channel1)) << 2U;
  153. hdma->DmaBaseAddress = DMA1;
  154. }
  155. else
  156. {
  157. /* DMA2 */
  158. hdma->ChannelIndex = (((uint32_t)hdma->Instance - (uint32_t)DMA2_Channel1) / ((uint32_t)DMA2_Channel2 - (uint32_t)DMA2_Channel1)) << 2U;
  159. hdma->DmaBaseAddress = DMA2;
  160. }
  161. #else
  162. /* DMA1 */
  163. hdma->ChannelIndex = (((uint32_t)hdma->Instance - (uint32_t)DMA1_Channel1) / ((uint32_t)DMA1_Channel2 - (uint32_t)DMA1_Channel1)) << 2U;
  164. hdma->DmaBaseAddress = DMA1;
  165. #endif
  166. /* Change DMA peripheral state */
  167. hdma->State = HAL_DMA_STATE_BUSY;
  168. /* Get the CR register value */
  169. tmp = hdma->Instance->CCR;
  170. /* Clear PL, MSIZE, PSIZE, MINC, PINC, CIRC, DIR and MEM2MEM bits */
  171. tmp &= ((uint32_t)~(DMA_CCR_PL | DMA_CCR_MSIZE | DMA_CCR_PSIZE |
  172. DMA_CCR_MINC | DMA_CCR_PINC | DMA_CCR_CIRC |
  173. DMA_CCR_DIR | DMA_CCR_MEM2MEM));
  174. /* Prepare the DMA Channel configuration */
  175. tmp |= hdma->Init.Direction |
  176. hdma->Init.PeriphInc | hdma->Init.MemInc |
  177. hdma->Init.PeriphDataAlignment | hdma->Init.MemDataAlignment |
  178. hdma->Init.Mode | hdma->Init.Priority;
  179. /* Write to DMA Channel CR register */
  180. hdma->Instance->CCR = tmp;
  181. /* Initialize parameters for DMAMUX channel :
  182. DMAmuxChannel, DMAmuxChannelStatus and DMAmuxChannelStatusMask
  183. */
  184. DMA_CalcDMAMUXChannelBaseAndMask(hdma);
  185. if (hdma->Init.Direction == DMA_MEMORY_TO_MEMORY)
  186. {
  187. /* if memory to memory force the request to 0*/
  188. hdma->Init.Request = DMA_REQUEST_MEM2MEM;
  189. }
  190. /* Set peripheral request to DMAMUX channel */
  191. hdma->DMAmuxChannel->CCR = (hdma->Init.Request & DMAMUX_CxCR_DMAREQ_ID);
  192. /* Clear the DMAMUX synchro overrun flag */
  193. hdma->DMAmuxChannelStatus->CFR = hdma->DMAmuxChannelStatusMask;
  194. if (((hdma->Init.Request > 0U) && (hdma->Init.Request <= DMA_REQUEST_GENERATOR3)))
  195. {
  196. /* Initialize parameters for DMAMUX request generator :
  197. DMAmuxRequestGen, DMAmuxRequestGenStatus and DMAmuxRequestGenStatusMask
  198. */
  199. DMA_CalcDMAMUXRequestGenBaseAndMask(hdma);
  200. /* Reset the DMAMUX request generator register*/
  201. hdma->DMAmuxRequestGen->RGCR = 0U;
  202. /* Clear the DMAMUX request generator overrun flag */
  203. hdma->DMAmuxRequestGenStatus->RGCFR = hdma->DMAmuxRequestGenStatusMask;
  204. }
  205. else
  206. {
  207. hdma->DMAmuxRequestGen = 0U;
  208. hdma->DMAmuxRequestGenStatus = 0U;
  209. hdma->DMAmuxRequestGenStatusMask = 0U;
  210. }
  211. /* Initialize the error code */
  212. hdma->ErrorCode = HAL_DMA_ERROR_NONE;
  213. /* Initialize the DMA state*/
  214. hdma->State = HAL_DMA_STATE_READY;
  215. /* Allocate lock resource and initialize it */
  216. hdma->Lock = HAL_UNLOCKED;
  217. return HAL_OK;
  218. }
  219. /**
  220. * @brief DeInitialize the DMA peripheral.
  221. * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
  222. * the configuration information for the specified DMA Channel.
  223. * @retval HAL status
  224. */
  225. HAL_StatusTypeDef HAL_DMA_DeInit(DMA_HandleTypeDef *hdma)
  226. {
  227. /* Check the DMA handle allocation */
  228. if (NULL == hdma)
  229. {
  230. return HAL_ERROR;
  231. }
  232. /* Check the parameters */
  233. assert_param(IS_DMA_ALL_INSTANCE(hdma->Instance));
  234. /* Disable the selected DMA Channelx */
  235. __HAL_DMA_DISABLE(hdma);
  236. #if defined(DMA2)
  237. /* Compute the channel index */
  238. if ((uint32_t)(hdma->Instance) < (uint32_t)(DMA2_Channel1))
  239. {
  240. /* DMA1 */
  241. hdma->ChannelIndex = (((uint32_t)hdma->Instance - (uint32_t)DMA1_Channel1) / ((uint32_t)DMA1_Channel2 - (uint32_t)DMA1_Channel1)) << 2U;
  242. hdma->DmaBaseAddress = DMA1;
  243. }
  244. else
  245. {
  246. /* DMA2 */
  247. hdma->ChannelIndex = (((uint32_t)hdma->Instance - (uint32_t)DMA2_Channel1) / ((uint32_t)DMA2_Channel2 - (uint32_t)DMA2_Channel1)) << 2U;
  248. hdma->DmaBaseAddress = DMA2;
  249. }
  250. #else
  251. /* DMA1 */
  252. hdma->ChannelIndex = (((uint32_t)hdma->Instance - (uint32_t)DMA1_Channel1) / ((uint32_t)DMA1_Channel2 - (uint32_t)DMA1_Channel1)) << 2U;
  253. hdma->DmaBaseAddress = DMA1;
  254. #endif
  255. /* Reset DMA Channel control register */
  256. hdma->Instance->CCR = 0U;
  257. /* Clear all flags */
  258. hdma->DmaBaseAddress->IFCR = (DMA_ISR_GIF1 << (hdma->ChannelIndex & 0x1cU));
  259. /* Initialize parameters for DMAMUX channel :
  260. DMAmuxChannel, DMAmuxChannelStatus and DMAmuxChannelStatusMask */
  261. DMA_CalcDMAMUXChannelBaseAndMask(hdma);
  262. /* Reset the DMAMUX channel that corresponds to the DMA channel */
  263. hdma->DMAmuxChannel->CCR = 0U;
  264. /* Clear the DMAMUX synchro overrun flag */
  265. hdma->DMAmuxChannelStatus->CFR = hdma->DMAmuxChannelStatusMask;
  266. /* Reset Request generator parameters if any */
  267. if (((hdma->Init.Request > 0U) && (hdma->Init.Request <= DMA_REQUEST_GENERATOR3)))
  268. {
  269. /* Initialize parameters for DMAMUX request generator :
  270. DMAmuxRequestGen, DMAmuxRequestGenStatus and DMAmuxRequestGenStatusMask
  271. */
  272. DMA_CalcDMAMUXRequestGenBaseAndMask(hdma);
  273. /* Reset the DMAMUX request generator register*/
  274. hdma->DMAmuxRequestGen->RGCR = 0U;
  275. /* Clear the DMAMUX request generator overrun flag */
  276. hdma->DMAmuxRequestGenStatus->RGCFR = hdma->DMAmuxRequestGenStatusMask;
  277. }
  278. hdma->DMAmuxRequestGen = 0U;
  279. hdma->DMAmuxRequestGenStatus = 0U;
  280. hdma->DMAmuxRequestGenStatusMask = 0U;
  281. /* Clean callbacks */
  282. hdma->XferCpltCallback = NULL;
  283. hdma->XferHalfCpltCallback = NULL;
  284. hdma->XferErrorCallback = NULL;
  285. hdma->XferAbortCallback = NULL;
  286. /* Initialize the error code */
  287. hdma->ErrorCode = HAL_DMA_ERROR_NONE;
  288. /* Initialize the DMA state */
  289. hdma->State = HAL_DMA_STATE_RESET;
  290. /* Release Lock */
  291. __HAL_UNLOCK(hdma);
  292. return HAL_OK;
  293. }
  294. /**
  295. * @}
  296. */
  297. /** @defgroup DMA_Exported_Functions_Group2 Input and Output operation functions
  298. * @brief Input and Output operation functions
  299. *
  300. @verbatim
  301. ===============================================================================
  302. ##### IO operation functions #####
  303. ===============================================================================
  304. [..] This section provides functions allowing to:
  305. (+) Configure the source, destination address and data length and Start DMA transfer
  306. (+) Configure the source, destination address and data length and
  307. Start DMA transfer with interrupt
  308. (+) Abort DMA transfer
  309. (+) Poll for transfer complete
  310. (+) Handle DMA interrupt request
  311. @endverbatim
  312. * @{
  313. */
  314. /**
  315. * @brief Start the DMA Transfer.
  316. * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
  317. * the configuration information for the specified DMA Channel.
  318. * @param SrcAddress The source memory Buffer address
  319. * @param DstAddress The destination memory Buffer address
  320. * @param DataLength The length of data to be transferred from source to destination
  321. * @retval HAL status
  322. */
  323. HAL_StatusTypeDef HAL_DMA_Start(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength)
  324. {
  325. HAL_StatusTypeDef status = HAL_OK;
  326. /* Check the parameters */
  327. assert_param(IS_DMA_BUFFER_SIZE(DataLength));
  328. /* Process locked */
  329. __HAL_LOCK(hdma);
  330. if (HAL_DMA_STATE_READY == hdma->State)
  331. {
  332. /* Change DMA peripheral state */
  333. hdma->State = HAL_DMA_STATE_BUSY;
  334. hdma->ErrorCode = HAL_DMA_ERROR_NONE;
  335. /* Disable the peripheral */
  336. __HAL_DMA_DISABLE(hdma);
  337. /* Configure the source, destination address and the data length & clear flags*/
  338. DMA_SetConfig(hdma, SrcAddress, DstAddress, DataLength);
  339. /* Enable the Peripheral */
  340. __HAL_DMA_ENABLE(hdma);
  341. }
  342. else
  343. {
  344. /* Process Unlocked */
  345. __HAL_UNLOCK(hdma);
  346. status = HAL_BUSY;
  347. }
  348. return status;
  349. }
  350. /**
  351. * @brief Start the DMA Transfer with interrupt enabled.
  352. * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
  353. * the configuration information for the specified DMA Channel.
  354. * @param SrcAddress The source memory Buffer address
  355. * @param DstAddress The destination memory Buffer address
  356. * @param DataLength The length of data to be transferred from source to destination
  357. * @retval HAL status
  358. */
  359. HAL_StatusTypeDef HAL_DMA_Start_IT(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength)
  360. {
  361. HAL_StatusTypeDef status = HAL_OK;
  362. /* Check the parameters */
  363. assert_param(IS_DMA_BUFFER_SIZE(DataLength));
  364. /* Process locked */
  365. __HAL_LOCK(hdma);
  366. if (HAL_DMA_STATE_READY == hdma->State)
  367. {
  368. /* Change DMA peripheral state */
  369. hdma->State = HAL_DMA_STATE_BUSY;
  370. hdma->ErrorCode = HAL_DMA_ERROR_NONE;
  371. /* Disable the peripheral */
  372. __HAL_DMA_DISABLE(hdma);
  373. /* Configure the source, destination address and the data length & clear flags*/
  374. DMA_SetConfig(hdma, SrcAddress, DstAddress, DataLength);
  375. /* Enable the transfer complete interrupt */
  376. /* Enable the transfer Error interrupt */
  377. if (NULL != hdma->XferHalfCpltCallback)
  378. {
  379. /* Enable the Half transfer complete interrupt as well */
  380. __HAL_DMA_ENABLE_IT(hdma, (DMA_IT_TC | DMA_IT_HT | DMA_IT_TE));
  381. }
  382. else
  383. {
  384. __HAL_DMA_DISABLE_IT(hdma, DMA_IT_HT);
  385. __HAL_DMA_ENABLE_IT(hdma, (DMA_IT_TC | DMA_IT_TE));
  386. }
  387. /* Check if DMAMUX Synchronization is enabled*/
  388. if ((hdma->DMAmuxChannel->CCR & DMAMUX_CxCR_SE) != 0U)
  389. {
  390. /* Enable DMAMUX sync overrun IT*/
  391. hdma->DMAmuxChannel->CCR |= DMAMUX_CxCR_SOIE;
  392. }
  393. if (hdma->DMAmuxRequestGen != 0U)
  394. {
  395. /* if using DMAMUX request generator, enable the DMAMUX request generator overrun IT*/
  396. /* enable the request gen overrun IT*/
  397. hdma->DMAmuxRequestGen->RGCR |= DMAMUX_RGxCR_OIE;
  398. }
  399. /* Enable the Peripheral */
  400. __HAL_DMA_ENABLE(hdma);
  401. }
  402. else
  403. {
  404. /* Process Unlocked */
  405. __HAL_UNLOCK(hdma);
  406. /* Remain BUSY */
  407. status = HAL_BUSY;
  408. }
  409. return status;
  410. }
  411. /**
  412. * @brief Abort the DMA Transfer.
  413. * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
  414. * the configuration information for the specified DMA Channel.
  415. * @retval HAL status
  416. */
  417. HAL_StatusTypeDef HAL_DMA_Abort(DMA_HandleTypeDef *hdma)
  418. {
  419. /* Check the DMA peripheral handle */
  420. if (NULL == hdma)
  421. {
  422. return HAL_ERROR;
  423. }
  424. /* Check the DMA peripheral state */
  425. if(hdma->State != HAL_DMA_STATE_BUSY)
  426. {
  427. hdma->ErrorCode = HAL_DMA_ERROR_NO_XFER;
  428. /* Process Unlocked */
  429. __HAL_UNLOCK(hdma);
  430. return HAL_ERROR;
  431. }
  432. else
  433. {
  434. /* Disable DMA IT */
  435. __HAL_DMA_DISABLE_IT(hdma, (DMA_IT_TC | DMA_IT_HT | DMA_IT_TE));
  436. /* disable the DMAMUX sync overrun IT*/
  437. hdma->DMAmuxChannel->CCR &= ~DMAMUX_CxCR_SOIE;
  438. /* Disable the channel */
  439. __HAL_DMA_DISABLE(hdma);
  440. /* Clear all flags */
  441. hdma->DmaBaseAddress->IFCR = (DMA_ISR_GIF1 << (hdma->ChannelIndex & 0x1cU));
  442. /* Clear the DMAMUX synchro overrun flag */
  443. hdma->DMAmuxChannelStatus->CFR = hdma->DMAmuxChannelStatusMask;
  444. if (hdma->DMAmuxRequestGen != 0U)
  445. {
  446. /* if using DMAMUX request generator, disable the DMAMUX request generator overrun IT*/
  447. /* disable the request gen overrun IT*/
  448. hdma->DMAmuxRequestGen->RGCR &= ~DMAMUX_RGxCR_OIE;
  449. /* Clear the DMAMUX request generator overrun flag */
  450. hdma->DMAmuxRequestGenStatus->RGCFR = hdma->DMAmuxRequestGenStatusMask;
  451. }
  452. /* Change the DMA state */
  453. hdma->State = HAL_DMA_STATE_READY;
  454. /* Process Unlocked */
  455. __HAL_UNLOCK(hdma);
  456. }
  457. return HAL_OK;
  458. }
  459. /**
  460. * @brief Aborts the DMA Transfer in Interrupt mode.
  461. * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
  462. * the configuration information for the specified DMA Channel.
  463. * @retval HAL status
  464. */
  465. HAL_StatusTypeDef HAL_DMA_Abort_IT(DMA_HandleTypeDef *hdma)
  466. {
  467. HAL_StatusTypeDef status = HAL_OK;
  468. if (HAL_DMA_STATE_BUSY != hdma->State)
  469. {
  470. /* no transfer ongoing */
  471. hdma->ErrorCode = HAL_DMA_ERROR_NO_XFER;
  472. status = HAL_ERROR;
  473. }
  474. else
  475. {
  476. /* Disable DMA IT */
  477. __HAL_DMA_DISABLE_IT(hdma, (DMA_IT_TC | DMA_IT_HT | DMA_IT_TE));
  478. /* Disable the channel */
  479. __HAL_DMA_DISABLE(hdma);
  480. /* disable the DMAMUX sync overrun IT*/
  481. hdma->DMAmuxChannel->CCR &= ~DMAMUX_CxCR_SOIE;
  482. /* Clear all flags */
  483. hdma->DmaBaseAddress->IFCR = (DMA_ISR_GIF1 << (hdma->ChannelIndex & 0x1cU));
  484. /* Clear the DMAMUX synchro overrun flag */
  485. hdma->DMAmuxChannelStatus->CFR = hdma->DMAmuxChannelStatusMask;
  486. if (hdma->DMAmuxRequestGen != 0U)
  487. {
  488. /* if using DMAMUX request generator, disable the DMAMUX request generator overrun IT*/
  489. /* disable the request gen overrun IT*/
  490. hdma->DMAmuxRequestGen->RGCR &= ~DMAMUX_RGxCR_OIE;
  491. /* Clear the DMAMUX request generator overrun flag */
  492. hdma->DMAmuxRequestGenStatus->RGCFR = hdma->DMAmuxRequestGenStatusMask;
  493. }
  494. /* Change the DMA state */
  495. hdma->State = HAL_DMA_STATE_READY;
  496. /* Process Unlocked */
  497. __HAL_UNLOCK(hdma);
  498. /* Call User Abort callback */
  499. if (hdma->XferAbortCallback != NULL)
  500. {
  501. hdma->XferAbortCallback(hdma);
  502. }
  503. }
  504. return status;
  505. }
  506. /**
  507. * @brief Polling for transfer complete.
  508. * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
  509. * the configuration information for the specified DMA Channel.
  510. * @param CompleteLevel Specifies the DMA level complete.
  511. * @param Timeout Timeout duration.
  512. * @retval HAL status
  513. */
  514. HAL_StatusTypeDef HAL_DMA_PollForTransfer(DMA_HandleTypeDef *hdma, HAL_DMA_LevelCompleteTypeDef CompleteLevel, uint32_t Timeout)
  515. {
  516. uint32_t temp;
  517. uint32_t tickstart;
  518. if (HAL_DMA_STATE_BUSY != hdma->State)
  519. {
  520. /* no transfer ongoing */
  521. hdma->ErrorCode = HAL_DMA_ERROR_NO_XFER;
  522. __HAL_UNLOCK(hdma);
  523. return HAL_ERROR;
  524. }
  525. /* Polling mode not supported in circular mode */
  526. if ((hdma->Instance->CCR & DMA_CCR_CIRC) != 0U)
  527. {
  528. hdma->ErrorCode = HAL_DMA_ERROR_NOT_SUPPORTED;
  529. return HAL_ERROR;
  530. }
  531. /* Get the level transfer complete flag */
  532. if (HAL_DMA_FULL_TRANSFER == CompleteLevel)
  533. {
  534. /* Transfer Complete flag */
  535. temp = DMA_FLAG_TC1 << (hdma->ChannelIndex & 0x1cU);
  536. }
  537. else
  538. {
  539. /* Half Transfer Complete flag */
  540. temp = DMA_FLAG_HT1 << (hdma->ChannelIndex & 0x1cU);
  541. }
  542. /* Get tick */
  543. tickstart = HAL_GetTick();
  544. while((hdma->DmaBaseAddress->ISR & temp) == 0U)
  545. {
  546. if((hdma->DmaBaseAddress->ISR & (DMA_FLAG_TE1 << (hdma->ChannelIndex& 0x1CU))) != 0U)
  547. {
  548. /* When a DMA transfer error occurs */
  549. /* A hardware clear of its EN bits is performed */
  550. /* Clear all flags */
  551. hdma->DmaBaseAddress->IFCR = (DMA_ISR_GIF1 << (hdma->ChannelIndex & 0x1cU));
  552. /* Update error code */
  553. hdma->ErrorCode = HAL_DMA_ERROR_TE;
  554. /* Change the DMA state */
  555. hdma->State = HAL_DMA_STATE_READY;
  556. /* Process Unlocked */
  557. __HAL_UNLOCK(hdma);
  558. return HAL_ERROR;
  559. }
  560. /* Check for the Timeout */
  561. if (Timeout != HAL_MAX_DELAY)
  562. {
  563. if(((HAL_GetTick() - tickstart ) > Timeout)||(Timeout == 0U))
  564. {
  565. /* Update error code */
  566. hdma->ErrorCode = HAL_DMA_ERROR_TIMEOUT;
  567. /* Change the DMA state */
  568. hdma->State = HAL_DMA_STATE_READY;
  569. /* Process Unlocked */
  570. __HAL_UNLOCK(hdma);
  571. return HAL_ERROR;
  572. }
  573. }
  574. }
  575. /*Check for DMAMUX Request generator (if used) overrun status */
  576. if (hdma->DMAmuxRequestGen != 0U)
  577. {
  578. /* if using DMAMUX request generator Check for DMAMUX request generator overrun */
  579. if ((hdma->DMAmuxRequestGenStatus->RGSR & hdma->DMAmuxRequestGenStatusMask) != 0U)
  580. {
  581. /* Disable the request gen overrun interrupt */
  582. hdma->DMAmuxRequestGen->RGCR |= DMAMUX_RGxCR_OIE;
  583. /* Clear the DMAMUX request generator overrun flag */
  584. hdma->DMAmuxRequestGenStatus->RGCFR = hdma->DMAmuxRequestGenStatusMask;
  585. /* Update error code */
  586. hdma->ErrorCode |= HAL_DMA_ERROR_REQGEN;
  587. }
  588. }
  589. /* Check for DMAMUX Synchronization overrun */
  590. if ((hdma->DMAmuxChannelStatus->CSR & hdma->DMAmuxChannelStatusMask) != 0U)
  591. {
  592. /* Clear the DMAMUX synchro overrun flag */
  593. hdma->DMAmuxChannelStatus->CFR = hdma->DMAmuxChannelStatusMask;
  594. /* Update error code */
  595. hdma->ErrorCode |= HAL_DMA_ERROR_SYNC;
  596. }
  597. if (HAL_DMA_FULL_TRANSFER == CompleteLevel)
  598. {
  599. /* Clear the transfer complete flag */
  600. hdma->DmaBaseAddress->IFCR = (DMA_FLAG_TC1 << (hdma->ChannelIndex & 0x1cU));
  601. /* The selected Channelx EN bit is cleared (DMA is disabled and
  602. all transfers are complete) */
  603. hdma->State = HAL_DMA_STATE_READY;
  604. }
  605. else
  606. {
  607. /* Clear the half transfer complete flag */
  608. hdma->DmaBaseAddress->IFCR = (DMA_FLAG_HT1 << (hdma->ChannelIndex & 0x1cU));
  609. }
  610. /* Process unlocked */
  611. __HAL_UNLOCK(hdma);
  612. return HAL_OK;
  613. }
  614. /**
  615. * @brief Handle DMA interrupt request.
  616. * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
  617. * the configuration information for the specified DMA Channel.
  618. * @retval None
  619. */
  620. void HAL_DMA_IRQHandler(DMA_HandleTypeDef *hdma)
  621. {
  622. uint32_t flag_it = hdma->DmaBaseAddress->ISR;
  623. uint32_t source_it = hdma->Instance->CCR;
  624. /* Half Transfer Complete Interrupt management ******************************/
  625. if (((flag_it & (DMA_FLAG_HT1 << (hdma->ChannelIndex & 0x1cU))) != 0U) && ((source_it & DMA_IT_HT) != 0U))
  626. {
  627. /* Disable the half transfer interrupt if the DMA mode is not CIRCULAR */
  628. if ((hdma->Instance->CCR & DMA_CCR_CIRC) == 0U)
  629. {
  630. /* Disable the half transfer interrupt */
  631. __HAL_DMA_DISABLE_IT(hdma, DMA_IT_HT);
  632. }
  633. /* Clear the half transfer complete flag */
  634. hdma->DmaBaseAddress->IFCR = (DMA_ISR_HTIF1 << (hdma->ChannelIndex & 0x1CU));
  635. /* DMA peripheral state is not updated in Half Transfer */
  636. /* but in Transfer Complete case */
  637. if (hdma->XferHalfCpltCallback != NULL)
  638. {
  639. /* Half transfer callback */
  640. hdma->XferHalfCpltCallback(hdma);
  641. }
  642. }
  643. /* Transfer Complete Interrupt management ***********************************/
  644. else if (((flag_it & (DMA_FLAG_TC1 << (hdma->ChannelIndex & 0x1cU))) != 0U) && ((source_it & DMA_IT_TC) != 0U))
  645. {
  646. if ((hdma->Instance->CCR & DMA_CCR_CIRC) == 0U)
  647. {
  648. /* Disable the transfer complete and error interrupt */
  649. __HAL_DMA_DISABLE_IT(hdma, DMA_IT_TE | DMA_IT_TC);
  650. /* Change the DMA state */
  651. hdma->State = HAL_DMA_STATE_READY;
  652. }
  653. /* Clear the transfer complete flag */
  654. hdma->DmaBaseAddress->IFCR = (DMA_ISR_TCIF1 << (hdma->ChannelIndex & 0x1cU));
  655. /* Process Unlocked */
  656. __HAL_UNLOCK(hdma);
  657. if (hdma->XferCpltCallback != NULL)
  658. {
  659. /* Transfer complete callback */
  660. hdma->XferCpltCallback(hdma);
  661. }
  662. }
  663. /* Transfer Error Interrupt management **************************************/
  664. else if (((flag_it & (DMA_FLAG_TE1 << (hdma->ChannelIndex & 0x1cU)))!= 0U) && ((source_it & DMA_IT_TE) != 0U))
  665. {
  666. /* When a DMA transfer error occurs */
  667. /* A hardware clear of its EN bits is performed */
  668. /* Disable ALL DMA IT */
  669. __HAL_DMA_DISABLE_IT(hdma, (DMA_IT_TC | DMA_IT_HT | DMA_IT_TE));
  670. /* Clear all flags */
  671. hdma->DmaBaseAddress->IFCR = (DMA_ISR_GIF1 << (hdma->ChannelIndex & 0x1cU));
  672. /* Update error code */
  673. hdma->ErrorCode = HAL_DMA_ERROR_TE;
  674. /* Change the DMA state */
  675. hdma->State = HAL_DMA_STATE_READY;
  676. /* Process Unlocked */
  677. __HAL_UNLOCK(hdma);
  678. if (hdma->XferErrorCallback != NULL)
  679. {
  680. /* Transfer error callback */
  681. hdma->XferErrorCallback(hdma);
  682. }
  683. }
  684. else
  685. {
  686. /* Nothing To Do */
  687. }
  688. return;
  689. }
  690. /**
  691. * @brief Register callbacks
  692. * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
  693. * the configuration information for the specified DMA Channel.
  694. * @param CallbackID User Callback identifer
  695. * a HAL_DMA_CallbackIDTypeDef ENUM as parameter.
  696. * @param pCallback Pointer to private callbacsk function which has pointer to
  697. * a DMA_HandleTypeDef structure as parameter.
  698. * @retval HAL status
  699. */
  700. HAL_StatusTypeDef HAL_DMA_RegisterCallback(DMA_HandleTypeDef *hdma, HAL_DMA_CallbackIDTypeDef CallbackID, void (* pCallback)(DMA_HandleTypeDef *_hdma))
  701. {
  702. HAL_StatusTypeDef status = HAL_OK;
  703. /* Process locked */
  704. __HAL_LOCK(hdma);
  705. if (HAL_DMA_STATE_READY == hdma->State)
  706. {
  707. switch (CallbackID)
  708. {
  709. case HAL_DMA_XFER_CPLT_CB_ID:
  710. hdma->XferCpltCallback = pCallback;
  711. break;
  712. case HAL_DMA_XFER_HALFCPLT_CB_ID:
  713. hdma->XferHalfCpltCallback = pCallback;
  714. break;
  715. case HAL_DMA_XFER_ERROR_CB_ID:
  716. hdma->XferErrorCallback = pCallback;
  717. break;
  718. case HAL_DMA_XFER_ABORT_CB_ID:
  719. hdma->XferAbortCallback = pCallback;
  720. break;
  721. default:
  722. status = HAL_ERROR;
  723. break;
  724. }
  725. }
  726. else
  727. {
  728. status = HAL_ERROR;
  729. }
  730. /* Release Lock */
  731. __HAL_UNLOCK(hdma);
  732. return status;
  733. }
  734. /**
  735. * @brief UnRegister callbacks
  736. * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
  737. * the configuration information for the specified DMA Channel.
  738. * @param CallbackID User Callback identifer
  739. * a HAL_DMA_CallbackIDTypeDef ENUM as parameter.
  740. * @retval HAL status
  741. */
  742. HAL_StatusTypeDef HAL_DMA_UnRegisterCallback(DMA_HandleTypeDef *hdma, HAL_DMA_CallbackIDTypeDef CallbackID)
  743. {
  744. HAL_StatusTypeDef status = HAL_OK;
  745. /* Process locked */
  746. __HAL_LOCK(hdma);
  747. if (HAL_DMA_STATE_READY == hdma->State)
  748. {
  749. switch (CallbackID)
  750. {
  751. case HAL_DMA_XFER_CPLT_CB_ID:
  752. hdma->XferCpltCallback = NULL;
  753. break;
  754. case HAL_DMA_XFER_HALFCPLT_CB_ID:
  755. hdma->XferHalfCpltCallback = NULL;
  756. break;
  757. case HAL_DMA_XFER_ERROR_CB_ID:
  758. hdma->XferErrorCallback = NULL;
  759. break;
  760. case HAL_DMA_XFER_ABORT_CB_ID:
  761. hdma->XferAbortCallback = NULL;
  762. break;
  763. case HAL_DMA_XFER_ALL_CB_ID:
  764. hdma->XferCpltCallback = NULL;
  765. hdma->XferHalfCpltCallback = NULL;
  766. hdma->XferErrorCallback = NULL;
  767. hdma->XferAbortCallback = NULL;
  768. break;
  769. default:
  770. status = HAL_ERROR;
  771. break;
  772. }
  773. }
  774. else
  775. {
  776. status = HAL_ERROR;
  777. }
  778. /* Release Lock */
  779. __HAL_UNLOCK(hdma);
  780. return status;
  781. }
  782. /**
  783. * @}
  784. */
  785. /** @defgroup DMA_Exported_Functions_Group3 Peripheral State and Errors functions
  786. * @brief Peripheral State and Errors functions
  787. *
  788. @verbatim
  789. ===============================================================================
  790. ##### Peripheral State and Errors functions #####
  791. ===============================================================================
  792. [..]
  793. This subsection provides functions allowing to
  794. (+) Check the DMA state
  795. (+) Get error code
  796. @endverbatim
  797. * @{
  798. */
  799. /**
  800. * @brief Return the DMA handle state.
  801. * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
  802. * the configuration information for the specified DMA Channel.
  803. * @retval HAL state
  804. */
  805. HAL_DMA_StateTypeDef HAL_DMA_GetState(DMA_HandleTypeDef *hdma)
  806. {
  807. /* Return DMA handle state */
  808. return hdma->State;
  809. }
  810. /**
  811. * @brief Return the DMA error code.
  812. * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
  813. * the configuration information for the specified DMA Channel.
  814. * @retval DMA Error Code
  815. */
  816. uint32_t HAL_DMA_GetError(DMA_HandleTypeDef *hdma)
  817. {
  818. return hdma->ErrorCode;
  819. }
  820. /**
  821. * @}
  822. */
  823. /**
  824. * @}
  825. */
  826. /** @addtogroup DMA_Private_Functions
  827. * @{
  828. */
  829. /**
  830. * @brief Sets the DMA Transfer parameter.
  831. * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
  832. * the configuration information for the specified DMA Channel.
  833. * @param SrcAddress The source memory Buffer address
  834. * @param DstAddress The destination memory Buffer address
  835. * @param DataLength The length of data to be transferred from source to destination
  836. * @retval HAL status
  837. */
  838. static void DMA_SetConfig(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength)
  839. {
  840. /* Clear the DMAMUX synchro overrun flag */
  841. hdma->DMAmuxChannelStatus->CFR = hdma->DMAmuxChannelStatusMask;
  842. if (hdma->DMAmuxRequestGen != 0U)
  843. {
  844. /* Clear the DMAMUX request generator overrun flag */
  845. hdma->DMAmuxRequestGenStatus->RGCFR = hdma->DMAmuxRequestGenStatusMask;
  846. }
  847. /* Clear all flags */
  848. hdma->DmaBaseAddress->IFCR = (DMA_ISR_GIF1 << (hdma->ChannelIndex & 0x1cU));
  849. /* Configure DMA Channel data length */
  850. hdma->Instance->CNDTR = DataLength;
  851. /* Memory to Peripheral */
  852. if ((hdma->Init.Direction) == DMA_MEMORY_TO_PERIPH)
  853. {
  854. /* Configure DMA Channel destination address */
  855. hdma->Instance->CPAR = DstAddress;
  856. /* Configure DMA Channel source address */
  857. hdma->Instance->CMAR = SrcAddress;
  858. }
  859. /* Peripheral to Memory */
  860. else
  861. {
  862. /* Configure DMA Channel source address */
  863. hdma->Instance->CPAR = SrcAddress;
  864. /* Configure DMA Channel destination address */
  865. hdma->Instance->CMAR = DstAddress;
  866. }
  867. }
  868. /**
  869. * @brief Updates the DMA handle with the DMAMUX channel and status mask depending on channel number
  870. * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
  871. * the configuration information for the specified DMA Channel.
  872. * @retval None
  873. */
  874. static void DMA_CalcDMAMUXChannelBaseAndMask(DMA_HandleTypeDef *hdma)
  875. {
  876. uint32_t channel_number;
  877. /* check if instance is not outside the DMA channel range */
  878. #if defined(DMA2)
  879. if ((uint32_t)hdma->Instance < (uint32_t)DMA2_Channel1)
  880. {
  881. /* DMA1 */
  882. hdma->DMAmuxChannel = (DMAMUX1_Channel0 + (hdma->ChannelIndex >> 2U));
  883. }
  884. else
  885. {
  886. /* DMA2 */
  887. hdma->DMAmuxChannel = (DMAMUX1_Channel7 + (hdma->ChannelIndex >> 2U));
  888. }
  889. #else
  890. /* DMA1 */
  891. hdma->DMAmuxChannel = (DMAMUX1_Channel0 + (hdma->ChannelIndex >> 2U));
  892. #endif
  893. channel_number = (((uint32_t)hdma->Instance & 0xFFU) - 8U) / 20U;
  894. hdma->DMAmuxChannelStatus = DMAMUX1_ChannelStatus;
  895. hdma->DMAmuxChannelStatusMask = 1UL << (channel_number & 0x1cU);
  896. }
  897. /**
  898. * @brief Updates the DMA handle with the DMAMUX request generator params
  899. * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
  900. * the configuration information for the specified DMA Channel.
  901. * @retval None
  902. */
  903. static void DMA_CalcDMAMUXRequestGenBaseAndMask(DMA_HandleTypeDef *hdma)
  904. {
  905. uint32_t request = hdma->Init.Request & DMAMUX_CxCR_DMAREQ_ID;
  906. /* DMA Channels are connected to DMAMUX1 request generator blocks*/
  907. hdma->DMAmuxRequestGen = (DMAMUX_RequestGen_TypeDef *)((uint32_t)(((uint32_t)DMAMUX1_RequestGenerator0) + ((request - 1U) * 4U)));
  908. hdma->DMAmuxRequestGenStatus = DMAMUX1_RequestGenStatus;
  909. /* here "Request" is either DMA_REQUEST_GENERATOR0 to DMA_REQUEST_GENERATOR3, i.e. <= 4*/
  910. hdma->DMAmuxRequestGenStatusMask = 1UL << ((request - 1U) & 0x3U);
  911. }
  912. /**
  913. * @}
  914. */
  915. /**
  916. * @}
  917. */
  918. #endif /* HAL_DMA_MODULE_ENABLED */
  919. /**
  920. * @}
  921. */
  922. /**
  923. * @}
  924. */
  925. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/