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.
 
 
 
 
 
 

814 lines
26 KiB

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