stm32f2xx_dma.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281
  1. /**
  2. ******************************************************************************
  3. * @file stm32f2xx_dma.c
  4. * @author MCD Application Team
  5. * @version V1.1.3
  6. * @date 31-December-2021
  7. * @brief This file provides firmware functions to manage the following
  8. * functionalities of the Direct Memory Access controller (DMA):
  9. * - Initialization and Configuration
  10. * - Data Counter
  11. * - Double Buffer mode configuration and command
  12. * - Interrupts and flags management
  13. *
  14. * @verbatim
  15. *
  16. * ===================================================================
  17. * How to use this driver
  18. * ===================================================================
  19. * 1. Enable The DMA controller clock using RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_DMA1, ENABLE)
  20. * function for DMA1 or using RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_DMA2, ENABLE)
  21. * function for DMA2.
  22. *
  23. * 2. Enable and configure the peripheral to be connected to the DMA Stream
  24. * (except for internal SRAM / FLASH memories: no initialization is
  25. * necessary).
  26. *
  27. * 3. For a given Stream, program the required configuration through following parameters:
  28. * Source and Destination addresses, Transfer Direction, Transfer size, Source and Destination
  29. * data formats, Circular or Normal mode, Stream Priority level, Source and Destination
  30. * Incrementation mode, FIFO mode and its Threshold (if needed), Burst mode for Source and/or
  31. * Destination (if needed) using the DMA_Init() function.
  32. * To avoid filling un-nesecessary fields, you can call DMA_StructInit() function
  33. * to initialize a given structure with default values (reset values), the modify
  34. * only necessary fields (ie. Source and Destination addresses, Transfer size and Data Formats).
  35. *
  36. * 4. Enable the NVIC and the corresponding interrupt(s) using the function
  37. * DMA_ITConfig() if you need to use DMA interrupts.
  38. *
  39. * 5. Optionally, if the Circular mode is enabled, you can use the Double buffer mode by configuring
  40. * the second Memory address and the first Memory to be used through the function
  41. * DMA_DoubleBufferModeConfig(). Then enable the Double buffer mode through the function
  42. * DMA_DoubleBufferModeCmd(). These operations must be done before step 6.
  43. *
  44. * 6. Enable the DMA stream using the DMA_Cmd() function.
  45. *
  46. * 7. Activate the needed Stream Request using PPP_DMACmd() function for
  47. * any PPP peripheral except internal SRAM and FLASH (ie. SPI, USART ...)
  48. * The function allowing this operation is provided in each PPP peripheral
  49. * driver (ie. SPI_DMACmd for SPI peripheral).
  50. * Once the Stream is enabled, it is not possible to modify its configuration
  51. * unless the stream is stopped and disabled.
  52. * After enabling the Stream, it is advised to monitor the EN bit status using
  53. * the function DMA_GetCmdStatus(). In case of configuration errors or bus errors
  54. * this bit will remain reset and all transfers on this Stream will remain on hold.
  55. *
  56. * 8. Optionally, you can configure the number of data to be transferred
  57. * when the Stream is disabled (ie. after each Transfer Complete event
  58. * or when a Transfer Error occurs) using the function DMA_SetCurrDataCounter().
  59. * And you can get the number of remaining data to be transferred using
  60. * the function DMA_GetCurrDataCounter() at run time (when the DMA Stream is
  61. * enabled and running).
  62. *
  63. * 9. To control DMA events you can use one of the following
  64. * two methods:
  65. * a- Check on DMA Stream flags using the function DMA_GetFlagStatus().
  66. * b- Use DMA interrupts through the function DMA_ITConfig() at initialization
  67. * phase and DMA_GetITStatus() function into interrupt routines in
  68. * communication phase.
  69. * After checking on a flag you should clear it using DMA_ClearFlag()
  70. * function. And after checking on an interrupt event you should
  71. * clear it using DMA_ClearITPendingBit() function.
  72. *
  73. * 10. Optionally, if Circular mode and Double Buffer mode are enabled, you can modify
  74. * the Memory Addresses using the function DMA_MemoryTargetConfig(). Make sure that
  75. * the Memory Address to be modified is not the one currently in use by DMA Stream.
  76. * This condition can be monitored using the function DMA_GetCurrentMemoryTarget().
  77. *
  78. * 11. Optionally, Pause-Resume operations may be performed:
  79. * The DMA_Cmd() function may be used to perform Pause-Resume operation. When a
  80. * transfer is ongoing, calling this function to disable the Stream will cause the
  81. * transfer to be paused. All configuration registers and the number of remaining
  82. * data will be preserved. When calling again this function to re-enable the Stream,
  83. * the transfer will be resumed from the point where it was paused.
  84. *
  85. * @note Memory-to-Memory transfer is possible by setting the address of the memory into
  86. * the Peripheral registers. In this mode, Circular mode and Double Buffer mode
  87. * are not allowed.
  88. *
  89. * @note The FIFO is used mainly to reduce bus usage and to allow data packing/unpacking: it is
  90. * possible to set different Data Sizes for the Peripheral and the Memory (ie. you can set
  91. * Half-Word data size for the peripheral to access its data register and set Word data size
  92. * for the Memory to gain in access time. Each two Half-words will be packed and written in
  93. * a single access to a Word in the Memory).
  94. *
  95. * @note When FIFO is disabled, it is not allowed to configure different Data Sizes for Source
  96. * and Destination. In this case the Peripheral Data Size will be applied to both Source
  97. * and Destination.
  98. *
  99. * @endverbatim
  100. *
  101. ******************************************************************************
  102. * @attention
  103. *
  104. * Copyright (c) 2012 STMicroelectronics.
  105. * All rights reserved.
  106. *
  107. * This software is licensed under terms that can be found in the LICENSE file
  108. * in the root directory of this software component.
  109. * If no LICENSE file comes with this software, it is provided AS-IS.
  110. *
  111. ******************************************************************************
  112. */
  113. /* Includes ------------------------------------------------------------------*/
  114. #include "stm32f2xx_dma.h"
  115. #include "stm32f2xx_rcc.h"
  116. /** @addtogroup STM32F2xx_StdPeriph_Driver
  117. * @{
  118. */
  119. /** @defgroup DMA
  120. * @brief DMA driver modules
  121. * @{
  122. */
  123. /* Private typedef -----------------------------------------------------------*/
  124. /* Private define ------------------------------------------------------------*/
  125. /* Masks Definition */
  126. #define TRANSFER_IT_ENABLE_MASK (uint32_t)(DMA_SxCR_TCIE | DMA_SxCR_HTIE | \
  127. DMA_SxCR_TEIE | DMA_SxCR_DMEIE)
  128. #define DMA_Stream0_IT_MASK (uint32_t)(DMA_LISR_FEIF0 | DMA_LISR_DMEIF0 | \
  129. DMA_LISR_TEIF0 | DMA_LISR_HTIF0 | \
  130. DMA_LISR_TCIF0)
  131. #define DMA_Stream1_IT_MASK (uint32_t)(DMA_Stream0_IT_MASK << 6)
  132. #define DMA_Stream2_IT_MASK (uint32_t)(DMA_Stream0_IT_MASK << 16)
  133. #define DMA_Stream3_IT_MASK (uint32_t)(DMA_Stream0_IT_MASK << 22)
  134. #define DMA_Stream4_IT_MASK (uint32_t)(DMA_Stream0_IT_MASK | (uint32_t)0x20000000)
  135. #define DMA_Stream5_IT_MASK (uint32_t)(DMA_Stream1_IT_MASK | (uint32_t)0x20000000)
  136. #define DMA_Stream6_IT_MASK (uint32_t)(DMA_Stream2_IT_MASK | (uint32_t)0x20000000)
  137. #define DMA_Stream7_IT_MASK (uint32_t)(DMA_Stream3_IT_MASK | (uint32_t)0x20000000)
  138. #define TRANSFER_IT_MASK (uint32_t)0x0F3C0F3C
  139. #define HIGH_ISR_MASK (uint32_t)0x20000000
  140. #define RESERVED_MASK (uint32_t)0x0F7D0F7D
  141. /* Private macro -------------------------------------------------------------*/
  142. /* Private variables ---------------------------------------------------------*/
  143. /* Private function prototypes -----------------------------------------------*/
  144. /* Private functions ---------------------------------------------------------*/
  145. /** @defgroup DMA_Private_Functions
  146. * @{
  147. */
  148. /** @defgroup DMA_Group1 Initialization and Configuration functions
  149. * @brief Initialization and Configuration functions
  150. *
  151. @verbatim
  152. ===============================================================================
  153. Initialization and Configuration functions
  154. ===============================================================================
  155. This subsection provides functions allowing to initialize the DMA Stream source
  156. and destination addresses, incrementation and data sizes, transfer direction,
  157. buffer size, circular/normal mode selection, memory-to-memory mode selection
  158. and Stream priority value.
  159. The DMA_Init() function follows the DMA configuration procedures as described in
  160. reference manual (RM0033) except the first point: waiting on EN bit to be reset.
  161. This condition should be checked by user application using the function DMA_GetCmdStatus()
  162. before calling the DMA_Init() function.
  163. @endverbatim
  164. * @{
  165. */
  166. /**
  167. * @brief Deinitialize the DMAy Streamx registers to their default reset values.
  168. * @param DMAy_Streamx: where y can be 1 or 2 to select the DMA and x can be 0
  169. * to 7 to select the DMA Stream.
  170. * @retval None
  171. */
  172. void DMA_DeInit(DMA_Stream_TypeDef* DMAy_Streamx)
  173. {
  174. /* Check the parameters */
  175. assert_param(IS_DMA_ALL_PERIPH(DMAy_Streamx));
  176. /* Disable the selected DMAy Streamx */
  177. DMAy_Streamx->CR &= ~((uint32_t)DMA_SxCR_EN);
  178. /* Reset DMAy Streamx control register */
  179. DMAy_Streamx->CR = 0;
  180. /* Reset DMAy Streamx Number of Data to Transfer register */
  181. DMAy_Streamx->NDTR = 0;
  182. /* Reset DMAy Streamx peripheral address register */
  183. DMAy_Streamx->PAR = 0;
  184. /* Reset DMAy Streamx memory 0 address register */
  185. DMAy_Streamx->M0AR = 0;
  186. /* Reset DMAy Streamx memory 1 address register */
  187. DMAy_Streamx->M1AR = 0;
  188. /* Reset DMAy Streamx FIFO control register */
  189. DMAy_Streamx->FCR = (uint32_t)0x00000021;
  190. /* Reset interrupt pending bits for the selected stream */
  191. if (DMAy_Streamx == DMA1_Stream0)
  192. {
  193. /* Reset interrupt pending bits for DMA1 Stream0 */
  194. DMA1->LIFCR = DMA_Stream0_IT_MASK;
  195. }
  196. else if (DMAy_Streamx == DMA1_Stream1)
  197. {
  198. /* Reset interrupt pending bits for DMA1 Stream1 */
  199. DMA1->LIFCR = DMA_Stream1_IT_MASK;
  200. }
  201. else if (DMAy_Streamx == DMA1_Stream2)
  202. {
  203. /* Reset interrupt pending bits for DMA1 Stream2 */
  204. DMA1->LIFCR = DMA_Stream2_IT_MASK;
  205. }
  206. else if (DMAy_Streamx == DMA1_Stream3)
  207. {
  208. /* Reset interrupt pending bits for DMA1 Stream3 */
  209. DMA1->LIFCR = DMA_Stream3_IT_MASK;
  210. }
  211. else if (DMAy_Streamx == DMA1_Stream4)
  212. {
  213. /* Reset interrupt pending bits for DMA1 Stream4 */
  214. DMA1->HIFCR = DMA_Stream4_IT_MASK;
  215. }
  216. else if (DMAy_Streamx == DMA1_Stream5)
  217. {
  218. /* Reset interrupt pending bits for DMA1 Stream5 */
  219. DMA1->HIFCR = DMA_Stream5_IT_MASK;
  220. }
  221. else if (DMAy_Streamx == DMA1_Stream6)
  222. {
  223. /* Reset interrupt pending bits for DMA1 Stream6 */
  224. DMA1->HIFCR = (uint32_t)DMA_Stream6_IT_MASK;
  225. }
  226. else if (DMAy_Streamx == DMA1_Stream7)
  227. {
  228. /* Reset interrupt pending bits for DMA1 Stream7 */
  229. DMA1->HIFCR = DMA_Stream7_IT_MASK;
  230. }
  231. else if (DMAy_Streamx == DMA2_Stream0)
  232. {
  233. /* Reset interrupt pending bits for DMA2 Stream0 */
  234. DMA2->LIFCR = DMA_Stream0_IT_MASK;
  235. }
  236. else if (DMAy_Streamx == DMA2_Stream1)
  237. {
  238. /* Reset interrupt pending bits for DMA2 Stream1 */
  239. DMA2->LIFCR = DMA_Stream1_IT_MASK;
  240. }
  241. else if (DMAy_Streamx == DMA2_Stream2)
  242. {
  243. /* Reset interrupt pending bits for DMA2 Stream2 */
  244. DMA2->LIFCR = DMA_Stream2_IT_MASK;
  245. }
  246. else if (DMAy_Streamx == DMA2_Stream3)
  247. {
  248. /* Reset interrupt pending bits for DMA2 Stream3 */
  249. DMA2->LIFCR = DMA_Stream3_IT_MASK;
  250. }
  251. else if (DMAy_Streamx == DMA2_Stream4)
  252. {
  253. /* Reset interrupt pending bits for DMA2 Stream4 */
  254. DMA2->HIFCR = DMA_Stream4_IT_MASK;
  255. }
  256. else if (DMAy_Streamx == DMA2_Stream5)
  257. {
  258. /* Reset interrupt pending bits for DMA2 Stream5 */
  259. DMA2->HIFCR = DMA_Stream5_IT_MASK;
  260. }
  261. else if (DMAy_Streamx == DMA2_Stream6)
  262. {
  263. /* Reset interrupt pending bits for DMA2 Stream6 */
  264. DMA2->HIFCR = DMA_Stream6_IT_MASK;
  265. }
  266. else
  267. {
  268. if (DMAy_Streamx == DMA2_Stream7)
  269. {
  270. /* Reset interrupt pending bits for DMA2 Stream7 */
  271. DMA2->HIFCR = DMA_Stream7_IT_MASK;
  272. }
  273. }
  274. }
  275. /**
  276. * @brief Initializes the DMAy Streamx according to the specified parameters in
  277. * the DMA_InitStruct structure.
  278. * @note Before calling this function, it is recommended to check that the Stream
  279. * is actually disabled using the function DMA_GetCmdStatus().
  280. * @param DMAy_Streamx: where y can be 1 or 2 to select the DMA and x can be 0
  281. * to 7 to select the DMA Stream.
  282. * @param DMA_InitStruct: pointer to a DMA_InitTypeDef structure that contains
  283. * the configuration information for the specified DMA Stream.
  284. * @retval None
  285. */
  286. void DMA_Init(DMA_Stream_TypeDef* DMAy_Streamx, DMA_InitTypeDef* DMA_InitStruct)
  287. {
  288. uint32_t tmpreg = 0;
  289. /* Check the parameters */
  290. assert_param(IS_DMA_ALL_PERIPH(DMAy_Streamx));
  291. assert_param(IS_DMA_CHANNEL(DMA_InitStruct->DMA_Channel));
  292. assert_param(IS_DMA_DIRECTION(DMA_InitStruct->DMA_DIR));
  293. assert_param(IS_DMA_BUFFER_SIZE(DMA_InitStruct->DMA_BufferSize));
  294. assert_param(IS_DMA_PERIPHERAL_INC_STATE(DMA_InitStruct->DMA_PeripheralInc));
  295. assert_param(IS_DMA_MEMORY_INC_STATE(DMA_InitStruct->DMA_MemoryInc));
  296. assert_param(IS_DMA_PERIPHERAL_DATA_SIZE(DMA_InitStruct->DMA_PeripheralDataSize));
  297. assert_param(IS_DMA_MEMORY_DATA_SIZE(DMA_InitStruct->DMA_MemoryDataSize));
  298. assert_param(IS_DMA_MODE(DMA_InitStruct->DMA_Mode));
  299. assert_param(IS_DMA_PRIORITY(DMA_InitStruct->DMA_Priority));
  300. assert_param(IS_DMA_FIFO_MODE_STATE(DMA_InitStruct->DMA_FIFOMode));
  301. assert_param(IS_DMA_FIFO_THRESHOLD(DMA_InitStruct->DMA_FIFOThreshold));
  302. assert_param(IS_DMA_MEMORY_BURST(DMA_InitStruct->DMA_MemoryBurst));
  303. assert_param(IS_DMA_PERIPHERAL_BURST(DMA_InitStruct->DMA_PeripheralBurst));
  304. /*------------------------- DMAy Streamx CR Configuration ------------------*/
  305. /* Get the DMAy_Streamx CR value */
  306. tmpreg = DMAy_Streamx->CR;
  307. /* Clear CHSEL, MBURST, PBURST, PL, MSIZE, PSIZE, MINC, PINC, CIRC and DIR bits */
  308. tmpreg &= ((uint32_t)~(DMA_SxCR_CHSEL | DMA_SxCR_MBURST | DMA_SxCR_PBURST | \
  309. DMA_SxCR_PL | DMA_SxCR_MSIZE | DMA_SxCR_PSIZE | \
  310. DMA_SxCR_MINC | DMA_SxCR_PINC | DMA_SxCR_CIRC | \
  311. DMA_SxCR_DIR));
  312. /* Configure DMAy Streamx: */
  313. /* Set CHSEL bits according to DMA_CHSEL value */
  314. /* Set DIR bits according to DMA_DIR value */
  315. /* Set PINC bit according to DMA_PeripheralInc value */
  316. /* Set MINC bit according to DMA_MemoryInc value */
  317. /* Set PSIZE bits according to DMA_PeripheralDataSize value */
  318. /* Set MSIZE bits according to DMA_MemoryDataSize value */
  319. /* Set CIRC bit according to DMA_Mode value */
  320. /* Set PL bits according to DMA_Priority value */
  321. /* Set MBURST bits according to DMA_MemoryBurst value */
  322. /* Set PBURST bits according to DMA_PeripheralBurst value */
  323. tmpreg |= DMA_InitStruct->DMA_Channel | DMA_InitStruct->DMA_DIR |
  324. DMA_InitStruct->DMA_PeripheralInc | DMA_InitStruct->DMA_MemoryInc |
  325. DMA_InitStruct->DMA_PeripheralDataSize | DMA_InitStruct->DMA_MemoryDataSize |
  326. DMA_InitStruct->DMA_Mode | DMA_InitStruct->DMA_Priority |
  327. DMA_InitStruct->DMA_MemoryBurst | DMA_InitStruct->DMA_PeripheralBurst;
  328. /* Write to DMAy Streamx CR register */
  329. DMAy_Streamx->CR = tmpreg;
  330. /*------------------------- DMAy Streamx FCR Configuration -----------------*/
  331. /* Get the DMAy_Streamx FCR value */
  332. tmpreg = DMAy_Streamx->FCR;
  333. /* Clear DMDIS and FTH bits */
  334. tmpreg &= (uint32_t)~(DMA_SxFCR_DMDIS | DMA_SxFCR_FTH);
  335. /* Configure DMAy Streamx FIFO:
  336. Set DMDIS bits according to DMA_FIFOMode value
  337. Set FTH bits according to DMA_FIFOThreshold value */
  338. tmpreg |= DMA_InitStruct->DMA_FIFOMode | DMA_InitStruct->DMA_FIFOThreshold;
  339. /* Write to DMAy Streamx CR */
  340. DMAy_Streamx->FCR = tmpreg;
  341. /*------------------------- DMAy Streamx NDTR Configuration ----------------*/
  342. /* Write to DMAy Streamx NDTR register */
  343. DMAy_Streamx->NDTR = DMA_InitStruct->DMA_BufferSize;
  344. /*------------------------- DMAy Streamx PAR Configuration -----------------*/
  345. /* Write to DMAy Streamx PAR */
  346. DMAy_Streamx->PAR = DMA_InitStruct->DMA_PeripheralBaseAddr;
  347. /*------------------------- DMAy Streamx M0AR Configuration ----------------*/
  348. /* Write to DMAy Streamx M0AR */
  349. DMAy_Streamx->M0AR = DMA_InitStruct->DMA_Memory0BaseAddr;
  350. }
  351. /**
  352. * @brief Fills each DMA_InitStruct member with its default value.
  353. * @param DMA_InitStruct : pointer to a DMA_InitTypeDef structure which will
  354. * be initialized.
  355. * @retval None
  356. */
  357. void DMA_StructInit(DMA_InitTypeDef* DMA_InitStruct)
  358. {
  359. /*-------------- Reset DMA init structure parameters values ----------------*/
  360. /* Initialize the DMA_Channel member */
  361. DMA_InitStruct->DMA_Channel = 0;
  362. /* Initialize the DMA_PeripheralBaseAddr member */
  363. DMA_InitStruct->DMA_PeripheralBaseAddr = 0;
  364. /* Initialize the DMA_Memory0BaseAddr member */
  365. DMA_InitStruct->DMA_Memory0BaseAddr = 0;
  366. /* Initialize the DMA_DIR member */
  367. DMA_InitStruct->DMA_DIR = DMA_DIR_PeripheralToMemory;
  368. /* Initialize the DMA_BufferSize member */
  369. DMA_InitStruct->DMA_BufferSize = 0;
  370. /* Initialize the DMA_PeripheralInc member */
  371. DMA_InitStruct->DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  372. /* Initialize the DMA_MemoryInc member */
  373. DMA_InitStruct->DMA_MemoryInc = DMA_MemoryInc_Disable;
  374. /* Initialize the DMA_PeripheralDataSize member */
  375. DMA_InitStruct->DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
  376. /* Initialize the DMA_MemoryDataSize member */
  377. DMA_InitStruct->DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
  378. /* Initialize the DMA_Mode member */
  379. DMA_InitStruct->DMA_Mode = DMA_Mode_Normal;
  380. /* Initialize the DMA_Priority member */
  381. DMA_InitStruct->DMA_Priority = DMA_Priority_Low;
  382. /* Initialize the DMA_FIFOMode member */
  383. DMA_InitStruct->DMA_FIFOMode = DMA_FIFOMode_Disable;
  384. /* Initialize the DMA_FIFOThreshold member */
  385. DMA_InitStruct->DMA_FIFOThreshold = DMA_FIFOThreshold_1QuarterFull;
  386. /* Initialize the DMA_MemoryBurst member */
  387. DMA_InitStruct->DMA_MemoryBurst = DMA_MemoryBurst_Single;
  388. /* Initialize the DMA_PeripheralBurst member */
  389. DMA_InitStruct->DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
  390. }
  391. /**
  392. * @brief Enables or disables the specified DMAy Streamx.
  393. * @param DMAy_Streamx: where y can be 1 or 2 to select the DMA and x can be 0
  394. * to 7 to select the DMA Stream.
  395. * @param NewState: new state of the DMAy Streamx.
  396. * This parameter can be: ENABLE or DISABLE.
  397. *
  398. * @note This function may be used to perform Pause-Resume operation. When a
  399. * transfer is ongoing, calling this function to disable the Stream will
  400. * cause the transfer to be paused. All configuration registers and the
  401. * number of remaining data will be preserved. When calling again this
  402. * function to re-enable the Stream, the transfer will be resumed from
  403. * the point where it was paused.
  404. *
  405. * @note After configuring the DMA Stream (DMA_Init() function) and enabling the
  406. * stream, it is recommended to check (or wait until) the DMA Stream is
  407. * effectively enabled. A Stream may remain disabled if a configuration
  408. * parameter is wrong.
  409. * After disabling a DMA Stream, it is also recommended to check (or wait
  410. * until) the DMA Stream is effectively disabled. If a Stream is disabled
  411. * while a data transfer is ongoing, the current data will be transferred
  412. * and the Stream will be effectively disabled only after the transfer of
  413. * this single data is finished.
  414. *
  415. * @retval None
  416. */
  417. void DMA_Cmd(DMA_Stream_TypeDef* DMAy_Streamx, FunctionalState NewState)
  418. {
  419. /* Check the parameters */
  420. assert_param(IS_DMA_ALL_PERIPH(DMAy_Streamx));
  421. assert_param(IS_FUNCTIONAL_STATE(NewState));
  422. if (NewState != DISABLE)
  423. {
  424. /* Enable the selected DMAy Streamx by setting EN bit */
  425. DMAy_Streamx->CR |= (uint32_t)DMA_SxCR_EN;
  426. }
  427. else
  428. {
  429. /* Disable the selected DMAy Streamx by clearing EN bit */
  430. DMAy_Streamx->CR &= ~(uint32_t)DMA_SxCR_EN;
  431. }
  432. }
  433. /**
  434. * @brief Configures, when the PINC (Peripheral Increment address mode) bit is
  435. * set, if the peripheral address should be incremented with the data
  436. * size (configured with PSIZE bits) or by a fixed offset equal to 4
  437. * (32-bit aligned addresses).
  438. *
  439. * @note This function has no effect if the Peripheral Increment mode is disabled.
  440. *
  441. * @param DMAy_Streamx: where y can be 1 or 2 to select the DMA and x can be 0
  442. * to 7 to select the DMA Stream.
  443. * @param DMA_Pincos: specifies the Peripheral increment offset size.
  444. * This parameter can be one of the following values:
  445. * @arg DMA_PINCOS_Psize: Peripheral address increment is done
  446. * accordingly to PSIZE parameter.
  447. * @arg DMA_PINCOS_WordAligned: Peripheral address increment offset is
  448. * fixed to 4 (32-bit aligned addresses).
  449. * @retval None
  450. */
  451. void DMA_PeriphIncOffsetSizeConfig(DMA_Stream_TypeDef* DMAy_Streamx, uint32_t DMA_Pincos)
  452. {
  453. /* Check the parameters */
  454. assert_param(IS_DMA_ALL_PERIPH(DMAy_Streamx));
  455. assert_param(IS_DMA_PINCOS_SIZE(DMA_Pincos));
  456. /* Check the needed Peripheral increment offset */
  457. if(DMA_Pincos != DMA_PINCOS_Psize)
  458. {
  459. /* Configure DMA_SxCR_PINCOS bit with the input parameter */
  460. DMAy_Streamx->CR |= (uint32_t)DMA_SxCR_PINCOS;
  461. }
  462. else
  463. {
  464. /* Clear the PINCOS bit: Peripheral address incremented according to PSIZE */
  465. DMAy_Streamx->CR &= ~(uint32_t)DMA_SxCR_PINCOS;
  466. }
  467. }
  468. /**
  469. * @brief Configures, when the DMAy Streamx is disabled, the flow controller for
  470. * the next transactions (Peripheral or Memory).
  471. *
  472. * @note Before enabling this feature, check if the used peripheral supports
  473. * the Flow Controller mode or not.
  474. *
  475. * @param DMAy_Streamx: where y can be 1 or 2 to select the DMA and x can be 0
  476. * to 7 to select the DMA Stream.
  477. * @param DMA_FlowCtrl: specifies the DMA flow controller.
  478. * This parameter can be one of the following values:
  479. * @arg DMA_FlowCtrl_Memory: DMAy_Streamx transactions flow controller is
  480. * the DMA controller.
  481. * @arg DMA_FlowCtrl_Peripheral: DMAy_Streamx transactions flow controller
  482. * is the peripheral.
  483. * @retval None
  484. */
  485. void DMA_FlowControllerConfig(DMA_Stream_TypeDef* DMAy_Streamx, uint32_t DMA_FlowCtrl)
  486. {
  487. /* Check the parameters */
  488. assert_param(IS_DMA_ALL_PERIPH(DMAy_Streamx));
  489. assert_param(IS_DMA_FLOW_CTRL(DMA_FlowCtrl));
  490. /* Check the needed flow controller */
  491. if(DMA_FlowCtrl != DMA_FlowCtrl_Memory)
  492. {
  493. /* Configure DMA_SxCR_PFCTRL bit with the input parameter */
  494. DMAy_Streamx->CR |= (uint32_t)DMA_SxCR_PFCTRL;
  495. }
  496. else
  497. {
  498. /* Clear the PFCTRL bit: Memory is the flow controller */
  499. DMAy_Streamx->CR &= ~(uint32_t)DMA_SxCR_PFCTRL;
  500. }
  501. }
  502. /**
  503. * @}
  504. */
  505. /** @defgroup DMA_Group2 Data Counter functions
  506. * @brief Data Counter functions
  507. *
  508. @verbatim
  509. ===============================================================================
  510. Data Counter functions
  511. ===============================================================================
  512. This subsection provides function allowing to configure and read the buffer size
  513. (number of data to be transferred).
  514. The DMA data counter can be written only when the DMA Stream is disabled
  515. (ie. after transfer complete event).
  516. The following function can be used to write the Stream data counter value:
  517. - void DMA_SetCurrDataCounter(DMA_Stream_TypeDef* DMAy_Streamx, uint16_t Counter);
  518. @note It is advised to use this function rather than DMA_Init() in situations where
  519. only the Data buffer needs to be reloaded.
  520. @note If the Source and Destination Data Sizes are different, then the value written in
  521. data counter, expressing the number of transfers, is relative to the number of
  522. transfers from the Peripheral point of view.
  523. ie. If Memory data size is Word, Peripheral data size is Half-Words, then the value
  524. to be configured in the data counter is the number of Half-Words to be transferred
  525. from/to the peripheral.
  526. The DMA data counter can be read to indicate the number of remaining transfers for
  527. the relative DMA Stream. This counter is decremented at the end of each data
  528. transfer and when the transfer is complete:
  529. - If Normal mode is selected: the counter is set to 0.
  530. - If Circular mode is selected: the counter is reloaded with the initial value
  531. (configured before enabling the DMA Stream)
  532. The following function can be used to read the Stream data counter value:
  533. - uint16_t DMA_GetCurrDataCounter(DMA_Stream_TypeDef* DMAy_Streamx);
  534. @endverbatim
  535. * @{
  536. */
  537. /**
  538. * @brief Writes the number of data units to be transferred on the DMAy Streamx.
  539. * @param DMAy_Streamx: where y can be 1 or 2 to select the DMA and x can be 0
  540. * to 7 to select the DMA Stream.
  541. * @param Counter: Number of data units to be transferred (from 0 to 65535)
  542. * Number of data items depends only on the Peripheral data format.
  543. *
  544. * @note If Peripheral data format is Bytes: number of data units is equal
  545. * to total number of bytes to be transferred.
  546. *
  547. * @note If Peripheral data format is Half-Word: number of data units is
  548. * equal to total number of bytes to be transferred / 2.
  549. *
  550. * @note If Peripheral data format is Word: number of data units is equal
  551. * to total number of bytes to be transferred / 4.
  552. *
  553. * @note In Memory-to-Memory transfer mode, the memory buffer pointed by
  554. * DMAy_SxPAR register is considered as Peripheral.
  555. *
  556. * @retval The number of remaining data units in the current DMAy Streamx transfer.
  557. */
  558. void DMA_SetCurrDataCounter(DMA_Stream_TypeDef* DMAy_Streamx, uint16_t Counter)
  559. {
  560. /* Check the parameters */
  561. assert_param(IS_DMA_ALL_PERIPH(DMAy_Streamx));
  562. /* Write the number of data units to be transferred */
  563. DMAy_Streamx->NDTR = (uint16_t)Counter;
  564. }
  565. /**
  566. * @brief Returns the number of remaining data units in the current DMAy Streamx transfer.
  567. * @param DMAy_Streamx: where y can be 1 or 2 to select the DMA and x can be 0
  568. * to 7 to select the DMA Stream.
  569. * @retval The number of remaining data units in the current DMAy Streamx transfer.
  570. */
  571. uint16_t DMA_GetCurrDataCounter(DMA_Stream_TypeDef* DMAy_Streamx)
  572. {
  573. /* Check the parameters */
  574. assert_param(IS_DMA_ALL_PERIPH(DMAy_Streamx));
  575. /* Return the number of remaining data units for DMAy Streamx */
  576. return ((uint16_t)(DMAy_Streamx->NDTR));
  577. }
  578. /**
  579. * @}
  580. */
  581. /** @defgroup DMA_Group3 Double Buffer mode functions
  582. * @brief Double Buffer mode functions
  583. *
  584. @verbatim
  585. ===============================================================================
  586. Double Buffer mode functions
  587. ===============================================================================
  588. This subsection provides function allowing to configure and control the double
  589. buffer mode parameters.
  590. The Double Buffer mode can be used only when Circular mode is enabled.
  591. The Double Buffer mode cannot be used when transferring data from Memory to Memory.
  592. The Double Buffer mode allows to set two different Memory addresses from/to which
  593. the DMA controller will access alternatively (after completing transfer to/from target
  594. memory 0, it will start transfer to/from target memory 1).
  595. This allows to reduce software overhead for double buffering and reduce the CPU
  596. access time.
  597. Two functions must be called before calling the DMA_Init() function:
  598. - void DMA_DoubleBufferModeConfig(DMA_Stream_TypeDef* DMAy_Streamx, uint32_t Memory1BaseAddr,
  599. uint32_t DMA_CurrentMemory);
  600. - void DMA_DoubleBufferModeCmd(DMA_Stream_TypeDef* DMAy_Streamx, FunctionalState NewState);
  601. DMA_DoubleBufferModeConfig() is called to configure the Memory 1 base address and the first
  602. Memory target from/to which the transfer will start after enabling the DMA Stream.
  603. Then DMA_DoubleBufferModeCmd() must be called to enable the Double Buffer mode (or disable
  604. it when it should not be used).
  605. Two functions can be called dynamically when the transfer is ongoing (or when the DMA Stream is
  606. stopped) to modify on of the target Memories addresses or to check wich Memory target is currently
  607. used:
  608. - void DMA_MemoryTargetConfig(DMA_Stream_TypeDef* DMAy_Streamx, uint32_t MemoryBaseAddr,
  609. uint32_t DMA_MemoryTarget);
  610. - uint32_t DMA_GetCurrentMemoryTarget(DMA_Stream_TypeDef* DMAy_Streamx);
  611. DMA_MemoryTargetConfig() can be called to modify the base address of one of the two target Memories.
  612. The Memory of which the base address will be modified must not be currently be used by the DMA Stream
  613. (ie. if the DMA Stream is currently transferring from Memory 1 then you can only modify base address
  614. of target Memory 0 and vice versa).
  615. To check this condition, it is recommended to use the function DMA_GetCurrentMemoryTarget() which
  616. returns the index of the Memory target currently in use by the DMA Stream.
  617. @endverbatim
  618. * @{
  619. */
  620. /**
  621. * @brief Configures, when the DMAy Streamx is disabled, the double buffer mode
  622. * and the current memory target.
  623. * @param DMAy_Streamx: where y can be 1 or 2 to select the DMA and x can be 0
  624. * to 7 to select the DMA Stream.
  625. * @param Memory1BaseAddr: the base address of the second buffer (Memory 1)
  626. * @param DMA_CurrentMemory: specifies which memory will be first buffer for
  627. * the transactions when the Stream will be enabled.
  628. * This parameter can be one of the following values:
  629. * @arg DMA_Memory_0: Memory 0 is the current buffer.
  630. * @arg DMA_Memory_1: Memory 1 is the current buffer.
  631. *
  632. * @note Memory0BaseAddr is set by the DMA structure configuration in DMA_Init().
  633. *
  634. * @retval None
  635. */
  636. void DMA_DoubleBufferModeConfig(DMA_Stream_TypeDef* DMAy_Streamx, uint32_t Memory1BaseAddr,
  637. uint32_t DMA_CurrentMemory)
  638. {
  639. /* Check the parameters */
  640. assert_param(IS_DMA_ALL_PERIPH(DMAy_Streamx));
  641. assert_param(IS_DMA_CURRENT_MEM(DMA_CurrentMemory));
  642. if (DMA_CurrentMemory != DMA_Memory_0)
  643. {
  644. /* Set Memory 1 as current memory address */
  645. DMAy_Streamx->CR |= (uint32_t)(DMA_SxCR_CT);
  646. }
  647. else
  648. {
  649. /* Set Memory 0 as current memory address */
  650. DMAy_Streamx->CR &= ~(uint32_t)(DMA_SxCR_CT);
  651. }
  652. /* Write to DMAy Streamx M1AR */
  653. DMAy_Streamx->M1AR = Memory1BaseAddr;
  654. }
  655. /**
  656. * @brief Enables or disables the double buffer mode for the selected DMA stream.
  657. * @note This function can be called only when the DMA Stream is disabled.
  658. * @param DMAy_Streamx: where y can be 1 or 2 to select the DMA and x can be 0
  659. * to 7 to select the DMA Stream.
  660. * @param NewState: new state of the DMAy Streamx double buffer mode.
  661. * This parameter can be: ENABLE or DISABLE.
  662. * @retval None
  663. */
  664. void DMA_DoubleBufferModeCmd(DMA_Stream_TypeDef* DMAy_Streamx, FunctionalState NewState)
  665. {
  666. /* Check the parameters */
  667. assert_param(IS_DMA_ALL_PERIPH(DMAy_Streamx));
  668. assert_param(IS_FUNCTIONAL_STATE(NewState));
  669. /* Configure the Double Buffer mode */
  670. if (NewState != DISABLE)
  671. {
  672. /* Enable the Double buffer mode */
  673. DMAy_Streamx->CR |= (uint32_t)DMA_SxCR_DBM;
  674. }
  675. else
  676. {
  677. /* Disable the Double buffer mode */
  678. DMAy_Streamx->CR &= ~(uint32_t)DMA_SxCR_DBM;
  679. }
  680. }
  681. /**
  682. * @brief Configures the Memory address for the next buffer transfer in double
  683. * buffer mode (for dynamic use). This function can be called when the
  684. * DMA Stream is enabled and when the transfer is ongoing.
  685. * @param DMAy_Streamx: where y can be 1 or 2 to select the DMA and x can be 0
  686. * to 7 to select the DMA Stream.
  687. * @param MemoryBaseAddr: The base address of the target memory buffer
  688. * @param DMA_MemoryTarget: Next memory target to be used.
  689. * This parameter can be one of the following values:
  690. * @arg DMA_Memory_0: To use the memory address 0
  691. * @arg DMA_Memory_1: To use the memory address 1
  692. *
  693. * @note It is not allowed to modify the Base Address of a target Memory when
  694. * this target is involved in the current transfer. ie. If the DMA Stream
  695. * is currently transferring to/from Memory 1, then it not possible to
  696. * modify Base address of Memory 1, but it is possible to modify Base
  697. * address of Memory 0.
  698. * To know which Memory is currently used, you can use the function
  699. * DMA_GetCurrentMemoryTarget().
  700. *
  701. * @retval None
  702. */
  703. void DMA_MemoryTargetConfig(DMA_Stream_TypeDef* DMAy_Streamx, uint32_t MemoryBaseAddr,
  704. uint32_t DMA_MemoryTarget)
  705. {
  706. /* Check the parameters */
  707. assert_param(IS_DMA_ALL_PERIPH(DMAy_Streamx));
  708. assert_param(IS_DMA_CURRENT_MEM(DMA_MemoryTarget));
  709. /* Check the Memory target to be configured */
  710. if (DMA_MemoryTarget != DMA_Memory_0)
  711. {
  712. /* Write to DMAy Streamx M1AR */
  713. DMAy_Streamx->M1AR = MemoryBaseAddr;
  714. }
  715. else
  716. {
  717. /* Write to DMAy Streamx M0AR */
  718. DMAy_Streamx->M0AR = MemoryBaseAddr;
  719. }
  720. }
  721. /**
  722. * @brief Returns the current memory target used by double buffer transfer.
  723. * @param DMAy_Streamx: where y can be 1 or 2 to select the DMA and x can be 0
  724. * to 7 to select the DMA Stream.
  725. * @retval The memory target number: 0 for Memory0 or 1 for Memory1.
  726. */
  727. uint32_t DMA_GetCurrentMemoryTarget(DMA_Stream_TypeDef* DMAy_Streamx)
  728. {
  729. uint32_t tmp = 0;
  730. /* Check the parameters */
  731. assert_param(IS_DMA_ALL_PERIPH(DMAy_Streamx));
  732. /* Get the current memory target */
  733. if ((DMAy_Streamx->CR & DMA_SxCR_CT) != 0)
  734. {
  735. /* Current memory buffer used is Memory 1 */
  736. tmp = 1;
  737. }
  738. else
  739. {
  740. /* Current memory buffer used is Memory 0 */
  741. tmp = 0;
  742. }
  743. return tmp;
  744. }
  745. /**
  746. * @}
  747. */
  748. /** @defgroup DMA_Group4 Interrupts and flags management functions
  749. * @brief Interrupts and flags management functions
  750. *
  751. @verbatim
  752. ===============================================================================
  753. Interrupts and flags management functions
  754. ===============================================================================
  755. This subsection provides functions allowing to
  756. - Check the DMA enable status
  757. - Check the FIFO status
  758. - Configure the DMA Interrupts sources and check or clear the flags or pending bits status.
  759. 1. DMA Enable status:
  760. After configuring the DMA Stream (DMA_Init() function) and enabling the stream,
  761. it is recommended to check (or wait until) the DMA Stream is effectively enabled.
  762. A Stream may remain disabled if a configuration parameter is wrong.
  763. After disabling a DMA Stream, it is also recommended to check (or wait until) the DMA
  764. Stream is effectively disabled. If a Stream is disabled while a data transfer is ongoing,
  765. the current data will be transferred and the Stream will be effectively disabled only after
  766. this data transfer completion.
  767. To monitor this state it is possible to use the following function:
  768. - FunctionalState DMA_GetCmdStatus(DMA_Stream_TypeDef* DMAy_Streamx);
  769. 2. FIFO Status:
  770. It is possible to monitor the FIFO status when a transfer is ongoing using the following
  771. function:
  772. - uint32_t DMA_GetFIFOStatus(DMA_Stream_TypeDef* DMAy_Streamx);
  773. 3. DMA Interrupts and Flags:
  774. The user should identify which mode will be used in his application to manage the
  775. DMA controller events: Polling mode or Interrupt mode.
  776. Polling Mode
  777. =============
  778. Each DMA stream can be managed through 4 event Flags:
  779. (x : DMA Stream number )
  780. 1. DMA_FLAG_FEIFx : to indicate that a FIFO Mode Transfer Error event occurred.
  781. 2. DMA_FLAG_DMEIFx : to indicate that a Direct Mode Transfer Error event occurred.
  782. 3. DMA_FLAG_TEIFx : to indicate that a Transfer Error event occurred.
  783. 4. DMA_FLAG_HTIFx : to indicate that a Half-Transfer Complete event occurred.
  784. 5. DMA_FLAG_TCIFx : to indicate that a Transfer Complete event occurred .
  785. In this Mode it is advised to use the following functions:
  786. - FlagStatus DMA_GetFlagStatus(DMA_Stream_TypeDef* DMAy_Streamx, uint32_t DMA_FLAG);
  787. - void DMA_ClearFlag(DMA_Stream_TypeDef* DMAy_Streamx, uint32_t DMA_FLAG);
  788. Interrupt Mode
  789. ===============
  790. Each DMA Stream can be managed through 4 Interrupts:
  791. Interrupt Source
  792. ----------------
  793. 1. DMA_IT_FEIFx : specifies the interrupt source for the FIFO Mode Transfer Error event.
  794. 2. DMA_IT_DMEIFx : specifies the interrupt source for the Direct Mode Transfer Error event.
  795. 3. DMA_IT_TEIFx : specifies the interrupt source for the Transfer Error event.
  796. 4. DMA_IT_HTIFx : specifies the interrupt source for the Half-Transfer Complete event.
  797. 5. DMA_IT_TCIFx : specifies the interrupt source for the a Transfer Complete event.
  798. In this Mode it is advised to use the following functions:
  799. - void DMA_ITConfig(DMA_Stream_TypeDef* DMAy_Streamx, uint32_t DMA_IT, FunctionalState NewState);
  800. - ITStatus DMA_GetITStatus(DMA_Stream_TypeDef* DMAy_Streamx, uint32_t DMA_IT);
  801. - void DMA_ClearITPendingBit(DMA_Stream_TypeDef* DMAy_Streamx, uint32_t DMA_IT);
  802. @endverbatim
  803. * @{
  804. */
  805. /**
  806. * @brief Returns the status of EN bit for the specified DMAy Streamx.
  807. * @param DMAy_Streamx: where y can be 1 or 2 to select the DMA and x can be 0
  808. * to 7 to select the DMA Stream.
  809. *
  810. * @note After configuring the DMA Stream (DMA_Init() function) and enabling
  811. * the stream, it is recommended to check (or wait until) the DMA Stream
  812. * is effectively enabled. A Stream may remain disabled if a configuration
  813. * parameter is wrong.
  814. * After disabling a DMA Stream, it is also recommended to check (or wait
  815. * until) the DMA Stream is effectively disabled. If a Stream is disabled
  816. * while a data transfer is ongoing, the current data will be transferred
  817. * and the Stream will be effectively disabled only after the transfer
  818. * of this single data is finished.
  819. *
  820. * @retval Current state of the DMAy Streamx (ENABLE or DISABLE).
  821. */
  822. FunctionalState DMA_GetCmdStatus(DMA_Stream_TypeDef* DMAy_Streamx)
  823. {
  824. FunctionalState state = DISABLE;
  825. /* Check the parameters */
  826. assert_param(IS_DMA_ALL_PERIPH(DMAy_Streamx));
  827. if ((DMAy_Streamx->CR & (uint32_t)DMA_SxCR_EN) != 0)
  828. {
  829. /* The selected DMAy Streamx EN bit is set (DMA is still transferring) */
  830. state = ENABLE;
  831. }
  832. else
  833. {
  834. /* The selected DMAy Streamx EN bit is cleared (DMA is disabled and
  835. all transfers are complete) */
  836. state = DISABLE;
  837. }
  838. return state;
  839. }
  840. /**
  841. * @brief Returns the current DMAy Streamx FIFO filled level.
  842. * @param DMAy_Streamx: where y can be 1 or 2 to select the DMA and x can be 0
  843. * to 7 to select the DMA Stream.
  844. * @retval The FIFO filling state.
  845. * - DMA_FIFOStatus_Less1QuarterFull: when FIFO is less than 1 quarter-full
  846. * and not empty.
  847. * - DMA_FIFOStatus_1QuarterFull: if more than 1 quarter-full.
  848. * - DMA_FIFOStatus_HalfFull: if more than 1 half-full.
  849. * - DMA_FIFOStatus_3QuartersFull: if more than 3 quarters-full.
  850. * - DMA_FIFOStatus_Empty: when FIFO is empty
  851. * - DMA_FIFOStatus_Full: when FIFO is full
  852. */
  853. uint32_t DMA_GetFIFOStatus(DMA_Stream_TypeDef* DMAy_Streamx)
  854. {
  855. uint32_t tmpreg = 0;
  856. /* Check the parameters */
  857. assert_param(IS_DMA_ALL_PERIPH(DMAy_Streamx));
  858. /* Get the FIFO level bits */
  859. tmpreg = (uint32_t)((DMAy_Streamx->FCR & DMA_SxFCR_FS));
  860. return tmpreg;
  861. }
  862. /**
  863. * @brief Checks whether the specified DMAy Streamx flag is set or not.
  864. * @param DMAy_Streamx: where y can be 1 or 2 to select the DMA and x can be 0
  865. * to 7 to select the DMA Stream.
  866. * @param DMA_FLAG: specifies the flag to check.
  867. * This parameter can be one of the following values:
  868. * @arg DMA_FLAG_TCIFx: Streamx transfer complete flag
  869. * @arg DMA_FLAG_HTIFx: Streamx half transfer complete flag
  870. * @arg DMA_FLAG_TEIFx: Streamx transfer error flag
  871. * @arg DMA_FLAG_DMEIFx: Streamx direct mode error flag
  872. * @arg DMA_FLAG_FEIFx: Streamx FIFO error flag
  873. * Where x can be 0 to 7 to select the DMA Stream.
  874. * @retval The new state of DMA_FLAG (SET or RESET).
  875. */
  876. FlagStatus DMA_GetFlagStatus(DMA_Stream_TypeDef* DMAy_Streamx, uint32_t DMA_FLAG)
  877. {
  878. FlagStatus bitstatus = RESET;
  879. DMA_TypeDef* DMAy;
  880. uint32_t tmpreg = 0;
  881. /* Check the parameters */
  882. assert_param(IS_DMA_ALL_PERIPH(DMAy_Streamx));
  883. assert_param(IS_DMA_GET_FLAG(DMA_FLAG));
  884. /* Determine the DMA to which belongs the stream */
  885. if (DMAy_Streamx < DMA2_Stream0)
  886. {
  887. /* DMAy_Streamx belongs to DMA1 */
  888. DMAy = DMA1;
  889. }
  890. else
  891. {
  892. /* DMAy_Streamx belongs to DMA2 */
  893. DMAy = DMA2;
  894. }
  895. /* Check if the flag is in HISR or LISR */
  896. if ((DMA_FLAG & HIGH_ISR_MASK) != (uint32_t)RESET)
  897. {
  898. /* Get DMAy HISR register value */
  899. tmpreg = DMAy->HISR;
  900. }
  901. else
  902. {
  903. /* Get DMAy LISR register value */
  904. tmpreg = DMAy->LISR;
  905. }
  906. /* Mask the reserved bits */
  907. tmpreg &= (uint32_t)RESERVED_MASK;
  908. /* Check the status of the specified DMA flag */
  909. if ((tmpreg & DMA_FLAG) != (uint32_t)RESET)
  910. {
  911. /* DMA_FLAG is set */
  912. bitstatus = SET;
  913. }
  914. else
  915. {
  916. /* DMA_FLAG is reset */
  917. bitstatus = RESET;
  918. }
  919. /* Return the DMA_FLAG status */
  920. return bitstatus;
  921. }
  922. /**
  923. * @brief Clears the DMAy Streamx's pending flags.
  924. * @param DMAy_Streamx: where y can be 1 or 2 to select the DMA and x can be 0
  925. * to 7 to select the DMA Stream.
  926. * @param DMA_FLAG: specifies the flag to clear.
  927. * This parameter can be any combination of the following values:
  928. * @arg DMA_FLAG_TCIFx: Streamx transfer complete flag
  929. * @arg DMA_FLAG_HTIFx: Streamx half transfer complete flag
  930. * @arg DMA_FLAG_TEIFx: Streamx transfer error flag
  931. * @arg DMA_FLAG_DMEIFx: Streamx direct mode error flag
  932. * @arg DMA_FLAG_FEIFx: Streamx FIFO error flag
  933. * Where x can be 0 to 7 to select the DMA Stream.
  934. * @retval None
  935. */
  936. void DMA_ClearFlag(DMA_Stream_TypeDef* DMAy_Streamx, uint32_t DMA_FLAG)
  937. {
  938. DMA_TypeDef* DMAy;
  939. /* Check the parameters */
  940. assert_param(IS_DMA_ALL_PERIPH(DMAy_Streamx));
  941. assert_param(IS_DMA_CLEAR_FLAG(DMA_FLAG));
  942. /* Determine the DMA to which belongs the stream */
  943. if (DMAy_Streamx < DMA2_Stream0)
  944. {
  945. /* DMAy_Streamx belongs to DMA1 */
  946. DMAy = DMA1;
  947. }
  948. else
  949. {
  950. /* DMAy_Streamx belongs to DMA2 */
  951. DMAy = DMA2;
  952. }
  953. /* Check if LIFCR or HIFCR register is targeted */
  954. if ((DMA_FLAG & HIGH_ISR_MASK) != (uint32_t)RESET)
  955. {
  956. /* Set DMAy HIFCR register clear flag bits */
  957. DMAy->HIFCR = (uint32_t)(DMA_FLAG & RESERVED_MASK);
  958. }
  959. else
  960. {
  961. /* Set DMAy LIFCR register clear flag bits */
  962. DMAy->LIFCR = (uint32_t)(DMA_FLAG & RESERVED_MASK);
  963. }
  964. }
  965. /**
  966. * @brief Enables or disables the specified DMAy Streamx interrupts.
  967. * @param DMAy_Streamx: where y can be 1 or 2 to select the DMA and x can be 0
  968. * to 7 to select the DMA Stream.
  969. * @param DMA_IT: specifies the DMA interrupt sources to be enabled or disabled.
  970. * This parameter can be any combination of the following values:
  971. * @arg DMA_IT_TC: Transfer complete interrupt mask
  972. * @arg DMA_IT_HT: Half transfer complete interrupt mask
  973. * @arg DMA_IT_TE: Transfer error interrupt mask
  974. * @arg DMA_IT_FE: FIFO error interrupt mask
  975. * @param NewState: new state of the specified DMA interrupts.
  976. * This parameter can be: ENABLE or DISABLE.
  977. * @retval None
  978. */
  979. void DMA_ITConfig(DMA_Stream_TypeDef* DMAy_Streamx, uint32_t DMA_IT, FunctionalState NewState)
  980. {
  981. /* Check the parameters */
  982. assert_param(IS_DMA_ALL_PERIPH(DMAy_Streamx));
  983. assert_param(IS_DMA_CONFIG_IT(DMA_IT));
  984. assert_param(IS_FUNCTIONAL_STATE(NewState));
  985. /* Check if the DMA_IT parameter contains a FIFO interrupt */
  986. if ((DMA_IT & DMA_IT_FE) != 0)
  987. {
  988. if (NewState != DISABLE)
  989. {
  990. /* Enable the selected DMA FIFO interrupts */
  991. DMAy_Streamx->FCR |= (uint32_t)DMA_IT_FE;
  992. }
  993. else
  994. {
  995. /* Disable the selected DMA FIFO interrupts */
  996. DMAy_Streamx->FCR &= ~(uint32_t)DMA_IT_FE;
  997. }
  998. }
  999. /* Check if the DMA_IT parameter contains a Transfer interrupt */
  1000. if (DMA_IT != DMA_IT_FE)
  1001. {
  1002. if (NewState != DISABLE)
  1003. {
  1004. /* Enable the selected DMA transfer interrupts */
  1005. DMAy_Streamx->CR |= (uint32_t)(DMA_IT & TRANSFER_IT_ENABLE_MASK);
  1006. }
  1007. else
  1008. {
  1009. /* Disable the selected DMA transfer interrupts */
  1010. DMAy_Streamx->CR &= ~(uint32_t)(DMA_IT & TRANSFER_IT_ENABLE_MASK);
  1011. }
  1012. }
  1013. }
  1014. /**
  1015. * @brief Checks whether the specified DMAy Streamx interrupt has occurred or not.
  1016. * @param DMAy_Streamx: where y can be 1 or 2 to select the DMA and x can be 0
  1017. * to 7 to select the DMA Stream.
  1018. * @param DMA_IT: specifies the DMA interrupt source to check.
  1019. * This parameter can be one of the following values:
  1020. * @arg DMA_IT_TCIFx: Streamx transfer complete interrupt
  1021. * @arg DMA_IT_HTIFx: Streamx half transfer complete interrupt
  1022. * @arg DMA_IT_TEIFx: Streamx transfer error interrupt
  1023. * @arg DMA_IT_DMEIFx: Streamx direct mode error interrupt
  1024. * @arg DMA_IT_FEIFx: Streamx FIFO error interrupt
  1025. * Where x can be 0 to 7 to select the DMA Stream.
  1026. * @retval The new state of DMA_IT (SET or RESET).
  1027. */
  1028. ITStatus DMA_GetITStatus(DMA_Stream_TypeDef* DMAy_Streamx, uint32_t DMA_IT)
  1029. {
  1030. ITStatus bitstatus = RESET;
  1031. DMA_TypeDef* DMAy;
  1032. uint32_t tmpreg = 0, enablestatus = 0;
  1033. /* Check the parameters */
  1034. assert_param(IS_DMA_ALL_PERIPH(DMAy_Streamx));
  1035. assert_param(IS_DMA_GET_IT(DMA_IT));
  1036. /* Determine the DMA to which belongs the stream */
  1037. if (DMAy_Streamx < DMA2_Stream0)
  1038. {
  1039. /* DMAy_Streamx belongs to DMA1 */
  1040. DMAy = DMA1;
  1041. }
  1042. else
  1043. {
  1044. /* DMAy_Streamx belongs to DMA2 */
  1045. DMAy = DMA2;
  1046. }
  1047. /* Check if the interrupt enable bit is in the CR or FCR register */
  1048. if ((DMA_IT & TRANSFER_IT_MASK) != (uint32_t)RESET)
  1049. {
  1050. /* Get the interrupt enable position mask in CR register */
  1051. tmpreg = (uint32_t)((DMA_IT >> 11) & TRANSFER_IT_ENABLE_MASK);
  1052. /* Check the enable bit in CR register */
  1053. enablestatus = (uint32_t)(DMAy_Streamx->CR & tmpreg);
  1054. }
  1055. else
  1056. {
  1057. /* Check the enable bit in FCR register */
  1058. enablestatus = (uint32_t)(DMAy_Streamx->FCR & DMA_IT_FE);
  1059. }
  1060. /* Check if the interrupt pending flag is in LISR or HISR */
  1061. if ((DMA_IT & HIGH_ISR_MASK) != (uint32_t)RESET)
  1062. {
  1063. /* Get DMAy HISR register value */
  1064. tmpreg = DMAy->HISR ;
  1065. }
  1066. else
  1067. {
  1068. /* Get DMAy LISR register value */
  1069. tmpreg = DMAy->LISR ;
  1070. }
  1071. /* mask all reserved bits */
  1072. tmpreg &= (uint32_t)RESERVED_MASK;
  1073. /* Check the status of the specified DMA interrupt */
  1074. if (((tmpreg & DMA_IT) != (uint32_t)RESET) && (enablestatus != (uint32_t)RESET))
  1075. {
  1076. /* DMA_IT is set */
  1077. bitstatus = SET;
  1078. }
  1079. else
  1080. {
  1081. /* DMA_IT is reset */
  1082. bitstatus = RESET;
  1083. }
  1084. /* Return the DMA_IT status */
  1085. return bitstatus;
  1086. }
  1087. /**
  1088. * @brief Clears the DMAy Streamx's interrupt pending bits.
  1089. * @param DMAy_Streamx: where y can be 1 or 2 to select the DMA and x can be 0
  1090. * to 7 to select the DMA Stream.
  1091. * @param DMA_IT: specifies the DMA interrupt pending bit to clear.
  1092. * This parameter can be any combination of the following values:
  1093. * @arg DMA_IT_TCIFx: Streamx transfer complete interrupt
  1094. * @arg DMA_IT_HTIFx: Streamx half transfer complete interrupt
  1095. * @arg DMA_IT_TEIFx: Streamx transfer error interrupt
  1096. * @arg DMA_IT_DMEIFx: Streamx direct mode error interrupt
  1097. * @arg DMA_IT_FEIFx: Streamx FIFO error interrupt
  1098. * Where x can be 0 to 7 to select the DMA Stream.
  1099. * @retval None
  1100. */
  1101. void DMA_ClearITPendingBit(DMA_Stream_TypeDef* DMAy_Streamx, uint32_t DMA_IT)
  1102. {
  1103. DMA_TypeDef* DMAy;
  1104. /* Check the parameters */
  1105. assert_param(IS_DMA_ALL_PERIPH(DMAy_Streamx));
  1106. assert_param(IS_DMA_CLEAR_IT(DMA_IT));
  1107. /* Determine the DMA to which belongs the stream */
  1108. if (DMAy_Streamx < DMA2_Stream0)
  1109. {
  1110. /* DMAy_Streamx belongs to DMA1 */
  1111. DMAy = DMA1;
  1112. }
  1113. else
  1114. {
  1115. /* DMAy_Streamx belongs to DMA2 */
  1116. DMAy = DMA2;
  1117. }
  1118. /* Check if LIFCR or HIFCR register is targeted */
  1119. if ((DMA_IT & HIGH_ISR_MASK) != (uint32_t)RESET)
  1120. {
  1121. /* Set DMAy HIFCR register clear interrupt bits */
  1122. DMAy->HIFCR = (uint32_t)(DMA_IT & RESERVED_MASK);
  1123. }
  1124. else
  1125. {
  1126. /* Set DMAy LIFCR register clear interrupt bits */
  1127. DMAy->LIFCR = (uint32_t)(DMA_IT & RESERVED_MASK);
  1128. }
  1129. }
  1130. /**
  1131. * @}
  1132. */
  1133. /**
  1134. * @}
  1135. */
  1136. /**
  1137. * @}
  1138. */
  1139. /**
  1140. * @}
  1141. */