Implement a secure ICS protocol targeting LoRa Node151 microcontroller for controlling irrigation.
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.
 
 
 
 
 
 

909 lines
27 KiB

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