stm32l4xx_hal_dma.c 38 KB

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