gd32f30x_usart.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909
  1. /*!
  2. \file gd32f30x_usart.c
  3. \brief USART driver
  4. \version 2017-02-10, V1.0.0, firmware for GD32F30x
  5. \version 2018-10-10, V1.1.0, firmware for GD32F30x
  6. \version 2018-12-25, V2.0.0, firmware for GD32F30x
  7. \version 2020-09-30, V2.1.0, firmware for GD32F30x
  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 "gd32f30x_usart.h"
  33. /* USART register bit offset */
  34. #define GP_GUAT_OFFSET ((uint32_t)8U) /* bit offset of GUAT in USART_GP */
  35. #define CTL3_SCRTNUM_OFFSET ((uint32_t)1U) /* bit offset of SCRTNUM in USART_CTL3 */
  36. #define RT_BL_OFFSET ((uint32_t)24U) /* bit offset of BL in USART_RT */
  37. /*!
  38. \brief reset USART/UART
  39. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  40. \param[out] none
  41. \retval none
  42. */
  43. void usart_deinit(uint32_t usart_periph)
  44. {
  45. switch(usart_periph){
  46. case USART0:
  47. /* reset USART0 */
  48. rcu_periph_reset_enable(RCU_USART0RST);
  49. rcu_periph_reset_disable(RCU_USART0RST);
  50. break;
  51. case USART1:
  52. /* reset USART1 */
  53. rcu_periph_reset_enable(RCU_USART1RST);
  54. rcu_periph_reset_disable(RCU_USART1RST);
  55. break;
  56. case USART2:
  57. /* reset USART2 */
  58. rcu_periph_reset_enable(RCU_USART2RST);
  59. rcu_periph_reset_disable(RCU_USART2RST);
  60. break;
  61. case UART3:
  62. /* reset UART3 */
  63. rcu_periph_reset_enable(RCU_UART3RST);
  64. rcu_periph_reset_disable(RCU_UART3RST);
  65. break;
  66. case UART4:
  67. /* reset UART4 */
  68. rcu_periph_reset_enable(RCU_UART4RST);
  69. rcu_periph_reset_disable(RCU_UART4RST);
  70. break;
  71. default:
  72. break;
  73. }
  74. }
  75. /*!
  76. \brief configure USART baud rate value
  77. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  78. \param[in] baudval: baud rate value
  79. \param[out] none
  80. \retval none
  81. */
  82. void usart_baudrate_set(uint32_t usart_periph, uint32_t baudval)
  83. {
  84. uint32_t uclk=0U, intdiv=0U, fradiv=0U, udiv=0U;
  85. switch(usart_periph){
  86. /* get clock frequency */
  87. case USART0:
  88. /* get USART0 clock */
  89. uclk = rcu_clock_freq_get(CK_APB2);
  90. break;
  91. case USART1:
  92. /* get USART1 clock */
  93. uclk = rcu_clock_freq_get(CK_APB1);
  94. break;
  95. case USART2:
  96. /* get USART2 clock */
  97. uclk = rcu_clock_freq_get(CK_APB1);
  98. break;
  99. case UART3:
  100. /* get UART3 clock */
  101. uclk = rcu_clock_freq_get(CK_APB1);
  102. break;
  103. case UART4:
  104. /* get UART4 clock */
  105. uclk = rcu_clock_freq_get(CK_APB1);
  106. break;
  107. default:
  108. break;
  109. }
  110. /* oversampling by 16, configure the value of USART_BAUD */
  111. udiv = (uclk+baudval/2U)/baudval;
  112. intdiv = udiv & 0xfff0U;
  113. fradiv = udiv & 0xfU;
  114. USART_BAUD(usart_periph) = ((USART_BAUD_FRADIV | USART_BAUD_INTDIV) & (intdiv | fradiv));
  115. }
  116. /*!
  117. \brief configure USART parity
  118. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  119. \param[in] paritycfg: configure USART parity
  120. only one parameter can be selected which is shown as below:
  121. \arg USART_PM_NONE: no parity
  122. \arg USART_PM_ODD: odd parity
  123. \arg USART_PM_EVEN: even parity
  124. \param[out] none
  125. \retval none
  126. */
  127. void usart_parity_config(uint32_t usart_periph, uint32_t paritycfg)
  128. {
  129. /* clear USART_CTL0 PM,PCEN bits */
  130. USART_CTL0(usart_periph) &= ~(USART_CTL0_PM | USART_CTL0_PCEN);
  131. /* configure USART parity mode */
  132. USART_CTL0(usart_periph) |= paritycfg ;
  133. }
  134. /*!
  135. \brief configure USART word length
  136. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  137. \param[in] wlen: USART word length configure
  138. only one parameter can be selected which is shown as below:
  139. \arg USART_WL_8BIT: 8 bits
  140. \arg USART_WL_9BIT: 9 bits
  141. \param[out] none
  142. \retval none
  143. */
  144. void usart_word_length_set(uint32_t usart_periph, uint32_t wlen)
  145. {
  146. /* clear USART_CTL0 WL bit */
  147. USART_CTL0(usart_periph) &= ~USART_CTL0_WL;
  148. /* configure USART word length */
  149. USART_CTL0(usart_periph) |= wlen;
  150. }
  151. /*!
  152. \brief configure USART stop bit length
  153. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  154. \param[in] stblen: USART stop bit configure
  155. only one parameter can be selected which is shown as below:
  156. \arg USART_STB_1BIT: 1 bit
  157. \arg USART_STB_0_5BIT: 0.5 bit, not available for UARTx(x=3,4)
  158. \arg USART_STB_2BIT: 2 bits
  159. \arg USART_STB_1_5BIT: 1.5 bits, not available for UARTx(x=3,4)
  160. \param[out] none
  161. \retval none
  162. */
  163. void usart_stop_bit_set(uint32_t usart_periph, uint32_t stblen)
  164. {
  165. /* clear USART_CTL1 STB bits */
  166. USART_CTL1(usart_periph) &= ~USART_CTL1_STB;
  167. /* configure USART stop bits */
  168. USART_CTL1(usart_periph) |= stblen;
  169. }
  170. /*!
  171. \brief enable USART
  172. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  173. \param[out] none
  174. \retval none
  175. */
  176. void usart_enable(uint32_t usart_periph)
  177. {
  178. USART_CTL0(usart_periph) |= USART_CTL0_UEN;
  179. }
  180. /*!
  181. \brief disable USART
  182. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  183. \param[out] none
  184. \retval none
  185. */
  186. void usart_disable(uint32_t usart_periph)
  187. {
  188. USART_CTL0(usart_periph) &= ~(USART_CTL0_UEN);
  189. }
  190. /*!
  191. \brief configure USART transmitter
  192. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  193. \param[in] txconfig: enable or disable USART transmitter
  194. only one parameter can be selected which is shown as below:
  195. \arg USART_TRANSMIT_ENABLE: enable USART transmission
  196. \arg USART_TRANSMIT_DISABLE: enable USART transmission
  197. \param[out] none
  198. \retval none
  199. */
  200. void usart_transmit_config(uint32_t usart_periph, uint32_t txconfig)
  201. {
  202. uint32_t ctl = 0U;
  203. ctl = USART_CTL0(usart_periph);
  204. ctl &= ~USART_CTL0_TEN;
  205. ctl |= txconfig;
  206. /* configure transfer mode */
  207. USART_CTL0(usart_periph) = ctl;
  208. }
  209. /*!
  210. \brief configure USART receiver
  211. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  212. \param[in] rxconfig: enable or disable USART receiver
  213. only one parameter can be selected which is shown as below:
  214. \arg USART_RECEIVE_ENABLE: enable USART reception
  215. \arg USART_RECEIVE_DISABLE: disable USART reception
  216. \param[out] none
  217. \retval none
  218. */
  219. void usart_receive_config(uint32_t usart_periph, uint32_t rxconfig)
  220. {
  221. uint32_t ctl = 0U;
  222. ctl = USART_CTL0(usart_periph);
  223. ctl &= ~USART_CTL0_REN;
  224. ctl |= rxconfig;
  225. /* configure transfer mode */
  226. USART_CTL0(usart_periph) = ctl;
  227. }
  228. /*!
  229. \brief data is transmitted/received with the LSB/MSB first
  230. \param[in] usart_periph: USARTx(x=0,1,2)
  231. \param[in] msbf: LSB/MSB
  232. only one parameter can be selected which is shown as below:
  233. \arg USART_MSBF_LSB: LSB first
  234. \arg USART_MSBF_MSB: MSB first
  235. \param[out] none
  236. \retval none
  237. */
  238. void usart_data_first_config(uint32_t usart_periph, uint32_t msbf)
  239. {
  240. USART_CTL3(usart_periph) &= ~(USART_CTL3_MSBF);
  241. USART_CTL3(usart_periph) |= msbf;
  242. }
  243. /*!
  244. \brief configure USART inversion
  245. \param[in] usart_periph: USARTx(x=0,1,2)
  246. \param[in] invertpara: refer to enum usart_invert_enum
  247. only one parameter can be selected which is shown as below:
  248. \arg USART_DINV_ENABLE: data bit level inversion
  249. \arg USART_DINV_DISABLE: data bit level not inversion
  250. \arg USART_TXPIN_ENABLE: TX pin level inversion
  251. \arg USART_TXPIN_DISABLE: TX pin level not inversion
  252. \arg USART_RXPIN_ENABLE: RX pin level inversion
  253. \arg USART_RXPIN_DISABLE: RX pin level not inversion
  254. \param[out] none
  255. \retval none
  256. */
  257. void usart_invert_config(uint32_t usart_periph, usart_invert_enum invertpara)
  258. {
  259. /* inverted or not the specified siginal */
  260. switch(invertpara){
  261. case USART_DINV_ENABLE:
  262. /* data bit level inversion */
  263. USART_CTL3(usart_periph) |= USART_CTL3_DINV;
  264. break;
  265. case USART_TXPIN_ENABLE:
  266. /* TX pin level inversion */
  267. USART_CTL3(usart_periph) |= USART_CTL3_TINV;
  268. break;
  269. case USART_RXPIN_ENABLE:
  270. /* RX pin level inversion */
  271. USART_CTL3(usart_periph) |= USART_CTL3_RINV;
  272. break;
  273. case USART_DINV_DISABLE:
  274. /* data bit level not inversion */
  275. USART_CTL3(usart_periph) &= ~(USART_CTL3_DINV);
  276. break;
  277. case USART_TXPIN_DISABLE:
  278. /* TX pin level not inversion */
  279. USART_CTL3(usart_periph) &= ~(USART_CTL3_TINV);
  280. break;
  281. case USART_RXPIN_DISABLE:
  282. /* RX pin level not inversion */
  283. USART_CTL3(usart_periph) &= ~(USART_CTL3_RINV);
  284. break;
  285. default:
  286. break;
  287. }
  288. }
  289. /*!
  290. \brief enable receiver timeout of USART
  291. \param[in] usart_periph: USARTx(x=0,1,2)
  292. \param[out] none
  293. \retval none
  294. */
  295. void usart_receiver_timeout_enable(uint32_t usart_periph)
  296. {
  297. USART_CTL3(usart_periph) |= USART_CTL3_RTEN;
  298. }
  299. /*!
  300. \brief disable receiver timeout of USART
  301. \param[in] usart_periph: USARTx(x=0,1,2)
  302. \param[out] none
  303. \retval none
  304. */
  305. void usart_receiver_timeout_disable(uint32_t usart_periph)
  306. {
  307. USART_CTL3(usart_periph) &= ~(USART_CTL3_RTEN);
  308. }
  309. /*!
  310. \brief set the receiver timeout threshold of USART
  311. \param[in] usart_periph: USARTx(x=0,1,2)
  312. \param[in] rtimeout: 0-0xFFFFFF
  313. \param[out] none
  314. \retval none
  315. */
  316. void usart_receiver_timeout_threshold_config(uint32_t usart_periph, uint32_t rtimeout)
  317. {
  318. USART_RT(usart_periph) &= ~(USART_RT_RT);
  319. USART_RT(usart_periph) |= rtimeout;
  320. }
  321. /*!
  322. \brief USART transmit data function
  323. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  324. \param[in] data: data of transmission
  325. \param[out] none
  326. \retval none
  327. */
  328. void usart_data_transmit(uint32_t usart_periph, uint32_t data)
  329. {
  330. USART_DATA(usart_periph) = ((uint16_t)USART_DATA_DATA & data);
  331. }
  332. /*!
  333. \brief USART receive data function
  334. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  335. \param[out] none
  336. \retval data of received
  337. */
  338. uint16_t usart_data_receive(uint32_t usart_periph)
  339. {
  340. return (uint16_t)(GET_BITS(USART_DATA(usart_periph), 0U, 8U));
  341. }
  342. /*!
  343. \brief configure the address of the USART in wake up by address match mode
  344. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  345. \param[in] addr: address of USART/UART
  346. \param[out] none
  347. \retval none
  348. */
  349. void usart_address_config(uint32_t usart_periph, uint8_t addr)
  350. {
  351. USART_CTL1(usart_periph) &= ~(USART_CTL1_ADDR);
  352. USART_CTL1(usart_periph) |= (USART_CTL1_ADDR & addr);
  353. }
  354. /*!
  355. \brief receiver in mute mode
  356. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  357. \param[out] none
  358. \retval none
  359. */
  360. void usart_mute_mode_enable(uint32_t usart_periph)
  361. {
  362. USART_CTL0(usart_periph) |= USART_CTL0_RWU;
  363. }
  364. /*!
  365. \brief receiver in active mode
  366. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  367. \param[out] none
  368. \retval none
  369. */
  370. void usart_mute_mode_disable(uint32_t usart_periph)
  371. {
  372. USART_CTL0(usart_periph) &= ~(USART_CTL0_RWU);
  373. }
  374. /*!
  375. \brief configure wakeup method in mute mode
  376. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  377. \param[in] wmethod: two methods be used to enter or exit the mute mode
  378. only one parameter can be selected which is shown as below:
  379. \arg USART_WM_IDLE: idle line
  380. \arg USART_WM_ADDR: address mask
  381. \param[out] none
  382. \retval none
  383. */
  384. void usart_mute_mode_wakeup_config(uint32_t usart_periph, uint32_t wmethod)
  385. {
  386. USART_CTL0(usart_periph) &= ~(USART_CTL0_WM);
  387. USART_CTL0(usart_periph) |= wmethod;
  388. }
  389. /*!
  390. \brief enable LIN mode
  391. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  392. \param[out] none
  393. \retval none
  394. */
  395. void usart_lin_mode_enable(uint32_t usart_periph)
  396. {
  397. USART_CTL1(usart_periph) |= USART_CTL1_LMEN;
  398. }
  399. /*!
  400. \brief disable LIN mode
  401. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  402. \param[out] none
  403. \retval none
  404. */
  405. void usart_lin_mode_disable(uint32_t usart_periph)
  406. {
  407. USART_CTL1(usart_periph) &= ~(USART_CTL1_LMEN);
  408. }
  409. /*!
  410. \brief configure lin break frame length
  411. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  412. \param[in] lblen: lin break frame length
  413. only one parameter can be selected which is shown as below:
  414. \arg USART_LBLEN_10B: 10 bits
  415. \arg USART_LBLEN_11B: 11 bits
  416. \param[out] none
  417. \retval none
  418. */
  419. void usart_lin_break_detection_length_config(uint32_t usart_periph, uint32_t lblen)
  420. {
  421. USART_CTL1(usart_periph) &= ~(USART_CTL1_LBLEN);
  422. USART_CTL1(usart_periph) |= (USART_CTL1_LBLEN & lblen);
  423. }
  424. /*!
  425. \brief send break frame
  426. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  427. \param[out] none
  428. \retval none
  429. */
  430. void usart_send_break(uint32_t usart_periph)
  431. {
  432. USART_CTL0(usart_periph) |= USART_CTL0_SBKCMD;
  433. }
  434. /*!
  435. \brief enable half duplex mode
  436. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  437. \param[out] none
  438. \retval none
  439. */
  440. void usart_halfduplex_enable(uint32_t usart_periph)
  441. {
  442. USART_CTL2(usart_periph) |= USART_CTL2_HDEN;
  443. }
  444. /*!
  445. \brief disable half duplex mode
  446. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  447. \param[out] none
  448. \retval none
  449. */
  450. void usart_halfduplex_disable(uint32_t usart_periph)
  451. {
  452. USART_CTL2(usart_periph) &= ~(USART_CTL2_HDEN);
  453. }
  454. /*!
  455. \brief enable CK pin in synchronous mode
  456. \param[in] usart_periph: USARTx(x=0,1,2)
  457. \param[out] none
  458. \retval none
  459. */
  460. void usart_synchronous_clock_enable(uint32_t usart_periph)
  461. {
  462. USART_CTL1(usart_periph) |= USART_CTL1_CKEN;
  463. }
  464. /*!
  465. \brief disable CK pin in synchronous mode
  466. \param[in] usart_periph: USARTx(x=0,1,2)
  467. \param[out] none
  468. \retval none
  469. */
  470. void usart_synchronous_clock_disable(uint32_t usart_periph)
  471. {
  472. USART_CTL1(usart_periph) &= ~(USART_CTL1_CKEN);
  473. }
  474. /*!
  475. \brief configure USART synchronous mode parameters
  476. \param[in] usart_periph: USARTx(x=0,1,2)
  477. \param[in] clen: CK length
  478. only one parameter can be selected which is shown as below:
  479. \arg USART_CLEN_NONE: there are 7 CK pulses for an 8 bit frame and 8 CK pulses for a 9 bit frame
  480. \arg USART_CLEN_EN: there are 8 CK pulses for an 8 bit frame and 9 CK pulses for a 9 bit frame
  481. \param[in] cph: clock phase
  482. only one parameter can be selected which is shown as below:
  483. \arg USART_CPH_1CK: first clock transition is the first data capture edge
  484. \arg USART_CPH_2CK: second clock transition is the first data capture edge
  485. \param[in] cpl: clock polarity
  486. only one parameter can be selected which is shown as below:
  487. \arg USART_CPL_LOW: steady low value on CK pin
  488. \arg USART_CPL_HIGH: steady high value on CK pin
  489. \param[out] none
  490. \retval none
  491. */
  492. void usart_synchronous_clock_config(uint32_t usart_periph, uint32_t clen, uint32_t cph, uint32_t cpl)
  493. {
  494. uint32_t ctl = 0U;
  495. /* read USART_CTL1 register */
  496. ctl = USART_CTL1(usart_periph);
  497. ctl &= ~(USART_CTL1_CLEN | USART_CTL1_CPH | USART_CTL1_CPL);
  498. /* set CK length, CK phase, CK polarity */
  499. ctl |= (USART_CTL1_CLEN & clen) | (USART_CTL1_CPH & cph) | (USART_CTL1_CPL & cpl);
  500. USART_CTL1(usart_periph) = ctl;
  501. }
  502. /*!
  503. \brief configure guard time value in smartcard mode
  504. \param[in] usart_periph: USARTx(x=0,1,2)
  505. \param[in] guat: guard time value, 0-0xFF
  506. \param[out] none
  507. \retval none
  508. */
  509. void usart_guard_time_config(uint32_t usart_periph,uint32_t guat)
  510. {
  511. USART_GP(usart_periph) &= ~(USART_GP_GUAT);
  512. USART_GP(usart_periph) |= (USART_GP_GUAT & ((guat)<<GP_GUAT_OFFSET));
  513. }
  514. /*!
  515. \brief enable smartcard mode
  516. \param[in] usart_periph: USARTx(x=0,1,2)
  517. \param[out] none
  518. \retval none
  519. */
  520. void usart_smartcard_mode_enable(uint32_t usart_periph)
  521. {
  522. USART_CTL2(usart_periph) |= USART_CTL2_SCEN;
  523. }
  524. /*!
  525. \brief disable smartcard mode
  526. \param[in] usart_periph: USARTx(x=0,1,2)
  527. \param[out] none
  528. \retval none
  529. */
  530. void usart_smartcard_mode_disable(uint32_t usart_periph)
  531. {
  532. USART_CTL2(usart_periph) &= ~(USART_CTL2_SCEN);
  533. }
  534. /*!
  535. \brief enable NACK in smartcard mode
  536. \param[in] usart_periph: USARTx(x=0,1,2)
  537. \param[out] none
  538. \retval none
  539. */
  540. void usart_smartcard_mode_nack_enable(uint32_t usart_periph)
  541. {
  542. USART_CTL2(usart_periph) |= USART_CTL2_NKEN;
  543. }
  544. /*!
  545. \brief disable NACK in smartcard mode
  546. \param[in] usart_periph: USARTx(x=0,1,2)
  547. \param[out] none
  548. \retval none
  549. */
  550. void usart_smartcard_mode_nack_disable(uint32_t usart_periph)
  551. {
  552. USART_CTL2(usart_periph) &= ~(USART_CTL2_NKEN);
  553. }
  554. /*!
  555. \brief configure smartcard auto-retry number
  556. \param[in] usart_periph: USARTx(x=0,1,2)
  557. \param[in] scrtnum: smartcard auto-retry number
  558. \param[out] none
  559. \retval none
  560. */
  561. void usart_smartcard_autoretry_config(uint32_t usart_periph, uint32_t scrtnum)
  562. {
  563. USART_CTL3(usart_periph) &= ~(USART_CTL3_SCRTNUM);
  564. USART_CTL3(usart_periph) |= (USART_CTL3_SCRTNUM & ((scrtnum)<<CTL3_SCRTNUM_OFFSET));
  565. }
  566. /*!
  567. \brief configure block length in Smartcard T=1 reception
  568. \param[in] usart_periph: USARTx(x=0,1,2)
  569. \param[in] bl: block length
  570. \param[out] none
  571. \retval none
  572. */
  573. void usart_block_length_config(uint32_t usart_periph, uint32_t bl)
  574. {
  575. USART_RT(usart_periph) &= ~(USART_RT_BL);
  576. USART_RT(usart_periph) |= (USART_RT_BL & ((bl)<<RT_BL_OFFSET));
  577. }
  578. /*!
  579. \brief enable IrDA mode
  580. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  581. \param[out] none
  582. \retval none
  583. */
  584. void usart_irda_mode_enable(uint32_t usart_periph)
  585. {
  586. USART_CTL2(usart_periph) |= USART_CTL2_IREN;
  587. }
  588. /*!
  589. \brief disable IrDA mode
  590. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  591. \param[out] none
  592. \retval none
  593. */
  594. void usart_irda_mode_disable(uint32_t usart_periph)
  595. {
  596. USART_CTL2(usart_periph) &= ~(USART_CTL2_IREN);
  597. }
  598. /*!
  599. \brief configure the peripheral clock prescaler in USART IrDA low-power mode
  600. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  601. \param[in] psc: 0x00-0xFF
  602. \param[out] none
  603. \retval none
  604. */
  605. void usart_prescaler_config(uint32_t usart_periph, uint8_t psc)
  606. {
  607. USART_GP(usart_periph) &= ~(USART_GP_PSC);
  608. USART_GP(usart_periph) |= psc;
  609. }
  610. /*!
  611. \brief configure IrDA low-power
  612. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  613. \param[in] irlp: IrDA low-power or normal
  614. only one parameter can be selected which is shown as below:
  615. \arg USART_IRLP_LOW: low-power
  616. \arg USART_IRLP_NORMAL: normal
  617. \param[out] none
  618. \retval none
  619. */
  620. void usart_irda_lowpower_config(uint32_t usart_periph, uint32_t irlp)
  621. {
  622. USART_CTL2(usart_periph) &= ~(USART_CTL2_IRLP);
  623. USART_CTL2(usart_periph) |= (USART_CTL2_IRLP & irlp);
  624. }
  625. /*!
  626. \brief configure hardware flow control RTS
  627. \param[in] usart_periph: USARTx(x=0,1,2)
  628. \param[in] rtsconfig: enable or disable RTS
  629. only one parameter can be selected which is shown as below:
  630. \arg USART_RTS_ENABLE: enable RTS
  631. \arg USART_RTS_DISABLE: disable RTS
  632. \param[out] none
  633. \retval none
  634. */
  635. void usart_hardware_flow_rts_config(uint32_t usart_periph, uint32_t rtsconfig)
  636. {
  637. uint32_t ctl = 0U;
  638. ctl = USART_CTL2(usart_periph);
  639. ctl &= ~USART_CTL2_RTSEN;
  640. ctl |= rtsconfig;
  641. /* configure RTS */
  642. USART_CTL2(usart_periph) = ctl;
  643. }
  644. /*!
  645. \brief configure hardware flow control CTS
  646. \param[in] usart_periph: USARTx(x=0,1,2)
  647. \param[in] ctsconfig: enable or disable CTS
  648. only one parameter can be selected which is shown as below:
  649. \arg USART_CTS_ENABLE: enable CTS
  650. \arg USART_CTS_DISABLE: disable CTS
  651. \param[out] none
  652. \retval none
  653. */
  654. void usart_hardware_flow_cts_config(uint32_t usart_periph, uint32_t ctsconfig)
  655. {
  656. uint32_t ctl = 0U;
  657. ctl = USART_CTL2(usart_periph);
  658. ctl &= ~USART_CTL2_CTSEN;
  659. ctl |= ctsconfig;
  660. /* configure CTS */
  661. USART_CTL2(usart_periph) = ctl;
  662. }
  663. /*!
  664. \brief configure USART DMA reception
  665. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3)
  666. \param[in] dmacmd: enable or disable DMA for reception
  667. only one parameter can be selected which is shown as below:
  668. \arg USART_DENR_ENABLE: DMA enable for reception
  669. \arg USART_DENR_DISABLE: DMA disable for reception
  670. \param[out] none
  671. \retval none
  672. */
  673. void usart_dma_receive_config(uint32_t usart_periph, uint32_t dmacmd)
  674. {
  675. uint32_t ctl = 0U;
  676. ctl = USART_CTL2(usart_periph);
  677. ctl &= ~USART_CTL2_DENR;
  678. ctl |= dmacmd;
  679. /* configure DMA reception */
  680. USART_CTL2(usart_periph) = ctl;
  681. }
  682. /*!
  683. \brief configure USART DMA transmission
  684. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3)
  685. \param[in] dmacmd: enable or disable DMA for transmission
  686. only one parameter can be selected which is shown as below:
  687. \arg USART_DENT_ENABLE: DMA enable for transmission
  688. \arg USART_DENT_DISABLE: DMA disable for transmission
  689. \param[out] none
  690. \retval none
  691. */
  692. void usart_dma_transmit_config(uint32_t usart_periph, uint32_t dmacmd)
  693. {
  694. uint32_t ctl = 0U;
  695. ctl = USART_CTL2(usart_periph);
  696. ctl &= ~USART_CTL2_DENT;
  697. ctl |= dmacmd;
  698. /* configure DMA transmission */
  699. USART_CTL2(usart_periph) = ctl;
  700. }
  701. /*!
  702. \brief get flag in STAT0/STAT1 register
  703. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  704. \param[in] flag: USART flags, refer to usart_flag_enum
  705. only one parameter can be selected which is shown as below:
  706. \arg USART_FLAG_CTS: CTS change flag
  707. \arg USART_FLAG_LBD: LIN break detected flag
  708. \arg USART_FLAG_TBE: transmit data buffer empty
  709. \arg USART_FLAG_TC: transmission complete
  710. \arg USART_FLAG_RBNE: read data buffer not empty
  711. \arg USART_FLAG_IDLE: IDLE frame detected flag
  712. \arg USART_FLAG_ORERR: overrun error
  713. \arg USART_FLAG_NERR: noise error flag
  714. \arg USART_FLAG_FERR: frame error flag
  715. \arg USART_FLAG_PERR: parity error flag
  716. \arg USART_FLAG_BSY: busy flag
  717. \arg USART_FLAG_EB: end of block flag
  718. \arg USART_FLAG_RT: receiver timeout flag
  719. \param[out] none
  720. \retval FlagStatus: SET or RESET
  721. */
  722. FlagStatus usart_flag_get(uint32_t usart_periph, usart_flag_enum flag)
  723. {
  724. if(RESET != (USART_REG_VAL(usart_periph, flag) & BIT(USART_BIT_POS(flag)))){
  725. return SET;
  726. }else{
  727. return RESET;
  728. }
  729. }
  730. /*!
  731. \brief clear flag in STAT0/STAT1 register
  732. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  733. \param[in] flag: USART flags, refer to usart_flag_enum
  734. only one parameter can be selected which is shown as below:
  735. \arg USART_FLAG_CTS: CTS change flag
  736. \arg USART_FLAG_LBD: LIN break detected flag
  737. \arg USART_FLAG_TC: transmission complete
  738. \arg USART_FLAG_RBNE: read data buffer not empty
  739. \arg USART_FLAG_EB: end of block flag
  740. \arg USART_FLAG_RT: receiver timeout flag
  741. \param[out] none
  742. \retval none
  743. */
  744. void usart_flag_clear(uint32_t usart_periph, usart_flag_enum flag)
  745. {
  746. USART_REG_VAL(usart_periph, flag) &= ~BIT(USART_BIT_POS(flag));
  747. }
  748. /*!
  749. \brief enable USART interrupt
  750. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  751. \param[in] interrupt: USART interrupts, refer to usart_interrupt_enum
  752. only one parameter can be selected which is shown as below:
  753. \arg USART_INT_PERR: parity error interrupt
  754. \arg USART_INT_TBE: transmitter buffer empty interrupt
  755. \arg USART_INT_TC: transmission complete interrupt
  756. \arg USART_INT_RBNE: read data buffer not empty interrupt and overrun error interrupt
  757. \arg USART_INT_IDLE: IDLE line detected interrupt
  758. \arg USART_INT_LBD: LIN break detected interrupt
  759. \arg USART_INT_ERR: error interrupt
  760. \arg USART_INT_CTS: CTS interrupt
  761. \arg USART_INT_RT: interrupt enable bit of receive timeout event
  762. \arg USART_INT_EB: interrupt enable bit of end of block event
  763. \param[out] none
  764. \retval none
  765. */
  766. void usart_interrupt_enable(uint32_t usart_periph, usart_interrupt_enum interrupt)
  767. {
  768. USART_REG_VAL(usart_periph, interrupt) |= BIT(USART_BIT_POS(interrupt));
  769. }
  770. /*!
  771. \brief disable USART interrupt
  772. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  773. \param[in] interrupt: USART interrupts, refer to usart_interrupt_enum
  774. only one parameter can be selected which is shown as below:
  775. \arg USART_INT_PERR: parity error interrupt
  776. \arg USART_INT_TBE: transmitter buffer empty interrupt
  777. \arg USART_INT_TC: transmission complete interrupt
  778. \arg USART_INT_RBNE: read data buffer not empty interrupt and overrun error interrupt
  779. \arg USART_INT_IDLE: IDLE line detected interrupt
  780. \arg USART_INT_LBD: LIN break detected interrupt
  781. \arg USART_INT_ERR: error interrupt
  782. \arg USART_INT_CTS: CTS interrupt
  783. \arg USART_INT_RT: interrupt enable bit of receive timeout event
  784. \arg USART_INT_EB: interrupt enable bit of end of block event
  785. \param[out] none
  786. \retval none
  787. */
  788. void usart_interrupt_disable(uint32_t usart_periph, usart_interrupt_enum interrupt)
  789. {
  790. USART_REG_VAL(usart_periph, interrupt) &= ~BIT(USART_BIT_POS(interrupt));
  791. }
  792. /*!
  793. \brief get USART interrupt and flag status
  794. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  795. \param[in] int_flag: USART interrupt flags, refer to usart_interrupt_flag_enum
  796. only one parameter can be selected which is shown as below:
  797. \arg USART_INT_FLAG_PERR: parity error interrupt and flag
  798. \arg USART_INT_FLAG_TBE: transmitter buffer empty interrupt and flag
  799. \arg USART_INT_FLAG_TC: transmission complete interrupt and flag
  800. \arg USART_INT_FLAG_RBNE: read data buffer not empty interrupt and flag
  801. \arg USART_INT_FLAG_RBNE_ORERR: read data buffer not empty interrupt and overrun error flag
  802. \arg USART_INT_FLAG_IDLE: IDLE line detected interrupt and flag
  803. \arg USART_INT_FLAG_LBD: LIN break detected interrupt and flag
  804. \arg USART_INT_FLAG_CTS: CTS interrupt and flag
  805. \arg USART_INT_FLAG_ERR_ORERR: error interrupt and overrun error
  806. \arg USART_INT_FLAG_ERR_NERR: error interrupt and noise error flag
  807. \arg USART_INT_FLAG_ERR_FERR: error interrupt and frame error flag
  808. \arg USART_INT_FLAG_EB: interrupt enable bit of end of block event and flag
  809. \arg USART_INT_FLAG_RT: interrupt enable bit of receive timeout event and flag
  810. \param[out] none
  811. \retval FlagStatus: SET or RESET
  812. */
  813. FlagStatus usart_interrupt_flag_get(uint32_t usart_periph, usart_interrupt_flag_enum int_flag)
  814. {
  815. uint32_t intenable = 0U, flagstatus = 0U;
  816. /* get the interrupt enable bit status */
  817. intenable = (USART_REG_VAL(usart_periph, int_flag) & BIT(USART_BIT_POS(int_flag)));
  818. /* get the corresponding flag bit status */
  819. flagstatus = (USART_REG_VAL2(usart_periph, int_flag) & BIT(USART_BIT_POS2(int_flag)));
  820. if((0U != flagstatus) && (0U != intenable)){
  821. return SET;
  822. }else{
  823. return RESET;
  824. }
  825. }
  826. /*!
  827. \brief clear USART interrupt flag in STAT0/STAT1 register
  828. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  829. \param[in] int_flag: USART interrupt flags, refer to usart_interrupt_flag_enum
  830. only one parameter can be selected which is shown as below:
  831. \arg USART_INT_FLAG_CTS: CTS change flag
  832. \arg USART_INT_FLAG_LBD: LIN break detected flag
  833. \arg USART_INT_FLAG_TC: transmission complete
  834. \arg USART_INT_FLAG_RBNE: read data buffer not empty
  835. \arg USART_INT_FLAG_EB: end of block flag
  836. \arg USART_INT_FLAG_RT: receiver timeout flag
  837. \param[out] none
  838. \retval none
  839. */
  840. void usart_interrupt_flag_clear(uint32_t usart_periph, usart_interrupt_flag_enum int_flag)
  841. {
  842. USART_REG_VAL2(usart_periph, int_flag) &= ~BIT(USART_BIT_POS2(int_flag));
  843. }