stm32l4xx_hal_dma.c 29 KB

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