stm32l5xx_hal_dma.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371
  1. /**
  2. ******************************************************************************
  3. * @file stm32l5xx_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) 2019 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. (##) DMAMUX1: __HAL_RCC_DMAMUX1_CLK_ENABLE();
  40. (#) Use HAL_DMA_GetState() function to return the DMA state and HAL_DMA_GetError() in case of error
  41. detection.
  42. (#) Use HAL_DMA_Abort() function to abort the current transfer
  43. -@- In Memory-to-Memory transfer mode, Circular mode is not allowed.
  44. *** Polling mode IO operation ***
  45. =================================
  46. [..]
  47. (+) Use HAL_DMA_Start() to start DMA transfer after the configuration of Source
  48. address and destination address and the Length of data to be transferred
  49. (+) Use HAL_DMA_PollForTransfer() to poll for the end of current transfer, in this
  50. case a fixed Timeout can be configured by User depending from his application.
  51. *** Interrupt mode IO operation ***
  52. ===================================
  53. [..]
  54. (+) Configure the DMA interrupt priority using HAL_NVIC_SetPriority()
  55. (+) Enable the DMA IRQ handler using HAL_NVIC_EnableIRQ()
  56. (+) Use HAL_DMA_Start_IT() to start DMA transfer after the configuration of
  57. Source address and destination address and the Length of data to be transferred.
  58. In this case the DMA interrupt is configured
  59. (+) Use HAL_DMA_IRQHandler() called under DMA_IRQHandler() Interrupt subroutine
  60. (+) At the end of data transfer HAL_DMA_IRQHandler() function is executed and user can
  61. add his own function to register callbacks with HAL_DMA_RegisterCallback().
  62. *** DMA HAL driver macros list ***
  63. =============================================
  64. [..]
  65. Below the list of macros in DMA HAL driver.
  66. (+) __HAL_DMA_ENABLE: Enable the specified DMA Channel.
  67. (+) __HAL_DMA_DISABLE: Disable the specified DMA Channel.
  68. (+) __HAL_DMA_GET_FLAG: Get the DMA Channel pending flags.
  69. (+) __HAL_DMA_CLEAR_FLAG: Clear the DMA Channel pending flags.
  70. (+) __HAL_DMA_ENABLE_IT: Enable the specified DMA Channel interrupts.
  71. (+) __HAL_DMA_DISABLE_IT: Disable the specified DMA Channel interrupts.
  72. (+) __HAL_DMA_GET_IT_SOURCE: Check whether the specified DMA Channel interrupt is enabled or not.
  73. [..]
  74. (@) You can refer to the DMA HAL driver header file for more useful macros
  75. @endverbatim
  76. ******************************************************************************
  77. */
  78. /* Includes ------------------------------------------------------------------*/
  79. #include "stm32l5xx_hal.h"
  80. /** @addtogroup STM32L5xx_HAL_Driver
  81. * @{
  82. */
  83. /** @defgroup DMA DMA
  84. * @brief DMA HAL module driver
  85. * @{
  86. */
  87. #ifdef HAL_DMA_MODULE_ENABLED
  88. /* Private typedef -----------------------------------------------------------*/
  89. /* Private define ------------------------------------------------------------*/
  90. /* Private macro -------------------------------------------------------------*/
  91. /* Private variables ---------------------------------------------------------*/
  92. /* Private function prototypes -----------------------------------------------*/
  93. /** @defgroup DMA_Private_Functions DMA Private Functions
  94. * @{
  95. */
  96. static void DMA_SetConfig(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength);
  97. static void DMA_CalcDMAMUXChannelBaseAndMask(DMA_HandleTypeDef *hdma);
  98. static void DMA_CalcDMAMUXRequestGenBaseAndMask(DMA_HandleTypeDef *hdma);
  99. /**
  100. * @}
  101. */
  102. /* Exported functions ---------------------------------------------------------*/
  103. /** @defgroup DMA_Exported_Functions DMA Exported Functions
  104. * @{
  105. */
  106. /** @defgroup DMA_Exported_Functions_Group1 Initialization and de-initialization functions
  107. * @brief Initialization and de-initialization functions
  108. *
  109. @verbatim
  110. ===============================================================================
  111. ##### Initialization and de-initialization functions #####
  112. ===============================================================================
  113. [..]
  114. This section provides functions allowing to initialize the DMA Channel source
  115. and destination addresses, incrementation and data sizes, transfer direction,
  116. circular/normal mode selection, memory-to-memory mode selection and Channel priority value.
  117. [..]
  118. The HAL_DMA_Init() function follows the DMA configuration procedures as described in
  119. reference manual.
  120. @endverbatim
  121. * @{
  122. */
  123. /**
  124. * @brief Initialize the DMA according to the specified
  125. * parameters in the DMA_InitTypeDef and initialize the associated handle.
  126. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  127. * the configuration information for the specified DMA Channel.
  128. * @retval HAL status
  129. */
  130. HAL_StatusTypeDef HAL_DMA_Init(DMA_HandleTypeDef *hdma)
  131. {
  132. uint32_t tmp;
  133. /* Check the DMA handle allocation */
  134. if(hdma == NULL)
  135. {
  136. return HAL_ERROR;
  137. }
  138. /* Check the parameters */
  139. assert_param(IS_DMA_ALL_INSTANCE(hdma->Instance));
  140. assert_param(IS_DMA_DIRECTION(hdma->Init.Direction));
  141. assert_param(IS_DMA_PERIPHERAL_INC_STATE(hdma->Init.PeriphInc));
  142. assert_param(IS_DMA_MEMORY_INC_STATE(hdma->Init.MemInc));
  143. assert_param(IS_DMA_PERIPHERAL_DATA_SIZE(hdma->Init.PeriphDataAlignment));
  144. assert_param(IS_DMA_MEMORY_DATA_SIZE(hdma->Init.MemDataAlignment));
  145. assert_param(IS_DMA_MODE(hdma->Init.Mode));
  146. assert_param(IS_DMA_PRIORITY(hdma->Init.Priority));
  147. assert_param(IS_DMA_ALL_REQUEST(hdma->Init.Request));
  148. /* Compute the channel index */
  149. if ((uint32_t)(hdma->Instance) < (uint32_t)(DMA2_Channel1))
  150. {
  151. /* DMA1 */
  152. hdma->ChannelIndex = (((uint32_t)hdma->Instance - (uint32_t)DMA1_Channel1) / ((uint32_t)DMA1_Channel2 - (uint32_t)DMA1_Channel1)) << 2U;
  153. hdma->DmaBaseAddress = DMA1;
  154. }
  155. else
  156. {
  157. /* DMA2 */
  158. hdma->ChannelIndex = (((uint32_t)hdma->Instance - (uint32_t)DMA2_Channel1) / ((uint32_t)DMA2_Channel2 - (uint32_t)DMA2_Channel1)) << 2U;
  159. hdma->DmaBaseAddress = DMA2;
  160. }
  161. /* Change DMA peripheral state */
  162. hdma->State = HAL_DMA_STATE_BUSY;
  163. /* Get the CR register value */
  164. tmp = hdma->Instance->CCR;
  165. /* Clear PL, MSIZE, PSIZE, MINC, PINC, CIRC, DIR, MEM2MEM, CT and DBM bits */
  166. tmp &= ((uint32_t)~(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 | DMA_CCR_CT |
  169. DMA_CCR_DBM));
  170. /* Prepare the DMA Channel configuration */
  171. tmp |= hdma->Init.Direction |
  172. hdma->Init.PeriphInc | hdma->Init.MemInc |
  173. hdma->Init.PeriphDataAlignment | hdma->Init.MemDataAlignment |
  174. hdma->Init.Mode | hdma->Init.Priority;
  175. /* Write to DMA Channel CR register */
  176. hdma->Instance->CCR = tmp;
  177. /* Initialize parameters for DMAMUX channel :
  178. DMAmuxChannel, DMAmuxChannelStatus and DMAmuxChannelStatusMask
  179. */
  180. DMA_CalcDMAMUXChannelBaseAndMask(hdma);
  181. if(hdma->Init.Direction == DMA_MEMORY_TO_MEMORY)
  182. {
  183. /* if memory to memory force the request to 0*/
  184. hdma->Init.Request = DMA_REQUEST_MEM2MEM;
  185. }
  186. /* Set peripheral request to DMAMUX channel */
  187. hdma->DMAmuxChannel->CCR = (hdma->Init.Request & DMAMUX_CxCR_DMAREQ_ID);
  188. /* Clear the DMAMUX synchro overrun flag */
  189. hdma->DMAmuxChannelStatus->CFR = hdma->DMAmuxChannelStatusMask;
  190. if(((hdma->Init.Request > 0U) && (hdma->Init.Request <= DMA_REQUEST_GENERATOR3)))
  191. {
  192. /* Initialize parameters for DMAMUX request generator :
  193. DMAmuxRequestGen, DMAmuxRequestGenStatus and DMAmuxRequestGenStatusMask
  194. */
  195. DMA_CalcDMAMUXRequestGenBaseAndMask(hdma);
  196. /* Reset the DMAMUX request generator register*/
  197. hdma->DMAmuxRequestGen->RGCR = 0U;
  198. /* Clear the DMAMUX request generator overrun flag */
  199. hdma->DMAmuxRequestGenStatus->RGCFR = hdma->DMAmuxRequestGenStatusMask;
  200. }
  201. else
  202. {
  203. hdma->DMAmuxRequestGen = 0U;
  204. hdma->DMAmuxRequestGenStatus = 0U;
  205. hdma->DMAmuxRequestGenStatusMask = 0U;
  206. }
  207. /* Initialise the error code */
  208. hdma->ErrorCode = HAL_DMA_ERROR_NONE;
  209. /* Initialize the DMA state*/
  210. hdma->State = HAL_DMA_STATE_READY;
  211. /* Allocate lock resource and initialize it */
  212. hdma->Lock = HAL_UNLOCKED;
  213. return HAL_OK;
  214. }
  215. /**
  216. * @brief DeInitialize the DMA peripheral.
  217. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  218. * the configuration information for the specified DMA Channel.
  219. * @retval HAL status
  220. */
  221. HAL_StatusTypeDef HAL_DMA_DeInit(DMA_HandleTypeDef *hdma)
  222. {
  223. /* Check the DMA handle allocation */
  224. if (NULL == hdma )
  225. {
  226. return HAL_ERROR;
  227. }
  228. /* Check the parameters */
  229. assert_param(IS_DMA_ALL_INSTANCE(hdma->Instance));
  230. /* Disable the selected DMA Channelx */
  231. __HAL_DMA_DISABLE(hdma);
  232. /* Compute the channel index */
  233. if ((uint32_t)(hdma->Instance) < (uint32_t)(DMA2_Channel1))
  234. {
  235. /* DMA1 */
  236. hdma->ChannelIndex = (((uint32_t)hdma->Instance - (uint32_t)DMA1_Channel1) / ((uint32_t)DMA1_Channel2 - (uint32_t)DMA1_Channel1)) << 2U;
  237. hdma->DmaBaseAddress = DMA1;
  238. }
  239. else
  240. {
  241. /* DMA2 */
  242. hdma->ChannelIndex = (((uint32_t)hdma->Instance - (uint32_t)DMA2_Channel1) / ((uint32_t)DMA2_Channel2 - (uint32_t)DMA2_Channel1)) << 2U;
  243. hdma->DmaBaseAddress = DMA2;
  244. }
  245. /* Reset DMA Channel control register */
  246. hdma->Instance->CCR = 0U;
  247. /* Clear all flags */
  248. hdma->DmaBaseAddress->IFCR = (DMA_ISR_GIF1 << (hdma->ChannelIndex & 0x1CU));
  249. /* Initialize parameters for DMAMUX channel :
  250. DMAmuxChannel, DMAmuxChannelStatus and DMAmuxChannelStatusMask */
  251. DMA_CalcDMAMUXChannelBaseAndMask(hdma);
  252. /* Reset the DMAMUX channel that corresponds to the DMA channel */
  253. hdma->DMAmuxChannel->CCR = 0U;
  254. /* Clear the DMAMUX synchro overrun flag */
  255. hdma->DMAmuxChannelStatus->CFR = hdma->DMAmuxChannelStatusMask;
  256. /* Reset Request generator parameters if any */
  257. if(((hdma->Init.Request > 0U) && (hdma->Init.Request <= DMA_REQUEST_GENERATOR3)))
  258. {
  259. /* Initialize parameters for DMAMUX request generator :
  260. DMAmuxRequestGen, DMAmuxRequestGenStatus and DMAmuxRequestGenStatusMask
  261. */
  262. DMA_CalcDMAMUXRequestGenBaseAndMask(hdma);
  263. /* Reset the DMAMUX request generator register*/
  264. hdma->DMAmuxRequestGen->RGCR = 0U;
  265. /* Clear the DMAMUX request generator overrun flag */
  266. hdma->DMAmuxRequestGenStatus->RGCFR = hdma->DMAmuxRequestGenStatusMask;
  267. }
  268. hdma->DMAmuxRequestGen = 0U;
  269. hdma->DMAmuxRequestGenStatus = 0U;
  270. hdma->DMAmuxRequestGenStatusMask = 0U;
  271. /* Clean callbacks */
  272. hdma->XferCpltCallback = NULL;
  273. hdma->XferHalfCpltCallback = NULL;
  274. hdma->XferM1CpltCallback = NULL;
  275. hdma->XferM1HalfCpltCallback = NULL;
  276. hdma->XferErrorCallback = NULL;
  277. hdma->XferAbortCallback = NULL;
  278. /* Initialise the error code */
  279. hdma->ErrorCode = HAL_DMA_ERROR_NONE;
  280. /* Initialize the DMA state */
  281. hdma->State = HAL_DMA_STATE_RESET;
  282. /* Release Lock */
  283. __HAL_UNLOCK(hdma);
  284. return HAL_OK;
  285. }
  286. /**
  287. * @}
  288. */
  289. /** @defgroup DMA_Exported_Functions_Group2 Input and Output operation functions
  290. * @brief Input and Output operation functions
  291. *
  292. @verbatim
  293. ===============================================================================
  294. ##### IO operation functions #####
  295. ===============================================================================
  296. [..] This section provides functions allowing to:
  297. (+) Configure the source, destination address and data length and Start DMA transfer
  298. (+) Configure the source, destination address and data length and
  299. Start DMA transfer with interrupt
  300. (+) Abort DMA transfer
  301. (+) Poll for transfer complete
  302. (+) Handle DMA interrupt request
  303. @endverbatim
  304. * @{
  305. */
  306. /**
  307. * @brief Start the DMA Transfer.
  308. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  309. * the configuration information for the specified DMA Channel.
  310. * @param SrcAddress The source memory Buffer address
  311. * @param DstAddress The destination memory Buffer address
  312. * @param DataLength The length of data to be transferred from source to destination
  313. * @retval HAL status
  314. */
  315. HAL_StatusTypeDef HAL_DMA_Start(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength)
  316. {
  317. HAL_StatusTypeDef status = HAL_OK;
  318. /* Check the parameters */
  319. assert_param(IS_DMA_BUFFER_SIZE(DataLength));
  320. /* Process locked */
  321. __HAL_LOCK(hdma);
  322. if(HAL_DMA_STATE_READY == hdma->State)
  323. {
  324. /* Change DMA peripheral state */
  325. hdma->State = HAL_DMA_STATE_BUSY;
  326. hdma->ErrorCode = HAL_DMA_ERROR_NONE;
  327. /* Disable the peripheral */
  328. __HAL_DMA_DISABLE(hdma);
  329. /* Configure the source, destination address and the data length & clear flags*/
  330. DMA_SetConfig(hdma, SrcAddress, DstAddress, DataLength);
  331. /* Enable the Peripheral */
  332. __HAL_DMA_ENABLE(hdma);
  333. }
  334. else
  335. {
  336. /* Process Unlocked */
  337. __HAL_UNLOCK(hdma);
  338. status = HAL_BUSY;
  339. }
  340. return status;
  341. }
  342. /**
  343. * @brief Start the DMA Transfer with interrupt enabled.
  344. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  345. * the configuration information for the specified DMA Channel.
  346. * @param SrcAddress The source memory Buffer address
  347. * @param DstAddress The destination memory Buffer address
  348. * @param DataLength The length of data to be transferred from source to destination
  349. * @retval HAL status
  350. */
  351. HAL_StatusTypeDef HAL_DMA_Start_IT(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength)
  352. {
  353. HAL_StatusTypeDef status = HAL_OK;
  354. /* Check the parameters */
  355. assert_param(IS_DMA_BUFFER_SIZE(DataLength));
  356. /* Process locked */
  357. __HAL_LOCK(hdma);
  358. if(HAL_DMA_STATE_READY == hdma->State)
  359. {
  360. /* Change DMA peripheral state */
  361. hdma->State = HAL_DMA_STATE_BUSY;
  362. hdma->ErrorCode = HAL_DMA_ERROR_NONE;
  363. /* Disable the peripheral */
  364. __HAL_DMA_DISABLE(hdma);
  365. /* Configure the source, destination address and the data length & clear flags*/
  366. DMA_SetConfig(hdma, SrcAddress, DstAddress, DataLength);
  367. /* Enable the transfer complete interrupt */
  368. /* Enable the transfer Error interrupt */
  369. if(NULL != hdma->XferHalfCpltCallback )
  370. {
  371. /* Enable the Half transfer complete interrupt as well */
  372. __HAL_DMA_ENABLE_IT(hdma, (DMA_IT_TC | DMA_IT_HT | DMA_IT_TE));
  373. }
  374. else
  375. {
  376. __HAL_DMA_DISABLE_IT(hdma, DMA_IT_HT);
  377. __HAL_DMA_ENABLE_IT(hdma, (DMA_IT_TC | DMA_IT_TE));
  378. }
  379. /* Check if DMAMUX Synchronization is enabled*/
  380. if((hdma->DMAmuxChannel->CCR & DMAMUX_CxCR_SE) != 0U)
  381. {
  382. /* Enable DMAMUX sync overrun IT*/
  383. hdma->DMAmuxChannel->CCR |= DMAMUX_CxCR_SOIE;
  384. }
  385. if(hdma->DMAmuxRequestGen != 0U)
  386. {
  387. /* if using DMAMUX request generator, enable the DMAMUX request generator overrun IT*/
  388. /* enable the request gen overrun IT*/
  389. hdma->DMAmuxRequestGen->RGCR |= DMAMUX_RGxCR_OIE;
  390. }
  391. /* Enable the Peripheral */
  392. __HAL_DMA_ENABLE(hdma);
  393. }
  394. else
  395. {
  396. /* Process Unlocked */
  397. __HAL_UNLOCK(hdma);
  398. /* Remain BUSY */
  399. status = HAL_BUSY;
  400. }
  401. return status;
  402. }
  403. /**
  404. * @brief Abort the DMA Transfer.
  405. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  406. * the configuration information for the specified DMA Channel.
  407. * @retval HAL status
  408. */
  409. HAL_StatusTypeDef HAL_DMA_Abort(DMA_HandleTypeDef *hdma)
  410. {
  411. HAL_StatusTypeDef status = HAL_OK;
  412. /* Check the DMA peripheral state */
  413. if(hdma->State != HAL_DMA_STATE_BUSY)
  414. {
  415. hdma->ErrorCode = HAL_DMA_ERROR_NO_XFER;
  416. /* Process Unlocked */
  417. __HAL_UNLOCK(hdma);
  418. return HAL_ERROR;
  419. }
  420. else
  421. {
  422. /* Disable DMA IT */
  423. __HAL_DMA_DISABLE_IT(hdma, (DMA_IT_TC | DMA_IT_HT | DMA_IT_TE));
  424. /* disable the DMAMUX sync overrun IT*/
  425. hdma->DMAmuxChannel->CCR &= ~DMAMUX_CxCR_SOIE;
  426. /* Disable the channel */
  427. __HAL_DMA_DISABLE(hdma);
  428. /* Clear all flags */
  429. hdma->DmaBaseAddress->IFCR = (DMA_ISR_GIF1 << (hdma->ChannelIndex & 0x1CU));
  430. /* Clear the DMAMUX synchro overrun flag */
  431. hdma->DMAmuxChannelStatus->CFR = hdma->DMAmuxChannelStatusMask;
  432. if(hdma->DMAmuxRequestGen != 0U)
  433. {
  434. /* if using DMAMUX request generator, disable the DMAMUX request generator overrun IT*/
  435. /* disable the request gen overrun IT*/
  436. hdma->DMAmuxRequestGen->RGCR &= ~DMAMUX_RGxCR_OIE;
  437. /* Clear the DMAMUX request generator overrun flag */
  438. hdma->DMAmuxRequestGenStatus->RGCFR = hdma->DMAmuxRequestGenStatusMask;
  439. }
  440. /* Change the DMA state */
  441. hdma->State = HAL_DMA_STATE_READY;
  442. /* Process Unlocked */
  443. __HAL_UNLOCK(hdma);
  444. return status;
  445. }
  446. }
  447. /**
  448. * @brief Aborts the DMA Transfer in Interrupt mode.
  449. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  450. * the configuration information for the specified DMA Channel.
  451. * @retval HAL status
  452. */
  453. HAL_StatusTypeDef HAL_DMA_Abort_IT(DMA_HandleTypeDef *hdma)
  454. {
  455. HAL_StatusTypeDef status = HAL_OK;
  456. if(HAL_DMA_STATE_BUSY != hdma->State)
  457. {
  458. /* no transfer ongoing */
  459. hdma->ErrorCode = HAL_DMA_ERROR_NO_XFER;
  460. status = HAL_ERROR;
  461. }
  462. else
  463. {
  464. /* Disable DMA IT */
  465. __HAL_DMA_DISABLE_IT(hdma, (DMA_IT_TC | DMA_IT_HT | DMA_IT_TE));
  466. /* Disable the channel */
  467. __HAL_DMA_DISABLE(hdma);
  468. /* disable the DMAMUX sync overrun IT*/
  469. hdma->DMAmuxChannel->CCR &= ~DMAMUX_CxCR_SOIE;
  470. /* Clear all flags */
  471. hdma->DmaBaseAddress->IFCR = (DMA_ISR_GIF1 << (hdma->ChannelIndex & 0x1CU));
  472. /* Clear the DMAMUX synchro overrun flag */
  473. hdma->DMAmuxChannelStatus->CFR = hdma->DMAmuxChannelStatusMask;
  474. if(hdma->DMAmuxRequestGen != 0U)
  475. {
  476. /* if using DMAMUX request generator, disable the DMAMUX request generator overrun IT*/
  477. /* disable the request gen overrun IT*/
  478. hdma->DMAmuxRequestGen->RGCR &= ~DMAMUX_RGxCR_OIE;
  479. /* Clear the DMAMUX request generator overrun flag */
  480. hdma->DMAmuxRequestGenStatus->RGCFR = hdma->DMAmuxRequestGenStatusMask;
  481. }
  482. /* Change the DMA state */
  483. hdma->State = HAL_DMA_STATE_READY;
  484. /* Process Unlocked */
  485. __HAL_UNLOCK(hdma);
  486. /* Call User Abort callback */
  487. if(hdma->XferAbortCallback != NULL)
  488. {
  489. hdma->XferAbortCallback(hdma);
  490. }
  491. }
  492. return status;
  493. }
  494. /**
  495. * @brief Polling for transfer complete.
  496. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  497. * the configuration information for the specified DMA Channel.
  498. * @param CompleteLevel Specifies the DMA level complete.
  499. * @param Timeout Timeout duration.
  500. * @retval HAL status
  501. */
  502. HAL_StatusTypeDef HAL_DMA_PollForTransfer(DMA_HandleTypeDef *hdma, HAL_DMA_LevelCompleteTypeDef CompleteLevel, uint32_t Timeout)
  503. {
  504. uint32_t temp;
  505. uint32_t tickstart;
  506. if(HAL_DMA_STATE_BUSY != hdma->State)
  507. {
  508. /* no transfer ongoing */
  509. hdma->ErrorCode = HAL_DMA_ERROR_NO_XFER;
  510. __HAL_UNLOCK(hdma);
  511. return HAL_ERROR;
  512. }
  513. /* Polling mode not supported in circular mode */
  514. if ((hdma->Instance->CCR & DMA_CCR_CIRC) != 0U)
  515. {
  516. hdma->ErrorCode = HAL_DMA_ERROR_NOT_SUPPORTED;
  517. return HAL_ERROR;
  518. }
  519. /* Get the level transfer complete flag */
  520. if (HAL_DMA_FULL_TRANSFER == CompleteLevel)
  521. {
  522. /* Transfer Complete flag */
  523. temp = DMA_FLAG_TC1 << (hdma->ChannelIndex & 0x1CU);
  524. }
  525. else
  526. {
  527. /* Half Transfer Complete flag */
  528. temp = DMA_FLAG_HT1 << (hdma->ChannelIndex & 0x1CU);
  529. }
  530. /* Get tick */
  531. tickstart = HAL_GetTick();
  532. while((hdma->DmaBaseAddress->ISR & temp) == 0U)
  533. {
  534. if((hdma->DmaBaseAddress->ISR & (DMA_FLAG_TE1 << (hdma->ChannelIndex& 0x1CU))) != 0U)
  535. {
  536. /* When a DMA transfer error occurs */
  537. /* A hardware clear of its EN bits is performed */
  538. /* Clear all flags */
  539. hdma->DmaBaseAddress->IFCR = (DMA_ISR_GIF1 << (hdma->ChannelIndex & 0x1CU));
  540. /* Update error code */
  541. hdma->ErrorCode = HAL_DMA_ERROR_TE;
  542. /* Change the DMA state */
  543. hdma->State= HAL_DMA_STATE_READY;
  544. /* Process Unlocked */
  545. __HAL_UNLOCK(hdma);
  546. return HAL_ERROR;
  547. }
  548. /* Check for the Timeout */
  549. if(Timeout != HAL_MAX_DELAY)
  550. {
  551. if(((HAL_GetTick() - tickstart) > Timeout) || (Timeout == 0U))
  552. {
  553. /* Update error code */
  554. hdma->ErrorCode = HAL_DMA_ERROR_TIMEOUT;
  555. /* Change the DMA state */
  556. hdma->State = HAL_DMA_STATE_READY;
  557. /* Process Unlocked */
  558. __HAL_UNLOCK(hdma);
  559. return HAL_ERROR;
  560. }
  561. }
  562. }
  563. /*Check for DMAMUX Request generator (if used) overrun status */
  564. if(hdma->DMAmuxRequestGen != 0U)
  565. {
  566. /* if using DMAMUX request generator Check for DMAMUX request generator overrun */
  567. if((hdma->DMAmuxRequestGenStatus->RGSR & hdma->DMAmuxRequestGenStatusMask) != 0U)
  568. {
  569. /* Disable the request gen overrun interrupt */
  570. hdma->DMAmuxRequestGen->RGCR |= DMAMUX_RGxCR_OIE;
  571. /* Clear the DMAMUX request generator overrun flag */
  572. hdma->DMAmuxRequestGenStatus->RGCFR = hdma->DMAmuxRequestGenStatusMask;
  573. /* Update error code */
  574. hdma->ErrorCode |= HAL_DMA_ERROR_REQGEN;
  575. }
  576. }
  577. /* Check for DMAMUX Synchronization overrun */
  578. if((hdma->DMAmuxChannelStatus->CSR & hdma->DMAmuxChannelStatusMask) != 0U)
  579. {
  580. /* Clear the DMAMUX synchro overrun flag */
  581. hdma->DMAmuxChannelStatus->CFR = hdma->DMAmuxChannelStatusMask;
  582. /* Update error code */
  583. hdma->ErrorCode |= HAL_DMA_ERROR_SYNC;
  584. }
  585. if(HAL_DMA_FULL_TRANSFER == CompleteLevel)
  586. {
  587. /* Clear the transfer complete flag */
  588. hdma->DmaBaseAddress->IFCR = (DMA_FLAG_TC1 << (hdma->ChannelIndex& 0x1CU));
  589. /* Process unlocked */
  590. __HAL_UNLOCK(hdma);
  591. /* The selected Channelx EN bit is cleared (DMA is disabled and
  592. all transfers are complete) */
  593. hdma->State = HAL_DMA_STATE_READY;
  594. }
  595. else
  596. {
  597. /* Clear the half transfer complete flag */
  598. hdma->DmaBaseAddress->IFCR = (DMA_FLAG_HT1 << (hdma->ChannelIndex & 0x1CU));
  599. }
  600. return HAL_OK;
  601. }
  602. /**
  603. * @brief Handle DMA interrupt request.
  604. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  605. * the configuration information for the specified DMA Channel.
  606. * @retval None
  607. */
  608. void HAL_DMA_IRQHandler(DMA_HandleTypeDef *hdma)
  609. {
  610. uint32_t flag_it = hdma->DmaBaseAddress->ISR;
  611. uint32_t source_it = hdma->Instance->CCR;
  612. /* Half Transfer Complete Interrupt management ******************************/
  613. if (((flag_it & (DMA_FLAG_HT1 << (hdma->ChannelIndex & 0x1CU))) != 0U) && ((source_it & DMA_IT_HT) != 0U))
  614. {
  615. /* Multi_Buffering mode enabled */
  616. if(((hdma->Instance->CCR) & (uint32_t)(DMA_CCR_DBM)) != 0U)
  617. {
  618. /* Clear the half transfer complete flag */
  619. hdma->DmaBaseAddress->IFCR = DMA_ISR_HTIF1 << (hdma->ChannelIndex & 0x1CU);
  620. /* Current memory buffer used is Memory 0 */
  621. if((hdma->Instance->CCR & DMA_CCR_CT) == 0U)
  622. {
  623. if(hdma->XferHalfCpltCallback != NULL)
  624. {
  625. /* Half transfer callback */
  626. hdma->XferHalfCpltCallback(hdma);
  627. }
  628. }
  629. /* Current memory buffer used is Memory 1 */
  630. else
  631. {
  632. if(hdma->XferM1HalfCpltCallback != NULL)
  633. {
  634. /* Half transfer callback */
  635. hdma->XferM1HalfCpltCallback(hdma);
  636. }
  637. }
  638. }
  639. else
  640. {
  641. /* Disable the half transfer interrupt if the DMA mode is not CIRCULAR */
  642. if((hdma->Instance->CCR & DMA_CCR_CIRC) == 0U)
  643. {
  644. /* Disable the half transfer interrupt */
  645. __HAL_DMA_DISABLE_IT(hdma, DMA_IT_HT);
  646. }
  647. /* Clear the half transfer complete flag */
  648. hdma->DmaBaseAddress->IFCR = DMA_ISR_HTIF1 << (hdma->ChannelIndex & 0x1CU);
  649. /* DMA peripheral state is not updated in Half Transfer */
  650. /* but in Transfer Complete case */
  651. if(hdma->XferHalfCpltCallback != NULL)
  652. {
  653. /* Half transfer callback */
  654. hdma->XferHalfCpltCallback(hdma);
  655. }
  656. }
  657. }
  658. /* Transfer Complete Interrupt management ***********************************/
  659. else if (((flag_it & (DMA_FLAG_TC1 << (hdma->ChannelIndex & 0x1CU))) != 0U) && ((source_it & DMA_IT_TC) != 0U))
  660. {
  661. if(((hdma->Instance->CCR) & (uint32_t)(DMA_CCR_DBM)) != 0U)
  662. {
  663. /* Current memory buffer used is Memory 0 */
  664. if((hdma->Instance->CCR & DMA_CCR_CT) == 0U)
  665. {
  666. if(hdma->XferM1CpltCallback != NULL)
  667. {
  668. /* Transfer complete Callback for memory1 */
  669. hdma->XferM1CpltCallback(hdma);
  670. }
  671. }
  672. /* Current memory buffer used is Memory 1 */
  673. else
  674. {
  675. if(hdma->XferCpltCallback != NULL)
  676. {
  677. /* Transfer complete Callback for memory0 */
  678. hdma->XferCpltCallback(hdma);
  679. }
  680. }
  681. }
  682. else
  683. {
  684. if((hdma->Instance->CCR & DMA_CCR_CIRC) == 0U)
  685. {
  686. /* Disable the transfer complete interrupt if the DMA mode is not CIRCULAR */
  687. /* Disable the transfer complete and error interrupt */
  688. /* if the DMA mode is not CIRCULAR */
  689. __HAL_DMA_DISABLE_IT(hdma, DMA_IT_TE | DMA_IT_TC);
  690. /* Change the DMA state */
  691. hdma->State = HAL_DMA_STATE_READY;
  692. }
  693. /* Clear the transfer complete flag */
  694. hdma->DmaBaseAddress->IFCR = (DMA_ISR_TCIF1 << (hdma->ChannelIndex & 0x1CU));
  695. /* Process Unlocked */
  696. __HAL_UNLOCK(hdma);
  697. if(hdma->XferCpltCallback != NULL)
  698. {
  699. /* Transfer complete callback */
  700. hdma->XferCpltCallback(hdma);
  701. }
  702. }
  703. }
  704. /* Transfer Error Interrupt management **************************************/
  705. else if (((flag_it & (DMA_FLAG_TE1 << (hdma->ChannelIndex & 0x1CU))) != 0U) && ((source_it & DMA_IT_TE) != 0U))
  706. {
  707. /* When a DMA transfer error occurs */
  708. /* A hardware clear of its EN bits is performed */
  709. /* Disable ALL DMA IT */
  710. __HAL_DMA_DISABLE_IT(hdma, (DMA_IT_TC | DMA_IT_HT | DMA_IT_TE));
  711. /* Clear all flags */
  712. hdma->DmaBaseAddress->IFCR = (DMA_ISR_GIF1 << (hdma->ChannelIndex & 0x1CU));
  713. /* Update error code */
  714. hdma->ErrorCode = HAL_DMA_ERROR_TE;
  715. /* Change the DMA state */
  716. hdma->State = HAL_DMA_STATE_READY;
  717. /* Process Unlocked */
  718. __HAL_UNLOCK(hdma);
  719. if (hdma->XferErrorCallback != NULL)
  720. {
  721. /* Transfer error callback */
  722. hdma->XferErrorCallback(hdma);
  723. }
  724. }
  725. else
  726. {
  727. /* Nothing To Do */
  728. }
  729. return;
  730. }
  731. /**
  732. * @brief Register callbacks
  733. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  734. * the configuration information for the specified DMA Channel.
  735. * @param CallbackID User Callback identifier
  736. * a HAL_DMA_CallbackIDTypeDef ENUM as parameter.
  737. * @param pCallback pointer to private callbacsk function which has pointer to
  738. * a DMA_HandleTypeDef structure as parameter.
  739. * @retval HAL status
  740. */
  741. HAL_StatusTypeDef HAL_DMA_RegisterCallback(DMA_HandleTypeDef *hdma, HAL_DMA_CallbackIDTypeDef CallbackID, void (* pCallback)( DMA_HandleTypeDef * _hdma))
  742. {
  743. HAL_StatusTypeDef status = HAL_OK;
  744. /* Process locked */
  745. __HAL_LOCK(hdma);
  746. if(HAL_DMA_STATE_READY == hdma->State)
  747. {
  748. switch (CallbackID)
  749. {
  750. case HAL_DMA_XFER_CPLT_CB_ID:
  751. hdma->XferCpltCallback = pCallback;
  752. break;
  753. case HAL_DMA_XFER_HALFCPLT_CB_ID:
  754. hdma->XferHalfCpltCallback = pCallback;
  755. break;
  756. case HAL_DMA_XFER_M1CPLT_CB_ID:
  757. hdma->XferM1CpltCallback = pCallback;
  758. break;
  759. case HAL_DMA_XFER_M1HALFCPLT_CB_ID:
  760. hdma->XferM1HalfCpltCallback = 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 identifier
  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_M1CPLT_CB_ID:
  805. hdma->XferM1CpltCallback = NULL;
  806. break;
  807. case HAL_DMA_XFER_M1HALFCPLT_CB_ID:
  808. hdma->XferM1HalfCpltCallback = NULL;
  809. break;
  810. case HAL_DMA_XFER_ERROR_CB_ID:
  811. hdma->XferErrorCallback = NULL;
  812. break;
  813. case HAL_DMA_XFER_ABORT_CB_ID:
  814. hdma->XferAbortCallback = NULL;
  815. break;
  816. case HAL_DMA_XFER_ALL_CB_ID:
  817. hdma->XferCpltCallback = NULL;
  818. hdma->XferHalfCpltCallback = NULL;
  819. hdma->XferM1CpltCallback = NULL;
  820. hdma->XferM1HalfCpltCallback = NULL;
  821. hdma->XferErrorCallback = NULL;
  822. hdma->XferAbortCallback = NULL;
  823. break;
  824. default:
  825. status = HAL_ERROR;
  826. break;
  827. }
  828. }
  829. else
  830. {
  831. status = HAL_ERROR;
  832. }
  833. /* Release Lock */
  834. __HAL_UNLOCK(hdma);
  835. return status;
  836. }
  837. /**
  838. * @}
  839. */
  840. /** @defgroup DMA_Exported_Functions_Group3 Peripheral State and Errors functions
  841. * @brief Peripheral State and Errors functions
  842. *
  843. @verbatim
  844. ===============================================================================
  845. ##### Peripheral State and Errors functions #####
  846. ===============================================================================
  847. [..]
  848. This subsection provides functions allowing to
  849. (+) Check the DMA state
  850. (+) Get error code
  851. @endverbatim
  852. * @{
  853. */
  854. /**
  855. * @brief Return the DMA handle state.
  856. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  857. * the configuration information for the specified DMA Channel.
  858. * @retval HAL state
  859. */
  860. HAL_DMA_StateTypeDef HAL_DMA_GetState(DMA_HandleTypeDef *hdma)
  861. {
  862. /* Return DMA handle state */
  863. return hdma->State;
  864. }
  865. /**
  866. * @brief Return the DMA error code.
  867. * @param hdma : pointer to a DMA_HandleTypeDef structure that contains
  868. * the configuration information for the specified DMA Channel.
  869. * @retval DMA Error Code
  870. */
  871. uint32_t HAL_DMA_GetError(DMA_HandleTypeDef *hdma)
  872. {
  873. return hdma->ErrorCode;
  874. }
  875. /**
  876. * @}
  877. */
  878. /**
  879. * @}
  880. */
  881. /** @defgroup DMA_Exported_Functions_Group4 Attributes management functions
  882. * @brief Attributes management functions
  883. *
  884. @verbatim
  885. ===============================================================================
  886. ##### Attributes management functions #####
  887. ===============================================================================
  888. [..]
  889. This subsection provides functions allowing to
  890. (+) Configure the DMA channel(s) privilege and non-privilege attributes
  891. (+) Configure the DMA channel(s) secure and non-secure attributes from
  892. secure world when when the system implements the security (TZEN=1)
  893. (+) Get the DMA channel(s) attributes
  894. @endverbatim
  895. * @{
  896. */
  897. /**
  898. * @brief Configure the DMA channel attribute(s).
  899. * @note Available attributes are security and privilege protection.
  900. * Each field can be set independently. Not allowed configurations
  901. * are not taken into account & HAL_ERROR returned.
  902. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  903. * the configuration information for the specified DMA Channel.
  904. * @param ChannelAttributes specifies the DMA channel secure/privilege attributes.
  905. * This parameter can be a one or a combination of @ref DMA_Channel_Attributes
  906. * @retval HAL Status
  907. */
  908. HAL_StatusTypeDef HAL_DMA_ConfigChannelAttributes(DMA_HandleTypeDef *hdma, uint32_t ChannelAttributes)
  909. {
  910. HAL_StatusTypeDef status = HAL_OK;
  911. uint32_t ccr;
  912. #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
  913. uint32_t ccr_SECM;
  914. #endif
  915. /* Check the DMA peripheral handle */
  916. if(hdma == NULL)
  917. {
  918. status = HAL_ERROR;
  919. return status;
  920. }
  921. /* Check the parameters */
  922. assert_param(IS_DMA_ATTRIBUTES(ChannelAttributes));
  923. /* Read CCR register */
  924. ccr = READ_REG(hdma->Instance->CCR);
  925. /* Apply any requested privilege/non-privilege attributes */
  926. if ((ChannelAttributes & DMA_CHANNEL_ATTR_PRIV_MASK) != 0U)
  927. {
  928. if((ChannelAttributes & DMA_CCR_PRIV) != 0U)
  929. {
  930. SET_BIT(ccr, DMA_CCR_PRIV);
  931. }
  932. else
  933. {
  934. CLEAR_BIT(ccr, DMA_CCR_PRIV);
  935. }
  936. }
  937. #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
  938. /* Channel */
  939. /* Check what is the current SECM status */
  940. if ((hdma->Instance->CCR & DMA_CCR_SECM) == DMA_CCR_SECM)
  941. {
  942. /* Channel is currently secure */
  943. ccr_SECM = DMA_CCR_SECM;
  944. }
  945. else
  946. {
  947. /* Channel is currently non-secure */
  948. ccr_SECM = 0U;
  949. }
  950. if((ChannelAttributes & DMA_CHANNEL_ATTR_SEC_MASK) != 0U)
  951. {
  952. if((ChannelAttributes & DMA_CCR_SECM) != 0U)
  953. {
  954. SET_BIT(ccr, DMA_CCR_SECM);
  955. /* Channel changed to secure */
  956. ccr_SECM = DMA_CCR_SECM;
  957. }
  958. else
  959. {
  960. CLEAR_BIT(ccr, DMA_CCR_SECM);
  961. /* Channel changed to non-secure */
  962. ccr_SECM = 0U;
  963. }
  964. }
  965. /* Channel source */
  966. if((ChannelAttributes & DMA_CHANNEL_ATTR_SEC_SRC_MASK) != 0U)
  967. {
  968. /* Configure Source security attributes */
  969. if ((ChannelAttributes & DMA_CCR_SSEC) != 0x0U)
  970. {
  971. /* SSEC can only be set if channel is secure */
  972. /* Otherwise configuration is not taken into account */
  973. if (ccr_SECM == 0U)
  974. {
  975. /* DSEC can not be secure */
  976. /* Source channel is non secure */
  977. status = HAL_ERROR;
  978. }
  979. else
  980. {
  981. SET_BIT(ccr, DMA_CCR_SSEC);
  982. }
  983. }
  984. else
  985. {
  986. CLEAR_BIT(ccr, DMA_CCR_SSEC);
  987. }
  988. }
  989. /* Channel destination */
  990. if((ChannelAttributes & DMA_CHANNEL_ATTR_SEC_DEST_MASK) != 0U)
  991. {
  992. /* Configure Destination security attributes */
  993. if((ChannelAttributes & DMA_CCR_DSEC) != 0U)
  994. {
  995. if (ccr_SECM == 0U)
  996. {
  997. /* DSEC can only be set if channel is secure */
  998. /* Destination channel is non secure */
  999. status = HAL_ERROR;
  1000. }
  1001. else
  1002. {
  1003. SET_BIT(ccr, DMA_CCR_DSEC);
  1004. }
  1005. }
  1006. else
  1007. {
  1008. CLEAR_BIT(ccr, DMA_CCR_DSEC);
  1009. }
  1010. }
  1011. #endif /* __ARM_FEATURE_CMSE */
  1012. /* Update CCR Register: PRIV, SECM, SCEC, DSEC bits */
  1013. WRITE_REG(hdma->Instance->CCR, ccr);
  1014. return status;
  1015. }
  1016. /**
  1017. * @brief Get the attribute of a DMA channel.
  1018. * @note Secure and non-secure attributes are only available from secure state
  1019. * when the system implements the security (TZEN=1)
  1020. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  1021. * the configuration information for the specified DMA Channel.
  1022. * @param ChannelAttributes pointer to return the attributes.
  1023. * @retval HAL Status.
  1024. */
  1025. HAL_StatusTypeDef HAL_DMA_GetConfigChannelAttributes(DMA_HandleTypeDef *hdma, uint32_t *ChannelAttributes)
  1026. {
  1027. uint32_t read_attributes;
  1028. uint32_t attributes;
  1029. /* Check the DMA peripheral handle and pointer to returned value */
  1030. if((hdma == NULL) || (ChannelAttributes == NULL))
  1031. {
  1032. return HAL_ERROR;
  1033. }
  1034. #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
  1035. /* Get secure or non-secure attributes */
  1036. read_attributes = READ_BIT(hdma->Instance->CCR, DMA_CCR_PRIV | DMA_CCR_SECM | DMA_CCR_SSEC | DMA_CCR_DSEC);
  1037. /* Get secure or non-secure attributes */
  1038. attributes = ((read_attributes & DMA_CCR_PRIV) == 0U) ? DMA_CHANNEL_NPRIV : DMA_CHANNEL_PRIV;
  1039. /* Get security attributes */
  1040. attributes |= ((read_attributes & DMA_CCR_SECM) == 0U) ? DMA_CHANNEL_NSEC : DMA_CHANNEL_SEC;
  1041. /* Get security attributes of the source */
  1042. attributes |= ((read_attributes & DMA_CCR_SSEC) == 0U) ? DMA_CHANNEL_SRC_NSEC : DMA_CHANNEL_SRC_SEC;
  1043. /* Get security attributes of the destination */
  1044. attributes |= ((read_attributes & DMA_CCR_DSEC) == 0U) ? DMA_CHANNEL_DEST_NSEC : DMA_CHANNEL_DEST_SEC;
  1045. #else
  1046. /* Get secure or non-secure attributes */
  1047. read_attributes = READ_BIT(hdma->Instance->CCR, DMA_CCR_PRIV);
  1048. attributes = ((read_attributes & DMA_CCR_PRIV) == 0U) ? DMA_CHANNEL_NPRIV : DMA_CHANNEL_PRIV;
  1049. #endif /* __ARM_FEATURE_CMSE */
  1050. /* return value */
  1051. *ChannelAttributes = attributes;
  1052. return HAL_OK;
  1053. }
  1054. /** @addtogroup DMA_Private_Functions
  1055. * @{
  1056. */
  1057. /**
  1058. * @brief Sets the DMA Transfer parameter.
  1059. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  1060. * the configuration information for the specified DMA Channel.
  1061. * @param SrcAddress The source memory Buffer address
  1062. * @param DstAddress The destination memory Buffer address
  1063. * @param DataLength The length of data to be transferred from source to destination
  1064. * @retval HAL status
  1065. */
  1066. static void DMA_SetConfig(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength)
  1067. {
  1068. /* Clear the DMAMUX synchro overrun flag */
  1069. hdma->DMAmuxChannelStatus->CFR = hdma->DMAmuxChannelStatusMask;
  1070. if(hdma->DMAmuxRequestGen != 0U)
  1071. {
  1072. /* Clear the DMAMUX request generator overrun flag */
  1073. hdma->DMAmuxRequestGenStatus->RGCFR = hdma->DMAmuxRequestGenStatusMask;
  1074. }
  1075. /* Clear all flags */
  1076. hdma->DmaBaseAddress->IFCR = (DMA_ISR_GIF1 << (hdma->ChannelIndex & 0x1CU));
  1077. /* Configure DMA Channel data length */
  1078. hdma->Instance->CNDTR = DataLength;
  1079. /* Memory to Peripheral */
  1080. if((hdma->Init.Direction) == DMA_MEMORY_TO_PERIPH)
  1081. {
  1082. /* Configure DMA Channel destination address */
  1083. hdma->Instance->CPAR = DstAddress;
  1084. /* Configure DMA Channel source address */
  1085. hdma->Instance->CM0AR = SrcAddress;
  1086. }
  1087. /* Peripheral to Memory */
  1088. else
  1089. {
  1090. /* Configure DMA Channel source address */
  1091. hdma->Instance->CPAR = SrcAddress;
  1092. /* Configure DMA Channel destination address */
  1093. hdma->Instance->CM0AR = DstAddress;
  1094. }
  1095. }
  1096. /**
  1097. * @brief Updates the DMA handle with the DMAMUX channel and status mask depending on channel number
  1098. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  1099. * the configuration information for the specified DMA Channel.
  1100. * @retval None
  1101. */
  1102. static void DMA_CalcDMAMUXChannelBaseAndMask(DMA_HandleTypeDef *hdma)
  1103. {
  1104. uint32_t channel_number;
  1105. /* check if instance is not outside the DMA channel range */
  1106. if ((uint32_t)hdma->Instance < (uint32_t)DMA2_Channel1)
  1107. {
  1108. /* DMA1 */
  1109. hdma->DMAmuxChannel = (DMAMUX1_Channel0 + (hdma->ChannelIndex >> 2U));
  1110. }
  1111. else
  1112. {
  1113. /* DMA2 */
  1114. hdma->DMAmuxChannel = (DMAMUX1_Channel8 + (hdma->ChannelIndex >> 2U));
  1115. }
  1116. channel_number = (((uint32_t)hdma->Instance & 0xFFU) - 8U) / 20U;
  1117. hdma->DMAmuxChannelStatus = DMAMUX1_ChannelStatus;
  1118. hdma->DMAmuxChannelStatusMask = 1UL << (channel_number & 0x1FU);
  1119. }
  1120. /**
  1121. * @brief Updates the DMA handle with the DMAMUX request generator params
  1122. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  1123. * the configuration information for the specified DMA Channel.
  1124. * @retval None
  1125. */
  1126. static void DMA_CalcDMAMUXRequestGenBaseAndMask(DMA_HandleTypeDef *hdma)
  1127. {
  1128. uint32_t request = hdma->Init.Request & DMAMUX_CxCR_DMAREQ_ID;
  1129. /* DMA Channels are connected to DMAMUX1 request generator blocks*/
  1130. hdma->DMAmuxRequestGen = (DMAMUX_RequestGen_TypeDef *)((uint32_t)(((uint32_t)DMAMUX1_RequestGenerator0) + ((request - 1U) * 4U)));
  1131. hdma->DMAmuxRequestGenStatus = DMAMUX1_RequestGenStatus;
  1132. /* here "Request" is either DMA_REQUEST_GENERATOR0 to DMA_REQUEST_GENERATOR3, i.e. <= 4*/
  1133. hdma->DMAmuxRequestGenStatusMask = 1UL << ((request - 1U) & 0x3U);
  1134. }
  1135. /**
  1136. * @}
  1137. */
  1138. /**
  1139. * @}
  1140. */
  1141. #endif /* HAL_DMA_MODULE_ENABLED */
  1142. /**
  1143. * @}
  1144. */
  1145. /**
  1146. * @}
  1147. */