stm32f10x_usart.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058
  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****/