stm32f4xx_dma.c 51 KB

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