stm8l15x_usart.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208
  1. /**
  2. ******************************************************************************
  3. * @file stm8l15x_usart.c
  4. * @author MCD Application Team
  5. * @version V1.6.1
  6. * @date 30-September-2014
  7. * @brief This file provides firmware functions to manage the following
  8. * functionalities of the Universal synchronous asynchronous receiver
  9. * transmitter (USART):
  10. * - Initialization and Configuration
  11. * - Data transfers
  12. * - Multi-Processor Communication
  13. * - Half-duplex mode
  14. * - Smartcard mode
  15. * - IrDA mode
  16. * - DMA transfers management
  17. * - Interrupts and flags management
  18. *
  19. * @verbatim
  20. *
  21. * ===================================================================
  22. * How to use this driver
  23. * ===================================================================
  24. * 1. Enable peripheral clock using CLK_PeripheralClockConfig(CLK_Peripheral_USARTx,
  25. * ENABLE) function (Refer to the product datasheet for the available USART
  26. * peripherals)
  27. *
  28. * 2. Enable the external Pull-up on the used USART Pins using the
  29. * GPIO_ExternalPullUpConfig() function or an external pull-up equivalent resistor
  30. * (RPU = 45 KOhm typical value).
  31. *
  32. * 3. Program the Baud Rate, Word Length , Stop Bit, Parity and Mode (Receiver/Transmitter)
  33. * using the USART_Init() function.
  34. *
  35. * 4. For synchronous mode, enable the clock and program the polarity,
  36. * phase and last bit using the USART_ClockInit() function.
  37. *
  38. * 5. Enable the corresponding interrupt using the function USART_ITConfig() if you need
  39. * to use interrupt mode.
  40. *
  41. * 6. When using the DMA mode
  42. * - Configure the DMA using DMA_Init() function
  43. * - Activate the needed channel Request using USART_DMACmd() function
  44. *
  45. * 7. Enable the USART using the USART_Cmd() function.
  46. *
  47. * 8. Enable the DMA using the DMA_Cmd() function, when using DMA mode.
  48. *
  49. * Refer to Multi-Processor, half-duplex, Smartcard, IrDA sub-sections for more details.
  50. *
  51. * @endverbatim
  52. *
  53. ******************************************************************************
  54. * @attention
  55. *
  56. * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
  57. *
  58. * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  59. * You may not use this file except in compliance with the License.
  60. * You may obtain a copy of the License at:
  61. *
  62. * http://www.st.com/software_license_agreement_liberty_v2
  63. *
  64. * Unless required by applicable law or agreed to in writing, software
  65. * distributed under the License is distributed on an "AS IS" BASIS,
  66. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  67. * See the License for the specific language governing permissions and
  68. * limitations under the License.
  69. *
  70. ******************************************************************************
  71. */
  72. /* Includes ------------------------------------------------------------------*/
  73. #include "stm8l15x_usart.h"
  74. /** @addtogroup STM8L15x_StdPeriph_Driver
  75. * @{
  76. */
  77. /** @defgroup USART
  78. * @brief USART driver modules
  79. * @{
  80. */
  81. /* Private typedef -----------------------------------------------------------*/
  82. /* Private define ------------------------------------------------------------*/
  83. /* Private macro -------------------------------------------------------------*/
  84. /* Private variables ---------------------------------------------------------*/
  85. /* Private function prototypes -----------------------------------------------*/
  86. /* Private functions ---------------------------------------------------------*/
  87. /** @defgroup USART_Private_Functions
  88. * @{
  89. */
  90. /** @defgroup USART_Group1 Initialization and Configuration functions
  91. * @brief Initialization and Configuration functions
  92. *
  93. @verbatim
  94. ===============================================================================
  95. Initialization and Configuration functions
  96. ===============================================================================
  97. This subsection provides a set of functions allowing to initialize the USART
  98. in asynchronous and in synchronous modes.
  99. - For the asynchronous mode only these parameters can be configured:
  100. - Baud Rate
  101. - Word Length
  102. - Stop Bit
  103. - Parity: If the parity is enabled, then the MSB bit of the data written
  104. in the data register is transmitted but is changed by the parity bit.
  105. Depending on the frame length defined by the M bit (8-bits or 9-bits),
  106. the possible USART frame formats are as listed in the following table:
  107. +-------------------------------------------------------------+
  108. | M bit | PCE bit | USART frame |
  109. |---------------------|---------------------------------------|
  110. | 0 | 0 | | SB | 8 bit data | STB | |
  111. |---------|-----------|---------------------------------------|
  112. | 0 | 1 | | SB | 7 bit data | PB | STB | |
  113. |---------|-----------|---------------------------------------|
  114. | 1 | 0 | | SB | 9 bit data | STB | |
  115. |---------|-----------|---------------------------------------|
  116. | 1 | 1 | | SB | 8 bit data | PB | STB | |
  117. +-------------------------------------------------------------+
  118. - Receiver/transmitter modes
  119. The USART_Init() function follows the USART asynchronous configuration procedure
  120. (details for the procedure are available in reference manual (RM0031)).
  121. - For the synchronous mode in addition to the asynchronous mode parameters these
  122. parameters should be also configured:
  123. - USART Clock Enabled
  124. - USART polarity
  125. - USART phase
  126. - USART LastBit
  127. These parameters can be configured using the USART_ClockInit() function.
  128. @endverbatim
  129. * @{
  130. */
  131. /**
  132. * @brief Deinitializes the USART peripheral.
  133. * @param USARTx: where x can be 1, 2 or 3 to select the specified USART peripheral.
  134. * @retval None
  135. */
  136. void USART_DeInit(USART_TypeDef* USARTx)
  137. {
  138. /* Clear the Idle Line Detected bit in the status register by a read
  139. to the USART_SR register followed by a Read to the USART_DR register */
  140. (void) USARTx->SR;
  141. (void) USARTx->DR;
  142. USARTx->BRR2 = USART_BRR2_RESET_VALUE; /* Set USART_BRR2 to reset value 0x00 */
  143. USARTx->BRR1 = USART_BRR1_RESET_VALUE; /* Set USART_BRR1 to reset value 0x00 */
  144. USARTx->CR1 = USART_CR1_RESET_VALUE; /* Set USART_CR1 to reset value 0x00 */
  145. USARTx->CR2 = USART_CR2_RESET_VALUE; /* Set USART_CR2 to reset value 0x00 */
  146. USARTx->CR3 = USART_CR3_RESET_VALUE; /* Set USART_CR3 to reset value 0x00 */
  147. USARTx->CR4 = USART_CR4_RESET_VALUE; /* Set USART_CR4 to reset value 0x00 */
  148. }
  149. /**
  150. * @brief Initializes the USART according to the specified parameters.
  151. * @note Configure in Push Pull or Open Drain mode the Tx pin by setting the
  152. * correct I/O Port register according the product package and line
  153. * configuration
  154. * @param USARTx: where x can be 1 to select the specified USART peripheral.
  155. * @param BaudRate: The baudrate.
  156. * @param USART_WordLength: the word length
  157. * This parameter can be one of the following values:
  158. * @arg USART_WordLength_8b: 8 bits Data
  159. * @arg USART_WordLength_9b: 9 bits Data
  160. * @param USART_StopBits: Stop Bit
  161. * This parameter can be one of the following values:
  162. * @arg USART_StopBits_1: One stop bit is transmitted at the end of frame
  163. * @arg USART_StopBits_2: Two stop bits are transmitted at the end of frame
  164. * @arg USART_StopBits_1_5: One and half stop bits
  165. * @param USART_Parity: Parity
  166. * This parameter can be one of the following values:
  167. * @arg USART_Parity_No: No Parity
  168. * @arg USART_Parity_Even: Even Parity
  169. * @arg USART_Parity_Odd: Odd Parity
  170. * @param USART_Mode: Mode
  171. * This parameter can be one of the following values:
  172. * @arg USART_Mode_Rx: Receive Enable
  173. * @arg USART_Mode_Tx: Transmit Enable
  174. * @retval None
  175. */
  176. void USART_Init(USART_TypeDef* USARTx, uint32_t BaudRate, USART_WordLength_TypeDef
  177. USART_WordLength, USART_StopBits_TypeDef USART_StopBits,
  178. USART_Parity_TypeDef USART_Parity, USART_Mode_TypeDef USART_Mode)
  179. {
  180. uint32_t BaudRate_Mantissa = 0;
  181. /* Check the parameters */
  182. assert_param(IS_USART_BAUDRATE(BaudRate));
  183. assert_param(IS_USART_WORDLENGTH(USART_WordLength));
  184. assert_param(IS_USART_STOPBITS(USART_StopBits));
  185. assert_param(IS_USART_PARITY(USART_Parity));
  186. assert_param(IS_USART_MODE(USART_Mode));
  187. /* Clear the word length and Parity Control bits */
  188. USARTx->CR1 &= (uint8_t)(~(USART_CR1_PCEN | USART_CR1_PS | USART_CR1_M));
  189. /* Set the word length bit according to USART_WordLength value */
  190. /* Set the Parity Control bit to USART_Parity value */
  191. USARTx->CR1 |= (uint8_t)((uint8_t)USART_WordLength | (uint8_t)USART_Parity);
  192. /* Clear the STOP bits */
  193. USARTx->CR3 &= (uint8_t)(~USART_CR3_STOP);
  194. /* Set the STOP bits number according to USART_StopBits value */
  195. USARTx->CR3 |= (uint8_t)USART_StopBits;
  196. /* Clear the LSB mantissa of USARTDIV */
  197. USARTx->BRR1 &= (uint8_t)(~USART_BRR1_DIVM);
  198. /* Clear the MSB mantissa of USARTDIV */
  199. USARTx->BRR2 &= (uint8_t)(~USART_BRR2_DIVM);
  200. /* Clear the Fraction bits of USARTDIV */
  201. USARTx->BRR2 &= (uint8_t)(~USART_BRR2_DIVF);
  202. BaudRate_Mantissa = (uint32_t)(CLK_GetClockFreq() / BaudRate );
  203. /* Set the fraction of USARTDIV */
  204. USARTx->BRR2 = (uint8_t)((BaudRate_Mantissa >> (uint8_t)8) & (uint8_t)0xF0);
  205. /* Set the MSB mantissa of USARTDIV */
  206. USARTx->BRR2 |= (uint8_t)(BaudRate_Mantissa & (uint8_t)0x0F);
  207. /* Set the LSB mantissa of USARTDIV */
  208. USARTx->BRR1 = (uint8_t)(BaudRate_Mantissa >> (uint8_t)4);
  209. /* Disable the Transmitter and Receiver */
  210. USARTx->CR2 &= (uint8_t)~(USART_CR2_TEN | USART_CR2_REN);
  211. /* Set TEN and REN bits according to USART_Mode value */
  212. USARTx->CR2 |= (uint8_t)USART_Mode;
  213. }
  214. /**
  215. * @brief Initializes the USART Clock according to the specified parameters.
  216. * @param USARTx: where x can be 1 to select the specified USART peripheral.
  217. * @param USART_Clock: Clock
  218. * This parameter can be one of the following values:
  219. * @arg USART_Clock_Disable: CK pin disabled
  220. * @arg USART_Clock_Enable: CK pin enabled
  221. * @param USART_CPOL: Clock Polarity
  222. * This parameter can be one of the following values:
  223. * @arg USART_CPOL_Low: CK to 0 when idle
  224. * @arg USART_CPOL_High: CK to 1 when idle
  225. * @param USART_CPHA: Clock Phase
  226. * This parameter can be one of the following values:
  227. * @arg USART_CPHA_1Edge: The first clock transition is the first data capture edge
  228. * @arg USART_CPHA_2Edge: The second clock transition is the first data capture edge
  229. * @param USART_LastBit: Last Bit
  230. * This parameter can be one of the following values:
  231. * @arg USART_LastBit_Disable: The clock pulse of the last data bit is
  232. * not output to the SCLK pin
  233. * @arg USART_LastBit_Enable: The clock pulse of the last data bit is
  234. * output to the SCLK pin
  235. * @retval None
  236. */
  237. void USART_ClockInit(USART_TypeDef* USARTx, USART_Clock_TypeDef USART_Clock,
  238. USART_CPOL_TypeDef USART_CPOL, USART_CPHA_TypeDef USART_CPHA,
  239. USART_LastBit_TypeDef USART_LastBit)
  240. {
  241. /* Check the parameters */
  242. assert_param(IS_USART_CLOCK(USART_Clock));
  243. assert_param(IS_USART_CPOL(USART_CPOL));
  244. assert_param(IS_USART_CPHA(USART_CPHA));
  245. assert_param(IS_USART_LASTBIT(USART_LastBit));
  246. /* Clear the Clock Polarity, lock Phase, Last Bit Clock pulse */
  247. USARTx->CR3 &= (uint8_t)~(USART_CR3_CPOL | USART_CR3_CPHA | USART_CR3_LBCL);
  248. /* Set the Clock Polarity, lock Phase, Last Bit Clock pulse */
  249. USARTx->CR3 |= (uint8_t)((uint8_t)((uint8_t)(USART_CPOL | (uint8_t)USART_CPHA ) | USART_LastBit));
  250. if (USART_Clock != USART_Clock_Disable)
  251. {
  252. USARTx->CR3 |= (uint8_t)(USART_CR3_CLKEN); /* Set the Clock Enable bit */
  253. }
  254. else
  255. {
  256. USARTx->CR3 &= (uint8_t)(~USART_CR3_CLKEN); /* Clear the Clock Enable bit */
  257. }
  258. }
  259. /**
  260. * @brief Enable the USART peripheral.
  261. * @param USARTx: where x can be 1 to select the specified USART peripheral.
  262. * @param NewState: The new state of the USART Communication.
  263. * This parameter can be: ENABLE or DISABLE.
  264. * @retval None
  265. */
  266. void USART_Cmd(USART_TypeDef* USARTx, FunctionalState NewState)
  267. {
  268. if (NewState != DISABLE)
  269. {
  270. USARTx->CR1 &= (uint8_t)(~USART_CR1_USARTD); /**< USART Enable */
  271. }
  272. else
  273. {
  274. USARTx->CR1 |= USART_CR1_USARTD; /**< USART Disable (for low power consumption) */
  275. }
  276. }
  277. /**
  278. * @brief Sets the system clock prescaler.
  279. * @note IrDA Low Power mode or smartcard mode should be enabled
  280. * @note This function is related to SmartCard and IrDa mode.
  281. * @param USARTx: Select the USARTx peripheral.
  282. * @param USART_Prescaler: specifies the prescaler clock.
  283. * @note In IrDA Low Power Mode the clock source is divided by the value given
  284. * in the register (8 bits)
  285. * - 0000 0000 Reserved
  286. * - 0000 0001 divides the clock source by 1
  287. * - 0000 0010 divides the clock source by 2
  288. * - ...
  289. * @note In Smart Card Mode the clock source is divided by the value given in the register
  290. * (5 significant bits) multiplied by 2
  291. * - 0 0000 Reserved
  292. * - 0 0001 divides the clock source by 2
  293. * - 0 0010 divides the clock source by 4
  294. * - 0 0011 divides the clock source by 6
  295. * - ...
  296. * @retval None
  297. */
  298. void USART_SetPrescaler(USART_TypeDef* USARTx, uint8_t USART_Prescaler)
  299. {
  300. /* Load the USART prescaler value*/
  301. USARTx->PSCR = USART_Prescaler;
  302. }
  303. /**
  304. * @brief Transmits break characters.
  305. * @param USARTx: where x can be 1 to select the specified USART peripheral.
  306. * @retval None
  307. */
  308. void USART_SendBreak(USART_TypeDef* USARTx)
  309. {
  310. USARTx->CR2 |= USART_CR2_SBK;
  311. }
  312. /**
  313. * @}
  314. */
  315. /** @defgroup USART_Group2 Data transfers functions
  316. * @brief Data transfers functions
  317. *
  318. @verbatim
  319. ===============================================================================
  320. Data transfers functions
  321. ===============================================================================
  322. This subsection provides a set of functions allowing to manage the USART data
  323. transfers.
  324. During an USART reception, data shifts in least significant bit first through
  325. the RX pin. In this mode, the USART_DR register is similar to a buffer (RDR)
  326. between the internal bus and the received shift register.
  327. When a transmission is taking place, a write instruction to the USART_DR register
  328. stores the data in the TDR register which is copied in the shift register
  329. at the end of the current transmission.
  330. The read access of the USART_DR register can be done using the USART_ReceiveData8()
  331. or USART_ReceiveData9() functions and returns the RDR buffered value. Whereas a write
  332. access to the USART_DR can be done using USART_SendData8() or USART_SendData9()
  333. functions and stores the written data into TDR buffer.
  334. @endverbatim
  335. * @{
  336. */
  337. /**
  338. * @brief Returns the most recent received data by the USART peripheral.
  339. * @param USARTx: where x can be 1 to select the specified USART peripheral.
  340. * @retval The received data.
  341. */
  342. uint8_t USART_ReceiveData8(USART_TypeDef* USARTx)
  343. {
  344. return USARTx->DR;
  345. }
  346. /**
  347. * @brief Returns the most recent received data by the USART peripheral.
  348. * @param USARTx: where x can be 1 to select the specified USART peripheral.
  349. * @retval The received data.
  350. */
  351. uint16_t USART_ReceiveData9(USART_TypeDef* USARTx)
  352. {
  353. uint16_t temp = 0;
  354. temp = ((uint16_t)(((uint16_t)((uint16_t)USARTx->CR1 & (uint16_t)USART_CR1_R8)) << 1));
  355. return (uint16_t)( ((uint16_t)((uint16_t)USARTx->DR) | temp) & ((uint16_t)0x01FF));
  356. }
  357. /**
  358. * @brief Transmits 8 bit data through the USART peripheral.
  359. * @param Data: The data to transmit.
  360. * @retval None
  361. */
  362. void USART_SendData8(USART_TypeDef* USARTx, uint8_t Data)
  363. {
  364. /* Transmit Data */
  365. USARTx->DR = Data;
  366. }
  367. /**
  368. * @brief Transmits 9 bit data through the USART peripheral.
  369. * @param USARTx: Select the USARTx peripheral.
  370. * @param Data: The data to transmit.
  371. * This parameter should be lower than 0x1FF.
  372. * @retval None
  373. */
  374. void USART_SendData9(USART_TypeDef* USARTx, uint16_t Data)
  375. {
  376. assert_param(IS_USART_DATA_9BITS(Data));
  377. /* Clear the transmit data bit 8 */
  378. USARTx->CR1 &= ((uint8_t)~USART_CR1_T8);
  379. /* Write the transmit data bit [8] */
  380. USARTx->CR1 |= (uint8_t)(((uint8_t)(Data >> 2)) & USART_CR1_T8);
  381. /* Write the transmit data bit [0:7] */
  382. USARTx->DR = (uint8_t)(Data);
  383. }
  384. /**
  385. * @}
  386. */
  387. /** @defgroup USART_Group3 MultiProcessor Communication functions
  388. * @brief Multi-Processor Communication functions
  389. *
  390. @verbatim
  391. ===============================================================================
  392. Multi-Processor Communication functions
  393. ===============================================================================
  394. This subsection provides a set of functions allowing to manage the USART
  395. multiprocessor communication.
  396. For instance one of the USARTs can be the master, its TX output is connected to
  397. the RX input of the other USART. The others are slaves, their respective TX outputs
  398. are logically ANDed together and connected to the RX input of the master.
  399. USART multiprocessor communication is possible through the following procedure:
  400. 1. Program the Baud rate, Word length = 9 bits, Stop bits, Parity, Mode transmitter
  401. or Mode receiver and hardware flow control values using the USART_Init()
  402. function.
  403. 2. Configures the USART address using the USART_SetAddress() function.
  404. 3. Configures the wake up method (USART_WakeUp_IdleLine or USART_WakeUp_AddressMark)
  405. using USART_WakeUpConfig() function only for the slaves.
  406. 4. Enable the USART using the USART_Cmd() function.
  407. 5. Enter the USART slaves in mute mode using USART_ReceiverWakeUpCmd() function.
  408. The USART Slave exit from mute mode when receiving the wake up condition.
  409. @endverbatim
  410. * @{
  411. */
  412. /**
  413. * @brief Determines if the USART is in mute mode or not.
  414. * @param USARTx: where x can be 1 to select the specified USART peripheral.
  415. * @param NewState: The new state of the USART mode.
  416. * This parameter can be: ENABLE or DISABLE.
  417. * @retval None
  418. */
  419. void USART_ReceiverWakeUpCmd(USART_TypeDef* USARTx, FunctionalState NewState)
  420. {
  421. assert_param(IS_FUNCTIONAL_STATE(NewState));
  422. if (NewState != DISABLE)
  423. {
  424. /* Enable the mute mode USART by setting the RWU bit in the CR2 register */
  425. USARTx->CR2 |= USART_CR2_RWU;
  426. }
  427. else
  428. {
  429. /* Disable the mute mode USART by clearing the RWU bit in the CR1 register */
  430. USARTx->CR2 &= ((uint8_t)~USART_CR2_RWU);
  431. }
  432. }
  433. /**
  434. * @brief Sets the address of the USART node.
  435. * @param USARTx: Select the USARTx peripheral.
  436. * @param Address: Indicates the address of the USART node.
  437. * This parameter should be lower than 16
  438. * @retval None
  439. */
  440. void USART_SetAddress(USART_TypeDef* USARTx, uint8_t USART_Address)
  441. {
  442. /* assert_param for USART_Address */
  443. assert_param(IS_USART_ADDRESS(USART_Address));
  444. /* Clear the USART address */
  445. USARTx->CR4 &= ((uint8_t)~USART_CR4_ADD);
  446. /* Set the USART address node */
  447. USARTx->CR4 |= USART_Address;
  448. }
  449. /**
  450. * @brief Selects the USART WakeUp method.
  451. * @param USART_WakeUp: Specifies the USART wakeup method.
  452. * This parameter can be one of the following values:
  453. * @arg USART_WakeUp_IdleLine: 0x01 Idle Line wake up
  454. * @arg USART_WakeUp_AddressMark: 0x02 Address Mark wake up
  455. * @retval None
  456. */
  457. void USART_WakeUpConfig(USART_TypeDef* USARTx, USART_WakeUp_TypeDef USART_WakeUp)
  458. {
  459. assert_param(IS_USART_WAKEUP(USART_WakeUp));
  460. USARTx->CR1 &= ((uint8_t)~USART_CR1_WAKE);
  461. USARTx->CR1 |= (uint8_t)USART_WakeUp;
  462. }
  463. /**
  464. * @}
  465. */
  466. /** @defgroup USART_Group4 Halfduplex mode function
  467. * @brief Half-duplex mode function
  468. *
  469. @verbatim
  470. ===============================================================================
  471. Half-duplex mode function
  472. ===============================================================================
  473. This subsection provides a function allowing to manage the USART
  474. Half-duplex communication.
  475. The USART can be configured to follow a single-wire half-duplex protocol where
  476. the TX and RX lines are internally connected.
  477. USART Half duplex communication is possible through the following procedure:
  478. 1. Program the Baud rate, Word length, Stop bits, Parity, Mode transmitter
  479. or Mode receiver and hardware flow control values using the USART_Init()
  480. function.
  481. 2. Configures the USART address using the USART_SetAddress() function.
  482. 3. Enable the USART using the USART_Cmd() function.
  483. 4. Enable the half duplex mode using USART_HalfDuplexCmd() function.
  484. Note:
  485. ----
  486. 1. The RX pin is no longer used
  487. 2. In Half-duplex mode the following bits must be kept cleared:
  488. - CLKEN bits in the USART_CR3 register.
  489. - SCEN and IREN bits in the USART_CR5 register.
  490. @endverbatim
  491. * @{
  492. */
  493. /**
  494. * @brief Enables or disables the USART’s Half Duplex communication.
  495. * @param USARTx: where x can be 1 to select the specified USART peripheral.
  496. * @param NewState new state of the USART Communication.
  497. * This parameter can be: ENABLE or DISABLE.
  498. * @retval None
  499. */
  500. void USART_HalfDuplexCmd(USART_TypeDef* USARTx, FunctionalState NewState)
  501. {
  502. assert_param(IS_FUNCTIONAL_STATE(NewState));
  503. if (NewState != DISABLE)
  504. {
  505. USARTx->CR5 |= USART_CR5_HDSEL; /**< USART Half Duplex Enable */
  506. }
  507. else
  508. {
  509. USARTx->CR5 &= (uint8_t)~USART_CR5_HDSEL; /**< USART Half Duplex Disable */
  510. }
  511. }
  512. /**
  513. * @}
  514. */
  515. /** @defgroup USART_Group5 Smartcard mode functions
  516. * @brief Smartcard mode functions
  517. *
  518. @verbatim
  519. ===============================================================================
  520. Smartcard mode functions
  521. ===============================================================================
  522. This subsection provides a set of functions allowing to manage the USART
  523. Smartcard communication.
  524. The Smartcard interface is designed to support asynchronous protocol Smartcards as
  525. defined in the ISO 7816-3 standard.
  526. The USART can provide a clock to the smartcard through the SCLK output.
  527. In smartcard mode, SCLK is not associated to the communication but is simply derived
  528. from the internal peripheral input clock through a 5-bit prescaler.
  529. Smartcard communication is possible through the following procedure:
  530. 1. Configures the Smartcard Prsecaler using the USART_SetPrescaler() function.
  531. 2. Configures the Smartcard Guard Time using the USART_SetGuardTime() function.
  532. 3. Program the USART clock using the USART_ClockInit() function as following:
  533. - USART Clock enabled
  534. - USART CPOL Low
  535. - USART CPHA on first edge
  536. - USART Last Bit Clock Enabled
  537. 4. Program the Smartcard interface using the USART_Init() function as following:
  538. - Word Length = 9 Bits
  539. - 1.5 Stop Bit
  540. - Even parity
  541. - BaudRate = 12096 baud
  542. - Hardware flow control disabled (RTS and CTS signals)
  543. - Tx and Rx enabled
  544. 5. Optionally you can enable the parity error interrupt using the USART_ITConfig()
  545. function
  546. 6. Enable the USART using the USART_Cmd() function.
  547. 7. Enable the Smartcard NACK using the USART_SmartCardNACKCmd() function.
  548. 8. Enable the Smartcard interface using the USART_SmartCardCmd() function.
  549. Please refer to the ISO 7816-3 specification for more details.
  550. Note:
  551. -----
  552. 1. It is also possible to choose 0.5 stop bit for receiving but it is recommended
  553. to use 1.5 stop bits for both transmitting and receiving to avoid switching
  554. between the two configurations.
  555. 2. In smartcard mode, the following bits must be kept cleared:
  556. - HDSEL and IREN bits in the USART_CR5 register.
  557. @endverbatim
  558. * @{
  559. */
  560. /**
  561. * @brief Enables or disables the USART Smart Card mode.
  562. * @param USARTx: Select the USARTx peripheral.
  563. * @param NewState: new state of the Smart Card mode.
  564. * This parameter can be: ENABLE or DISABLE.
  565. * @retval None
  566. */
  567. void USART_SmartCardCmd(USART_TypeDef* USARTx, FunctionalState NewState)
  568. {
  569. assert_param(IS_FUNCTIONAL_STATE(NewState));
  570. if (NewState != DISABLE)
  571. {
  572. /* Enable the SC mode by setting the SCEN bit in the CR5 register */
  573. USARTx->CR5 |= USART_CR5_SCEN;
  574. }
  575. else
  576. {
  577. /* Disable the SC mode by clearing the SCEN bit in the CR5 register */
  578. USARTx->CR5 &= ((uint8_t)(~USART_CR5_SCEN));
  579. }
  580. }
  581. /**
  582. * @brief Enables or disables NACK transmission.
  583. * @param USARTx: where x can be 1 to select the specified USART peripheral.
  584. * @param NewState: new state of the Smart Card mode.
  585. * This parameter can be: ENABLE or DISABLE.
  586. * @retval None
  587. */
  588. void USART_SmartCardNACKCmd(USART_TypeDef* USARTx, FunctionalState NewState)
  589. {
  590. assert_param(IS_FUNCTIONAL_STATE(NewState));
  591. if (NewState != DISABLE)
  592. {
  593. /* Enable the NACK transmission by setting the NACK bit in the CR5 register */
  594. USARTx->CR5 |= USART_CR5_NACK;
  595. }
  596. else
  597. {
  598. /* Disable the NACK transmission by clearing the NACK bit in the CR5 register */
  599. USARTx->CR5 &= ((uint8_t)~(USART_CR5_NACK));
  600. }
  601. }
  602. /**
  603. * @brief Sets the specified USART guard time.
  604. * @note SmartCard Mode should be Enabled
  605. * @param USARTx: where x can be 1 to select the specified USART peripheral.
  606. * @param USART_GuardTime: specifies the guard time.
  607. * @retval None
  608. */
  609. void USART_SetGuardTime(USART_TypeDef* USARTx, uint8_t USART_GuardTime)
  610. {
  611. /* Set the USART guard time */
  612. USARTx->GTR = USART_GuardTime;
  613. }
  614. /**
  615. * @}
  616. */
  617. /** @defgroup USART_Group6 IrDA mode functions
  618. * @brief IrDA mode functions
  619. *
  620. @verbatim
  621. ===============================================================================
  622. IrDA mode functions
  623. ===============================================================================
  624. This subsection provides a set of functions allowing to manage the USART
  625. IrDA communication.
  626. IrDA is a half duplex communication protocol. If the Transmitter is busy, any data
  627. on the IrDA receive line will be ignored by the IrDA decoder and if the Receiver
  628. is busy, data on the TX from the USART to IrDA will not be encoded by IrDA.
  629. While receiving data, transmission should be avoided as the data to be transmitted
  630. could be corrupted.
  631. IrDA communication is possible through the following procedure:
  632. 1. Program the Baud rate, Word length = 8 bits, Stop bits, Parity, Transmitter/Receiver
  633. modes and hardware flow control values using the USART_Init() function.
  634. 2. Enable the USART using the USART_Cmd() function.
  635. 3. Configures the IrDA pulse width by configuring the prescaler using
  636. the USART_SetPrescaler() function.
  637. 4. Configures the IrDA USART_IrDAMode_LowPower or USART_IrDAMode_Normal mode
  638. using the USART_IrDAConfig() function.
  639. 5. Enable the IrDA using the USART_IrDACmd() function.
  640. Note:
  641. -----
  642. 1. A pulse of width less than two and greater than one PSC period(s) may or may
  643. not be rejected.
  644. 2. The receiver set up time should be managed by software. The IrDA physical layer
  645. specification specifies a minimum of 10 ms delay between transmission and
  646. reception (IrDA is a half duplex protocol).
  647. 3. In IrDA mode, the following bits must be kept cleared:
  648. - STOP and CLKEN bits in the USART_CR3 register.
  649. - SCEN and HDSEL bits in the USART_CR5 register.
  650. @endverbatim
  651. * @{
  652. */
  653. /**
  654. * @brief Configures the USART’s IrDA interface.
  655. * @param USARTx: where x can be 1 to select the specified USART peripheral.
  656. * @param USART_IrDAMode specifies the IrDA mode.
  657. * This parameter can be one of the following values:
  658. * @arg USART_IrDAMode_Normal: IrDA Normal Mode
  659. * @arg USART_IrDAMode_LowPower: IrDA Low Power Mode
  660. * @retval None
  661. */
  662. void USART_IrDAConfig(USART_TypeDef* USARTx, USART_IrDAMode_TypeDef USART_IrDAMode)
  663. {
  664. assert_param(IS_USART_IRDAMODE(USART_IrDAMode));
  665. if (USART_IrDAMode != USART_IrDAMode_Normal)
  666. {
  667. USARTx->CR5 |= USART_CR5_IRLP;
  668. }
  669. else
  670. {
  671. USARTx->CR5 &= ((uint8_t)~USART_CR5_IRLP);
  672. }
  673. }
  674. /**
  675. * @brief Enables or disables the USART’s IrDA interface.
  676. * @param USARTx: where x can be 1 to select the specified USART peripheral.
  677. * @param NewState new state of the IrDA mode.
  678. * This parameter can be: ENABLE or DISABLE.
  679. * @retval None
  680. */
  681. void USART_IrDACmd(USART_TypeDef* USARTx, FunctionalState NewState)
  682. {
  683. /* Check parameters */
  684. assert_param(IS_FUNCTIONAL_STATE(NewState));
  685. if (NewState != DISABLE)
  686. {
  687. /* Enable the IrDA mode by setting the IREN bit in the CR3 register */
  688. USARTx->CR5 |= USART_CR5_IREN;
  689. }
  690. else
  691. {
  692. /* Disable the IrDA mode by clearing the IREN bit in the CR3 register */
  693. USARTx->CR5 &= ((uint8_t)~USART_CR5_IREN);
  694. }
  695. }
  696. /**
  697. * @}
  698. */
  699. /** @defgroup USART_Group7 DMA transfers management functions
  700. * @brief DMA transfers management functions
  701. *
  702. @verbatim
  703. ===============================================================================
  704. DMA transfers management functions
  705. ===============================================================================
  706. @endverbatim
  707. * @{
  708. */
  709. /**
  710. * @brief Enables or disables the USART DMA interface.
  711. * @param USARTx: where x can be 1 to select the specified USART peripheral.
  712. * @param USART_DMAReq Specifies the USART DMA transfer request to be enabled or disabled.
  713. * This parameter can be one of the following values:
  714. * @arg USART_DMAReq_TX: Receive DMA request Enable
  715. * @arg USART_DMAReq_RX: Transmit DMA request Enable
  716. * @param NewState Indicates the new state of the USART DMA request.
  717. * This parameter can be: ENABLE or DISABLE.
  718. * @retval None
  719. */
  720. void USART_DMACmd(USART_TypeDef* USARTx, USART_DMAReq_TypeDef USART_DMAReq,
  721. FunctionalState NewState)
  722. {
  723. /* Check the parameters */
  724. assert_param(IS_USART_DMAREQ(USART_DMAReq));
  725. assert_param(IS_FUNCTIONAL_STATE(NewState));
  726. if (NewState != DISABLE)
  727. {
  728. /* Enable the DMA transfer for selected requests by setting the DMAT and/or
  729. DMAR bits in the USART CR5 register */
  730. USARTx->CR5 |= (uint8_t) USART_DMAReq;
  731. }
  732. else
  733. {
  734. /* Disable the DMA transfer for selected requests by clearing the DMAT and/or
  735. DMAR bits in the USART CR5 register */
  736. USARTx->CR5 &= (uint8_t)~USART_DMAReq;
  737. }
  738. }
  739. /**
  740. * @}
  741. */
  742. /** @defgroup USART_Group8 Interrupts and flags management functions
  743. * @brief Interrupts and flags management functions
  744. *
  745. @verbatim
  746. ===============================================================================
  747. Interrupts and flags management functions
  748. ===============================================================================
  749. This subsection provides a set of functions allowing to configure the USART
  750. Interrupts sources, DMA channels requests and check or clear the flags or
  751. pending bits status.
  752. The user should identify which mode will be used in his application to manage
  753. the communication: Polling mode, Interrupt mode or DMA mode.
  754. Polling Mode
  755. =============
  756. In Polling Mode, the USART communication can be managed by 9 flags:
  757. 1. USART_FLAG_TXE: to indicate the status of the transmit buffer register
  758. 2. USART_FLAG_RXNE: to indicate the status of the receive buffer register
  759. 3. USART_FLAG_TC: to indicate the status of the transmit operation
  760. 4. USART_FLAG_IDLE: to indicate the status of the Idle Line
  761. 5. USART_FLAG_SBK: to indicate the status of the Send Break characters
  762. 6. USART_FLAG_NE: to indicate if a noise error occur
  763. 7. USART_FLAG_FE: to indicate if a frame error occur
  764. 8. USART_FLAG_PE: to indicate if a parity error occur
  765. 9. USART_FLAG_ORE: to indicate if an Overrun error occur
  766. In this Mode it is advised to use the following functions:
  767. - FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, USART_FLAG_TypeDef USART_FLAG);
  768. - void USART_ClearFlag(USART_TypeDef* USARTx, USART_FLAG_TypeDef USART_FLAG);
  769. Interrupt Mode
  770. ===============
  771. In Interrupt Mode, the USART communication can be managed by 7 interrupt sources
  772. and 6 pending bits:
  773. Pending Bits:
  774. -------------
  775. 1. USART_IT_TXE: to indicate the status of the transmit buffer register
  776. 2. USART_IT_RXNE: to indicate the status of the receive buffer register
  777. 3. USART_IT_TC: to indicate the status of the transmit operation
  778. 4. USART_IT_IDLE: to indicate the status of the Idle Line
  779. 5. USART_IT_PE: to indicate if a parity error occur
  780. 6. USART_IT_OR: to indicate if an Noise flag, overrun error and framing error in
  781. multibuffer communication error occur
  782. Interrupt Source:
  783. -----------------
  784. 1. USART_IT_TXE: specifies the interrupt source for the Tx buffer empty
  785. interrupt.
  786. 2. USART_IT_RXNE: specifies the interrupt source for the Rx buffer not
  787. empty interrupt.
  788. 3. USART_IT_TC: specifies the interrupt source for the Transmit complete
  789. interrupt.
  790. 4. USART_IT_IDLE: specifies the interrupt source for the Idle Line interrupt.
  791. 5. USART_IT_PE: specifies the interrupt source for the parity error interrupt.
  792. 6. USART_IT_ERR: specifies the interrupt source for the errors interrupt.
  793. 7. USART_IT_OR: specifies the interrupt source for the overrun error interrupt.
  794. Note: Some parameters are coded in order to use them as interrupt source or
  795. ---- as pending bits.
  796. In this Mode it is advised to use the following functions:
  797. - void USART_ITConfig(USART_TypeDef* USARTx, USART_IT_TypeDef USART_IT, FunctionalState
  798. NewState);
  799. - ITStatus USART_GetITStatus(USART_TypeDef* USARTx, USART_IT_TypeDef USART_IT);
  800. - void USART_ClearITPendingBit(USART_TypeDef* USARTx, USART_IT_TypeDef USART_IT);
  801. DMA Mode
  802. ========
  803. In DMA Mode, the USART communication can be managed by 2 DMA Channel requests:
  804. 1. USART_DMAReq_Tx: specifies the Tx buffer DMA transfer request
  805. 2. USART_DMAReq_Rx: specifies the Rx buffer DMA transfer request
  806. In this Mode it is advised to use the following function:
  807. - void USART_DMACmd(USART_TypeDef* USARTx, uint16_t USART_DMAReq, FunctionalState NewState);
  808. @endverbatim
  809. * @{
  810. */
  811. /**
  812. * @brief Enables or disables the specified USART interrupts.
  813. * @param USARTx: where x can be 1 to select the specified USART peripheral.
  814. * @param USART_IT specifies the USART interrupt sources to be enabled or disabled.
  815. * This parameter can be one of the following values:
  816. * @arg USART_IT_TXE: Transmit Data Register empty interrupt
  817. * @arg USART_IT_TC: Transmission complete interrupt
  818. * @arg USART_IT_RXNE: Receive Data register not empty interrupt
  819. * @arg USART_IT_OR: Overrun error interrupt
  820. * @arg USART_IT_IDLE: Idle line detection interrupt
  821. * @arg USART_IT_ERR: Error interrupt
  822. * @param NewState new state of the specified USART interrupts.
  823. * This parameter can be: ENABLE or DISABLE.
  824. * @retval None
  825. */
  826. void USART_ITConfig(USART_TypeDef* USARTx, USART_IT_TypeDef USART_IT, FunctionalState NewState)
  827. {
  828. uint8_t usartreg, itpos = 0x00;
  829. assert_param(IS_USART_CONFIG_IT(USART_IT));
  830. assert_param(IS_FUNCTIONAL_STATE(NewState));
  831. /* Get the USART register index */
  832. usartreg = (uint8_t)((uint16_t)USART_IT >> 0x08);
  833. /* Get the USART IT index */
  834. itpos = (uint8_t)((uint8_t)1 << (uint8_t)((uint8_t)USART_IT & (uint8_t)0x0F));
  835. if (NewState != DISABLE)
  836. {
  837. /**< Enable the Interrupt bits according to USART_IT mask */
  838. if (usartreg == 0x01)
  839. {
  840. USARTx->CR1 |= itpos;
  841. }
  842. else if (usartreg == 0x05)
  843. {
  844. USARTx->CR5 |= itpos;
  845. }
  846. /*uartreg =0x02*/
  847. else
  848. {
  849. USARTx->CR2 |= itpos;
  850. }
  851. }
  852. else
  853. {
  854. /**< Disable the interrupt bits according to USART_IT mask */
  855. if (usartreg == 0x01)
  856. {
  857. USARTx->CR1 &= (uint8_t)(~itpos);
  858. }
  859. else if (usartreg == 0x05)
  860. {
  861. USARTx->CR5 &= (uint8_t)(~itpos);
  862. }
  863. /*uartreg =0x02*/
  864. else
  865. {
  866. USARTx->CR2 &= (uint8_t)(~itpos);
  867. }
  868. }
  869. }
  870. /**
  871. * @brief Checks whether the specified USART flag is set or not.
  872. * @param USARTx: Select the USARTx peripheral.
  873. * @param USART_FLAG specifies the flag to check.
  874. * This parameter can be one of the following values:
  875. * @arg USART_FLAG_TXE: Transmit Data Register empty
  876. * @arg USART_FLAG_TC: Transmission Complete
  877. * @arg USART_FLAG_RXNE: Read Data Register Not Empty
  878. * @arg USART_FLAG_IDLE: Idle line detected
  879. * @arg USART_FLAG_OR: OverRun error
  880. * @arg USART_FLAG_NF: Noise error
  881. * @arg USART_FLAG_FE: Framing Error
  882. * @arg USART_FLAG_PE: Parity Error
  883. * @arg USART_FLAG_SBK: Send Break characters
  884. * @retval FlagStatus (SET or RESET)
  885. */
  886. FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, USART_FLAG_TypeDef USART_FLAG)
  887. {
  888. FlagStatus status = RESET;
  889. /* Check parameters */
  890. assert_param(IS_USART_FLAG(USART_FLAG));
  891. if (USART_FLAG == USART_FLAG_SBK)
  892. {
  893. if ((USARTx->CR2 & (uint8_t)USART_FLAG) != (uint8_t)0x00)
  894. {
  895. /* USART_FLAG is set*/
  896. status = SET;
  897. }
  898. else
  899. {
  900. /* USART_FLAG is reset*/
  901. status = RESET;
  902. }
  903. }
  904. else
  905. {
  906. if ((USARTx->SR & (uint8_t)USART_FLAG) != (uint8_t)0x00)
  907. {
  908. /* USART_FLAG is set*/
  909. status = SET;
  910. }
  911. else
  912. {
  913. /* USART_FLAG is reset*/
  914. status = RESET;
  915. }
  916. }
  917. /* Return the USART_FLAG status*/
  918. return status;
  919. }
  920. /**
  921. * @brief Clears the USARTx's pending flags.
  922. * @param USARTx: where x can be 1 to select the specified USART peripheral.
  923. * @param USART_FLAG: specifies the flag to clear.
  924. * This parameter can be any combination of the following values:
  925. * @arg USART_FLAG_TC: Transmission Complete flag.
  926. * @arg USART_FLAG_RXNE: Receive data register not empty flag.
  927. * @note PE (Parity error), FE (Framing error), NE (Noise error), OR (OverRun error)
  928. * and IDLE (Idle line detected) flags are cleared by software sequence: a read
  929. * operation to USART_SR register (USART_GetFlagStatus())followed by a read
  930. * operation to USART_DR register(USART_ReceiveData8() or USART_ReceiveData9()).
  931. * @note RXNE flag can be also cleared by a read to the USART_DR register
  932. * (USART_ReceiveData8()or USART_ReceiveData9()).
  933. * @note TC flag can be also cleared by software sequence: a read operation to USART_SR
  934. * register (USART_GetFlagStatus()) followed by a write operation to USART_DR
  935. * register (USART_SendData8() or USART_SendData9()).
  936. * @note TXE flag is cleared only by a write to the USART_DR register
  937. * (USART_SendData8() or USART_SendData9()).
  938. * @note SBK flag is cleared during the stop bit of break.
  939. * @retval None
  940. */
  941. void USART_ClearFlag(USART_TypeDef* USARTx, USART_FLAG_TypeDef USART_FLAG)
  942. {
  943. /* Check the parameters */
  944. assert_param(IS_USART_CLEAR_FLAG(USART_FLAG));
  945. /*< Clear RXNE or TC flags */
  946. USARTx->SR = (uint8_t)((uint16_t)~((uint16_t)USART_FLAG));
  947. }
  948. /**
  949. * @brief Checks whether the specified USART interrupt has occurred or not.
  950. * @param USARTx: where x can be 1 to select the specified USART peripheral.
  951. * @param USART_IT: Specifies the USART interrupt pending bit to check.
  952. * This parameter can be one of the following values:
  953. * @arg USART_IT_TXE: Transmit Data Register empty interrupt
  954. * @arg USART_IT_TC: Transmission complete interrupt
  955. * @arg USART_IT_RXNE: Receive Data register not empty interrupt
  956. * @arg USART_IT_IDLE: Idle line detection interrupt
  957. * @arg USART_IT_OR: OverRun Error interrupt
  958. * @arg USART_IT_PE: Parity Error interrupt
  959. * @arg USART_IT_FE: Frame Error interrupt
  960. * @arg USART_IT_NF: Noise Flag Error interrupt
  961. * @retval ITStatus The new state of USART_IT (SET or RESET).
  962. */
  963. ITStatus USART_GetITStatus(USART_TypeDef* USARTx, USART_IT_TypeDef USART_IT)
  964. {
  965. ITStatus pendingbitstatus = RESET;
  966. uint8_t temp = 0;
  967. uint8_t itpos = 0;
  968. uint8_t itmask1 = 0;
  969. uint8_t itmask2 = 0;
  970. uint8_t enablestatus = 0;
  971. /* Check parameters */
  972. assert_param(IS_USART_GET_IT(USART_IT));
  973. /* Get the USART IT index */
  974. itpos = (uint8_t)((uint8_t)1 << (uint8_t)((uint8_t)USART_IT & (uint8_t)0x0F));
  975. /* Get the USART IT index */
  976. itmask1 = (uint8_t)((uint8_t)USART_IT >> (uint8_t)4);
  977. /* Set the IT mask*/
  978. itmask2 = (uint8_t)((uint8_t)1 << itmask1);
  979. /* Check the status of the specified USART pending bit*/
  980. if (USART_IT == USART_IT_PE)
  981. {
  982. /* Get the USART_IT enable bit status*/
  983. enablestatus = (uint8_t)((uint8_t)USARTx->CR1 & itmask2);
  984. /* Check the status of the specified USART interrupt*/
  985. if (((USARTx->SR & itpos) != (uint8_t)0x00) && enablestatus)
  986. {
  987. /* Interrupt occurred*/
  988. pendingbitstatus = SET;
  989. }
  990. else
  991. {
  992. /* Interrupt not occurred*/
  993. pendingbitstatus = RESET;
  994. }
  995. }
  996. else if (USART_IT == USART_IT_OR)
  997. {
  998. /* Get the USART_IT enable bit status*/
  999. enablestatus = (uint8_t)((uint8_t)USARTx->CR2 & itmask2);
  1000. /* Check the status of the specified USART interrupt*/
  1001. temp = (uint8_t)(USARTx->CR5 & USART_CR5_EIE);
  1002. if (( (USARTx->SR & itpos) != 0x00) && ((enablestatus || temp)))
  1003. {
  1004. /* Interrupt occurred*/
  1005. pendingbitstatus = SET;
  1006. }
  1007. else
  1008. {
  1009. /* Interrupt not occurred*/
  1010. pendingbitstatus = RESET;
  1011. }
  1012. }
  1013. else
  1014. {
  1015. /* Get the USART_IT enable bit status*/
  1016. enablestatus = (uint8_t)((uint8_t)USARTx->CR2 & itmask2);
  1017. /* Check the status of the specified USART interrupt*/
  1018. if (((USARTx->SR & itpos) != (uint8_t)0x00) && enablestatus)
  1019. {
  1020. /* Interrupt occurred*/
  1021. pendingbitstatus = SET;
  1022. }
  1023. else
  1024. {
  1025. /* Interrupt not occurred*/
  1026. pendingbitstatus = RESET;
  1027. }
  1028. }
  1029. /* Return the USART_IT status*/
  1030. return pendingbitstatus;
  1031. }
  1032. /**
  1033. * @brief Clears the USARTx’s interrupt pending bits.
  1034. * @param USARTx: where x can be 1 to select the specified USART peripheral.
  1035. * @param USART_IT: specifies the interrupt pending bit to clear.
  1036. * This parameter can be one of the following values:
  1037. * @arg USART_IT_TC: Transmission complete interrupt.
  1038. * @note PE (Parity error), FE (Framing error), NE (Noise error),
  1039. * OR (OverRun error) and IDLE (Idle line detected) pending bits are
  1040. * cleared by software sequence: a read operation to USART_SR register
  1041. * (USART_GetITStatus()) followed by a read operation to USART_DR
  1042. * register (USART_ReceiveData8() or USART_ReceiveData9()).
  1043. * @note RXNE pending bit can be also cleared by a read to the USART_DR register
  1044. * (USART_ReceiveData8() or USART_ReceiveData9()).
  1045. * @note TC (Transmit complete) pending bit can be also cleared by software
  1046. * sequence: a read operation to USART_SR register (USART_GetITStatus())
  1047. * followed by a write operation to USART_DR register (USART_SendData8()
  1048. * or USART_SendData9()).
  1049. * @note TXE pending bit is cleared only by a write to the USART_DR register
  1050. * (USART_SendData8() or USART_SendData9()).
  1051. * @retval None
  1052. */
  1053. void USART_ClearITPendingBit(USART_TypeDef* USARTx, USART_IT_TypeDef USART_IT)
  1054. {
  1055. assert_param(IS_USART_CLEAR_IT(USART_IT));
  1056. /*< Clear RXNE or TC pending bit */
  1057. USARTx->SR &= (uint8_t)(~USART_SR_TC);
  1058. }
  1059. /**
  1060. * @}
  1061. */
  1062. /**
  1063. * @}
  1064. */
  1065. /**
  1066. * @}
  1067. */
  1068. /**
  1069. * @}
  1070. */
  1071. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/