gd32f30x_usart.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  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. USART_CTL0(usart_periph) &= ~(USART_CTL0_TEN);
  203. USART_CTL0(usart_periph) |= (USART_CTL0_TEN & txconfig);
  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. USART_CTL0(usart_periph) &= ~(USART_CTL0_REN);
  218. USART_CTL0(usart_periph) |= (USART_CTL0_REN & rxconfig);
  219. }
  220. /*!
  221. \brief data is transmitted/received with the LSB/MSB first
  222. \param[in] usart_periph: USARTx(x=0,1,2)
  223. \param[in] msbf: LSB/MSB
  224. only one parameter can be selected which is shown as below:
  225. \arg USART_MSBF_LSB: LSB first
  226. \arg USART_MSBF_MSB: MSB first
  227. \param[out] none
  228. \retval none
  229. */
  230. void usart_data_first_config(uint32_t usart_periph, uint32_t msbf)
  231. {
  232. USART_CTL3(usart_periph) &= ~(USART_CTL3_MSBF);
  233. USART_CTL3(usart_periph) |= msbf;
  234. }
  235. /*!
  236. \brief configure USART inversion
  237. \param[in] usart_periph: USARTx(x=0,1,2)
  238. \param[in] invertpara: refer to enum usart_invert_enum
  239. only one parameter can be selected which is shown as below:
  240. \arg USART_DINV_ENABLE: data bit level inversion
  241. \arg USART_DINV_DISABLE: data bit level not inversion
  242. \arg USART_TXPIN_ENABLE: TX pin level inversion
  243. \arg USART_TXPIN_DISABLE: TX pin level not inversion
  244. \arg USART_RXPIN_ENABLE: RX pin level inversion
  245. \arg USART_RXPIN_DISABLE: RX pin level not inversion
  246. \param[out] none
  247. \retval none
  248. */
  249. void usart_invert_config(uint32_t usart_periph, usart_invert_enum invertpara)
  250. {
  251. /* inverted or not the specified siginal */
  252. switch(invertpara){
  253. case USART_DINV_ENABLE:
  254. /* data bit level inversion */
  255. USART_CTL3(usart_periph) |= USART_CTL3_DINV;
  256. break;
  257. case USART_TXPIN_ENABLE:
  258. /* TX pin level inversion */
  259. USART_CTL3(usart_periph) |= USART_CTL3_TINV;
  260. break;
  261. case USART_RXPIN_ENABLE:
  262. /* RX pin level inversion */
  263. USART_CTL3(usart_periph) |= USART_CTL3_RINV;
  264. break;
  265. case USART_DINV_DISABLE:
  266. /* data bit level not inversion */
  267. USART_CTL3(usart_periph) &= ~(USART_CTL3_DINV);
  268. break;
  269. case USART_TXPIN_DISABLE:
  270. /* TX pin level not inversion */
  271. USART_CTL3(usart_periph) &= ~(USART_CTL3_TINV);
  272. break;
  273. case USART_RXPIN_DISABLE:
  274. /* RX pin level not inversion */
  275. USART_CTL3(usart_periph) &= ~(USART_CTL3_RINV);
  276. break;
  277. default:
  278. break;
  279. }
  280. }
  281. /*!
  282. \brief enable receiver timeout of USART
  283. \param[in] usart_periph: USARTx(x=0,1,2)
  284. \param[out] none
  285. \retval none
  286. */
  287. void usart_receiver_timeout_enable(uint32_t usart_periph)
  288. {
  289. USART_CTL3(usart_periph) |= USART_CTL3_RTEN;
  290. }
  291. /*!
  292. \brief disable receiver timeout of USART
  293. \param[in] usart_periph: USARTx(x=0,1,2)
  294. \param[out] none
  295. \retval none
  296. */
  297. void usart_receiver_timeout_disable(uint32_t usart_periph)
  298. {
  299. USART_CTL3(usart_periph) &= ~(USART_CTL3_RTEN);
  300. }
  301. /*!
  302. \brief set the receiver timeout threshold of USART
  303. \param[in] usart_periph: USARTx(x=0,1,2)
  304. \param[in] rtimeout: 0-0xFFFFFF
  305. \param[out] none
  306. \retval none
  307. */
  308. void usart_receiver_timeout_threshold_config(uint32_t usart_periph, uint32_t rtimeout)
  309. {
  310. USART_RT(usart_periph) &= ~(USART_RT_RT);
  311. USART_RT(usart_periph) |= rtimeout;
  312. }
  313. /*!
  314. \brief USART transmit data function
  315. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  316. \param[in] data: data of transmission
  317. \param[out] none
  318. \retval none
  319. */
  320. void usart_data_transmit(uint32_t usart_periph, uint16_t data)
  321. {
  322. USART_DATA(usart_periph) = USART_DATA_DATA & (uint32_t)data;
  323. }
  324. /*!
  325. \brief USART receive data function
  326. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  327. \param[out] none
  328. \retval data of received
  329. */
  330. uint16_t usart_data_receive(uint32_t usart_periph)
  331. {
  332. return (uint16_t)(GET_BITS(USART_DATA(usart_periph), 0U, 8U));
  333. }
  334. /*!
  335. \brief configure the address of the USART in wake up by address match mode
  336. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  337. \param[in] addr: address of USART/UART
  338. \param[out] none
  339. \retval none
  340. */
  341. void usart_address_config(uint32_t usart_periph, uint8_t addr)
  342. {
  343. USART_CTL1(usart_periph) &= ~(USART_CTL1_ADDR);
  344. USART_CTL1(usart_periph) |= (USART_CTL1_ADDR & (uint32_t)addr);
  345. }
  346. /*!
  347. \brief receiver in mute mode
  348. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  349. \param[out] none
  350. \retval none
  351. */
  352. void usart_mute_mode_enable(uint32_t usart_periph)
  353. {
  354. USART_CTL0(usart_periph) |= USART_CTL0_RWU;
  355. }
  356. /*!
  357. \brief receiver in active mode
  358. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  359. \param[out] none
  360. \retval none
  361. */
  362. void usart_mute_mode_disable(uint32_t usart_periph)
  363. {
  364. USART_CTL0(usart_periph) &= ~(USART_CTL0_RWU);
  365. }
  366. /*!
  367. \brief configure wakeup method in mute mode
  368. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  369. \param[in] wmethod: two methods be used to enter or exit the mute mode
  370. only one parameter can be selected which is shown as below:
  371. \arg USART_WM_IDLE: idle line
  372. \arg USART_WM_ADDR: address mask
  373. \param[out] none
  374. \retval none
  375. */
  376. void usart_mute_mode_wakeup_config(uint32_t usart_periph, uint32_t wmethod)
  377. {
  378. USART_CTL0(usart_periph) &= ~(USART_CTL0_WM);
  379. USART_CTL0(usart_periph) |= wmethod;
  380. }
  381. /*!
  382. \brief enable LIN mode
  383. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  384. \param[out] none
  385. \retval none
  386. */
  387. void usart_lin_mode_enable(uint32_t usart_periph)
  388. {
  389. USART_CTL1(usart_periph) |= USART_CTL1_LMEN;
  390. }
  391. /*!
  392. \brief disable LIN mode
  393. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  394. \param[out] none
  395. \retval none
  396. */
  397. void usart_lin_mode_disable(uint32_t usart_periph)
  398. {
  399. USART_CTL1(usart_periph) &= ~(USART_CTL1_LMEN);
  400. }
  401. /*!
  402. \brief configure lin break frame length
  403. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  404. \param[in] lblen: lin break frame length
  405. only one parameter can be selected which is shown as below:
  406. \arg USART_LBLEN_10B: 10 bits
  407. \arg USART_LBLEN_11B: 11 bits
  408. \param[out] none
  409. \retval none
  410. */
  411. void usart_lin_break_detection_length_config(uint32_t usart_periph, uint32_t lblen)
  412. {
  413. USART_CTL1(usart_periph) &= ~(USART_CTL1_LBLEN);
  414. USART_CTL1(usart_periph) |= (USART_CTL1_LBLEN & lblen);
  415. }
  416. /*!
  417. \brief send break frame
  418. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  419. \param[out] none
  420. \retval none
  421. */
  422. void usart_send_break(uint32_t usart_periph)
  423. {
  424. USART_CTL0(usart_periph) |= USART_CTL0_SBKCMD;
  425. }
  426. /*!
  427. \brief enable half duplex mode
  428. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  429. \param[out] none
  430. \retval none
  431. */
  432. void usart_halfduplex_enable(uint32_t usart_periph)
  433. {
  434. USART_CTL2(usart_periph) |= USART_CTL2_HDEN;
  435. }
  436. /*!
  437. \brief disable half duplex mode
  438. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  439. \param[out] none
  440. \retval none
  441. */
  442. void usart_halfduplex_disable(uint32_t usart_periph)
  443. {
  444. USART_CTL2(usart_periph) &= ~(USART_CTL2_HDEN);
  445. }
  446. /*!
  447. \brief enable CK pin in synchronous mode
  448. \param[in] usart_periph: USARTx(x=0,1,2)
  449. \param[out] none
  450. \retval none
  451. */
  452. void usart_synchronous_clock_enable(uint32_t usart_periph)
  453. {
  454. USART_CTL1(usart_periph) |= USART_CTL1_CKEN;
  455. }
  456. /*!
  457. \brief disable CK pin in synchronous mode
  458. \param[in] usart_periph: USARTx(x=0,1,2)
  459. \param[out] none
  460. \retval none
  461. */
  462. void usart_synchronous_clock_disable(uint32_t usart_periph)
  463. {
  464. USART_CTL1(usart_periph) &= ~(USART_CTL1_CKEN);
  465. }
  466. /*!
  467. \brief configure USART synchronous mode parameters
  468. \param[in] usart_periph: USARTx(x=0,1,2)
  469. \param[in] clen: CK length
  470. only one parameter can be selected which is shown as below:
  471. \arg USART_CLEN_NONE: there are 7 CK pulses for an 8 bit frame and 8 CK pulses for a 9 bit frame
  472. \arg USART_CLEN_EN: there are 8 CK pulses for an 8 bit frame and 9 CK pulses for a 9 bit frame
  473. \param[in] cph: clock phase
  474. only one parameter can be selected which is shown as below:
  475. \arg USART_CPH_1CK: first clock transition is the first data capture edge
  476. \arg USART_CPH_2CK: second clock transition is the first data capture edge
  477. \param[in] cpl: clock polarity
  478. only one parameter can be selected which is shown as below:
  479. \arg USART_CPL_LOW: steady low value on CK pin
  480. \arg USART_CPL_HIGH: steady high value on CK pin
  481. \param[out] none
  482. \retval none
  483. */
  484. void usart_synchronous_clock_config(uint32_t usart_periph, uint32_t clen, uint32_t cph, uint32_t cpl)
  485. {
  486. USART_CTL1(usart_periph) &= ~(USART_CTL1_CLEN | USART_CTL1_CPH | USART_CTL1_CPL);
  487. USART_CTL1(usart_periph) |= (USART_CTL1_CLEN & clen) | (USART_CTL1_CPH & cph) | (USART_CTL1_CPL & cpl);
  488. }
  489. /*!
  490. \brief configure guard time value in smartcard mode
  491. \param[in] usart_periph: USARTx(x=0,1,2)
  492. \param[in] guat: guard time value, 0-0xFF
  493. \param[out] none
  494. \retval none
  495. */
  496. void usart_guard_time_config(uint32_t usart_periph, uint8_t guat)
  497. {
  498. USART_GP(usart_periph) &= ~(USART_GP_GUAT);
  499. USART_GP(usart_periph) |= (USART_GP_GUAT & ((uint32_t)guat << GP_GUAT_OFFSET));
  500. }
  501. /*!
  502. \brief enable smartcard mode
  503. \param[in] usart_periph: USARTx(x=0,1,2)
  504. \param[out] none
  505. \retval none
  506. */
  507. void usart_smartcard_mode_enable(uint32_t usart_periph)
  508. {
  509. USART_CTL2(usart_periph) |= USART_CTL2_SCEN;
  510. }
  511. /*!
  512. \brief disable smartcard mode
  513. \param[in] usart_periph: USARTx(x=0,1,2)
  514. \param[out] none
  515. \retval none
  516. */
  517. void usart_smartcard_mode_disable(uint32_t usart_periph)
  518. {
  519. USART_CTL2(usart_periph) &= ~(USART_CTL2_SCEN);
  520. }
  521. /*!
  522. \brief enable NACK in smartcard mode
  523. \param[in] usart_periph: USARTx(x=0,1,2)
  524. \param[out] none
  525. \retval none
  526. */
  527. void usart_smartcard_mode_nack_enable(uint32_t usart_periph)
  528. {
  529. USART_CTL2(usart_periph) |= USART_CTL2_NKEN;
  530. }
  531. /*!
  532. \brief disable NACK in smartcard mode
  533. \param[in] usart_periph: USARTx(x=0,1,2)
  534. \param[out] none
  535. \retval none
  536. */
  537. void usart_smartcard_mode_nack_disable(uint32_t usart_periph)
  538. {
  539. USART_CTL2(usart_periph) &= ~(USART_CTL2_NKEN);
  540. }
  541. /*!
  542. \brief configure smartcard auto-retry number
  543. \param[in] usart_periph: USARTx(x=0,1,2)
  544. \param[in] scrtnum: smartcard auto-retry number
  545. \param[out] none
  546. \retval none
  547. */
  548. void usart_smartcard_autoretry_config(uint32_t usart_periph, uint8_t scrtnum)
  549. {
  550. USART_CTL3(usart_periph) &= ~(USART_CTL3_SCRTNUM);
  551. USART_CTL3(usart_periph) |= (USART_CTL3_SCRTNUM & ((uint32_t)scrtnum << CTL3_SCRTNUM_OFFSET));
  552. }
  553. /*!
  554. \brief configure block length in Smartcard T=1 reception
  555. \param[in] usart_periph: USARTx(x=0,1,2)
  556. \param[in] bl: block length
  557. \param[out] none
  558. \retval none
  559. */
  560. void usart_block_length_config(uint32_t usart_periph, uint8_t bl)
  561. {
  562. USART_RT(usart_periph) &= ~(USART_RT_BL);
  563. USART_RT(usart_periph) |= (USART_RT_BL & ((uint32_t)bl << RT_BL_OFFSET));
  564. }
  565. /*!
  566. \brief enable IrDA mode
  567. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  568. \param[out] none
  569. \retval none
  570. */
  571. void usart_irda_mode_enable(uint32_t usart_periph)
  572. {
  573. USART_CTL2(usart_periph) |= USART_CTL2_IREN;
  574. }
  575. /*!
  576. \brief disable IrDA mode
  577. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  578. \param[out] none
  579. \retval none
  580. */
  581. void usart_irda_mode_disable(uint32_t usart_periph)
  582. {
  583. USART_CTL2(usart_periph) &= ~(USART_CTL2_IREN);
  584. }
  585. /*!
  586. \brief configure the peripheral clock prescaler in USART IrDA low-power mode
  587. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  588. \param[in] psc: 0x00-0xFF
  589. \param[out] none
  590. \retval none
  591. */
  592. void usart_prescaler_config(uint32_t usart_periph, uint8_t psc)
  593. {
  594. USART_GP(usart_periph) &= ~(USART_GP_PSC);
  595. USART_GP(usart_periph) |= (uint32_t)psc;
  596. }
  597. /*!
  598. \brief configure IrDA low-power
  599. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  600. \param[in] irlp: IrDA low-power or normal
  601. only one parameter can be selected which is shown as below:
  602. \arg USART_IRLP_LOW: low-power
  603. \arg USART_IRLP_NORMAL: normal
  604. \param[out] none
  605. \retval none
  606. */
  607. void usart_irda_lowpower_config(uint32_t usart_periph, uint32_t irlp)
  608. {
  609. USART_CTL2(usart_periph) &= ~(USART_CTL2_IRLP);
  610. USART_CTL2(usart_periph) |= (USART_CTL2_IRLP & irlp);
  611. }
  612. /*!
  613. \brief configure hardware flow control RTS
  614. \param[in] usart_periph: USARTx(x=0,1,2)
  615. \param[in] rtsconfig: enable or disable RTS
  616. only one parameter can be selected which is shown as below:
  617. \arg USART_RTS_ENABLE: enable RTS
  618. \arg USART_RTS_DISABLE: disable RTS
  619. \param[out] none
  620. \retval none
  621. */
  622. void usart_hardware_flow_rts_config(uint32_t usart_periph, uint32_t rtsconfig)
  623. {
  624. USART_CTL2(usart_periph) &= ~(USART_CTL2_RTSEN);
  625. USART_CTL2(usart_periph) |= (USART_CTL2_RTSEN & rtsconfig);
  626. }
  627. /*!
  628. \brief configure hardware flow control CTS
  629. \param[in] usart_periph: USARTx(x=0,1,2)
  630. \param[in] ctsconfig: enable or disable CTS
  631. only one parameter can be selected which is shown as below:
  632. \arg USART_CTS_ENABLE: enable CTS
  633. \arg USART_CTS_DISABLE: disable CTS
  634. \param[out] none
  635. \retval none
  636. */
  637. void usart_hardware_flow_cts_config(uint32_t usart_periph, uint32_t ctsconfig)
  638. {
  639. USART_CTL2(usart_periph) &= ~(USART_CTL2_CTSEN);
  640. USART_CTL2(usart_periph) |= (USART_CTL2_CTSEN & ctsconfig);
  641. }
  642. /*!
  643. \brief configure USART DMA reception
  644. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  645. \param[in] dmaconfig: USART DMA mode
  646. only one parameter can be selected which is shown as below:
  647. \arg USART_RECEIVE_DMA_ENABLE: enable USART DMA for reception
  648. \arg USART_RECEIVE_DMA_DISABLE: disable USART DMA for reception
  649. \param[out] none
  650. \retval none
  651. */
  652. void usart_dma_receive_config(uint32_t usart_periph, uint8_t dmacmd)
  653. {
  654. USART_CTL2(usart_periph) &= ~(USART_CTL2_DENR);
  655. USART_CTL2(usart_periph) |= (USART_CTL2_DENR & dmacmd);
  656. }
  657. /*!
  658. \brief configure USART DMA transmission
  659. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  660. \param[in] dmaconfig: USART DMA mode
  661. only one parameter can be selected which is shown as below:
  662. \arg USART_TRANSMIT_DMA_ENABLE: enable USART DMA for transmission
  663. \arg USART_TRANSMIT_DMA_DISABLE: disable USART DMA for transmission
  664. \param[out] none
  665. \retval none
  666. */
  667. void usart_dma_transmit_config(uint32_t usart_periph, uint8_t dmacmd)
  668. {
  669. USART_CTL2(usart_periph) &= ~(USART_CTL2_DENT);
  670. USART_CTL2(usart_periph) |= (USART_CTL2_DENT & dmacmd);
  671. }
  672. /*!
  673. \brief get flag in STAT0/STAT1 register
  674. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  675. \param[in] flag: USART flags, refer to usart_flag_enum
  676. only one parameter can be selected which is shown as below:
  677. \arg USART_FLAG_CTS: CTS change flag
  678. \arg USART_FLAG_LBD: LIN break detected flag
  679. \arg USART_FLAG_TBE: transmit data buffer empty
  680. \arg USART_FLAG_TC: transmission complete
  681. \arg USART_FLAG_RBNE: read data buffer not empty
  682. \arg USART_FLAG_IDLE: IDLE frame detected flag
  683. \arg USART_FLAG_ORERR: overrun error
  684. \arg USART_FLAG_NERR: noise error flag
  685. \arg USART_FLAG_FERR: frame error flag
  686. \arg USART_FLAG_PERR: parity error flag
  687. \arg USART_FLAG_BSY: busy flag
  688. \arg USART_FLAG_EB: end of block flag
  689. \arg USART_FLAG_RT: receiver timeout flag
  690. \param[out] none
  691. \retval FlagStatus: SET or RESET
  692. */
  693. FlagStatus usart_flag_get(uint32_t usart_periph, usart_flag_enum flag)
  694. {
  695. if(RESET != (USART_REG_VAL(usart_periph, flag) & BIT(USART_BIT_POS(flag)))){
  696. return SET;
  697. }else{
  698. return RESET;
  699. }
  700. }
  701. /*!
  702. \brief clear 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_TC: transmission complete
  709. \arg USART_FLAG_RBNE: read data buffer not empty
  710. \arg USART_FLAG_EB: end of block flag
  711. \arg USART_FLAG_RT: receiver timeout flag
  712. \param[out] none
  713. \retval none
  714. */
  715. void usart_flag_clear(uint32_t usart_periph, usart_flag_enum flag)
  716. {
  717. USART_REG_VAL(usart_periph, flag) = ~BIT(USART_BIT_POS(flag));
  718. }
  719. /*!
  720. \brief enable USART interrupt
  721. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  722. \param[in] interrupt: USART interrupts, refer to usart_interrupt_enum
  723. only one parameter can be selected which is shown as below:
  724. \arg USART_INT_PERR: parity error interrupt
  725. \arg USART_INT_TBE: transmitter buffer empty interrupt
  726. \arg USART_INT_TC: transmission complete interrupt
  727. \arg USART_INT_RBNE: read data buffer not empty interrupt and overrun error interrupt
  728. \arg USART_INT_IDLE: IDLE line detected interrupt
  729. \arg USART_INT_LBD: LIN break detected interrupt
  730. \arg USART_INT_ERR: error interrupt
  731. \arg USART_INT_CTS: CTS interrupt
  732. \arg USART_INT_RT: interrupt enable bit of receive timeout event
  733. \arg USART_INT_EB: interrupt enable bit of end of block event
  734. \param[out] none
  735. \retval none
  736. */
  737. void usart_interrupt_enable(uint32_t usart_periph, usart_interrupt_enum interrupt)
  738. {
  739. USART_REG_VAL(usart_periph, interrupt) |= BIT(USART_BIT_POS(interrupt));
  740. }
  741. /*!
  742. \brief disable USART interrupt
  743. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  744. \param[in] interrupt: USART interrupts, refer to usart_interrupt_enum
  745. only one parameter can be selected which is shown as below:
  746. \arg USART_INT_PERR: parity error interrupt
  747. \arg USART_INT_TBE: transmitter buffer empty interrupt
  748. \arg USART_INT_TC: transmission complete interrupt
  749. \arg USART_INT_RBNE: read data buffer not empty interrupt and overrun error interrupt
  750. \arg USART_INT_IDLE: IDLE line detected interrupt
  751. \arg USART_INT_LBD: LIN break detected interrupt
  752. \arg USART_INT_ERR: error interrupt
  753. \arg USART_INT_CTS: CTS interrupt
  754. \arg USART_INT_RT: interrupt enable bit of receive timeout event
  755. \arg USART_INT_EB: interrupt enable bit of end of block event
  756. \param[out] none
  757. \retval none
  758. */
  759. void usart_interrupt_disable(uint32_t usart_periph, usart_interrupt_enum interrupt)
  760. {
  761. USART_REG_VAL(usart_periph, interrupt) &= ~BIT(USART_BIT_POS(interrupt));
  762. }
  763. /*!
  764. \brief get USART interrupt and flag status
  765. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  766. \param[in] int_flag: USART interrupt flags, refer to usart_interrupt_flag_enum
  767. only one parameter can be selected which is shown as below:
  768. \arg USART_INT_FLAG_PERR: parity error interrupt and flag
  769. \arg USART_INT_FLAG_TBE: transmitter buffer empty interrupt and flag
  770. \arg USART_INT_FLAG_TC: transmission complete interrupt and flag
  771. \arg USART_INT_FLAG_RBNE: read data buffer not empty interrupt and flag
  772. \arg USART_INT_FLAG_RBNE_ORERR: read data buffer not empty interrupt and overrun error flag
  773. \arg USART_INT_FLAG_IDLE: IDLE line detected interrupt and flag
  774. \arg USART_INT_FLAG_LBD: LIN break detected interrupt and flag
  775. \arg USART_INT_FLAG_CTS: CTS interrupt and flag
  776. \arg USART_INT_FLAG_ERR_ORERR: error interrupt and overrun error
  777. \arg USART_INT_FLAG_ERR_NERR: error interrupt and noise error flag
  778. \arg USART_INT_FLAG_ERR_FERR: error interrupt and frame error flag
  779. \arg USART_INT_FLAG_EB: interrupt enable bit of end of block event and flag
  780. \arg USART_INT_FLAG_RT: interrupt enable bit of receive timeout event and flag
  781. \param[out] none
  782. \retval FlagStatus: SET or RESET
  783. */
  784. FlagStatus usart_interrupt_flag_get(uint32_t usart_periph, usart_interrupt_flag_enum int_flag)
  785. {
  786. uint32_t intenable = 0U, flagstatus = 0U;
  787. /* get the interrupt enable bit status */
  788. intenable = (USART_REG_VAL(usart_periph, int_flag) & BIT(USART_BIT_POS(int_flag)));
  789. /* get the corresponding flag bit status */
  790. flagstatus = (USART_REG_VAL2(usart_periph, int_flag) & BIT(USART_BIT_POS2(int_flag)));
  791. if((0U != flagstatus) && (0U != intenable)){
  792. return SET;
  793. }else{
  794. return RESET;
  795. }
  796. }
  797. /*!
  798. \brief clear USART interrupt flag in STAT0/STAT1 register
  799. \param[in] usart_periph: USARTx(x=0,1,2)/UARTx(x=3,4)
  800. \param[in] int_flag: USART interrupt flags, refer to usart_interrupt_flag_enum
  801. only one parameter can be selected which is shown as below:
  802. \arg USART_INT_FLAG_CTS: CTS change flag
  803. \arg USART_INT_FLAG_LBD: LIN break detected flag
  804. \arg USART_INT_FLAG_TC: transmission complete
  805. \arg USART_INT_FLAG_RBNE: read data buffer not empty
  806. \arg USART_INT_FLAG_EB: end of block flag
  807. \arg USART_INT_FLAG_RT: receiver timeout flag
  808. \param[out] none
  809. \retval none
  810. */
  811. void usart_interrupt_flag_clear(uint32_t usart_periph, usart_interrupt_flag_enum int_flag)
  812. {
  813. USART_REG_VAL2(usart_periph, int_flag) = ~BIT(USART_BIT_POS2(int_flag));
  814. }