stm32f10x_usart.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  1. /**
  2. ******************************************************************************
  3. * @file stm32f10x_usart.c
  4. * @author MCD Application Team
  5. * @version V3.5.0
  6. * @date 11-March-2011
  7. * @brief This file provides all the USART firmware functions.
  8. ******************************************************************************
  9. * @attention
  10. *
  11. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  12. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  13. * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  14. * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  15. * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  16. * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  17. *
  18. * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
  19. ******************************************************************************
  20. */
  21. /* Includes ------------------------------------------------------------------*/
  22. #include "stm32f10x_usart.h"
  23. #include "stm32f10x_rcc.h"
  24. /** @addtogroup STM32F10x_StdPeriph_Driver
  25. * @{
  26. */
  27. /** @defgroup USART
  28. * @brief USART driver modules
  29. * @{
  30. */
  31. /** @defgroup USART_Private_TypesDefinitions
  32. * @{
  33. */
  34. /**
  35. * @}
  36. */
  37. /** @defgroup USART_Private_Defines
  38. * @{
  39. */
  40. #define CR1_UE_Set ((uint16_t)0x2000) /*!< USART Enable Mask */
  41. #define CR1_UE_Reset ((uint16_t)0xDFFF) /*!< USART Disable Mask */
  42. #define CR1_WAKE_Mask ((uint16_t)0xF7FF) /*!< USART WakeUp Method Mask */
  43. #define CR1_RWU_Set ((uint16_t)0x0002) /*!< USART mute mode Enable Mask */
  44. #define CR1_RWU_Reset ((uint16_t)0xFFFD) /*!< USART mute mode Enable Mask */
  45. #define CR1_SBK_Set ((uint16_t)0x0001) /*!< USART Break Character send Mask */
  46. #define CR1_CLEAR_Mask ((uint16_t)0xE9F3) /*!< USART CR1 Mask */
  47. #define CR2_Address_Mask ((uint16_t)0xFFF0) /*!< USART address Mask */
  48. #define CR2_LINEN_Set ((uint16_t)0x4000) /*!< USART LIN Enable Mask */
  49. #define CR2_LINEN_Reset ((uint16_t)0xBFFF) /*!< USART LIN Disable Mask */
  50. #define CR2_LBDL_Mask ((uint16_t)0xFFDF) /*!< USART LIN Break detection Mask */
  51. #define CR2_STOP_CLEAR_Mask ((uint16_t)0xCFFF) /*!< USART CR2 STOP Bits Mask */
  52. #define CR2_CLOCK_CLEAR_Mask ((uint16_t)0xF0FF) /*!< USART CR2 Clock Mask */
  53. #define CR3_SCEN_Set ((uint16_t)0x0020) /*!< USART SC Enable Mask */
  54. #define CR3_SCEN_Reset ((uint16_t)0xFFDF) /*!< USART SC Disable Mask */
  55. #define CR3_NACK_Set ((uint16_t)0x0010) /*!< USART SC NACK Enable Mask */
  56. #define CR3_NACK_Reset ((uint16_t)0xFFEF) /*!< USART SC NACK Disable Mask */
  57. #define CR3_HDSEL_Set ((uint16_t)0x0008) /*!< USART Half-Duplex Enable Mask */
  58. #define CR3_HDSEL_Reset ((uint16_t)0xFFF7) /*!< USART Half-Duplex Disable Mask */
  59. #define CR3_IRLP_Mask ((uint16_t)0xFFFB) /*!< USART IrDA LowPower mode Mask */
  60. #define CR3_CLEAR_Mask ((uint16_t)0xFCFF) /*!< USART CR3 Mask */
  61. #define CR3_IREN_Set ((uint16_t)0x0002) /*!< USART IrDA Enable Mask */
  62. #define CR3_IREN_Reset ((uint16_t)0xFFFD) /*!< USART IrDA Disable Mask */
  63. #define GTPR_LSB_Mask ((uint16_t)0x00FF) /*!< Guard Time Register LSB Mask */
  64. #define GTPR_MSB_Mask ((uint16_t)0xFF00) /*!< Guard Time Register MSB Mask */
  65. #define IT_Mask ((uint16_t)0x001F) /*!< USART Interrupt Mask */
  66. /* USART OverSampling-8 Mask */
  67. #define CR1_OVER8_Set ((u16)0x8000) /* USART OVER8 mode Enable Mask */
  68. #define CR1_OVER8_Reset ((u16)0x7FFF) /* USART OVER8 mode Disable Mask */
  69. /* USART One Bit Sampling Mask */
  70. #define CR3_ONEBITE_Set ((u16)0x0800) /* USART ONEBITE mode Enable Mask */
  71. #define CR3_ONEBITE_Reset ((u16)0xF7FF) /* USART ONEBITE mode Disable Mask */
  72. /**
  73. * @}
  74. */
  75. /** @defgroup USART_Private_Macros
  76. * @{
  77. */
  78. /**
  79. * @}
  80. */
  81. /** @defgroup USART_Private_Variables
  82. * @{
  83. */
  84. /**
  85. * @}
  86. */
  87. /** @defgroup USART_Private_FunctionPrototypes
  88. * @{
  89. */
  90. /**
  91. * @}
  92. */
  93. /** @defgroup USART_Private_Functions
  94. * @{
  95. */
  96. /**
  97. * @brief Deinitializes the USARTx peripheral registers to their default reset values.
  98. * @param USARTx: Select the USART or the UART peripheral.
  99. * This parameter can be one of the following values:
  100. * USART1, USART2, USART3, UART4 or UART5.
  101. * @retval None
  102. */
  103. void USART_DeInit(USART_TypeDef* USARTx)
  104. {
  105. /* Check the parameters */
  106. assert_param(IS_USART_ALL_PERIPH(USARTx));
  107. if (USARTx == USART1)
  108. {
  109. RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, ENABLE);
  110. RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, DISABLE);
  111. }
  112. else if (USARTx == USART2)
  113. {
  114. RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, ENABLE);
  115. RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, DISABLE);
  116. }
  117. else if (USARTx == USART3)
  118. {
  119. RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, ENABLE);
  120. RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART3, DISABLE);
  121. }
  122. else if (USARTx == UART4)
  123. {
  124. RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART4, ENABLE);
  125. RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART4, DISABLE);
  126. }
  127. else
  128. {
  129. if (USARTx == UART5)
  130. {
  131. RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART5, ENABLE);
  132. RCC_APB1PeriphResetCmd(RCC_APB1Periph_UART5, DISABLE);
  133. }
  134. }
  135. }
  136. /**
  137. * @brief Initializes the USARTx peripheral according to the specified
  138. * parameters in the USART_InitStruct .
  139. * @param USARTx: Select the USART or the UART peripheral.
  140. * This parameter can be one of the following values:
  141. * USART1, USART2, USART3, UART4 or UART5.
  142. * @param USART_InitStruct: pointer to a USART_InitTypeDef structure
  143. * that contains the configuration information for the specified USART
  144. * peripheral.
  145. * @retval None
  146. */
  147. void USART_Init(USART_TypeDef* USARTx, USART_InitTypeDef* USART_InitStruct)
  148. {
  149. uint32_t tmpreg = 0x00, apbclock = 0x00;
  150. uint32_t integerdivider = 0x00;
  151. uint32_t fractionaldivider = 0x00;
  152. uint32_t usartxbase = 0;
  153. RCC_ClocksTypeDef RCC_ClocksStatus;
  154. /* Check the parameters */
  155. assert_param(IS_USART_ALL_PERIPH(USARTx));
  156. assert_param(IS_USART_BAUDRATE(USART_InitStruct->USART_BaudRate));
  157. assert_param(IS_USART_WORD_LENGTH(USART_InitStruct->USART_WordLength));
  158. assert_param(IS_USART_STOPBITS(USART_InitStruct->USART_StopBits));
  159. assert_param(IS_USART_PARITY(USART_InitStruct->USART_Parity));
  160. assert_param(IS_USART_MODE(USART_InitStruct->USART_Mode));
  161. assert_param(IS_USART_HARDWARE_FLOW_CONTROL(USART_InitStruct->USART_HardwareFlowControl));
  162. /* The hardware flow control is available only for USART1, USART2 and USART3 */
  163. if (USART_InitStruct->USART_HardwareFlowControl != USART_HardwareFlowControl_None)
  164. {
  165. assert_param(IS_USART_123_PERIPH(USARTx));
  166. }
  167. usartxbase = (uint32_t)USARTx;
  168. /*---------------------------- USART CR2 Configuration -----------------------*/
  169. tmpreg = USARTx->CR2;
  170. /* Clear STOP[13:12] bits */
  171. tmpreg &= CR2_STOP_CLEAR_Mask;
  172. /* Configure the USART Stop Bits, Clock, CPOL, CPHA and LastBit ------------*/
  173. /* Set STOP[13:12] bits according to USART_StopBits value */
  174. tmpreg |= (uint32_t)USART_InitStruct->USART_StopBits;
  175. /* Write to USART CR2 */
  176. USARTx->CR2 = (uint16_t)tmpreg;
  177. /*---------------------------- USART CR1 Configuration -----------------------*/
  178. tmpreg = USARTx->CR1;
  179. /* Clear M, PCE, PS, TE and RE bits */
  180. tmpreg &= CR1_CLEAR_Mask;
  181. /* Configure the USART Word Length, Parity and mode ----------------------- */
  182. /* Set the M bits according to USART_WordLength value */
  183. /* Set PCE and PS bits according to USART_Parity value */
  184. /* Set TE and RE bits according to USART_Mode value */
  185. tmpreg |= (uint32_t)USART_InitStruct->USART_WordLength | USART_InitStruct->USART_Parity |
  186. USART_InitStruct->USART_Mode;
  187. /* Write to USART CR1 */
  188. USARTx->CR1 = (uint16_t)tmpreg;
  189. /*---------------------------- USART CR3 Configuration -----------------------*/
  190. tmpreg = USARTx->CR3;
  191. /* Clear CTSE and RTSE bits */
  192. tmpreg &= CR3_CLEAR_Mask;
  193. /* Configure the USART HFC -------------------------------------------------*/
  194. /* Set CTSE and RTSE bits according to USART_HardwareFlowControl value */
  195. tmpreg |= USART_InitStruct->USART_HardwareFlowControl;
  196. /* Write to USART CR3 */
  197. USARTx->CR3 = (uint16_t)tmpreg;
  198. /*---------------------------- USART BRR Configuration -----------------------*/
  199. /* Configure the USART Baud Rate -------------------------------------------*/
  200. RCC_GetClocksFreq(&RCC_ClocksStatus);
  201. if (usartxbase == USART1_BASE)
  202. {
  203. apbclock = RCC_ClocksStatus.PCLK2_Frequency;
  204. }
  205. else
  206. {
  207. apbclock = RCC_ClocksStatus.PCLK1_Frequency;
  208. }
  209. /* Determine the integer part */
  210. if ((USARTx->CR1 & CR1_OVER8_Set) != 0)
  211. {
  212. /* Integer part computing in case Oversampling mode is 8 Samples */
  213. integerdivider = ((25 * apbclock) / (2 * (USART_InitStruct->USART_BaudRate)));
  214. }
  215. else /* if ((USARTx->CR1 & CR1_OVER8_Set) == 0) */
  216. {
  217. /* Integer part computing in case Oversampling mode is 16 Samples */
  218. integerdivider = ((25 * apbclock) / (4 * (USART_InitStruct->USART_BaudRate)));
  219. }
  220. tmpreg = (integerdivider / 100) << 4;
  221. /* Determine the fractional part */
  222. fractionaldivider = integerdivider - (100 * (tmpreg >> 4));
  223. /* Implement the fractional part in the register */
  224. if ((USARTx->CR1 & CR1_OVER8_Set) != 0)
  225. {
  226. tmpreg |= ((((fractionaldivider * 8) + 50) / 100)) & ((uint8_t)0x07);
  227. }
  228. else /* if ((USARTx->CR1 & CR1_OVER8_Set) == 0) */
  229. {
  230. tmpreg |= ((((fractionaldivider * 16) + 50) / 100)) & ((uint8_t)0x0F);
  231. }
  232. /* Write to USART BRR */
  233. USARTx->BRR = (uint16_t)tmpreg;
  234. }
  235. /**
  236. * @brief Fills each USART_InitStruct member with its default value.
  237. * @param USART_InitStruct: pointer to a USART_InitTypeDef structure
  238. * which will be initialized.
  239. * @retval None
  240. */
  241. void USART_StructInit(USART_InitTypeDef* USART_InitStruct)
  242. {
  243. /* USART_InitStruct members default value */
  244. USART_InitStruct->USART_BaudRate = 9600;
  245. USART_InitStruct->USART_WordLength = USART_WordLength_8b;
  246. USART_InitStruct->USART_StopBits = USART_StopBits_1;
  247. USART_InitStruct->USART_Parity = USART_Parity_No ;
  248. USART_InitStruct->USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  249. USART_InitStruct->USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  250. }
  251. /**
  252. * @brief Initializes the USARTx peripheral Clock according to the
  253. * specified parameters in the USART_ClockInitStruct .
  254. * @param USARTx: where x can be 1, 2, 3 to select the USART peripheral.
  255. * @param USART_ClockInitStruct: pointer to a USART_ClockInitTypeDef
  256. * structure that contains the configuration information for the specified
  257. * USART peripheral.
  258. * @note The Smart Card and Synchronous modes are not available for UART4 and UART5.
  259. * @retval None
  260. */
  261. void USART_ClockInit(USART_TypeDef* USARTx, USART_ClockInitTypeDef* USART_ClockInitStruct)
  262. {
  263. uint32_t tmpreg = 0x00;
  264. /* Check the parameters */
  265. assert_param(IS_USART_123_PERIPH(USARTx));
  266. assert_param(IS_USART_CLOCK(USART_ClockInitStruct->USART_Clock));
  267. assert_param(IS_USART_CPOL(USART_ClockInitStruct->USART_CPOL));
  268. assert_param(IS_USART_CPHA(USART_ClockInitStruct->USART_CPHA));
  269. assert_param(IS_USART_LASTBIT(USART_ClockInitStruct->USART_LastBit));
  270. /*---------------------------- USART CR2 Configuration -----------------------*/
  271. tmpreg = USARTx->CR2;
  272. /* Clear CLKEN, CPOL, CPHA and LBCL bits */
  273. tmpreg &= CR2_CLOCK_CLEAR_Mask;
  274. /* Configure the USART Clock, CPOL, CPHA and LastBit ------------*/
  275. /* Set CLKEN bit according to USART_Clock value */
  276. /* Set CPOL bit according to USART_CPOL value */
  277. /* Set CPHA bit according to USART_CPHA value */
  278. /* Set LBCL bit according to USART_LastBit value */
  279. tmpreg |= (uint32_t)USART_ClockInitStruct->USART_Clock | USART_ClockInitStruct->USART_CPOL |
  280. USART_ClockInitStruct->USART_CPHA | USART_ClockInitStruct->USART_LastBit;
  281. /* Write to USART CR2 */
  282. USARTx->CR2 = (uint16_t)tmpreg;
  283. }
  284. /**
  285. * @brief Fills each USART_ClockInitStruct member with its default value.
  286. * @param USART_ClockInitStruct: pointer to a USART_ClockInitTypeDef
  287. * structure which will be initialized.
  288. * @retval None
  289. */
  290. void USART_ClockStructInit(USART_ClockInitTypeDef* USART_ClockInitStruct)
  291. {
  292. /* USART_ClockInitStruct members default value */
  293. USART_ClockInitStruct->USART_Clock = USART_Clock_Disable;
  294. USART_ClockInitStruct->USART_CPOL = USART_CPOL_Low;
  295. USART_ClockInitStruct->USART_CPHA = USART_CPHA_1Edge;
  296. USART_ClockInitStruct->USART_LastBit = USART_LastBit_Disable;
  297. }
  298. /**
  299. * @brief Enables or disables the specified USART peripheral.
  300. * @param USARTx: Select the USART or the UART peripheral.
  301. * This parameter can be one of the following values:
  302. * USART1, USART2, USART3, UART4 or UART5.
  303. * @param NewState: new state of the USARTx peripheral.
  304. * This parameter can be: ENABLE or DISABLE.
  305. * @retval None
  306. */
  307. void USART_Cmd(USART_TypeDef* USARTx, FunctionalState NewState)
  308. {
  309. /* Check the parameters */
  310. assert_param(IS_USART_ALL_PERIPH(USARTx));
  311. assert_param(IS_FUNCTIONAL_STATE(NewState));
  312. if (NewState != DISABLE)
  313. {
  314. /* Enable the selected USART by setting the UE bit in the CR1 register */
  315. USARTx->CR1 |= CR1_UE_Set;
  316. }
  317. else
  318. {
  319. /* Disable the selected USART by clearing the UE bit in the CR1 register */
  320. USARTx->CR1 &= CR1_UE_Reset;
  321. }
  322. }
  323. /**
  324. * @brief Enables or disables the specified USART interrupts.
  325. * @param USARTx: Select the USART or the UART peripheral.
  326. * This parameter can be one of the following values:
  327. * USART1, USART2, USART3, UART4 or UART5.
  328. * @param USART_IT: specifies the USART interrupt sources to be enabled or disabled.
  329. * This parameter can be one of the following values:
  330. * @arg USART_IT_CTS: CTS change interrupt (not available for UART4 and UART5)
  331. * @arg USART_IT_LBD: LIN Break detection interrupt
  332. * @arg USART_IT_TXE: Transmit Data Register empty interrupt
  333. * @arg USART_IT_TC: Transmission complete interrupt
  334. * @arg USART_IT_RXNE: Receive Data register not empty interrupt
  335. * @arg USART_IT_IDLE: Idle line detection interrupt
  336. * @arg USART_IT_PE: Parity Error interrupt
  337. * @arg USART_IT_ERR: Error interrupt(Frame error, noise error, overrun error)
  338. * @param NewState: new state of the specified USARTx interrupts.
  339. * This parameter can be: ENABLE or DISABLE.
  340. * @retval None
  341. */
  342. void USART_ITConfig(USART_TypeDef* USARTx, uint16_t USART_IT, FunctionalState NewState)
  343. {
  344. uint32_t usartreg = 0x00, itpos = 0x00, itmask = 0x00;
  345. uint32_t usartxbase = 0x00;
  346. /* Check the parameters */
  347. assert_param(IS_USART_ALL_PERIPH(USARTx));
  348. assert_param(IS_USART_CONFIG_IT(USART_IT));
  349. assert_param(IS_FUNCTIONAL_STATE(NewState));
  350. /* The CTS interrupt is not available for UART4 and UART5 */
  351. if (USART_IT == USART_IT_CTS)
  352. {
  353. assert_param(IS_USART_123_PERIPH(USARTx));
  354. }
  355. usartxbase = (uint32_t)USARTx;
  356. /* Get the USART register index */
  357. usartreg = (((uint8_t)USART_IT) >> 0x05);
  358. /* Get the interrupt position */
  359. itpos = USART_IT & IT_Mask;
  360. itmask = (((uint32_t)0x01) << itpos);
  361. if (usartreg == 0x01) /* The IT is in CR1 register */
  362. {
  363. usartxbase += 0x0C;
  364. }
  365. else if (usartreg == 0x02) /* The IT is in CR2 register */
  366. {
  367. usartxbase += 0x10;
  368. }
  369. else /* The IT is in CR3 register */
  370. {
  371. usartxbase += 0x14;
  372. }
  373. if (NewState != DISABLE)
  374. {
  375. *(__IO uint32_t*)usartxbase |= itmask;
  376. }
  377. else
  378. {
  379. *(__IO uint32_t*)usartxbase &= ~itmask;
  380. }
  381. }
  382. /**
  383. * @brief Enables or disables the USART’s DMA interface.
  384. * @param USARTx: Select the USART or the UART peripheral.
  385. * This parameter can be one of the following values:
  386. * USART1, USART2, USART3, UART4 or UART5.
  387. * @param USART_DMAReq: specifies the DMA request.
  388. * This parameter can be any combination of the following values:
  389. * @arg USART_DMAReq_Tx: USART DMA transmit request
  390. * @arg USART_DMAReq_Rx: USART DMA receive request
  391. * @param NewState: new state of the DMA Request sources.
  392. * This parameter can be: ENABLE or DISABLE.
  393. * @note The DMA mode is not available for UART5 except in the STM32
  394. * High density value line devices(STM32F10X_HD_VL).
  395. * @retval None
  396. */
  397. void USART_DMACmd(USART_TypeDef* USARTx, uint16_t USART_DMAReq, FunctionalState NewState)
  398. {
  399. /* Check the parameters */
  400. assert_param(IS_USART_ALL_PERIPH(USARTx));
  401. assert_param(IS_USART_DMAREQ(USART_DMAReq));
  402. assert_param(IS_FUNCTIONAL_STATE(NewState));
  403. if (NewState != DISABLE)
  404. {
  405. /* Enable the DMA transfer for selected requests by setting the DMAT and/or
  406. DMAR bits in the USART CR3 register */
  407. USARTx->CR3 |= USART_DMAReq;
  408. }
  409. else
  410. {
  411. /* Disable the DMA transfer for selected requests by clearing the DMAT and/or
  412. DMAR bits in the USART CR3 register */
  413. USARTx->CR3 &= (uint16_t)~USART_DMAReq;
  414. }
  415. }
  416. /**
  417. * @brief Sets the address of the USART node.
  418. * @param USARTx: Select the USART or the UART peripheral.
  419. * This parameter can be one of the following values:
  420. * USART1, USART2, USART3, UART4 or UART5.
  421. * @param USART_Address: Indicates the address of the USART node.
  422. * @retval None
  423. */
  424. void USART_SetAddress(USART_TypeDef* USARTx, uint8_t USART_Address)
  425. {
  426. /* Check the parameters */
  427. assert_param(IS_USART_ALL_PERIPH(USARTx));
  428. assert_param(IS_USART_ADDRESS(USART_Address));
  429. /* Clear the USART address */
  430. USARTx->CR2 &= CR2_Address_Mask;
  431. /* Set the USART address node */
  432. USARTx->CR2 |= USART_Address;
  433. }
  434. /**
  435. * @brief Selects the USART WakeUp method.
  436. * @param USARTx: Select the USART or the UART peripheral.
  437. * This parameter can be one of the following values:
  438. * USART1, USART2, USART3, UART4 or UART5.
  439. * @param USART_WakeUp: specifies the USART wakeup method.
  440. * This parameter can be one of the following values:
  441. * @arg USART_WakeUp_IdleLine: WakeUp by an idle line detection
  442. * @arg USART_WakeUp_AddressMark: WakeUp by an address mark
  443. * @retval None
  444. */
  445. void USART_WakeUpConfig(USART_TypeDef* USARTx, uint16_t USART_WakeUp)
  446. {
  447. /* Check the parameters */
  448. assert_param(IS_USART_ALL_PERIPH(USARTx));
  449. assert_param(IS_USART_WAKEUP(USART_WakeUp));
  450. USARTx->CR1 &= CR1_WAKE_Mask;
  451. USARTx->CR1 |= USART_WakeUp;
  452. }
  453. /**
  454. * @brief Determines if the USART is in mute mode or not.
  455. * @param USARTx: Select the USART or the UART peripheral.
  456. * This parameter can be one of the following values:
  457. * USART1, USART2, USART3, UART4 or UART5.
  458. * @param NewState: new state of the USART mute mode.
  459. * This parameter can be: ENABLE or DISABLE.
  460. * @retval None
  461. */
  462. void USART_ReceiverWakeUpCmd(USART_TypeDef* USARTx, FunctionalState NewState)
  463. {
  464. /* Check the parameters */
  465. assert_param(IS_USART_ALL_PERIPH(USARTx));
  466. assert_param(IS_FUNCTIONAL_STATE(NewState));
  467. if (NewState != DISABLE)
  468. {
  469. /* Enable the USART mute mode by setting the RWU bit in the CR1 register */
  470. USARTx->CR1 |= CR1_RWU_Set;
  471. }
  472. else
  473. {
  474. /* Disable the USART mute mode by clearing the RWU bit in the CR1 register */
  475. USARTx->CR1 &= CR1_RWU_Reset;
  476. }
  477. }
  478. /**
  479. * @brief Sets the USART LIN Break detection length.
  480. * @param USARTx: Select the USART or the UART peripheral.
  481. * This parameter can be one of the following values:
  482. * USART1, USART2, USART3, UART4 or UART5.
  483. * @param USART_LINBreakDetectLength: specifies the LIN break detection length.
  484. * This parameter can be one of the following values:
  485. * @arg USART_LINBreakDetectLength_10b: 10-bit break detection
  486. * @arg USART_LINBreakDetectLength_11b: 11-bit break detection
  487. * @retval None
  488. */
  489. void USART_LINBreakDetectLengthConfig(USART_TypeDef* USARTx, uint16_t USART_LINBreakDetectLength)
  490. {
  491. /* Check the parameters */
  492. assert_param(IS_USART_ALL_PERIPH(USARTx));
  493. assert_param(IS_USART_LIN_BREAK_DETECT_LENGTH(USART_LINBreakDetectLength));
  494. USARTx->CR2 &= CR2_LBDL_Mask;
  495. USARTx->CR2 |= USART_LINBreakDetectLength;
  496. }
  497. /**
  498. * @brief Enables or disables the USART’s LIN mode.
  499. * @param USARTx: Select the USART or the UART peripheral.
  500. * This parameter can be one of the following values:
  501. * USART1, USART2, USART3, UART4 or UART5.
  502. * @param NewState: new state of the USART LIN mode.
  503. * This parameter can be: ENABLE or DISABLE.
  504. * @retval None
  505. */
  506. void USART_LINCmd(USART_TypeDef* USARTx, FunctionalState NewState)
  507. {
  508. /* Check the parameters */
  509. assert_param(IS_USART_ALL_PERIPH(USARTx));
  510. assert_param(IS_FUNCTIONAL_STATE(NewState));
  511. if (NewState != DISABLE)
  512. {
  513. /* Enable the LIN mode by setting the LINEN bit in the CR2 register */
  514. USARTx->CR2 |= CR2_LINEN_Set;
  515. }
  516. else
  517. {
  518. /* Disable the LIN mode by clearing the LINEN bit in the CR2 register */
  519. USARTx->CR2 &= CR2_LINEN_Reset;
  520. }
  521. }
  522. /**
  523. * @brief Transmits single data through the USARTx peripheral.
  524. * @param USARTx: Select the USART or the UART peripheral.
  525. * This parameter can be one of the following values:
  526. * USART1, USART2, USART3, UART4 or UART5.
  527. * @param Data: the data to transmit.
  528. * @retval None
  529. */
  530. void USART_SendData(USART_TypeDef* USARTx, uint16_t Data)
  531. {
  532. /* Check the parameters */
  533. assert_param(IS_USART_ALL_PERIPH(USARTx));
  534. assert_param(IS_USART_DATA(Data));
  535. /* Transmit Data */
  536. USARTx->DR = (Data & (uint16_t)0x01FF);
  537. }
  538. /**
  539. * @brief Returns the most recent received data by the USARTx peripheral.
  540. * @param USARTx: Select the USART or the UART peripheral.
  541. * This parameter can be one of the following values:
  542. * USART1, USART2, USART3, UART4 or UART5.
  543. * @retval The received data.
  544. */
  545. uint16_t USART_ReceiveData(USART_TypeDef* USARTx)
  546. {
  547. /* Check the parameters */
  548. assert_param(IS_USART_ALL_PERIPH(USARTx));
  549. /* Receive Data */
  550. return (uint16_t)(USARTx->DR & (uint16_t)0x01FF);
  551. }
  552. /**
  553. * @brief Transmits break characters.
  554. * @param USARTx: Select the USART or the UART peripheral.
  555. * This parameter can be one of the following values:
  556. * USART1, USART2, USART3, UART4 or UART5.
  557. * @retval None
  558. */
  559. void USART_SendBreak(USART_TypeDef* USARTx)
  560. {
  561. /* Check the parameters */
  562. assert_param(IS_USART_ALL_PERIPH(USARTx));
  563. /* Send break characters */
  564. USARTx->CR1 |= CR1_SBK_Set;
  565. }
  566. /**
  567. * @brief Sets the specified USART guard time.
  568. * @param USARTx: where x can be 1, 2 or 3 to select the USART peripheral.
  569. * @param USART_GuardTime: specifies the guard time.
  570. * @note The guard time bits are not available for UART4 and UART5.
  571. * @retval None
  572. */
  573. void USART_SetGuardTime(USART_TypeDef* USARTx, uint8_t USART_GuardTime)
  574. {
  575. /* Check the parameters */
  576. assert_param(IS_USART_123_PERIPH(USARTx));
  577. /* Clear the USART Guard time */
  578. USARTx->GTPR &= GTPR_LSB_Mask;
  579. /* Set the USART guard time */
  580. USARTx->GTPR |= (uint16_t)((uint16_t)USART_GuardTime << 0x08);
  581. }
  582. /**
  583. * @brief Sets the system clock prescaler.
  584. * @param USARTx: Select the USART or the UART peripheral.
  585. * This parameter can be one of the following values:
  586. * USART1, USART2, USART3, UART4 or UART5.
  587. * @param USART_Prescaler: specifies the prescaler clock.
  588. * @note The function is used for IrDA mode with UART4 and UART5.
  589. * @retval None
  590. */
  591. void USART_SetPrescaler(USART_TypeDef* USARTx, uint8_t USART_Prescaler)
  592. {
  593. /* Check the parameters */
  594. assert_param(IS_USART_ALL_PERIPH(USARTx));
  595. /* Clear the USART prescaler */
  596. USARTx->GTPR &= GTPR_MSB_Mask;
  597. /* Set the USART prescaler */
  598. USARTx->GTPR |= USART_Prescaler;
  599. }
  600. /**
  601. * @brief Enables or disables the USART’s Smart Card mode.
  602. * @param USARTx: where x can be 1, 2 or 3 to select the USART peripheral.
  603. * @param NewState: new state of the Smart Card mode.
  604. * This parameter can be: ENABLE or DISABLE.
  605. * @note The Smart Card mode is not available for UART4 and UART5.
  606. * @retval None
  607. */
  608. void USART_SmartCardCmd(USART_TypeDef* USARTx, FunctionalState NewState)
  609. {
  610. /* Check the parameters */
  611. assert_param(IS_USART_123_PERIPH(USARTx));
  612. assert_param(IS_FUNCTIONAL_STATE(NewState));
  613. if (NewState != DISABLE)
  614. {
  615. /* Enable the SC mode by setting the SCEN bit in the CR3 register */
  616. USARTx->CR3 |= CR3_SCEN_Set;
  617. }
  618. else
  619. {
  620. /* Disable the SC mode by clearing the SCEN bit in the CR3 register */
  621. USARTx->CR3 &= CR3_SCEN_Reset;
  622. }
  623. }
  624. /**
  625. * @brief Enables or disables NACK transmission.
  626. * @param USARTx: where x can be 1, 2 or 3 to select the USART peripheral.
  627. * @param NewState: new state of the NACK transmission.
  628. * This parameter can be: ENABLE or DISABLE.
  629. * @note The Smart Card mode is not available for UART4 and UART5.
  630. * @retval None
  631. */
  632. void USART_SmartCardNACKCmd(USART_TypeDef* USARTx, FunctionalState NewState)
  633. {
  634. /* Check the parameters */
  635. assert_param(IS_USART_123_PERIPH(USARTx));
  636. assert_param(IS_FUNCTIONAL_STATE(NewState));
  637. if (NewState != DISABLE)
  638. {
  639. /* Enable the NACK transmission by setting the NACK bit in the CR3 register */
  640. USARTx->CR3 |= CR3_NACK_Set;
  641. }
  642. else
  643. {
  644. /* Disable the NACK transmission by clearing the NACK bit in the CR3 register */
  645. USARTx->CR3 &= CR3_NACK_Reset;
  646. }
  647. }
  648. /**
  649. * @brief Enables or disables the USART’s Half Duplex communication.
  650. * @param USARTx: Select the USART or the UART peripheral.
  651. * This parameter can be one of the following values:
  652. * USART1, USART2, USART3, UART4 or UART5.
  653. * @param NewState: new state of the USART Communication.
  654. * This parameter can be: ENABLE or DISABLE.
  655. * @retval None
  656. */
  657. void USART_HalfDuplexCmd(USART_TypeDef* USARTx, FunctionalState NewState)
  658. {
  659. /* Check the parameters */
  660. assert_param(IS_USART_ALL_PERIPH(USARTx));
  661. assert_param(IS_FUNCTIONAL_STATE(NewState));
  662. if (NewState != DISABLE)
  663. {
  664. /* Enable the Half-Duplex mode by setting the HDSEL bit in the CR3 register */
  665. USARTx->CR3 |= CR3_HDSEL_Set;
  666. }
  667. else
  668. {
  669. /* Disable the Half-Duplex mode by clearing the HDSEL bit in the CR3 register */
  670. USARTx->CR3 &= CR3_HDSEL_Reset;
  671. }
  672. }
  673. /**
  674. * @brief Enables or disables the USART's 8x oversampling mode.
  675. * @param USARTx: Select the USART or the UART peripheral.
  676. * This parameter can be one of the following values:
  677. * USART1, USART2, USART3, UART4 or UART5.
  678. * @param NewState: new state of the USART one bit sampling method.
  679. * This parameter can be: ENABLE or DISABLE.
  680. * @note
  681. * This function has to be called before calling USART_Init()
  682. * function in order to have correct baudrate Divider value.
  683. * @retval None
  684. */
  685. void USART_OverSampling8Cmd(USART_TypeDef* USARTx, FunctionalState NewState)
  686. {
  687. /* Check the parameters */
  688. assert_param(IS_USART_ALL_PERIPH(USARTx));
  689. assert_param(IS_FUNCTIONAL_STATE(NewState));
  690. if (NewState != DISABLE)
  691. {
  692. /* Enable the 8x Oversampling mode by setting the OVER8 bit in the CR1 register */
  693. USARTx->CR1 |= CR1_OVER8_Set;
  694. }
  695. else
  696. {
  697. /* Disable the 8x Oversampling mode by clearing the OVER8 bit in the CR1 register */
  698. USARTx->CR1 &= CR1_OVER8_Reset;
  699. }
  700. }
  701. /**
  702. * @brief Enables or disables the USART's one bit sampling method.
  703. * @param USARTx: Select the USART or the UART peripheral.
  704. * This parameter can be one of the following values:
  705. * USART1, USART2, USART3, UART4 or UART5.
  706. * @param NewState: new state of the USART one bit sampling method.
  707. * This parameter can be: ENABLE or DISABLE.
  708. * @retval None
  709. */
  710. void USART_OneBitMethodCmd(USART_TypeDef* USARTx, FunctionalState NewState)
  711. {
  712. /* Check the parameters */
  713. assert_param(IS_USART_ALL_PERIPH(USARTx));
  714. assert_param(IS_FUNCTIONAL_STATE(NewState));
  715. if (NewState != DISABLE)
  716. {
  717. /* Enable the one bit method by setting the ONEBITE bit in the CR3 register */
  718. USARTx->CR3 |= CR3_ONEBITE_Set;
  719. }
  720. else
  721. {
  722. /* Disable tthe one bit method by clearing the ONEBITE bit in the CR3 register */
  723. USARTx->CR3 &= CR3_ONEBITE_Reset;
  724. }
  725. }
  726. /**
  727. * @brief Configures the USART's IrDA interface.
  728. * @param USARTx: Select the USART or the UART peripheral.
  729. * This parameter can be one of the following values:
  730. * USART1, USART2, USART3, UART4 or UART5.
  731. * @param USART_IrDAMode: specifies the IrDA mode.
  732. * This parameter can be one of the following values:
  733. * @arg USART_IrDAMode_LowPower
  734. * @arg USART_IrDAMode_Normal
  735. * @retval None
  736. */
  737. void USART_IrDAConfig(USART_TypeDef* USARTx, uint16_t USART_IrDAMode)
  738. {
  739. /* Check the parameters */
  740. assert_param(IS_USART_ALL_PERIPH(USARTx));
  741. assert_param(IS_USART_IRDA_MODE(USART_IrDAMode));
  742. USARTx->CR3 &= CR3_IRLP_Mask;
  743. USARTx->CR3 |= USART_IrDAMode;
  744. }
  745. /**
  746. * @brief Enables or disables the USART's IrDA interface.
  747. * @param USARTx: Select the USART or the UART peripheral.
  748. * This parameter can be one of the following values:
  749. * USART1, USART2, USART3, UART4 or UART5.
  750. * @param NewState: new state of the IrDA mode.
  751. * This parameter can be: ENABLE or DISABLE.
  752. * @retval None
  753. */
  754. void USART_IrDACmd(USART_TypeDef* USARTx, FunctionalState NewState)
  755. {
  756. /* Check the parameters */
  757. assert_param(IS_USART_ALL_PERIPH(USARTx));
  758. assert_param(IS_FUNCTIONAL_STATE(NewState));
  759. if (NewState != DISABLE)
  760. {
  761. /* Enable the IrDA mode by setting the IREN bit in the CR3 register */
  762. USARTx->CR3 |= CR3_IREN_Set;
  763. }
  764. else
  765. {
  766. /* Disable the IrDA mode by clearing the IREN bit in the CR3 register */
  767. USARTx->CR3 &= CR3_IREN_Reset;
  768. }
  769. }
  770. /**
  771. * @brief Checks whether the specified USART flag is set or not.
  772. * @param USARTx: Select the USART or the UART peripheral.
  773. * This parameter can be one of the following values:
  774. * USART1, USART2, USART3, UART4 or UART5.
  775. * @param USART_FLAG: specifies the flag to check.
  776. * This parameter can be one of the following values:
  777. * @arg USART_FLAG_CTS: CTS Change flag (not available for UART4 and UART5)
  778. * @arg USART_FLAG_LBD: LIN Break detection flag
  779. * @arg USART_FLAG_TXE: Transmit data register empty flag
  780. * @arg USART_FLAG_TC: Transmission Complete flag
  781. * @arg USART_FLAG_RXNE: Receive data register not empty flag
  782. * @arg USART_FLAG_IDLE: Idle Line detection flag
  783. * @arg USART_FLAG_ORE: OverRun Error flag
  784. * @arg USART_FLAG_NE: Noise Error flag
  785. * @arg USART_FLAG_FE: Framing Error flag
  786. * @arg USART_FLAG_PE: Parity Error flag
  787. * @retval The new state of USART_FLAG (SET or RESET).
  788. */
  789. FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, uint16_t USART_FLAG)
  790. {
  791. FlagStatus bitstatus = RESET;
  792. /* Check the parameters */
  793. assert_param(IS_USART_ALL_PERIPH(USARTx));
  794. assert_param(IS_USART_FLAG(USART_FLAG));
  795. /* The CTS flag is not available for UART4 and UART5 */
  796. if (USART_FLAG == USART_FLAG_CTS)
  797. {
  798. assert_param(IS_USART_123_PERIPH(USARTx));
  799. }
  800. if ((USARTx->SR & USART_FLAG) != (uint16_t)RESET)
  801. {
  802. bitstatus = SET;
  803. }
  804. else
  805. {
  806. bitstatus = RESET;
  807. }
  808. return bitstatus;
  809. }
  810. /**
  811. * @brief Clears the USARTx's pending flags.
  812. * @param USARTx: Select the USART or the UART peripheral.
  813. * This parameter can be one of the following values:
  814. * USART1, USART2, USART3, UART4 or UART5.
  815. * @param USART_FLAG: specifies the flag to clear.
  816. * This parameter can be any combination of the following values:
  817. * @arg USART_FLAG_CTS: CTS Change flag (not available for UART4 and UART5).
  818. * @arg USART_FLAG_LBD: LIN Break detection flag.
  819. * @arg USART_FLAG_TC: Transmission Complete flag.
  820. * @arg USART_FLAG_RXNE: Receive data register not empty flag.
  821. *
  822. * @note
  823. * - PE (Parity error), FE (Framing error), NE (Noise error), ORE (OverRun
  824. * error) and IDLE (Idle line detected) flags are cleared by software
  825. * sequence: a read operation to USART_SR register (USART_GetFlagStatus())
  826. * followed by a read operation to USART_DR register (USART_ReceiveData()).
  827. * - RXNE flag can be also cleared by a read to the USART_DR register
  828. * (USART_ReceiveData()).
  829. * - TC flag can be also cleared by software sequence: a read operation to
  830. * USART_SR register (USART_GetFlagStatus()) followed by a write operation
  831. * to USART_DR register (USART_SendData()).
  832. * - TXE flag is cleared only by a write to the USART_DR register
  833. * (USART_SendData()).
  834. * @retval None
  835. */
  836. void USART_ClearFlag(USART_TypeDef* USARTx, uint16_t USART_FLAG)
  837. {
  838. /* Check the parameters */
  839. assert_param(IS_USART_ALL_PERIPH(USARTx));
  840. assert_param(IS_USART_CLEAR_FLAG(USART_FLAG));
  841. /* The CTS flag is not available for UART4 and UART5 */
  842. if ((USART_FLAG & USART_FLAG_CTS) == USART_FLAG_CTS)
  843. {
  844. assert_param(IS_USART_123_PERIPH(USARTx));
  845. }
  846. USARTx->SR = (uint16_t)~USART_FLAG;
  847. }
  848. /**
  849. * @brief Checks whether the specified USART interrupt has occurred or not.
  850. * @param USARTx: Select the USART or the UART peripheral.
  851. * This parameter can be one of the following values:
  852. * USART1, USART2, USART3, UART4 or UART5.
  853. * @param USART_IT: specifies the USART interrupt source to check.
  854. * This parameter can be one of the following values:
  855. * @arg USART_IT_CTS: CTS change interrupt (not available for UART4 and UART5)
  856. * @arg USART_IT_LBD: LIN Break detection interrupt
  857. * @arg USART_IT_TXE: Tansmit Data Register empty interrupt
  858. * @arg USART_IT_TC: Transmission complete interrupt
  859. * @arg USART_IT_RXNE: Receive Data register not empty interrupt
  860. * @arg USART_IT_IDLE: Idle line detection interrupt
  861. * @arg USART_IT_ORE: OverRun Error interrupt
  862. * @arg USART_IT_NE: Noise Error interrupt
  863. * @arg USART_IT_FE: Framing Error interrupt
  864. * @arg USART_IT_PE: Parity Error interrupt
  865. * @retval The new state of USART_IT (SET or RESET).
  866. */
  867. ITStatus USART_GetITStatus(USART_TypeDef* USARTx, uint16_t USART_IT)
  868. {
  869. uint32_t bitpos = 0x00, itmask = 0x00, usartreg = 0x00;
  870. ITStatus bitstatus = RESET;
  871. /* Check the parameters */
  872. assert_param(IS_USART_ALL_PERIPH(USARTx));
  873. assert_param(IS_USART_GET_IT(USART_IT));
  874. /* The CTS interrupt is not available for UART4 and UART5 */
  875. if (USART_IT == USART_IT_CTS)
  876. {
  877. assert_param(IS_USART_123_PERIPH(USARTx));
  878. }
  879. /* Get the USART register index */
  880. usartreg = (((uint8_t)USART_IT) >> 0x05);
  881. /* Get the interrupt position */
  882. itmask = USART_IT & IT_Mask;
  883. itmask = (uint32_t)0x01 << itmask;
  884. if (usartreg == 0x01) /* The IT is in CR1 register */
  885. {
  886. itmask &= USARTx->CR1;
  887. }
  888. else if (usartreg == 0x02) /* The IT is in CR2 register */
  889. {
  890. itmask &= USARTx->CR2;
  891. }
  892. else /* The IT is in CR3 register */
  893. {
  894. itmask &= USARTx->CR3;
  895. }
  896. bitpos = USART_IT >> 0x08;
  897. bitpos = (uint32_t)0x01 << bitpos;
  898. bitpos &= USARTx->SR;
  899. if ((itmask != (uint16_t)RESET)&&(bitpos != (uint16_t)RESET))
  900. {
  901. bitstatus = SET;
  902. }
  903. else
  904. {
  905. bitstatus = RESET;
  906. }
  907. return bitstatus;
  908. }
  909. /**
  910. * @brief Clears the USARTx's interrupt pending bits.
  911. * @param USARTx: Select the USART or the UART peripheral.
  912. * This parameter can be one of the following values:
  913. * USART1, USART2, USART3, UART4 or UART5.
  914. * @param USART_IT: specifies the interrupt pending bit to clear.
  915. * This parameter can be one of the following values:
  916. * @arg USART_IT_CTS: CTS change interrupt (not available for UART4 and UART5)
  917. * @arg USART_IT_LBD: LIN Break detection interrupt
  918. * @arg USART_IT_TC: Transmission complete interrupt.
  919. * @arg USART_IT_RXNE: Receive Data register not empty interrupt.
  920. *
  921. * @note
  922. * - PE (Parity error), FE (Framing error), NE (Noise error), ORE (OverRun
  923. * error) and IDLE (Idle line detected) pending bits are cleared by
  924. * software sequence: a read operation to USART_SR register
  925. * (USART_GetITStatus()) followed by a read operation to USART_DR register
  926. * (USART_ReceiveData()).
  927. * - RXNE pending bit can be also cleared by a read to the USART_DR register
  928. * (USART_ReceiveData()).
  929. * - TC pending bit can be also cleared by software sequence: a read
  930. * operation to USART_SR register (USART_GetITStatus()) followed by a write
  931. * operation to USART_DR register (USART_SendData()).
  932. * - TXE pending bit is cleared only by a write to the USART_DR register
  933. * (USART_SendData()).
  934. * @retval None
  935. */
  936. void USART_ClearITPendingBit(USART_TypeDef* USARTx, uint16_t USART_IT)
  937. {
  938. uint16_t bitpos = 0x00, itmask = 0x00;
  939. /* Check the parameters */
  940. assert_param(IS_USART_ALL_PERIPH(USARTx));
  941. assert_param(IS_USART_CLEAR_IT(USART_IT));
  942. /* The CTS interrupt is not available for UART4 and UART5 */
  943. if (USART_IT == USART_IT_CTS)
  944. {
  945. assert_param(IS_USART_123_PERIPH(USARTx));
  946. }
  947. bitpos = USART_IT >> 0x08;
  948. itmask = ((uint16_t)0x01 << (uint16_t)bitpos);
  949. USARTx->SR = (uint16_t)~itmask;
  950. }
  951. /**
  952. * @}
  953. */
  954. /**
  955. * @}
  956. */
  957. /**
  958. * @}
  959. */
  960. /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/