ft32f0xx_usart.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188
  1. /**
  2. ******************************************************************************
  3. * @file ft32f0xx_usart.c
  4. * @author FMD AE
  5. * @brief This file provides firmware functions to manage the following
  6. * functionalities of the Universal synchronous asynchronous receiver
  7. * transmitter (USART):
  8. * + Initialization and Configuration
  9. * + STOP Mode
  10. * + AutoBaudRate
  11. * + Data transfers
  12. * + Multi-Processor Communication
  13. * + LIN mode
  14. * + Half-duplex mode
  15. * + Smartcard mode
  16. * + IrDA mode
  17. * + RS485 mode
  18. * + DMA transfers management
  19. * + Interrupts and flags management
  20. * @version V1.0.0
  21. * @data 2021-07-01
  22. ******************************************************************************
  23. */
  24. /* Includes ------------------------------------------------------------------*/
  25. #include "ft32f0xx_usart.h"
  26. #include "ft32f0xx_rcc.h"
  27. /*!< USART CR1 register clear Mask ((~(uint32_t)0xFFFFE6F3)) */
  28. #define CR1_CLEAR_MASK ((uint32_t)(USART_CR1_M | USART_CR1_PCE | \
  29. USART_CR1_PS | USART_CR1_TE | \
  30. USART_CR1_RE))
  31. /*!< USART CR2 register clock bits clear Mask ((~(uint32_t)0xFFFFF0FF)) */
  32. #define CR2_CLOCK_CLEAR_MASK ((uint32_t)(USART_CR2_CLKEN | USART_CR2_CPOL | \
  33. USART_CR2_CPHA | USART_CR2_LBCL))
  34. /*!< USART CR3 register clear Mask ((~(uint32_t)0xFFFFFCFF)) */
  35. #define CR3_CLEAR_MASK ((uint32_t)(USART_CR3_RTSE | USART_CR3_CTSE))
  36. /*!< USART Interrupts mask */
  37. #define IT_MASK ((uint32_t)0x000000FF)
  38. /**
  39. * @brief Deinitializes the USARTx peripheral registers to their default reset values.
  40. * @param USARTx: where x can be from 1 to 2 to select the USART peripheral.
  41. * @retval None
  42. */
  43. void USART_DeInit(USART_TypeDef* USARTx)
  44. {
  45. /* Check the parameters */
  46. assert_param(IS_USART_ALL_PERIPH(USARTx));
  47. if (USARTx == USART1)
  48. {
  49. RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, ENABLE);
  50. RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, DISABLE);
  51. }
  52. else if (USARTx == USART2)
  53. {
  54. RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, ENABLE);
  55. RCC_APB1PeriphResetCmd(RCC_APB1Periph_USART2, DISABLE);
  56. }
  57. }
  58. /**
  59. * @brief Initializes the USARTx peripheral according to the specified
  60. * parameters in the USART_InitStruct .
  61. * @param USARTx: where x can be from 1 to 2 to select the USART peripheral.
  62. * @param USART_InitStruct: pointer to a USART_InitTypeDef structure that contains
  63. * the configuration information for the specified USART peripheral.
  64. * @retval None
  65. */
  66. void USART_Init(USART_TypeDef* USARTx, USART_InitTypeDef* USART_InitStruct)
  67. {
  68. uint32_t divider = 0, apbclock = 0, tmpreg = 0;
  69. RCC_ClocksTypeDef RCC_ClocksStatus;
  70. /* Check the parameters */
  71. assert_param(IS_USART_ALL_PERIPH(USARTx));
  72. assert_param(IS_USART_BAUDRATE(USART_InitStruct->USART_BaudRate));
  73. assert_param(IS_USART_WORD_LENGTH(USART_InitStruct->USART_WordLength));
  74. assert_param(IS_USART_STOPBITS(USART_InitStruct->USART_StopBits));
  75. assert_param(IS_USART_PARITY(USART_InitStruct->USART_Parity));
  76. assert_param(IS_USART_MODE(USART_InitStruct->USART_Mode));
  77. assert_param(IS_USART_HARDWARE_FLOW_CONTROL(USART_InitStruct->USART_HardwareFlowControl));
  78. /* Disable USART */
  79. USARTx->CR1 &= (uint32_t)~((uint32_t)USART_CR1_UE);
  80. /*---------------------------- USART CR2 Configuration -----------------------*/
  81. tmpreg = USARTx->CR2;
  82. /* Clear STOP[13:12] bits */
  83. tmpreg &= (uint32_t)~((uint32_t)USART_CR2_STOP);
  84. /* Configure the USART Stop Bits, Clock, CPOL, CPHA and LastBit ------------*/
  85. /* Set STOP[13:12] bits according to USART_StopBits value */
  86. tmpreg |= (uint32_t)USART_InitStruct->USART_StopBits;
  87. /* Write to USART CR2 */
  88. USARTx->CR2 = tmpreg;
  89. /*---------------------------- USART CR1 Configuration -----------------------*/
  90. tmpreg = USARTx->CR1;
  91. /* Clear M, PCE, PS, TE and RE bits */
  92. tmpreg &= (uint32_t)~((uint32_t)CR1_CLEAR_MASK);
  93. /* Configure the USART Word Length, Parity and mode ----------------------- */
  94. /* Set the M bits according to USART_WordLength value */
  95. /* Set PCE and PS bits according to USART_Parity value */
  96. /* Set TE and RE bits according to USART_Mode value */
  97. tmpreg |= (uint32_t)USART_InitStruct->USART_WordLength | USART_InitStruct->USART_Parity |
  98. USART_InitStruct->USART_Mode;
  99. /* Write to USART CR1 */
  100. USARTx->CR1 = tmpreg;
  101. /*---------------------------- USART CR3 Configuration -----------------------*/
  102. tmpreg = USARTx->CR3;
  103. /* Clear CTSE and RTSE bits */
  104. tmpreg &= (uint32_t)~((uint32_t)CR3_CLEAR_MASK);
  105. /* Configure the USART HFC -------------------------------------------------*/
  106. /* Set CTSE and RTSE bits according to USART_HardwareFlowControl value */
  107. tmpreg |= USART_InitStruct->USART_HardwareFlowControl;
  108. /* Write to USART CR3 */
  109. USARTx->CR3 = tmpreg;
  110. /*---------------------------- USART BRR Configuration -----------------------*/
  111. /* Configure the USART Baud Rate -------------------------------------------*/
  112. RCC_GetClocksFreq(&RCC_ClocksStatus);
  113. if (USARTx == USART1)
  114. {
  115. apbclock = RCC_ClocksStatus.USART1CLK_Frequency;
  116. }
  117. else if (USARTx == USART2)
  118. {
  119. apbclock = RCC_ClocksStatus.USART2CLK_Frequency;
  120. }
  121. /* Determine the integer part */
  122. if ((USARTx->CR1 & USART_CR1_OVER8) != 0)
  123. {
  124. /* (divider * 10) computing in case Oversampling mode is 8 Samples */
  125. divider = (uint32_t)((2 * apbclock) / (USART_InitStruct->USART_BaudRate));
  126. tmpreg = (uint32_t)((2 * apbclock) % (USART_InitStruct->USART_BaudRate));
  127. }
  128. else /* if ((USARTx->CR1 & CR1_OVER8_Set) == 0) */
  129. {
  130. /* (divider * 10) computing in case Oversampling mode is 16 Samples */
  131. divider = (uint32_t)((apbclock) / (USART_InitStruct->USART_BaudRate));
  132. tmpreg = (uint32_t)((apbclock) % (USART_InitStruct->USART_BaudRate));
  133. }
  134. /* round the divider : if fractional part i greater than 0.5 increment divider */
  135. if (tmpreg >= (USART_InitStruct->USART_BaudRate) / 2)
  136. {
  137. divider++;
  138. }
  139. /* Implement the divider in case Oversampling mode is 8 Samples */
  140. if ((USARTx->CR1 & USART_CR1_OVER8) != 0)
  141. {
  142. /* get the LSB of divider and shift it to the right by 1 bit */
  143. tmpreg = (divider & (uint16_t)0x000F) >> 1;
  144. /* update the divider value */
  145. divider = (divider & (uint16_t)0xFFF0) | tmpreg;
  146. }
  147. /* Write to USART BRR */
  148. USARTx->BRR = (uint16_t)divider;
  149. }
  150. /**
  151. * @brief Fills each USART_InitStruct member with its default value.
  152. * @param USART_InitStruct: pointer to a USART_InitTypeDef structure
  153. * which will be initialized.
  154. * @retval None
  155. */
  156. void USART_StructInit(USART_InitTypeDef* USART_InitStruct)
  157. {
  158. /* USART_InitStruct members default value */
  159. USART_InitStruct->USART_BaudRate = 9600;
  160. USART_InitStruct->USART_WordLength = USART_WordLength_8b;
  161. USART_InitStruct->USART_StopBits = USART_StopBits_1;
  162. USART_InitStruct->USART_Parity = USART_Parity_No ;
  163. USART_InitStruct->USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  164. USART_InitStruct->USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  165. }
  166. /**
  167. * @brief Initializes the USARTx peripheral Clock according to the
  168. * specified parameters in the USART_ClockInitStruct.
  169. * @param USARTx: where x can be from 1 to 2 to select the USART peripheral.
  170. * @param USART_ClockInitStruct: pointer to a USART_ClockInitTypeDef
  171. * structure that contains the configuration information for the specified
  172. * USART peripheral.
  173. * @retval None
  174. */
  175. void USART_ClockInit(USART_TypeDef* USARTx, USART_ClockInitTypeDef* USART_ClockInitStruct)
  176. {
  177. uint32_t tmpreg = 0;
  178. /* Check the parameters */
  179. assert_param(IS_USART_ALL_PERIPH(USARTx));
  180. assert_param(IS_USART_CLOCK(USART_ClockInitStruct->USART_Clock));
  181. assert_param(IS_USART_CPOL(USART_ClockInitStruct->USART_CPOL));
  182. assert_param(IS_USART_CPHA(USART_ClockInitStruct->USART_CPHA));
  183. assert_param(IS_USART_LASTBIT(USART_ClockInitStruct->USART_LastBit));
  184. /*---------------------------- USART CR2 Configuration -----------------------*/
  185. tmpreg = USARTx->CR2;
  186. /* Clear CLKEN, CPOL, CPHA, LBCL and SSM bits */
  187. tmpreg &= (uint32_t)~((uint32_t)CR2_CLOCK_CLEAR_MASK);
  188. /* Configure the USART Clock, CPOL, CPHA, LastBit and SSM ------------*/
  189. /* Set CLKEN bit according to USART_Clock value */
  190. /* Set CPOL bit according to USART_CPOL value */
  191. /* Set CPHA bit according to USART_CPHA value */
  192. /* Set LBCL bit according to USART_LastBit value */
  193. tmpreg |= (uint32_t)(USART_ClockInitStruct->USART_Clock | USART_ClockInitStruct->USART_CPOL |
  194. USART_ClockInitStruct->USART_CPHA | USART_ClockInitStruct->USART_LastBit);
  195. /* Write to USART CR2 */
  196. USARTx->CR2 = tmpreg;
  197. }
  198. /**
  199. * @brief Fills each USART_ClockInitStruct member with its default value.
  200. * @param USART_ClockInitStruct: pointer to a USART_ClockInitTypeDef
  201. * structure which will be initialized.
  202. * @retval None
  203. */
  204. void USART_ClockStructInit(USART_ClockInitTypeDef* USART_ClockInitStruct)
  205. {
  206. /* USART_ClockInitStruct members default value */
  207. USART_ClockInitStruct->USART_Clock = USART_Clock_Disable;
  208. USART_ClockInitStruct->USART_CPOL = USART_CPOL_Low;
  209. USART_ClockInitStruct->USART_CPHA = USART_CPHA_1Edge;
  210. USART_ClockInitStruct->USART_LastBit = USART_LastBit_Disable;
  211. }
  212. /**
  213. * @brief Enables or disables the specified USART peripheral.
  214. * @param USARTx: where x can be from 1 to 2 to select the USART peripheral.
  215. * @param NewState: new state of the USARTx peripheral.
  216. * This parameter can be: ENABLE or DISABLE.
  217. * @retval None
  218. */
  219. void USART_Cmd(USART_TypeDef* USARTx, FunctionalState NewState)
  220. {
  221. /* Check the parameters */
  222. assert_param(IS_USART_ALL_PERIPH(USARTx));
  223. assert_param(IS_FUNCTIONAL_STATE(NewState));
  224. if (NewState != DISABLE)
  225. {
  226. /* Enable the selected USART by setting the UE bit in the CR1 register */
  227. USARTx->CR1 |= USART_CR1_UE;
  228. }
  229. else
  230. {
  231. /* Disable the selected USART by clearing the UE bit in the CR1 register */
  232. USARTx->CR1 &= (uint32_t)~((uint32_t)USART_CR1_UE);
  233. }
  234. }
  235. /**
  236. * @brief Enables or disables the USART's transmitter or receiver.
  237. * @param USARTx: where x can be from 1 to 2 to select the USART peripheral.
  238. * @param USART_Direction: specifies the USART direction.
  239. * This parameter can be any combination of the following values:
  240. * @arg USART_Mode_Tx: USART Transmitter
  241. * @arg USART_Mode_Rx: USART Receiver
  242. * @param NewState: new state of the USART transfer direction.
  243. * This parameter can be: ENABLE or DISABLE.
  244. * @retval None
  245. */
  246. void USART_DirectionModeCmd(USART_TypeDef* USARTx, uint32_t USART_DirectionMode, FunctionalState NewState)
  247. {
  248. /* Check the parameters */
  249. assert_param(IS_USART_ALL_PERIPH(USARTx));
  250. assert_param(IS_USART_MODE(USART_DirectionMode));
  251. assert_param(IS_FUNCTIONAL_STATE(NewState));
  252. if (NewState != DISABLE)
  253. {
  254. /* Enable the USART's transfer interface by setting the TE and/or RE bits
  255. in the USART CR1 register */
  256. USARTx->CR1 |= USART_DirectionMode;
  257. }
  258. else
  259. {
  260. /* Disable the USART's transfer interface by clearing the TE and/or RE bits
  261. in the USART CR3 register */
  262. USARTx->CR1 &= (uint32_t)~USART_DirectionMode;
  263. }
  264. }
  265. /**
  266. * @brief Enables or disables the USART's 8x oversampling mode.
  267. * @param USARTx: where x can be from 1 to 2 to select the USART peripheral.
  268. * @param NewState: new state of the USART 8x oversampling mode.
  269. * This parameter can be: ENABLE or DISABLE.
  270. * @note This function has to be called before calling USART_Init() function
  271. * in order to have correct baudrate Divider value.
  272. * @retval None
  273. */
  274. void USART_OverSampling8Cmd(USART_TypeDef* USARTx, FunctionalState NewState)
  275. {
  276. /* Check the parameters */
  277. assert_param(IS_USART_ALL_PERIPH(USARTx));
  278. assert_param(IS_FUNCTIONAL_STATE(NewState));
  279. if (NewState != DISABLE)
  280. {
  281. /* Enable the 8x Oversampling mode by setting the OVER8 bit in the CR1 register */
  282. USARTx->CR1 |= USART_CR1_OVER8;
  283. }
  284. else
  285. {
  286. /* Disable the 8x Oversampling mode by clearing the OVER8 bit in the CR1 register */
  287. USARTx->CR1 &= (uint32_t)~((uint32_t)USART_CR1_OVER8);
  288. }
  289. }
  290. /**
  291. * @brief Enables or disables the USART's one bit sampling method.
  292. * @param USARTx: where x can be from 1 to 2 to select the USART peripheral.
  293. * @param NewState: new state of the USART one bit sampling method.
  294. * This parameter can be: ENABLE or DISABLE.
  295. * @note This function has to be called before calling USART_Cmd() function.
  296. * @retval None
  297. */
  298. void USART_OneBitMethodCmd(USART_TypeDef* USARTx, FunctionalState NewState)
  299. {
  300. /* Check the parameters */
  301. assert_param(IS_USART_ALL_PERIPH(USARTx));
  302. assert_param(IS_FUNCTIONAL_STATE(NewState));
  303. if (NewState != DISABLE)
  304. {
  305. /* Enable the one bit method by setting the ONEBITE bit in the CR3 register */
  306. USARTx->CR3 |= USART_CR3_ONEBIT;
  307. }
  308. else
  309. {
  310. /* Disable the one bit method by clearing the ONEBITE bit in the CR3 register */
  311. USARTx->CR3 &= (uint32_t)~((uint32_t)USART_CR3_ONEBIT);
  312. }
  313. }
  314. /**
  315. * @brief Enables or disables the USART's most significant bit first
  316. * transmitted/received following the start bit.
  317. * @param USARTx: where x can be from 1 to 2 to select the USART peripheral.
  318. * @param NewState: new state of the USART most significant bit first
  319. * transmitted/received following the start bit.
  320. * This parameter can be: ENABLE or DISABLE.
  321. * @note This function has to be called before calling USART_Cmd() function.
  322. * @retval None
  323. */
  324. void USART_MSBFirstCmd(USART_TypeDef* USARTx, FunctionalState NewState)
  325. {
  326. /* Check the parameters */
  327. assert_param(IS_USART_ALL_PERIPH(USARTx));
  328. assert_param(IS_FUNCTIONAL_STATE(NewState));
  329. if (NewState != DISABLE)
  330. {
  331. /* Enable the most significant bit first transmitted/received following the
  332. start bit by setting the MSBFIRST bit in the CR2 register */
  333. USARTx->CR2 |= USART_CR2_MSBFIRST;
  334. }
  335. else
  336. {
  337. /* Disable the most significant bit first transmitted/received following the
  338. start bit by clearing the MSBFIRST bit in the CR2 register */
  339. USARTx->CR2 &= (uint32_t)~((uint32_t)USART_CR2_MSBFIRST);
  340. }
  341. }
  342. /**
  343. * @brief Enables or disables the binary data inversion.
  344. * @param USARTx: where x can be from 1 to 2 to select the USART peripheral.
  345. * @param NewState: new defined levels for the USART data.
  346. * This parameter can be:
  347. * @arg ENABLE: Logical data from the data register are send/received in negative
  348. * logic (1=L, 0=H). The parity bit is also inverted.
  349. * @arg DISABLE: Logical data from the data register are send/received in positive
  350. * logic (1=H, 0=L)
  351. * @note This function has to be called before calling USART_Cmd() function.
  352. * @retval None
  353. */
  354. void USART_DataInvCmd(USART_TypeDef* USARTx, FunctionalState NewState)
  355. {
  356. /* Check the parameters */
  357. assert_param(IS_USART_ALL_PERIPH(USARTx));
  358. assert_param(IS_FUNCTIONAL_STATE(NewState));
  359. if (NewState != DISABLE)
  360. {
  361. /* Enable the binary data inversion feature by setting the DATAINV bit in
  362. the CR2 register */
  363. USARTx->CR2 |= USART_CR2_DATAINV;
  364. }
  365. else
  366. {
  367. /* Disable the binary data inversion feature by clearing the DATAINV bit in
  368. the CR2 register */
  369. USARTx->CR2 &= (uint32_t)~((uint32_t)USART_CR2_DATAINV);
  370. }
  371. }
  372. /**
  373. * @brief Enables or disables the Pin(s) active level inversion.
  374. * @param USARTx: where x can be from 1 to 2 to select the USART peripheral.
  375. * @param USART_InvPin: specifies the USART pin(s) to invert.
  376. * This parameter can be any combination of the following values:
  377. * @arg USART_InvPin_Tx: USART Tx pin active level inversion.
  378. * @arg USART_InvPin_Rx: USART Rx pin active level inversion.
  379. * @param NewState: new active level status for the USART pin(s).
  380. * This parameter can be:
  381. * @arg ENABLE: pin(s) signal values are inverted (Vdd =0, Gnd =1).
  382. * @arg DISABLE: pin(s) signal works using the standard logic levels (Vdd =1, Gnd =0).
  383. * @note This function has to be called before calling USART_Cmd() function.
  384. * @retval None
  385. */
  386. void USART_InvPinCmd(USART_TypeDef* USARTx, uint32_t USART_InvPin, FunctionalState NewState)
  387. {
  388. /* Check the parameters */
  389. assert_param(IS_USART_ALL_PERIPH(USARTx));
  390. assert_param(IS_USART_INVERSTION_PIN(USART_InvPin));
  391. assert_param(IS_FUNCTIONAL_STATE(NewState));
  392. if (NewState != DISABLE)
  393. {
  394. /* Enable the active level inversion for selected pins by setting the TXINV
  395. and/or RXINV bits in the USART CR2 register */
  396. USARTx->CR2 |= USART_InvPin;
  397. }
  398. else
  399. {
  400. /* Disable the active level inversion for selected requests by clearing the
  401. TXINV and/or RXINV bits in the USART CR2 register */
  402. USARTx->CR2 &= (uint32_t)~USART_InvPin;
  403. }
  404. }
  405. /**
  406. * @brief Enables or disables the swap Tx/Rx pins.
  407. * @param USARTx: where x can be from 1 to 2 to select the USART peripheral.
  408. * @param NewState: new state of the USARTx TX/RX pins pinout.
  409. * This parameter can be:
  410. * @arg ENABLE: The TX and RX pins functions are swapped.
  411. * @arg DISABLE: TX/RX pins are used as defined in standard pinout
  412. * @note This function has to be called before calling USART_Cmd() function.
  413. * @retval None
  414. */
  415. void USART_SWAPPinCmd(USART_TypeDef* USARTx, FunctionalState NewState)
  416. {
  417. /* Check the parameters */
  418. assert_param(IS_USART_ALL_PERIPH(USARTx));
  419. assert_param(IS_FUNCTIONAL_STATE(NewState));
  420. if (NewState != DISABLE)
  421. {
  422. /* Enable the SWAP feature by setting the SWAP bit in the CR2 register */
  423. USARTx->CR2 |= USART_CR2_SWAP;
  424. }
  425. else
  426. {
  427. /* Disable the SWAP feature by clearing the SWAP bit in the CR2 register */
  428. USARTx->CR2 &= (uint32_t)~((uint32_t)USART_CR2_SWAP);
  429. }
  430. }
  431. /**
  432. * @brief Enables or disables the receiver Time Out feature.
  433. * @param USARTx: where x can be 1, 2 to select the USART peripheral.
  434. * @param NewState: new state of the USARTx receiver Time Out.
  435. * This parameter can be: ENABLE or DISABLE.
  436. * @retval None
  437. */
  438. void USART_ReceiverTimeOutCmd(USART_TypeDef* USARTx, FunctionalState NewState)
  439. {
  440. /* Check the parameters */
  441. assert_param(IS_USART_123_PERIPH(USARTx));
  442. assert_param(IS_FUNCTIONAL_STATE(NewState));
  443. if (NewState != DISABLE)
  444. {
  445. /* Enable the receiver time out feature by setting the RTOEN bit in the CR2
  446. register */
  447. USARTx->CR2 |= USART_CR2_RTOEN;
  448. }
  449. else
  450. {
  451. /* Disable the receiver time out feature by clearing the RTOEN bit in the CR2
  452. register */
  453. USARTx->CR2 &= (uint32_t)~((uint32_t)USART_CR2_RTOEN);
  454. }
  455. }
  456. /**
  457. * @brief Sets the receiver Time Out value.
  458. * @param USARTx: where x can be 1, 2 to select the USART peripheral.
  459. * @param USART_ReceiverTimeOut: specifies the Receiver Time Out value.
  460. * @retval None
  461. */
  462. void USART_SetReceiverTimeOut(USART_TypeDef* USARTx, uint32_t USART_ReceiverTimeOut)
  463. {
  464. /* Check the parameters */
  465. assert_param(IS_USART_123_PERIPH(USARTx));
  466. assert_param(IS_USART_TIMEOUT(USART_ReceiverTimeOut));
  467. /* Clear the receiver Time Out value by clearing the RTO[23:0] bits in the RTOR
  468. register */
  469. USARTx->RTOR &= (uint32_t)~((uint32_t)USART_RTOR_RTO);
  470. /* Set the receiver Time Out value by setting the RTO[23:0] bits in the RTOR
  471. register */
  472. USARTx->RTOR |= USART_ReceiverTimeOut;
  473. }
  474. /**
  475. * @}
  476. */
  477. /**
  478. * @brief Enables or disables the Auto Baud Rate.
  479. * @param USARTx: where x can be 1or 2 to select the USART peripheral.
  480. * @param NewState: new state of the USARTx auto baud rate.
  481. * This parameter can be: ENABLE or DISABLE.
  482. * @retval None
  483. */
  484. void USART_AutoBaudRateCmd(USART_TypeDef* USARTx, FunctionalState NewState)
  485. {
  486. /* Check the parameters */
  487. assert_param(IS_USART_123_PERIPH(USARTx));
  488. assert_param(IS_FUNCTIONAL_STATE(NewState));
  489. if (NewState != DISABLE)
  490. {
  491. /* Enable the auto baud rate feature by setting the ABREN bit in the CR2
  492. register */
  493. USARTx->CR2 |= USART_CR2_ABREN;
  494. }
  495. else
  496. {
  497. /* Disable the auto baud rate feature by clearing the ABREN bit in the CR2
  498. register */
  499. USARTx->CR2 &= (uint32_t)~((uint32_t)USART_CR2_ABREN);
  500. }
  501. }
  502. /**
  503. * @brief Selects the USART auto baud rate method.
  504. * @param USARTx: where x can be 1or 2 to select the USART peripheral.
  505. * @param USART_AutoBaudRate: specifies the selected USART auto baud rate method.
  506. * This parameter can be one of the following values:
  507. * @arg USART_AutoBaudRate_StartBit: Start Bit duration measurement.
  508. * @arg USART_AutoBaudRate_FallingEdge: Falling edge to falling edge measurement.
  509. * @note This function has to be called before calling USART_Cmd() function.
  510. * @retval None
  511. */
  512. void USART_AutoBaudRateConfig(USART_TypeDef* USARTx, uint32_t USART_AutoBaudRate)
  513. {
  514. /* Check the parameters */
  515. assert_param(IS_USART_123_PERIPH(USARTx));
  516. assert_param(IS_USART_AUTOBAUDRATE_MODE(USART_AutoBaudRate));
  517. USARTx->CR2 &= (uint32_t)~((uint32_t)USART_CR2_ABRMODE);
  518. USARTx->CR2 |= USART_AutoBaudRate;
  519. }
  520. /**
  521. * @}
  522. */
  523. /**
  524. * @brief Transmits single data through the USARTx peripheral.
  525. * @param USARTx: where x can be from 1 to 2 to select the USART peripheral.
  526. * @param Data: the data to transmit.
  527. * @retval None
  528. */
  529. void USART_SendData(USART_TypeDef* USARTx, uint16_t Data)
  530. {
  531. /* Check the parameters */
  532. assert_param(IS_USART_ALL_PERIPH(USARTx));
  533. assert_param(IS_USART_DATA(Data));
  534. /* Transmit Data */
  535. USARTx->TDR = (Data & (uint16_t)0x01FF);
  536. }
  537. /**
  538. * @brief Returns the most recent received data by the USARTx peripheral.
  539. * @param USARTx: where x can be from 1 to 2 to select the USART peripheral.
  540. * @retval The received data.
  541. */
  542. uint16_t USART_ReceiveData(USART_TypeDef* USARTx)
  543. {
  544. /* Check the parameters */
  545. assert_param(IS_USART_ALL_PERIPH(USARTx));
  546. /* Receive Data */
  547. return (uint16_t)(USARTx->RDR & (uint16_t)0x01FF);
  548. }
  549. /**
  550. * @}
  551. */
  552. /**
  553. * @brief Sets the address of the USART node.
  554. * @param USARTx: where x can be from 1 to 2 to select the USART peripheral.
  555. * @param USART_Address: Indicates the address of the USART node.
  556. * @retval None
  557. */
  558. void USART_SetAddress(USART_TypeDef* USARTx, uint8_t USART_Address)
  559. {
  560. /* Check the parameters */
  561. assert_param(IS_USART_ALL_PERIPH(USARTx));
  562. /* Clear the USART address */
  563. USARTx->CR2 &= (uint32_t)~((uint32_t)USART_CR2_ADD);
  564. /* Set the USART address node */
  565. USARTx->CR2 |=((uint32_t)USART_Address << (uint32_t)0x18);
  566. }
  567. /**
  568. * @brief Enables or disables the USART's mute mode.
  569. * @param USARTx: where x can be from 1 to 2 to select the USART peripheral.
  570. * @param NewState: new state of the USART mute mode.
  571. * This parameter can be: ENABLE or DISABLE.
  572. * @retval None
  573. */
  574. void USART_MuteModeCmd(USART_TypeDef* USARTx, FunctionalState NewState)
  575. {
  576. /* Check the parameters */
  577. assert_param(IS_USART_ALL_PERIPH(USARTx));
  578. assert_param(IS_FUNCTIONAL_STATE(NewState));
  579. if (NewState != DISABLE)
  580. {
  581. /* Enable the USART mute mode by setting the MME bit in the CR1 register */
  582. USARTx->CR1 |= USART_CR1_MME;
  583. }
  584. else
  585. {
  586. /* Disable the USART mute mode by clearing the MME bit in the CR1 register */
  587. USARTx->CR1 &= (uint32_t)~((uint32_t)USART_CR1_MME);
  588. }
  589. }
  590. /**
  591. * @brief Selects the USART WakeUp method from mute mode.
  592. * @param USARTx: where x can be from 1 to 2 to select the USART peripheral.
  593. * @param USART_WakeUp: specifies the USART wakeup method.
  594. * This parameter can be one of the following values:
  595. * @arg USART_WakeUp_IdleLine: WakeUp by an idle line detection
  596. * @arg USART_WakeUp_AddressMark: WakeUp by an address mark
  597. * @retval None
  598. */
  599. void USART_MuteModeWakeUpConfig(USART_TypeDef* USARTx, uint32_t USART_WakeUp)
  600. {
  601. /* Check the parameters */
  602. assert_param(IS_USART_ALL_PERIPH(USARTx));
  603. assert_param(IS_USART_MUTEMODE_WAKEUP(USART_WakeUp));
  604. USARTx->CR1 &= (uint32_t)~((uint32_t)USART_CR1_WAKE);
  605. USARTx->CR1 |= USART_WakeUp;
  606. }
  607. /**
  608. * @brief Configure the the USART Address detection length.
  609. * @param USARTx: where x can be from 1 to 2 to select the USART peripheral.
  610. * @param USART_AddressLength: specifies the USART address length detection.
  611. * This parameter can be one of the following values:
  612. * @arg USART_AddressLength_4b: 4-bit address length detection
  613. * @arg USART_AddressLength_7b: 7-bit address length detection
  614. * @retval None
  615. */
  616. void USART_AddressDetectionConfig(USART_TypeDef* USARTx, uint32_t USART_AddressLength)
  617. {
  618. /* Check the parameters */
  619. assert_param(IS_USART_ALL_PERIPH(USARTx));
  620. assert_param(IS_USART_ADDRESS_DETECTION(USART_AddressLength));
  621. USARTx->CR2 &= (uint32_t)~((uint32_t)USART_CR2_ADDM7);
  622. USARTx->CR2 |= USART_AddressLength;
  623. }
  624. /**
  625. * @}
  626. */
  627. /**
  628. * @brief Enables or disables the USART's Half Duplex communication.
  629. * @param USARTx: where x can be from 1 to 2 to select the USART peripheral.
  630. * @param NewState: new state of the USART Communication.
  631. * This parameter can be: ENABLE or DISABLE.
  632. * @retval None
  633. */
  634. void USART_HalfDuplexCmd(USART_TypeDef* USARTx, FunctionalState NewState)
  635. {
  636. /* Check the parameters */
  637. assert_param(IS_USART_ALL_PERIPH(USARTx));
  638. assert_param(IS_FUNCTIONAL_STATE(NewState));
  639. if (NewState != DISABLE)
  640. {
  641. /* Enable the Half-Duplex mode by setting the HDSEL bit in the CR3 register */
  642. USARTx->CR3 |= USART_CR3_HDSEL;
  643. }
  644. else
  645. {
  646. /* Disable the Half-Duplex mode by clearing the HDSEL bit in the CR3 register */
  647. USARTx->CR3 &= (uint32_t)~((uint32_t)USART_CR3_HDSEL);
  648. }
  649. }
  650. /**
  651. * @}
  652. */
  653. /**
  654. * @brief Enables or disables the USART's DE functionality.
  655. * @param USARTx: where x can be from 1 to 8 to select the USART peripheral.
  656. * @param NewState: new state of the driver enable mode.
  657. * This parameter can be: ENABLE or DISABLE.
  658. * @retval None
  659. */
  660. void USART_DECmd(USART_TypeDef* USARTx, FunctionalState NewState)
  661. {
  662. /* Check the parameters */
  663. assert_param(IS_USART_ALL_PERIPH(USARTx));
  664. assert_param(IS_FUNCTIONAL_STATE(NewState));
  665. if (NewState != DISABLE)
  666. {
  667. /* Enable the DE functionality by setting the DEM bit in the CR3 register */
  668. USARTx->CR3 |= USART_CR3_DEM;
  669. }
  670. else
  671. {
  672. /* Disable the DE functionality by clearing the DEM bit in the CR3 register */
  673. USARTx->CR3 &= (uint32_t)~((uint32_t)USART_CR3_DEM);
  674. }
  675. }
  676. /**
  677. * @brief Configures the USART's DE polarity
  678. * @param USARTx: where x can be from 1 to 8 to select the USART peripheral.
  679. * @param USART_DEPolarity: specifies the DE polarity.
  680. * This parameter can be one of the following values:
  681. * @arg USART_DEPolarity_Low
  682. * @arg USART_DEPolarity_High
  683. * @retval None
  684. */
  685. void USART_DEPolarityConfig(USART_TypeDef* USARTx, uint32_t USART_DEPolarity)
  686. {
  687. /* Check the parameters */
  688. assert_param(IS_USART_ALL_PERIPH(USARTx));
  689. assert_param(IS_USART_DE_POLARITY(USART_DEPolarity));
  690. USARTx->CR3 &= (uint32_t)~((uint32_t)USART_CR3_DEP);
  691. USARTx->CR3 |= USART_DEPolarity;
  692. }
  693. /**
  694. * @brief Sets the specified RS485 DE assertion time
  695. * @param USARTx: where x can be from 1 to 8 to select the USART peripheral.
  696. * @param USART_DEAssertionTime: specifies the time between the activation of
  697. * the DE signal and the beginning of the start bit
  698. * @retval None
  699. */
  700. void USART_SetDEAssertionTime(USART_TypeDef* USARTx, uint32_t USART_DEAssertionTime)
  701. {
  702. /* Check the parameters */
  703. assert_param(IS_USART_ALL_PERIPH(USARTx));
  704. assert_param(IS_USART_DE_ASSERTION_DEASSERTION_TIME(USART_DEAssertionTime));
  705. /* Clear the DE assertion time */
  706. USARTx->CR1 &= (uint32_t)~((uint32_t)USART_CR1_DEAT);
  707. /* Set the new value for the DE assertion time */
  708. USARTx->CR1 |=((uint32_t)USART_DEAssertionTime << (uint32_t)0x15);
  709. }
  710. /**
  711. * @brief Sets the specified RS485 DE deassertion time
  712. * @param USARTx: where x can be from 1 to 8 to select the USART peripheral.
  713. * @param USART_DeassertionTime: specifies the time between the middle of the last
  714. * stop bit in a transmitted message and the de-activation of the DE signal
  715. * @retval None
  716. */
  717. void USART_SetDEDeassertionTime(USART_TypeDef* USARTx, uint32_t USART_DEDeassertionTime)
  718. {
  719. /* Check the parameters */
  720. assert_param(IS_USART_ALL_PERIPH(USARTx));
  721. assert_param(IS_USART_DE_ASSERTION_DEASSERTION_TIME(USART_DEDeassertionTime));
  722. /* Clear the DE deassertion time */
  723. USARTx->CR1 &= (uint32_t)~((uint32_t)USART_CR1_DEDT);
  724. /* Set the new value for the DE deassertion time */
  725. USARTx->CR1 |=((uint32_t)USART_DEDeassertionTime << (uint32_t)0x10);
  726. }
  727. /**
  728. * @}
  729. */
  730. /**
  731. * @brief Enables or disables the USART's DMA interface.
  732. * @param USARTx: where x can be from 1 to 8 to select the USART peripheral.
  733. * @param USART_DMAReq: specifies the DMA request.
  734. * This parameter can be any combination of the following values:
  735. * @arg USART_DMAReq_Tx: USART DMA transmit request
  736. * @arg USART_DMAReq_Rx: USART DMA receive request
  737. * @param NewState: new state of the DMA Request sources.
  738. * This parameter can be: ENABLE or DISABLE.
  739. * @retval None
  740. */
  741. void USART_DMACmd(USART_TypeDef* USARTx, uint32_t USART_DMAReq, FunctionalState NewState)
  742. {
  743. /* Check the parameters */
  744. assert_param(IS_USART_ALL_PERIPH(USARTx));
  745. assert_param(IS_USART_DMAREQ(USART_DMAReq));
  746. assert_param(IS_FUNCTIONAL_STATE(NewState));
  747. if (NewState != DISABLE)
  748. {
  749. /* Enable the DMA transfer for selected requests by setting the DMAT and/or
  750. DMAR bits in the USART CR3 register */
  751. USARTx->CR3 |= USART_DMAReq;
  752. }
  753. else
  754. {
  755. /* Disable the DMA transfer for selected requests by clearing the DMAT and/or
  756. DMAR bits in the USART CR3 register */
  757. USARTx->CR3 &= (uint32_t)~USART_DMAReq;
  758. }
  759. }
  760. /**
  761. * @brief Enables or disables the USART's DMA interface when reception error occurs.
  762. * @param USARTx: where x can be from 1 to 8 to select the USART peripheral.
  763. * @param USART_DMAOnError: specifies the DMA status in case of reception error.
  764. * This parameter can be any combination of the following values:
  765. * @arg USART_DMAOnError_Enable: DMA receive request enabled when the USART DMA
  766. * reception error is asserted.
  767. * @arg USART_DMAOnError_Disable: DMA receive request disabled when the USART DMA
  768. * reception error is asserted.
  769. * @retval None
  770. */
  771. void USART_DMAReceptionErrorConfig(USART_TypeDef* USARTx, uint32_t USART_DMAOnError)
  772. {
  773. /* Check the parameters */
  774. assert_param(IS_USART_ALL_PERIPH(USARTx));
  775. assert_param(IS_USART_DMAONERROR(USART_DMAOnError));
  776. /* Clear the DMA Reception error detection bit */
  777. USARTx->CR3 &= (uint32_t)~((uint32_t)USART_CR3_DDRE);
  778. /* Set the new value for the DMA Reception error detection bit */
  779. USARTx->CR3 |= USART_DMAOnError;
  780. }
  781. /**
  782. * @}
  783. */
  784. /**
  785. * @brief Enables or disables the specified USART interrupts.
  786. * @param USARTx: where x can be from 1 to 2 to select the USART peripheral.
  787. * @param USART_IT: specifies the USART interrupt sources to be enabled or disabled.
  788. * This parameter can be one of the following values:
  789. * @arg USART_IT_WU: Wake up interrupt
  790. * @arg USART_IT_CM: Character match interrupt.
  791. * @arg USART_IT_EOB: End of block interrupt
  792. * @arg USART_IT_RTO: Receive time out interrupt.
  793. * @arg USART_IT_CTS: CTS change interrupt.
  794. * @arg USART_IT_LBD: LIN Break detection interrupt
  795. * @arg USART_IT_TXE: Tansmit Data Register empty interrupt.
  796. * @arg USART_IT_TC: Transmission complete interrupt.
  797. * @arg USART_IT_RXNE: Receive Data register not empty interrupt.
  798. * @arg USART_IT_IDLE: Idle line detection interrupt.
  799. * @arg USART_IT_PE: Parity Error interrupt.
  800. * @arg USART_IT_ERR: Error interrupt(Frame error, noise error, overrun error)
  801. * @param NewState: new state of the specified USARTx interrupts.
  802. * This parameter can be: ENABLE or DISABLE.
  803. * @retval None
  804. */
  805. void USART_ITConfig(USART_TypeDef* USARTx, uint32_t USART_IT, FunctionalState NewState)
  806. {
  807. uint32_t usartreg = 0, itpos = 0, itmask = 0;
  808. uint32_t usartxbase = 0;
  809. /* Check the parameters */
  810. assert_param(IS_USART_ALL_PERIPH(USARTx));
  811. assert_param(IS_USART_CONFIG_IT(USART_IT));
  812. assert_param(IS_FUNCTIONAL_STATE(NewState));
  813. usartxbase = (uint32_t)USARTx;
  814. /* Get the USART register index */
  815. usartreg = (((uint16_t)USART_IT) >> 0x08);
  816. /* Get the interrupt position */
  817. itpos = USART_IT & IT_MASK;
  818. itmask = (((uint32_t)0x01) << itpos);
  819. if (usartreg == 0x02) /* The IT is in CR2 register */
  820. {
  821. usartxbase += 0x04;
  822. }
  823. else if (usartreg == 0x03) /* The IT is in CR3 register */
  824. {
  825. usartxbase += 0x08;
  826. }
  827. else /* The IT is in CR1 register */
  828. {
  829. }
  830. if (NewState != DISABLE)
  831. {
  832. *(__IO uint32_t*)usartxbase |= itmask;
  833. }
  834. else
  835. {
  836. *(__IO uint32_t*)usartxbase &= ~itmask;
  837. }
  838. }
  839. /**
  840. * @brief Enables the specified USART's Request.
  841. * @param USARTx: where x can be from 1 to 2 to select the USART peripheral.
  842. * @param USART_Request: specifies the USART request.
  843. * This parameter can be any combination of the following values:
  844. * @arg USART_Request_TXFRQ: Transmit data flush ReQuest
  845. * @arg USART_Request_RXFRQ: Receive data flush ReQuest
  846. * @arg USART_Request_MMRQ: Mute Mode ReQuest
  847. * @arg USART_Request_SBKRQ: Send Break ReQuest
  848. * @arg USART_Request_ABRRQ: Auto Baud Rate ReQuest
  849. * @param NewState: new state of the DMA interface when reception error occurs.
  850. * This parameter can be: ENABLE or DISABLE.
  851. * @retval None
  852. */
  853. void USART_RequestCmd(USART_TypeDef* USARTx, uint32_t USART_Request, FunctionalState NewState)
  854. {
  855. /* Check the parameters */
  856. assert_param(IS_USART_ALL_PERIPH(USARTx));
  857. assert_param(IS_USART_REQUEST(USART_Request));
  858. assert_param(IS_FUNCTIONAL_STATE(NewState));
  859. if (NewState != DISABLE)
  860. {
  861. /* Enable the USART ReQuest by setting the dedicated request bit in the RQR
  862. register.*/
  863. USARTx->RQR |= USART_Request;
  864. }
  865. else
  866. {
  867. /* Disable the USART ReQuest by clearing the dedicated request bit in the RQR
  868. register.*/
  869. USARTx->RQR &= (uint32_t)~USART_Request;
  870. }
  871. }
  872. /**
  873. * @brief Enables or disables the USART's Overrun detection.
  874. * @param USARTx: where x can be from 1 to 2 to select the USART peripheral.
  875. * @param USART_OVRDetection: specifies the OVR detection status in case of OVR error.
  876. * This parameter can be any combination of the following values:
  877. * @arg USART_OVRDetection_Enable: OVR error detection enabled when
  878. * the USART OVR error is asserted.
  879. * @arg USART_OVRDetection_Disable: OVR error detection disabled when
  880. * the USART OVR error is asserted.
  881. * @retval None
  882. */
  883. void USART_OverrunDetectionConfig(USART_TypeDef* USARTx, uint32_t USART_OVRDetection)
  884. {
  885. /* Check the parameters */
  886. assert_param(IS_USART_ALL_PERIPH(USARTx));
  887. assert_param(IS_USART_OVRDETECTION(USART_OVRDetection));
  888. /* Clear the OVR detection bit */
  889. USARTx->CR3 &= (uint32_t)~((uint32_t)USART_CR3_OVRDIS);
  890. /* Set the new value for the OVR detection bit */
  891. USARTx->CR3 |= USART_OVRDetection;
  892. }
  893. /**
  894. * @brief Checks whether the specified USART flag is set or not.
  895. * @param USARTx: where x can be from 1 to 2 to select the USART peripheral..
  896. * @param USART_FLAG: specifies the flag to check.
  897. * This parameter can be one of the following values:
  898. * @arg USART_FLAG_REACK: Receive Enable acknowledge flag.
  899. * @arg USART_FLAG_TEACK: Transmit Enable acknowledge flag.
  900. * @arg USART_FLAG_WU: Wake up flag
  901. * @arg USART_FLAG_RWU: Receive Wake up flag
  902. * @arg USART_FLAG_SBK: Send Break flag.
  903. * @arg USART_FLAG_CM: Character match flag.
  904. * @arg USART_FLAG_BUSY: Busy flag.
  905. * @arg USART_FLAG_ABRF: Auto baud rate flag.
  906. * @arg USART_FLAG_ABRE: Auto baud rate error flag.
  907. * @arg USART_FLAG_EOB: End of block flag
  908. * @arg USART_FLAG_RTO: Receive time out flag.
  909. * @arg USART_FLAG_nCTSS: Inverted nCTS input bit status.
  910. * @arg USART_FLAG_CTS: CTS Change flag.
  911. * @arg USART_FLAG_LBD: LIN Break detection flag
  912. * @arg USART_FLAG_TXE: Transmit data register empty flag.
  913. * @arg USART_FLAG_TC: Transmission Complete flag.
  914. * @arg USART_FLAG_RXNE: Receive data register not empty flag.
  915. * @arg USART_FLAG_IDLE: Idle Line detection flag.
  916. * @arg USART_FLAG_ORE: OverRun Error flag.
  917. * @arg USART_FLAG_NE: Noise Error flag.
  918. * @arg USART_FLAG_FE: Framing Error flag.
  919. * @arg USART_FLAG_PE: Parity Error flag.
  920. * @retval The new state of USART_FLAG (SET or RESET).
  921. */
  922. FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, uint32_t USART_FLAG)
  923. {
  924. FlagStatus bitstatus = RESET;
  925. /* Check the parameters */
  926. assert_param(IS_USART_ALL_PERIPH(USARTx));
  927. assert_param(IS_USART_FLAG(USART_FLAG));
  928. if ((USARTx->ISR & USART_FLAG) != (uint16_t)RESET)
  929. {
  930. bitstatus = SET;
  931. }
  932. else
  933. {
  934. bitstatus = RESET;
  935. }
  936. return bitstatus;
  937. }
  938. /**
  939. * @brief Clears the USARTx's pending flags.
  940. * @param USARTx: where x can be from 1 to 2 to select the USART peripheral.
  941. * @param USART_FLAG: specifies the flag to clear.
  942. * This parameter can be any combination of the following values:
  943. * @arg USART_FLAG_WU: Wake up flag
  944. * @arg USART_FLAG_CM: Character match flag.
  945. * @arg USART_FLAG_EOB: End of block flag
  946. * @arg USART_FLAG_RTO: Receive time out flag.
  947. * @arg USART_FLAG_CTS: CTS Change flag.
  948. * @arg USART_FLAG_LBD: LIN Break detection flag
  949. * @arg USART_FLAG_TC: Transmission Complete flag.
  950. * @arg USART_FLAG_IDLE: IDLE line detected flag.
  951. * @arg USART_FLAG_ORE: OverRun Error flag.
  952. * @arg USART_FLAG_NE: Noise Error flag.
  953. * @arg USART_FLAG_FE: Framing Error flag.
  954. * @arg USART_FLAG_PE: Parity Errorflag.
  955. *
  956. * @note RXNE pending bit is cleared by a read to the USART_RDR register
  957. * (USART_ReceiveData()) or by writing 1 to the RXFRQ in the register
  958. * USART_RQR (USART_RequestCmd()).
  959. * @note TC flag can be also cleared by software sequence: a read operation
  960. * to USART_SR register (USART_GetFlagStatus()) followed by a write
  961. * operation to USART_TDR register (USART_SendData()).
  962. * @note TXE flag is cleared by a write to the USART_TDR register (USART_SendData())
  963. * or by writing 1 to the TXFRQ in the register USART_RQR (USART_RequestCmd()).
  964. * @note SBKF flag is cleared by 1 to the SBKRQ in the register USART_RQR
  965. * (USART_RequestCmd()).
  966. * @retval None
  967. */
  968. void USART_ClearFlag(USART_TypeDef* USARTx, uint32_t USART_FLAG)
  969. {
  970. /* Check the parameters */
  971. assert_param(IS_USART_ALL_PERIPH(USARTx));
  972. assert_param(IS_USART_CLEAR_FLAG(USART_FLAG));
  973. USARTx->ICR = USART_FLAG;
  974. }
  975. /**
  976. * @brief Checks whether the specified USART interrupt has occurred or not.
  977. * @param USARTx: where x can be from 1 to 2 to select the USART peripheral.
  978. * @param USART_IT: specifies the USART interrupt source to check.
  979. * This parameter can be one of the following values:
  980. * @arg USART_IT_WU: Wake up interrupt
  981. * @arg USART_IT_CM: Character match interrupt.
  982. * @arg USART_IT_EOB: End of block interrupt
  983. * @arg USART_IT_RTO: Receive time out interrupt.
  984. * @arg USART_IT_CTS: CTS change interrupt.
  985. * @arg USART_IT_LBD: LIN Break detection interrupt
  986. * @arg USART_IT_TXE: Tansmit Data Register empty interrupt.
  987. * @arg USART_IT_TC: Transmission complete interrupt.
  988. * @arg USART_IT_RXNE: Receive Data register not empty interrupt.
  989. * @arg USART_IT_IDLE: Idle line detection interrupt.
  990. * @arg USART_IT_ORE: OverRun Error interrupt.
  991. * @arg USART_IT_NE: Noise Error interrupt.
  992. * @arg USART_IT_FE: Framing Error interrupt.
  993. * @arg USART_IT_PE: Parity Error interrupt.
  994. * @retval The new state of USART_IT (SET or RESET).
  995. */
  996. ITStatus USART_GetITStatus(USART_TypeDef* USARTx, uint32_t USART_IT)
  997. {
  998. uint32_t bitpos = 0, itmask = 0, usartreg = 0;
  999. ITStatus bitstatus = RESET;
  1000. /* Check the parameters */
  1001. assert_param(IS_USART_ALL_PERIPH(USARTx));
  1002. assert_param(IS_USART_GET_IT(USART_IT));
  1003. /* Get the USART register index */
  1004. usartreg = (((uint16_t)USART_IT) >> 0x08);
  1005. /* Get the interrupt position */
  1006. itmask = USART_IT & IT_MASK;
  1007. itmask = (uint32_t)0x01 << itmask;
  1008. if (usartreg == 0x01) /* The IT is in CR1 register */
  1009. {
  1010. itmask &= USARTx->CR1;
  1011. }
  1012. else if (usartreg == 0x02) /* The IT is in CR2 register */
  1013. {
  1014. itmask &= USARTx->CR2;
  1015. }
  1016. else /* The IT is in CR3 register */
  1017. {
  1018. itmask &= USARTx->CR3;
  1019. }
  1020. bitpos = USART_IT >> 0x10;
  1021. bitpos = (uint32_t)0x01 << bitpos;
  1022. bitpos &= USARTx->ISR;
  1023. if ((itmask != (uint16_t)RESET)&&(bitpos != (uint16_t)RESET))
  1024. {
  1025. bitstatus = SET;
  1026. }
  1027. else
  1028. {
  1029. bitstatus = RESET;
  1030. }
  1031. return bitstatus;
  1032. }
  1033. /**
  1034. * @brief Clears the USARTx's interrupt pending bits.
  1035. * @param USARTx: where x can be from 1 to 2 to select the USART peripheral.
  1036. * @param USART_IT: specifies the interrupt pending bit to clear.
  1037. * This parameter can be one of the following values:
  1038. * @arg USART_IT_WU: Wake up interrupt
  1039. * @arg USART_IT_CM: Character match interrupt.
  1040. * @arg USART_IT_EOB: End of block interrupt
  1041. * @arg USART_IT_RTO: Receive time out interrupt.
  1042. * @arg USART_IT_CTS: CTS change interrupt.
  1043. * @arg USART_IT_LBD: LIN Break detection interrupt
  1044. * @arg USART_IT_TC: Transmission complete interrupt.
  1045. * @arg USART_IT_IDLE: IDLE line detected interrupt.
  1046. * @arg USART_IT_ORE: OverRun Error interrupt.
  1047. * @arg USART_IT_NE: Noise Error interrupt.
  1048. * @arg USART_IT_FE: Framing Error interrupt.
  1049. * @arg USART_IT_PE: Parity Error interrupt.
  1050. *
  1051. * @note RXNE pending bit is cleared by a read to the USART_RDR register
  1052. * (USART_ReceiveData()) or by writing 1 to the RXFRQ in the register
  1053. * USART_RQR (USART_RequestCmd()).
  1054. * @note TC pending bit can be also cleared by software sequence: a read
  1055. * operation to USART_SR register (USART_GetITStatus()) followed by
  1056. * a write operation to USART_TDR register (USART_SendData()).
  1057. * @note TXE pending bit is cleared by a write to the USART_TDR register
  1058. * (USART_SendData()) or by writing 1 to the TXFRQ in the register
  1059. * USART_RQR (USART_RequestCmd()).
  1060. * @retval None
  1061. */
  1062. void USART_ClearITPendingBit(USART_TypeDef* USARTx, uint32_t USART_IT)
  1063. {
  1064. uint32_t bitpos = 0, itmask = 0;
  1065. /* Check the parameters */
  1066. assert_param(IS_USART_ALL_PERIPH(USARTx));
  1067. assert_param(IS_USART_CLEAR_IT(USART_IT));
  1068. bitpos = USART_IT >> 0x10;
  1069. itmask = ((uint32_t)0x01 << (uint32_t)bitpos);
  1070. USARTx->ICR = (uint32_t)itmask;
  1071. }
  1072. /**
  1073. * @}
  1074. */
  1075. /**
  1076. * @}
  1077. */
  1078. /**
  1079. * @}
  1080. */
  1081. /**
  1082. * @}
  1083. */
  1084. /************************ (C) COPYRIGHT FMD *****END OF FILE****/