at32f413_usart.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. /**
  2. **************************************************************************
  3. * @file at32f413_usart.c
  4. * @brief contains all the functions for the usart firmware library
  5. **************************************************************************
  6. * Copyright notice & Disclaimer
  7. *
  8. * The software Board Support Package (BSP) that is made available to
  9. * download from Artery official website is the copyrighted work of Artery.
  10. * Artery authorizes customers to use, copy, and distribute the BSP
  11. * software and its related documentation for the purpose of design and
  12. * development in conjunction with Artery microcontrollers. Use of the
  13. * software is governed by this copyright notice and the following disclaimer.
  14. *
  15. * THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
  16. * GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
  17. * TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
  18. * STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
  19. * INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
  21. *
  22. **************************************************************************
  23. */
  24. /* includes ------------------------------------------------------------------*/
  25. #include "at32f413_conf.h"
  26. /** @addtogroup AT32F413_periph_driver
  27. * @{
  28. */
  29. /** @defgroup USART
  30. * @brief USART driver modules
  31. * @{
  32. */
  33. #ifdef USART_MODULE_ENABLED
  34. /** @defgroup USART_private_functions
  35. * @{
  36. */
  37. /**
  38. * @brief deinitialize the usart peripheral registers to their default reset values.
  39. * @param usart_x: select the usart or the uart peripheral.
  40. * this parameter can be one of the following values:
  41. * USART1, USART2, USART3, UART4 or UART5.
  42. * @retval none
  43. */
  44. void usart_reset(usart_type* usart_x)
  45. {
  46. if(usart_x == USART1)
  47. {
  48. crm_periph_reset(CRM_USART1_PERIPH_RESET, TRUE);
  49. crm_periph_reset(CRM_USART1_PERIPH_RESET, FALSE);
  50. }
  51. else if(usart_x == USART2)
  52. {
  53. crm_periph_reset(CRM_USART2_PERIPH_RESET, TRUE);
  54. crm_periph_reset(CRM_USART2_PERIPH_RESET, FALSE);
  55. }
  56. #if defined (AT32F413Rx) || defined (AT32F413Cx) || defined (AT32FEBKC8T7)
  57. else if(usart_x == USART3)
  58. {
  59. crm_periph_reset(CRM_USART3_PERIPH_RESET, TRUE);
  60. crm_periph_reset(CRM_USART3_PERIPH_RESET, FALSE);
  61. }
  62. #endif
  63. #if defined (AT32F413Rx)
  64. else if(usart_x == UART4)
  65. {
  66. crm_periph_reset(CRM_UART4_PERIPH_RESET, TRUE);
  67. crm_periph_reset(CRM_UART4_PERIPH_RESET, FALSE);
  68. }
  69. else if(usart_x == UART5)
  70. {
  71. crm_periph_reset(CRM_UART5_PERIPH_RESET, TRUE);
  72. crm_periph_reset(CRM_UART5_PERIPH_RESET, FALSE);
  73. }
  74. #endif
  75. }
  76. /**
  77. * @brief initialize the usart peripheral according to the specified parameters.
  78. * @param usart_x: select the usart or the uart peripheral.
  79. * this parameter can be one of the following values:
  80. * USART1, USART2, USART3, UART4 or UART5.
  81. * @param baud_rate: configure the usart communication baud rate.
  82. * @param data_bit: data bits transmitted or received in a frame
  83. * this parameter can be one of the following values:
  84. * - USART_DATA_8BITS
  85. * - USART_DATA_9BITS.
  86. * @param stop_bit: stop bits transmitted
  87. * this parameter can be one of the following values:
  88. * - USART_STOP_1_BIT
  89. * - USART_STOP_0_5_BIT.
  90. * - USART_STOP_2_BIT
  91. * - USART_STOP_1_5_BIT.
  92. * @retval none
  93. */
  94. void usart_init(usart_type* usart_x, uint32_t baud_rate, usart_data_bit_num_type data_bit, usart_stop_bit_num_type stop_bit)
  95. {
  96. crm_clocks_freq_type clocks_freq;
  97. uint32_t apb_clock, temp_val;
  98. crm_clocks_freq_get(&clocks_freq);
  99. if(usart_x == USART1)
  100. {
  101. apb_clock = clocks_freq.apb2_freq;
  102. }
  103. else
  104. {
  105. apb_clock = clocks_freq.apb1_freq;
  106. }
  107. temp_val = (apb_clock * 10 / baud_rate);
  108. if((temp_val % 10) < 5)
  109. {
  110. temp_val = (temp_val / 10);
  111. }
  112. else
  113. {
  114. temp_val = (temp_val / 10) + 1;
  115. }
  116. usart_x->baudr_bit.div = temp_val;
  117. usart_x->ctrl1_bit.dbn = data_bit;
  118. usart_x->ctrl2_bit.stopbn = stop_bit;
  119. }
  120. /**
  121. * @brief usart parity selection config.
  122. * @param usart_x: select the usart or the uart peripheral.
  123. * this parameter can be one of the following values:
  124. * USART1, USART2, USART3, UART4 or UART5.
  125. * @param parity: select the none, odd or even parity.
  126. * this parameter can be one of the following values:
  127. * - USART_PARITY_NONE
  128. * - USART_PARITY_EVEN.
  129. * - USART_PARITY_ODD
  130. * @retval none
  131. */
  132. void usart_parity_selection_config(usart_type* usart_x, usart_parity_selection_type parity)
  133. {
  134. if(parity == USART_PARITY_NONE)
  135. {
  136. usart_x->ctrl1_bit.psel = FALSE;
  137. usart_x->ctrl1_bit.pen = FALSE;
  138. }
  139. else if(parity == USART_PARITY_EVEN)
  140. {
  141. usart_x->ctrl1_bit.psel = FALSE;
  142. usart_x->ctrl1_bit.pen = TRUE;
  143. }
  144. else if(parity == USART_PARITY_ODD)
  145. {
  146. usart_x->ctrl1_bit.psel = TRUE;
  147. usart_x->ctrl1_bit.pen = TRUE;
  148. }
  149. }
  150. /**
  151. * @brief enable or disable the specified usart peripheral.
  152. * @param usart_x: select the usart or the uart peripheral.
  153. * this parameter can be one of the following values:
  154. * USART1, USART2, USART3, UART4 or UART5.
  155. * @param new_state: new state of the usart peripheral.
  156. * this parameter can be: TRUE or FALSE.
  157. * @retval none
  158. */
  159. void usart_enable(usart_type* usart_x, confirm_state new_state)
  160. {
  161. usart_x->ctrl1_bit.uen = new_state;
  162. }
  163. /**
  164. * @brief usart transmitter enable.
  165. * @param usart_x: select the usart or the uart peripheral.
  166. * this parameter can be one of the following values:
  167. * USART1, USART2, USART3, UART4 or UART5.
  168. * @param new_state: TRUE or FALSE.
  169. * @retval none
  170. */
  171. void usart_transmitter_enable(usart_type* usart_x, confirm_state new_state)
  172. {
  173. usart_x->ctrl1_bit.ten = new_state;
  174. }
  175. /**
  176. * @brief usart receiver enable.
  177. * @param usart_x: select the usart or the uart peripheral.
  178. * this parameter can be one of the following values:
  179. * USART1, USART2, USART3, UART4 or UART5.
  180. * @param new_state: TRUE or FALSE.
  181. * @retval none
  182. */
  183. void usart_receiver_enable(usart_type* usart_x, confirm_state new_state)
  184. {
  185. usart_x->ctrl1_bit.ren = new_state;
  186. }
  187. /**
  188. * @brief usart clock config.
  189. * @note clock config are not available for UART4 and UART5.
  190. * @param usart_x: select the usart or the uart peripheral.
  191. * this parameter can be one of the following values:
  192. * USART1, USART2 or USART3.
  193. * @param clk_pol: polarity of the clock output on the ck pin.
  194. * this parameter can be one of the following values:
  195. * - USART_CLOCK_POLARITY_LOW
  196. * - USART_CLOCK_POLARITY_HIGH
  197. * @param clk_pha: phase of the clock output on the ck pin.
  198. * this parameter can be one of the following values:
  199. * - USART_CLOCK_PHASE_1EDGE
  200. * - USART_CLOCK_PHASE_2EDGE
  201. * @param clk_lb: whether the clock pulse of the last data bit transmitted (MSB) is outputted on the ck pin.
  202. * this parameter can be one of the following values:
  203. * - USART_CLOCK_LAST_BIT_NONE
  204. * - USART_CLOCK_LAST_BIT_OUTPUT
  205. * @retval none
  206. */
  207. void usart_clock_config(usart_type* usart_x, usart_clock_polarity_type clk_pol, usart_clock_phase_type clk_pha, usart_lbcp_type clk_lb)
  208. {
  209. usart_x->ctrl2_bit.clkpol = clk_pol;
  210. usart_x->ctrl2_bit.clkpha = clk_pha;
  211. usart_x->ctrl2_bit.lbcp = clk_lb;
  212. }
  213. /**
  214. * @brief usart enable the ck pin.
  215. * @note clock enable are not available for UART4 and UART5.
  216. * @param usart_x: select the usart or the uart peripheral.
  217. * this parameter can be one of the following values:
  218. * USART1, USART2 or USART3.
  219. * @param new_state: TRUE or FALSE
  220. * @retval none
  221. */
  222. void usart_clock_enable(usart_type* usart_x, confirm_state new_state)
  223. {
  224. usart_x->ctrl2_bit.clken = new_state;
  225. }
  226. /**
  227. * @brief enable or disable the specified usart interrupts.
  228. * @param usart_x: select the usart or the uart peripheral.
  229. * this parameter can be one of the following values:
  230. * USART1, USART2, USART3, UART4 or UART5.
  231. * @param usart_int: specifies the USART interrupt sources to be enabled or disabled.
  232. * this parameter can be one of the following values:
  233. * - USART_IDLE_INT: idle interrupt
  234. * - USART_RDBF_INT: rdbf interrupt
  235. * - USART_TDC_INT: tdc interrupt
  236. * - USART_TDBE_INT: tdbe interrupt
  237. * - USART_PERR_INT: perr interrupt
  238. * - USART_BF_INT: break frame interrupt
  239. * - USART_ERR_INT: err interrupt
  240. * - USART_CTSCF_INT: ctscf interrupt
  241. * @param new_state: new state of the specified usart interrupts.
  242. * this parameter can be: TRUE or FALSE.
  243. * @retval none
  244. */
  245. void usart_interrupt_enable(usart_type* usart_x, uint32_t usart_int, confirm_state new_state)
  246. {
  247. if(new_state == TRUE)
  248. PERIPH_REG((uint32_t)usart_x, usart_int) |= PERIPH_REG_BIT(usart_int);
  249. else
  250. PERIPH_REG((uint32_t)usart_x, usart_int) &= ~PERIPH_REG_BIT(usart_int);
  251. }
  252. /**
  253. * @brief enable or disable the usart's dma transmitter interface.
  254. * @param usart_x: select the usart or the uart peripheral.
  255. * this parameter can be one of the following values:
  256. * USART1, USART2, USART3, UART4 or UART5.
  257. * @param new_state: new state of the dma request sources.
  258. * this parameter can be: TRUE or FALSE.
  259. * @retval none
  260. */
  261. void usart_dma_transmitter_enable(usart_type* usart_x, confirm_state new_state)
  262. {
  263. usart_x->ctrl3_bit.dmaten = new_state;
  264. }
  265. /**
  266. * @brief enable or disable the usart's dma receiver interface.
  267. * @param usart_x: select the usart or the uart peripheral.
  268. * this parameter can be one of the following values:
  269. * USART1, USART2, USART3, UART4 or UART5.
  270. * @param new_state: new state of the dma request sources.
  271. * this parameter can be: TRUE or FALSE.
  272. * @retval none
  273. */
  274. void usart_dma_receiver_enable(usart_type* usart_x, confirm_state new_state)
  275. {
  276. usart_x->ctrl3_bit.dmaren = new_state;
  277. }
  278. /**
  279. * @brief set the wakeup id of the usart.
  280. * @param usart_x: select the usart or the uart peripheral.
  281. * this parameter can be one of the following values:
  282. * USART1, USART2, USART3, UART4 or UART5.
  283. * @param usart_id: the matching id(0x0~0xF).
  284. * @retval none
  285. */
  286. void usart_wakeup_id_set(usart_type* usart_x, uint8_t usart_id)
  287. {
  288. usart_x->ctrl2_bit.id = usart_id;
  289. }
  290. /**
  291. * @brief select the usart wakeup method in multi-processor communication.
  292. * @param usart_x: select the usart or the uart peripheral.
  293. * this parameter can be one of the following values:
  294. * USART1, USART2, USART3, UART4 or UART5.
  295. * @param wakeup_mode: determines the way to wake up usart method.
  296. * this parameter can be one of the following values:
  297. * - USART_WAKEUP_BY_IDLE_FRAME
  298. * - USART_WAKEUP_BY_MATCHING_ID
  299. * @retval none
  300. */
  301. void usart_wakeup_mode_set(usart_type* usart_x, usart_wakeup_mode_type wakeup_mode)
  302. {
  303. usart_x->ctrl1_bit.wum = wakeup_mode;
  304. }
  305. /**
  306. * @brief config the usart in mute mode in multi-processor communication.
  307. * @param usart_x: select the usart or the uart peripheral.
  308. * this parameter can be one of the following values:
  309. * USART1, USART2, USART3, UART4 or UART5.
  310. * @param new_state: new state of the usart mute mode.
  311. * this parameter can be: TRUE or FALSE.
  312. * @retval none
  313. */
  314. void usart_receiver_mute_enable(usart_type* usart_x, confirm_state new_state)
  315. {
  316. usart_x->ctrl1_bit.rm = new_state;
  317. }
  318. /**
  319. * @brief set the usart break frame bit num.
  320. * @param usart_x: select the usart or the uart peripheral.
  321. * this parameter can be one of the following values:
  322. * USART1, USART2, USART3, UART4 or UART5.
  323. * @param break_bit: specifies the break bit num.
  324. * this parameter can be one of the following values:
  325. * - USART_BREAK_10BITS
  326. * - USART_BREAK_11BITS
  327. * @retval none
  328. */
  329. void usart_break_bit_num_set(usart_type* usart_x, usart_break_bit_num_type break_bit)
  330. {
  331. usart_x->ctrl2_bit.bfbn = break_bit;
  332. }
  333. /**
  334. * @brief enable or disable the usart lin mode.
  335. * @param usart_x: select the usart or the uart peripheral.
  336. * this parameter can be one of the following values:
  337. * USART1, USART2, USART3, UART4 or UART5.
  338. * @param new_state: new state of the usart lin mode.
  339. * this parameter can be: TRUE or FALSE.
  340. * @retval none
  341. */
  342. void usart_lin_mode_enable(usart_type* usart_x, confirm_state new_state)
  343. {
  344. usart_x->ctrl2_bit.linen = new_state;
  345. }
  346. /**
  347. * @brief transmit single data through the usart peripheral.
  348. * @param usart_x: select the usart or the uart peripheral.
  349. * this parameter can be one of the following values:
  350. * USART1, USART2, USART3, UART4 or UART5.
  351. * @param data: the data to transmit.
  352. * @retval none
  353. */
  354. void usart_data_transmit(usart_type* usart_x, uint16_t data)
  355. {
  356. usart_x->dt = (data & 0x01FF);
  357. }
  358. /**
  359. * @brief return the most recent received data by the usart peripheral.
  360. * @param usart_x: select the usart or the uart peripheral.
  361. * this parameter can be one of the following values:
  362. * USART1, USART2, USART3, UART4 or UART5.
  363. * @retval the received data.
  364. */
  365. uint16_t usart_data_receive(usart_type* usart_x)
  366. {
  367. return (uint16_t)(usart_x->dt);
  368. }
  369. /**
  370. * @brief transmit break characters.
  371. * @param usart_x: select the usart or the uart peripheral.
  372. * this parameter can be one of the following values:
  373. * USART1, USART2, USART3, UART4 or UART5.
  374. * @retval none
  375. */
  376. void usart_break_send(usart_type* usart_x)
  377. {
  378. usart_x->ctrl1_bit.sbf = TRUE;
  379. }
  380. /**
  381. * @brief config the specified usart smartcard guard time.
  382. * @note The guard time bits are not available for UART4 and UART5.
  383. * @param usart_x: select the usart or the uart peripheral.
  384. * this parameter can be one of the following values:
  385. * USART1, USART2 or USART3.
  386. * @param guard_time_val: specifies the guard time (0x00~0xFF).
  387. * @retval none
  388. */
  389. void usart_smartcard_guard_time_set(usart_type* usart_x, uint8_t guard_time_val)
  390. {
  391. usart_x->gdiv_bit.scgt = guard_time_val;
  392. }
  393. /**
  394. * @brief config the irda/smartcard division.
  395. * @note the division are not available for UART4 and UART5.
  396. * @param usart_x: select the usart or the uart peripheral.
  397. * this parameter can be one of the following values:
  398. * USART1, USART2 or USART3.
  399. * @param div_val: specifies the division.
  400. * @retval none
  401. */
  402. void usart_irda_smartcard_division_set(usart_type* usart_x, uint8_t div_val)
  403. {
  404. usart_x->gdiv_bit.isdiv = div_val;
  405. }
  406. /**
  407. * @brief enable or disable the usart smart card mode.
  408. * @note the smart card mode are not available for UART4 and UART5.
  409. * @param usart_x: select the usart or the uart peripheral.
  410. * this parameter can be one of the following values:
  411. * USART1, USART2 or USART3.
  412. * @param new_state: new state of the smart card mode.
  413. * this parameter can be: TRUE or FALSE.
  414. * @retval none
  415. */
  416. void usart_smartcard_mode_enable(usart_type* usart_x, confirm_state new_state)
  417. {
  418. usart_x->ctrl3_bit.scmen = new_state;
  419. }
  420. /**
  421. * @brief enable or disable nack transmission in smartcard mode.
  422. * @note the smart card nack are not available for UART4 and UART5.
  423. * @param usart_x: select the usart or the uart peripheral.
  424. * this parameter can be one of the following values:
  425. * USART1, USART2 or USART3.
  426. * @param new_state: new state of the nack transmission.
  427. * this parameter can be: TRUE or FALSE.
  428. * @retval none
  429. */
  430. void usart_smartcard_nack_set(usart_type* usart_x, confirm_state new_state)
  431. {
  432. usart_x->ctrl3_bit.scnacken = new_state;
  433. }
  434. /**
  435. * @brief enable or disable the usart single line bidirectional half-duplex communication.
  436. * @param usart_x: select the usart or the uart peripheral.
  437. * this parameter can be one of the following values:
  438. * USART1, USART2, USART3, UART4 or UART5.
  439. * @param new_state: new state of the single line half-duplex select.
  440. * this parameter can be: TRUE or FALSE.
  441. * @retval none
  442. */
  443. void usart_single_line_halfduplex_select(usart_type* usart_x, confirm_state new_state)
  444. {
  445. usart_x->ctrl3_bit.slben = new_state;
  446. }
  447. /**
  448. * @brief enable or disable the usart's irda interface.
  449. * @param usart_x: select the usart or the uart peripheral.
  450. * this parameter can be one of the following values:
  451. * USART1, USART2, USART3, UART4 or UART5.
  452. * @param new_state: new state of the irda mode.
  453. * this parameter can be: TRUE or FALSE.
  454. * @retval none
  455. */
  456. void usart_irda_mode_enable(usart_type* usart_x, confirm_state new_state)
  457. {
  458. usart_x->ctrl3_bit.irdaen = new_state;
  459. }
  460. /**
  461. * @brief configure the usart's irda low power.
  462. * @param usart_x: select the usart or the uart peripheral.
  463. * this parameter can be one of the following values:
  464. * USART1, USART2, USART3, UART4 or UART5.
  465. * @param new_state: new state of the irda mode.
  466. * this parameter can be: TRUE or FALSE.
  467. * @retval none
  468. */
  469. void usart_irda_low_power_enable(usart_type* usart_x, confirm_state new_state)
  470. {
  471. usart_x->ctrl3_bit.irdalp = new_state;
  472. }
  473. /**
  474. * @brief configure the usart's hardware flow control.
  475. * @param usart_x: select the usart or the uart peripheral.
  476. * this parameter can be one of the following values:
  477. * USART1, USART2 or USART3
  478. * @param flow_state: specifies the hardware flow control.
  479. * this parameter can be one of the following values:
  480. * - USART_HARDWARE_FLOW_NONE
  481. * - USART_HARDWARE_FLOW_RTS,
  482. * - USART_HARDWARE_FLOW_CTS,
  483. * - USART_HARDWARE_FLOW_RTS_CTS
  484. * @retval none
  485. */
  486. void usart_hardware_flow_control_set(usart_type* usart_x,usart_hardware_flow_control_type flow_state)
  487. {
  488. if(flow_state == USART_HARDWARE_FLOW_NONE)
  489. {
  490. usart_x->ctrl3_bit.rtsen = FALSE;
  491. usart_x->ctrl3_bit.ctsen = FALSE;
  492. }
  493. else if(flow_state == USART_HARDWARE_FLOW_RTS)
  494. {
  495. usart_x->ctrl3_bit.rtsen = TRUE;
  496. usart_x->ctrl3_bit.ctsen = FALSE;
  497. }
  498. else if(flow_state == USART_HARDWARE_FLOW_CTS)
  499. {
  500. usart_x->ctrl3_bit.rtsen = FALSE;
  501. usart_x->ctrl3_bit.ctsen = TRUE;
  502. }
  503. else if(flow_state == USART_HARDWARE_FLOW_RTS_CTS)
  504. {
  505. usart_x->ctrl3_bit.rtsen = TRUE;
  506. usart_x->ctrl3_bit.ctsen = TRUE;
  507. }
  508. }
  509. /**
  510. * @brief check whether the specified usart flag is set or not.
  511. * @param usart_x: select the usart or the uart peripheral.
  512. * this parameter can be one of the following values:
  513. * USART1, USART2, USART3, UART4 or UART5.
  514. * @param flag: specifies the flag to check.
  515. * this parameter can be one of the following values:
  516. * - USART_CTSCF_FLAG: cts change flag (not available for UART4,UART5,USART6,UART7 and UART8)
  517. * - USART_BFF_FLAG: break frame flag
  518. * - USART_TDBE_FLAG: transmit data buffer empty flag
  519. * - USART_TDC_FLAG: transmit data complete flag
  520. * - USART_RDBF_FLAG: receive data buffer full flag
  521. * - USART_IDLEF_FLAG: idle flag
  522. * - USART_ROERR_FLAG: receiver overflow error flag
  523. * - USART_NERR_FLAG: noise error flag
  524. * - USART_FERR_FLAG: framing error flag
  525. * - USART_PERR_FLAG: parity error flag
  526. * @retval the new state of usart_flag (SET or RESET).
  527. */
  528. flag_status usart_flag_get(usart_type* usart_x, uint32_t flag)
  529. {
  530. if(usart_x->sts & flag)
  531. {
  532. return SET;
  533. }
  534. else
  535. {
  536. return RESET;
  537. }
  538. }
  539. /**
  540. * @brief clear the usart's pending flags.
  541. * @param usart_x: select the usart or the uart peripheral.
  542. * this parameter can be one of the following values:
  543. * USART1, USART2, USART3, UART4 or UART5.
  544. * @param flag: specifies the flag to clear.
  545. * this parameter can be any combination of the following values:
  546. * - USART_CTSCF_FLAG: (not available for UART4 and UART5).
  547. * - USART_BFF_FLAG:
  548. * - USART_TDC_FLAG:
  549. * - USART_RDBF_FLAG:
  550. * - USART_PERR_FLAG:
  551. * - USART_FERR_FLAG:
  552. * - USART_NERR_FLAG:
  553. * - USART_ROERR_FLAG:
  554. * - USART_IDLEF_FLAG:
  555. * @note
  556. * - USART_PERR_FLAG, USART_FERR_FLAG, USART_NERR_FLAG, USART_ROERR_FLAG and USART_IDLEF_FLAG are cleared by software
  557. * sequence: a read operation to usart sts register (usart_flag_get())
  558. * followed by a read operation to usart dt register (usart_data_receive()).
  559. * - USART_RDBF_FLAG can be also cleared by a read to the usart dt register(usart_data_receive()).
  560. * - USART_TDC_FLAG can be also cleared by software sequence: a read operation to usart sts register (usart_flag_get())
  561. * followed by a write operation to usart dt register (usart_data_transmit()).
  562. * - USART_TDBE_FLAG is cleared only by a write to the usart dt register(usart_data_transmit()).
  563. * @retval none
  564. */
  565. void usart_flag_clear(usart_type* usart_x, uint32_t flag)
  566. {
  567. if(flag & (USART_PERR_FLAG | USART_FERR_FLAG | USART_NERR_FLAG | USART_ROERR_FLAG | USART_IDLEF_FLAG))
  568. {
  569. UNUSED(usart_x->sts);
  570. UNUSED(usart_x->dt);
  571. }
  572. else
  573. {
  574. usart_x->sts = ~flag;
  575. }
  576. }
  577. /**
  578. * @}
  579. */
  580. #endif
  581. /**
  582. * @}
  583. */
  584. /**
  585. * @}
  586. */