stm32h7xx_hal_dma.c 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056
  1. /**
  2. ******************************************************************************
  3. * @file stm32h7xx_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. ******************************************************************************
  12. * @attention
  13. *
  14. * Copyright (c) 2017 STMicroelectronics.
  15. * All rights reserved.
  16. *
  17. * This software is licensed under terms that can be found in the LICENSE file
  18. * in the root directory of this software component.
  19. * If no LICENSE file comes with this software, it is provided AS-IS.
  20. *
  21. ******************************************************************************
  22. @verbatim
  23. ==============================================================================
  24. ##### How to use this driver #####
  25. ==============================================================================
  26. [..]
  27. (#) Enable and configure the peripheral to be connected to the DMA Stream
  28. (except for internal SRAM/FLASH memories: no initialization is
  29. necessary) please refer to Reference manual for connection between peripherals
  30. and DMA requests .
  31. (#) For a given Stream, program the required configuration through the following parameters:
  32. Transfer Direction, Source and Destination data formats,
  33. Circular, Normal or peripheral flow control mode, Stream Priority level,
  34. Source and Destination Increment mode, FIFO mode and its Threshold (if needed),
  35. Burst mode for Source and/or Destination (if needed) using HAL_DMA_Init() function.
  36. *** Polling mode IO operation ***
  37. =================================
  38. [..]
  39. (+) Use HAL_DMA_Start() to start DMA transfer after the configuration of Source
  40. address and destination address and the Length of data to be transferred
  41. (+) Use HAL_DMA_PollForTransfer() to poll for the end of current transfer, in this
  42. case a fixed Timeout can be configured by User depending from his application.
  43. *** Interrupt mode IO operation ***
  44. ===================================
  45. [..]
  46. (+) Configure the DMA interrupt priority using HAL_NVIC_SetPriority()
  47. (+) Enable the DMA IRQ handler using HAL_NVIC_EnableIRQ()
  48. (+) Use HAL_DMA_Start_IT() to start DMA transfer after the configuration of
  49. Source address and destination address and the Length of data to be transferred. In this
  50. case the DMA interrupt is configured
  51. (+) Use HAL_DMA_IRQHandler() called under DMA_IRQHandler() Interrupt subroutine
  52. (+) At the end of data transfer HAL_DMA_IRQHandler() function is executed and user can
  53. add his own function by customization of function pointer XferCpltCallback and
  54. XferErrorCallback (i.e a member of DMA handle structure).
  55. [..]
  56. (#) Use HAL_DMA_GetState() function to return the DMA state and HAL_DMA_GetError() in case of error
  57. detection.
  58. (#) Use HAL_DMA_Abort() function to abort the current transfer
  59. -@- In Memory-to-Memory transfer mode, Circular mode is not allowed.
  60. -@- The FIFO is used mainly to reduce bus usage and to allow data packing/unpacking: it is
  61. possible to set different Data Sizes for the Peripheral and the Memory (ie. you can set
  62. Half-Word data size for the peripheral to access its data register and set Word data size
  63. for the Memory to gain in access time. Each two half words will be packed and written in
  64. a single access to a Word in the Memory).
  65. -@- When FIFO is disabled, it is not allowed to configure different Data Sizes for Source
  66. and Destination. In this case the Peripheral Data Size will be applied to both Source
  67. and Destination.
  68. *** DMA HAL driver macros list ***
  69. =============================================
  70. [..]
  71. Below the list of most used macros in DMA HAL driver.
  72. (+) __HAL_DMA_ENABLE: Enable the specified DMA Stream.
  73. (+) __HAL_DMA_DISABLE: Disable the specified DMA Stream.
  74. (+) __HAL_DMA_GET_FS: Return the current DMA Stream FIFO filled level.
  75. (+) __HAL_DMA_ENABLE_IT: Enable the specified DMA Stream interrupts.
  76. (+) __HAL_DMA_DISABLE_IT: Disable the specified DMA Stream interrupts.
  77. (+) __HAL_DMA_GET_IT_SOURCE: Check whether the specified DMA Stream interrupt has occurred or not.
  78. [..]
  79. (@) You can refer to the DMA HAL driver header file for more useful macros.
  80. @endverbatim
  81. */
  82. /* Includes ------------------------------------------------------------------*/
  83. #include "stm32h7xx_hal.h"
  84. /** @addtogroup STM32H7xx_HAL_Driver
  85. * @{
  86. */
  87. /** @defgroup DMA DMA
  88. * @brief DMA HAL module driver
  89. * @{
  90. */
  91. #ifdef HAL_DMA_MODULE_ENABLED
  92. /* Private types -------------------------------------------------------------*/
  93. typedef struct
  94. {
  95. __IO uint32_t ISR; /*!< DMA interrupt status register */
  96. __IO uint32_t Reserved0;
  97. __IO uint32_t IFCR; /*!< DMA interrupt flag clear register */
  98. } DMA_Base_Registers;
  99. typedef struct
  100. {
  101. __IO uint32_t ISR; /*!< BDMA interrupt status register */
  102. __IO uint32_t IFCR; /*!< BDMA interrupt flag clear register */
  103. } BDMA_Base_Registers;
  104. /* Private variables ---------------------------------------------------------*/
  105. /* Private constants ---------------------------------------------------------*/
  106. /** @addtogroup DMA_Private_Constants
  107. * @{
  108. */
  109. #define HAL_TIMEOUT_DMA_ABORT (5U) /* 5 ms */
  110. #define BDMA_PERIPH_TO_MEMORY (0x00000000U) /*!< Peripheral to memory direction */
  111. #define BDMA_MEMORY_TO_PERIPH ((uint32_t)BDMA_CCR_DIR) /*!< Memory to peripheral direction */
  112. #define BDMA_MEMORY_TO_MEMORY ((uint32_t)BDMA_CCR_MEM2MEM) /*!< Memory to memory direction */
  113. /* DMA to BDMA conversion */
  114. #define DMA_TO_BDMA_DIRECTION(__DMA_DIRECTION__) (((__DMA_DIRECTION__) == DMA_MEMORY_TO_PERIPH)? BDMA_MEMORY_TO_PERIPH: \
  115. ((__DMA_DIRECTION__) == DMA_MEMORY_TO_MEMORY)? BDMA_MEMORY_TO_MEMORY: \
  116. BDMA_PERIPH_TO_MEMORY)
  117. #define DMA_TO_BDMA_PERIPHERAL_INC(__DMA_PERIPHERAL_INC__) ((__DMA_PERIPHERAL_INC__) >> 3U)
  118. #define DMA_TO_BDMA_MEMORY_INC(__DMA_MEMORY_INC__) ((__DMA_MEMORY_INC__) >> 3U)
  119. #define DMA_TO_BDMA_PDATA_SIZE(__DMA_PDATA_SIZE__) ((__DMA_PDATA_SIZE__) >> 3U)
  120. #define DMA_TO_BDMA_MDATA_SIZE(__DMA_MDATA_SIZE__) ((__DMA_MDATA_SIZE__) >> 3U)
  121. #define DMA_TO_BDMA_MODE(__DMA_MODE__) ((__DMA_MODE__) >> 3U)
  122. #define DMA_TO_BDMA_PRIORITY(__DMA_PRIORITY__) ((__DMA_PRIORITY__) >> 4U)
  123. #if defined(UART9)
  124. #define IS_DMA_UART_USART_REQUEST(__REQUEST__) ((((__REQUEST__) >= DMA_REQUEST_USART1_RX) && ((__REQUEST__) <= DMA_REQUEST_USART3_TX)) || \
  125. (((__REQUEST__) >= DMA_REQUEST_UART4_RX) && ((__REQUEST__) <= DMA_REQUEST_UART5_TX )) || \
  126. (((__REQUEST__) >= DMA_REQUEST_USART6_RX) && ((__REQUEST__) <= DMA_REQUEST_USART6_TX)) || \
  127. (((__REQUEST__) >= DMA_REQUEST_UART7_RX) && ((__REQUEST__) <= DMA_REQUEST_UART8_TX )) || \
  128. (((__REQUEST__) >= DMA_REQUEST_UART9_RX) && ((__REQUEST__) <= DMA_REQUEST_USART10_TX )))
  129. #else
  130. #define IS_DMA_UART_USART_REQUEST(__REQUEST__) ((((__REQUEST__) >= DMA_REQUEST_USART1_RX) && ((__REQUEST__) <= DMA_REQUEST_USART3_TX)) || \
  131. (((__REQUEST__) >= DMA_REQUEST_UART4_RX) && ((__REQUEST__) <= DMA_REQUEST_UART5_TX )) || \
  132. (((__REQUEST__) >= DMA_REQUEST_USART6_RX) && ((__REQUEST__) <= DMA_REQUEST_USART6_TX)) || \
  133. (((__REQUEST__) >= DMA_REQUEST_UART7_RX) && ((__REQUEST__) <= DMA_REQUEST_UART8_TX )))
  134. #endif
  135. /**
  136. * @}
  137. */
  138. /* Private macros ------------------------------------------------------------*/
  139. /* Private functions ---------------------------------------------------------*/
  140. /** @addtogroup DMA_Private_Functions
  141. * @{
  142. */
  143. static void DMA_SetConfig(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength);
  144. static uint32_t DMA_CalcBaseAndBitshift(DMA_HandleTypeDef *hdma);
  145. static HAL_StatusTypeDef DMA_CheckFifoParam(DMA_HandleTypeDef *hdma);
  146. static void DMA_CalcDMAMUXChannelBaseAndMask(DMA_HandleTypeDef *hdma);
  147. static void DMA_CalcDMAMUXRequestGenBaseAndMask(DMA_HandleTypeDef *hdma);
  148. /**
  149. * @}
  150. */
  151. /* Exported functions ---------------------------------------------------------*/
  152. /** @addtogroup DMA_Exported_Functions
  153. * @{
  154. */
  155. /** @addtogroup DMA_Exported_Functions_Group1
  156. *
  157. @verbatim
  158. ===============================================================================
  159. ##### Initialization and de-initialization functions #####
  160. ===============================================================================
  161. [..]
  162. This section provides functions allowing to initialize the DMA Stream source
  163. and destination incrementation and data sizes, transfer direction,
  164. circular/normal mode selection, memory-to-memory mode selection and Stream priority value.
  165. [..]
  166. The HAL_DMA_Init() function follows the DMA configuration procedures as described in
  167. reference manual.
  168. The HAL_DMA_DeInit function allows to deinitialize the DMA stream.
  169. @endverbatim
  170. * @{
  171. */
  172. /**
  173. * @brief Initialize the DMA according to the specified
  174. * parameters in the DMA_InitTypeDef and create the associated handle.
  175. * @param hdma: Pointer to a DMA_HandleTypeDef structure that contains
  176. * the configuration information for the specified DMA Stream.
  177. * @retval HAL status
  178. */
  179. HAL_StatusTypeDef HAL_DMA_Init(DMA_HandleTypeDef *hdma)
  180. {
  181. uint32_t registerValue;
  182. uint32_t tickstart = HAL_GetTick();
  183. DMA_Base_Registers *regs_dma;
  184. BDMA_Base_Registers *regs_bdma;
  185. /* Check the DMA peripheral handle */
  186. if(hdma == NULL)
  187. {
  188. return HAL_ERROR;
  189. }
  190. /* Check the parameters */
  191. assert_param(IS_DMA_ALL_INSTANCE(hdma->Instance));
  192. assert_param(IS_DMA_DIRECTION(hdma->Init.Direction));
  193. assert_param(IS_DMA_PERIPHERAL_INC_STATE(hdma->Init.PeriphInc));
  194. assert_param(IS_DMA_MEMORY_INC_STATE(hdma->Init.MemInc));
  195. assert_param(IS_DMA_PERIPHERAL_DATA_SIZE(hdma->Init.PeriphDataAlignment));
  196. assert_param(IS_DMA_MEMORY_DATA_SIZE(hdma->Init.MemDataAlignment));
  197. assert_param(IS_DMA_MODE(hdma->Init.Mode));
  198. assert_param(IS_DMA_PRIORITY(hdma->Init.Priority));
  199. if(IS_DMA_STREAM_INSTANCE(hdma->Instance) != 0U) /* DMA1 or DMA2 instance */
  200. {
  201. assert_param(IS_DMA_REQUEST(hdma->Init.Request));
  202. assert_param(IS_DMA_FIFO_MODE_STATE(hdma->Init.FIFOMode));
  203. /* Check the memory burst, peripheral burst and FIFO threshold parameters only
  204. when FIFO mode is enabled */
  205. if(hdma->Init.FIFOMode != DMA_FIFOMODE_DISABLE)
  206. {
  207. assert_param(IS_DMA_FIFO_THRESHOLD(hdma->Init.FIFOThreshold));
  208. assert_param(IS_DMA_MEMORY_BURST(hdma->Init.MemBurst));
  209. assert_param(IS_DMA_PERIPHERAL_BURST(hdma->Init.PeriphBurst));
  210. }
  211. /* Change DMA peripheral state */
  212. hdma->State = HAL_DMA_STATE_BUSY;
  213. /* Allocate lock resource */
  214. __HAL_UNLOCK(hdma);
  215. /* Disable the peripheral */
  216. __HAL_DMA_DISABLE(hdma);
  217. /* Check if the DMA Stream is effectively disabled */
  218. while((((DMA_Stream_TypeDef *)hdma->Instance)->CR & DMA_SxCR_EN) != 0U)
  219. {
  220. /* Check for the Timeout */
  221. if((HAL_GetTick() - tickstart ) > HAL_TIMEOUT_DMA_ABORT)
  222. {
  223. /* Update error code */
  224. hdma->ErrorCode = HAL_DMA_ERROR_TIMEOUT;
  225. /* Change the DMA state */
  226. hdma->State = HAL_DMA_STATE_ERROR;
  227. return HAL_ERROR;
  228. }
  229. }
  230. /* Get the CR register value */
  231. registerValue = ((DMA_Stream_TypeDef *)hdma->Instance)->CR;
  232. /* Clear CHSEL, MBURST, PBURST, PL, MSIZE, PSIZE, MINC, PINC, CIRC, DIR, CT and DBM bits */
  233. registerValue &= ((uint32_t)~(DMA_SxCR_MBURST | DMA_SxCR_PBURST | \
  234. DMA_SxCR_PL | DMA_SxCR_MSIZE | DMA_SxCR_PSIZE | \
  235. DMA_SxCR_MINC | DMA_SxCR_PINC | DMA_SxCR_CIRC | \
  236. DMA_SxCR_DIR | DMA_SxCR_CT | DMA_SxCR_DBM));
  237. /* Prepare the DMA Stream configuration */
  238. registerValue |= hdma->Init.Direction |
  239. hdma->Init.PeriphInc | hdma->Init.MemInc |
  240. hdma->Init.PeriphDataAlignment | hdma->Init.MemDataAlignment |
  241. hdma->Init.Mode | hdma->Init.Priority;
  242. /* the Memory burst and peripheral burst are not used when the FIFO is disabled */
  243. if(hdma->Init.FIFOMode == DMA_FIFOMODE_ENABLE)
  244. {
  245. /* Get memory burst and peripheral burst */
  246. registerValue |= hdma->Init.MemBurst | hdma->Init.PeriphBurst;
  247. }
  248. /* Work around for Errata 2.22: UART/USART- DMA transfer lock: DMA stream could be
  249. lock when transferring data to/from USART/UART */
  250. #if (STM32H7_DEV_ID == 0x450UL)
  251. if((DBGMCU->IDCODE & 0xFFFF0000U) >= 0x20000000U)
  252. {
  253. #endif /* STM32H7_DEV_ID == 0x450UL */
  254. if(IS_DMA_UART_USART_REQUEST(hdma->Init.Request) != 0U)
  255. {
  256. registerValue |= DMA_SxCR_TRBUFF;
  257. }
  258. #if (STM32H7_DEV_ID == 0x450UL)
  259. }
  260. #endif /* STM32H7_DEV_ID == 0x450UL */
  261. /* Write to DMA Stream CR register */
  262. ((DMA_Stream_TypeDef *)hdma->Instance)->CR = registerValue;
  263. /* Get the FCR register value */
  264. registerValue = ((DMA_Stream_TypeDef *)hdma->Instance)->FCR;
  265. /* Clear Direct mode and FIFO threshold bits */
  266. registerValue &= (uint32_t)~(DMA_SxFCR_DMDIS | DMA_SxFCR_FTH);
  267. /* Prepare the DMA Stream FIFO configuration */
  268. registerValue |= hdma->Init.FIFOMode;
  269. /* the FIFO threshold is not used when the FIFO mode is disabled */
  270. if(hdma->Init.FIFOMode == DMA_FIFOMODE_ENABLE)
  271. {
  272. /* Get the FIFO threshold */
  273. registerValue |= hdma->Init.FIFOThreshold;
  274. /* Check compatibility between FIFO threshold level and size of the memory burst */
  275. /* for INCR4, INCR8, INCR16 */
  276. if(hdma->Init.MemBurst != DMA_MBURST_SINGLE)
  277. {
  278. if (DMA_CheckFifoParam(hdma) != HAL_OK)
  279. {
  280. /* Update error code */
  281. hdma->ErrorCode = HAL_DMA_ERROR_PARAM;
  282. /* Change the DMA state */
  283. hdma->State = HAL_DMA_STATE_READY;
  284. return HAL_ERROR;
  285. }
  286. }
  287. }
  288. /* Write to DMA Stream FCR */
  289. ((DMA_Stream_TypeDef *)hdma->Instance)->FCR = registerValue;
  290. /* Initialize StreamBaseAddress and StreamIndex parameters to be used to calculate
  291. DMA steam Base Address needed by HAL_DMA_IRQHandler() and HAL_DMA_PollForTransfer() */
  292. regs_dma = (DMA_Base_Registers *)DMA_CalcBaseAndBitshift(hdma);
  293. /* Clear all interrupt flags */
  294. regs_dma->IFCR = 0x3FUL << (hdma->StreamIndex & 0x1FU);
  295. }
  296. else if(IS_BDMA_CHANNEL_INSTANCE(hdma->Instance) != 0U) /* BDMA instance(s) */
  297. {
  298. if(IS_BDMA_CHANNEL_DMAMUX_INSTANCE(hdma->Instance) != 0U)
  299. {
  300. /* Check the request parameter */
  301. assert_param(IS_BDMA_REQUEST(hdma->Init.Request));
  302. }
  303. /* Change DMA peripheral state */
  304. hdma->State = HAL_DMA_STATE_BUSY;
  305. /* Allocate lock resource */
  306. __HAL_UNLOCK(hdma);
  307. /* Get the CR register value */
  308. registerValue = ((BDMA_Channel_TypeDef *)hdma->Instance)->CCR;
  309. /* Clear PL, MSIZE, PSIZE, MINC, PINC, CIRC, DIR, MEM2MEM, DBM and CT bits */
  310. registerValue &= ((uint32_t)~(BDMA_CCR_PL | BDMA_CCR_MSIZE | BDMA_CCR_PSIZE | \
  311. BDMA_CCR_MINC | BDMA_CCR_PINC | BDMA_CCR_CIRC | \
  312. BDMA_CCR_DIR | BDMA_CCR_MEM2MEM | BDMA_CCR_DBM | \
  313. BDMA_CCR_CT));
  314. /* Prepare the DMA Channel configuration */
  315. registerValue |= DMA_TO_BDMA_DIRECTION(hdma->Init.Direction) |
  316. DMA_TO_BDMA_PERIPHERAL_INC(hdma->Init.PeriphInc) |
  317. DMA_TO_BDMA_MEMORY_INC(hdma->Init.MemInc) |
  318. DMA_TO_BDMA_PDATA_SIZE(hdma->Init.PeriphDataAlignment) |
  319. DMA_TO_BDMA_MDATA_SIZE(hdma->Init.MemDataAlignment) |
  320. DMA_TO_BDMA_MODE(hdma->Init.Mode) |
  321. DMA_TO_BDMA_PRIORITY(hdma->Init.Priority);
  322. /* Write to DMA Channel CR register */
  323. ((BDMA_Channel_TypeDef *)hdma->Instance)->CCR = registerValue;
  324. /* calculation of the channel index */
  325. hdma->StreamIndex = (((uint32_t)((uint32_t*)hdma->Instance) - (uint32_t)BDMA_Channel0) / ((uint32_t)BDMA_Channel1 - (uint32_t)BDMA_Channel0)) << 2U;
  326. /* Initialize StreamBaseAddress and StreamIndex parameters to be used to calculate
  327. DMA steam Base Address needed by HAL_DMA_IRQHandler() and HAL_DMA_PollForTransfer() */
  328. regs_bdma = (BDMA_Base_Registers *)DMA_CalcBaseAndBitshift(hdma);
  329. /* Clear all interrupt flags */
  330. regs_bdma->IFCR = ((BDMA_IFCR_CGIF0) << (hdma->StreamIndex & 0x1FU));
  331. }
  332. else
  333. {
  334. hdma->ErrorCode = HAL_DMA_ERROR_PARAM;
  335. hdma->State = HAL_DMA_STATE_ERROR;
  336. return HAL_ERROR;
  337. }
  338. if(IS_DMA_DMAMUX_ALL_INSTANCE(hdma->Instance) != 0U) /* No DMAMUX available for BDMA1 */
  339. {
  340. /* Initialize parameters for DMAMUX channel :
  341. DMAmuxChannel, DMAmuxChannelStatus and DMAmuxChannelStatusMask
  342. */
  343. DMA_CalcDMAMUXChannelBaseAndMask(hdma);
  344. if(hdma->Init.Direction == DMA_MEMORY_TO_MEMORY)
  345. {
  346. /* if memory to memory force the request to 0*/
  347. hdma->Init.Request = DMA_REQUEST_MEM2MEM;
  348. }
  349. /* Set peripheral request to DMAMUX channel */
  350. hdma->DMAmuxChannel->CCR = (hdma->Init.Request & DMAMUX_CxCR_DMAREQ_ID);
  351. /* Clear the DMAMUX synchro overrun flag */
  352. hdma->DMAmuxChannelStatus->CFR = hdma->DMAmuxChannelStatusMask;
  353. /* Initialize parameters for DMAMUX request generator :
  354. if the DMA request is DMA_REQUEST_GENERATOR0 to DMA_REQUEST_GENERATOR7
  355. */
  356. if((hdma->Init.Request >= DMA_REQUEST_GENERATOR0) && (hdma->Init.Request <= DMA_REQUEST_GENERATOR7))
  357. {
  358. /* Initialize parameters for DMAMUX request generator :
  359. DMAmuxRequestGen, DMAmuxRequestGenStatus and DMAmuxRequestGenStatusMask */
  360. DMA_CalcDMAMUXRequestGenBaseAndMask(hdma);
  361. /* Reset the DMAMUX request generator register */
  362. hdma->DMAmuxRequestGen->RGCR = 0U;
  363. /* Clear the DMAMUX request generator overrun flag */
  364. hdma->DMAmuxRequestGenStatus->RGCFR = hdma->DMAmuxRequestGenStatusMask;
  365. }
  366. else
  367. {
  368. hdma->DMAmuxRequestGen = 0U;
  369. hdma->DMAmuxRequestGenStatus = 0U;
  370. hdma->DMAmuxRequestGenStatusMask = 0U;
  371. }
  372. }
  373. /* Initialize the error code */
  374. hdma->ErrorCode = HAL_DMA_ERROR_NONE;
  375. /* Initialize the DMA state */
  376. hdma->State = HAL_DMA_STATE_READY;
  377. return HAL_OK;
  378. }
  379. /**
  380. * @brief DeInitializes the DMA peripheral
  381. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  382. * the configuration information for the specified DMA Stream.
  383. * @retval HAL status
  384. */
  385. HAL_StatusTypeDef HAL_DMA_DeInit(DMA_HandleTypeDef *hdma)
  386. {
  387. DMA_Base_Registers *regs_dma;
  388. BDMA_Base_Registers *regs_bdma;
  389. /* Check the DMA peripheral handle */
  390. if(hdma == NULL)
  391. {
  392. return HAL_ERROR;
  393. }
  394. /* Disable the selected DMA Streamx */
  395. __HAL_DMA_DISABLE(hdma);
  396. if(IS_DMA_STREAM_INSTANCE(hdma->Instance) != 0U) /* DMA1 or DMA2 instance */
  397. {
  398. /* Reset DMA Streamx control register */
  399. ((DMA_Stream_TypeDef *)hdma->Instance)->CR = 0U;
  400. /* Reset DMA Streamx number of data to transfer register */
  401. ((DMA_Stream_TypeDef *)hdma->Instance)->NDTR = 0U;
  402. /* Reset DMA Streamx peripheral address register */
  403. ((DMA_Stream_TypeDef *)hdma->Instance)->PAR = 0U;
  404. /* Reset DMA Streamx memory 0 address register */
  405. ((DMA_Stream_TypeDef *)hdma->Instance)->M0AR = 0U;
  406. /* Reset DMA Streamx memory 1 address register */
  407. ((DMA_Stream_TypeDef *)hdma->Instance)->M1AR = 0U;
  408. /* Reset DMA Streamx FIFO control register */
  409. ((DMA_Stream_TypeDef *)hdma->Instance)->FCR = (uint32_t)0x00000021U;
  410. /* Get DMA steam Base Address */
  411. regs_dma = (DMA_Base_Registers *)DMA_CalcBaseAndBitshift(hdma);
  412. /* Clear all interrupt flags at correct offset within the register */
  413. regs_dma->IFCR = 0x3FUL << (hdma->StreamIndex & 0x1FU);
  414. }
  415. else if(IS_BDMA_CHANNEL_INSTANCE(hdma->Instance) != 0U) /* BDMA instance(s) */
  416. {
  417. /* Reset DMA Channel control register */
  418. ((BDMA_Channel_TypeDef *)hdma->Instance)->CCR = 0U;
  419. /* Reset DMA Channel Number of Data to Transfer register */
  420. ((BDMA_Channel_TypeDef *)hdma->Instance)->CNDTR = 0U;
  421. /* Reset DMA Channel peripheral address register */
  422. ((BDMA_Channel_TypeDef *)hdma->Instance)->CPAR = 0U;
  423. /* Reset DMA Channel memory 0 address register */
  424. ((BDMA_Channel_TypeDef *)hdma->Instance)->CM0AR = 0U;
  425. /* Reset DMA Channel memory 1 address register */
  426. ((BDMA_Channel_TypeDef *)hdma->Instance)->CM1AR = 0U;
  427. /* Get DMA steam Base Address */
  428. regs_bdma = (BDMA_Base_Registers *)DMA_CalcBaseAndBitshift(hdma);
  429. /* Clear all interrupt flags at correct offset within the register */
  430. regs_bdma->IFCR = ((BDMA_IFCR_CGIF0) << (hdma->StreamIndex & 0x1FU));
  431. }
  432. else
  433. {
  434. /* Return error status */
  435. return HAL_ERROR;
  436. }
  437. #if defined (BDMA1) /* No DMAMUX available for BDMA1 available on STM32H7Ax/Bx devices only */
  438. if(IS_DMA_DMAMUX_ALL_INSTANCE(hdma->Instance) != 0U) /* No DMAMUX available for BDMA1 */
  439. #endif /* BDMA1 */
  440. {
  441. /* Initialize parameters for DMAMUX channel :
  442. DMAmuxChannel, DMAmuxChannelStatus and DMAmuxChannelStatusMask */
  443. DMA_CalcDMAMUXChannelBaseAndMask(hdma);
  444. if(hdma->DMAmuxChannel != 0U)
  445. {
  446. /* Resett he DMAMUX channel that corresponds to the DMA stream */
  447. hdma->DMAmuxChannel->CCR = 0U;
  448. /* Clear the DMAMUX synchro overrun flag */
  449. hdma->DMAmuxChannelStatus->CFR = hdma->DMAmuxChannelStatusMask;
  450. }
  451. if((hdma->Init.Request >= DMA_REQUEST_GENERATOR0) && (hdma->Init.Request <= DMA_REQUEST_GENERATOR7))
  452. {
  453. /* Initialize parameters for DMAMUX request generator :
  454. DMAmuxRequestGen, DMAmuxRequestGenStatus and DMAmuxRequestGenStatusMask */
  455. DMA_CalcDMAMUXRequestGenBaseAndMask(hdma);
  456. /* Reset the DMAMUX request generator register */
  457. hdma->DMAmuxRequestGen->RGCR = 0U;
  458. /* Clear the DMAMUX request generator overrun flag */
  459. hdma->DMAmuxRequestGenStatus->RGCFR = hdma->DMAmuxRequestGenStatusMask;
  460. }
  461. hdma->DMAmuxRequestGen = 0U;
  462. hdma->DMAmuxRequestGenStatus = 0U;
  463. hdma->DMAmuxRequestGenStatusMask = 0U;
  464. }
  465. /* Clean callbacks */
  466. hdma->XferCpltCallback = NULL;
  467. hdma->XferHalfCpltCallback = NULL;
  468. hdma->XferM1CpltCallback = NULL;
  469. hdma->XferM1HalfCpltCallback = NULL;
  470. hdma->XferErrorCallback = NULL;
  471. hdma->XferAbortCallback = NULL;
  472. /* Initialize the error code */
  473. hdma->ErrorCode = HAL_DMA_ERROR_NONE;
  474. /* Initialize the DMA state */
  475. hdma->State = HAL_DMA_STATE_RESET;
  476. /* Release Lock */
  477. __HAL_UNLOCK(hdma);
  478. return HAL_OK;
  479. }
  480. /**
  481. * @}
  482. */
  483. /** @addtogroup DMA_Exported_Functions_Group2
  484. *
  485. @verbatim
  486. ===============================================================================
  487. ##### IO operation functions #####
  488. ===============================================================================
  489. [..] This section provides functions allowing to:
  490. (+) Configure the source, destination address and data length and Start DMA transfer
  491. (+) Configure the source, destination address and data length and
  492. Start DMA transfer with interrupt
  493. (+) Register and Unregister DMA callbacks
  494. (+) Abort DMA transfer
  495. (+) Poll for transfer complete
  496. (+) Handle DMA interrupt request
  497. @endverbatim
  498. * @{
  499. */
  500. /**
  501. * @brief Starts the DMA Transfer.
  502. * @param hdma : pointer to a DMA_HandleTypeDef structure that contains
  503. * the configuration information for the specified DMA Stream.
  504. * @param SrcAddress: The source memory Buffer address
  505. * @param DstAddress: The destination memory Buffer address
  506. * @param DataLength: The length of data to be transferred from source to destination
  507. * @retval HAL status
  508. */
  509. HAL_StatusTypeDef HAL_DMA_Start(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength)
  510. {
  511. HAL_StatusTypeDef status = HAL_OK;
  512. /* Check the parameters */
  513. assert_param(IS_DMA_BUFFER_SIZE(DataLength));
  514. /* Check the DMA peripheral handle */
  515. if(hdma == NULL)
  516. {
  517. return HAL_ERROR;
  518. }
  519. /* Process locked */
  520. __HAL_LOCK(hdma);
  521. if(HAL_DMA_STATE_READY == hdma->State)
  522. {
  523. /* Change DMA peripheral state */
  524. hdma->State = HAL_DMA_STATE_BUSY;
  525. /* Initialize the error code */
  526. hdma->ErrorCode = HAL_DMA_ERROR_NONE;
  527. /* Disable the peripheral */
  528. __HAL_DMA_DISABLE(hdma);
  529. /* Configure the source, destination address and the data length */
  530. DMA_SetConfig(hdma, SrcAddress, DstAddress, DataLength);
  531. /* Enable the Peripheral */
  532. __HAL_DMA_ENABLE(hdma);
  533. }
  534. else
  535. {
  536. /* Set the error code to busy */
  537. hdma->ErrorCode = HAL_DMA_ERROR_BUSY;
  538. /* Process unlocked */
  539. __HAL_UNLOCK(hdma);
  540. /* Return error status */
  541. status = HAL_ERROR;
  542. }
  543. return status;
  544. }
  545. /**
  546. * @brief Start the DMA Transfer with interrupt enabled.
  547. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  548. * the configuration information for the specified DMA Stream.
  549. * @param SrcAddress: The source memory Buffer address
  550. * @param DstAddress: The destination memory Buffer address
  551. * @param DataLength: The length of data to be transferred from source to destination
  552. * @retval HAL status
  553. */
  554. HAL_StatusTypeDef HAL_DMA_Start_IT(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength)
  555. {
  556. HAL_StatusTypeDef status = HAL_OK;
  557. /* Check the parameters */
  558. assert_param(IS_DMA_BUFFER_SIZE(DataLength));
  559. /* Check the DMA peripheral handle */
  560. if(hdma == NULL)
  561. {
  562. return HAL_ERROR;
  563. }
  564. /* Process locked */
  565. __HAL_LOCK(hdma);
  566. if(HAL_DMA_STATE_READY == hdma->State)
  567. {
  568. /* Change DMA peripheral state */
  569. hdma->State = HAL_DMA_STATE_BUSY;
  570. /* Initialize the error code */
  571. hdma->ErrorCode = HAL_DMA_ERROR_NONE;
  572. /* Disable the peripheral */
  573. __HAL_DMA_DISABLE(hdma);
  574. /* Configure the source, destination address and the data length */
  575. DMA_SetConfig(hdma, SrcAddress, DstAddress, DataLength);
  576. if(IS_DMA_STREAM_INSTANCE(hdma->Instance) != 0U) /* DMA1 or DMA2 instance */
  577. {
  578. /* Enable Common interrupts*/
  579. MODIFY_REG(((DMA_Stream_TypeDef *)hdma->Instance)->CR, (DMA_IT_TC | DMA_IT_TE | DMA_IT_DME | DMA_IT_HT), (DMA_IT_TC | DMA_IT_TE | DMA_IT_DME));
  580. if(hdma->XferHalfCpltCallback != NULL)
  581. {
  582. /* Enable Half Transfer IT if corresponding Callback is set */
  583. ((DMA_Stream_TypeDef *)hdma->Instance)->CR |= DMA_IT_HT;
  584. }
  585. }
  586. else /* BDMA channel */
  587. {
  588. /* Enable Common interrupts */
  589. MODIFY_REG(((BDMA_Channel_TypeDef *)hdma->Instance)->CCR, (BDMA_CCR_TCIE | BDMA_CCR_HTIE | BDMA_CCR_TEIE), (BDMA_CCR_TCIE | BDMA_CCR_TEIE));
  590. if(hdma->XferHalfCpltCallback != NULL)
  591. {
  592. /*Enable Half Transfer IT if corresponding Callback is set */
  593. ((BDMA_Channel_TypeDef *)hdma->Instance)->CCR |= BDMA_CCR_HTIE;
  594. }
  595. }
  596. if(IS_DMA_DMAMUX_ALL_INSTANCE(hdma->Instance) != 0U) /* No DMAMUX available for BDMA1 */
  597. {
  598. /* Check if DMAMUX Synchronization is enabled */
  599. if((hdma->DMAmuxChannel->CCR & DMAMUX_CxCR_SE) != 0U)
  600. {
  601. /* Enable DMAMUX sync overrun IT*/
  602. hdma->DMAmuxChannel->CCR |= DMAMUX_CxCR_SOIE;
  603. }
  604. if(hdma->DMAmuxRequestGen != 0U)
  605. {
  606. /* if using DMAMUX request generator, enable the DMAMUX request generator overrun IT*/
  607. /* enable the request gen overrun IT */
  608. hdma->DMAmuxRequestGen->RGCR |= DMAMUX_RGxCR_OIE;
  609. }
  610. }
  611. /* Enable the Peripheral */
  612. __HAL_DMA_ENABLE(hdma);
  613. }
  614. else
  615. {
  616. /* Set the error code to busy */
  617. hdma->ErrorCode = HAL_DMA_ERROR_BUSY;
  618. /* Process unlocked */
  619. __HAL_UNLOCK(hdma);
  620. /* Return error status */
  621. status = HAL_ERROR;
  622. }
  623. return status;
  624. }
  625. /**
  626. * @brief Aborts the DMA Transfer.
  627. * @param hdma : pointer to a DMA_HandleTypeDef structure that contains
  628. * the configuration information for the specified DMA Stream.
  629. *
  630. * @note After disabling a DMA Stream, a check for wait until the DMA Stream is
  631. * effectively disabled is added. If a Stream is disabled
  632. * while a data transfer is ongoing, the current data will be transferred
  633. * and the Stream will be effectively disabled only after the transfer of
  634. * this single data is finished.
  635. * @retval HAL status
  636. */
  637. HAL_StatusTypeDef HAL_DMA_Abort(DMA_HandleTypeDef *hdma)
  638. {
  639. /* calculate DMA base and stream number */
  640. DMA_Base_Registers *regs_dma;
  641. BDMA_Base_Registers *regs_bdma;
  642. const __IO uint32_t *enableRegister;
  643. uint32_t tickstart = HAL_GetTick();
  644. /* Check the DMA peripheral handle */
  645. if(hdma == NULL)
  646. {
  647. return HAL_ERROR;
  648. }
  649. /* Check the DMA peripheral state */
  650. if(hdma->State != HAL_DMA_STATE_BUSY)
  651. {
  652. hdma->ErrorCode = HAL_DMA_ERROR_NO_XFER;
  653. /* Process Unlocked */
  654. __HAL_UNLOCK(hdma);
  655. return HAL_ERROR;
  656. }
  657. else
  658. {
  659. /* Disable all the transfer interrupts */
  660. if(IS_DMA_STREAM_INSTANCE(hdma->Instance) != 0U) /* DMA1 or DMA2 instance */
  661. {
  662. /* Disable DMA All Interrupts */
  663. ((DMA_Stream_TypeDef *)hdma->Instance)->CR &= ~(DMA_IT_TC | DMA_IT_TE | DMA_IT_DME | DMA_IT_HT);
  664. ((DMA_Stream_TypeDef *)hdma->Instance)->FCR &= ~(DMA_IT_FE);
  665. enableRegister = (__IO uint32_t *)(&(((DMA_Stream_TypeDef *)hdma->Instance)->CR));
  666. }
  667. else /* BDMA channel */
  668. {
  669. /* Disable DMA All Interrupts */
  670. ((BDMA_Channel_TypeDef *)hdma->Instance)->CCR &= ~(BDMA_CCR_TCIE | BDMA_CCR_HTIE | BDMA_CCR_TEIE);
  671. enableRegister = (__IO uint32_t *)(&(((BDMA_Channel_TypeDef *)hdma->Instance)->CCR));
  672. }
  673. if(IS_DMA_DMAMUX_ALL_INSTANCE(hdma->Instance) != 0U) /* No DMAMUX available for BDMA1 */
  674. {
  675. /* disable the DMAMUX sync overrun IT */
  676. hdma->DMAmuxChannel->CCR &= ~DMAMUX_CxCR_SOIE;
  677. }
  678. /* Disable the stream */
  679. __HAL_DMA_DISABLE(hdma);
  680. /* Check if the DMA Stream is effectively disabled */
  681. while(((*enableRegister) & DMA_SxCR_EN) != 0U)
  682. {
  683. /* Check for the Timeout */
  684. if((HAL_GetTick() - tickstart ) > HAL_TIMEOUT_DMA_ABORT)
  685. {
  686. /* Update error code */
  687. hdma->ErrorCode = HAL_DMA_ERROR_TIMEOUT;
  688. /* Change the DMA state */
  689. hdma->State = HAL_DMA_STATE_ERROR;
  690. /* Process Unlocked */
  691. __HAL_UNLOCK(hdma);
  692. return HAL_ERROR;
  693. }
  694. }
  695. /* Clear all interrupt flags at correct offset within the register */
  696. if(IS_DMA_STREAM_INSTANCE(hdma->Instance) != 0U) /* DMA1 or DMA2 instance */
  697. {
  698. regs_dma = (DMA_Base_Registers *)hdma->StreamBaseAddress;
  699. regs_dma->IFCR = 0x3FUL << (hdma->StreamIndex & 0x1FU);
  700. }
  701. else /* BDMA channel */
  702. {
  703. regs_bdma = (BDMA_Base_Registers *)hdma->StreamBaseAddress;
  704. regs_bdma->IFCR = ((BDMA_IFCR_CGIF0) << (hdma->StreamIndex & 0x1FU));
  705. }
  706. if(IS_DMA_DMAMUX_ALL_INSTANCE(hdma->Instance) != 0U) /* No DMAMUX available for BDMA1 */
  707. {
  708. /* Clear the DMAMUX synchro overrun flag */
  709. hdma->DMAmuxChannelStatus->CFR = hdma->DMAmuxChannelStatusMask;
  710. if(hdma->DMAmuxRequestGen != 0U)
  711. {
  712. /* if using DMAMUX request generator, disable the DMAMUX request generator overrun IT */
  713. /* disable the request gen overrun IT */
  714. hdma->DMAmuxRequestGen->RGCR &= ~DMAMUX_RGxCR_OIE;
  715. /* Clear the DMAMUX request generator overrun flag */
  716. hdma->DMAmuxRequestGenStatus->RGCFR = hdma->DMAmuxRequestGenStatusMask;
  717. }
  718. }
  719. /* Change the DMA state */
  720. hdma->State = HAL_DMA_STATE_READY;
  721. /* Process Unlocked */
  722. __HAL_UNLOCK(hdma);
  723. }
  724. return HAL_OK;
  725. }
  726. /**
  727. * @brief Aborts the DMA Transfer in Interrupt mode.
  728. * @param hdma : pointer to a DMA_HandleTypeDef structure that contains
  729. * the configuration information for the specified DMA Stream.
  730. * @retval HAL status
  731. */
  732. HAL_StatusTypeDef HAL_DMA_Abort_IT(DMA_HandleTypeDef *hdma)
  733. {
  734. BDMA_Base_Registers *regs_bdma;
  735. /* Check the DMA peripheral handle */
  736. if(hdma == NULL)
  737. {
  738. return HAL_ERROR;
  739. }
  740. if(hdma->State != HAL_DMA_STATE_BUSY)
  741. {
  742. hdma->ErrorCode = HAL_DMA_ERROR_NO_XFER;
  743. return HAL_ERROR;
  744. }
  745. else
  746. {
  747. if(IS_DMA_STREAM_INSTANCE(hdma->Instance) != 0U) /* DMA1 or DMA2 instance */
  748. {
  749. /* Set Abort State */
  750. hdma->State = HAL_DMA_STATE_ABORT;
  751. /* Disable the stream */
  752. __HAL_DMA_DISABLE(hdma);
  753. }
  754. else /* BDMA channel */
  755. {
  756. /* Disable DMA All Interrupts */
  757. ((BDMA_Channel_TypeDef *)hdma->Instance)->CCR &= ~(BDMA_CCR_TCIE | BDMA_CCR_HTIE | BDMA_CCR_TEIE);
  758. /* Disable the channel */
  759. __HAL_DMA_DISABLE(hdma);
  760. if(IS_DMA_DMAMUX_ALL_INSTANCE(hdma->Instance) != 0U) /* No DMAMUX available for BDMA1 */
  761. {
  762. /* disable the DMAMUX sync overrun IT */
  763. hdma->DMAmuxChannel->CCR &= ~DMAMUX_CxCR_SOIE;
  764. /* Clear all flags */
  765. regs_bdma = (BDMA_Base_Registers *)hdma->StreamBaseAddress;
  766. regs_bdma->IFCR = ((BDMA_IFCR_CGIF0) << (hdma->StreamIndex & 0x1FU));
  767. /* Clear the DMAMUX synchro overrun flag */
  768. hdma->DMAmuxChannelStatus->CFR = hdma->DMAmuxChannelStatusMask;
  769. if(hdma->DMAmuxRequestGen != 0U)
  770. {
  771. /* if using DMAMUX request generator, disable the DMAMUX request generator overrun IT*/
  772. /* disable the request gen overrun IT */
  773. hdma->DMAmuxRequestGen->RGCR &= ~DMAMUX_RGxCR_OIE;
  774. /* Clear the DMAMUX request generator overrun flag */
  775. hdma->DMAmuxRequestGenStatus->RGCFR = hdma->DMAmuxRequestGenStatusMask;
  776. }
  777. }
  778. /* Change the DMA state */
  779. hdma->State = HAL_DMA_STATE_READY;
  780. /* Process Unlocked */
  781. __HAL_UNLOCK(hdma);
  782. /* Call User Abort callback */
  783. if(hdma->XferAbortCallback != NULL)
  784. {
  785. hdma->XferAbortCallback(hdma);
  786. }
  787. }
  788. }
  789. return HAL_OK;
  790. }
  791. /**
  792. * @brief Polling for transfer complete.
  793. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  794. * the configuration information for the specified DMA Stream.
  795. * @param CompleteLevel: Specifies the DMA level complete.
  796. * @note The polling mode is kept in this version for legacy. it is recommended to use the IT model instead.
  797. * This model could be used for debug purpose.
  798. * @note The HAL_DMA_PollForTransfer API cannot be used in circular and double buffering mode (automatic circular mode).
  799. * @param Timeout: Timeout duration.
  800. * @retval HAL status
  801. */
  802. HAL_StatusTypeDef HAL_DMA_PollForTransfer(DMA_HandleTypeDef *hdma, HAL_DMA_LevelCompleteTypeDef CompleteLevel, uint32_t Timeout)
  803. {
  804. HAL_StatusTypeDef status = HAL_OK;
  805. uint32_t cpltlevel_mask;
  806. uint32_t tickstart = HAL_GetTick();
  807. /* IT status register */
  808. __IO uint32_t *isr_reg;
  809. /* IT clear flag register */
  810. __IO uint32_t *ifcr_reg;
  811. /* Check the DMA peripheral handle */
  812. if(hdma == NULL)
  813. {
  814. return HAL_ERROR;
  815. }
  816. if(HAL_DMA_STATE_BUSY != hdma->State)
  817. {
  818. /* No transfer ongoing */
  819. hdma->ErrorCode = HAL_DMA_ERROR_NO_XFER;
  820. __HAL_UNLOCK(hdma);
  821. return HAL_ERROR;
  822. }
  823. if(IS_DMA_STREAM_INSTANCE(hdma->Instance) != 0U) /* DMA1 or DMA2 instance */
  824. {
  825. /* Polling mode not supported in circular mode and double buffering mode */
  826. if ((((DMA_Stream_TypeDef *)hdma->Instance)->CR & DMA_SxCR_CIRC) != 0U)
  827. {
  828. hdma->ErrorCode = HAL_DMA_ERROR_NOT_SUPPORTED;
  829. return HAL_ERROR;
  830. }
  831. /* Get the level transfer complete flag */
  832. if(CompleteLevel == HAL_DMA_FULL_TRANSFER)
  833. {
  834. /* Transfer Complete flag */
  835. cpltlevel_mask = DMA_FLAG_TCIF0_4 << (hdma->StreamIndex & 0x1FU);
  836. }
  837. else
  838. {
  839. /* Half Transfer Complete flag */
  840. cpltlevel_mask = DMA_FLAG_HTIF0_4 << (hdma->StreamIndex & 0x1FU);
  841. }
  842. isr_reg = &(((DMA_Base_Registers *)hdma->StreamBaseAddress)->ISR);
  843. ifcr_reg = &(((DMA_Base_Registers *)hdma->StreamBaseAddress)->IFCR);
  844. }
  845. else /* BDMA channel */
  846. {
  847. /* Polling mode not supported in circular mode */
  848. if ((((BDMA_Channel_TypeDef *)hdma->Instance)->CCR & BDMA_CCR_CIRC) != 0U)
  849. {
  850. hdma->ErrorCode = HAL_DMA_ERROR_NOT_SUPPORTED;
  851. return HAL_ERROR;
  852. }
  853. /* Get the level transfer complete flag */
  854. if(CompleteLevel == HAL_DMA_FULL_TRANSFER)
  855. {
  856. /* Transfer Complete flag */
  857. cpltlevel_mask = BDMA_FLAG_TC0 << (hdma->StreamIndex & 0x1FU);
  858. }
  859. else
  860. {
  861. /* Half Transfer Complete flag */
  862. cpltlevel_mask = BDMA_FLAG_HT0 << (hdma->StreamIndex & 0x1FU);
  863. }
  864. isr_reg = &(((BDMA_Base_Registers *)hdma->StreamBaseAddress)->ISR);
  865. ifcr_reg = &(((BDMA_Base_Registers *)hdma->StreamBaseAddress)->IFCR);
  866. }
  867. while(((*isr_reg) & cpltlevel_mask) == 0U)
  868. {
  869. if(IS_DMA_STREAM_INSTANCE(hdma->Instance) != 0U) /* DMA1 or DMA2 instance */
  870. {
  871. if(((*isr_reg) & (DMA_FLAG_FEIF0_4 << (hdma->StreamIndex & 0x1FU))) != 0U)
  872. {
  873. /* Update error code */
  874. hdma->ErrorCode |= HAL_DMA_ERROR_FE;
  875. /* Clear the FIFO error flag */
  876. (*ifcr_reg) = DMA_FLAG_FEIF0_4 << (hdma->StreamIndex & 0x1FU);
  877. }
  878. if(((*isr_reg) & (DMA_FLAG_DMEIF0_4 << (hdma->StreamIndex & 0x1FU))) != 0U)
  879. {
  880. /* Update error code */
  881. hdma->ErrorCode |= HAL_DMA_ERROR_DME;
  882. /* Clear the Direct Mode error flag */
  883. (*ifcr_reg) = DMA_FLAG_DMEIF0_4 << (hdma->StreamIndex & 0x1FU);
  884. }
  885. if(((*isr_reg) & (DMA_FLAG_TEIF0_4 << (hdma->StreamIndex & 0x1FU))) != 0U)
  886. {
  887. /* Update error code */
  888. hdma->ErrorCode |= HAL_DMA_ERROR_TE;
  889. /* Clear the transfer error flag */
  890. (*ifcr_reg) = DMA_FLAG_TEIF0_4 << (hdma->StreamIndex & 0x1FU);
  891. /* Change the DMA state */
  892. hdma->State = HAL_DMA_STATE_READY;
  893. /* Process Unlocked */
  894. __HAL_UNLOCK(hdma);
  895. return HAL_ERROR;
  896. }
  897. }
  898. else /* BDMA channel */
  899. {
  900. if(((*isr_reg) & (BDMA_FLAG_TE0 << (hdma->StreamIndex & 0x1FU))) != 0U)
  901. {
  902. /* When a DMA transfer error occurs */
  903. /* A hardware clear of its EN bits is performed */
  904. /* Clear all flags */
  905. (*isr_reg) = ((BDMA_ISR_GIF0) << (hdma->StreamIndex & 0x1FU));
  906. /* Update error code */
  907. hdma->ErrorCode = HAL_DMA_ERROR_TE;
  908. /* Change the DMA state */
  909. hdma->State = HAL_DMA_STATE_READY;
  910. /* Process Unlocked */
  911. __HAL_UNLOCK(hdma);
  912. return HAL_ERROR;
  913. }
  914. }
  915. /* Check for the Timeout (Not applicable in circular mode)*/
  916. if(Timeout != HAL_MAX_DELAY)
  917. {
  918. if(((HAL_GetTick() - tickstart ) > Timeout)||(Timeout == 0U))
  919. {
  920. /* Update error code */
  921. hdma->ErrorCode = HAL_DMA_ERROR_TIMEOUT;
  922. /* if timeout then abort the current transfer */
  923. /* No need to check return value: as in this case we will return HAL_ERROR with HAL_DMA_ERROR_TIMEOUT error code */
  924. (void) HAL_DMA_Abort(hdma);
  925. /*
  926. Note that the Abort function will
  927. - Clear the transfer error flags
  928. - Unlock
  929. - Set the State
  930. */
  931. return HAL_ERROR;
  932. }
  933. }
  934. if(IS_DMA_DMAMUX_ALL_INSTANCE(hdma->Instance) != 0U) /* No DMAMUX available for BDMA1 */
  935. {
  936. /* Check for DMAMUX Request generator (if used) overrun status */
  937. if(hdma->DMAmuxRequestGen != 0U)
  938. {
  939. /* if using DMAMUX request generator Check for DMAMUX request generator overrun */
  940. if((hdma->DMAmuxRequestGenStatus->RGSR & hdma->DMAmuxRequestGenStatusMask) != 0U)
  941. {
  942. /* Clear the DMAMUX request generator overrun flag */
  943. hdma->DMAmuxRequestGenStatus->RGCFR = hdma->DMAmuxRequestGenStatusMask;
  944. /* Update error code */
  945. hdma->ErrorCode |= HAL_DMA_ERROR_REQGEN;
  946. }
  947. }
  948. /* Check for DMAMUX Synchronization overrun */
  949. if((hdma->DMAmuxChannelStatus->CSR & hdma->DMAmuxChannelStatusMask) != 0U)
  950. {
  951. /* Clear the DMAMUX synchro overrun flag */
  952. hdma->DMAmuxChannelStatus->CFR = hdma->DMAmuxChannelStatusMask;
  953. /* Update error code */
  954. hdma->ErrorCode |= HAL_DMA_ERROR_SYNC;
  955. }
  956. }
  957. }
  958. /* Get the level transfer complete flag */
  959. if(CompleteLevel == HAL_DMA_FULL_TRANSFER)
  960. {
  961. /* Clear the half transfer and transfer complete flags */
  962. if(IS_DMA_STREAM_INSTANCE(hdma->Instance) != 0U) /* DMA1 or DMA2 instance */
  963. {
  964. (*ifcr_reg) = (DMA_FLAG_HTIF0_4 | DMA_FLAG_TCIF0_4) << (hdma->StreamIndex & 0x1FU);
  965. }
  966. else /* BDMA channel */
  967. {
  968. (*ifcr_reg) = (BDMA_FLAG_TC0 << (hdma->StreamIndex & 0x1FU));
  969. }
  970. hdma->State = HAL_DMA_STATE_READY;
  971. /* Process Unlocked */
  972. __HAL_UNLOCK(hdma);
  973. }
  974. else /*CompleteLevel = HAL_DMA_HALF_TRANSFER*/
  975. {
  976. /* Clear the half transfer and transfer complete flags */
  977. if(IS_DMA_STREAM_INSTANCE(hdma->Instance) != 0U) /* DMA1 or DMA2 instance */
  978. {
  979. (*ifcr_reg) = (DMA_FLAG_HTIF0_4) << (hdma->StreamIndex & 0x1FU);
  980. }
  981. else /* BDMA channel */
  982. {
  983. (*ifcr_reg) = (BDMA_FLAG_HT0 << (hdma->StreamIndex & 0x1FU));
  984. }
  985. }
  986. return status;
  987. }
  988. /**
  989. * @brief Handles DMA interrupt request.
  990. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  991. * the configuration information for the specified DMA Stream.
  992. * @retval None
  993. */
  994. void HAL_DMA_IRQHandler(DMA_HandleTypeDef *hdma)
  995. {
  996. uint32_t tmpisr_dma, tmpisr_bdma;
  997. uint32_t ccr_reg;
  998. __IO uint32_t count = 0U;
  999. uint32_t timeout = SystemCoreClock / 9600U;
  1000. /* calculate DMA base and stream number */
  1001. DMA_Base_Registers *regs_dma = (DMA_Base_Registers *)hdma->StreamBaseAddress;
  1002. BDMA_Base_Registers *regs_bdma = (BDMA_Base_Registers *)hdma->StreamBaseAddress;
  1003. tmpisr_dma = regs_dma->ISR;
  1004. tmpisr_bdma = regs_bdma->ISR;
  1005. if(IS_DMA_STREAM_INSTANCE(hdma->Instance) != 0U) /* DMA1 or DMA2 instance */
  1006. {
  1007. /* Transfer Error Interrupt management ***************************************/
  1008. if ((tmpisr_dma & (DMA_FLAG_TEIF0_4 << (hdma->StreamIndex & 0x1FU))) != 0U)
  1009. {
  1010. if(__HAL_DMA_GET_IT_SOURCE(hdma, DMA_IT_TE) != 0U)
  1011. {
  1012. /* Disable the transfer error interrupt */
  1013. ((DMA_Stream_TypeDef *)hdma->Instance)->CR &= ~(DMA_IT_TE);
  1014. /* Clear the transfer error flag */
  1015. regs_dma->IFCR = DMA_FLAG_TEIF0_4 << (hdma->StreamIndex & 0x1FU);
  1016. /* Update error code */
  1017. hdma->ErrorCode |= HAL_DMA_ERROR_TE;
  1018. }
  1019. }
  1020. /* FIFO Error Interrupt management ******************************************/
  1021. if ((tmpisr_dma & (DMA_FLAG_FEIF0_4 << (hdma->StreamIndex & 0x1FU))) != 0U)
  1022. {
  1023. if(__HAL_DMA_GET_IT_SOURCE(hdma, DMA_IT_FE) != 0U)
  1024. {
  1025. /* Clear the FIFO error flag */
  1026. regs_dma->IFCR = DMA_FLAG_FEIF0_4 << (hdma->StreamIndex & 0x1FU);
  1027. /* Update error code */
  1028. hdma->ErrorCode |= HAL_DMA_ERROR_FE;
  1029. }
  1030. }
  1031. /* Direct Mode Error Interrupt management ***********************************/
  1032. if ((tmpisr_dma & (DMA_FLAG_DMEIF0_4 << (hdma->StreamIndex & 0x1FU))) != 0U)
  1033. {
  1034. if(__HAL_DMA_GET_IT_SOURCE(hdma, DMA_IT_DME) != 0U)
  1035. {
  1036. /* Clear the direct mode error flag */
  1037. regs_dma->IFCR = DMA_FLAG_DMEIF0_4 << (hdma->StreamIndex & 0x1FU);
  1038. /* Update error code */
  1039. hdma->ErrorCode |= HAL_DMA_ERROR_DME;
  1040. }
  1041. }
  1042. /* Half Transfer Complete Interrupt management ******************************/
  1043. if ((tmpisr_dma & (DMA_FLAG_HTIF0_4 << (hdma->StreamIndex & 0x1FU))) != 0U)
  1044. {
  1045. if(__HAL_DMA_GET_IT_SOURCE(hdma, DMA_IT_HT) != 0U)
  1046. {
  1047. /* Clear the half transfer complete flag */
  1048. regs_dma->IFCR = DMA_FLAG_HTIF0_4 << (hdma->StreamIndex & 0x1FU);
  1049. /* Multi_Buffering mode enabled */
  1050. if(((((DMA_Stream_TypeDef *)hdma->Instance)->CR) & (uint32_t)(DMA_SxCR_DBM)) != 0U)
  1051. {
  1052. /* Current memory buffer used is Memory 0 */
  1053. if((((DMA_Stream_TypeDef *)hdma->Instance)->CR & DMA_SxCR_CT) == 0U)
  1054. {
  1055. if(hdma->XferHalfCpltCallback != NULL)
  1056. {
  1057. /* Half transfer callback */
  1058. hdma->XferHalfCpltCallback(hdma);
  1059. }
  1060. }
  1061. /* Current memory buffer used is Memory 1 */
  1062. else
  1063. {
  1064. if(hdma->XferM1HalfCpltCallback != NULL)
  1065. {
  1066. /* Half transfer callback */
  1067. hdma->XferM1HalfCpltCallback(hdma);
  1068. }
  1069. }
  1070. }
  1071. else
  1072. {
  1073. /* Disable the half transfer interrupt if the DMA mode is not CIRCULAR */
  1074. if((((DMA_Stream_TypeDef *)hdma->Instance)->CR & DMA_SxCR_CIRC) == 0U)
  1075. {
  1076. /* Disable the half transfer interrupt */
  1077. ((DMA_Stream_TypeDef *)hdma->Instance)->CR &= ~(DMA_IT_HT);
  1078. }
  1079. if(hdma->XferHalfCpltCallback != NULL)
  1080. {
  1081. /* Half transfer callback */
  1082. hdma->XferHalfCpltCallback(hdma);
  1083. }
  1084. }
  1085. }
  1086. }
  1087. /* Transfer Complete Interrupt management ***********************************/
  1088. if ((tmpisr_dma & (DMA_FLAG_TCIF0_4 << (hdma->StreamIndex & 0x1FU))) != 0U)
  1089. {
  1090. if(__HAL_DMA_GET_IT_SOURCE(hdma, DMA_IT_TC) != 0U)
  1091. {
  1092. /* Clear the transfer complete flag */
  1093. regs_dma->IFCR = DMA_FLAG_TCIF0_4 << (hdma->StreamIndex & 0x1FU);
  1094. if(HAL_DMA_STATE_ABORT == hdma->State)
  1095. {
  1096. /* Disable all the transfer interrupts */
  1097. ((DMA_Stream_TypeDef *)hdma->Instance)->CR &= ~(DMA_IT_TC | DMA_IT_TE | DMA_IT_DME);
  1098. ((DMA_Stream_TypeDef *)hdma->Instance)->FCR &= ~(DMA_IT_FE);
  1099. if((hdma->XferHalfCpltCallback != NULL) || (hdma->XferM1HalfCpltCallback != NULL))
  1100. {
  1101. ((DMA_Stream_TypeDef *)hdma->Instance)->CR &= ~(DMA_IT_HT);
  1102. }
  1103. /* Clear all interrupt flags at correct offset within the register */
  1104. regs_dma->IFCR = 0x3FUL << (hdma->StreamIndex & 0x1FU);
  1105. /* Change the DMA state */
  1106. hdma->State = HAL_DMA_STATE_READY;
  1107. /* Process Unlocked */
  1108. __HAL_UNLOCK(hdma);
  1109. if(hdma->XferAbortCallback != NULL)
  1110. {
  1111. hdma->XferAbortCallback(hdma);
  1112. }
  1113. return;
  1114. }
  1115. if(((((DMA_Stream_TypeDef *)hdma->Instance)->CR) & (uint32_t)(DMA_SxCR_DBM)) != 0U)
  1116. {
  1117. /* Current memory buffer used is Memory 0 */
  1118. if((((DMA_Stream_TypeDef *)hdma->Instance)->CR & DMA_SxCR_CT) == 0U)
  1119. {
  1120. if(hdma->XferM1CpltCallback != NULL)
  1121. {
  1122. /* Transfer complete Callback for memory1 */
  1123. hdma->XferM1CpltCallback(hdma);
  1124. }
  1125. }
  1126. /* Current memory buffer used is Memory 1 */
  1127. else
  1128. {
  1129. if(hdma->XferCpltCallback != NULL)
  1130. {
  1131. /* Transfer complete Callback for memory0 */
  1132. hdma->XferCpltCallback(hdma);
  1133. }
  1134. }
  1135. }
  1136. /* Disable the transfer complete interrupt if the DMA mode is not CIRCULAR */
  1137. else
  1138. {
  1139. if((((DMA_Stream_TypeDef *)hdma->Instance)->CR & DMA_SxCR_CIRC) == 0U)
  1140. {
  1141. /* Disable the transfer complete interrupt */
  1142. ((DMA_Stream_TypeDef *)hdma->Instance)->CR &= ~(DMA_IT_TC);
  1143. /* Change the DMA state */
  1144. hdma->State = HAL_DMA_STATE_READY;
  1145. /* Process Unlocked */
  1146. __HAL_UNLOCK(hdma);
  1147. }
  1148. if(hdma->XferCpltCallback != NULL)
  1149. {
  1150. /* Transfer complete callback */
  1151. hdma->XferCpltCallback(hdma);
  1152. }
  1153. }
  1154. }
  1155. }
  1156. /* manage error case */
  1157. if(hdma->ErrorCode != HAL_DMA_ERROR_NONE)
  1158. {
  1159. if((hdma->ErrorCode & HAL_DMA_ERROR_TE) != 0U)
  1160. {
  1161. hdma->State = HAL_DMA_STATE_ABORT;
  1162. /* Disable the stream */
  1163. __HAL_DMA_DISABLE(hdma);
  1164. do
  1165. {
  1166. if (++count > timeout)
  1167. {
  1168. break;
  1169. }
  1170. }
  1171. while((((DMA_Stream_TypeDef *)hdma->Instance)->CR & DMA_SxCR_EN) != 0U);
  1172. if((((DMA_Stream_TypeDef *)hdma->Instance)->CR & DMA_SxCR_EN) != 0U)
  1173. {
  1174. /* Change the DMA state to error if DMA disable fails */
  1175. hdma->State = HAL_DMA_STATE_ERROR;
  1176. }
  1177. else
  1178. {
  1179. /* Change the DMA state to Ready if DMA disable success */
  1180. hdma->State = HAL_DMA_STATE_READY;
  1181. }
  1182. /* Process Unlocked */
  1183. __HAL_UNLOCK(hdma);
  1184. }
  1185. if(hdma->XferErrorCallback != NULL)
  1186. {
  1187. /* Transfer error callback */
  1188. hdma->XferErrorCallback(hdma);
  1189. }
  1190. }
  1191. }
  1192. else if(IS_BDMA_CHANNEL_INSTANCE(hdma->Instance) != 0U) /* BDMA instance(s) */
  1193. {
  1194. ccr_reg = (((BDMA_Channel_TypeDef *)hdma->Instance)->CCR);
  1195. /* Half Transfer Complete Interrupt management ******************************/
  1196. if (((tmpisr_bdma & (BDMA_FLAG_HT0 << (hdma->StreamIndex & 0x1FU))) != 0U) && ((ccr_reg & BDMA_CCR_HTIE) != 0U))
  1197. {
  1198. /* Clear the half transfer complete flag */
  1199. regs_bdma->IFCR = (BDMA_ISR_HTIF0 << (hdma->StreamIndex & 0x1FU));
  1200. /* Disable the transfer complete interrupt if the DMA mode is Double Buffering */
  1201. if((ccr_reg & BDMA_CCR_DBM) != 0U)
  1202. {
  1203. /* Current memory buffer used is Memory 0 */
  1204. if((ccr_reg & BDMA_CCR_CT) == 0U)
  1205. {
  1206. if(hdma->XferM1HalfCpltCallback != NULL)
  1207. {
  1208. /* Half transfer Callback for Memory 1 */
  1209. hdma->XferM1HalfCpltCallback(hdma);
  1210. }
  1211. }
  1212. /* Current memory buffer used is Memory 1 */
  1213. else
  1214. {
  1215. if(hdma->XferHalfCpltCallback != NULL)
  1216. {
  1217. /* Half transfer Callback for Memory 0 */
  1218. hdma->XferHalfCpltCallback(hdma);
  1219. }
  1220. }
  1221. }
  1222. else
  1223. {
  1224. if((ccr_reg & BDMA_CCR_CIRC) == 0U)
  1225. {
  1226. /* Disable the half transfer interrupt */
  1227. __HAL_DMA_DISABLE_IT(hdma, DMA_IT_HT);
  1228. }
  1229. /* DMA peripheral state is not updated in Half Transfer */
  1230. /* but in Transfer Complete case */
  1231. if(hdma->XferHalfCpltCallback != NULL)
  1232. {
  1233. /* Half transfer callback */
  1234. hdma->XferHalfCpltCallback(hdma);
  1235. }
  1236. }
  1237. }
  1238. /* Transfer Complete Interrupt management ***********************************/
  1239. else if (((tmpisr_bdma & (BDMA_FLAG_TC0 << (hdma->StreamIndex & 0x1FU))) != 0U) && ((ccr_reg & BDMA_CCR_TCIE) != 0U))
  1240. {
  1241. /* Clear the transfer complete flag */
  1242. regs_bdma->IFCR = (BDMA_ISR_TCIF0) << (hdma->StreamIndex & 0x1FU);
  1243. /* Disable the transfer complete interrupt if the DMA mode is Double Buffering */
  1244. if((ccr_reg & BDMA_CCR_DBM) != 0U)
  1245. {
  1246. /* Current memory buffer used is Memory 0 */
  1247. if((ccr_reg & BDMA_CCR_CT) == 0U)
  1248. {
  1249. if(hdma->XferM1CpltCallback != NULL)
  1250. {
  1251. /* Transfer complete Callback for Memory 1 */
  1252. hdma->XferM1CpltCallback(hdma);
  1253. }
  1254. }
  1255. /* Current memory buffer used is Memory 1 */
  1256. else
  1257. {
  1258. if(hdma->XferCpltCallback != NULL)
  1259. {
  1260. /* Transfer complete Callback for Memory 0 */
  1261. hdma->XferCpltCallback(hdma);
  1262. }
  1263. }
  1264. }
  1265. else
  1266. {
  1267. if((ccr_reg & BDMA_CCR_CIRC) == 0U)
  1268. {
  1269. /* Disable the transfer complete and error interrupt, if the DMA mode is not CIRCULAR */
  1270. __HAL_DMA_DISABLE_IT(hdma, DMA_IT_TE | DMA_IT_TC);
  1271. /* Change the DMA state */
  1272. hdma->State = HAL_DMA_STATE_READY;
  1273. /* Process Unlocked */
  1274. __HAL_UNLOCK(hdma);
  1275. }
  1276. if(hdma->XferCpltCallback != NULL)
  1277. {
  1278. /* Transfer complete callback */
  1279. hdma->XferCpltCallback(hdma);
  1280. }
  1281. }
  1282. }
  1283. /* Transfer Error Interrupt management **************************************/
  1284. else if (((tmpisr_bdma & (BDMA_FLAG_TE0 << (hdma->StreamIndex & 0x1FU))) != 0U) && ((ccr_reg & BDMA_CCR_TEIE) != 0U))
  1285. {
  1286. /* When a DMA transfer error occurs */
  1287. /* A hardware clear of its EN bits is performed */
  1288. /* Disable ALL DMA IT */
  1289. __HAL_DMA_DISABLE_IT(hdma, (DMA_IT_TC | DMA_IT_HT | DMA_IT_TE));
  1290. /* Clear all flags */
  1291. regs_bdma->IFCR = (BDMA_ISR_GIF0) << (hdma->StreamIndex & 0x1FU);
  1292. /* Update error code */
  1293. hdma->ErrorCode = HAL_DMA_ERROR_TE;
  1294. /* Change the DMA state */
  1295. hdma->State = HAL_DMA_STATE_READY;
  1296. /* Process Unlocked */
  1297. __HAL_UNLOCK(hdma);
  1298. if (hdma->XferErrorCallback != NULL)
  1299. {
  1300. /* Transfer error callback */
  1301. hdma->XferErrorCallback(hdma);
  1302. }
  1303. }
  1304. else
  1305. {
  1306. /* Nothing To Do */
  1307. }
  1308. }
  1309. else
  1310. {
  1311. /* Nothing To Do */
  1312. }
  1313. }
  1314. /**
  1315. * @brief Register callbacks
  1316. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1317. * the configuration information for the specified DMA Stream.
  1318. * @param CallbackID: User Callback identifier
  1319. * a DMA_HandleTypeDef structure as parameter.
  1320. * @param pCallback: pointer to private callback function which has pointer to
  1321. * a DMA_HandleTypeDef structure as parameter.
  1322. * @retval HAL status
  1323. */
  1324. HAL_StatusTypeDef HAL_DMA_RegisterCallback(DMA_HandleTypeDef *hdma, HAL_DMA_CallbackIDTypeDef CallbackID, void (* pCallback)(DMA_HandleTypeDef *_hdma))
  1325. {
  1326. HAL_StatusTypeDef status = HAL_OK;
  1327. /* Check the DMA peripheral handle */
  1328. if(hdma == NULL)
  1329. {
  1330. return HAL_ERROR;
  1331. }
  1332. /* Process locked */
  1333. __HAL_LOCK(hdma);
  1334. if(HAL_DMA_STATE_READY == hdma->State)
  1335. {
  1336. switch (CallbackID)
  1337. {
  1338. case HAL_DMA_XFER_CPLT_CB_ID:
  1339. hdma->XferCpltCallback = pCallback;
  1340. break;
  1341. case HAL_DMA_XFER_HALFCPLT_CB_ID:
  1342. hdma->XferHalfCpltCallback = pCallback;
  1343. break;
  1344. case HAL_DMA_XFER_M1CPLT_CB_ID:
  1345. hdma->XferM1CpltCallback = pCallback;
  1346. break;
  1347. case HAL_DMA_XFER_M1HALFCPLT_CB_ID:
  1348. hdma->XferM1HalfCpltCallback = pCallback;
  1349. break;
  1350. case HAL_DMA_XFER_ERROR_CB_ID:
  1351. hdma->XferErrorCallback = pCallback;
  1352. break;
  1353. case HAL_DMA_XFER_ABORT_CB_ID:
  1354. hdma->XferAbortCallback = pCallback;
  1355. break;
  1356. default:
  1357. status = HAL_ERROR;
  1358. break;
  1359. }
  1360. }
  1361. else
  1362. {
  1363. /* Return error status */
  1364. status = HAL_ERROR;
  1365. }
  1366. /* Release Lock */
  1367. __HAL_UNLOCK(hdma);
  1368. return status;
  1369. }
  1370. /**
  1371. * @brief UnRegister callbacks
  1372. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1373. * the configuration information for the specified DMA Stream.
  1374. * @param CallbackID: User Callback identifier
  1375. * a HAL_DMA_CallbackIDTypeDef ENUM as parameter.
  1376. * @retval HAL status
  1377. */
  1378. HAL_StatusTypeDef HAL_DMA_UnRegisterCallback(DMA_HandleTypeDef *hdma, HAL_DMA_CallbackIDTypeDef CallbackID)
  1379. {
  1380. HAL_StatusTypeDef status = HAL_OK;
  1381. /* Check the DMA peripheral handle */
  1382. if(hdma == NULL)
  1383. {
  1384. return HAL_ERROR;
  1385. }
  1386. /* Process locked */
  1387. __HAL_LOCK(hdma);
  1388. if(HAL_DMA_STATE_READY == hdma->State)
  1389. {
  1390. switch (CallbackID)
  1391. {
  1392. case HAL_DMA_XFER_CPLT_CB_ID:
  1393. hdma->XferCpltCallback = NULL;
  1394. break;
  1395. case HAL_DMA_XFER_HALFCPLT_CB_ID:
  1396. hdma->XferHalfCpltCallback = NULL;
  1397. break;
  1398. case HAL_DMA_XFER_M1CPLT_CB_ID:
  1399. hdma->XferM1CpltCallback = NULL;
  1400. break;
  1401. case HAL_DMA_XFER_M1HALFCPLT_CB_ID:
  1402. hdma->XferM1HalfCpltCallback = NULL;
  1403. break;
  1404. case HAL_DMA_XFER_ERROR_CB_ID:
  1405. hdma->XferErrorCallback = NULL;
  1406. break;
  1407. case HAL_DMA_XFER_ABORT_CB_ID:
  1408. hdma->XferAbortCallback = NULL;
  1409. break;
  1410. case HAL_DMA_XFER_ALL_CB_ID:
  1411. hdma->XferCpltCallback = NULL;
  1412. hdma->XferHalfCpltCallback = NULL;
  1413. hdma->XferM1CpltCallback = NULL;
  1414. hdma->XferM1HalfCpltCallback = NULL;
  1415. hdma->XferErrorCallback = NULL;
  1416. hdma->XferAbortCallback = NULL;
  1417. break;
  1418. default:
  1419. status = HAL_ERROR;
  1420. break;
  1421. }
  1422. }
  1423. else
  1424. {
  1425. status = HAL_ERROR;
  1426. }
  1427. /* Release Lock */
  1428. __HAL_UNLOCK(hdma);
  1429. return status;
  1430. }
  1431. /**
  1432. * @}
  1433. */
  1434. /** @addtogroup DMA_Exported_Functions_Group3
  1435. *
  1436. @verbatim
  1437. ===============================================================================
  1438. ##### State and Errors functions #####
  1439. ===============================================================================
  1440. [..]
  1441. This subsection provides functions allowing to
  1442. (+) Check the DMA state
  1443. (+) Get error code
  1444. @endverbatim
  1445. * @{
  1446. */
  1447. /**
  1448. * @brief Returns the DMA state.
  1449. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1450. * the configuration information for the specified DMA Stream.
  1451. * @retval HAL state
  1452. */
  1453. HAL_DMA_StateTypeDef HAL_DMA_GetState(DMA_HandleTypeDef *hdma)
  1454. {
  1455. return hdma->State;
  1456. }
  1457. /**
  1458. * @brief Return the DMA error code
  1459. * @param hdma : pointer to a DMA_HandleTypeDef structure that contains
  1460. * the configuration information for the specified DMA Stream.
  1461. * @retval DMA Error Code
  1462. */
  1463. uint32_t HAL_DMA_GetError(DMA_HandleTypeDef *hdma)
  1464. {
  1465. return hdma->ErrorCode;
  1466. }
  1467. /**
  1468. * @}
  1469. */
  1470. /**
  1471. * @}
  1472. */
  1473. /** @addtogroup DMA_Private_Functions
  1474. * @{
  1475. */
  1476. /**
  1477. * @brief Sets the DMA Transfer parameter.
  1478. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1479. * the configuration information for the specified DMA Stream.
  1480. * @param SrcAddress: The source memory Buffer address
  1481. * @param DstAddress: The destination memory Buffer address
  1482. * @param DataLength: The length of data to be transferred from source to destination
  1483. * @retval None
  1484. */
  1485. static void DMA_SetConfig(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength)
  1486. {
  1487. /* calculate DMA base and stream number */
  1488. DMA_Base_Registers *regs_dma = (DMA_Base_Registers *)hdma->StreamBaseAddress;
  1489. BDMA_Base_Registers *regs_bdma = (BDMA_Base_Registers *)hdma->StreamBaseAddress;
  1490. if(IS_DMA_DMAMUX_ALL_INSTANCE(hdma->Instance) != 0U) /* No DMAMUX available for BDMA1 */
  1491. {
  1492. /* Clear the DMAMUX synchro overrun flag */
  1493. hdma->DMAmuxChannelStatus->CFR = hdma->DMAmuxChannelStatusMask;
  1494. if(hdma->DMAmuxRequestGen != 0U)
  1495. {
  1496. /* Clear the DMAMUX request generator overrun flag */
  1497. hdma->DMAmuxRequestGenStatus->RGCFR = hdma->DMAmuxRequestGenStatusMask;
  1498. }
  1499. }
  1500. if(IS_DMA_STREAM_INSTANCE(hdma->Instance) != 0U) /* DMA1 or DMA2 instance */
  1501. {
  1502. /* Clear all interrupt flags at correct offset within the register */
  1503. regs_dma->IFCR = 0x3FUL << (hdma->StreamIndex & 0x1FU);
  1504. /* Clear DBM bit */
  1505. ((DMA_Stream_TypeDef *)hdma->Instance)->CR &= (uint32_t)(~DMA_SxCR_DBM);
  1506. /* Configure DMA Stream data length */
  1507. ((DMA_Stream_TypeDef *)hdma->Instance)->NDTR = DataLength;
  1508. /* Peripheral to Memory */
  1509. if((hdma->Init.Direction) == DMA_MEMORY_TO_PERIPH)
  1510. {
  1511. /* Configure DMA Stream destination address */
  1512. ((DMA_Stream_TypeDef *)hdma->Instance)->PAR = DstAddress;
  1513. /* Configure DMA Stream source address */
  1514. ((DMA_Stream_TypeDef *)hdma->Instance)->M0AR = SrcAddress;
  1515. }
  1516. /* Memory to Peripheral */
  1517. else
  1518. {
  1519. /* Configure DMA Stream source address */
  1520. ((DMA_Stream_TypeDef *)hdma->Instance)->PAR = SrcAddress;
  1521. /* Configure DMA Stream destination address */
  1522. ((DMA_Stream_TypeDef *)hdma->Instance)->M0AR = DstAddress;
  1523. }
  1524. }
  1525. else if(IS_BDMA_CHANNEL_INSTANCE(hdma->Instance) != 0U) /* BDMA instance(s) */
  1526. {
  1527. /* Clear all flags */
  1528. regs_bdma->IFCR = (BDMA_ISR_GIF0) << (hdma->StreamIndex & 0x1FU);
  1529. /* Configure DMA Channel data length */
  1530. ((BDMA_Channel_TypeDef *)hdma->Instance)->CNDTR = DataLength;
  1531. /* Peripheral to Memory */
  1532. if((hdma->Init.Direction) == DMA_MEMORY_TO_PERIPH)
  1533. {
  1534. /* Configure DMA Channel destination address */
  1535. ((BDMA_Channel_TypeDef *)hdma->Instance)->CPAR = DstAddress;
  1536. /* Configure DMA Channel source address */
  1537. ((BDMA_Channel_TypeDef *)hdma->Instance)->CM0AR = SrcAddress;
  1538. }
  1539. /* Memory to Peripheral */
  1540. else
  1541. {
  1542. /* Configure DMA Channel source address */
  1543. ((BDMA_Channel_TypeDef *)hdma->Instance)->CPAR = SrcAddress;
  1544. /* Configure DMA Channel destination address */
  1545. ((BDMA_Channel_TypeDef *)hdma->Instance)->CM0AR = DstAddress;
  1546. }
  1547. }
  1548. else
  1549. {
  1550. /* Nothing To Do */
  1551. }
  1552. }
  1553. /**
  1554. * @brief Returns the DMA Stream base address depending on stream number
  1555. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1556. * the configuration information for the specified DMA Stream.
  1557. * @retval Stream base address
  1558. */
  1559. static uint32_t DMA_CalcBaseAndBitshift(DMA_HandleTypeDef *hdma)
  1560. {
  1561. if(IS_DMA_STREAM_INSTANCE(hdma->Instance) != 0U) /* DMA1 or DMA2 instance */
  1562. {
  1563. uint32_t stream_number = (((uint32_t)((uint32_t*)hdma->Instance) & 0xFFU) - 16U) / 24U;
  1564. /* lookup table for necessary bitshift of flags within status registers */
  1565. static const uint8_t flagBitshiftOffset[8U] = {0U, 6U, 16U, 22U, 0U, 6U, 16U, 22U};
  1566. hdma->StreamIndex = flagBitshiftOffset[stream_number & 0x7U];
  1567. if (stream_number > 3U)
  1568. {
  1569. /* return pointer to HISR and HIFCR */
  1570. hdma->StreamBaseAddress = (((uint32_t)((uint32_t*)hdma->Instance) & (uint32_t)(~0x3FFU)) + 4U);
  1571. }
  1572. else
  1573. {
  1574. /* return pointer to LISR and LIFCR */
  1575. hdma->StreamBaseAddress = ((uint32_t)((uint32_t*)hdma->Instance) & (uint32_t)(~0x3FFU));
  1576. }
  1577. }
  1578. else /* BDMA instance(s) */
  1579. {
  1580. /* return pointer to ISR and IFCR */
  1581. hdma->StreamBaseAddress = ((uint32_t)((uint32_t*)hdma->Instance) & (uint32_t)(~0xFFU));
  1582. }
  1583. return hdma->StreamBaseAddress;
  1584. }
  1585. /**
  1586. * @brief Check compatibility between FIFO threshold level and size of the memory burst
  1587. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1588. * the configuration information for the specified DMA Stream.
  1589. * @retval HAL status
  1590. */
  1591. static HAL_StatusTypeDef DMA_CheckFifoParam(DMA_HandleTypeDef *hdma)
  1592. {
  1593. HAL_StatusTypeDef status = HAL_OK;
  1594. /* Memory Data size equal to Byte */
  1595. if (hdma->Init.MemDataAlignment == DMA_MDATAALIGN_BYTE)
  1596. {
  1597. switch (hdma->Init.FIFOThreshold)
  1598. {
  1599. case DMA_FIFO_THRESHOLD_1QUARTERFULL:
  1600. case DMA_FIFO_THRESHOLD_3QUARTERSFULL:
  1601. if ((hdma->Init.MemBurst & DMA_SxCR_MBURST_1) == DMA_SxCR_MBURST_1)
  1602. {
  1603. status = HAL_ERROR;
  1604. }
  1605. break;
  1606. case DMA_FIFO_THRESHOLD_HALFFULL:
  1607. if (hdma->Init.MemBurst == DMA_MBURST_INC16)
  1608. {
  1609. status = HAL_ERROR;
  1610. }
  1611. break;
  1612. case DMA_FIFO_THRESHOLD_FULL:
  1613. break;
  1614. default:
  1615. break;
  1616. }
  1617. }
  1618. /* Memory Data size equal to Half-Word */
  1619. else if (hdma->Init.MemDataAlignment == DMA_MDATAALIGN_HALFWORD)
  1620. {
  1621. switch (hdma->Init.FIFOThreshold)
  1622. {
  1623. case DMA_FIFO_THRESHOLD_1QUARTERFULL:
  1624. case DMA_FIFO_THRESHOLD_3QUARTERSFULL:
  1625. status = HAL_ERROR;
  1626. break;
  1627. case DMA_FIFO_THRESHOLD_HALFFULL:
  1628. if ((hdma->Init.MemBurst & DMA_SxCR_MBURST_1) == DMA_SxCR_MBURST_1)
  1629. {
  1630. status = HAL_ERROR;
  1631. }
  1632. break;
  1633. case DMA_FIFO_THRESHOLD_FULL:
  1634. if (hdma->Init.MemBurst == DMA_MBURST_INC16)
  1635. {
  1636. status = HAL_ERROR;
  1637. }
  1638. break;
  1639. default:
  1640. break;
  1641. }
  1642. }
  1643. /* Memory Data size equal to Word */
  1644. else
  1645. {
  1646. switch (hdma->Init.FIFOThreshold)
  1647. {
  1648. case DMA_FIFO_THRESHOLD_1QUARTERFULL:
  1649. case DMA_FIFO_THRESHOLD_HALFFULL:
  1650. case DMA_FIFO_THRESHOLD_3QUARTERSFULL:
  1651. status = HAL_ERROR;
  1652. break;
  1653. case DMA_FIFO_THRESHOLD_FULL:
  1654. if ((hdma->Init.MemBurst & DMA_SxCR_MBURST_1) == DMA_SxCR_MBURST_1)
  1655. {
  1656. status = HAL_ERROR;
  1657. }
  1658. break;
  1659. default:
  1660. break;
  1661. }
  1662. }
  1663. return status;
  1664. }
  1665. /**
  1666. * @brief Updates the DMA handle with the DMAMUX channel and status mask depending on stream number
  1667. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1668. * the configuration information for the specified DMA Stream.
  1669. * @retval HAL status
  1670. */
  1671. static void DMA_CalcDMAMUXChannelBaseAndMask(DMA_HandleTypeDef *hdma)
  1672. {
  1673. uint32_t stream_number;
  1674. uint32_t stream_baseaddress = (uint32_t)((uint32_t*)hdma->Instance);
  1675. if(IS_BDMA_CHANNEL_DMAMUX_INSTANCE(hdma->Instance) != 0U)
  1676. {
  1677. /* BDMA Channels are connected to DMAMUX2 channels */
  1678. stream_number = (((uint32_t)((uint32_t*)hdma->Instance) & 0xFFU) - 8U) / 20U;
  1679. hdma->DMAmuxChannel = (DMAMUX_Channel_TypeDef *)((uint32_t)(((uint32_t)DMAMUX2_Channel0) + (stream_number * 4U)));
  1680. hdma->DMAmuxChannelStatus = DMAMUX2_ChannelStatus;
  1681. hdma->DMAmuxChannelStatusMask = 1UL << (stream_number & 0x1FU);
  1682. }
  1683. else
  1684. {
  1685. /* DMA1/DMA2 Streams are connected to DMAMUX1 channels */
  1686. stream_number = (((uint32_t)((uint32_t*)hdma->Instance) & 0xFFU) - 16U) / 24U;
  1687. if((stream_baseaddress <= ((uint32_t)DMA2_Stream7) ) && \
  1688. (stream_baseaddress >= ((uint32_t)DMA2_Stream0)))
  1689. {
  1690. stream_number += 8U;
  1691. }
  1692. hdma->DMAmuxChannel = (DMAMUX_Channel_TypeDef *)((uint32_t)(((uint32_t)DMAMUX1_Channel0) + (stream_number * 4U)));
  1693. hdma->DMAmuxChannelStatus = DMAMUX1_ChannelStatus;
  1694. hdma->DMAmuxChannelStatusMask = 1UL << (stream_number & 0x1FU);
  1695. }
  1696. }
  1697. /**
  1698. * @brief Updates the DMA handle with the DMAMUX request generator params
  1699. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1700. * the configuration information for the specified DMA Stream.
  1701. * @retval HAL status
  1702. */
  1703. static void DMA_CalcDMAMUXRequestGenBaseAndMask(DMA_HandleTypeDef *hdma)
  1704. {
  1705. uint32_t request = hdma->Init.Request & DMAMUX_CxCR_DMAREQ_ID;
  1706. if((request >= DMA_REQUEST_GENERATOR0) && (request <= DMA_REQUEST_GENERATOR7))
  1707. {
  1708. if(IS_BDMA_CHANNEL_DMAMUX_INSTANCE(hdma->Instance) != 0U)
  1709. {
  1710. /* BDMA Channels are connected to DMAMUX2 request generator blocks */
  1711. hdma->DMAmuxRequestGen = (DMAMUX_RequestGen_TypeDef *)((uint32_t)(((uint32_t)DMAMUX2_RequestGenerator0) + ((request - 1U) * 4U)));
  1712. hdma->DMAmuxRequestGenStatus = DMAMUX2_RequestGenStatus;
  1713. }
  1714. else
  1715. {
  1716. /* DMA1 and DMA2 Streams use DMAMUX1 request generator blocks */
  1717. hdma->DMAmuxRequestGen = (DMAMUX_RequestGen_TypeDef *)((uint32_t)(((uint32_t)DMAMUX1_RequestGenerator0) + ((request - 1U) * 4U)));
  1718. hdma->DMAmuxRequestGenStatus = DMAMUX1_RequestGenStatus;
  1719. }
  1720. hdma->DMAmuxRequestGenStatusMask = 1UL << (request - 1U);
  1721. }
  1722. }
  1723. /**
  1724. * @}
  1725. */
  1726. #endif /* HAL_DMA_MODULE_ENABLED */
  1727. /**
  1728. * @}
  1729. */
  1730. /**
  1731. * @}
  1732. */