gd32f10x_usart.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  1. /*!
  2. \file gd32f10x_usart.c
  3. \brief USART driver
  4. \version 2014-12-26, V1.0.0, firmware for GD32F10x
  5. \version 2017-06-20, V2.0.1, firmware for GD32F10x
  6. \version 2018-07-31, V2.1.0, firmware for GD32F10x
  7. \version 2020-09-30, V2.2.0, firmware for GD32F10x
  8. */
  9. /*
  10. Copyright (c) 2020, GigaDevice Semiconductor Inc.
  11. Redistribution and use in source and binary forms, with or without modification,
  12. are permitted provided that the following conditions are met:
  13. 1. Redistributions of source code must retain the above copyright notice, this
  14. list of conditions and the following disclaimer.
  15. 2. Redistributions in binary form must reproduce the above copyright notice,
  16. this list of conditions and the following disclaimer in the documentation
  17. and/or other materials provided with the distribution.
  18. 3. Neither the name of the copyright holder nor the names of its contributors
  19. may be used to endorse or promote products derived from this software without
  20. specific prior written permission.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  22. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  23. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  24. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  25. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  26. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  28. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  30. OF SUCH DAMAGE.
  31. */
  32. #include "gd32f10x_usart.h"
  33. /*!
  34. \brief reset USART/UART
  35. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  36. \param[out] none
  37. \retval none
  38. */
  39. void usart_deinit(uint32_t usart_periph)
  40. {
  41. switch(usart_periph){
  42. case USART0:
  43. /* reset USART0 */
  44. rcu_periph_reset_enable(RCU_USART0RST);
  45. rcu_periph_reset_disable(RCU_USART0RST);
  46. break;
  47. case USART1:
  48. /* reset USART1 */
  49. rcu_periph_reset_enable(RCU_USART1RST);
  50. rcu_periph_reset_disable(RCU_USART1RST);
  51. break;
  52. case USART2:
  53. /* reset USART2 */
  54. rcu_periph_reset_enable(RCU_USART2RST);
  55. rcu_periph_reset_disable(RCU_USART2RST);
  56. break;
  57. case UART3:
  58. /* reset UART3 */
  59. rcu_periph_reset_enable(RCU_UART3RST);
  60. rcu_periph_reset_disable(RCU_UART3RST);
  61. break;
  62. case UART4:
  63. /* reset UART4 */
  64. rcu_periph_reset_enable(RCU_UART4RST);
  65. rcu_periph_reset_disable(RCU_UART4RST);
  66. break;
  67. default:
  68. break;
  69. }
  70. }
  71. /*!
  72. \brief configure USART baud rate value
  73. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  74. \param[in] baudval: baud rate value
  75. \param[out] none
  76. \retval none
  77. */
  78. void usart_baudrate_set(uint32_t usart_periph, uint32_t baudval)
  79. {
  80. uint32_t uclk=0U, intdiv=0U, fradiv=0U, udiv=0U;
  81. switch(usart_periph){
  82. /* get clock frequency */
  83. case USART0:
  84. /* get USART0 clock */
  85. uclk=rcu_clock_freq_get(CK_APB2);
  86. break;
  87. case USART1:
  88. /* get USART1 clock */
  89. uclk=rcu_clock_freq_get(CK_APB1);
  90. break;
  91. case USART2:
  92. /* get USART2 clock */
  93. uclk=rcu_clock_freq_get(CK_APB1);
  94. break;
  95. case UART3:
  96. /* get UART3 clock */
  97. uclk=rcu_clock_freq_get(CK_APB1);
  98. break;
  99. case UART4:
  100. /* get UART4 clock */
  101. uclk=rcu_clock_freq_get(CK_APB1);
  102. break;
  103. default:
  104. break;
  105. }
  106. /* oversampling by 16, configure the value of USART_BAUD */
  107. udiv = (uclk+baudval/2U)/baudval;
  108. intdiv = udiv & (0x0000fff0U);
  109. fradiv = udiv & (0x0000000fU);
  110. USART_BAUD(usart_periph) = ((USART_BAUD_FRADIV | USART_BAUD_INTDIV) & (intdiv | fradiv));
  111. }
  112. /*!
  113. \brief configure USART parity
  114. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  115. \param[in] paritycfg: configure USART parity
  116. only one parameter can be selected which is shown as below:
  117. \arg USART_PM_NONE: no parity
  118. \arg USART_PM_ODD: odd parity
  119. \arg USART_PM_EVEN: even parity
  120. \param[out] none
  121. \retval none
  122. */
  123. void usart_parity_config(uint32_t usart_periph, uint32_t paritycfg)
  124. {
  125. /* clear USART_CTL0 PM,PCEN bits */
  126. USART_CTL0(usart_periph) &= ~(USART_CTL0_PM | USART_CTL0_PCEN);
  127. /* configure USART parity mode */
  128. USART_CTL0(usart_periph) |= paritycfg ;
  129. }
  130. /*!
  131. \brief configure USART word length
  132. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  133. \param[in] wlen: USART word length configure
  134. only one parameter can be selected which is shown as below:
  135. \arg USART_WL_8BIT: 8 bits
  136. \arg USART_WL_9BIT: 9 bits
  137. \param[out] none
  138. \retval none
  139. */
  140. void usart_word_length_set(uint32_t usart_periph, uint32_t wlen)
  141. {
  142. /* clear USART_CTL0 WL bit */
  143. USART_CTL0(usart_periph) &= ~USART_CTL0_WL;
  144. /* configure USART word length */
  145. USART_CTL0(usart_periph) |= wlen;
  146. }
  147. /*!
  148. \brief configure USART stop bit length
  149. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  150. \param[in] stblen: USART stop bit configure
  151. only one parameter can be selected which is shown as below:
  152. \arg USART_STB_1BIT: 1 bit
  153. \arg USART_STB_0_5BIT: 0.5 bit, not available for UARTx(x=3,4)
  154. \arg USART_STB_2BIT: 2 bits
  155. \arg USART_STB_1_5BIT: 1.5 bits, not available for UARTx(x=3,4)
  156. \param[out] none
  157. \retval none
  158. */
  159. void usart_stop_bit_set(uint32_t usart_periph, uint32_t stblen)
  160. {
  161. /* clear USART_CTL1 STB bits */
  162. USART_CTL1(usart_periph) &= ~USART_CTL1_STB;
  163. /* configure USART stop bits */
  164. USART_CTL1(usart_periph) |= stblen;
  165. }
  166. /*!
  167. \brief enable USART
  168. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  169. \param[out] none
  170. \retval none
  171. */
  172. void usart_enable(uint32_t usart_periph)
  173. {
  174. USART_CTL0(usart_periph) |= USART_CTL0_UEN;
  175. }
  176. /*!
  177. \brief disable USART
  178. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  179. \param[out] none
  180. \retval none
  181. */
  182. void usart_disable(uint32_t usart_periph)
  183. {
  184. USART_CTL0(usart_periph) &= ~(USART_CTL0_UEN);
  185. }
  186. /*!
  187. \brief configure USART transmitter
  188. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  189. \param[in] txconfig: enable or disable USART transmitter
  190. only one parameter can be selected which is shown as below:
  191. \arg USART_TRANSMIT_ENABLE: enable USART transmission
  192. \arg USART_TRANSMIT_DISABLE: disable USART transmission
  193. \param[out] none
  194. \retval none
  195. */
  196. void usart_transmit_config(uint32_t usart_periph, uint32_t txconfig)
  197. {
  198. uint32_t ctl = 0U;
  199. ctl = USART_CTL0(usart_periph);
  200. ctl &= ~USART_CTL0_TEN;
  201. ctl |= txconfig;
  202. /* configure transfer mode */
  203. USART_CTL0(usart_periph) = ctl;
  204. }
  205. /*!
  206. \brief configure USART receiver
  207. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  208. \param[in] rxconfig: enable or disable USART receiver
  209. only one parameter can be selected which is shown as below:
  210. \arg USART_RECEIVE_ENABLE: enable USART reception
  211. \arg USART_RECEIVE_DISABLE: disable USART reception
  212. \param[out] none
  213. \retval none
  214. */
  215. void usart_receive_config(uint32_t usart_periph, uint32_t rxconfig)
  216. {
  217. uint32_t ctl = 0U;
  218. ctl = USART_CTL0(usart_periph);
  219. ctl &= ~USART_CTL0_REN;
  220. ctl |= rxconfig;
  221. /* configure receiver mode */
  222. USART_CTL0(usart_periph) = ctl;
  223. }
  224. /*!
  225. \brief USART transmit data function
  226. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  227. \param[in] data: data of transmission
  228. \param[out] none
  229. \retval none
  230. */
  231. void usart_data_transmit(uint32_t usart_periph, uint16_t data)
  232. {
  233. USART_DATA(usart_periph) = USART_DATA_DATA & data;
  234. }
  235. /*!
  236. \brief USART receive data function
  237. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  238. \param[out] none
  239. \retval data of received
  240. */
  241. uint16_t usart_data_receive(uint32_t usart_periph)
  242. {
  243. return (uint16_t)(GET_BITS(USART_DATA(usart_periph), 0U, 8U));
  244. }
  245. /*!
  246. \brief configure the address of the USART in wake up by address match mode
  247. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  248. \param[in] addr: address of USART/UART
  249. \param[out] none
  250. \retval none
  251. */
  252. void usart_address_config(uint32_t usart_periph, uint8_t addr)
  253. {
  254. USART_CTL1(usart_periph) &= ~(USART_CTL1_ADDR);
  255. USART_CTL1(usart_periph) |= (USART_CTL1_ADDR & addr);
  256. }
  257. /*!
  258. \brief receiver in mute mode
  259. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  260. \param[out] none
  261. \retval none
  262. */
  263. void usart_mute_mode_enable(uint32_t usart_periph)
  264. {
  265. USART_CTL0(usart_periph) |= USART_CTL0_RWU;
  266. }
  267. /*!
  268. \brief receiver in active mode
  269. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  270. \param[out] none
  271. \retval none
  272. */
  273. void usart_mute_mode_disable(uint32_t usart_periph)
  274. {
  275. USART_CTL0(usart_periph) &= ~(USART_CTL0_RWU);
  276. }
  277. /*!
  278. \brief configure wakeup method in mute mode
  279. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  280. \param[in] wmethod: two methods be used to enter or exit the mute mode
  281. only one parameter can be selected which is shown as below:
  282. \arg USART_WM_IDLE: idle line
  283. \arg USART_WM_ADDR: address mask
  284. \param[out] none
  285. \retval none
  286. */
  287. void usart_mute_mode_wakeup_config(uint32_t usart_periph, uint32_t wmethod)
  288. {
  289. USART_CTL0(usart_periph) &= ~(USART_CTL0_WM);
  290. USART_CTL0(usart_periph) |= wmethod;
  291. }
  292. /*!
  293. \brief enable LIN mode
  294. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  295. \param[out] none
  296. \retval none
  297. */
  298. void usart_lin_mode_enable(uint32_t usart_periph)
  299. {
  300. USART_CTL1(usart_periph) |= USART_CTL1_LMEN;
  301. }
  302. /*!
  303. \brief disable LIN mode
  304. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  305. \param[out] none
  306. \retval none
  307. */
  308. void usart_lin_mode_disable(uint32_t usart_periph)
  309. {
  310. USART_CTL1(usart_periph) &= ~(USART_CTL1_LMEN);
  311. }
  312. /*!
  313. \brief configure lin break frame length
  314. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  315. \param[in] lblen: lin break frame length
  316. only one parameter can be selected which is shown as below:
  317. \arg USART_LBLEN_10B: 10 bits
  318. \arg USART_LBLEN_11B: 11 bits
  319. \param[out] none
  320. \retval none
  321. */
  322. void usart_lin_break_detection_length_config(uint32_t usart_periph, uint32_t lblen)
  323. {
  324. USART_CTL1(usart_periph) &= ~(USART_CTL1_LBLEN);
  325. USART_CTL1(usart_periph) |= (USART_CTL1_LBLEN & lblen);
  326. }
  327. /*!
  328. \brief send break frame
  329. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  330. \param[out] none
  331. \retval none
  332. */
  333. void usart_send_break(uint32_t usart_periph)
  334. {
  335. USART_CTL0(usart_periph) |= USART_CTL0_SBKCMD;
  336. }
  337. /*!
  338. \brief enable half duplex mode
  339. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  340. \param[out] none
  341. \retval none
  342. */
  343. void usart_halfduplex_enable(uint32_t usart_periph)
  344. {
  345. USART_CTL2(usart_periph) |= USART_CTL2_HDEN;
  346. }
  347. /*!
  348. \brief disable half duplex mode
  349. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  350. \param[out] none
  351. \retval none
  352. */
  353. void usart_halfduplex_disable(uint32_t usart_periph)
  354. {
  355. USART_CTL2(usart_periph) &= ~(USART_CTL2_HDEN);
  356. }
  357. /*!
  358. \brief enable CK pin in synchronous mode
  359. \param[in] usart_periph: USARTx(x=0,1,2)
  360. \param[out] none
  361. \retval none
  362. */
  363. void usart_synchronous_clock_enable(uint32_t usart_periph)
  364. {
  365. USART_CTL1(usart_periph) |= USART_CTL1_CKEN;
  366. }
  367. /*!
  368. \brief disable CK pin in synchronous mode
  369. \param[in] usart_periph: USARTx(x=0,1,2)
  370. \param[out] none
  371. \retval none
  372. */
  373. void usart_synchronous_clock_disable(uint32_t usart_periph)
  374. {
  375. USART_CTL1(usart_periph) &= ~(USART_CTL1_CKEN);
  376. }
  377. /*!
  378. \brief configure USART synchronous mode parameters
  379. \param[in] usart_periph: USARTx(x=0,1,2)
  380. \param[in] clen: CK length
  381. only one parameter can be selected which is shown as below:
  382. \arg USART_CLEN_NONE: there are 7 CK pulses for an 8 bit frame and 8 CK pulses for a 9 bit frame
  383. \arg USART_CLEN_EN: there are 8 CK pulses for an 8 bit frame and 9 CK pulses for a 9 bit frame
  384. \param[in] cph: clock phase
  385. only one parameter can be selected which is shown as below:
  386. \arg USART_CPH_1CK: first clock transition is the first data capture edge
  387. \arg USART_CPH_2CK: second clock transition is the first data capture edge
  388. \param[in] cpl: clock polarity
  389. only one parameter can be selected which is shown as below:
  390. \arg USART_CPL_LOW: steady low value on CK pin
  391. \arg USART_CPL_HIGH: steady high value on CK pin
  392. \param[out] none
  393. \retval none
  394. */
  395. void usart_synchronous_clock_config(uint32_t usart_periph, uint32_t clen, uint32_t cph, uint32_t cpl)
  396. {
  397. uint32_t ctl = 0U;
  398. /* read USART_CTL1 register */
  399. ctl = USART_CTL1(usart_periph);
  400. ctl &= ~(USART_CTL1_CLEN | USART_CTL1_CPH | USART_CTL1_CPL);
  401. /* set CK length, CK phase, CK polarity */
  402. ctl |= (USART_CTL1_CLEN & clen) | (USART_CTL1_CPH & cph) | (USART_CTL1_CPL & cpl);
  403. USART_CTL1(usart_periph) = ctl;
  404. }
  405. /*!
  406. \brief configure guard time value in smartcard mode
  407. \param[in] usart_periph: USARTx(x=0,1,2)
  408. \param[in] gaut: guard time value
  409. \param[out] none
  410. \retval none
  411. */
  412. void usart_guard_time_config(uint32_t usart_periph,uint32_t gaut)
  413. {
  414. USART_GP(usart_periph) &= ~(USART_GP_GUAT);
  415. USART_GP(usart_periph) |= (USART_GP_GUAT & ((gaut)<<8));
  416. }
  417. /*!
  418. \brief enable smartcard mode
  419. \param[in] usart_periph: USARTx(x=0,1,2)
  420. \param[out] none
  421. \retval none
  422. */
  423. void usart_smartcard_mode_enable(uint32_t usart_periph)
  424. {
  425. USART_CTL2(usart_periph) |= USART_CTL2_SCEN;
  426. }
  427. /*!
  428. \brief disable smartcard mode
  429. \param[in] usart_periph: USARTx(x=0,1,2)
  430. \param[out] none
  431. \retval none
  432. */
  433. void usart_smartcard_mode_disable(uint32_t usart_periph)
  434. {
  435. USART_CTL2(usart_periph) &= ~(USART_CTL2_SCEN);
  436. }
  437. /*!
  438. \brief enable NACK in smartcard mode
  439. \param[in] usart_periph: USARTx(x=0,1,2)
  440. \param[out] none
  441. \retval none
  442. */
  443. void usart_smartcard_mode_nack_enable(uint32_t usart_periph)
  444. {
  445. USART_CTL2(usart_periph) |= USART_CTL2_NKEN;
  446. }
  447. /*!
  448. \brief disable NACK in smartcard mode
  449. \param[in] usart_periph: USARTx(x=0,1,2)
  450. \param[out] none
  451. \retval none
  452. */
  453. void usart_smartcard_mode_nack_disable(uint32_t usart_periph)
  454. {
  455. USART_CTL2(usart_periph) &= ~(USART_CTL2_NKEN);
  456. }
  457. /*!
  458. \brief enable IrDA mode
  459. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  460. \param[out] none
  461. \retval none
  462. */
  463. void usart_irda_mode_enable(uint32_t usart_periph)
  464. {
  465. USART_CTL2(usart_periph) |= USART_CTL2_IREN;
  466. }
  467. /*!
  468. \brief disable IrDA mode
  469. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  470. \param[out] none
  471. \retval none
  472. */
  473. void usart_irda_mode_disable(uint32_t usart_periph)
  474. {
  475. USART_CTL2(usart_periph) &= ~(USART_CTL2_IREN);
  476. }
  477. /*!
  478. \brief configure the peripheral clock prescaler in USART IrDA low-power mode
  479. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  480. \param[in] psc: 0x00-0xFF
  481. \param[out] none
  482. \retval none
  483. */
  484. void usart_prescaler_config(uint32_t usart_periph, uint8_t psc)
  485. {
  486. USART_GP(usart_periph) &= ~(USART_GP_PSC);
  487. USART_GP(usart_periph) |= psc;
  488. }
  489. /*!
  490. \brief configure IrDA low-power
  491. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  492. \param[in] irlp: IrDA low-power or normal
  493. only one parameter can be selected which is shown as below:
  494. \arg USART_IRLP_LOW: low-power
  495. \arg USART_IRLP_NORMAL: normal
  496. \param[out] none
  497. \retval none
  498. */
  499. void usart_irda_lowpower_config(uint32_t usart_periph, uint32_t irlp)
  500. {
  501. USART_CTL2(usart_periph) &= ~(USART_CTL2_IRLP);
  502. USART_CTL2(usart_periph) |= (USART_CTL2_IRLP & irlp);
  503. }
  504. /*!
  505. \brief configure hardware flow control RTS
  506. \param[in] usart_periph: USARTx(x=0,1,2)
  507. \param[in] rtsconfig: enable or disable RTS
  508. only one parameter can be selected which is shown as below:
  509. \arg USART_RTS_ENABLE: enable RTS
  510. \arg USART_RTS_DISABLE: disable RTS
  511. \param[out] none
  512. \retval none
  513. */
  514. void usart_hardware_flow_rts_config(uint32_t usart_periph, uint32_t rtsconfig)
  515. {
  516. uint32_t ctl = 0U;
  517. ctl = USART_CTL2(usart_periph);
  518. ctl &= ~USART_CTL2_RTSEN;
  519. ctl |= rtsconfig;
  520. /* configure RTS */
  521. USART_CTL2(usart_periph) = ctl;
  522. }
  523. /*!
  524. \brief configure hardware flow control CTS
  525. \param[in] usart_periph: USARTx(x=0,1,2)
  526. \param[in] ctsconfig: enable or disable CTS
  527. only one parameter can be selected which is shown as below:
  528. \arg USART_CTS_ENABLE: enable CTS
  529. \arg USART_CTS_DISABLE: disable CTS
  530. \param[out] none
  531. \retval none
  532. */
  533. void usart_hardware_flow_cts_config(uint32_t usart_periph, uint32_t ctsconfig)
  534. {
  535. uint32_t ctl = 0U;
  536. ctl = USART_CTL2(usart_periph);
  537. ctl &= ~USART_CTL2_CTSEN;
  538. ctl |= ctsconfig;
  539. /* configure CTS */
  540. USART_CTL2(usart_periph) = ctl;
  541. }
  542. /*!
  543. \brief configure USART DMA reception
  544. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3)
  545. \param[in] dmacmd: enable or disable DMA for reception
  546. only one parameter can be selected which is shown as below:
  547. \arg USART_DENR_ENABLE: DMA enable for reception
  548. \arg USART_DENR_DISABLE: DMA disable for reception
  549. \param[out] none
  550. \retval none
  551. */
  552. void usart_dma_receive_config(uint32_t usart_periph, uint32_t dmacmd)
  553. {
  554. uint32_t ctl = 0U;
  555. ctl = USART_CTL2(usart_periph);
  556. ctl &= ~USART_CTL2_DENR;
  557. ctl |= dmacmd;
  558. /* configure DMA reception */
  559. USART_CTL2(usart_periph) = ctl;
  560. }
  561. /*!
  562. \brief configure USART DMA transmission
  563. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3)
  564. \param[in] dmacmd: enable or disable DMA for transmission
  565. only one parameter can be selected which is shown as below:
  566. \arg USART_DENT_ENABLE: DMA enable for transmission
  567. \arg USART_DENT_DISABLE: DMA disable for transmission
  568. \param[out] none
  569. \retval none
  570. */
  571. void usart_dma_transmit_config(uint32_t usart_periph, uint32_t dmacmd)
  572. {
  573. uint32_t ctl = 0U;
  574. ctl = USART_CTL2(usart_periph);
  575. ctl &= ~USART_CTL2_DENT;
  576. ctl |= dmacmd;
  577. /* configure DMA transmission */
  578. USART_CTL2(usart_periph) = ctl;
  579. }
  580. /*!
  581. \brief get flag in STAT register
  582. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  583. \param[in] flag: USART flags, refer to usart_flag_enum
  584. only one parameter can be selected which is shown as below:
  585. \arg USART_FLAG_CTSF: CTS change flag
  586. \arg USART_FLAG_LBDF: LIN break detected flag
  587. \arg USART_FLAG_TBE: transmit data buffer empty
  588. \arg USART_FLAG_TC: transmission complete
  589. \arg USART_FLAG_RBNE: read data buffer not empty
  590. \arg USART_FLAG_IDLEF: IDLE frame detected flag
  591. \arg USART_FLAG_ORERR: overrun error
  592. \arg USART_FLAG_NERR: noise error flag
  593. \arg USART_FLAG_FERR: frame error flag
  594. \arg USART_FLAG_PERR: parity error flag
  595. \param[out] none
  596. \retval FlagStatus: SET or RESET
  597. */
  598. FlagStatus usart_flag_get(uint32_t usart_periph, usart_flag_enum flag)
  599. {
  600. if(RESET != (USART_REG_VAL(usart_periph, flag) & BIT(USART_BIT_POS(flag)))){
  601. return SET;
  602. }else{
  603. return RESET;
  604. }
  605. }
  606. /*!
  607. \brief clear flag in STAT register
  608. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  609. \param[in] flag: USART flags, refer to usart_flag_enum
  610. only one parameter can be selected which is shown as below:
  611. \arg USART_FLAG_CTSF: CTS change flag
  612. \arg USART_FLAG_LBDF: LIN break detected flag
  613. \arg USART_FLAG_TC: transmission complete
  614. \arg USART_FLAG_RBNE: read data buffer not empty
  615. \param[out] none
  616. \retval none
  617. */
  618. void usart_flag_clear(uint32_t usart_periph, usart_flag_enum flag)
  619. {
  620. USART_REG_VAL(usart_periph, flag) &= ~BIT(USART_BIT_POS(flag));
  621. }
  622. /*!
  623. \brief enable USART interrupt
  624. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  625. \param[in] int_flag
  626. only one parameter can be selected which is shown as below:
  627. \arg USART_INT_PERR: parity error interrupt
  628. \arg USART_INT_TBE: transmitter buffer empty interrupt
  629. \arg USART_INT_TC: transmission complete interrupt
  630. \arg USART_INT_RBNE: read data buffer not empty interrupt and overrun error interrupt
  631. \arg USART_INT_IDLE: IDLE line detected interrupt
  632. \arg USART_INT_LBD: LIN break detected interrupt
  633. \arg USART_INT_ERR: error interrupt
  634. \arg USART_INT_CTS: CTS interrupt
  635. \param[out] none
  636. \retval none
  637. */
  638. void usart_interrupt_enable(uint32_t usart_periph, uint32_t int_flag)
  639. {
  640. USART_REG_VAL(usart_periph, int_flag) |= BIT(USART_BIT_POS(int_flag));
  641. }
  642. /*!
  643. \brief disable USART interrupt
  644. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  645. \param[in] int_flag
  646. only one parameter can be selected which is shown as below:
  647. \arg USART_INT_PERR: parity error interrupt
  648. \arg USART_INT_TBE: transmitter buffer empty interrupt
  649. \arg USART_INT_TC: transmission complete interrupt
  650. \arg USART_INT_RBNE: read data buffer not empty interrupt and overrun error interrupt
  651. \arg USART_INT_IDLE: IDLE line detected interrupt
  652. \arg USART_INT_LBD: LIN break detected interrupt
  653. \arg USART_INT_ERR: error interrupt
  654. \arg USART_INT_CTS: CTS interrupt
  655. \param[out] none
  656. \retval none
  657. */
  658. void usart_interrupt_disable(uint32_t usart_periph, uint32_t int_flag)
  659. {
  660. USART_REG_VAL(usart_periph, int_flag) &= ~BIT(USART_BIT_POS(int_flag));
  661. }
  662. /*!
  663. \brief get USART interrupt and flag status
  664. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  665. \param[in] int_flag
  666. only one parameter can be selected which is shown as below:
  667. \arg USART_INT_FLAG_PERR: parity error interrupt and flag
  668. \arg USART_INT_FLAG_TBE: transmitter buffer empty interrupt and flag
  669. \arg USART_INT_FLAG_TC: transmission complete interrupt and flag
  670. \arg USART_INT_FLAG_RBNE: read data buffer not empty interrupt and flag
  671. \arg USART_INT_FLAG_RBNE_ORERR: read data buffer not empty interrupt and overrun error flag
  672. \arg USART_INT_FLAG_IDLE: IDLE line detected interrupt and flag
  673. \arg USART_INT_FLAG_LBD: LIN break detected interrupt and flag
  674. \arg USART_INT_FLAG_CTS: CTS interrupt and flag
  675. \arg USART_INT_FLAG_ERR_ORERR: error interrupt and overrun error
  676. \arg USART_INT_FLAG_ERR_NERR: error interrupt and noise error flag
  677. \arg USART_INT_FLAG_ERR_FERR: error interrupt and frame error flag
  678. \param[out] none
  679. \retval FlagStatus: SET or RESET
  680. */
  681. FlagStatus usart_interrupt_flag_get(uint32_t usart_periph, uint32_t int_flag)
  682. {
  683. uint32_t intenable = 0U, flagstatus = 0U;
  684. /* get the interrupt enable bit status */
  685. intenable = (USART_REG_VAL(usart_periph, int_flag) & BIT(USART_BIT_POS(int_flag)));
  686. /* get the corresponding flag bit status */
  687. flagstatus = (USART_REG_VAL2(usart_periph, int_flag) & BIT(USART_BIT_POS2(int_flag)));
  688. if(flagstatus && intenable){
  689. return SET;
  690. }else{
  691. return RESET;
  692. }
  693. }
  694. /*!
  695. \brief clear USART interrupt flag in STAT register
  696. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  697. \param[in] flag: USART interrupt flag
  698. only one parameter can be selected which is shown as below:
  699. \arg USART_INT_FLAG_CTS: CTS change flag
  700. \arg USART_INT_FLAG_LBD: LIN break detected flag
  701. \arg USART_INT_FLAG_TC: transmission complete
  702. \arg USART_INT_FLAG_RBNE: read data buffer not empty
  703. \param[out] none
  704. \retval none
  705. */
  706. void usart_interrupt_flag_clear(uint32_t usart_periph, uint32_t flag)
  707. {
  708. USART_REG_VAL2(usart_periph, flag) &= ~BIT(USART_BIT_POS2(flag));
  709. }