stm32f4xx_hal_dma.c 42 KB

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