stm32g0xx_hal_dma.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193
  1. /**
  2. ******************************************************************************
  3. * @file stm32g0xx_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. ******************************************************************************
  13. * @attention
  14. *
  15. * Copyright (c) 2018 STMicroelectronics.
  16. * All rights reserved.
  17. *
  18. * This software is licensed under terms that can be found in the LICENSE file
  19. * in the root directory of this software component.
  20. * If no LICENSE file comes with this software, it is provided AS-IS.
  21. *
  22. ******************************************************************************
  23. @verbatim
  24. ==============================================================================
  25. ##### How to use this driver #####
  26. ==============================================================================
  27. [..]
  28. (#) Enable and configure the peripheral to be connected to the DMA Channel
  29. (except for internal SRAM / FLASH memories: no initialization is
  30. necessary). Please refer to the Reference manual for connection between peripherals
  31. and DMA requests.
  32. (#) For a given Channel, program the required configuration through the following parameters:
  33. Channel request, Transfer Direction, Source and Destination data formats,
  34. Circular or Normal mode, Channel Priority level, Source and Destination Increment mode
  35. using HAL_DMA_Init() function.
  36. Prior to HAL_DMA_Init the peripheral clock shall be enabled for both DMA & DMAMUX
  37. thanks to:
  38. (##) DMA1 or DMA2: __HAL_RCC_DMA1_CLK_ENABLE() or __HAL_RCC_DMA2_CLK_ENABLE();
  39. (#) Use HAL_DMA_GetState() function to return the DMA state and HAL_DMA_GetError() in case of error
  40. detection.
  41. (#) Use HAL_DMA_Abort() function to abort the current transfer
  42. -@- In Memory-to-Memory transfer mode, Circular mode is not allowed.
  43. *** Polling mode IO operation ***
  44. =================================
  45. [..]
  46. (+) Use HAL_DMA_Start() to start DMA transfer after the configuration of Source
  47. address and destination address and the Length of data to be transferred
  48. (+) Use HAL_DMA_PollForTransfer() to poll for the end of current transfer, in this
  49. case a fixed Timeout can be configured by User depending from his application.
  50. *** Interrupt mode IO operation ***
  51. ===================================
  52. [..]
  53. (+) Configure the DMA interrupt priority using HAL_NVIC_SetPriority()
  54. (+) Enable the DMA IRQ handler using HAL_NVIC_EnableIRQ()
  55. (+) Use HAL_DMA_Start_IT() to start DMA transfer after the configuration of
  56. Source address and destination address and the Length of data to be transferred.
  57. In this case the DMA interrupt is configured
  58. (+) Use HAL_DMA_IRQHandler() called under DMA_IRQHandler() Interrupt subroutine
  59. (+) At the end of data transfer HAL_DMA_IRQHandler() function is executed and user can
  60. add his own function to register callbacks with HAL_DMA_RegisterCallback().
  61. *** DMA HAL driver macros list ***
  62. =============================================
  63. [..]
  64. Below the list of macros in DMA HAL driver.
  65. (+) __HAL_DMA_ENABLE: Enable the specified DMA Channel.
  66. (+) __HAL_DMA_DISABLE: Disable the specified DMA Channel.
  67. (+) __HAL_DMA_GET_FLAG: Get the DMA Channel pending flags.
  68. (+) __HAL_DMA_CLEAR_FLAG: Clear the DMA Channel pending flags.
  69. (+) __HAL_DMA_ENABLE_IT: Enable the specified DMA Channel interrupts.
  70. (+) __HAL_DMA_DISABLE_IT: Disable the specified DMA Channel interrupts.
  71. (+) __HAL_DMA_GET_IT_SOURCE: Check whether the specified DMA Channel interrupt is enabled or not.
  72. [..]
  73. (@) You can refer to the DMA HAL driver header file for more useful macros
  74. @endverbatim
  75. ******************************************************************************
  76. */
  77. /* Includes ------------------------------------------------------------------*/
  78. #include "stm32g0xx_hal.h"
  79. /** @addtogroup STM32G0xx_HAL_Driver
  80. * @{
  81. */
  82. /** @defgroup DMA DMA
  83. * @brief DMA HAL module driver
  84. * @{
  85. */
  86. #ifdef HAL_DMA_MODULE_ENABLED
  87. /* Private typedef -----------------------------------------------------------*/
  88. /* Private define ------------------------------------------------------------*/
  89. /* Private macro -------------------------------------------------------------*/
  90. /* Private variables ---------------------------------------------------------*/
  91. /* Private function prototypes -----------------------------------------------*/
  92. /** @defgroup DMA_Private_Functions DMA Private Functions
  93. * @{
  94. */
  95. static void DMA_SetConfig(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength);
  96. static void DMA_CalcDMAMUXChannelBaseAndMask(DMA_HandleTypeDef *hdma);
  97. static void DMA_CalcDMAMUXRequestGenBaseAndMask(DMA_HandleTypeDef *hdma);
  98. /**
  99. * @}
  100. */
  101. /* Exported functions ---------------------------------------------------------*/
  102. /** @defgroup DMA_Exported_Functions DMA Exported Functions
  103. * @{
  104. */
  105. /** @defgroup DMA_Exported_Functions_Group1 Initialization and de-initialization functions
  106. * @brief Initialization and de-initialization functions
  107. *
  108. @verbatim
  109. ===============================================================================
  110. ##### Initialization and de-initialization functions #####
  111. ===============================================================================
  112. [..]
  113. This section provides functions allowing to initialize the DMA Channel source
  114. and destination addresses, incrementation and data sizes, transfer direction,
  115. circular/normal mode selection, memory-to-memory mode selection and Channel priority value.
  116. [..]
  117. The HAL_DMA_Init() function follows the DMA configuration procedures as described in
  118. reference manual.
  119. @endverbatim
  120. * @{
  121. */
  122. /**
  123. * @brief Initialize the DMA according to the specified
  124. * parameters in the DMA_InitTypeDef and initialize the associated handle.
  125. * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
  126. * the configuration information for the specified DMA Channel.
  127. * @retval HAL status
  128. */
  129. HAL_StatusTypeDef HAL_DMA_Init(DMA_HandleTypeDef *hdma)
  130. {
  131. /* Check the DMA handle allocation */
  132. if (hdma == NULL)
  133. {
  134. return HAL_ERROR;
  135. }
  136. /* Check the parameters */
  137. assert_param(IS_DMA_ALL_INSTANCE(hdma->Instance));
  138. assert_param(IS_DMA_DIRECTION(hdma->Init.Direction));
  139. assert_param(IS_DMA_PERIPHERAL_INC_STATE(hdma->Init.PeriphInc));
  140. assert_param(IS_DMA_MEMORY_INC_STATE(hdma->Init.MemInc));
  141. assert_param(IS_DMA_PERIPHERAL_DATA_SIZE(hdma->Init.PeriphDataAlignment));
  142. assert_param(IS_DMA_MEMORY_DATA_SIZE(hdma->Init.MemDataAlignment));
  143. assert_param(IS_DMA_MODE(hdma->Init.Mode));
  144. assert_param(IS_DMA_PRIORITY(hdma->Init.Priority));
  145. assert_param(IS_DMA_ALL_REQUEST(hdma->Init.Request));
  146. /* Compute the channel index */
  147. #if defined(DMA2)
  148. if ((uint32_t)(hdma->Instance) < (uint32_t)(DMA2_Channel1))
  149. {
  150. /* DMA1 */
  151. hdma->ChannelIndex = (((uint32_t)hdma->Instance - (uint32_t)DMA1_Channel1) / ((uint32_t)DMA1_Channel2 - (uint32_t)DMA1_Channel1)) << 2U;
  152. hdma->DmaBaseAddress = DMA1;
  153. }
  154. else
  155. {
  156. /* DMA2 */
  157. hdma->ChannelIndex = (((uint32_t)hdma->Instance - (uint32_t)DMA2_Channel1) / ((uint32_t)DMA2_Channel2 - (uint32_t)DMA2_Channel1)) << 2U;
  158. hdma->DmaBaseAddress = DMA2;
  159. }
  160. #else
  161. hdma->ChannelIndex = (((uint32_t)hdma->Instance - (uint32_t)DMA1_Channel1) / ((uint32_t)DMA1_Channel2 - (uint32_t)DMA1_Channel1)) << 2U;
  162. #endif /* DMA2 */
  163. /* Change DMA peripheral state */
  164. hdma->State = HAL_DMA_STATE_BUSY;
  165. /* Clear PL, MSIZE, PSIZE, MINC, PINC, CIRC, DIR and MEM2MEM bits */
  166. CLEAR_BIT(hdma->Instance->CCR, (DMA_CCR_PL | DMA_CCR_MSIZE | DMA_CCR_PSIZE | \
  167. DMA_CCR_MINC | DMA_CCR_PINC | DMA_CCR_CIRC | \
  168. DMA_CCR_DIR | DMA_CCR_MEM2MEM));
  169. /* Set the DMA Channel configuration */
  170. SET_BIT(hdma->Instance->CCR, (hdma->Init.Direction | \
  171. hdma->Init.PeriphInc | hdma->Init.MemInc | \
  172. hdma->Init.PeriphDataAlignment | hdma->Init.MemDataAlignment | \
  173. hdma->Init.Mode | hdma->Init.Priority));
  174. /* Initialize parameters for DMAMUX channel :
  175. DMAmuxChannel, DMAmuxChannelStatus and DMAmuxChannelStatusMask
  176. */
  177. DMA_CalcDMAMUXChannelBaseAndMask(hdma);
  178. if (hdma->Init.Direction == DMA_MEMORY_TO_MEMORY)
  179. {
  180. /* if memory to memory force the request to 0*/
  181. hdma->Init.Request = DMA_REQUEST_MEM2MEM;
  182. }
  183. /* Set peripheral request to DMAMUX channel */
  184. hdma->DMAmuxChannel->CCR = (hdma->Init.Request & DMAMUX_CxCR_DMAREQ_ID);
  185. /* Clear the DMAMUX synchro overrun flag */
  186. hdma->DMAmuxChannelStatus->CFR = hdma->DMAmuxChannelStatusMask;
  187. if (((hdma->Init.Request > 0UL) && (hdma->Init.Request <= DMA_REQUEST_GENERATOR3)))
  188. {
  189. /* Initialize parameters for DMAMUX request generator :
  190. DMAmuxRequestGen, DMAmuxRequestGenStatus and DMAmuxRequestGenStatusMask
  191. */
  192. DMA_CalcDMAMUXRequestGenBaseAndMask(hdma);
  193. /* Reset the DMAMUX request generator register*/
  194. hdma->DMAmuxRequestGen->RGCR = 0U;
  195. /* Clear the DMAMUX request generator overrun flag */
  196. hdma->DMAmuxRequestGenStatus->RGCFR = hdma->DMAmuxRequestGenStatusMask;
  197. }
  198. else
  199. {
  200. hdma->DMAmuxRequestGen = 0U;
  201. hdma->DMAmuxRequestGenStatus = 0U;
  202. hdma->DMAmuxRequestGenStatusMask = 0U;
  203. }
  204. /* Initialize the error code */
  205. hdma->ErrorCode = HAL_DMA_ERROR_NONE;
  206. /* Initialize the DMA state*/
  207. hdma->State = HAL_DMA_STATE_READY;
  208. /* Release Lock */
  209. __HAL_UNLOCK(hdma);
  210. return HAL_OK;
  211. }
  212. /**
  213. * @brief DeInitialize the DMA peripheral.
  214. * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
  215. * the configuration information for the specified DMA Channel.
  216. * @retval HAL status
  217. */
  218. HAL_StatusTypeDef HAL_DMA_DeInit(DMA_HandleTypeDef *hdma)
  219. {
  220. /* Check the DMA handle allocation */
  221. if (NULL == hdma)
  222. {
  223. return HAL_ERROR;
  224. }
  225. /* Check the parameters */
  226. assert_param(IS_DMA_ALL_INSTANCE(hdma->Instance));
  227. /* Disable the selected DMA Channelx */
  228. __HAL_DMA_DISABLE(hdma);
  229. /* Compute the channel index */
  230. #if defined(DMA2)
  231. if ((uint32_t)(hdma->Instance) < (uint32_t)(DMA2_Channel1))
  232. {
  233. /* DMA1 */
  234. hdma->ChannelIndex = (((uint32_t)hdma->Instance - (uint32_t)DMA1_Channel1) / ((uint32_t)DMA1_Channel2 - (uint32_t)DMA1_Channel1)) << 2U;
  235. hdma->DmaBaseAddress = DMA1;
  236. }
  237. else
  238. {
  239. /* DMA2 */
  240. hdma->ChannelIndex = (((uint32_t)hdma->Instance - (uint32_t)DMA2_Channel1) / ((uint32_t)DMA2_Channel2 - (uint32_t)DMA2_Channel1)) << 2U;
  241. hdma->DmaBaseAddress = DMA2;
  242. }
  243. #else
  244. hdma->ChannelIndex = (((uint32_t)hdma->Instance - (uint32_t)DMA1_Channel1) / ((uint32_t)DMA1_Channel2 - (uint32_t)DMA1_Channel1)) << 2U;
  245. #endif /* DMA2 */
  246. /* Reset DMA Channel control register */
  247. hdma->Instance->CCR = 0U;
  248. /* Clear all flags */
  249. #if defined(DMA2)
  250. hdma->DmaBaseAddress->IFCR = (DMA_ISR_GIF1 << (hdma->ChannelIndex & 0x1CU));
  251. #else
  252. __HAL_DMA_CLEAR_FLAG(hdma, (DMA_FLAG_GI1 << (hdma->ChannelIndex & 0x1CU)));
  253. #endif /* DMA2 */
  254. /* Initialize parameters for DMAMUX channel :
  255. DMAmuxChannel, DMAmuxChannelStatus and DMAmuxChannelStatusMask */
  256. DMA_CalcDMAMUXChannelBaseAndMask(hdma);
  257. /* Reset the DMAMUX channel that corresponds to the DMA channel */
  258. hdma->DMAmuxChannel->CCR = 0U;
  259. /* Clear the DMAMUX synchro overrun flag */
  260. hdma->DMAmuxChannelStatus->CFR = hdma->DMAmuxChannelStatusMask;
  261. /* Reset Request generator parameters if any */
  262. if (((hdma->Init.Request > 0UL) && (hdma->Init.Request <= DMA_REQUEST_GENERATOR3)))
  263. {
  264. /* Initialize parameters for DMAMUX request generator :
  265. DMAmuxRequestGen, DMAmuxRequestGenStatus and DMAmuxRequestGenStatusMask
  266. */
  267. DMA_CalcDMAMUXRequestGenBaseAndMask(hdma);
  268. /* Reset the DMAMUX request generator register*/
  269. hdma->DMAmuxRequestGen->RGCR = 0U;
  270. /* Clear the DMAMUX request generator overrun flag */
  271. hdma->DMAmuxRequestGenStatus->RGCFR = hdma->DMAmuxRequestGenStatusMask;
  272. }
  273. hdma->DMAmuxRequestGen = 0U;
  274. hdma->DMAmuxRequestGenStatus = 0U;
  275. hdma->DMAmuxRequestGenStatusMask = 0U;
  276. /* Clean callbacks */
  277. hdma->XferCpltCallback = NULL;
  278. hdma->XferHalfCpltCallback = NULL;
  279. hdma->XferErrorCallback = NULL;
  280. hdma->XferAbortCallback = NULL;
  281. /* Initialize the error code */
  282. hdma->ErrorCode = HAL_DMA_ERROR_NONE;
  283. /* Initialize the DMA state */
  284. hdma->State = HAL_DMA_STATE_RESET;
  285. /* Release Lock */
  286. __HAL_UNLOCK(hdma);
  287. return HAL_OK;
  288. }
  289. /**
  290. * @}
  291. */
  292. /** @defgroup DMA_Exported_Functions_Group2 Input and Output operation functions
  293. * @brief Input and Output operation functions
  294. *
  295. @verbatim
  296. ===============================================================================
  297. ##### IO operation functions #####
  298. ===============================================================================
  299. [..] This section provides functions allowing to:
  300. (+) Configure the source, destination address and data length and Start DMA transfer
  301. (+) Configure the source, destination address and data length and
  302. Start DMA transfer with interrupt
  303. (+) Abort DMA transfer
  304. (+) Poll for transfer complete
  305. (+) Handle DMA interrupt request
  306. (+) Register and Unregister DMA callbacks
  307. @endverbatim
  308. * @{
  309. */
  310. /**
  311. * @brief Start the DMA Transfer.
  312. * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
  313. * the configuration information for the specified DMA Channel.
  314. * @param SrcAddress The source memory Buffer address
  315. * @param DstAddress The destination memory Buffer address
  316. * @param DataLength The length of data to be transferred from source to destination
  317. * @retval HAL status
  318. */
  319. HAL_StatusTypeDef HAL_DMA_Start(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength)
  320. {
  321. HAL_StatusTypeDef status = HAL_OK;
  322. /* Check the parameters */
  323. assert_param(IS_DMA_BUFFER_SIZE(DataLength));
  324. /* Process locked */
  325. __HAL_LOCK(hdma);
  326. if (hdma->State == HAL_DMA_STATE_READY)
  327. {
  328. /* Change DMA peripheral state */
  329. hdma->State = HAL_DMA_STATE_BUSY;
  330. /* Initialize the error code */
  331. hdma->ErrorCode = HAL_DMA_ERROR_NONE;
  332. /* Disable the peripheral */
  333. __HAL_DMA_DISABLE(hdma);
  334. /* Configure the source, destination address and the data length & clear flags*/
  335. DMA_SetConfig(hdma, SrcAddress, DstAddress, DataLength);
  336. /* Enable the Peripheral */
  337. __HAL_DMA_ENABLE(hdma);
  338. }
  339. else
  340. {
  341. /* Change the error code */
  342. hdma->ErrorCode = HAL_DMA_ERROR_BUSY;
  343. /* Process Unlocked */
  344. __HAL_UNLOCK(hdma);
  345. /* Return error status */
  346. status = HAL_ERROR;
  347. }
  348. return status;
  349. }
  350. /**
  351. * @brief Start the DMA Transfer with interrupt enabled.
  352. * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
  353. * the configuration information for the specified DMA Channel.
  354. * @param SrcAddress The source memory Buffer address
  355. * @param DstAddress The destination memory Buffer address
  356. * @param DataLength The length of data to be transferred from source to destination
  357. * @retval HAL status
  358. */
  359. HAL_StatusTypeDef HAL_DMA_Start_IT(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress,
  360. uint32_t DataLength)
  361. {
  362. HAL_StatusTypeDef status = HAL_OK;
  363. /* Check the parameters */
  364. assert_param(IS_DMA_BUFFER_SIZE(DataLength));
  365. /* Process locked */
  366. __HAL_LOCK(hdma);
  367. if (hdma->State == HAL_DMA_STATE_READY)
  368. {
  369. /* Change DMA peripheral state */
  370. hdma->State = HAL_DMA_STATE_BUSY;
  371. hdma->ErrorCode = HAL_DMA_ERROR_NONE;
  372. /* Disable the peripheral */
  373. __HAL_DMA_DISABLE(hdma);
  374. /* Configure the source, destination address and the data length & clear flags*/
  375. DMA_SetConfig(hdma, SrcAddress, DstAddress, DataLength);
  376. /* Enable the transfer complete interrupt */
  377. /* Enable the transfer Error interrupt */
  378. if (NULL != hdma->XferHalfCpltCallback)
  379. {
  380. /* Enable the Half transfer complete interrupt as well */
  381. __HAL_DMA_ENABLE_IT(hdma, (DMA_IT_TC | DMA_IT_HT | DMA_IT_TE));
  382. }
  383. else
  384. {
  385. __HAL_DMA_DISABLE_IT(hdma, DMA_IT_HT);
  386. __HAL_DMA_ENABLE_IT(hdma, (DMA_IT_TC | DMA_IT_TE));
  387. }
  388. /* Check if DMAMUX Synchronization is enabled*/
  389. if ((hdma->DMAmuxChannel->CCR & DMAMUX_CxCR_SE) != 0U)
  390. {
  391. /* Enable DMAMUX sync overrun IT*/
  392. hdma->DMAmuxChannel->CCR |= DMAMUX_CxCR_SOIE;
  393. }
  394. if (hdma->DMAmuxRequestGen != 0U)
  395. {
  396. /* if using DMAMUX request generator, enable the DMAMUX request generator overrun IT*/
  397. /* enable the request gen overrun IT*/
  398. hdma->DMAmuxRequestGen->RGCR |= DMAMUX_RGxCR_OIE;
  399. }
  400. /* Enable the Peripheral */
  401. __HAL_DMA_ENABLE(hdma);
  402. }
  403. else
  404. {
  405. /* Change the error code */
  406. hdma->ErrorCode = HAL_DMA_ERROR_BUSY;
  407. /* Process Unlocked */
  408. __HAL_UNLOCK(hdma);
  409. /* Return error status */
  410. status = HAL_ERROR;
  411. }
  412. return status;
  413. }
  414. /**
  415. * @brief Abort the DMA Transfer.
  416. * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
  417. * the configuration information for the specified DMA Channel.
  418. * @retval HAL status
  419. */
  420. HAL_StatusTypeDef HAL_DMA_Abort(DMA_HandleTypeDef *hdma)
  421. {
  422. /* Check the DMA peripheral handle */
  423. if (NULL == hdma)
  424. {
  425. return HAL_ERROR;
  426. }
  427. /* Check the DMA peripheral state */
  428. if (hdma->State != HAL_DMA_STATE_BUSY)
  429. {
  430. hdma->ErrorCode = HAL_DMA_ERROR_NO_XFER;
  431. /* Process Unlocked */
  432. __HAL_UNLOCK(hdma);
  433. return HAL_ERROR;
  434. }
  435. else
  436. {
  437. /* Disable DMA IT */
  438. __HAL_DMA_DISABLE_IT(hdma, (DMA_IT_TC | DMA_IT_HT | DMA_IT_TE));
  439. /* disable the DMAMUX sync overrun IT*/
  440. hdma->DMAmuxChannel->CCR &= ~DMAMUX_CxCR_SOIE;
  441. /* Disable the channel */
  442. __HAL_DMA_DISABLE(hdma);
  443. /* Clear all flags */
  444. #if defined(DMA2)
  445. hdma->DmaBaseAddress->IFCR = (DMA_ISR_GIF1 << (hdma->ChannelIndex & 0x1CU));
  446. #else
  447. __HAL_DMA_CLEAR_FLAG(hdma, ((DMA_FLAG_GI1) << (hdma->ChannelIndex & 0x1CU)));
  448. #endif /* DMA2 */
  449. /* Clear the DMAMUX synchro overrun flag */
  450. hdma->DMAmuxChannelStatus->CFR = hdma->DMAmuxChannelStatusMask;
  451. if (hdma->DMAmuxRequestGen != 0U)
  452. {
  453. /* if using DMAMUX request generator, disable the DMAMUX request generator overrun IT*/
  454. /* disable the request gen overrun IT*/
  455. hdma->DMAmuxRequestGen->RGCR &= ~DMAMUX_RGxCR_OIE;
  456. /* Clear the DMAMUX request generator overrun flag */
  457. hdma->DMAmuxRequestGenStatus->RGCFR = hdma->DMAmuxRequestGenStatusMask;
  458. }
  459. /* Change the DMA state */
  460. hdma->State = HAL_DMA_STATE_READY;
  461. /* Process Unlocked */
  462. __HAL_UNLOCK(hdma);
  463. }
  464. return HAL_OK;
  465. }
  466. /**
  467. * @brief Aborts the DMA Transfer in Interrupt mode.
  468. * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
  469. * the configuration information for the specified DMA Channel.
  470. * @retval HAL status
  471. */
  472. HAL_StatusTypeDef HAL_DMA_Abort_IT(DMA_HandleTypeDef *hdma)
  473. {
  474. HAL_StatusTypeDef status = HAL_OK;
  475. if (hdma->State != HAL_DMA_STATE_BUSY)
  476. {
  477. /* no transfer ongoing */
  478. hdma->ErrorCode = HAL_DMA_ERROR_NO_XFER;
  479. status = HAL_ERROR;
  480. }
  481. else
  482. {
  483. /* Disable DMA IT */
  484. __HAL_DMA_DISABLE_IT(hdma, (DMA_IT_TC | DMA_IT_HT | DMA_IT_TE));
  485. /* Disable the channel */
  486. __HAL_DMA_DISABLE(hdma);
  487. /* disable the DMAMUX sync overrun IT*/
  488. hdma->DMAmuxChannel->CCR &= ~DMAMUX_CxCR_SOIE;
  489. /* Clear all flags */
  490. #if defined(DMA2)
  491. hdma->DmaBaseAddress->IFCR = (DMA_ISR_GIF1 << (hdma->ChannelIndex & 0x1CU));
  492. #else
  493. __HAL_DMA_CLEAR_FLAG(hdma, ((DMA_FLAG_GI1) << (hdma->ChannelIndex & 0x1CU)));
  494. #endif /* DMA2 */
  495. /* Clear the DMAMUX synchro overrun flag */
  496. hdma->DMAmuxChannelStatus->CFR = hdma->DMAmuxChannelStatusMask;
  497. if (hdma->DMAmuxRequestGen != 0U)
  498. {
  499. /* if using DMAMUX request generator, disable the DMAMUX request generator overrun IT*/
  500. /* disable the request gen overrun IT*/
  501. hdma->DMAmuxRequestGen->RGCR &= ~DMAMUX_RGxCR_OIE;
  502. /* Clear the DMAMUX request generator overrun flag */
  503. hdma->DMAmuxRequestGenStatus->RGCFR = hdma->DMAmuxRequestGenStatusMask;
  504. }
  505. /* Change the DMA state */
  506. hdma->State = HAL_DMA_STATE_READY;
  507. /* Process Unlocked */
  508. __HAL_UNLOCK(hdma);
  509. /* Call User Abort callback */
  510. if (hdma->XferAbortCallback != NULL)
  511. {
  512. hdma->XferAbortCallback(hdma);
  513. }
  514. }
  515. return status;
  516. }
  517. /**
  518. * @brief Polling for transfer complete.
  519. * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
  520. * the configuration information for the specified DMA Channel.
  521. * @param CompleteLevel Specifies the DMA level complete.
  522. * @param Timeout Timeout duration.
  523. * @retval HAL status
  524. */
  525. HAL_StatusTypeDef HAL_DMA_PollForTransfer(DMA_HandleTypeDef *hdma, HAL_DMA_LevelCompleteTypeDef CompleteLevel,
  526. uint32_t Timeout)
  527. {
  528. uint32_t temp;
  529. uint32_t tickstart;
  530. if (hdma->State != HAL_DMA_STATE_BUSY)
  531. {
  532. /* no transfer ongoing */
  533. hdma->ErrorCode = HAL_DMA_ERROR_NO_XFER;
  534. __HAL_UNLOCK(hdma);
  535. return HAL_ERROR;
  536. }
  537. /* Polling mode not supported in circular mode */
  538. if ((hdma->Instance->CCR & DMA_CCR_CIRC) != 0U)
  539. {
  540. hdma->ErrorCode = HAL_DMA_ERROR_NOT_SUPPORTED;
  541. return HAL_ERROR;
  542. }
  543. /* Get the level transfer complete flag */
  544. if (HAL_DMA_FULL_TRANSFER == CompleteLevel)
  545. {
  546. /* Transfer Complete flag */
  547. temp = DMA_FLAG_TC1 << (hdma->ChannelIndex & 0x1CU);
  548. }
  549. else
  550. {
  551. /* Half Transfer Complete flag */
  552. temp = DMA_FLAG_HT1 << (hdma->ChannelIndex & 0x1CU);
  553. }
  554. /* Get tick */
  555. tickstart = HAL_GetTick();
  556. #if defined(DMA2)
  557. while ((hdma->DmaBaseAddress->ISR & temp) == 0U)
  558. {
  559. if ((hdma->DmaBaseAddress->ISR & (DMA_FLAG_TE1 << (hdma->ChannelIndex & 0x1CU))) != 0U)
  560. {
  561. /* When a DMA transfer error occurs */
  562. /* A hardware clear of its EN bits is performed */
  563. /* Clear all flags */
  564. hdma->DmaBaseAddress->IFCR = (DMA_ISR_GIF1 << (hdma->ChannelIndex & 0x1CU));
  565. /* Update error code */
  566. hdma->ErrorCode = HAL_DMA_ERROR_TE;
  567. /* Change the DMA state */
  568. hdma->State = HAL_DMA_STATE_READY;
  569. /* Process Unlocked */
  570. __HAL_UNLOCK(hdma);
  571. return HAL_ERROR;
  572. }
  573. #else
  574. while (0U == __HAL_DMA_GET_FLAG(hdma, temp))
  575. {
  576. if (0U != __HAL_DMA_GET_FLAG(hdma, (DMA_FLAG_TE1 << (hdma->ChannelIndex & 0x1CU))))
  577. {
  578. /* When a DMA transfer error occurs */
  579. /* A hardware clear of its EN bits is performed */
  580. /* Clear all flags */
  581. __HAL_DMA_CLEAR_FLAG(hdma, ((DMA_FLAG_GI1) << (hdma->ChannelIndex & 0x1CU)));
  582. /* Update error code */
  583. hdma->ErrorCode = HAL_DMA_ERROR_TE;
  584. /* Change the DMA state */
  585. hdma->State = HAL_DMA_STATE_READY;
  586. /* Process Unlocked */
  587. __HAL_UNLOCK(hdma);
  588. return HAL_ERROR;
  589. }
  590. #endif /* DMA2 */
  591. /* Check for the Timeout */
  592. if (Timeout != HAL_MAX_DELAY)
  593. {
  594. if (((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  595. {
  596. /* Update error code */
  597. hdma->ErrorCode = HAL_DMA_ERROR_TIMEOUT;
  598. /* Change the DMA state */
  599. hdma->State = HAL_DMA_STATE_READY;
  600. /* Process Unlocked */
  601. __HAL_UNLOCK(hdma);
  602. return HAL_ERROR;
  603. }
  604. }
  605. }
  606. /*Check for DMAMUX Request generator (if used) overrun status */
  607. if (hdma->DMAmuxRequestGen != 0U)
  608. {
  609. /* if using DMAMUX request generator Check for DMAMUX request generator overrun */
  610. if ((hdma->DMAmuxRequestGenStatus->RGSR & hdma->DMAmuxRequestGenStatusMask) != 0U)
  611. {
  612. /* Disable the request gen overrun interrupt */
  613. hdma->DMAmuxRequestGen->RGCR |= DMAMUX_RGxCR_OIE;
  614. /* Clear the DMAMUX request generator overrun flag */
  615. hdma->DMAmuxRequestGenStatus->RGCFR = hdma->DMAmuxRequestGenStatusMask;
  616. /* Update error code */
  617. hdma->ErrorCode |= HAL_DMA_ERROR_REQGEN;
  618. }
  619. }
  620. /* Check for DMAMUX Synchronization overrun */
  621. if ((hdma->DMAmuxChannelStatus->CSR & hdma->DMAmuxChannelStatusMask) != 0U)
  622. {
  623. /* Clear the DMAMUX synchro overrun flag */
  624. hdma->DMAmuxChannelStatus->CFR = hdma->DMAmuxChannelStatusMask;
  625. /* Update error code */
  626. hdma->ErrorCode |= HAL_DMA_ERROR_SYNC;
  627. }
  628. if (HAL_DMA_FULL_TRANSFER == CompleteLevel)
  629. {
  630. /* Clear the transfer complete flag */
  631. #if defined(DMA2)
  632. hdma->DmaBaseAddress->IFCR = (DMA_FLAG_TC1 << (hdma->ChannelIndex & 0x1CU));
  633. #else
  634. __HAL_DMA_CLEAR_FLAG(hdma, (DMA_FLAG_TC1 << (hdma->ChannelIndex & 0x1CU)));
  635. #endif /* DMA2 */
  636. /* Process unlocked */
  637. __HAL_UNLOCK(hdma);
  638. /* The selected Channelx EN bit is cleared (DMA is disabled and
  639. all transfers are complete) */
  640. hdma->State = HAL_DMA_STATE_READY;
  641. }
  642. else
  643. {
  644. /* Clear the half transfer complete flag */
  645. #if defined(DMA2)
  646. hdma->DmaBaseAddress->IFCR = (DMA_FLAG_HT1 << (hdma->ChannelIndex & 0x1CU));
  647. #else
  648. __HAL_DMA_CLEAR_FLAG(hdma, (DMA_FLAG_HT1 << (hdma->ChannelIndex & 0x1CU)));
  649. #endif /* DMA2 */
  650. }
  651. return HAL_OK;
  652. }
  653. /**
  654. * @brief Handle DMA interrupt request.
  655. * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
  656. * the configuration information for the specified DMA Channel.
  657. * @retval None
  658. */
  659. void HAL_DMA_IRQHandler(DMA_HandleTypeDef *hdma)
  660. {
  661. #if defined(DMA2)
  662. uint32_t flag_it = hdma->DmaBaseAddress->ISR;
  663. #else
  664. uint32_t flag_it = DMA1->ISR;
  665. #endif /* DMA2 */
  666. uint32_t source_it = hdma->Instance->CCR;
  667. /* Half Transfer Complete Interrupt management ******************************/
  668. if (((flag_it & (DMA_FLAG_HT1 << (hdma->ChannelIndex & 0x1CU))) != 0U) && ((source_it & DMA_IT_HT) != 0U))
  669. {
  670. /* Disable the half transfer interrupt if the DMA mode is not CIRCULAR */
  671. if ((hdma->Instance->CCR & DMA_CCR_CIRC) == 0U)
  672. {
  673. /* Disable the half transfer interrupt */
  674. __HAL_DMA_DISABLE_IT(hdma, DMA_IT_HT);
  675. }
  676. /* Clear the half transfer complete flag */
  677. #if defined(DMA2)
  678. hdma->DmaBaseAddress->IFCR = DMA_ISR_HTIF1 << (hdma->ChannelIndex & 0x1CU);
  679. #else
  680. __HAL_DMA_CLEAR_FLAG(hdma, (DMA_FLAG_HT1 << (hdma->ChannelIndex & 0x1CU)));
  681. #endif /* DMA2 */
  682. /* DMA peripheral state is not updated in Half Transfer */
  683. /* but in Transfer Complete case */
  684. if (hdma->XferHalfCpltCallback != NULL)
  685. {
  686. /* Half transfer callback */
  687. hdma->XferHalfCpltCallback(hdma);
  688. }
  689. }
  690. /* Transfer Complete Interrupt management ***********************************/
  691. else if ((0U != (flag_it & (DMA_FLAG_TC1 << (hdma->ChannelIndex & 0x1CU)))) && (0U != (source_it & DMA_IT_TC)))
  692. {
  693. if ((hdma->Instance->CCR & DMA_CCR_CIRC) == 0U)
  694. {
  695. /* Disable the transfer complete and error interrupt */
  696. __HAL_DMA_DISABLE_IT(hdma, DMA_IT_TE | DMA_IT_TC);
  697. /* Change the DMA state */
  698. hdma->State = HAL_DMA_STATE_READY;
  699. }
  700. /* Clear the transfer complete flag */
  701. __HAL_DMA_CLEAR_FLAG(hdma, (DMA_FLAG_TC1 << (hdma->ChannelIndex & 0x1CU)));
  702. /* Process Unlocked */
  703. __HAL_UNLOCK(hdma);
  704. if (hdma->XferCpltCallback != NULL)
  705. {
  706. /* Transfer complete callback */
  707. hdma->XferCpltCallback(hdma);
  708. }
  709. }
  710. /* Transfer Error Interrupt management **************************************/
  711. else if (((flag_it & (DMA_FLAG_TE1 << (hdma->ChannelIndex & 0x1CU))) != 0U) && ((source_it & DMA_IT_TE) != 0U))
  712. {
  713. /* When a DMA transfer error occurs */
  714. /* A hardware clear of its EN bits is performed */
  715. /* Disable ALL DMA IT */
  716. __HAL_DMA_DISABLE_IT(hdma, (DMA_IT_TC | DMA_IT_HT | DMA_IT_TE));
  717. /* Clear all flags */
  718. #if defined(DMA2)
  719. hdma->DmaBaseAddress->IFCR = (DMA_ISR_GIF1 << (hdma->ChannelIndex & 0x1CU));
  720. #else
  721. __HAL_DMA_CLEAR_FLAG(hdma, (DMA_FLAG_GI1 << (hdma->ChannelIndex & 0x1CU)));
  722. #endif /* DMA2 */
  723. /* Update error code */
  724. hdma->ErrorCode = HAL_DMA_ERROR_TE;
  725. /* Change the DMA state */
  726. hdma->State = HAL_DMA_STATE_READY;
  727. /* Process Unlocked */
  728. __HAL_UNLOCK(hdma);
  729. if (hdma->XferErrorCallback != NULL)
  730. {
  731. /* Transfer error callback */
  732. hdma->XferErrorCallback(hdma);
  733. }
  734. }
  735. else
  736. {
  737. /* Nothing To Do */
  738. }
  739. return;
  740. }
  741. /**
  742. * @brief Register callbacks
  743. * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
  744. * the configuration information for the specified DMA Channel.
  745. * @param CallbackID User Callback identifier
  746. * a HAL_DMA_CallbackIDTypeDef ENUM as parameter.
  747. * @param pCallback Pointer to private callback function which has pointer to
  748. * a DMA_HandleTypeDef structure as parameter.
  749. * @retval HAL status
  750. */
  751. HAL_StatusTypeDef HAL_DMA_RegisterCallback(DMA_HandleTypeDef *hdma, HAL_DMA_CallbackIDTypeDef CallbackID, void (* pCallback)(DMA_HandleTypeDef *_hdma))
  752. {
  753. HAL_StatusTypeDef status = HAL_OK;
  754. /* Process locked */
  755. __HAL_LOCK(hdma);
  756. if (hdma->State == HAL_DMA_STATE_READY)
  757. {
  758. switch (CallbackID)
  759. {
  760. case HAL_DMA_XFER_CPLT_CB_ID:
  761. hdma->XferCpltCallback = pCallback;
  762. break;
  763. case HAL_DMA_XFER_HALFCPLT_CB_ID:
  764. hdma->XferHalfCpltCallback = pCallback;
  765. break;
  766. case HAL_DMA_XFER_ERROR_CB_ID:
  767. hdma->XferErrorCallback = pCallback;
  768. break;
  769. case HAL_DMA_XFER_ABORT_CB_ID:
  770. hdma->XferAbortCallback = pCallback;
  771. break;
  772. default:
  773. status = HAL_ERROR;
  774. break;
  775. }
  776. }
  777. else
  778. {
  779. status = HAL_ERROR;
  780. }
  781. /* Release Lock */
  782. __HAL_UNLOCK(hdma);
  783. return status;
  784. }
  785. /**
  786. * @brief UnRegister callbacks
  787. * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
  788. * the configuration information for the specified DMA Channel.
  789. * @param CallbackID User Callback identifier
  790. * a HAL_DMA_CallbackIDTypeDef ENUM as parameter.
  791. * @retval HAL status
  792. */
  793. HAL_StatusTypeDef HAL_DMA_UnRegisterCallback(DMA_HandleTypeDef *hdma, HAL_DMA_CallbackIDTypeDef CallbackID)
  794. {
  795. HAL_StatusTypeDef status = HAL_OK;
  796. /* Process locked */
  797. __HAL_LOCK(hdma);
  798. if (hdma->State == HAL_DMA_STATE_READY)
  799. {
  800. switch (CallbackID)
  801. {
  802. case HAL_DMA_XFER_CPLT_CB_ID:
  803. hdma->XferCpltCallback = NULL;
  804. break;
  805. case HAL_DMA_XFER_HALFCPLT_CB_ID:
  806. hdma->XferHalfCpltCallback = NULL;
  807. break;
  808. case HAL_DMA_XFER_ERROR_CB_ID:
  809. hdma->XferErrorCallback = NULL;
  810. break;
  811. case HAL_DMA_XFER_ABORT_CB_ID:
  812. hdma->XferAbortCallback = NULL;
  813. break;
  814. case HAL_DMA_XFER_ALL_CB_ID:
  815. hdma->XferCpltCallback = NULL;
  816. hdma->XferHalfCpltCallback = NULL;
  817. hdma->XferErrorCallback = NULL;
  818. hdma->XferAbortCallback = NULL;
  819. break;
  820. default:
  821. status = HAL_ERROR;
  822. break;
  823. }
  824. }
  825. else
  826. {
  827. status = HAL_ERROR;
  828. }
  829. /* Release Lock */
  830. __HAL_UNLOCK(hdma);
  831. return status;
  832. }
  833. /**
  834. * @}
  835. */
  836. /** @defgroup DMA_Exported_Functions_Group3 Peripheral State and Errors functions
  837. * @brief Peripheral State and Errors functions
  838. *
  839. @verbatim
  840. ===============================================================================
  841. ##### Peripheral State and Errors functions #####
  842. ===============================================================================
  843. [..]
  844. This subsection provides functions allowing to
  845. (+) Check the DMA state
  846. (+) Get error code
  847. @endverbatim
  848. * @{
  849. */
  850. /**
  851. * @brief Return the DMA handle state.
  852. * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
  853. * the configuration information for the specified DMA Channel.
  854. * @retval HAL state
  855. */
  856. HAL_DMA_StateTypeDef HAL_DMA_GetState(DMA_HandleTypeDef *hdma)
  857. {
  858. /* Return DMA handle state */
  859. return hdma->State;
  860. }
  861. /**
  862. * @brief Return the DMA error code.
  863. * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
  864. * the configuration information for the specified DMA Channel.
  865. * @retval DMA Error Code
  866. */
  867. uint32_t HAL_DMA_GetError(DMA_HandleTypeDef *hdma)
  868. {
  869. /* Return the DMA error code */
  870. return hdma->ErrorCode;
  871. }
  872. /**
  873. * @}
  874. */
  875. /**
  876. * @}
  877. */
  878. /** @addtogroup DMA_Private_Functions
  879. * @{
  880. */
  881. /**
  882. * @brief Sets the DMA Transfer parameter.
  883. * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
  884. * the configuration information for the specified DMA Channel.
  885. * @param SrcAddress The source memory Buffer address
  886. * @param DstAddress The destination memory Buffer address
  887. * @param DataLength The length of data to be transferred from source to destination
  888. * @retval HAL status
  889. */
  890. static void DMA_SetConfig(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength)
  891. {
  892. /* Clear the DMAMUX synchro overrun flag */
  893. hdma->DMAmuxChannelStatus->CFR = hdma->DMAmuxChannelStatusMask;
  894. if (hdma->DMAmuxRequestGen != 0U)
  895. {
  896. /* Clear the DMAMUX request generator overrun flag */
  897. hdma->DMAmuxRequestGenStatus->RGCFR = hdma->DMAmuxRequestGenStatusMask;
  898. }
  899. /* Clear all flags */
  900. #if defined(DMA2)
  901. hdma->DmaBaseAddress->IFCR = (DMA_ISR_GIF1 << (hdma->ChannelIndex & 0x1CU));
  902. #else
  903. __HAL_DMA_CLEAR_FLAG(hdma, (DMA_FLAG_GI1 << (hdma->ChannelIndex & 0x1CU)));
  904. #endif /* DMA2 */
  905. /* Configure DMA Channel data length */
  906. hdma->Instance->CNDTR = DataLength;
  907. /* Memory to Peripheral */
  908. if ((hdma->Init.Direction) == DMA_MEMORY_TO_PERIPH)
  909. {
  910. /* Configure DMA Channel destination address */
  911. hdma->Instance->CPAR = DstAddress;
  912. /* Configure DMA Channel source address */
  913. hdma->Instance->CMAR = SrcAddress;
  914. }
  915. /* Peripheral to Memory */
  916. else
  917. {
  918. /* Configure DMA Channel source address */
  919. hdma->Instance->CPAR = SrcAddress;
  920. /* Configure DMA Channel destination address */
  921. hdma->Instance->CMAR = DstAddress;
  922. }
  923. }
  924. /**
  925. * @brief Updates the DMA handle with the DMAMUX channel and status mask depending on channel number
  926. * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
  927. * the configuration information for the specified DMA Channel.
  928. * @retval None
  929. */
  930. static void DMA_CalcDMAMUXChannelBaseAndMask(DMA_HandleTypeDef *hdma)
  931. {
  932. uint32_t channel_number;
  933. #if defined(DMA2)
  934. /* check if instance is not outside the DMA channel range */
  935. if ((uint32_t)hdma->Instance < (uint32_t)DMA2_Channel1)
  936. {
  937. /* DMA1 */
  938. /* Associate a DMA Channel to a DMAMUX channel */
  939. hdma->DMAmuxChannel = (DMAMUX1_Channel0 + (hdma->ChannelIndex >> 2U));
  940. /* Prepare channel_number used for DMAmuxChannelStatusMask computation */
  941. channel_number = (((uint32_t)hdma->Instance & 0xFFU) - 8U) / 20U;
  942. }
  943. else
  944. {
  945. /* DMA2 */
  946. /* Associate a DMA Channel to a DMAMUX channel */
  947. hdma->DMAmuxChannel = (DMAMUX1_Channel7 + (hdma->ChannelIndex >> 2U));
  948. /* Prepare channel_number used for DMAmuxChannelStatusMask computation */
  949. channel_number = (((((uint32_t)hdma->Instance & 0xFFU) - 8U) / 20U) + 7U);
  950. }
  951. #else
  952. /* Associate a DMA Channel to a DMAMUX channel */
  953. hdma->DMAmuxChannel = (DMAMUX_Channel_TypeDef *)(uint32_t)((uint32_t)DMAMUX1_Channel0 + ((hdma->ChannelIndex >> 2U) * ((uint32_t)DMAMUX1_Channel1 - (uint32_t)DMAMUX1_Channel0)));
  954. /* Prepare channel_number used for DMAmuxChannelStatusMask computation */
  955. channel_number = (((uint32_t)hdma->Instance & 0xFFU) - 8U) / 20U;
  956. #endif /* DMA2 */
  957. /* Initialize the field DMAmuxChannelStatus to DMAMUX1_ChannelStatus base */
  958. hdma->DMAmuxChannelStatus = DMAMUX1_ChannelStatus;
  959. /* Initialize the field DMAmuxChannelStatusMask with the corresponding index of the DMAMUX channel selected for the current ChannelIndex */
  960. hdma->DMAmuxChannelStatusMask = 1UL << (channel_number & 0x1FU);
  961. }
  962. /**
  963. * @brief Updates the DMA handle with the DMAMUX request generator params
  964. * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
  965. * the configuration information for the specified DMA Channel.
  966. * @retval None
  967. */
  968. static void DMA_CalcDMAMUXRequestGenBaseAndMask(DMA_HandleTypeDef *hdma)
  969. {
  970. uint32_t request = hdma->Init.Request & DMAMUX_CxCR_DMAREQ_ID;
  971. /* DMA Channels are connected to DMAMUX1 request generator blocks*/
  972. hdma->DMAmuxRequestGen = (DMAMUX_RequestGen_TypeDef *)((uint32_t)(((uint32_t)DMAMUX1_RequestGenerator0) + ((request - 1U) * 4U)));
  973. hdma->DMAmuxRequestGenStatus = DMAMUX1_RequestGenStatus;
  974. /* here "Request" is either DMA_REQUEST_GENERATOR0 to DMA_REQUEST_GENERATOR3, i.e. <= 4*/
  975. hdma->DMAmuxRequestGenStatusMask = 1UL << ((request - 1U) & 0x3U);
  976. }
  977. /**
  978. * @}
  979. */
  980. /**
  981. * @}
  982. */
  983. #endif /* HAL_DMA_MODULE_ENABLED */
  984. /**
  985. * @}
  986. */
  987. /**
  988. * @}
  989. */