stm32f0xx_hal_dma.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  1. /**
  2. ******************************************************************************
  3. * @file stm32f0xx_hal_dma.c
  4. * @author MCD Application Team
  5. * @brief DMA HAL module driver.
  6. *
  7. * This file provides firmware functions to manage the following
  8. * functionalities of the Direct Memory Access (DMA) peripheral:
  9. * + Initialization and de-initialization functions
  10. * + IO operation functions
  11. * + Peripheral State and errors functions
  12. @verbatim
  13. ==============================================================================
  14. ##### How to use this driver #####
  15. ==============================================================================
  16. [..]
  17. (#) Enable and configure the peripheral to be connected to the DMA Channel
  18. (except for internal SRAM / FLASH memories: no initialization is
  19. necessary). Please refer to Reference manual for connection between peripherals
  20. and DMA requests .
  21. (#) For a given Channel, program the required configuration through the following parameters:
  22. Transfer Direction, Source and Destination data formats,
  23. Circular or Normal mode, Channel Priority level, Source and Destination Increment mode,
  24. using HAL_DMA_Init() function.
  25. (#) Use HAL_DMA_GetState() function to return the DMA state and HAL_DMA_GetError() in case of error
  26. detection.
  27. (#) Use HAL_DMA_Abort() function to abort the current transfer
  28. -@- In Memory-to-Memory transfer mode, Circular mode is not allowed.
  29. *** Polling mode IO operation ***
  30. =================================
  31. [..]
  32. (+) Use HAL_DMA_Start() to start DMA transfer after the configuration of Source
  33. address and destination address and the Length of data to be transferred
  34. (+) Use HAL_DMA_PollForTransfer() to poll for the end of current transfer, in this
  35. case a fixed Timeout can be configured by User depending from his application.
  36. *** Interrupt mode IO operation ***
  37. ===================================
  38. [..]
  39. (+) Configure the DMA interrupt priority using HAL_NVIC_SetPriority()
  40. (+) Enable the DMA IRQ handler using HAL_NVIC_EnableIRQ()
  41. (+) Use HAL_DMA_Start_IT() to start DMA transfer after the configuration of
  42. Source address and destination address and the Length of data to be transferred.
  43. In this case the DMA interrupt is configured
  44. (+) Use HAL_DMA_Channel_IRQHandler() called under DMA_IRQHandler() Interrupt subroutine
  45. (+) At the end of data transfer HAL_DMA_IRQHandler() function is executed and user can
  46. add his own function by customization of function pointer XferCpltCallback and
  47. XferErrorCallback (i.e a member of DMA handle structure).
  48. *** DMA HAL driver macros list ***
  49. =============================================
  50. [..]
  51. Below the list of most used macros in DMA HAL driver.
  52. [..]
  53. (@) You can refer to the DMA HAL driver header file for more useful macros
  54. @endverbatim
  55. ******************************************************************************
  56. * @attention
  57. *
  58. * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  59. * All rights reserved.</center></h2>
  60. *
  61. * This software component is licensed by ST under BSD 3-Clause license,
  62. * the "License"; You may not use this file except in compliance with the
  63. * License. You may obtain a copy of the License at:
  64. * opensource.org/licenses/BSD-3-Clause
  65. *
  66. ******************************************************************************
  67. */
  68. /* Includes ------------------------------------------------------------------*/
  69. #include "stm32f0xx_hal.h"
  70. /** @addtogroup STM32F0xx_HAL_Driver
  71. * @{
  72. */
  73. /** @defgroup DMA DMA
  74. * @brief DMA HAL module driver
  75. * @{
  76. */
  77. #ifdef HAL_DMA_MODULE_ENABLED
  78. /* Private typedef -----------------------------------------------------------*/
  79. /* Private define ------------------------------------------------------------*/
  80. /* Private macro -------------------------------------------------------------*/
  81. /* Private variables ---------------------------------------------------------*/
  82. /* Private function prototypes -----------------------------------------------*/
  83. /** @defgroup DMA_Private_Functions DMA Private Functions
  84. * @{
  85. */
  86. static void DMA_SetConfig(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength);
  87. static void DMA_CalcBaseAndBitshift(DMA_HandleTypeDef *hdma);
  88. /**
  89. * @}
  90. */
  91. /* Exported functions ---------------------------------------------------------*/
  92. /** @defgroup DMA_Exported_Functions DMA Exported Functions
  93. * @{
  94. */
  95. /** @defgroup DMA_Exported_Functions_Group1 Initialization and de-initialization functions
  96. * @brief Initialization and de-initialization functions
  97. *
  98. @verbatim
  99. ===============================================================================
  100. ##### Initialization and de-initialization functions #####
  101. ===============================================================================
  102. [..]
  103. This section provides functions allowing to initialize the DMA Channel source
  104. and destination addresses, incrementation and data sizes, transfer direction,
  105. circular/normal mode selection, memory-to-memory mode selection and Channel priority value.
  106. [..]
  107. The HAL_DMA_Init() function follows the DMA configuration procedures as described in
  108. reference manual.
  109. @endverbatim
  110. * @{
  111. */
  112. /**
  113. * @brief Initialize the DMA according to the specified
  114. * parameters in the DMA_InitTypeDef and initialize the associated handle.
  115. * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
  116. * the configuration information for the specified DMA Channel.
  117. * @retval HAL status
  118. */
  119. HAL_StatusTypeDef HAL_DMA_Init(DMA_HandleTypeDef *hdma)
  120. {
  121. uint32_t tmp = 0U;
  122. /* Check the DMA handle allocation */
  123. if(NULL == hdma)
  124. {
  125. return HAL_ERROR;
  126. }
  127. /* Check the parameters */
  128. assert_param(IS_DMA_ALL_INSTANCE(hdma->Instance));
  129. assert_param(IS_DMA_DIRECTION(hdma->Init.Direction));
  130. assert_param(IS_DMA_PERIPHERAL_INC_STATE(hdma->Init.PeriphInc));
  131. assert_param(IS_DMA_MEMORY_INC_STATE(hdma->Init.MemInc));
  132. assert_param(IS_DMA_PERIPHERAL_DATA_SIZE(hdma->Init.PeriphDataAlignment));
  133. assert_param(IS_DMA_MEMORY_DATA_SIZE(hdma->Init.MemDataAlignment));
  134. assert_param(IS_DMA_MODE(hdma->Init.Mode));
  135. assert_param(IS_DMA_PRIORITY(hdma->Init.Priority));
  136. /* Change DMA peripheral state */
  137. hdma->State = HAL_DMA_STATE_BUSY;
  138. /* Get the CR register value */
  139. tmp = hdma->Instance->CCR;
  140. /* Clear PL, MSIZE, PSIZE, MINC, PINC, CIRC, DIR bits */
  141. tmp &= ((uint32_t)~(DMA_CCR_PL | DMA_CCR_MSIZE | DMA_CCR_PSIZE | \
  142. DMA_CCR_MINC | DMA_CCR_PINC | DMA_CCR_CIRC | \
  143. DMA_CCR_DIR));
  144. /* Prepare the DMA Channel configuration */
  145. tmp |= hdma->Init.Direction |
  146. hdma->Init.PeriphInc | hdma->Init.MemInc |
  147. hdma->Init.PeriphDataAlignment | hdma->Init.MemDataAlignment |
  148. hdma->Init.Mode | hdma->Init.Priority;
  149. /* Write to DMA Channel CR register */
  150. hdma->Instance->CCR = tmp;
  151. /* Initialize DmaBaseAddress and ChannelIndex parameters used
  152. by HAL_DMA_IRQHandler() and HAL_DMA_PollForTransfer() */
  153. DMA_CalcBaseAndBitshift(hdma);
  154. /* Initialise the error code */
  155. hdma->ErrorCode = HAL_DMA_ERROR_NONE;
  156. /* Initialize the DMA state*/
  157. hdma->State = HAL_DMA_STATE_READY;
  158. /* Allocate lock resource and initialize it */
  159. hdma->Lock = HAL_UNLOCKED;
  160. return HAL_OK;
  161. }
  162. /**
  163. * @brief DeInitialize the DMA peripheral
  164. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  165. * the configuration information for the specified DMA Channel.
  166. * @retval HAL status
  167. */
  168. HAL_StatusTypeDef HAL_DMA_DeInit(DMA_HandleTypeDef *hdma)
  169. {
  170. /* Check the DMA handle allocation */
  171. if(NULL == hdma)
  172. {
  173. return HAL_ERROR;
  174. }
  175. /* Check the parameters */
  176. assert_param(IS_DMA_ALL_INSTANCE(hdma->Instance));
  177. /* Disable the selected DMA Channelx */
  178. hdma->Instance->CCR &= ~DMA_CCR_EN;
  179. /* Reset DMA Channel control register */
  180. hdma->Instance->CCR = 0U;
  181. /* Reset DMA Channel Number of Data to Transfer register */
  182. hdma->Instance->CNDTR = 0U;
  183. /* Reset DMA Channel peripheral address register */
  184. hdma->Instance->CPAR = 0U;
  185. /* Reset DMA Channel memory address register */
  186. hdma->Instance->CMAR = 0U;
  187. /* Get DMA Base Address */
  188. DMA_CalcBaseAndBitshift(hdma);
  189. /* Clear all flags */
  190. hdma->DmaBaseAddress->IFCR = DMA_FLAG_GL1 << hdma->ChannelIndex;
  191. /* Clean callbacks */
  192. hdma->XferCpltCallback = NULL;
  193. hdma->XferHalfCpltCallback = NULL;
  194. hdma->XferErrorCallback = NULL;
  195. hdma->XferAbortCallback = NULL;
  196. /* Reset the error code */
  197. hdma->ErrorCode = HAL_DMA_ERROR_NONE;
  198. /* Reset the DMA state */
  199. hdma->State = HAL_DMA_STATE_RESET;
  200. /* Release Lock */
  201. __HAL_UNLOCK(hdma);
  202. return HAL_OK;
  203. }
  204. /**
  205. * @}
  206. */
  207. /** @defgroup DMA_Exported_Functions_Group2 Input and Output operation functions
  208. * @brief I/O operation functions
  209. *
  210. @verbatim
  211. ===============================================================================
  212. ##### IO operation functions #####
  213. ===============================================================================
  214. [..] This section provides functions allowing to:
  215. (+) Configure the source, destination address and data length and Start DMA transfer
  216. (+) Configure the source, destination address and data length and
  217. Start DMA transfer with interrupt
  218. (+) Abort DMA transfer
  219. (+) Poll for transfer complete
  220. (+) Handle DMA interrupt request
  221. @endverbatim
  222. * @{
  223. */
  224. /**
  225. * @brief Start the DMA Transfer.
  226. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  227. * the configuration information for the specified DMA Channel.
  228. * @param SrcAddress The source memory Buffer address
  229. * @param DstAddress The destination memory Buffer address
  230. * @param DataLength The length of data to be transferred from source to destination
  231. * @retval HAL status
  232. */
  233. HAL_StatusTypeDef HAL_DMA_Start(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength)
  234. {
  235. HAL_StatusTypeDef status = HAL_OK;
  236. /* Check the parameters */
  237. assert_param(IS_DMA_BUFFER_SIZE(DataLength));
  238. /* Process locked */
  239. __HAL_LOCK(hdma);
  240. if(HAL_DMA_STATE_READY == hdma->State)
  241. {
  242. /* Change DMA peripheral state */
  243. hdma->State = HAL_DMA_STATE_BUSY;
  244. hdma->ErrorCode = HAL_DMA_ERROR_NONE;
  245. /* Disable the peripheral */
  246. hdma->Instance->CCR &= ~DMA_CCR_EN;
  247. /* Configure the source, destination address and the data length */
  248. DMA_SetConfig(hdma, SrcAddress, DstAddress, DataLength);
  249. /* Enable the Peripheral */
  250. hdma->Instance->CCR |= DMA_CCR_EN;
  251. }
  252. else
  253. {
  254. /* Process Unlocked */
  255. __HAL_UNLOCK(hdma);
  256. /* Remain BUSY */
  257. status = HAL_BUSY;
  258. }
  259. return status;
  260. }
  261. /**
  262. * @brief Start the DMA Transfer with interrupt enabled.
  263. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  264. * the configuration information for the specified DMA Channel.
  265. * @param SrcAddress The source memory Buffer address
  266. * @param DstAddress The destination memory Buffer address
  267. * @param DataLength The length of data to be transferred from source to destination
  268. * @retval HAL status
  269. */
  270. HAL_StatusTypeDef HAL_DMA_Start_IT(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength)
  271. {
  272. HAL_StatusTypeDef status = HAL_OK;
  273. /* Check the parameters */
  274. assert_param(IS_DMA_BUFFER_SIZE(DataLength));
  275. /* Process locked */
  276. __HAL_LOCK(hdma);
  277. if(HAL_DMA_STATE_READY == hdma->State)
  278. {
  279. /* Change DMA peripheral state */
  280. hdma->State = HAL_DMA_STATE_BUSY;
  281. hdma->ErrorCode = HAL_DMA_ERROR_NONE;
  282. /* Disable the peripheral */
  283. hdma->Instance->CCR &= ~DMA_CCR_EN;
  284. /* Configure the source, destination address and the data length */
  285. DMA_SetConfig(hdma, SrcAddress, DstAddress, DataLength);
  286. /* Enable the transfer complete, & transfer error interrupts */
  287. /* Half transfer interrupt is optional: enable it only if associated callback is available */
  288. if(NULL != hdma->XferHalfCpltCallback )
  289. {
  290. hdma->Instance->CCR |= (DMA_IT_TC | DMA_IT_HT | DMA_IT_TE);
  291. }
  292. else
  293. {
  294. hdma->Instance->CCR |= (DMA_IT_TC | DMA_IT_TE);
  295. hdma->Instance->CCR &= ~DMA_IT_HT;
  296. }
  297. /* Enable the Peripheral */
  298. hdma->Instance->CCR |= DMA_CCR_EN;
  299. }
  300. else
  301. {
  302. /* Process Unlocked */
  303. __HAL_UNLOCK(hdma);
  304. /* Remain BUSY */
  305. status = HAL_BUSY;
  306. }
  307. return status;
  308. }
  309. /**
  310. * @brief Abort the DMA Transfer.
  311. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  312. * the configuration information for the specified DMA Channel.
  313. * @retval HAL status
  314. */
  315. HAL_StatusTypeDef HAL_DMA_Abort(DMA_HandleTypeDef *hdma)
  316. {
  317. if(hdma->State != HAL_DMA_STATE_BUSY)
  318. {
  319. /* no transfer ongoing */
  320. hdma->ErrorCode = HAL_DMA_ERROR_NO_XFER;
  321. /* Process Unlocked */
  322. __HAL_UNLOCK(hdma);
  323. return HAL_ERROR;
  324. }
  325. else
  326. {
  327. /* Disable DMA IT */
  328. hdma->Instance->CCR &= ~(DMA_IT_TC | DMA_IT_HT | DMA_IT_TE);
  329. /* Disable the channel */
  330. hdma->Instance->CCR &= ~DMA_CCR_EN;
  331. /* Clear all flags */
  332. hdma->DmaBaseAddress->IFCR = (DMA_FLAG_GL1 << hdma->ChannelIndex);
  333. }
  334. /* Change the DMA state*/
  335. hdma->State = HAL_DMA_STATE_READY;
  336. /* Process Unlocked */
  337. __HAL_UNLOCK(hdma);
  338. return HAL_OK;
  339. }
  340. /**
  341. * @brief Abort the DMA Transfer in Interrupt mode.
  342. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  343. * the configuration information for the specified DMA Stream.
  344. * @retval HAL status
  345. */
  346. HAL_StatusTypeDef HAL_DMA_Abort_IT(DMA_HandleTypeDef *hdma)
  347. {
  348. HAL_StatusTypeDef status = HAL_OK;
  349. if(HAL_DMA_STATE_BUSY != hdma->State)
  350. {
  351. /* no transfer ongoing */
  352. hdma->ErrorCode = HAL_DMA_ERROR_NO_XFER;
  353. status = HAL_ERROR;
  354. }
  355. else
  356. {
  357. /* Disable DMA IT */
  358. hdma->Instance->CCR &= ~(DMA_IT_TC | DMA_IT_HT | DMA_IT_TE);
  359. /* Disable the channel */
  360. hdma->Instance->CCR &= ~DMA_CCR_EN;
  361. /* Clear all flags */
  362. hdma->DmaBaseAddress->IFCR = DMA_FLAG_GL1 << hdma->ChannelIndex;
  363. /* Change the DMA state */
  364. hdma->State = HAL_DMA_STATE_READY;
  365. /* Process Unlocked */
  366. __HAL_UNLOCK(hdma);
  367. /* Call User Abort callback */
  368. if(hdma->XferAbortCallback != NULL)
  369. {
  370. hdma->XferAbortCallback(hdma);
  371. }
  372. }
  373. return status;
  374. }
  375. /**
  376. * @brief Polling for transfer complete.
  377. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  378. * the configuration information for the specified DMA Channel.
  379. * @param CompleteLevel Specifies the DMA level complete.
  380. * @param Timeout Timeout duration.
  381. * @retval HAL status
  382. */
  383. HAL_StatusTypeDef HAL_DMA_PollForTransfer(DMA_HandleTypeDef *hdma, uint32_t CompleteLevel, uint32_t Timeout)
  384. {
  385. uint32_t temp;
  386. uint32_t tickstart = 0U;
  387. if(HAL_DMA_STATE_BUSY != hdma->State)
  388. {
  389. /* no transfer ongoing */
  390. hdma->ErrorCode = HAL_DMA_ERROR_NO_XFER;
  391. __HAL_UNLOCK(hdma);
  392. return HAL_ERROR;
  393. }
  394. /* Polling mode not supported in circular mode */
  395. if (RESET != (hdma->Instance->CCR & DMA_CCR_CIRC))
  396. {
  397. hdma->ErrorCode = HAL_DMA_ERROR_NOT_SUPPORTED;
  398. return HAL_ERROR;
  399. }
  400. /* Get the level transfer complete flag */
  401. if(HAL_DMA_FULL_TRANSFER == CompleteLevel)
  402. {
  403. /* Transfer Complete flag */
  404. temp = DMA_FLAG_TC1 << hdma->ChannelIndex;
  405. }
  406. else
  407. {
  408. /* Half Transfer Complete flag */
  409. temp = DMA_FLAG_HT1 << hdma->ChannelIndex;
  410. }
  411. /* Get tick */
  412. tickstart = HAL_GetTick();
  413. while(RESET == (hdma->DmaBaseAddress->ISR & temp))
  414. {
  415. if(RESET != (hdma->DmaBaseAddress->ISR & (DMA_FLAG_TE1 << hdma->ChannelIndex)))
  416. {
  417. /* When a DMA transfer error occurs */
  418. /* A hardware clear of its EN bits is performed */
  419. /* Clear all flags */
  420. hdma->DmaBaseAddress->IFCR = DMA_FLAG_GL1 << hdma->ChannelIndex;
  421. /* Update error code */
  422. hdma->ErrorCode = HAL_DMA_ERROR_TE;
  423. /* Change the DMA state */
  424. hdma->State= HAL_DMA_STATE_READY;
  425. /* Process Unlocked */
  426. __HAL_UNLOCK(hdma);
  427. return HAL_ERROR;
  428. }
  429. /* Check for the Timeout */
  430. if(Timeout != HAL_MAX_DELAY)
  431. {
  432. if((Timeout == 0U) || ((HAL_GetTick() - tickstart) > Timeout))
  433. {
  434. /* Update error code */
  435. hdma->ErrorCode = HAL_DMA_ERROR_TIMEOUT;
  436. /* Change the DMA state */
  437. hdma->State = HAL_DMA_STATE_READY;
  438. /* Process Unlocked */
  439. __HAL_UNLOCK(hdma);
  440. return HAL_ERROR;
  441. }
  442. }
  443. }
  444. if(HAL_DMA_FULL_TRANSFER == CompleteLevel)
  445. {
  446. /* Clear the transfer complete flag */
  447. hdma->DmaBaseAddress->IFCR = DMA_FLAG_TC1 << hdma->ChannelIndex;
  448. /* The selected Channelx EN bit is cleared (DMA is disabled and
  449. all transfers are complete) */
  450. hdma->State = HAL_DMA_STATE_READY;
  451. }
  452. else
  453. {
  454. /* Clear the half transfer complete flag */
  455. hdma->DmaBaseAddress->IFCR = DMA_FLAG_HT1 << hdma->ChannelIndex;
  456. }
  457. /* Process unlocked */
  458. __HAL_UNLOCK(hdma);
  459. return HAL_OK;
  460. }
  461. /**
  462. * @brief Handle DMA interrupt request.
  463. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  464. * the configuration information for the specified DMA Channel.
  465. * @retval None
  466. */
  467. void HAL_DMA_IRQHandler(DMA_HandleTypeDef *hdma)
  468. {
  469. uint32_t flag_it = hdma->DmaBaseAddress->ISR;
  470. uint32_t source_it = hdma->Instance->CCR;
  471. /* Half Transfer Complete Interrupt management ******************************/
  472. if ((RESET != (flag_it & (DMA_FLAG_HT1 << hdma->ChannelIndex))) && (RESET != (source_it & DMA_IT_HT)))
  473. {
  474. /* Disable the half transfer interrupt if the DMA mode is not CIRCULAR */
  475. if((hdma->Instance->CCR & DMA_CCR_CIRC) == 0U)
  476. {
  477. /* Disable the half transfer interrupt */
  478. hdma->Instance->CCR &= ~DMA_IT_HT;
  479. }
  480. /* Clear the half transfer complete flag */
  481. hdma->DmaBaseAddress->IFCR = DMA_FLAG_HT1 << hdma->ChannelIndex;
  482. /* DMA peripheral state is not updated in Half Transfer */
  483. /* State is updated only in Transfer Complete case */
  484. if(hdma->XferHalfCpltCallback != NULL)
  485. {
  486. /* Half transfer callback */
  487. hdma->XferHalfCpltCallback(hdma);
  488. }
  489. }
  490. /* Transfer Complete Interrupt management ***********************************/
  491. else if ((RESET != (flag_it & (DMA_FLAG_TC1 << hdma->ChannelIndex))) && (RESET != (source_it & DMA_IT_TC)))
  492. {
  493. if((hdma->Instance->CCR & DMA_CCR_CIRC) == 0U)
  494. {
  495. /* Disable the transfer complete & transfer error interrupts */
  496. /* if the DMA mode is not CIRCULAR */
  497. hdma->Instance->CCR &= ~(DMA_IT_TC | DMA_IT_TE);
  498. /* Change the DMA state */
  499. hdma->State = HAL_DMA_STATE_READY;
  500. }
  501. /* Clear the transfer complete flag */
  502. hdma->DmaBaseAddress->IFCR = DMA_FLAG_TC1 << hdma->ChannelIndex;
  503. /* Process Unlocked */
  504. __HAL_UNLOCK(hdma);
  505. if(hdma->XferCpltCallback != NULL)
  506. {
  507. /* Transfer complete callback */
  508. hdma->XferCpltCallback(hdma);
  509. }
  510. }
  511. /* Transfer Error Interrupt management ***************************************/
  512. else if (( RESET != (flag_it & (DMA_FLAG_TE1 << hdma->ChannelIndex))) && (RESET != (source_it & DMA_IT_TE)))
  513. {
  514. /* When a DMA transfer error occurs */
  515. /* A hardware clear of its EN bits is performed */
  516. /* Then, disable all DMA interrupts */
  517. hdma->Instance->CCR &= ~(DMA_IT_TC | DMA_IT_HT | DMA_IT_TE);
  518. /* Clear all flags */
  519. hdma->DmaBaseAddress->IFCR = DMA_FLAG_GL1 << hdma->ChannelIndex;
  520. /* Update error code */
  521. hdma->ErrorCode = HAL_DMA_ERROR_TE;
  522. /* Change the DMA state */
  523. hdma->State = HAL_DMA_STATE_READY;
  524. /* Process Unlocked */
  525. __HAL_UNLOCK(hdma);
  526. if(hdma->XferErrorCallback != NULL)
  527. {
  528. /* Transfer error callback */
  529. hdma->XferErrorCallback(hdma);
  530. }
  531. }
  532. }
  533. /**
  534. * @brief Register callbacks
  535. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  536. * the configuration information for the specified DMA Stream.
  537. * @param CallbackID User Callback identifer
  538. * a HAL_DMA_CallbackIDTypeDef ENUM as parameter.
  539. * @param pCallback pointer to private callback function which has pointer to
  540. * a DMA_HandleTypeDef structure as parameter.
  541. * @retval HAL status
  542. */
  543. HAL_StatusTypeDef HAL_DMA_RegisterCallback(DMA_HandleTypeDef *hdma, HAL_DMA_CallbackIDTypeDef CallbackID, void (* pCallback)( DMA_HandleTypeDef * _hdma))
  544. {
  545. HAL_StatusTypeDef status = HAL_OK;
  546. /* Process locked */
  547. __HAL_LOCK(hdma);
  548. if(HAL_DMA_STATE_READY == hdma->State)
  549. {
  550. switch (CallbackID)
  551. {
  552. case HAL_DMA_XFER_CPLT_CB_ID:
  553. hdma->XferCpltCallback = pCallback;
  554. break;
  555. case HAL_DMA_XFER_HALFCPLT_CB_ID:
  556. hdma->XferHalfCpltCallback = pCallback;
  557. break;
  558. case HAL_DMA_XFER_ERROR_CB_ID:
  559. hdma->XferErrorCallback = pCallback;
  560. break;
  561. case HAL_DMA_XFER_ABORT_CB_ID:
  562. hdma->XferAbortCallback = pCallback;
  563. break;
  564. default:
  565. status = HAL_ERROR;
  566. break;
  567. }
  568. }
  569. else
  570. {
  571. status = HAL_ERROR;
  572. }
  573. /* Release Lock */
  574. __HAL_UNLOCK(hdma);
  575. return status;
  576. }
  577. /**
  578. * @brief UnRegister callbacks
  579. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  580. * the configuration information for the specified DMA Stream.
  581. * @param CallbackID User Callback identifer
  582. * a HAL_DMA_CallbackIDTypeDef ENUM as parameter.
  583. * @retval HAL status
  584. */
  585. HAL_StatusTypeDef HAL_DMA_UnRegisterCallback(DMA_HandleTypeDef *hdma, HAL_DMA_CallbackIDTypeDef CallbackID)
  586. {
  587. HAL_StatusTypeDef status = HAL_OK;
  588. /* Process locked */
  589. __HAL_LOCK(hdma);
  590. if(HAL_DMA_STATE_READY == hdma->State)
  591. {
  592. switch (CallbackID)
  593. {
  594. case HAL_DMA_XFER_CPLT_CB_ID:
  595. hdma->XferCpltCallback = NULL;
  596. break;
  597. case HAL_DMA_XFER_HALFCPLT_CB_ID:
  598. hdma->XferHalfCpltCallback = NULL;
  599. break;
  600. case HAL_DMA_XFER_ERROR_CB_ID:
  601. hdma->XferErrorCallback = NULL;
  602. break;
  603. case HAL_DMA_XFER_ABORT_CB_ID:
  604. hdma->XferAbortCallback = NULL;
  605. break;
  606. case HAL_DMA_XFER_ALL_CB_ID:
  607. hdma->XferCpltCallback = NULL;
  608. hdma->XferHalfCpltCallback = NULL;
  609. hdma->XferErrorCallback = NULL;
  610. hdma->XferAbortCallback = NULL;
  611. break;
  612. default:
  613. status = HAL_ERROR;
  614. break;
  615. }
  616. }
  617. else
  618. {
  619. status = HAL_ERROR;
  620. }
  621. /* Release Lock */
  622. __HAL_UNLOCK(hdma);
  623. return status;
  624. }
  625. /**
  626. * @}
  627. */
  628. /** @defgroup DMA_Exported_Functions_Group3 Peripheral State functions
  629. * @brief Peripheral State functions
  630. *
  631. @verbatim
  632. ===============================================================================
  633. ##### State and Errors functions #####
  634. ===============================================================================
  635. [..]
  636. This subsection provides functions allowing to
  637. (+) Check the DMA state
  638. (+) Get error code
  639. @endverbatim
  640. * @{
  641. */
  642. /**
  643. * @brief Returns the DMA state.
  644. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  645. * the configuration information for the specified DMA Channel.
  646. * @retval HAL state
  647. */
  648. HAL_DMA_StateTypeDef HAL_DMA_GetState(DMA_HandleTypeDef *hdma)
  649. {
  650. return hdma->State;
  651. }
  652. /**
  653. * @brief Return the DMA error code
  654. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  655. * the configuration information for the specified DMA Channel.
  656. * @retval DMA Error Code
  657. */
  658. uint32_t HAL_DMA_GetError(DMA_HandleTypeDef *hdma)
  659. {
  660. return hdma->ErrorCode;
  661. }
  662. /**
  663. * @}
  664. */
  665. /**
  666. * @}
  667. */
  668. /** @addtogroup DMA_Private_Functions
  669. * @{
  670. */
  671. /**
  672. * @brief Set the DMA Transfer parameters.
  673. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  674. * the configuration information for the specified DMA Channel.
  675. * @param SrcAddress The source memory Buffer address
  676. * @param DstAddress The destination memory Buffer address
  677. * @param DataLength The length of data to be transferred from source to destination
  678. * @retval HAL status
  679. */
  680. static void DMA_SetConfig(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength)
  681. {
  682. /* Clear all flags */
  683. hdma->DmaBaseAddress->IFCR = (DMA_FLAG_GL1 << hdma->ChannelIndex);
  684. /* Configure DMA Channel data length */
  685. hdma->Instance->CNDTR = DataLength;
  686. /* Memory to Peripheral */
  687. if((hdma->Init.Direction) == DMA_MEMORY_TO_PERIPH)
  688. {
  689. /* Configure DMA Channel destination address */
  690. hdma->Instance->CPAR = DstAddress;
  691. /* Configure DMA Channel source address */
  692. hdma->Instance->CMAR = SrcAddress;
  693. }
  694. /* Peripheral to Memory */
  695. else
  696. {
  697. /* Configure DMA Channel source address */
  698. hdma->Instance->CPAR = SrcAddress;
  699. /* Configure DMA Channel destination address */
  700. hdma->Instance->CMAR = DstAddress;
  701. }
  702. }
  703. /**
  704. * @brief set the DMA base address and channel index depending on DMA instance
  705. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  706. * the configuration information for the specified DMA Stream.
  707. * @retval None
  708. */
  709. static void DMA_CalcBaseAndBitshift(DMA_HandleTypeDef *hdma)
  710. {
  711. #if defined (DMA2)
  712. /* calculation of the channel index */
  713. if ((uint32_t)(hdma->Instance) < (uint32_t)(DMA2_Channel1))
  714. {
  715. /* DMA1 */
  716. hdma->ChannelIndex = (((uint32_t)hdma->Instance - (uint32_t)DMA1_Channel1) / ((uint32_t)DMA1_Channel2 - (uint32_t)DMA1_Channel1)) << 2U;
  717. hdma->DmaBaseAddress = DMA1;
  718. }
  719. else
  720. {
  721. /* DMA2 */
  722. hdma->ChannelIndex = (((uint32_t)hdma->Instance - (uint32_t)DMA2_Channel1) / ((uint32_t)DMA2_Channel2 - (uint32_t)DMA2_Channel1)) << 2U;
  723. hdma->DmaBaseAddress = DMA2;
  724. }
  725. #else
  726. /* calculation of the channel index */
  727. /* DMA1 */
  728. hdma->ChannelIndex = (((uint32_t)hdma->Instance - (uint32_t)DMA1_Channel1) / ((uint32_t)DMA1_Channel2 - (uint32_t)DMA1_Channel1)) << 2U;
  729. hdma->DmaBaseAddress = DMA1;
  730. #endif
  731. }
  732. /**
  733. * @}
  734. */
  735. /**
  736. * @}
  737. */
  738. #endif /* HAL_DMA_MODULE_ENABLED */
  739. /**
  740. * @}
  741. */
  742. /**
  743. * @}
  744. */
  745. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/