stm32h7xx_hal_dma.c 62 KB

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