gd32f30x_usart.c 25 KB

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