stm32f4xx_hal_dma.c 40 KB

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