stm32f10x_usart.c 37 KB

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