at32f403a_407_usart.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  1. /**
  2. **************************************************************************
  3. * @file at32f403a_407_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 "at32f403a_407_conf.h"
  26. /** @addtogroup AT32F403A_407_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, UART5, USART6, UART7,or UART8.
  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. else if(usart_x == USART3)
  57. {
  58. crm_periph_reset(CRM_USART3_PERIPH_RESET, TRUE);
  59. crm_periph_reset(CRM_USART3_PERIPH_RESET, FALSE);
  60. }
  61. else if(usart_x == UART4)
  62. {
  63. crm_periph_reset(CRM_UART4_PERIPH_RESET, TRUE);
  64. crm_periph_reset(CRM_UART4_PERIPH_RESET, FALSE);
  65. }
  66. else if(usart_x == UART5)
  67. {
  68. crm_periph_reset(CRM_UART5_PERIPH_RESET, TRUE);
  69. crm_periph_reset(CRM_UART5_PERIPH_RESET, FALSE);
  70. }
  71. else if(usart_x == USART6)
  72. {
  73. crm_periph_reset(CRM_USART6_PERIPH_RESET, TRUE);
  74. crm_periph_reset(CRM_USART6_PERIPH_RESET, FALSE);
  75. }
  76. else if(usart_x == UART7)
  77. {
  78. crm_periph_reset(CRM_UART7_PERIPH_RESET, TRUE);
  79. crm_periph_reset(CRM_UART7_PERIPH_RESET, FALSE);
  80. }
  81. #if defined (AT32F403ARx) || defined (AT32F403AVx) || defined (AT32F407Rx) || \
  82. defined (AT32F407Vx)
  83. else if(usart_x == UART8)
  84. {
  85. crm_periph_reset(CRM_UART8_PERIPH_RESET, TRUE);
  86. crm_periph_reset(CRM_UART8_PERIPH_RESET, FALSE);
  87. }
  88. #endif
  89. }
  90. /**
  91. * @brief initialize the usart peripheral according to the specified parameters.
  92. * @param usart_x: select the usart or the uart peripheral.
  93. * this parameter can be one of the following values:
  94. * USART1, USART2, USART3, UART4 ,UART5, USART6, UART7 or UART8.
  95. * @param baud_rate: configure the usart communication baud rate.
  96. * @param data_bit: data bits transmitted or received in a frame
  97. * this parameter can be one of the following values:
  98. * - USART_DATA_8BITS
  99. * - USART_DATA_9BITS.
  100. * note:
  101. * - when parity check is disabled, the data bit width is the actual data bit number.
  102. * - when parity check is enabled, the data bit width is the actual data bit number minus 1, and the MSB bit is replaced with the parity bit.
  103. * @param stop_bit: stop bits transmitted
  104. * this parameter can be one of the following values:
  105. * - USART_STOP_1_BIT
  106. * - USART_STOP_0_5_BIT.
  107. * - USART_STOP_2_BIT
  108. * - USART_STOP_1_5_BIT.
  109. * @retval none
  110. */
  111. 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)
  112. {
  113. crm_clocks_freq_type clocks_freq;
  114. uint32_t apb_clock, temp_val;
  115. crm_clocks_freq_get(&clocks_freq);
  116. if((usart_x == USART1) || (usart_x == USART6) || (usart_x == UART7)
  117. #if defined (AT32F403ARx) || defined (AT32F403AVx) || defined (AT32F407Rx) || \
  118. defined (AT32F407Vx)
  119. || (usart_x == UART8)
  120. #endif
  121. )
  122. {
  123. apb_clock = clocks_freq.apb2_freq;
  124. }
  125. else
  126. {
  127. apb_clock = clocks_freq.apb1_freq;
  128. }
  129. temp_val = (apb_clock * 10 / baud_rate);
  130. if((temp_val % 10) < 5)
  131. {
  132. temp_val = (temp_val / 10);
  133. }
  134. else
  135. {
  136. temp_val = (temp_val / 10) + 1;
  137. }
  138. usart_x->baudr_bit.div = temp_val;
  139. usart_x->ctrl1_bit.dbn = data_bit;
  140. usart_x->ctrl2_bit.stopbn = stop_bit;
  141. }
  142. /**
  143. * @brief usart parity selection config.
  144. * @param usart_x: select the usart or the uart peripheral.
  145. * this parameter can be one of the following values:
  146. * USART1, USART2, USART3, UART4 ,UART5, USART6, UART7 or UART8.
  147. * @param parity: select the none, odd or even parity.
  148. * this parameter can be one of the following values:
  149. * - USART_PARITY_NONE
  150. * - USART_PARITY_EVEN.
  151. * - USART_PARITY_ODD
  152. * @retval none
  153. */
  154. void usart_parity_selection_config(usart_type* usart_x, usart_parity_selection_type parity)
  155. {
  156. if(parity == USART_PARITY_NONE)
  157. {
  158. usart_x->ctrl1_bit.psel = FALSE;
  159. usart_x->ctrl1_bit.pen = FALSE;
  160. }
  161. else if(parity == USART_PARITY_EVEN)
  162. {
  163. usart_x->ctrl1_bit.psel = FALSE;
  164. usart_x->ctrl1_bit.pen = TRUE;
  165. }
  166. else if(parity == USART_PARITY_ODD)
  167. {
  168. usart_x->ctrl1_bit.psel = TRUE;
  169. usart_x->ctrl1_bit.pen = TRUE;
  170. }
  171. }
  172. /**
  173. * @brief enable or disable the specified usart peripheral.
  174. * @param usart_x: select the usart or the uart peripheral.
  175. * this parameter can be one of the following values:
  176. * USART1, USART2, USART3, UART4, UART5, USART6, UART7 or UART8.
  177. * @param new_state: new state of the usart peripheral.
  178. * this parameter can be: TRUE or FALSE.
  179. * @retval none
  180. */
  181. void usart_enable(usart_type* usart_x, confirm_state new_state)
  182. {
  183. usart_x->ctrl1_bit.uen = new_state;
  184. }
  185. /**
  186. * @brief usart transmitter enable.
  187. * @param usart_x: select the usart or the uart peripheral.
  188. * this parameter can be one of the following values:
  189. * USART1, USART2, USART3, UART4 ,UART5, USART6, UART7 or UART8.
  190. * @param new_state: TRUE or FALSE.
  191. * @retval none
  192. */
  193. void usart_transmitter_enable(usart_type* usart_x, confirm_state new_state)
  194. {
  195. usart_x->ctrl1_bit.ten = new_state;
  196. }
  197. /**
  198. * @brief usart receiver enable.
  199. * @param usart_x: select the usart or the uart peripheral.
  200. * this parameter can be one of the following values:
  201. * USART1, USART2, USART3, UART4 ,UART5, USART6, UART7 or UART8.
  202. * @param new_state: TRUE or FALSE.
  203. * @retval none
  204. */
  205. void usart_receiver_enable(usart_type* usart_x, confirm_state new_state)
  206. {
  207. usart_x->ctrl1_bit.ren = new_state;
  208. }
  209. /**
  210. * @brief usart clock config.
  211. * @note clock config are not available for UART4, UART5, UART7 and UART8.
  212. * @param usart_x: select the usart or the uart peripheral.
  213. * this parameter can be one of the following values:
  214. * USART1, USART2, USART3 or USART6.
  215. * @param clk_pol: polarity of the clock output on the ck pin.
  216. * this parameter can be one of the following values:
  217. * - USART_CLOCK_POLARITY_LOW
  218. * - USART_CLOCK_POLARITY_HIGH
  219. * @param clk_pha: phase of the clock output on the ck pin.
  220. * this parameter can be one of the following values:
  221. * - USART_CLOCK_PHASE_1EDGE
  222. * - USART_CLOCK_PHASE_2EDGE
  223. * @param clk_lb: whether the clock pulse of the last data bit transmitted (MSB) is outputted on the ck pin.
  224. * this parameter can be one of the following values:
  225. * - USART_CLOCK_LAST_BIT_NONE
  226. * - USART_CLOCK_LAST_BIT_OUTPUT
  227. * @retval none
  228. */
  229. 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)
  230. {
  231. usart_x->ctrl2_bit.clkpol = clk_pol;
  232. usart_x->ctrl2_bit.clkpha = clk_pha;
  233. usart_x->ctrl2_bit.lbcp = clk_lb;
  234. }
  235. /**
  236. * @brief usart enable the ck pin.
  237. * @note clock enable are not available for UART4, UART5, UART7 and UART8.
  238. * @param usart_x: select the usart or the uart peripheral.
  239. * this parameter can be one of the following values:
  240. * USART1, USART2, USART3 or USART6.
  241. * @param new_state: TRUE or FALSE
  242. * @retval none
  243. */
  244. void usart_clock_enable(usart_type* usart_x, confirm_state new_state)
  245. {
  246. usart_x->ctrl2_bit.clken = new_state;
  247. }
  248. /**
  249. * @brief enable or disable the specified usart interrupts.
  250. * @param usart_x: select the usart or the uart peripheral.
  251. * this parameter can be one of the following values:
  252. * USART1, USART2, USART3, UART4, UART5, USART6, UART7 or UART8.
  253. * @param usart_int: specifies the USART interrupt sources to be enabled or disabled.
  254. * this parameter can be one of the following values:
  255. * - USART_IDLE_INT: idle interrupt
  256. * - USART_RDBF_INT: rdbf interrupt
  257. * - USART_TDC_INT: tdc interrupt
  258. * - USART_TDBE_INT: tdbe interrupt
  259. * - USART_PERR_INT: perr interrupt
  260. * - USART_BF_INT: break frame interrupt
  261. * - USART_ERR_INT: err interrupt
  262. * - USART_CTSCF_INT: ctscf interrupt
  263. * @param new_state: new state of the specified usart interrupts.
  264. * this parameter can be: TRUE or FALSE.
  265. * @retval none
  266. */
  267. void usart_interrupt_enable(usart_type* usart_x, uint32_t usart_int, confirm_state new_state)
  268. {
  269. if(new_state == TRUE)
  270. PERIPH_REG((uint32_t)usart_x, usart_int) |= PERIPH_REG_BIT(usart_int);
  271. else
  272. PERIPH_REG((uint32_t)usart_x, usart_int) &= ~PERIPH_REG_BIT(usart_int);
  273. }
  274. /**
  275. * @brief enable or disable the usart's dma transmitter interface.
  276. * @param usart_x: select the usart or the uart peripheral.
  277. * this parameter can be one of the following values:
  278. * USART1, USART2, USART3, UART4, UART5, USART6, UART7 or UART8.
  279. * @param new_state: new state of the dma request sources.
  280. * this parameter can be: TRUE or FALSE.
  281. * @retval none
  282. */
  283. void usart_dma_transmitter_enable(usart_type* usart_x, confirm_state new_state)
  284. {
  285. usart_x->ctrl3_bit.dmaten = new_state;
  286. }
  287. /**
  288. * @brief enable or disable the usart's dma receiver interface.
  289. * @param usart_x: select the usart or the uart peripheral.
  290. * this parameter can be one of the following values:
  291. * USART1, USART2, USART3, UART4, UART5, USART6, UART7 or UART8.
  292. * @param new_state: new state of the dma request sources.
  293. * this parameter can be: TRUE or FALSE.
  294. * @retval none
  295. */
  296. void usart_dma_receiver_enable(usart_type* usart_x, confirm_state new_state)
  297. {
  298. usart_x->ctrl3_bit.dmaren = new_state;
  299. }
  300. /**
  301. * @brief set the wakeup id of the usart.
  302. * @param usart_x: select the usart or the uart peripheral.
  303. * this parameter can be one of the following values:
  304. * USART1, USART2, USART3, UART4, UART5, USART6, UART7 or UART8.
  305. * @param usart_id: the matching id(0x0~0xF).
  306. * @retval none
  307. */
  308. void usart_wakeup_id_set(usart_type* usart_x, uint8_t usart_id)
  309. {
  310. usart_x->ctrl2_bit.id = usart_id;
  311. }
  312. /**
  313. * @brief select the usart wakeup method in multi-processor communication.
  314. * @param usart_x: select the usart or the uart peripheral.
  315. * this parameter can be one of the following values:
  316. * USART1, USART2, USART3, UART4, UART5, USART6, UART7 or UART8.
  317. * @param wakeup_mode: determines the way to wake up usart method.
  318. * this parameter can be one of the following values:
  319. * - USART_WAKEUP_BY_IDLE_FRAME
  320. * - USART_WAKEUP_BY_MATCHING_ID
  321. * @retval none
  322. */
  323. void usart_wakeup_mode_set(usart_type* usart_x, usart_wakeup_mode_type wakeup_mode)
  324. {
  325. usart_x->ctrl1_bit.wum = wakeup_mode;
  326. }
  327. /**
  328. * @brief config the usart in mute mode in multi-processor communication.
  329. * @param usart_x: select the usart or the uart peripheral.
  330. * this parameter can be one of the following values:
  331. * USART1, USART2, USART3, UART4, UART5, USART6, UART7 or UART8.
  332. * @param new_state: new state of the usart mute mode.
  333. * this parameter can be: TRUE or FALSE.
  334. * @retval none
  335. */
  336. void usart_receiver_mute_enable(usart_type* usart_x, confirm_state new_state)
  337. {
  338. usart_x->ctrl1_bit.rm = new_state;
  339. }
  340. /**
  341. * @brief set the usart break frame bit num.
  342. * @param usart_x: select the usart or the uart peripheral.
  343. * this parameter can be one of the following values:
  344. * USART1, USART2, USART3, UART4, UART5, USART6, UART7 or UART8.
  345. * @param break_bit: specifies the break bit num.
  346. * this parameter can be one of the following values:
  347. * - USART_BREAK_10BITS
  348. * - USART_BREAK_11BITS
  349. * @retval none
  350. */
  351. void usart_break_bit_num_set(usart_type* usart_x, usart_break_bit_num_type break_bit)
  352. {
  353. usart_x->ctrl2_bit.bfbn = break_bit;
  354. }
  355. /**
  356. * @brief enable or disable the usart lin mode.
  357. * @param usart_x: select the usart or the uart peripheral.
  358. * this parameter can be one of the following values:
  359. * USART1, USART2, USART3, UART4, UART5, USART6, UART7 or UART8.
  360. * @param new_state: new state of the usart lin mode.
  361. * this parameter can be: TRUE or FALSE.
  362. * @retval none
  363. */
  364. void usart_lin_mode_enable(usart_type* usart_x, confirm_state new_state)
  365. {
  366. usart_x->ctrl2_bit.linen = new_state;
  367. }
  368. /**
  369. * @brief transmit single data through the usart peripheral.
  370. * @param usart_x: select the usart or the uart peripheral.
  371. * this parameter can be one of the following values:
  372. * USART1, USART2, USART3, UART4, UART5, USART6, UART7 or UART8.
  373. * @param data: the data to transmit.
  374. * @retval none
  375. */
  376. void usart_data_transmit(usart_type* usart_x, uint16_t data)
  377. {
  378. usart_x->dt = (data & 0x01FF);
  379. }
  380. /**
  381. * @brief return the most recent received data by the usart peripheral.
  382. * @param usart_x: select the usart or the uart peripheral.
  383. * this parameter can be one of the following values:
  384. * USART1, USART2, USART3, UART4, UART5, USART6, UART7 or UART8.
  385. * @retval the received data.
  386. */
  387. uint16_t usart_data_receive(usart_type* usart_x)
  388. {
  389. return (uint16_t)(usart_x->dt);
  390. }
  391. /**
  392. * @brief transmit break characters.
  393. * @param usart_x: select the usart or the uart peripheral.
  394. * this parameter can be one of the following values:
  395. * USART1, USART2, USART3, UART4, UART5, USART6, UART7 or UART8.
  396. * @retval none
  397. */
  398. void usart_break_send(usart_type* usart_x)
  399. {
  400. usart_x->ctrl1_bit.sbf = TRUE;
  401. }
  402. /**
  403. * @brief config the specified usart smartcard guard time.
  404. * @note The guard time bits are not available for UART4, UART5, UART7 or UART8.
  405. * @param usart_x: select the usart or the uart peripheral.
  406. * this parameter can be one of the following values:
  407. * USART1, USART2, USART3 or USART6.
  408. * @param guard_time_val: specifies the guard time (0x00~0xFF).
  409. * @retval none
  410. */
  411. void usart_smartcard_guard_time_set(usart_type* usart_x, uint8_t guard_time_val)
  412. {
  413. usart_x->gdiv_bit.scgt = guard_time_val;
  414. }
  415. /**
  416. * @brief config the irda/smartcard division.
  417. * @note the division are not available for UART4, UART5, UART7 or UART8.
  418. * @param usart_x: select the usart or the uart peripheral.
  419. * this parameter can be one of the following values:
  420. * USART1, USART2, USART3 or USART6.
  421. * @param div_val: specifies the division.
  422. * @retval none
  423. */
  424. void usart_irda_smartcard_division_set(usart_type* usart_x, uint8_t div_val)
  425. {
  426. usart_x->gdiv_bit.isdiv = div_val;
  427. }
  428. /**
  429. * @brief enable or disable the usart smart card mode.
  430. * @note the smart card mode are not available for UART4, UART5, UART7 or UART8.
  431. * @param usart_x: select the usart or the uart peripheral.
  432. * this parameter can be one of the following values:
  433. * USART1, USART2, USART3 or USART6.
  434. * @param new_state: new state of the smart card mode.
  435. * this parameter can be: TRUE or FALSE.
  436. * @retval none
  437. */
  438. void usart_smartcard_mode_enable(usart_type* usart_x, confirm_state new_state)
  439. {
  440. usart_x->ctrl3_bit.scmen = new_state;
  441. }
  442. /**
  443. * @brief enable or disable nack transmission in smartcard mode.
  444. * @note the smart card nack are not available for UART4, UART5, UART7 or UART8.
  445. * @param usart_x: select the usart or the uart peripheral.
  446. * this parameter can be one of the following values:
  447. * USART1, USART2, USART3 or USART6.
  448. * @param new_state: new state of the nack transmission.
  449. * this parameter can be: TRUE or FALSE.
  450. * @retval none
  451. */
  452. void usart_smartcard_nack_set(usart_type* usart_x, confirm_state new_state)
  453. {
  454. usart_x->ctrl3_bit.scnacken = new_state;
  455. }
  456. /**
  457. * @brief enable or disable the usart single line bidirectional half-duplex communication.
  458. * @param usart_x: select the usart or the uart peripheral.
  459. * this parameter can be one of the following values:
  460. * USART1, USART2, USART3, UART4, UART5, USART6, UART7 or UART8.
  461. * @param new_state: new state of the single line half-duplex select.
  462. * this parameter can be: TRUE or FALSE.
  463. * @retval none
  464. */
  465. void usart_single_line_halfduplex_select(usart_type* usart_x, confirm_state new_state)
  466. {
  467. usart_x->ctrl3_bit.slben = new_state;
  468. }
  469. /**
  470. * @brief enable or disable the usart's irda interface.
  471. * @param usart_x: select the usart or the uart peripheral.
  472. * this parameter can be one of the following values:
  473. * USART1, USART2, USART3, UART4, UART5, USART6, UART7 or UART8.
  474. * @param new_state: new state of the irda mode.
  475. * this parameter can be: TRUE or FALSE.
  476. * @retval none
  477. */
  478. void usart_irda_mode_enable(usart_type* usart_x, confirm_state new_state)
  479. {
  480. usart_x->ctrl3_bit.irdaen = new_state;
  481. }
  482. /**
  483. * @brief configure the usart's irda low power.
  484. * @param usart_x: select the usart or the uart peripheral.
  485. * this parameter can be one of the following values:
  486. * USART1, USART2, USART3, UART4, UART5, USART6, UART7 or UART8.
  487. * @param new_state: new state of the irda mode.
  488. * this parameter can be: TRUE or FALSE.
  489. * @retval none
  490. */
  491. void usart_irda_low_power_enable(usart_type* usart_x, confirm_state new_state)
  492. {
  493. usart_x->ctrl3_bit.irdalp = new_state;
  494. }
  495. /**
  496. * @brief configure the usart's hardware flow control.
  497. * @param usart_x: select the usart or the uart peripheral.
  498. * this parameter can be one of the following values:
  499. * USART1, USART2, USART3
  500. * @param flow_state: specifies the hardware flow control.
  501. * this parameter can be one of the following values:
  502. * - USART_HARDWARE_FLOW_NONE
  503. * - USART_HARDWARE_FLOW_RTS,
  504. * - USART_HARDWARE_FLOW_CTS,
  505. * - USART_HARDWARE_FLOW_RTS_CTS
  506. * @retval none
  507. */
  508. void usart_hardware_flow_control_set(usart_type* usart_x,usart_hardware_flow_control_type flow_state)
  509. {
  510. if(flow_state == USART_HARDWARE_FLOW_NONE)
  511. {
  512. usart_x->ctrl3_bit.rtsen = FALSE;
  513. usart_x->ctrl3_bit.ctsen = FALSE;
  514. }
  515. else if(flow_state == USART_HARDWARE_FLOW_RTS)
  516. {
  517. usart_x->ctrl3_bit.rtsen = TRUE;
  518. usart_x->ctrl3_bit.ctsen = FALSE;
  519. }
  520. else if(flow_state == USART_HARDWARE_FLOW_CTS)
  521. {
  522. usart_x->ctrl3_bit.rtsen = FALSE;
  523. usart_x->ctrl3_bit.ctsen = TRUE;
  524. }
  525. else if(flow_state == USART_HARDWARE_FLOW_RTS_CTS)
  526. {
  527. usart_x->ctrl3_bit.rtsen = TRUE;
  528. usart_x->ctrl3_bit.ctsen = TRUE;
  529. }
  530. }
  531. /**
  532. * @brief check whether the specified usart flag is set or not.
  533. * @param usart_x: select the usart or the uart peripheral.
  534. * this parameter can be one of the following values:
  535. * USART1, USART2, USART3, UART4, UART5, USART6, UART7 or UART8.
  536. * @param flag: specifies the flag to check.
  537. * this parameter can be one of the following values:
  538. * - USART_CTSCF_FLAG: cts change flag (not available for UART4,UART5,USART6,UART7 and UART8)
  539. * - USART_BFF_FLAG: break frame flag
  540. * - USART_TDBE_FLAG: transmit data buffer empty flag
  541. * - USART_TDC_FLAG: transmit data complete flag
  542. * - USART_RDBF_FLAG: receive data buffer full flag
  543. * - USART_IDLEF_FLAG: idle flag
  544. * - USART_ROERR_FLAG: receiver overflow error flag
  545. * - USART_NERR_FLAG: noise error flag
  546. * - USART_FERR_FLAG: framing error flag
  547. * - USART_PERR_FLAG: parity error flag
  548. * @retval the new state of usart_flag (SET or RESET).
  549. */
  550. flag_status usart_flag_get(usart_type* usart_x, uint32_t flag)
  551. {
  552. if(usart_x->sts & flag)
  553. {
  554. return SET;
  555. }
  556. else
  557. {
  558. return RESET;
  559. }
  560. }
  561. /**
  562. * @brief check whether the specified usart interrupt flag is set or not.
  563. * @param usart_x: select the usart or the uart peripheral.
  564. * this parameter can be one of the following values:
  565. * USART1, USART2, USART3, UART4, UART5, USART6, UART7 or UART8.
  566. * @param flag: specifies the flag to check.
  567. * this parameter can be one of the following values:
  568. * - USART_CTSCF_FLAG: cts change flag (not available for UART4,UART5)
  569. * - USART_BFF_FLAG: break frame flag
  570. * - USART_TDBE_FLAG: transmit data buffer empty flag
  571. * - USART_TDC_FLAG: transmit data complete flag
  572. * - USART_RDBF_FLAG: receive data buffer full flag
  573. * - USART_IDLEF_FLAG: idle flag
  574. * - USART_ROERR_FLAG: receiver overflow error flag
  575. * - USART_NERR_FLAG: noise error flag
  576. * - USART_FERR_FLAG: framing error flag
  577. * - USART_PERR_FLAG: parity error flag
  578. * @retval the new state of usart_flag (SET or RESET).
  579. */
  580. flag_status usart_interrupt_flag_get(usart_type* usart_x, uint32_t flag)
  581. {
  582. flag_status int_status = RESET;
  583. switch(flag)
  584. {
  585. case USART_CTSCF_FLAG:
  586. int_status = (flag_status)usart_x->ctrl3_bit.ctscfien;
  587. break;
  588. case USART_BFF_FLAG:
  589. int_status = (flag_status)usart_x->ctrl2_bit.bfien;
  590. break;
  591. case USART_TDBE_FLAG:
  592. int_status = (flag_status)usart_x->ctrl1_bit.tdbeien;
  593. break;
  594. case USART_TDC_FLAG:
  595. int_status = (flag_status)usart_x->ctrl1_bit.tdcien;
  596. break;
  597. case USART_RDBF_FLAG:
  598. int_status = (flag_status)usart_x->ctrl1_bit.rdbfien;
  599. break;
  600. case USART_ROERR_FLAG:
  601. int_status = (flag_status)(usart_x->ctrl1_bit.rdbfien || usart_x->ctrl3_bit.errien);
  602. break;
  603. case USART_IDLEF_FLAG:
  604. int_status = (flag_status)usart_x->ctrl1_bit.idleien;
  605. break;
  606. case USART_NERR_FLAG:
  607. case USART_FERR_FLAG:
  608. int_status = (flag_status)usart_x->ctrl3_bit.errien;
  609. break;
  610. case USART_PERR_FLAG:
  611. int_status = (flag_status)usart_x->ctrl1_bit.perrien;
  612. break;
  613. default:
  614. int_status = RESET;
  615. break;
  616. }
  617. if(int_status != SET)
  618. {
  619. return RESET;
  620. }
  621. if(usart_x->sts & flag)
  622. {
  623. return SET;
  624. }
  625. else
  626. {
  627. return RESET;
  628. }
  629. }
  630. /**
  631. * @brief clear the usart's pending flags.
  632. * @param usart_x: select the usart or the uart peripheral.
  633. * this parameter can be one of the following values:
  634. * USART1, USART2, USART3, UART4, UART5, USART6, UART7 or UART8.
  635. * @param flag: specifies the flag to clear.
  636. * this parameter can be any combination of the following values:
  637. * - USART_CTSCF_FLAG: (not available for UART4,UART5,USART6,UART7 and UART8).
  638. * - USART_BFF_FLAG:
  639. * - USART_TDC_FLAG:
  640. * - USART_RDBF_FLAG:
  641. * - USART_PERR_FLAG:
  642. * - USART_FERR_FLAG:
  643. * - USART_NERR_FLAG:
  644. * - USART_ROERR_FLAG:
  645. * - USART_IDLEF_FLAG:
  646. * @note
  647. * - USART_PERR_FLAG, USART_FERR_FLAG, USART_NERR_FLAG, USART_ROERR_FLAG and USART_IDLEF_FLAG are cleared by software
  648. * sequence: a read operation to usart sts register (usart_flag_get())
  649. * followed by a read operation to usart dt register (usart_data_receive()).
  650. * - USART_RDBF_FLAG can be also cleared by a read to the usart dt register(usart_data_receive()).
  651. * - USART_TDC_FLAG can be also cleared by software sequence: a read operation to usart sts register (usart_flag_get())
  652. * followed by a write operation to usart dt register (usart_data_transmit()).
  653. * - USART_TDBE_FLAG is cleared only by a write to the usart dt register(usart_data_transmit()).
  654. * @retval none
  655. */
  656. void usart_flag_clear(usart_type* usart_x, uint32_t flag)
  657. {
  658. if(flag & (USART_PERR_FLAG | USART_FERR_FLAG | USART_NERR_FLAG | USART_ROERR_FLAG | USART_IDLEF_FLAG))
  659. {
  660. UNUSED(usart_x->sts);
  661. UNUSED(usart_x->dt);
  662. }
  663. else
  664. {
  665. usart_x->sts = ~flag;
  666. }
  667. }
  668. /**
  669. * @}
  670. */
  671. #endif
  672. /**
  673. * @}
  674. */
  675. /**
  676. * @}
  677. */