gd32f30x_usart.c 29 KB

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