stm32f0xx_hal_dma.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  1. /**
  2. ******************************************************************************
  3. * @file stm32f0xx_hal_dma.c
  4. * @author MCD Application Team
  5. * @version V1.4.0
  6. * @date 27-May-2016
  7. * @brief DMA HAL module driver.
  8. *
  9. * This file provides firmware functions to manage the following
  10. * functionalities of the Direct Memory Access (DMA) peripheral:
  11. * + Initialization and de-initialization functions
  12. * + IO operation functions
  13. * + Peripheral State and errors functions
  14. @verbatim
  15. ==============================================================================
  16. ##### How to use this driver #####
  17. ==============================================================================
  18. [..]
  19. (#) Enable and configure the peripheral to be connected to the DMA Channel
  20. (except for internal SRAM / FLASH memories: no initialization is
  21. necessary) please refer to Reference manual for connection between peripherals
  22. and DMA requests .
  23. (#) For a given Channel, program the required configuration through the following parameters:
  24. Transfer Direction, Source and Destination data formats,
  25. Circular or Normal mode, Channel Priority level, Source and Destination Increment mode,
  26. using HAL_DMA_Init() function.
  27. (#) Use HAL_DMA_GetState() function to return the DMA state and HAL_DMA_GetError() in case of error
  28. detection.
  29. (#) Use HAL_DMA_Abort() function to abort the current transfer
  30. -@- In Memory-to-Memory transfer mode, Circular mode is not allowed.
  31. *** Polling mode IO operation ***
  32. =================================
  33. [..]
  34. (+) Use HAL_DMA_Start() to start DMA transfer after the configuration of Source
  35. address and destination address and the Length of data to be transferred
  36. (+) Use HAL_DMA_PollForTransfer() to poll for the end of current transfer, in this
  37. case a fixed Timeout can be configured by User depending from his application.
  38. *** Interrupt mode IO operation ***
  39. ===================================
  40. [..]
  41. (+) Configure the DMA interrupt priority using HAL_NVIC_SetPriority()
  42. (+) Enable the DMA IRQ handler using HAL_NVIC_EnableIRQ()
  43. (+) Use HAL_DMA_Start_IT() to start DMA transfer after the configuration of
  44. Source address and destination address and the Length of data to be transferred.
  45. In this case the DMA interrupt is configured
  46. (+) Use HAL_DMAy_Channelx_IRQHandler() called under DMA_IRQHandler() Interrupt subroutine
  47. (+) At the end of data transfer HAL_DMA_IRQHandler() function is executed and user can
  48. add his own function by customization of function pointer XferCpltCallback and
  49. XferErrorCallback (i.e a member of DMA handle structure).
  50. *** DMA HAL driver macros list ***
  51. =============================================
  52. [..]
  53. Below the list of most used 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 has occurred 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) 2016 STMicroelectronics</center></h2>
  68. *
  69. * Redistribution and use in source and binary forms, with or without modification,
  70. * are permitted provided that the following conditions are met:
  71. * 1. Redistributions of source code must retain the above copyright notice,
  72. * this list of conditions and the following disclaimer.
  73. * 2. Redistributions in binary form must reproduce the above copyright notice,
  74. * this list of conditions and the following disclaimer in the documentation
  75. * and/or other materials provided with the distribution.
  76. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  77. * may be used to endorse or promote products derived from this software
  78. * without specific prior written permission.
  79. *
  80. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  81. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  82. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  83. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  84. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  85. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  86. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  87. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  88. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  89. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  90. *
  91. ******************************************************************************
  92. */
  93. /* Includes ------------------------------------------------------------------*/
  94. #include "stm32f0xx_hal.h"
  95. /** @addtogroup STM32F0xx_HAL_Driver
  96. * @{
  97. */
  98. #ifdef HAL_DMA_MODULE_ENABLED
  99. /** @defgroup DMA DMA
  100. * @brief DMA HAL module driver
  101. * @{
  102. */
  103. /* Private typedef -----------------------------------------------------------*/
  104. /* Private define ------------------------------------------------------------*/
  105. /** @defgroup DMA_Private_Constants DMA Private Constants
  106. * @{
  107. */
  108. #define HAL_TIMEOUT_DMA_ABORT ((uint32_t)1000) /* 1s */
  109. /**
  110. * @}
  111. */
  112. /* Private macro -------------------------------------------------------------*/
  113. /* Private macros ------------------------------------------------------------*/
  114. /** @defgroup DMA_Private_Macros DMA Private Macros
  115. * @{
  116. */
  117. /**
  118. * @}
  119. */
  120. /* Private variables ---------------------------------------------------------*/
  121. /* Private function prototypes -----------------------------------------------*/
  122. /** @defgroup DMA_Private_Functions DMA Private Functions
  123. * @{
  124. */
  125. static void DMA_SetConfig(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength);
  126. /**
  127. * @}
  128. */
  129. /* Exported functions ---------------------------------------------------------*/
  130. /** @defgroup DMA_Exported_Functions DMA Exported Functions
  131. * @{
  132. */
  133. /** @defgroup DMA_Exported_Functions_Group1 Initialization and de-initialization functions
  134. * @brief Initialization and de-initialization functions
  135. *
  136. @verbatim
  137. ===============================================================================
  138. ##### Initialization and de-initialization functions #####
  139. ===============================================================================
  140. [..]
  141. This section provides functions allowing to initialize the DMA Channel source
  142. and destination addresses, incrementation and data sizes, transfer direction,
  143. circular/normal mode selection, memory-to-memory mode selection and Channel priority value.
  144. [..]
  145. The HAL_DMA_Init() function follows the DMA configuration procedures as described in
  146. reference manual.
  147. @endverbatim
  148. * @{
  149. */
  150. /**
  151. * @brief Initializes the DMA according to the specified
  152. * parameters in the DMA_InitTypeDef and create the associated handle.
  153. * @param hdma: Pointer to a DMA_HandleTypeDef structure that contains
  154. * the configuration information for the specified DMA Channel.
  155. * @retval HAL status
  156. */
  157. HAL_StatusTypeDef HAL_DMA_Init(DMA_HandleTypeDef *hdma)
  158. {
  159. uint32_t tmp = 0;
  160. /* Check the DMA handle allocation */
  161. if(hdma == NULL)
  162. {
  163. return HAL_ERROR;
  164. }
  165. /* Check the parameters */
  166. assert_param(IS_DMA_ALL_INSTANCE(hdma->Instance));
  167. assert_param(IS_DMA_DIRECTION(hdma->Init.Direction));
  168. assert_param(IS_DMA_PERIPHERAL_INC_STATE(hdma->Init.PeriphInc));
  169. assert_param(IS_DMA_MEMORY_INC_STATE(hdma->Init.MemInc));
  170. assert_param(IS_DMA_PERIPHERAL_DATA_SIZE(hdma->Init.PeriphDataAlignment));
  171. assert_param(IS_DMA_MEMORY_DATA_SIZE(hdma->Init.MemDataAlignment));
  172. assert_param(IS_DMA_MODE(hdma->Init.Mode));
  173. assert_param(IS_DMA_PRIORITY(hdma->Init.Priority));
  174. if(hdma->State == HAL_DMA_STATE_RESET)
  175. {
  176. /* Allocate lock resource and initialize it */
  177. hdma->Lock = HAL_UNLOCKED;
  178. }
  179. /* Change DMA peripheral state */
  180. hdma->State = HAL_DMA_STATE_BUSY;
  181. /* Get the CR register value */
  182. tmp = hdma->Instance->CCR;
  183. /* Clear PL, MSIZE, PSIZE, MINC, PINC, CIRC, DIR bits */
  184. tmp &= ((uint32_t)~(DMA_CCR_PL | DMA_CCR_MSIZE | DMA_CCR_PSIZE | \
  185. DMA_CCR_MINC | DMA_CCR_PINC | DMA_CCR_CIRC | \
  186. DMA_CCR_DIR));
  187. /* Prepare the DMA Channel configuration */
  188. tmp |= hdma->Init.Direction |
  189. hdma->Init.PeriphInc | hdma->Init.MemInc |
  190. hdma->Init.PeriphDataAlignment | hdma->Init.MemDataAlignment |
  191. hdma->Init.Mode | hdma->Init.Priority;
  192. /* Write to DMA Channel CR register */
  193. hdma->Instance->CCR = tmp;
  194. /* Initialise the error code */
  195. hdma->ErrorCode = HAL_DMA_ERROR_NONE;
  196. /* Initialize the DMA state*/
  197. hdma->State = HAL_DMA_STATE_READY;
  198. return HAL_OK;
  199. }
  200. /**
  201. * @brief DeInitializes the DMA peripheral
  202. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  203. * the configuration information for the specified DMA Channel.
  204. * @retval HAL status
  205. */
  206. HAL_StatusTypeDef HAL_DMA_DeInit(DMA_HandleTypeDef *hdma)
  207. {
  208. /* Check the DMA handle allocation */
  209. if(hdma == NULL)
  210. {
  211. return HAL_ERROR;
  212. }
  213. /* Check the parameters */
  214. assert_param(IS_DMA_ALL_INSTANCE(hdma->Instance));
  215. /* Disable the selected DMA Channelx */
  216. __HAL_DMA_DISABLE(hdma);
  217. /* Reset DMA Channel control register */
  218. hdma->Instance->CCR = 0;
  219. /* Reset DMA Channel Number of Data to Transfer register */
  220. hdma->Instance->CNDTR = 0;
  221. /* Reset DMA Channel peripheral address register */
  222. hdma->Instance->CPAR = 0;
  223. /* Reset DMA Channel memory address register */
  224. hdma->Instance->CMAR = 0;
  225. /* Clear all flags */
  226. __HAL_DMA_CLEAR_FLAG(hdma, __HAL_DMA_GET_TC_FLAG_INDEX(hdma));
  227. __HAL_DMA_CLEAR_FLAG(hdma, __HAL_DMA_GET_TE_FLAG_INDEX(hdma));
  228. __HAL_DMA_CLEAR_FLAG(hdma, __HAL_DMA_GET_HT_FLAG_INDEX(hdma));
  229. /* Initialize the error code */
  230. hdma->ErrorCode = HAL_DMA_ERROR_NONE;
  231. /* Initialize the DMA state */
  232. hdma->State = HAL_DMA_STATE_RESET;
  233. /* Release Lock */
  234. __HAL_UNLOCK(hdma);
  235. return HAL_OK;
  236. }
  237. /**
  238. * @}
  239. */
  240. /** @defgroup DMA_Exported_Functions_Group2 Input and Output operation functions
  241. * @brief I/O operation functions
  242. *
  243. @verbatim
  244. ===============================================================================
  245. ##### IO operation functions #####
  246. ===============================================================================
  247. [..] This section provides functions allowing to:
  248. (+) Configure the source, destination address and data length and Start DMA transfer
  249. (+) Configure the source, destination address and data length and
  250. Start DMA transfer with interrupt
  251. (+) Abort DMA transfer
  252. (+) Poll for transfer complete
  253. (+) Handle DMA interrupt request
  254. @endverbatim
  255. * @{
  256. */
  257. /**
  258. * @brief Starts the DMA Transfer.
  259. * @param hdma : pointer to a DMA_HandleTypeDef structure that contains
  260. * the configuration information for the specified DMA Channel.
  261. * @param SrcAddress: The source memory Buffer address
  262. * @param DstAddress: The destination memory Buffer address
  263. * @param DataLength: The length of data to be transferred from source to destination
  264. * @retval HAL status
  265. */
  266. HAL_StatusTypeDef HAL_DMA_Start(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength)
  267. {
  268. /* Process locked */
  269. __HAL_LOCK(hdma);
  270. /* Change DMA peripheral state */
  271. hdma->State = HAL_DMA_STATE_BUSY;
  272. /* Check the parameters */
  273. assert_param(IS_DMA_BUFFER_SIZE(DataLength));
  274. /* Disable the peripheral */
  275. __HAL_DMA_DISABLE(hdma);
  276. /* Configure the source, destination address and the data length */
  277. DMA_SetConfig(hdma, SrcAddress, DstAddress, DataLength);
  278. /* Enable the Peripheral */
  279. __HAL_DMA_ENABLE(hdma);
  280. return HAL_OK;
  281. }
  282. /**
  283. * @brief Start the DMA Transfer with interrupt enabled.
  284. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  285. * the configuration information for the specified DMA Channel.
  286. * @param SrcAddress: The source memory Buffer address
  287. * @param DstAddress: The destination memory Buffer address
  288. * @param DataLength: The length of data to be transferred from source to destination
  289. * @retval HAL status
  290. */
  291. HAL_StatusTypeDef HAL_DMA_Start_IT(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength)
  292. {
  293. /* Process locked */
  294. __HAL_LOCK(hdma);
  295. /* Change DMA peripheral state */
  296. hdma->State = HAL_DMA_STATE_BUSY;
  297. /* Check the parameters */
  298. assert_param(IS_DMA_BUFFER_SIZE(DataLength));
  299. /* Disable the peripheral */
  300. __HAL_DMA_DISABLE(hdma);
  301. /* Configure the source, destination address and the data length */
  302. DMA_SetConfig(hdma, SrcAddress, DstAddress, DataLength);
  303. /* Enable the transfer complete interrupt */
  304. __HAL_DMA_ENABLE_IT(hdma, DMA_IT_TC);
  305. /* Enable the Half transfer complete interrupt */
  306. __HAL_DMA_ENABLE_IT(hdma, DMA_IT_HT);
  307. /* Enable the transfer Error interrupt */
  308. __HAL_DMA_ENABLE_IT(hdma, DMA_IT_TE);
  309. /* Enable the Peripheral */
  310. __HAL_DMA_ENABLE(hdma);
  311. return HAL_OK;
  312. }
  313. /**
  314. * @brief Aborts the DMA Transfer.
  315. * @param hdma : pointer to a DMA_HandleTypeDef structure that contains
  316. * the configuration information for the specified DMA Channel.
  317. *
  318. * @note After disabling a DMA Channel, a check for wait until the DMA Channel is
  319. * effectively disabled is added. If a Channel is disabled
  320. * while a data transfer is ongoing, the current data will be transferred
  321. * and the Channel will be effectively disabled only after the transfer of
  322. * this single data is finished.
  323. * @retval HAL status
  324. */
  325. HAL_StatusTypeDef HAL_DMA_Abort(DMA_HandleTypeDef *hdma)
  326. {
  327. uint32_t tickstart = 0x00;
  328. /* Disable the channel */
  329. __HAL_DMA_DISABLE(hdma);
  330. /* Get tick */
  331. tickstart = HAL_GetTick();
  332. /* Check if the DMA Channel is effectively disabled */
  333. while((hdma->Instance->CCR & DMA_CCR_EN) != 0)
  334. {
  335. /* Check for the Timeout */
  336. if((HAL_GetTick() - tickstart) > HAL_TIMEOUT_DMA_ABORT)
  337. {
  338. /* Update error code */
  339. SET_BIT(hdma->ErrorCode, HAL_DMA_ERROR_TIMEOUT);
  340. /* Change the DMA state */
  341. hdma->State = HAL_DMA_STATE_TIMEOUT;
  342. /* Process Unlocked */
  343. __HAL_UNLOCK(hdma);
  344. return HAL_TIMEOUT;
  345. }
  346. }
  347. /* Change the DMA state*/
  348. hdma->State = HAL_DMA_STATE_READY;
  349. /* Process Unlocked */
  350. __HAL_UNLOCK(hdma);
  351. return HAL_OK;
  352. }
  353. /**
  354. * @brief Aborts the DMA Transfer in Interrupt mode.
  355. * @param hdma : pointer to a DMA_HandleTypeDef structure that contains
  356. * the configuration information for the specified DMA Stream.
  357. * @retval HAL status
  358. */
  359. HAL_StatusTypeDef HAL_DMA_Abort_IT(DMA_HandleTypeDef *hdma)
  360. {
  361. HAL_StatusTypeDef status = HAL_OK;
  362. if(HAL_DMA_STATE_BUSY != hdma->State)
  363. {
  364. /* no transfer ongoing */
  365. hdma->ErrorCode = HAL_DMA_ERROR_NO_XFER;
  366. status = HAL_ERROR;
  367. }
  368. else
  369. {
  370. /* Disable DMA IT */
  371. __HAL_DMA_DISABLE_IT(hdma, (DMA_IT_TC | DMA_IT_HT | DMA_IT_TE));
  372. /* Disable the channel */
  373. __HAL_DMA_DISABLE(hdma);
  374. /* Clear all flags */
  375. __HAL_DMA_CLEAR_FLAG(hdma, __HAL_DMA_GET_GI_FLAG_INDEX(hdma));
  376. /* Change the DMA state */
  377. hdma->State = HAL_DMA_STATE_READY;
  378. /* Process Unlocked */
  379. __HAL_UNLOCK(hdma);
  380. /* Call User Abort callback */
  381. if(hdma->XferAbortCallback != NULL)
  382. {
  383. hdma->XferAbortCallback(hdma);
  384. }
  385. }
  386. return status;
  387. }
  388. /**
  389. * @brief Polling for transfer complete.
  390. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  391. * the configuration information for the specified DMA Channel.
  392. * @param CompleteLevel: Specifies the DMA level complete.
  393. * @param Timeout: Timeout duration.
  394. * @retval HAL status
  395. */
  396. HAL_StatusTypeDef HAL_DMA_PollForTransfer(DMA_HandleTypeDef *hdma, uint32_t CompleteLevel, uint32_t Timeout)
  397. {
  398. uint32_t temp;
  399. uint32_t tickstart = 0x00;
  400. /* Get the level transfer complete flag */
  401. if(CompleteLevel == HAL_DMA_FULL_TRANSFER)
  402. {
  403. /* Transfer Complete flag */
  404. temp = __HAL_DMA_GET_TC_FLAG_INDEX(hdma);
  405. }
  406. else
  407. {
  408. /* Half Transfer Complete flag */
  409. temp = __HAL_DMA_GET_HT_FLAG_INDEX(hdma);
  410. }
  411. /* Get tick */
  412. tickstart = HAL_GetTick();
  413. while(__HAL_DMA_GET_FLAG(hdma, temp) == RESET)
  414. {
  415. if((__HAL_DMA_GET_FLAG(hdma, __HAL_DMA_GET_TE_FLAG_INDEX(hdma)) != RESET))
  416. {
  417. /* Clear the transfer error flags */
  418. __HAL_DMA_CLEAR_FLAG(hdma, __HAL_DMA_GET_TE_FLAG_INDEX(hdma));
  419. /* Update error code */
  420. SET_BIT(hdma->ErrorCode, HAL_DMA_ERROR_TE);
  421. /* Change the DMA state */
  422. hdma->State= HAL_DMA_STATE_ERROR;
  423. /* Process Unlocked */
  424. __HAL_UNLOCK(hdma);
  425. return HAL_ERROR;
  426. }
  427. /* Check for the Timeout */
  428. if(Timeout != HAL_MAX_DELAY)
  429. {
  430. if((Timeout == 0) || ((HAL_GetTick() - tickstart) > Timeout))
  431. {
  432. /* Update error code */
  433. SET_BIT(hdma->ErrorCode, HAL_DMA_ERROR_TIMEOUT);
  434. /* Change the DMA state */
  435. hdma->State = HAL_DMA_STATE_TIMEOUT;
  436. /* Process Unlocked */
  437. __HAL_UNLOCK(hdma);
  438. return HAL_TIMEOUT;
  439. }
  440. }
  441. }
  442. if(CompleteLevel == HAL_DMA_FULL_TRANSFER)
  443. {
  444. /* Clear the transfer complete flag */
  445. __HAL_DMA_CLEAR_FLAG(hdma, __HAL_DMA_GET_TC_FLAG_INDEX(hdma));
  446. /* The selected Channelx EN bit is cleared (DMA is disabled and
  447. all transfers are complete) */
  448. hdma->State = HAL_DMA_STATE_READY;
  449. }
  450. else
  451. {
  452. /* Clear the half transfer complete flag */
  453. __HAL_DMA_CLEAR_FLAG(hdma, __HAL_DMA_GET_HT_FLAG_INDEX(hdma));
  454. /* The selected Channelx EN bit is cleared (DMA is disabled and
  455. all transfers of half buffer are complete) */
  456. hdma->State = HAL_DMA_STATE_READY_HALF;
  457. }
  458. /* Process unlocked */
  459. __HAL_UNLOCK(hdma);
  460. return HAL_OK;
  461. }
  462. /**
  463. * @brief Handles DMA interrupt request.
  464. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  465. * the configuration information for the specified DMA Channel.
  466. * @retval None
  467. */
  468. void HAL_DMA_IRQHandler(DMA_HandleTypeDef *hdma)
  469. {
  470. /* Transfer Error Interrupt management ***************************************/
  471. if(__HAL_DMA_GET_FLAG(hdma, __HAL_DMA_GET_TE_FLAG_INDEX(hdma)) != RESET)
  472. {
  473. if(__HAL_DMA_GET_IT_SOURCE(hdma, DMA_IT_TE) != RESET)
  474. {
  475. /* Disable the transfer error interrupt */
  476. __HAL_DMA_DISABLE_IT(hdma, DMA_IT_TE);
  477. /* Clear the transfer error flag */
  478. __HAL_DMA_CLEAR_FLAG(hdma, __HAL_DMA_GET_TE_FLAG_INDEX(hdma));
  479. /* Update error code */
  480. SET_BIT(hdma->ErrorCode, HAL_DMA_ERROR_TE);
  481. /* Change the DMA state */
  482. hdma->State = HAL_DMA_STATE_ERROR;
  483. /* Process Unlocked */
  484. __HAL_UNLOCK(hdma);
  485. if(hdma->XferErrorCallback != NULL)
  486. {
  487. /* Transfer error callback */
  488. hdma->XferErrorCallback(hdma);
  489. }
  490. }
  491. }
  492. /* Half Transfer Complete Interrupt management ******************************/
  493. if(__HAL_DMA_GET_FLAG(hdma, __HAL_DMA_GET_HT_FLAG_INDEX(hdma)) != RESET)
  494. {
  495. if(__HAL_DMA_GET_IT_SOURCE(hdma, DMA_IT_HT) != RESET)
  496. {
  497. /* Disable the half transfer interrupt if the DMA mode is not CIRCULAR */
  498. if((hdma->Instance->CCR & DMA_CCR_CIRC) == 0)
  499. {
  500. /* Disable the half transfer interrupt */
  501. __HAL_DMA_DISABLE_IT(hdma, DMA_IT_HT);
  502. }
  503. /* Clear the half transfer complete flag */
  504. __HAL_DMA_CLEAR_FLAG(hdma, __HAL_DMA_GET_HT_FLAG_INDEX(hdma));
  505. /* Change DMA peripheral state */
  506. hdma->State = HAL_DMA_STATE_READY_HALF;
  507. if(hdma->XferHalfCpltCallback != NULL)
  508. {
  509. /* Half transfer callback */
  510. hdma->XferHalfCpltCallback(hdma);
  511. }
  512. }
  513. }
  514. /* Transfer Complete Interrupt management ***********************************/
  515. if(__HAL_DMA_GET_FLAG(hdma, __HAL_DMA_GET_TC_FLAG_INDEX(hdma)) != RESET)
  516. {
  517. if(__HAL_DMA_GET_IT_SOURCE(hdma, DMA_IT_TC) != RESET)
  518. {
  519. if((hdma->Instance->CCR & DMA_CCR_CIRC) == 0)
  520. {
  521. /* Disable the transfer complete interrupt */
  522. __HAL_DMA_DISABLE_IT(hdma, DMA_IT_TC);
  523. }
  524. /* Clear the transfer complete flag */
  525. __HAL_DMA_CLEAR_FLAG(hdma, __HAL_DMA_GET_TC_FLAG_INDEX(hdma));
  526. /* Update error code */
  527. SET_BIT(hdma->ErrorCode, HAL_DMA_ERROR_NONE);
  528. /* Change the DMA state */
  529. hdma->State = HAL_DMA_STATE_READY;
  530. /* Process Unlocked */
  531. __HAL_UNLOCK(hdma);
  532. if(hdma->XferCpltCallback != NULL)
  533. {
  534. /* Transfer complete callback */
  535. hdma->XferCpltCallback(hdma);
  536. }
  537. }
  538. }
  539. }
  540. /**
  541. * @}
  542. */
  543. /** @defgroup DMA_Exported_Functions_Group3 Peripheral State functions
  544. * @brief Peripheral State functions
  545. *
  546. @verbatim
  547. ===============================================================================
  548. ##### State and Errors functions #####
  549. ===============================================================================
  550. [..]
  551. This subsection provides functions allowing to
  552. (+) Check the DMA state
  553. (+) Get error code
  554. @endverbatim
  555. * @{
  556. */
  557. /**
  558. * @brief Returns the DMA state.
  559. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  560. * the configuration information for the specified DMA Channel.
  561. * @retval HAL state
  562. */
  563. HAL_DMA_StateTypeDef HAL_DMA_GetState(DMA_HandleTypeDef *hdma)
  564. {
  565. return hdma->State;
  566. }
  567. /**
  568. * @brief Return the DMA error code
  569. * @param hdma : pointer to a DMA_HandleTypeDef structure that contains
  570. * the configuration information for the specified DMA Channel.
  571. * @retval DMA Error Code
  572. */
  573. uint32_t HAL_DMA_GetError(DMA_HandleTypeDef *hdma)
  574. {
  575. return hdma->ErrorCode;
  576. }
  577. /**
  578. * @}
  579. */
  580. /**
  581. * @}
  582. */
  583. /** @addtogroup DMA_Private_Functions
  584. * @{
  585. */
  586. /**
  587. * @brief Sets the DMA Transfer parameter.
  588. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  589. * the configuration information for the specified DMA Channel.
  590. * @param SrcAddress: The source memory Buffer address
  591. * @param DstAddress: The destination memory Buffer address
  592. * @param DataLength: The length of data to be transferred from source to destination
  593. * @retval HAL status
  594. */
  595. static void DMA_SetConfig(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength)
  596. {
  597. /* Configure DMA Channel data length */
  598. hdma->Instance->CNDTR = DataLength;
  599. /* Peripheral to Memory */
  600. if((hdma->Init.Direction) == DMA_MEMORY_TO_PERIPH)
  601. {
  602. /* Configure DMA Channel destination address */
  603. hdma->Instance->CPAR = DstAddress;
  604. /* Configure DMA Channel source address */
  605. hdma->Instance->CMAR = SrcAddress;
  606. }
  607. /* Memory to Peripheral */
  608. else
  609. {
  610. /* Configure DMA Channel source address */
  611. hdma->Instance->CPAR = SrcAddress;
  612. /* Configure DMA Channel destination address */
  613. hdma->Instance->CMAR = DstAddress;
  614. }
  615. }
  616. /**
  617. * @}
  618. */
  619. /**
  620. * @}
  621. */
  622. #endif /* HAL_DMA_MODULE_ENABLED */
  623. /**
  624. * @}
  625. */
  626. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/