stm32f7xx_hal_dma.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312
  1. /**
  2. ******************************************************************************
  3. * @file stm32f7xx_hal_dma.c
  4. * @author MCD Application Team
  5. * @brief DMA HAL module driver.
  6. *
  7. * This file provides firmware functions to manage the following
  8. * functionalities of the Direct Memory Access (DMA) peripheral:
  9. * + Initialization and de-initialization functions
  10. * + IO operation functions
  11. * + Peripheral State and errors functions
  12. @verbatim
  13. ==============================================================================
  14. ##### How to use this driver #####
  15. ==============================================================================
  16. [..]
  17. (#) Enable and configure the peripheral to be connected to the DMA Stream
  18. (except for internal SRAM/FLASH memories: no initialization is
  19. necessary) please refer to Reference manual for connection between peripherals
  20. and DMA requests.
  21. (#) For a given Stream, program the required configuration through the following parameters:
  22. Transfer Direction, Source and Destination data formats,
  23. Circular, Normal or peripheral flow control mode, Stream Priority level,
  24. Source and Destination Increment mode, FIFO mode and its Threshold (if needed),
  25. Burst mode for Source and/or Destination (if needed) using HAL_DMA_Init() function.
  26. -@- Prior to HAL_DMA_Init() the clock must be enabled for DMA through the following macros:
  27. __HAL_RCC_DMA1_CLK_ENABLE() or __HAL_RCC_DMA2_CLK_ENABLE().
  28. *** Polling mode IO operation ***
  29. =================================
  30. [..]
  31. (+) Use HAL_DMA_Start() to start DMA transfer after the configuration of Source
  32. address and destination address and the Length of data to be transferred.
  33. (+) Use HAL_DMA_PollForTransfer() to poll for the end of current transfer, in this
  34. case a fixed Timeout can be configured by User depending from his application.
  35. (+) Use HAL_DMA_Abort() function to abort the current transfer.
  36. *** Interrupt mode IO operation ***
  37. ===================================
  38. [..]
  39. (+) Configure the DMA interrupt priority using HAL_NVIC_SetPriority()
  40. (+) Enable the DMA IRQ handler using HAL_NVIC_EnableIRQ()
  41. (+) Select Callbacks functions using HAL_DMA_RegisterCallback()
  42. (+) Use HAL_DMA_Start_IT() to start DMA transfer after the configuration of
  43. Source address and destination address and the Length of data to be transferred. In this
  44. case the DMA interrupt is configured
  45. (+) Use HAL_DMA_IRQHandler() called under DMA_IRQHandler() Interrupt subroutine
  46. (+) At the end of data transfer HAL_DMA_IRQHandler() function is executed and user can
  47. add his own function by customization of function pointer XferCpltCallback and
  48. XferErrorCallback (i.e a member of DMA handle structure).
  49. [..]
  50. (#) Use HAL_DMA_GetState() function to return the DMA state and HAL_DMA_GetError() in case of error
  51. detection.
  52. (#) Use HAL_DMA_Abort_IT() function to abort the current transfer
  53. -@- In Memory-to-Memory transfer mode, Circular mode is not allowed.
  54. -@- The FIFO is used mainly to reduce bus usage and to allow data packing/unpacking: it is
  55. possible to set different Data Sizes for the Peripheral and the Memory (ie. you can set
  56. Half-Word data size for the peripheral to access its data register and set Word data size
  57. for the Memory to gain in access time. Each two half words will be packed and written in
  58. a single access to a Word in the Memory).
  59. -@- When FIFO is disabled, it is not allowed to configure different Data Sizes for Source
  60. and Destination. In this case the Peripheral Data Size will be applied to both Source
  61. and Destination.
  62. *** DMA HAL driver macros list ***
  63. =============================================
  64. [..]
  65. Below the list of most used macros in DMA HAL driver.
  66. (+) __HAL_DMA_ENABLE: Enable the specified DMA Stream.
  67. (+) __HAL_DMA_DISABLE: Disable the specified DMA Stream.
  68. (+) __HAL_DMA_GET_IT_SOURCE: Check whether the specified DMA Stream interrupt has occurred or not.
  69. [..]
  70. (@) You can refer to the DMA HAL driver header file for more useful macros
  71. @endverbatim
  72. ******************************************************************************
  73. * @attention
  74. *
  75. * Copyright (c) 2017 STMicroelectronics.
  76. * All rights reserved.
  77. *
  78. * This software is licensed under terms that can be found in the LICENSE file in
  79. * the root directory of this software component.
  80. * If no LICENSE file comes with this software, it is provided AS-IS.
  81. *
  82. ******************************************************************************
  83. */
  84. /* Includes ------------------------------------------------------------------*/
  85. #include "stm32f7xx_hal.h"
  86. /** @addtogroup STM32F7xx_HAL_Driver
  87. * @{
  88. */
  89. /** @defgroup DMA DMA
  90. * @brief DMA HAL module driver
  91. * @{
  92. */
  93. #ifdef HAL_DMA_MODULE_ENABLED
  94. /* Private types -------------------------------------------------------------*/
  95. typedef struct
  96. {
  97. __IO uint32_t ISR; /*!< DMA interrupt status register */
  98. __IO uint32_t Reserved0;
  99. __IO uint32_t IFCR; /*!< DMA interrupt flag clear register */
  100. } DMA_Base_Registers;
  101. /* Private variables ---------------------------------------------------------*/
  102. /* Private constants ---------------------------------------------------------*/
  103. /** @addtogroup DMA_Private_Constants
  104. * @{
  105. */
  106. #define HAL_TIMEOUT_DMA_ABORT ((uint32_t)5) /* 5 ms */
  107. /**
  108. * @}
  109. */
  110. /* Private macros ------------------------------------------------------------*/
  111. /* Private functions ---------------------------------------------------------*/
  112. /** @addtogroup DMA_Private_Functions
  113. * @{
  114. */
  115. static void DMA_SetConfig(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength);
  116. static uint32_t DMA_CalcBaseAndBitshift(DMA_HandleTypeDef *hdma);
  117. static HAL_StatusTypeDef DMA_CheckFifoParam(DMA_HandleTypeDef *hdma);
  118. /**
  119. * @}
  120. */
  121. /* Exported functions ---------------------------------------------------------*/
  122. /** @addtogroup DMA_Exported_Functions
  123. * @{
  124. */
  125. /** @addtogroup DMA_Exported_Functions_Group1
  126. *
  127. @verbatim
  128. ===============================================================================
  129. ##### Initialization and de-initialization functions #####
  130. ===============================================================================
  131. [..]
  132. This section provides functions allowing to initialize the DMA Stream source
  133. and destination addresses, incrementation and data sizes, transfer direction,
  134. circular/normal mode selection, memory-to-memory mode selection and Stream priority value.
  135. [..]
  136. The HAL_DMA_Init() function follows the DMA configuration procedures as described in
  137. reference manual.
  138. @endverbatim
  139. * @{
  140. */
  141. /**
  142. * @brief Initialize the DMA according to the specified
  143. * parameters in the DMA_InitTypeDef and create the associated handle.
  144. * @param hdma Pointer to a DMA_HandleTypeDef structure that contains
  145. * the configuration information for the specified DMA Stream.
  146. * @retval HAL status
  147. */
  148. HAL_StatusTypeDef HAL_DMA_Init(DMA_HandleTypeDef *hdma)
  149. {
  150. uint32_t tmp = 0U;
  151. uint32_t tickstart = HAL_GetTick();
  152. DMA_Base_Registers *regs;
  153. /* Check the DMA peripheral state */
  154. if(hdma == NULL)
  155. {
  156. return HAL_ERROR;
  157. }
  158. /* Check the parameters */
  159. assert_param(IS_DMA_STREAM_ALL_INSTANCE(hdma->Instance));
  160. assert_param(IS_DMA_CHANNEL(hdma->Init.Channel));
  161. assert_param(IS_DMA_DIRECTION(hdma->Init.Direction));
  162. assert_param(IS_DMA_PERIPHERAL_INC_STATE(hdma->Init.PeriphInc));
  163. assert_param(IS_DMA_MEMORY_INC_STATE(hdma->Init.MemInc));
  164. assert_param(IS_DMA_PERIPHERAL_DATA_SIZE(hdma->Init.PeriphDataAlignment));
  165. assert_param(IS_DMA_MEMORY_DATA_SIZE(hdma->Init.MemDataAlignment));
  166. assert_param(IS_DMA_MODE(hdma->Init.Mode));
  167. assert_param(IS_DMA_PRIORITY(hdma->Init.Priority));
  168. assert_param(IS_DMA_FIFO_MODE_STATE(hdma->Init.FIFOMode));
  169. /* Check the memory burst, peripheral burst and FIFO threshold parameters only
  170. when FIFO mode is enabled */
  171. if(hdma->Init.FIFOMode != DMA_FIFOMODE_DISABLE)
  172. {
  173. assert_param(IS_DMA_FIFO_THRESHOLD(hdma->Init.FIFOThreshold));
  174. assert_param(IS_DMA_MEMORY_BURST(hdma->Init.MemBurst));
  175. assert_param(IS_DMA_PERIPHERAL_BURST(hdma->Init.PeriphBurst));
  176. }
  177. /* Change DMA peripheral state */
  178. hdma->State = HAL_DMA_STATE_BUSY;
  179. /* Allocate lock resource */
  180. __HAL_UNLOCK(hdma);
  181. /* Disable the peripheral */
  182. __HAL_DMA_DISABLE(hdma);
  183. /* Check if the DMA Stream is effectively disabled */
  184. while((hdma->Instance->CR & DMA_SxCR_EN) != RESET)
  185. {
  186. /* Check for the Timeout */
  187. if((HAL_GetTick() - tickstart ) > HAL_TIMEOUT_DMA_ABORT)
  188. {
  189. /* Update error code */
  190. hdma->ErrorCode = HAL_DMA_ERROR_TIMEOUT;
  191. /* Change the DMA state */
  192. hdma->State = HAL_DMA_STATE_TIMEOUT;
  193. return HAL_TIMEOUT;
  194. }
  195. }
  196. /* Get the CR register value */
  197. tmp = hdma->Instance->CR;
  198. /* Clear CHSEL, MBURST, PBURST, PL, MSIZE, PSIZE, MINC, PINC, CIRC, DIR, CT and DBM bits */
  199. tmp &= ((uint32_t)~(DMA_SxCR_CHSEL | DMA_SxCR_MBURST | DMA_SxCR_PBURST | \
  200. DMA_SxCR_PL | DMA_SxCR_MSIZE | DMA_SxCR_PSIZE | \
  201. DMA_SxCR_MINC | DMA_SxCR_PINC | DMA_SxCR_CIRC | \
  202. DMA_SxCR_DIR | DMA_SxCR_CT | DMA_SxCR_DBM));
  203. /* Prepare the DMA Stream configuration */
  204. tmp |= hdma->Init.Channel | hdma->Init.Direction |
  205. hdma->Init.PeriphInc | hdma->Init.MemInc |
  206. hdma->Init.PeriphDataAlignment | hdma->Init.MemDataAlignment |
  207. hdma->Init.Mode | hdma->Init.Priority;
  208. /* the Memory burst and peripheral burst are not used when the FIFO is disabled */
  209. if(hdma->Init.FIFOMode == DMA_FIFOMODE_ENABLE)
  210. {
  211. /* Get memory burst and peripheral burst */
  212. tmp |= hdma->Init.MemBurst | hdma->Init.PeriphBurst;
  213. }
  214. /* Write to DMA Stream CR register */
  215. hdma->Instance->CR = tmp;
  216. /* Get the FCR register value */
  217. tmp = hdma->Instance->FCR;
  218. /* Clear Direct mode and FIFO threshold bits */
  219. tmp &= (uint32_t)~(DMA_SxFCR_DMDIS | DMA_SxFCR_FTH);
  220. /* Prepare the DMA Stream FIFO configuration */
  221. tmp |= hdma->Init.FIFOMode;
  222. /* The FIFO threshold is not used when the FIFO mode is disabled */
  223. if(hdma->Init.FIFOMode == DMA_FIFOMODE_ENABLE)
  224. {
  225. /* Get the FIFO threshold */
  226. tmp |= hdma->Init.FIFOThreshold;
  227. /* Check compatibility between FIFO threshold level and size of the memory burst */
  228. /* for INCR4, INCR8, INCR16 bursts */
  229. if (hdma->Init.MemBurst != DMA_MBURST_SINGLE)
  230. {
  231. if (DMA_CheckFifoParam(hdma) != HAL_OK)
  232. {
  233. /* Update error code */
  234. hdma->ErrorCode = HAL_DMA_ERROR_PARAM;
  235. /* Change the DMA state */
  236. hdma->State = HAL_DMA_STATE_READY;
  237. return HAL_ERROR;
  238. }
  239. }
  240. }
  241. /* Write to DMA Stream FCR */
  242. hdma->Instance->FCR = tmp;
  243. /* Initialize StreamBaseAddress and StreamIndex parameters to be used to calculate
  244. DMA steam Base Address needed by HAL_DMA_IRQHandler() and HAL_DMA_PollForTransfer() */
  245. regs = (DMA_Base_Registers *)DMA_CalcBaseAndBitshift(hdma);
  246. /* Clear all interrupt flags */
  247. regs->IFCR = 0x3FU << hdma->StreamIndex;
  248. /* Initialize the error code */
  249. hdma->ErrorCode = HAL_DMA_ERROR_NONE;
  250. /* Initialize the DMA state */
  251. hdma->State = HAL_DMA_STATE_READY;
  252. return HAL_OK;
  253. }
  254. /**
  255. * @brief DeInitializes the DMA peripheral
  256. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  257. * the configuration information for the specified DMA Stream.
  258. * @retval HAL status
  259. */
  260. HAL_StatusTypeDef HAL_DMA_DeInit(DMA_HandleTypeDef *hdma)
  261. {
  262. DMA_Base_Registers *regs;
  263. /* Check the DMA peripheral state */
  264. if(hdma == NULL)
  265. {
  266. return HAL_ERROR;
  267. }
  268. /* Check the DMA peripheral state */
  269. if(hdma->State == HAL_DMA_STATE_BUSY)
  270. {
  271. /* Return error status */
  272. return HAL_BUSY;
  273. }
  274. /* Check the parameters */
  275. assert_param(IS_DMA_STREAM_ALL_INSTANCE(hdma->Instance));
  276. /* Disable the selected DMA Streamx */
  277. __HAL_DMA_DISABLE(hdma);
  278. /* Reset DMA Streamx control register */
  279. hdma->Instance->CR = 0U;
  280. /* Reset DMA Streamx number of data to transfer register */
  281. hdma->Instance->NDTR = 0U;
  282. /* Reset DMA Streamx peripheral address register */
  283. hdma->Instance->PAR = 0U;
  284. /* Reset DMA Streamx memory 0 address register */
  285. hdma->Instance->M0AR = 0U;
  286. /* Reset DMA Streamx memory 1 address register */
  287. hdma->Instance->M1AR = 0U;
  288. /* Reset DMA Streamx FIFO control register */
  289. hdma->Instance->FCR = (uint32_t)0x00000021U;
  290. /* Get DMA steam Base Address */
  291. regs = (DMA_Base_Registers *)DMA_CalcBaseAndBitshift(hdma);
  292. /* Clear all interrupt flags at correct offset within the register */
  293. regs->IFCR = 0x3FU << hdma->StreamIndex;
  294. /* Clean all callbacks */
  295. hdma->XferCpltCallback = NULL;
  296. hdma->XferHalfCpltCallback = NULL;
  297. hdma->XferM1CpltCallback = NULL;
  298. hdma->XferM1HalfCpltCallback = NULL;
  299. hdma->XferErrorCallback = NULL;
  300. hdma->XferAbortCallback = NULL;
  301. /* Reset the error code */
  302. hdma->ErrorCode = HAL_DMA_ERROR_NONE;
  303. /* Reset the DMA state */
  304. hdma->State = HAL_DMA_STATE_RESET;
  305. /* Release Lock */
  306. __HAL_UNLOCK(hdma);
  307. return HAL_OK;
  308. }
  309. /**
  310. * @}
  311. */
  312. /** @addtogroup DMA_Exported_Functions_Group2
  313. *
  314. @verbatim
  315. ===============================================================================
  316. ##### IO operation functions #####
  317. ===============================================================================
  318. [..] This section provides functions allowing to:
  319. (+) Configure the source, destination address and data length and Start DMA transfer
  320. (+) Configure the source, destination address and data length and
  321. Start DMA transfer with interrupt
  322. (+) Abort DMA transfer
  323. (+) Poll for transfer complete
  324. (+) Handle DMA interrupt request
  325. @endverbatim
  326. * @{
  327. */
  328. /**
  329. * @brief Starts the DMA Transfer.
  330. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  331. * the configuration information for the specified DMA Stream.
  332. * @param SrcAddress The source memory Buffer address
  333. * @param DstAddress The destination memory Buffer address
  334. * @param DataLength The length of data to be transferred from source to destination
  335. * @retval HAL status
  336. */
  337. HAL_StatusTypeDef HAL_DMA_Start(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength)
  338. {
  339. HAL_StatusTypeDef status = HAL_OK;
  340. /* Check the parameters */
  341. assert_param(IS_DMA_BUFFER_SIZE(DataLength));
  342. /* Process locked */
  343. __HAL_LOCK(hdma);
  344. if(HAL_DMA_STATE_READY == hdma->State)
  345. {
  346. /* Change DMA peripheral state */
  347. hdma->State = HAL_DMA_STATE_BUSY;
  348. /* Initialize the error code */
  349. hdma->ErrorCode = HAL_DMA_ERROR_NONE;
  350. /* Configure the source, destination address and the data length */
  351. DMA_SetConfig(hdma, SrcAddress, DstAddress, DataLength);
  352. /* Enable the Peripheral */
  353. __HAL_DMA_ENABLE(hdma);
  354. }
  355. else
  356. {
  357. /* Process unlocked */
  358. __HAL_UNLOCK(hdma);
  359. /* Return error status */
  360. status = HAL_BUSY;
  361. }
  362. return status;
  363. }
  364. /**
  365. * @brief Start the DMA Transfer with interrupt enabled.
  366. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  367. * the configuration information for the specified DMA Stream.
  368. * @param SrcAddress The source memory Buffer address
  369. * @param DstAddress The destination memory Buffer address
  370. * @param DataLength The length of data to be transferred from source to destination
  371. * @retval HAL status
  372. */
  373. HAL_StatusTypeDef HAL_DMA_Start_IT(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength)
  374. {
  375. HAL_StatusTypeDef status = HAL_OK;
  376. /* calculate DMA base and stream number */
  377. DMA_Base_Registers *regs = (DMA_Base_Registers *)hdma->StreamBaseAddress;
  378. /* Check the parameters */
  379. assert_param(IS_DMA_BUFFER_SIZE(DataLength));
  380. /* Process locked */
  381. __HAL_LOCK(hdma);
  382. if(HAL_DMA_STATE_READY == hdma->State)
  383. {
  384. /* Change DMA peripheral state */
  385. hdma->State = HAL_DMA_STATE_BUSY;
  386. /* Initialize the error code */
  387. hdma->ErrorCode = HAL_DMA_ERROR_NONE;
  388. /* Configure the source, destination address and the data length */
  389. DMA_SetConfig(hdma, SrcAddress, DstAddress, DataLength);
  390. /* Clear all interrupt flags at correct offset within the register */
  391. regs->IFCR = 0x3FU << hdma->StreamIndex;
  392. /* Enable Common interrupts*/
  393. hdma->Instance->CR |= DMA_IT_TC | DMA_IT_TE | DMA_IT_DME;
  394. hdma->Instance->FCR |= DMA_IT_FE;
  395. if(hdma->XferHalfCpltCallback != NULL)
  396. {
  397. hdma->Instance->CR |= DMA_IT_HT;
  398. }
  399. /* Enable the Peripheral */
  400. __HAL_DMA_ENABLE(hdma);
  401. }
  402. else
  403. {
  404. /* Process unlocked */
  405. __HAL_UNLOCK(hdma);
  406. /* Return error status */
  407. status = HAL_BUSY;
  408. }
  409. return status;
  410. }
  411. /**
  412. * @brief Aborts the DMA Transfer.
  413. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  414. * the configuration information for the specified DMA Stream.
  415. *
  416. * @note After disabling a DMA Stream, a check for wait until the DMA Stream is
  417. * effectively disabled is added. If a Stream is disabled
  418. * while a data transfer is ongoing, the current data will be transferred
  419. * and the Stream will be effectively disabled only after the transfer of
  420. * this single data is finished.
  421. * @retval HAL status
  422. */
  423. HAL_StatusTypeDef HAL_DMA_Abort(DMA_HandleTypeDef *hdma)
  424. {
  425. /* calculate DMA base and stream number */
  426. DMA_Base_Registers *regs = (DMA_Base_Registers *)hdma->StreamBaseAddress;
  427. uint32_t tickstart = HAL_GetTick();
  428. if(hdma->State != HAL_DMA_STATE_BUSY)
  429. {
  430. hdma->ErrorCode = HAL_DMA_ERROR_NO_XFER;
  431. /* Process Unlocked */
  432. __HAL_UNLOCK(hdma);
  433. return HAL_ERROR;
  434. }
  435. else
  436. {
  437. /* Disable all the transfer interrupts */
  438. hdma->Instance->CR &= ~(DMA_IT_TC | DMA_IT_TE | DMA_IT_DME);
  439. hdma->Instance->FCR &= ~(DMA_IT_FE);
  440. if((hdma->XferHalfCpltCallback != NULL) || (hdma->XferM1HalfCpltCallback != NULL))
  441. {
  442. hdma->Instance->CR &= ~(DMA_IT_HT);
  443. }
  444. /* Disable the stream */
  445. __HAL_DMA_DISABLE(hdma);
  446. /* Check if the DMA Stream is effectively disabled */
  447. while((hdma->Instance->CR & DMA_SxCR_EN) != RESET)
  448. {
  449. /* Check for the Timeout */
  450. if((HAL_GetTick() - tickstart ) > HAL_TIMEOUT_DMA_ABORT)
  451. {
  452. /* Update error code */
  453. hdma->ErrorCode = HAL_DMA_ERROR_TIMEOUT;
  454. /* Change the DMA state */
  455. hdma->State = HAL_DMA_STATE_TIMEOUT;
  456. /* Process Unlocked */
  457. __HAL_UNLOCK(hdma);
  458. return HAL_TIMEOUT;
  459. }
  460. }
  461. /* Clear all interrupt flags at correct offset within the register */
  462. regs->IFCR = 0x3FU << hdma->StreamIndex;
  463. /* Change the DMA state*/
  464. hdma->State = HAL_DMA_STATE_READY;
  465. /* Process Unlocked */
  466. __HAL_UNLOCK(hdma);
  467. }
  468. return HAL_OK;
  469. }
  470. /**
  471. * @brief Aborts the DMA Transfer in Interrupt mode.
  472. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  473. * the configuration information for the specified DMA Stream.
  474. * @retval HAL status
  475. */
  476. HAL_StatusTypeDef HAL_DMA_Abort_IT(DMA_HandleTypeDef *hdma)
  477. {
  478. if(hdma->State != HAL_DMA_STATE_BUSY)
  479. {
  480. hdma->ErrorCode = HAL_DMA_ERROR_NO_XFER;
  481. return HAL_ERROR;
  482. }
  483. else
  484. {
  485. /* Set Abort State */
  486. hdma->State = HAL_DMA_STATE_ABORT;
  487. /* Disable the stream */
  488. __HAL_DMA_DISABLE(hdma);
  489. }
  490. return HAL_OK;
  491. }
  492. /**
  493. * @brief Polling for transfer complete.
  494. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  495. * the configuration information for the specified DMA Stream.
  496. * @param CompleteLevel Specifies the DMA level complete.
  497. * @note The polling mode is kept in this version for legacy. it is recommended to use the IT model instead.
  498. * This model could be used for debug purpose.
  499. * @note The HAL_DMA_PollForTransfer API cannot be used in circular and double buffering mode (automatic circular mode).
  500. * @param Timeout Timeout duration.
  501. * @retval HAL status
  502. */
  503. HAL_StatusTypeDef HAL_DMA_PollForTransfer(DMA_HandleTypeDef *hdma, HAL_DMA_LevelCompleteTypeDef CompleteLevel, uint32_t Timeout)
  504. {
  505. HAL_StatusTypeDef status = HAL_OK;
  506. uint32_t mask_cpltlevel;
  507. uint32_t tickstart = HAL_GetTick();
  508. uint32_t tmpisr;
  509. /* calculate DMA base and stream number */
  510. DMA_Base_Registers *regs;
  511. if(HAL_DMA_STATE_BUSY != hdma->State)
  512. {
  513. /* No transfer ongoing */
  514. hdma->ErrorCode = HAL_DMA_ERROR_NO_XFER;
  515. __HAL_UNLOCK(hdma);
  516. return HAL_ERROR;
  517. }
  518. /* Polling mode not supported in circular mode and double buffering mode */
  519. if ((hdma->Instance->CR & DMA_SxCR_CIRC) != RESET)
  520. {
  521. hdma->ErrorCode = HAL_DMA_ERROR_NOT_SUPPORTED;
  522. return HAL_ERROR;
  523. }
  524. /* Get the level transfer complete flag */
  525. if(CompleteLevel == HAL_DMA_FULL_TRANSFER)
  526. {
  527. /* Transfer Complete flag */
  528. mask_cpltlevel = DMA_FLAG_TCIF0_4 << hdma->StreamIndex;
  529. }
  530. else
  531. {
  532. /* Half Transfer Complete flag */
  533. mask_cpltlevel = DMA_FLAG_HTIF0_4 << hdma->StreamIndex;
  534. }
  535. regs = (DMA_Base_Registers *)hdma->StreamBaseAddress;
  536. tmpisr = regs->ISR;
  537. while(((tmpisr & mask_cpltlevel) == RESET) && ((hdma->ErrorCode & HAL_DMA_ERROR_TE) == RESET))
  538. {
  539. /* Check for the Timeout (Not applicable in circular mode)*/
  540. if(Timeout != HAL_MAX_DELAY)
  541. {
  542. if((Timeout == 0)||((HAL_GetTick() - tickstart ) > Timeout))
  543. {
  544. /* Update error code */
  545. hdma->ErrorCode = HAL_DMA_ERROR_TIMEOUT;
  546. /* Change the DMA state */
  547. hdma->State = HAL_DMA_STATE_READY;
  548. /* Process Unlocked */
  549. __HAL_UNLOCK(hdma);
  550. return HAL_TIMEOUT;
  551. }
  552. }
  553. /* Get the ISR register value */
  554. tmpisr = regs->ISR;
  555. if((tmpisr & (DMA_FLAG_TEIF0_4 << hdma->StreamIndex)) != RESET)
  556. {
  557. /* Update error code */
  558. hdma->ErrorCode |= HAL_DMA_ERROR_TE;
  559. /* Clear the transfer error flag */
  560. regs->IFCR = DMA_FLAG_TEIF0_4 << hdma->StreamIndex;
  561. }
  562. if((tmpisr & (DMA_FLAG_FEIF0_4 << hdma->StreamIndex)) != RESET)
  563. {
  564. /* Update error code */
  565. hdma->ErrorCode |= HAL_DMA_ERROR_FE;
  566. /* Clear the FIFO error flag */
  567. regs->IFCR = DMA_FLAG_FEIF0_4 << hdma->StreamIndex;
  568. }
  569. if((tmpisr & (DMA_FLAG_DMEIF0_4 << hdma->StreamIndex)) != RESET)
  570. {
  571. /* Update error code */
  572. hdma->ErrorCode |= HAL_DMA_ERROR_DME;
  573. /* Clear the Direct Mode error flag */
  574. regs->IFCR = DMA_FLAG_DMEIF0_4 << hdma->StreamIndex;
  575. }
  576. }
  577. if(hdma->ErrorCode != HAL_DMA_ERROR_NONE)
  578. {
  579. if((hdma->ErrorCode & HAL_DMA_ERROR_TE) != RESET)
  580. {
  581. HAL_DMA_Abort(hdma);
  582. /* Clear the half transfer and transfer complete flags */
  583. regs->IFCR = (DMA_FLAG_HTIF0_4 | DMA_FLAG_TCIF0_4) << hdma->StreamIndex;
  584. /* Change the DMA state */
  585. hdma->State= HAL_DMA_STATE_READY;
  586. /* Process Unlocked */
  587. __HAL_UNLOCK(hdma);
  588. return HAL_ERROR;
  589. }
  590. }
  591. /* Get the level transfer complete flag */
  592. if(CompleteLevel == HAL_DMA_FULL_TRANSFER)
  593. {
  594. /* Clear the half transfer and transfer complete flags */
  595. regs->IFCR = (DMA_FLAG_HTIF0_4 | DMA_FLAG_TCIF0_4) << hdma->StreamIndex;
  596. hdma->State = HAL_DMA_STATE_READY;
  597. /* Process Unlocked */
  598. __HAL_UNLOCK(hdma);
  599. }
  600. else
  601. {
  602. /* Clear the half transfer flag */
  603. regs->IFCR = (DMA_FLAG_HTIF0_4) << hdma->StreamIndex;
  604. }
  605. return status;
  606. }
  607. /**
  608. * @brief Handles DMA interrupt request.
  609. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  610. * the configuration information for the specified DMA Stream.
  611. * @retval None
  612. */
  613. void HAL_DMA_IRQHandler(DMA_HandleTypeDef *hdma)
  614. {
  615. uint32_t tmpisr;
  616. __IO uint32_t count = 0;
  617. uint32_t timeout = SystemCoreClock / 9600;
  618. /* calculate DMA base and stream number */
  619. DMA_Base_Registers *regs = (DMA_Base_Registers *)hdma->StreamBaseAddress;
  620. tmpisr = regs->ISR;
  621. /* Transfer Error Interrupt management ***************************************/
  622. if ((tmpisr & (DMA_FLAG_TEIF0_4 << hdma->StreamIndex)) != RESET)
  623. {
  624. if(__HAL_DMA_GET_IT_SOURCE(hdma, DMA_IT_TE) != RESET)
  625. {
  626. /* Disable the transfer error interrupt */
  627. hdma->Instance->CR &= ~(DMA_IT_TE);
  628. /* Clear the transfer error flag */
  629. regs->IFCR = DMA_FLAG_TEIF0_4 << hdma->StreamIndex;
  630. /* Update error code */
  631. hdma->ErrorCode |= HAL_DMA_ERROR_TE;
  632. }
  633. }
  634. /* FIFO Error Interrupt management ******************************************/
  635. if ((tmpisr & (DMA_FLAG_FEIF0_4 << hdma->StreamIndex)) != RESET)
  636. {
  637. if(__HAL_DMA_GET_IT_SOURCE(hdma, DMA_IT_FE) != RESET)
  638. {
  639. /* Clear the FIFO error flag */
  640. regs->IFCR = DMA_FLAG_FEIF0_4 << hdma->StreamIndex;
  641. /* Update error code */
  642. hdma->ErrorCode |= HAL_DMA_ERROR_FE;
  643. }
  644. }
  645. /* Direct Mode Error Interrupt management ***********************************/
  646. if ((tmpisr & (DMA_FLAG_DMEIF0_4 << hdma->StreamIndex)) != RESET)
  647. {
  648. if(__HAL_DMA_GET_IT_SOURCE(hdma, DMA_IT_DME) != RESET)
  649. {
  650. /* Clear the direct mode error flag */
  651. regs->IFCR = DMA_FLAG_DMEIF0_4 << hdma->StreamIndex;
  652. /* Update error code */
  653. hdma->ErrorCode |= HAL_DMA_ERROR_DME;
  654. }
  655. }
  656. /* Half Transfer Complete Interrupt management ******************************/
  657. if ((tmpisr & (DMA_FLAG_HTIF0_4 << hdma->StreamIndex)) != RESET)
  658. {
  659. if(__HAL_DMA_GET_IT_SOURCE(hdma, DMA_IT_HT) != RESET)
  660. {
  661. /* Clear the half transfer complete flag */
  662. regs->IFCR = DMA_FLAG_HTIF0_4 << hdma->StreamIndex;
  663. /* Multi_Buffering mode enabled */
  664. if(((hdma->Instance->CR) & (uint32_t)(DMA_SxCR_DBM)) != RESET)
  665. {
  666. /* Current memory buffer used is Memory 0 */
  667. if((hdma->Instance->CR & DMA_SxCR_CT) == RESET)
  668. {
  669. if(hdma->XferHalfCpltCallback != NULL)
  670. {
  671. /* Half transfer callback */
  672. hdma->XferHalfCpltCallback(hdma);
  673. }
  674. }
  675. /* Current memory buffer used is Memory 1 */
  676. else
  677. {
  678. if(hdma->XferM1HalfCpltCallback != NULL)
  679. {
  680. /* Half transfer callback */
  681. hdma->XferM1HalfCpltCallback(hdma);
  682. }
  683. }
  684. }
  685. else
  686. {
  687. /* Disable the half transfer interrupt if the DMA mode is not CIRCULAR */
  688. if((hdma->Instance->CR & DMA_SxCR_CIRC) == RESET)
  689. {
  690. /* Disable the half transfer interrupt */
  691. hdma->Instance->CR &= ~(DMA_IT_HT);
  692. }
  693. if(hdma->XferHalfCpltCallback != NULL)
  694. {
  695. /* Half transfer callback */
  696. hdma->XferHalfCpltCallback(hdma);
  697. }
  698. }
  699. }
  700. }
  701. /* Transfer Complete Interrupt management ***********************************/
  702. if ((tmpisr & (DMA_FLAG_TCIF0_4 << hdma->StreamIndex)) != RESET)
  703. {
  704. if(__HAL_DMA_GET_IT_SOURCE(hdma, DMA_IT_TC) != RESET)
  705. {
  706. /* Clear the transfer complete flag */
  707. regs->IFCR = DMA_FLAG_TCIF0_4 << hdma->StreamIndex;
  708. if(HAL_DMA_STATE_ABORT == hdma->State)
  709. {
  710. /* Disable all the transfer interrupts */
  711. hdma->Instance->CR &= ~(DMA_IT_TC | DMA_IT_TE | DMA_IT_DME);
  712. hdma->Instance->FCR &= ~(DMA_IT_FE);
  713. if((hdma->XferHalfCpltCallback != NULL) || (hdma->XferM1HalfCpltCallback != NULL))
  714. {
  715. hdma->Instance->CR &= ~(DMA_IT_HT);
  716. }
  717. /* Clear all interrupt flags at correct offset within the register */
  718. regs->IFCR = 0x3FU << hdma->StreamIndex;
  719. /* Change the DMA state */
  720. hdma->State = HAL_DMA_STATE_READY;
  721. /* Process Unlocked */
  722. __HAL_UNLOCK(hdma);
  723. if(hdma->XferAbortCallback != NULL)
  724. {
  725. hdma->XferAbortCallback(hdma);
  726. }
  727. return;
  728. }
  729. if(((hdma->Instance->CR) & (uint32_t)(DMA_SxCR_DBM)) != RESET)
  730. {
  731. /* Current memory buffer used is Memory 0 */
  732. if((hdma->Instance->CR & DMA_SxCR_CT) == RESET)
  733. {
  734. if(hdma->XferM1CpltCallback != NULL)
  735. {
  736. /* Transfer complete Callback for memory1 */
  737. hdma->XferM1CpltCallback(hdma);
  738. }
  739. }
  740. /* Current memory buffer used is Memory 1 */
  741. else
  742. {
  743. if(hdma->XferCpltCallback != NULL)
  744. {
  745. /* Transfer complete Callback for memory0 */
  746. hdma->XferCpltCallback(hdma);
  747. }
  748. }
  749. }
  750. /* Disable the transfer complete interrupt if the DMA mode is not CIRCULAR */
  751. else
  752. {
  753. if((hdma->Instance->CR & DMA_SxCR_CIRC) == RESET)
  754. {
  755. /* Disable the transfer complete interrupt */
  756. hdma->Instance->CR &= ~(DMA_IT_TC);
  757. /* Change the DMA state */
  758. hdma->State = HAL_DMA_STATE_READY;
  759. /* Process Unlocked */
  760. __HAL_UNLOCK(hdma);
  761. }
  762. if(hdma->XferCpltCallback != NULL)
  763. {
  764. /* Transfer complete callback */
  765. hdma->XferCpltCallback(hdma);
  766. }
  767. }
  768. }
  769. }
  770. /* manage error case */
  771. if(hdma->ErrorCode != HAL_DMA_ERROR_NONE)
  772. {
  773. if((hdma->ErrorCode & HAL_DMA_ERROR_TE) != RESET)
  774. {
  775. hdma->State = HAL_DMA_STATE_ABORT;
  776. /* Disable the stream */
  777. __HAL_DMA_DISABLE(hdma);
  778. do
  779. {
  780. if (++count > timeout)
  781. {
  782. break;
  783. }
  784. }
  785. while((hdma->Instance->CR & DMA_SxCR_EN) != RESET);
  786. /* Change the DMA state */
  787. hdma->State = HAL_DMA_STATE_READY;
  788. /* Process Unlocked */
  789. __HAL_UNLOCK(hdma);
  790. }
  791. if(hdma->XferErrorCallback != NULL)
  792. {
  793. /* Transfer error callback */
  794. hdma->XferErrorCallback(hdma);
  795. }
  796. }
  797. }
  798. /**
  799. * @brief Register callbacks
  800. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  801. * the configuration information for the specified DMA Stream.
  802. * @param CallbackID User Callback identifier
  803. * a DMA_HandleTypeDef structure as parameter.
  804. * @param pCallback pointer to private callbacsk function which has pointer to
  805. * a DMA_HandleTypeDef structure as parameter.
  806. * @retval HAL status
  807. */
  808. HAL_StatusTypeDef HAL_DMA_RegisterCallback(DMA_HandleTypeDef *hdma, HAL_DMA_CallbackIDTypeDef CallbackID, void (* pCallback)(DMA_HandleTypeDef *_hdma))
  809. {
  810. HAL_StatusTypeDef status = HAL_OK;
  811. /* Process locked */
  812. __HAL_LOCK(hdma);
  813. if(HAL_DMA_STATE_READY == hdma->State)
  814. {
  815. switch (CallbackID)
  816. {
  817. case HAL_DMA_XFER_CPLT_CB_ID:
  818. hdma->XferCpltCallback = pCallback;
  819. break;
  820. case HAL_DMA_XFER_HALFCPLT_CB_ID:
  821. hdma->XferHalfCpltCallback = pCallback;
  822. break;
  823. case HAL_DMA_XFER_M1CPLT_CB_ID:
  824. hdma->XferM1CpltCallback = pCallback;
  825. break;
  826. case HAL_DMA_XFER_M1HALFCPLT_CB_ID:
  827. hdma->XferM1HalfCpltCallback = pCallback;
  828. break;
  829. case HAL_DMA_XFER_ERROR_CB_ID:
  830. hdma->XferErrorCallback = pCallback;
  831. break;
  832. case HAL_DMA_XFER_ABORT_CB_ID:
  833. hdma->XferAbortCallback = pCallback;
  834. break;
  835. default:
  836. /* Return error status */
  837. status = HAL_ERROR;
  838. break;
  839. }
  840. }
  841. else
  842. {
  843. /* Return error status */
  844. status = HAL_ERROR;
  845. }
  846. /* Release Lock */
  847. __HAL_UNLOCK(hdma);
  848. return status;
  849. }
  850. /**
  851. * @brief UnRegister callbacks
  852. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  853. * the configuration information for the specified DMA Stream.
  854. * @param CallbackID User Callback identifier
  855. * a HAL_DMA_CallbackIDTypeDef ENUM as parameter.
  856. * @retval HAL status
  857. */
  858. HAL_StatusTypeDef HAL_DMA_UnRegisterCallback(DMA_HandleTypeDef *hdma, HAL_DMA_CallbackIDTypeDef CallbackID)
  859. {
  860. HAL_StatusTypeDef status = HAL_OK;
  861. /* Process locked */
  862. __HAL_LOCK(hdma);
  863. if(HAL_DMA_STATE_READY == hdma->State)
  864. {
  865. switch (CallbackID)
  866. {
  867. case HAL_DMA_XFER_CPLT_CB_ID:
  868. hdma->XferCpltCallback = NULL;
  869. break;
  870. case HAL_DMA_XFER_HALFCPLT_CB_ID:
  871. hdma->XferHalfCpltCallback = NULL;
  872. break;
  873. case HAL_DMA_XFER_M1CPLT_CB_ID:
  874. hdma->XferM1CpltCallback = NULL;
  875. break;
  876. case HAL_DMA_XFER_M1HALFCPLT_CB_ID:
  877. hdma->XferM1HalfCpltCallback = NULL;
  878. break;
  879. case HAL_DMA_XFER_ERROR_CB_ID:
  880. hdma->XferErrorCallback = NULL;
  881. break;
  882. case HAL_DMA_XFER_ABORT_CB_ID:
  883. hdma->XferAbortCallback = NULL;
  884. break;
  885. case HAL_DMA_XFER_ALL_CB_ID:
  886. hdma->XferCpltCallback = NULL;
  887. hdma->XferHalfCpltCallback = NULL;
  888. hdma->XferM1CpltCallback = NULL;
  889. hdma->XferM1HalfCpltCallback = NULL;
  890. hdma->XferErrorCallback = NULL;
  891. hdma->XferAbortCallback = NULL;
  892. break;
  893. default:
  894. status = HAL_ERROR;
  895. break;
  896. }
  897. }
  898. else
  899. {
  900. status = HAL_ERROR;
  901. }
  902. /* Release Lock */
  903. __HAL_UNLOCK(hdma);
  904. return status;
  905. }
  906. /**
  907. * @}
  908. */
  909. /** @addtogroup DMA_Exported_Functions_Group3
  910. *
  911. @verbatim
  912. ===============================================================================
  913. ##### State and Errors functions #####
  914. ===============================================================================
  915. [..]
  916. This subsection provides functions allowing to
  917. (+) Check the DMA state
  918. (+) Get error code
  919. @endverbatim
  920. * @{
  921. */
  922. /**
  923. * @brief Returns the DMA state.
  924. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  925. * the configuration information for the specified DMA Stream.
  926. * @retval HAL state
  927. */
  928. HAL_DMA_StateTypeDef HAL_DMA_GetState(DMA_HandleTypeDef *hdma)
  929. {
  930. return hdma->State;
  931. }
  932. /**
  933. * @brief Return the DMA error code
  934. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  935. * the configuration information for the specified DMA Stream.
  936. * @retval DMA Error Code
  937. */
  938. uint32_t HAL_DMA_GetError(DMA_HandleTypeDef *hdma)
  939. {
  940. return hdma->ErrorCode;
  941. }
  942. /**
  943. * @}
  944. */
  945. /**
  946. * @}
  947. */
  948. /** @addtogroup DMA_Private_Functions
  949. * @{
  950. */
  951. /**
  952. * @brief Sets the DMA Transfer parameter.
  953. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  954. * the configuration information for the specified DMA Stream.
  955. * @param SrcAddress The source memory Buffer address
  956. * @param DstAddress The destination memory Buffer address
  957. * @param DataLength The length of data to be transferred from source to destination
  958. * @retval HAL status
  959. */
  960. static void DMA_SetConfig(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t DataLength)
  961. {
  962. /* Clear DBM bit */
  963. hdma->Instance->CR &= (uint32_t)(~DMA_SxCR_DBM);
  964. /* Configure DMA Stream data length */
  965. hdma->Instance->NDTR = DataLength;
  966. /* Memory to Peripheral */
  967. if((hdma->Init.Direction) == DMA_MEMORY_TO_PERIPH)
  968. {
  969. /* Configure DMA Stream destination address */
  970. hdma->Instance->PAR = DstAddress;
  971. /* Configure DMA Stream source address */
  972. hdma->Instance->M0AR = SrcAddress;
  973. }
  974. /* Peripheral to Memory */
  975. else
  976. {
  977. /* Configure DMA Stream source address */
  978. hdma->Instance->PAR = SrcAddress;
  979. /* Configure DMA Stream destination address */
  980. hdma->Instance->M0AR = DstAddress;
  981. }
  982. }
  983. /**
  984. * @brief Returns the DMA Stream base address depending on stream number
  985. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  986. * the configuration information for the specified DMA Stream.
  987. * @retval Stream base address
  988. */
  989. static uint32_t DMA_CalcBaseAndBitshift(DMA_HandleTypeDef *hdma)
  990. {
  991. uint32_t stream_number = (((uint32_t)hdma->Instance & 0xFFU) - 16U) / 24U;
  992. /* lookup table for necessary bitshift of flags within status registers */
  993. static const uint8_t flagBitshiftOffset[8U] = {0U, 6U, 16U, 22U, 0U, 6U, 16U, 22U};
  994. hdma->StreamIndex = flagBitshiftOffset[stream_number];
  995. if (stream_number > 3U)
  996. {
  997. /* return pointer to HISR and HIFCR */
  998. hdma->StreamBaseAddress = (((uint32_t)hdma->Instance & (uint32_t)(~0x3FFU)) + 4U);
  999. }
  1000. else
  1001. {
  1002. /* return pointer to LISR and LIFCR */
  1003. hdma->StreamBaseAddress = ((uint32_t)hdma->Instance & (uint32_t)(~0x3FFU));
  1004. }
  1005. return hdma->StreamBaseAddress;
  1006. }
  1007. /**
  1008. * @brief Check compatibility between FIFO threshold level and size of the memory burst
  1009. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  1010. * the configuration information for the specified DMA Stream.
  1011. * @retval HAL status
  1012. */
  1013. static HAL_StatusTypeDef DMA_CheckFifoParam(DMA_HandleTypeDef *hdma)
  1014. {
  1015. HAL_StatusTypeDef status = HAL_OK;
  1016. uint32_t tmp = hdma->Init.FIFOThreshold;
  1017. /* Memory Data size equal to Byte */
  1018. if(hdma->Init.MemDataAlignment == DMA_MDATAALIGN_BYTE)
  1019. {
  1020. switch (tmp)
  1021. {
  1022. case DMA_FIFO_THRESHOLD_1QUARTERFULL:
  1023. case DMA_FIFO_THRESHOLD_3QUARTERSFULL:
  1024. if ((hdma->Init.MemBurst & DMA_SxCR_MBURST_1) == DMA_SxCR_MBURST_1)
  1025. {
  1026. status = HAL_ERROR;
  1027. }
  1028. break;
  1029. case DMA_FIFO_THRESHOLD_HALFFULL:
  1030. if (hdma->Init.MemBurst == DMA_MBURST_INC16)
  1031. {
  1032. status = HAL_ERROR;
  1033. }
  1034. break;
  1035. case DMA_FIFO_THRESHOLD_FULL:
  1036. break;
  1037. default:
  1038. break;
  1039. }
  1040. }
  1041. /* Memory Data size equal to Half-Word */
  1042. else if (hdma->Init.MemDataAlignment == DMA_MDATAALIGN_HALFWORD)
  1043. {
  1044. switch (tmp)
  1045. {
  1046. case DMA_FIFO_THRESHOLD_1QUARTERFULL:
  1047. case DMA_FIFO_THRESHOLD_3QUARTERSFULL:
  1048. status = HAL_ERROR;
  1049. break;
  1050. case DMA_FIFO_THRESHOLD_HALFFULL:
  1051. if ((hdma->Init.MemBurst & DMA_SxCR_MBURST_1) == DMA_SxCR_MBURST_1)
  1052. {
  1053. status = HAL_ERROR;
  1054. }
  1055. break;
  1056. case DMA_FIFO_THRESHOLD_FULL:
  1057. if (hdma->Init.MemBurst == DMA_MBURST_INC16)
  1058. {
  1059. status = HAL_ERROR;
  1060. }
  1061. break;
  1062. default:
  1063. break;
  1064. }
  1065. }
  1066. /* Memory Data size equal to Word */
  1067. else
  1068. {
  1069. switch (tmp)
  1070. {
  1071. case DMA_FIFO_THRESHOLD_1QUARTERFULL:
  1072. case DMA_FIFO_THRESHOLD_HALFFULL:
  1073. case DMA_FIFO_THRESHOLD_3QUARTERSFULL:
  1074. status = HAL_ERROR;
  1075. break;
  1076. case DMA_FIFO_THRESHOLD_FULL:
  1077. if ((hdma->Init.MemBurst & DMA_SxCR_MBURST_1) == DMA_SxCR_MBURST_1)
  1078. {
  1079. status = HAL_ERROR;
  1080. }
  1081. break;
  1082. default:
  1083. break;
  1084. }
  1085. }
  1086. return status;
  1087. }
  1088. /**
  1089. * @}
  1090. */
  1091. #endif /* HAL_DMA_MODULE_ENABLED */
  1092. /**
  1093. * @}
  1094. */
  1095. /**
  1096. * @}
  1097. */