stm32f4xx_hal_dma.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal_dma.c
  4. * @author MCD Application Team
  5. * @version V1.4.3
  6. * @date 11-December-2015
  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 Stream
  20. (except for internal SRAM/FLASH memories: no initialization is
  21. necessary) please refer to Reference manual for connection between peripherals
  22. and DMA requests .
  23. (#) For a given Stream, program the required configuration through the following parameters:
  24. Transfer Direction, Source and Destination data formats,
  25. Circular, Normal or peripheral flow control mode, Stream Priority level,
  26. Source and Destination Increment mode, FIFO mode and its Threshold (if needed),
  27. Burst mode for Source and/or Destination (if needed) using HAL_DMA_Init() function.
  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. In this
  42. 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. [..]
  48. (#) Use HAL_DMA_GetState() function to return the DMA state and HAL_DMA_GetError() in case of error
  49. detection.
  50. (#) Use HAL_DMA_Abort() function to abort the current transfer
  51. -@- In Memory-to-Memory transfer mode, Circular mode is not allowed.
  52. -@- The FIFO is used mainly to reduce bus usage and to allow data packing/unpacking: it is
  53. possible to set different Data Sizes for the Peripheral and the Memory (ie. you can set
  54. Half-Word data size for the peripheral to access its data register and set Word data size
  55. for the Memory to gain in access time. Each two half words will be packed and written in
  56. a single access to a Word in the Memory).
  57. -@- When FIFO is disabled, it is not allowed to configure different Data Sizes for Source
  58. and Destination. In this case the Peripheral Data Size will be applied to both Source
  59. and Destination.
  60. *** DMA HAL driver macros list ***
  61. =============================================
  62. [..]
  63. Below the list of most used macros in DMA HAL driver.
  64. (+) __HAL_DMA_ENABLE: Enable the specified DMA Stream.
  65. (+) __HAL_DMA_DISABLE: Disable the specified DMA Stream.
  66. (+) __HAL_DMA_DISABLE_IT: Disable the specified DMA Stream interrupts.
  67. (+) __HAL_DMA_GET_IT_SOURCE: Check whether the specified DMA Stream interrupt has occurred or not.
  68. [..]
  69. (@) You can refer to the DMA HAL driver header file for more useful macros
  70. @endverbatim
  71. ******************************************************************************
  72. * @attention
  73. *
  74. * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
  75. *
  76. * Redistribution and use in source and binary forms, with or without modification,
  77. * are permitted provided that the following conditions are met:
  78. * 1. Redistributions of source code must retain the above copyright notice,
  79. * this list of conditions and the following disclaimer.
  80. * 2. Redistributions in binary form must reproduce the above copyright notice,
  81. * this list of conditions and the following disclaimer in the documentation
  82. * and/or other materials provided with the distribution.
  83. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  84. * may be used to endorse or promote products derived from this software
  85. * without specific prior written permission.
  86. *
  87. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  88. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  89. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  90. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  91. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  92. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  93. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  94. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  95. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  96. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  97. *
  98. ******************************************************************************
  99. */
  100. /* Includes ------------------------------------------------------------------*/
  101. #include "stm32f4xx_hal.h"
  102. /** @addtogroup STM32F4xx_HAL_Driver
  103. * @{
  104. */
  105. /** @defgroup DMA DMA
  106. * @brief DMA HAL module driver
  107. * @{
  108. */
  109. #ifdef HAL_DMA_MODULE_ENABLED
  110. /* Private types -------------------------------------------------------------*/
  111. typedef struct
  112. {
  113. __IO uint32_t ISR; /*!< DMA interrupt status register */
  114. __IO uint32_t Reserved0;
  115. __IO uint32_t IFCR; /*!< DMA interrupt flag clear register */
  116. } DMA_Base_Registers;
  117. /* Private variables ---------------------------------------------------------*/
  118. /* Private constants ---------------------------------------------------------*/
  119. /** @addtogroup DMA_Private_Constants
  120. * @{
  121. */
  122. #define HAL_TIMEOUT_DMA_ABORT ((uint32_t)1000) /* 1s */
  123. /**
  124. * @}
  125. */
  126. /* Private macros ------------------------------------------------------------*/
  127. /* Private functions ---------------------------------------------------------*/
  128. /** @addtogroup DMA_Private_Functions
  129. * @{
  130. */
  131. static void DMA_SetConfig(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength);
  132. static uint32_t DMA_CalcBaseAndBitshift(DMA_HandleTypeDef *hdma);
  133. /**
  134. * @}
  135. */
  136. /* Exported functions ---------------------------------------------------------*/
  137. /** @addtogroup DMA_Exported_Functions
  138. * @{
  139. */
  140. /** @addtogroup DMA_Exported_Functions_Group1
  141. *
  142. @verbatim
  143. ===============================================================================
  144. ##### Initialization and de-initialization functions #####
  145. ===============================================================================
  146. [..]
  147. This section provides functions allowing to initialize the DMA Stream source
  148. and destination addresses, incrementation and data sizes, transfer direction,
  149. circular/normal mode selection, memory-to-memory mode selection and Stream priority value.
  150. [..]
  151. The HAL_DMA_Init() function follows the DMA configuration procedures as described in
  152. reference manual.
  153. @endverbatim
  154. * @{
  155. */
  156. /**
  157. * @brief Initializes the DMA according to the specified
  158. * parameters in the DMA_InitTypeDef and create the associated handle.
  159. * @param hdma: Pointer to a DMA_HandleTypeDef structure that contains
  160. * the configuration information for the specified DMA Stream.
  161. * @retval HAL status
  162. */
  163. HAL_StatusTypeDef HAL_DMA_Init(DMA_HandleTypeDef *hdma)
  164. {
  165. uint32_t tmp = 0;
  166. /* Check the DMA peripheral state */
  167. if(hdma == NULL)
  168. {
  169. return HAL_ERROR;
  170. }
  171. /* Check the parameters */
  172. assert_param(IS_DMA_STREAM_ALL_INSTANCE(hdma->Instance));
  173. assert_param(IS_DMA_CHANNEL(hdma->Init.Channel));
  174. assert_param(IS_DMA_DIRECTION(hdma->Init.Direction));
  175. assert_param(IS_DMA_PERIPHERAL_INC_STATE(hdma->Init.PeriphInc));
  176. assert_param(IS_DMA_MEMORY_INC_STATE(hdma->Init.MemInc));
  177. assert_param(IS_DMA_PERIPHERAL_DATA_SIZE(hdma->Init.PeriphDataAlignment));
  178. assert_param(IS_DMA_MEMORY_DATA_SIZE(hdma->Init.MemDataAlignment));
  179. assert_param(IS_DMA_MODE(hdma->Init.Mode));
  180. assert_param(IS_DMA_PRIORITY(hdma->Init.Priority));
  181. assert_param(IS_DMA_FIFO_MODE_STATE(hdma->Init.FIFOMode));
  182. /* Check the memory burst, peripheral burst and FIFO threshold parameters only
  183. when FIFO mode is enabled */
  184. if(hdma->Init.FIFOMode != DMA_FIFOMODE_DISABLE)
  185. {
  186. assert_param(IS_DMA_FIFO_THRESHOLD(hdma->Init.FIFOThreshold));
  187. assert_param(IS_DMA_MEMORY_BURST(hdma->Init.MemBurst));
  188. assert_param(IS_DMA_PERIPHERAL_BURST(hdma->Init.PeriphBurst));
  189. }
  190. /* Change DMA peripheral state */
  191. hdma->State = HAL_DMA_STATE_BUSY;
  192. /* Get the CR register value */
  193. tmp = hdma->Instance->CR;
  194. /* Clear CHSEL, MBURST, PBURST, PL, MSIZE, PSIZE, MINC, PINC, CIRC, DIR, CT and DBM bits */
  195. tmp &= ((uint32_t)~(DMA_SxCR_CHSEL | DMA_SxCR_MBURST | DMA_SxCR_PBURST | \
  196. DMA_SxCR_PL | DMA_SxCR_MSIZE | DMA_SxCR_PSIZE | \
  197. DMA_SxCR_MINC | DMA_SxCR_PINC | DMA_SxCR_CIRC | \
  198. DMA_SxCR_DIR | DMA_SxCR_CT | DMA_SxCR_DBM));
  199. /* Prepare the DMA Stream configuration */
  200. tmp |= hdma->Init.Channel | hdma->Init.Direction |
  201. hdma->Init.PeriphInc | hdma->Init.MemInc |
  202. hdma->Init.PeriphDataAlignment | hdma->Init.MemDataAlignment |
  203. hdma->Init.Mode | hdma->Init.Priority;
  204. /* the Memory burst and peripheral burst are not used when the FIFO is disabled */
  205. if(hdma->Init.FIFOMode == DMA_FIFOMODE_ENABLE)
  206. {
  207. /* Get memory burst and peripheral burst */
  208. tmp |= hdma->Init.MemBurst | hdma->Init.PeriphBurst;
  209. }
  210. /* Write to DMA Stream CR register */
  211. hdma->Instance->CR = tmp;
  212. /* Get the FCR register value */
  213. tmp = hdma->Instance->FCR;
  214. /* Clear Direct mode and FIFO threshold bits */
  215. tmp &= (uint32_t)~(DMA_SxFCR_DMDIS | DMA_SxFCR_FTH);
  216. /* Prepare the DMA Stream FIFO configuration */
  217. tmp |= hdma->Init.FIFOMode;
  218. /* the FIFO threshold is not used when the FIFO mode is disabled */
  219. if(hdma->Init.FIFOMode == DMA_FIFOMODE_ENABLE)
  220. {
  221. /* Get the FIFO threshold */
  222. tmp |= hdma->Init.FIFOThreshold;
  223. }
  224. /* Write to DMA Stream FCR */
  225. hdma->Instance->FCR = tmp;
  226. /* Initialize StreamBaseAddress and StreamIndex parameters to be used to calculate
  227. DMA steam Base Address needed by HAL_DMA_IRQHandler() and HAL_DMA_PollForTransfer() */
  228. DMA_CalcBaseAndBitshift(hdma);
  229. /* Initialize the error code */
  230. hdma->ErrorCode = HAL_DMA_ERROR_NONE;
  231. /* Initialize the DMA state */
  232. hdma->State = HAL_DMA_STATE_READY;
  233. return HAL_OK;
  234. }
  235. /**
  236. * @brief DeInitializes the DMA peripheral
  237. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  238. * the configuration information for the specified DMA Stream.
  239. * @retval HAL status
  240. */
  241. HAL_StatusTypeDef HAL_DMA_DeInit(DMA_HandleTypeDef *hdma)
  242. {
  243. DMA_Base_Registers *regs;
  244. /* Check the DMA peripheral state */
  245. if(hdma == NULL)
  246. {
  247. return HAL_ERROR;
  248. }
  249. /* Check the DMA peripheral state */
  250. if(hdma->State == HAL_DMA_STATE_BUSY)
  251. {
  252. return HAL_ERROR;
  253. }
  254. /* Disable the selected DMA Streamx */
  255. __HAL_DMA_DISABLE(hdma);
  256. /* Reset DMA Streamx control register */
  257. hdma->Instance->CR = 0;
  258. /* Reset DMA Streamx number of data to transfer register */
  259. hdma->Instance->NDTR = 0;
  260. /* Reset DMA Streamx peripheral address register */
  261. hdma->Instance->PAR = 0;
  262. /* Reset DMA Streamx memory 0 address register */
  263. hdma->Instance->M0AR = 0;
  264. /* Reset DMA Streamx memory 1 address register */
  265. hdma->Instance->M1AR = 0;
  266. /* Reset DMA Streamx FIFO control register */
  267. hdma->Instance->FCR = (uint32_t)0x00000021;
  268. /* Get DMA steam Base Address */
  269. regs = (DMA_Base_Registers *)DMA_CalcBaseAndBitshift(hdma);
  270. /* Clear all interrupt flags at correct offset within the register */
  271. regs->IFCR = 0x3F << hdma->StreamIndex;
  272. /* Initialize the error code */
  273. hdma->ErrorCode = HAL_DMA_ERROR_NONE;
  274. /* Initialize the DMA state */
  275. hdma->State = HAL_DMA_STATE_RESET;
  276. /* Release Lock */
  277. __HAL_UNLOCK(hdma);
  278. return HAL_OK;
  279. }
  280. /**
  281. * @}
  282. */
  283. /** @addtogroup DMA_Exported_Functions_Group2
  284. *
  285. @verbatim
  286. ===============================================================================
  287. ##### IO operation functions #####
  288. ===============================================================================
  289. [..] This section provides functions allowing to:
  290. (+) Configure the source, destination address and data length and Start DMA transfer
  291. (+) Configure the source, destination address and data length and
  292. Start DMA transfer with interrupt
  293. (+) Abort DMA transfer
  294. (+) Poll for transfer complete
  295. (+) Handle DMA interrupt request
  296. @endverbatim
  297. * @{
  298. */
  299. /**
  300. * @brief Starts the DMA Transfer.
  301. * @param hdma : pointer to a DMA_HandleTypeDef structure that contains
  302. * the configuration information for the specified DMA Stream.
  303. * @param SrcAddress: The source memory Buffer address
  304. * @param DstAddress: The destination memory Buffer address
  305. * @param DataLength: The length of data to be transferred from source to destination
  306. * @retval HAL status
  307. */
  308. HAL_StatusTypeDef HAL_DMA_Start(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength)
  309. {
  310. /* Process locked */
  311. __HAL_LOCK(hdma);
  312. /* Change DMA peripheral state */
  313. hdma->State = HAL_DMA_STATE_BUSY;
  314. /* Check the parameters */
  315. assert_param(IS_DMA_BUFFER_SIZE(DataLength));
  316. /* Disable the peripheral */
  317. __HAL_DMA_DISABLE(hdma);
  318. /* Configure the source, destination address and the data length */
  319. DMA_SetConfig(hdma, SrcAddress, DstAddress, DataLength);
  320. /* Enable the Peripheral */
  321. __HAL_DMA_ENABLE(hdma);
  322. return HAL_OK;
  323. }
  324. /**
  325. * @brief Start the DMA Transfer with interrupt enabled.
  326. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  327. * the configuration information for the specified DMA Stream.
  328. * @param SrcAddress: The source memory Buffer address
  329. * @param DstAddress: The destination memory Buffer address
  330. * @param DataLength: The length of data to be transferred from source to destination
  331. * @retval HAL status
  332. */
  333. HAL_StatusTypeDef HAL_DMA_Start_IT(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength)
  334. {
  335. /* Process locked */
  336. __HAL_LOCK(hdma);
  337. /* Change DMA peripheral state */
  338. hdma->State = HAL_DMA_STATE_BUSY;
  339. /* Check the parameters */
  340. assert_param(IS_DMA_BUFFER_SIZE(DataLength));
  341. /* Disable the peripheral */
  342. __HAL_DMA_DISABLE(hdma);
  343. /* Configure the source, destination address and the data length */
  344. DMA_SetConfig(hdma, SrcAddress, DstAddress, DataLength);
  345. /* Enable all interrupts */
  346. hdma->Instance->CR |= DMA_IT_TC | DMA_IT_HT | DMA_IT_TE | DMA_IT_DME;
  347. hdma->Instance->FCR |= DMA_IT_FE;
  348. /* Enable the Peripheral */
  349. __HAL_DMA_ENABLE(hdma);
  350. return HAL_OK;
  351. }
  352. /**
  353. * @brief Aborts the DMA Transfer.
  354. * @param hdma : pointer to a DMA_HandleTypeDef structure that contains
  355. * the configuration information for the specified DMA Stream.
  356. *
  357. * @note After disabling a DMA Stream, a check for wait until the DMA Stream is
  358. * effectively disabled is added. If a Stream is disabled
  359. * while a data transfer is ongoing, the current data will be transferred
  360. * and the Stream will be effectively disabled only after the transfer of
  361. * this single data is finished.
  362. * @retval HAL status
  363. */
  364. HAL_StatusTypeDef HAL_DMA_Abort(DMA_HandleTypeDef *hdma)
  365. {
  366. uint32_t tickstart = 0;
  367. /* Disable the stream */
  368. __HAL_DMA_DISABLE(hdma);
  369. /* Get tick */
  370. tickstart = HAL_GetTick();
  371. /* Check if the DMA Stream is effectively disabled */
  372. while((hdma->Instance->CR & DMA_SxCR_EN) != 0)
  373. {
  374. /* Check for the Timeout */
  375. if((HAL_GetTick() - tickstart ) > HAL_TIMEOUT_DMA_ABORT)
  376. {
  377. /* Update error code */
  378. hdma->ErrorCode |= HAL_DMA_ERROR_TIMEOUT;
  379. /* Process Unlocked */
  380. __HAL_UNLOCK(hdma);
  381. /* Change the DMA state */
  382. hdma->State = HAL_DMA_STATE_TIMEOUT;
  383. return HAL_TIMEOUT;
  384. }
  385. }
  386. /* Process Unlocked */
  387. __HAL_UNLOCK(hdma);
  388. /* Change the DMA state*/
  389. hdma->State = HAL_DMA_STATE_READY;
  390. return HAL_OK;
  391. }
  392. /**
  393. * @brief Polling for transfer complete.
  394. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  395. * the configuration information for the specified DMA Stream.
  396. * @param CompleteLevel: Specifies the DMA level complete.
  397. * @param Timeout: Timeout duration.
  398. * @retval HAL status
  399. */
  400. HAL_StatusTypeDef HAL_DMA_PollForTransfer(DMA_HandleTypeDef *hdma, uint32_t CompleteLevel, uint32_t Timeout)
  401. {
  402. uint32_t temp, tmp, tmp1, tmp2;
  403. uint32_t tickstart = 0;
  404. /* calculate DMA base and stream number */
  405. DMA_Base_Registers *regs;
  406. regs = (DMA_Base_Registers *)hdma->StreamBaseAddress;
  407. /* Get the level transfer complete flag */
  408. if(CompleteLevel == HAL_DMA_FULL_TRANSFER)
  409. {
  410. /* Transfer Complete flag */
  411. temp = DMA_FLAG_TCIF0_4 << hdma->StreamIndex;
  412. }
  413. else
  414. {
  415. /* Half Transfer Complete flag */
  416. temp = DMA_FLAG_HTIF0_4 << hdma->StreamIndex;
  417. }
  418. /* Get tick */
  419. tickstart = HAL_GetTick();
  420. while((regs->ISR & temp) == RESET)
  421. {
  422. tmp = regs->ISR & (DMA_FLAG_TEIF0_4 << hdma->StreamIndex);
  423. tmp1 = regs->ISR & (DMA_FLAG_FEIF0_4 << hdma->StreamIndex);
  424. tmp2 = regs->ISR & (DMA_FLAG_DMEIF0_4 << hdma->StreamIndex);
  425. if((tmp != RESET) || (tmp1 != RESET) || (tmp2 != RESET))
  426. {
  427. if(tmp != RESET)
  428. {
  429. /* Update error code */
  430. hdma->ErrorCode |= HAL_DMA_ERROR_TE;
  431. /* Clear the transfer error flag */
  432. regs->IFCR = DMA_FLAG_TEIF0_4 << hdma->StreamIndex;
  433. }
  434. if(tmp1 != RESET)
  435. {
  436. /* Update error code */
  437. hdma->ErrorCode |= HAL_DMA_ERROR_FE;
  438. /* Clear the FIFO error flag */
  439. regs->IFCR = DMA_FLAG_FEIF0_4 << hdma->StreamIndex;
  440. }
  441. if(tmp2 != RESET)
  442. {
  443. /* Update error code */
  444. hdma->ErrorCode |= HAL_DMA_ERROR_DME;
  445. /* Clear the Direct Mode error flag */
  446. regs->IFCR = DMA_FLAG_DMEIF0_4 << hdma->StreamIndex;
  447. }
  448. /* Change the DMA state */
  449. hdma->State= HAL_DMA_STATE_ERROR;
  450. /* Process Unlocked */
  451. __HAL_UNLOCK(hdma);
  452. return HAL_ERROR;
  453. }
  454. /* Check for the Timeout */
  455. if(Timeout != HAL_MAX_DELAY)
  456. {
  457. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  458. {
  459. /* Update error code */
  460. hdma->ErrorCode |= HAL_DMA_ERROR_TIMEOUT;
  461. /* Change the DMA state */
  462. hdma->State = HAL_DMA_STATE_TIMEOUT;
  463. /* Process Unlocked */
  464. __HAL_UNLOCK(hdma);
  465. return HAL_TIMEOUT;
  466. }
  467. }
  468. }
  469. if(CompleteLevel == HAL_DMA_FULL_TRANSFER)
  470. {
  471. /* Clear the half transfer and transfer complete flags */
  472. regs->IFCR = (DMA_FLAG_HTIF0_4 | DMA_FLAG_TCIF0_4) << hdma->StreamIndex;
  473. /* Multi_Buffering mode enabled */
  474. if(((hdma->Instance->CR) & (uint32_t)(DMA_SxCR_DBM)) != 0)
  475. {
  476. /* Current memory buffer used is Memory 0 */
  477. if((hdma->Instance->CR & DMA_SxCR_CT) == 0)
  478. {
  479. /* Change DMA peripheral state */
  480. hdma->State = HAL_DMA_STATE_READY_MEM0;
  481. }
  482. /* Current memory buffer used is Memory 1 */
  483. else if((hdma->Instance->CR & DMA_SxCR_CT) != 0)
  484. {
  485. /* Change DMA peripheral state */
  486. hdma->State = HAL_DMA_STATE_READY_MEM1;
  487. }
  488. }
  489. else
  490. {
  491. /* The selected Streamx EN bit is cleared (DMA is disabled and all transfers
  492. are complete) */
  493. hdma->State = HAL_DMA_STATE_READY_MEM0;
  494. }
  495. /* Process Unlocked */
  496. __HAL_UNLOCK(hdma);
  497. }
  498. else
  499. {
  500. /* Clear the half transfer complete flag */
  501. regs->IFCR = DMA_FLAG_HTIF0_4 << hdma->StreamIndex;
  502. /* Multi_Buffering mode enabled */
  503. if(((hdma->Instance->CR) & (uint32_t)(DMA_SxCR_DBM)) != 0)
  504. {
  505. /* Current memory buffer used is Memory 0 */
  506. if((hdma->Instance->CR & DMA_SxCR_CT) == 0)
  507. {
  508. /* Change DMA peripheral state */
  509. hdma->State = HAL_DMA_STATE_READY_HALF_MEM0;
  510. }
  511. /* Current memory buffer used is Memory 1 */
  512. else if((hdma->Instance->CR & DMA_SxCR_CT) != 0)
  513. {
  514. /* Change DMA peripheral state */
  515. hdma->State = HAL_DMA_STATE_READY_HALF_MEM1;
  516. }
  517. }
  518. else
  519. {
  520. /* Change DMA peripheral state */
  521. hdma->State = HAL_DMA_STATE_READY_HALF_MEM0;
  522. }
  523. }
  524. return HAL_OK;
  525. }
  526. /**
  527. * @brief Handles DMA interrupt request.
  528. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  529. * the configuration information for the specified DMA Stream.
  530. * @retval None
  531. */
  532. void HAL_DMA_IRQHandler(DMA_HandleTypeDef *hdma)
  533. {
  534. /* calculate DMA base and stream number */
  535. DMA_Base_Registers *regs;
  536. regs = (DMA_Base_Registers *)hdma->StreamBaseAddress;
  537. /* Transfer Error Interrupt management ***************************************/
  538. if ((regs->ISR & (DMA_FLAG_TEIF0_4 << hdma->StreamIndex)) != RESET)
  539. {
  540. if(__HAL_DMA_GET_IT_SOURCE(hdma, DMA_IT_TE) != RESET)
  541. {
  542. /* Disable the transfer error interrupt */
  543. __HAL_DMA_DISABLE_IT(hdma, DMA_IT_TE);
  544. /* Clear the transfer error flag */
  545. regs->IFCR = DMA_FLAG_TEIF0_4 << hdma->StreamIndex;
  546. /* Update error code */
  547. hdma->ErrorCode |= HAL_DMA_ERROR_TE;
  548. /* Change the DMA state */
  549. hdma->State = HAL_DMA_STATE_ERROR;
  550. /* Process Unlocked */
  551. __HAL_UNLOCK(hdma);
  552. if(hdma->XferErrorCallback != NULL)
  553. {
  554. /* Transfer error callback */
  555. hdma->XferErrorCallback(hdma);
  556. }
  557. }
  558. }
  559. /* FIFO Error Interrupt management ******************************************/
  560. if ((regs->ISR & (DMA_FLAG_FEIF0_4 << hdma->StreamIndex)) != RESET)
  561. {
  562. if(__HAL_DMA_GET_IT_SOURCE(hdma, DMA_IT_FE) != RESET)
  563. {
  564. /* Disable the FIFO Error interrupt */
  565. __HAL_DMA_DISABLE_IT(hdma, DMA_IT_FE);
  566. /* Clear the FIFO error flag */
  567. regs->IFCR = DMA_FLAG_FEIF0_4 << hdma->StreamIndex;
  568. /* Update error code */
  569. hdma->ErrorCode |= HAL_DMA_ERROR_FE;
  570. /* Change the DMA state */
  571. hdma->State = HAL_DMA_STATE_ERROR;
  572. /* Process Unlocked */
  573. __HAL_UNLOCK(hdma);
  574. if(hdma->XferErrorCallback != NULL)
  575. {
  576. /* Transfer error callback */
  577. hdma->XferErrorCallback(hdma);
  578. }
  579. }
  580. }
  581. /* Direct Mode Error Interrupt management ***********************************/
  582. if ((regs->ISR & (DMA_FLAG_DMEIF0_4 << hdma->StreamIndex)) != RESET)
  583. {
  584. if(__HAL_DMA_GET_IT_SOURCE(hdma, DMA_IT_DME) != RESET)
  585. {
  586. /* Disable the direct mode Error interrupt */
  587. __HAL_DMA_DISABLE_IT(hdma, DMA_IT_DME);
  588. /* Clear the direct mode error flag */
  589. regs->IFCR = DMA_FLAG_DMEIF0_4 << hdma->StreamIndex;
  590. /* Update error code */
  591. hdma->ErrorCode |= HAL_DMA_ERROR_DME;
  592. /* Change the DMA state */
  593. hdma->State = HAL_DMA_STATE_ERROR;
  594. /* Process Unlocked */
  595. __HAL_UNLOCK(hdma);
  596. if(hdma->XferErrorCallback != NULL)
  597. {
  598. /* Transfer error callback */
  599. hdma->XferErrorCallback(hdma);
  600. }
  601. }
  602. }
  603. /* Half Transfer Complete Interrupt management ******************************/
  604. if ((regs->ISR & (DMA_FLAG_HTIF0_4 << hdma->StreamIndex)) != RESET)
  605. {
  606. if(__HAL_DMA_GET_IT_SOURCE(hdma, DMA_IT_HT) != RESET)
  607. {
  608. /* Multi_Buffering mode enabled */
  609. if(((hdma->Instance->CR) & (uint32_t)(DMA_SxCR_DBM)) != 0)
  610. {
  611. /* Clear the half transfer complete flag */
  612. regs->IFCR = DMA_FLAG_HTIF0_4 << hdma->StreamIndex;
  613. /* Current memory buffer used is Memory 0 */
  614. if((hdma->Instance->CR & DMA_SxCR_CT) == 0)
  615. {
  616. /* Change DMA peripheral state */
  617. hdma->State = HAL_DMA_STATE_READY_HALF_MEM0;
  618. }
  619. /* Current memory buffer used is Memory 1 */
  620. else if((hdma->Instance->CR & DMA_SxCR_CT) != 0)
  621. {
  622. /* Change DMA peripheral state */
  623. hdma->State = HAL_DMA_STATE_READY_HALF_MEM1;
  624. }
  625. }
  626. else
  627. {
  628. /* Disable the half transfer interrupt if the DMA mode is not CIRCULAR */
  629. if((hdma->Instance->CR & DMA_SxCR_CIRC) == 0)
  630. {
  631. /* Disable the half transfer interrupt */
  632. __HAL_DMA_DISABLE_IT(hdma, DMA_IT_HT);
  633. }
  634. /* Clear the half transfer complete flag */
  635. regs->IFCR = DMA_FLAG_HTIF0_4 << hdma->StreamIndex;
  636. /* Change DMA peripheral state */
  637. hdma->State = HAL_DMA_STATE_READY_HALF_MEM0;
  638. }
  639. if(hdma->XferHalfCpltCallback != NULL)
  640. {
  641. /* Half transfer callback */
  642. hdma->XferHalfCpltCallback(hdma);
  643. }
  644. }
  645. }
  646. /* Transfer Complete Interrupt management ***********************************/
  647. if ((regs->ISR & (DMA_FLAG_TCIF0_4 << hdma->StreamIndex)) != RESET)
  648. {
  649. if(__HAL_DMA_GET_IT_SOURCE(hdma, DMA_IT_TC) != RESET)
  650. {
  651. if(((hdma->Instance->CR) & (uint32_t)(DMA_SxCR_DBM)) != 0)
  652. {
  653. /* Clear the transfer complete flag */
  654. regs->IFCR = DMA_FLAG_TCIF0_4 << hdma->StreamIndex;
  655. /* Current memory buffer used is Memory 1 */
  656. if((hdma->Instance->CR & DMA_SxCR_CT) == 0)
  657. {
  658. if(hdma->XferM1CpltCallback != NULL)
  659. {
  660. /* Transfer complete Callback for memory1 */
  661. hdma->XferM1CpltCallback(hdma);
  662. }
  663. }
  664. /* Current memory buffer used is Memory 0 */
  665. else if((hdma->Instance->CR & DMA_SxCR_CT) != 0)
  666. {
  667. if(hdma->XferCpltCallback != NULL)
  668. {
  669. /* Transfer complete Callback for memory0 */
  670. hdma->XferCpltCallback(hdma);
  671. }
  672. }
  673. }
  674. /* Disable the transfer complete interrupt if the DMA mode is not CIRCULAR */
  675. else
  676. {
  677. if((hdma->Instance->CR & DMA_SxCR_CIRC) == 0)
  678. {
  679. /* Disable the transfer complete interrupt */
  680. __HAL_DMA_DISABLE_IT(hdma, DMA_IT_TC);
  681. }
  682. /* Clear the transfer complete flag */
  683. regs->IFCR = DMA_FLAG_TCIF0_4 << hdma->StreamIndex;
  684. /* Update error code */
  685. hdma->ErrorCode |= HAL_DMA_ERROR_NONE;
  686. /* Change the DMA state */
  687. hdma->State = HAL_DMA_STATE_READY_MEM0;
  688. /* Process Unlocked */
  689. __HAL_UNLOCK(hdma);
  690. if(hdma->XferCpltCallback != NULL)
  691. {
  692. /* Transfer complete callback */
  693. hdma->XferCpltCallback(hdma);
  694. }
  695. }
  696. }
  697. }
  698. }
  699. /**
  700. * @}
  701. */
  702. /** @addtogroup DMA_Exported_Functions_Group3
  703. *
  704. @verbatim
  705. ===============================================================================
  706. ##### State and Errors functions #####
  707. ===============================================================================
  708. [..]
  709. This subsection provides functions allowing to
  710. (+) Check the DMA state
  711. (+) Get error code
  712. @endverbatim
  713. * @{
  714. */
  715. /**
  716. * @brief Returns the DMA state.
  717. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  718. * the configuration information for the specified DMA Stream.
  719. * @retval HAL state
  720. */
  721. HAL_DMA_StateTypeDef HAL_DMA_GetState(DMA_HandleTypeDef *hdma)
  722. {
  723. return hdma->State;
  724. }
  725. /**
  726. * @brief Return the DMA error code
  727. * @param hdma : pointer to a DMA_HandleTypeDef structure that contains
  728. * the configuration information for the specified DMA Stream.
  729. * @retval DMA Error Code
  730. */
  731. uint32_t HAL_DMA_GetError(DMA_HandleTypeDef *hdma)
  732. {
  733. return hdma->ErrorCode;
  734. }
  735. /**
  736. * @}
  737. */
  738. /**
  739. * @}
  740. */
  741. /** @addtogroup DMA_Private_Functions
  742. * @{
  743. */
  744. /**
  745. * @brief Sets the DMA Transfer parameter.
  746. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  747. * the configuration information for the specified DMA Stream.
  748. * @param SrcAddress: The source memory Buffer address
  749. * @param DstAddress: The destination memory Buffer address
  750. * @param DataLength: The length of data to be transferred from source to destination
  751. * @retval HAL status
  752. */
  753. static void DMA_SetConfig(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength)
  754. {
  755. /* Clear DBM bit */
  756. hdma->Instance->CR &= (uint32_t)(~DMA_SxCR_DBM);
  757. /* Configure DMA Stream data length */
  758. hdma->Instance->NDTR = DataLength;
  759. /* Peripheral to Memory */
  760. if((hdma->Init.Direction) == DMA_MEMORY_TO_PERIPH)
  761. {
  762. /* Configure DMA Stream destination address */
  763. hdma->Instance->PAR = DstAddress;
  764. /* Configure DMA Stream source address */
  765. hdma->Instance->M0AR = SrcAddress;
  766. }
  767. /* Memory to Peripheral */
  768. else
  769. {
  770. /* Configure DMA Stream source address */
  771. hdma->Instance->PAR = SrcAddress;
  772. /* Configure DMA Stream destination address */
  773. hdma->Instance->M0AR = DstAddress;
  774. }
  775. }
  776. /**
  777. * @brief Returns the DMA Stream base address depending on stream number
  778. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  779. * the configuration information for the specified DMA Stream.
  780. * @retval Stream base address
  781. */
  782. static uint32_t DMA_CalcBaseAndBitshift(DMA_HandleTypeDef *hdma)
  783. {
  784. uint32_t stream_number = (((uint32_t)hdma->Instance & 0xFF) - 16) / 24;
  785. /* lookup table for necessary bitshift of flags within status registers */
  786. static const uint8_t flagBitshiftOffset[8] = {0, 6, 16, 22, 0, 6, 16, 22};
  787. hdma->StreamIndex = flagBitshiftOffset[stream_number];
  788. if (stream_number > 3)
  789. {
  790. /* return pointer to HISR and HIFCR */
  791. hdma->StreamBaseAddress = (((uint32_t)hdma->Instance & (uint32_t)(~0x3FF)) + 4);
  792. }
  793. else
  794. {
  795. /* return pointer to LISR and LIFCR */
  796. hdma->StreamBaseAddress = ((uint32_t)hdma->Instance & (uint32_t)(~0x3FF));
  797. }
  798. return hdma->StreamBaseAddress;
  799. }
  800. /**
  801. * @}
  802. */
  803. #endif /* HAL_DMA_MODULE_ENABLED */
  804. /**
  805. * @}
  806. */
  807. /**
  808. * @}
  809. */
  810. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/