stm32f1xx_hal_dma.c 28 KB

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