You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

900 lines
28 KiB

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