stm32f0xx_hal_dma.c 29 KB

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