stm32f1xx_hal_dma.c 28 KB

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