gd32e23x_usart.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275
  1. /*!
  2. \file gd32e23x_usart.c
  3. \brief USART driver
  4. \version 2024-02-22, V2.1.0, firmware for GD32E23x
  5. */
  6. /*
  7. Copyright (c) 2024, 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 "gd32e23x_usart.h"
  30. /*!
  31. \brief reset USART
  32. \param[in] usart_periph: USARTx(x=0,1)
  33. \param[out] none
  34. \retval none
  35. */
  36. void usart_deinit(uint32_t usart_periph)
  37. {
  38. switch(usart_periph){
  39. case USART0:
  40. /* reset USART0 */
  41. rcu_periph_reset_enable(RCU_USART0RST);
  42. rcu_periph_reset_disable(RCU_USART0RST);
  43. break;
  44. case USART1:
  45. /* reset USART1 */
  46. rcu_periph_reset_enable(RCU_USART1RST);
  47. rcu_periph_reset_disable(RCU_USART1RST);
  48. break;
  49. default:
  50. break;
  51. }
  52. }
  53. /*!
  54. \brief configure USART baud rate value
  55. \param[in] usart_periph: USARTx(x=0,1)
  56. \param[in] baudval: baud rate value
  57. \param[out] none
  58. \retval none
  59. */
  60. void usart_baudrate_set(uint32_t usart_periph, uint32_t baudval)
  61. {
  62. uint32_t uclk = 0U, intdiv = 0U, fradiv = 0U, udiv = 0U;
  63. switch(usart_periph){
  64. /* get clock frequency */
  65. case USART0:
  66. /* get USART0 clock */
  67. uclk = rcu_clock_freq_get(CK_USART);
  68. break;
  69. case USART1:
  70. /* get USART1 clock */
  71. uclk = rcu_clock_freq_get(CK_APB1);
  72. break;
  73. default:
  74. break;
  75. }
  76. if(USART_CTL0(usart_periph) & USART_CTL0_OVSMOD){
  77. /* oversampling by 8, configure the value of USART_BAUD */
  78. udiv = ((2U*uclk)+baudval/2U)/baudval;
  79. intdiv = udiv & 0x0000fff0U;
  80. fradiv = (udiv>>1U) & 0x00000007U;
  81. USART_BAUD(usart_periph) = ((USART_BAUD_FRADIV | USART_BAUD_INTDIV) & (intdiv | fradiv));
  82. }else{
  83. /* oversampling by 16, configure the value of USART_BAUD */
  84. udiv = (uclk+baudval/2U)/baudval;
  85. intdiv = udiv & 0x0000fff0U;
  86. fradiv = udiv & 0x0000000fU;
  87. USART_BAUD(usart_periph) = ((USART_BAUD_FRADIV | USART_BAUD_INTDIV) & (intdiv | fradiv));
  88. }
  89. }
  90. /*!
  91. \brief configure USART parity
  92. \param[in] usart_periph: USARTx(x=0,1)
  93. \param[in] paritycfg: USART parity configure
  94. only one parameter can be selected which is shown as below:
  95. \arg USART_PM_NONE: no parity
  96. \arg USART_PM_ODD: odd parity
  97. \arg USART_PM_EVEN: even parity
  98. \param[out] none
  99. \retval none
  100. */
  101. void usart_parity_config(uint32_t usart_periph, uint32_t paritycfg)
  102. {
  103. /* disable USART */
  104. USART_CTL0(usart_periph) &= ~(USART_CTL0_UEN);
  105. /* clear USART_CTL0 PM,PCEN bits */
  106. USART_CTL0(usart_periph) &= ~(USART_CTL0_PM | USART_CTL0_PCEN);
  107. /* configure USART parity mode */
  108. USART_CTL0(usart_periph) |= paritycfg;
  109. }
  110. /*!
  111. \brief configure USART word length
  112. \param[in] usart_periph: USARTx(x=0,1)
  113. \param[in] wlen: USART word length configure
  114. only one parameter can be selected which is shown as below:
  115. \arg USART_WL_8BIT: 8 bits
  116. \arg USART_WL_9BIT: 9 bits
  117. \param[out] none
  118. \retval none
  119. */
  120. void usart_word_length_set(uint32_t usart_periph, uint32_t wlen)
  121. {
  122. /* disable USART */
  123. USART_CTL0(usart_periph) &= ~(USART_CTL0_UEN);
  124. /* clear USART_CTL0 WL bit */
  125. USART_CTL0(usart_periph) &= ~USART_CTL0_WL;
  126. /* configure USART word length */
  127. USART_CTL0(usart_periph) |= wlen;
  128. }
  129. /*!
  130. \brief configure USART stop bit length
  131. \param[in] usart_periph: USARTx(x=0,1)
  132. \param[in] stblen: USART stop bit configure
  133. only one parameter can be selected which is shown as below:
  134. \arg USART_STB_1BIT: 1 bit
  135. \arg USART_STB_0_5BIT: 0.5bit
  136. \arg USART_STB_2BIT: 2 bits
  137. \arg USART_STB_1_5BIT: 1.5bit
  138. \param[out] none
  139. \retval none
  140. */
  141. void usart_stop_bit_set(uint32_t usart_periph, uint32_t stblen)
  142. {
  143. /* disable USART */
  144. USART_CTL0(usart_periph) &= ~(USART_CTL0_UEN);
  145. /* clear USART_CTL1 STB bits */
  146. USART_CTL1(usart_periph) &= ~USART_CTL1_STB;
  147. USART_CTL1(usart_periph) |= stblen;
  148. }
  149. /*!
  150. \brief enable USART
  151. \param[in] usart_periph: USARTx(x=0,1)
  152. \param[out] none
  153. \retval none
  154. */
  155. void usart_enable(uint32_t usart_periph)
  156. {
  157. USART_CTL0(usart_periph) |= USART_CTL0_UEN;
  158. }
  159. /*!
  160. \brief disable USART
  161. \param[in] usart_periph: USARTx(x=0,1)
  162. \param[out] none
  163. \retval none
  164. */
  165. void usart_disable(uint32_t usart_periph)
  166. {
  167. USART_CTL0(usart_periph) &= ~(USART_CTL0_UEN);
  168. }
  169. /*!
  170. \brief configure USART transmitter
  171. \param[in] usart_periph: USARTx(x=0,1)
  172. \param[in] txconfig: enable or disable USART transmitter
  173. only one parameter can be selected which is shown as below:
  174. \arg USART_TRANSMIT_ENABLE: enable USART transmission
  175. \arg USART_TRANSMIT_DISABLE: enable USART transmission
  176. \param[out] none
  177. \retval none
  178. */
  179. void usart_transmit_config(uint32_t usart_periph, uint32_t txconfig)
  180. {
  181. USART_CTL0(usart_periph) &= ~USART_CTL0_TEN;
  182. /* configure transfer mode */
  183. USART_CTL0(usart_periph) |= txconfig;
  184. }
  185. /*!
  186. \brief configure USART receiver
  187. \param[in] usart_periph: USARTx(x=0,1)
  188. \param[in] rxconfig: enable or disable USART receiver
  189. only one parameter can be selected which is shown as below:
  190. \arg USART_RECEIVE_ENABLE: enable USART reception
  191. \arg USART_RECEIVE_DISABLE: disable USART reception
  192. \param[out] none
  193. \retval none
  194. */
  195. void usart_receive_config(uint32_t usart_periph, uint32_t rxconfig)
  196. {
  197. USART_CTL0(usart_periph) &= ~USART_CTL0_REN;
  198. /* configure receiver mode */
  199. USART_CTL0(usart_periph) |= rxconfig;
  200. }
  201. /*!
  202. \brief data is transmitted/received with the LSB/MSB first
  203. \param[in] usart_periph: USARTx(x=0,1)
  204. \param[in] msbf: LSB/MSB
  205. only one parameter can be selected which is shown as below:
  206. \arg USART_MSBF_LSB: LSB first
  207. \arg USART_MSBF_MSB: MSB first
  208. \param[out] none
  209. \retval none
  210. */
  211. void usart_data_first_config(uint32_t usart_periph, uint32_t msbf)
  212. {
  213. USART_CTL0(usart_periph) &= ~(USART_CTL0_UEN);
  214. /* configure LSB or MSB first */
  215. USART_CTL1(usart_periph) &= ~(USART_CTL1_MSBF);
  216. USART_CTL1(usart_periph) |= (USART_CTL1_MSBF & msbf);
  217. }
  218. /*!
  219. \brief USART inverted configure
  220. \param[in] usart_periph: USARTx(x=0,1)
  221. \param[in] invertpara: refer to usart_invert_enum
  222. only one parameter can be selected which is shown as below:
  223. \arg USART_DINV_ENABLE: data bit level inversion
  224. \arg USART_DINV_DISABLE: data bit level not inversion
  225. \arg USART_TXPIN_ENABLE: TX pin level inversion
  226. \arg USART_TXPIN_DISABLE: TX pin level not inversion
  227. \arg USART_RXPIN_ENABLE: RX pin level inversion
  228. \arg USART_RXPIN_DISABLE: RX pin level not inversion
  229. \arg USART_SWAP_ENABLE: swap TX/RX pins
  230. \arg USART_SWAP_DISABLE: not swap TX/RX pins
  231. \param[out] none
  232. \retval none
  233. */
  234. void usart_invert_config(uint32_t usart_periph, usart_invert_enum invertpara)
  235. {
  236. USART_CTL0(usart_periph) &= ~(USART_CTL0_UEN);
  237. /* inverted or not the specified signal */
  238. switch(invertpara){
  239. case USART_DINV_ENABLE:
  240. USART_CTL1(usart_periph) |= USART_CTL1_DINV;
  241. break;
  242. case USART_DINV_DISABLE:
  243. USART_CTL1(usart_periph) &= ~(USART_CTL1_DINV);
  244. break;
  245. case USART_TXPIN_ENABLE:
  246. USART_CTL1(usart_periph) |= USART_CTL1_TINV;
  247. break;
  248. case USART_TXPIN_DISABLE:
  249. USART_CTL1(usart_periph) &= ~(USART_CTL1_TINV);
  250. break;
  251. case USART_RXPIN_ENABLE:
  252. USART_CTL1(usart_periph) |= USART_CTL1_RINV;
  253. break;
  254. case USART_RXPIN_DISABLE:
  255. USART_CTL1(usart_periph) &= ~(USART_CTL1_RINV);
  256. break;
  257. case USART_SWAP_ENABLE:
  258. USART_CTL1(usart_periph) |= USART_CTL1_STRP;
  259. break;
  260. case USART_SWAP_DISABLE:
  261. USART_CTL1(usart_periph) &= ~(USART_CTL1_STRP);
  262. break;
  263. default:
  264. break;
  265. }
  266. }
  267. /*!
  268. \brief enable the USART overrun function
  269. \param[in] usart_periph: USARTx(x=0,1)
  270. \param[out] none
  271. \retval none
  272. */
  273. void usart_overrun_enable(uint32_t usart_periph)
  274. {
  275. USART_CTL0(usart_periph) &= ~(USART_CTL0_UEN);
  276. /* enable overrun function */
  277. USART_CTL2(usart_periph) &= ~(USART_CTL2_OVRD);
  278. }
  279. /*!
  280. \brief disable the USART overrun function
  281. \param[in] usart_periph: USARTx(x=0,1)
  282. \param[out] none
  283. \retval none
  284. */
  285. void usart_overrun_disable(uint32_t usart_periph)
  286. {
  287. USART_CTL0(usart_periph) &= ~(USART_CTL0_UEN);
  288. /* disable overrun function */
  289. USART_CTL2(usart_periph) |= USART_CTL2_OVRD;
  290. }
  291. /*!
  292. \brief configure the USART oversample mode
  293. \param[in] usart_periph: USARTx(x=0,1)
  294. \param[in] oversamp: oversample value
  295. only one parameter can be selected which is shown as below:
  296. \arg USART_OVSMOD_8: oversampling by 8
  297. \arg USART_OVSMOD_16: oversampling by 16
  298. \param[out] none
  299. \retval none
  300. */
  301. void usart_oversample_config(uint32_t usart_periph, uint32_t oversamp)
  302. {
  303. /* disable USART */
  304. USART_CTL0(usart_periph) &= ~(USART_CTL0_UEN);
  305. /* clear OVSMOD bit */
  306. USART_CTL0(usart_periph) &= ~(USART_CTL0_OVSMOD);
  307. USART_CTL0(usart_periph) |= oversamp;
  308. }
  309. /*!
  310. \brief configure the sample bit method
  311. \param[in] usart_periph: USARTx(x=0,1)
  312. \param[in] osb: sample bit
  313. only one parameter can be selected which is shown as below:
  314. \arg USART_OSB_1BIT: 1 bit
  315. \arg USART_OSB_3BIT: 3 bits
  316. \param[out] none
  317. \retval none
  318. */
  319. void usart_sample_bit_config(uint32_t usart_periph, uint32_t osb)
  320. {
  321. /* disable USART */
  322. USART_CTL0(usart_periph) &= ~(USART_CTL0_UEN);
  323. USART_CTL2(usart_periph) &= ~(USART_CTL2_OSB);
  324. USART_CTL2(usart_periph) |= osb;
  325. }
  326. /*!
  327. \brief enable receiver timeout
  328. \param[in] usart_periph: USARTx(x=0)
  329. \param[out] none
  330. \retval none
  331. */
  332. void usart_receiver_timeout_enable(uint32_t usart_periph)
  333. {
  334. USART_CTL1(usart_periph) |= USART_CTL1_RTEN;
  335. }
  336. /*!
  337. \brief disable receiver timeout
  338. \param[in] usart_periph: USARTx(x=0)
  339. \param[out] none
  340. \retval none
  341. */
  342. void usart_receiver_timeout_disable(uint32_t usart_periph)
  343. {
  344. USART_CTL1(usart_periph) &= ~(USART_CTL1_RTEN);
  345. }
  346. /*!
  347. \brief configure receiver timeout threshold
  348. \param[in] usart_periph: USARTx(x=0)
  349. \param[in] rtimeout: 0x00000000-0x00FFFFFF, receiver timeout value in terms of number of baud clocks
  350. \param[out] none
  351. \retval none
  352. */
  353. void usart_receiver_timeout_threshold_config(uint32_t usart_periph, uint32_t rtimeout)
  354. {
  355. USART_RT(usart_periph) &= ~(USART_RT_RT);
  356. USART_RT(usart_periph) |= rtimeout;
  357. }
  358. /*!
  359. \brief USART transmit data function
  360. \param[in] usart_periph: USARTx(x=0,1)
  361. \param[in] data: data of transmission
  362. \param[out] none
  363. \retval none
  364. */
  365. void usart_data_transmit(uint32_t usart_periph, uint32_t data)
  366. {
  367. USART_TDATA(usart_periph) = (USART_TDATA_TDATA & data);
  368. }
  369. /*!
  370. \brief USART receive data function
  371. \param[in] usart_periph: USARTx(x=0,1)
  372. \param[out] none
  373. \retval data of received
  374. */
  375. uint16_t usart_data_receive(uint32_t usart_periph)
  376. {
  377. return (uint16_t)(GET_BITS(USART_RDATA(usart_periph), 0U, 8U));
  378. }
  379. /*!
  380. \brief address of the USART terminal
  381. \param[in] usart_periph: USARTx(x=0,1)
  382. \param[in] addr: 0x00-0xFF, address of USART terminal
  383. \param[out] none
  384. \retval none
  385. */
  386. void usart_address_config(uint32_t usart_periph, uint8_t addr)
  387. {
  388. /* disable USART */
  389. USART_CTL0(usart_periph) &= ~(USART_CTL0_UEN);
  390. USART_CTL1(usart_periph) &= ~(USART_CTL1_ADDR);
  391. USART_CTL1(usart_periph) |= (USART_CTL1_ADDR & (((uint32_t)addr) << 24));
  392. }
  393. /*!
  394. \brief configure address detection mode
  395. \param[in] usart_periph: USARTx(x=0,1)
  396. \param[in] addmod: address detection mode
  397. only one parameter can be selected which is shown as below:
  398. \arg USART_ADDM_4BIT: 4 bits
  399. \arg USART_ADDM_FULLBIT: full bits
  400. \param[out] none
  401. \retval none
  402. */
  403. void usart_address_detection_mode_config(uint32_t usart_periph, uint32_t addmod)
  404. {
  405. /* disable USART */
  406. USART_CTL0(usart_periph) &= ~(USART_CTL0_UEN);
  407. USART_CTL1(usart_periph) &= ~(USART_CTL1_ADDM);
  408. USART_CTL1(usart_periph) |= USART_CTL1_ADDM & (addmod);
  409. }
  410. /*!
  411. \brief enable mute mode
  412. \param[in] usart_periph: USARTx(x=0,1)
  413. \param[out] none
  414. \retval none
  415. */
  416. void usart_mute_mode_enable(uint32_t usart_periph)
  417. {
  418. USART_CTL0(usart_periph) |= USART_CTL0_MEN;
  419. }
  420. /*!
  421. \brief disable mute mode
  422. \param[in] usart_periph: USARTx(x=0,1)
  423. \param[out] none
  424. \retval none
  425. */
  426. void usart_mute_mode_disable(uint32_t usart_periph)
  427. {
  428. USART_CTL0(usart_periph) &= ~(USART_CTL0_MEN);
  429. }
  430. /*!
  431. \brief configure wakeup method in mute mode
  432. \param[in] usart_periph: USARTx(x=0,1)
  433. \param[in] wmethod: two methods be used to enter or exit the mute mode
  434. only one parameter can be selected which is shown as below:
  435. \arg USART_WM_IDLE: idle line
  436. \arg USART_WM_ADDR: address mark
  437. \param[out] none
  438. \retval none
  439. */
  440. void usart_mute_mode_wakeup_config(uint32_t usart_periph, uint32_t wmethod)
  441. {
  442. /* disable USART */
  443. USART_CTL0(usart_periph) &= ~(USART_CTL0_UEN);
  444. USART_CTL0(usart_periph) &= ~(USART_CTL0_WM);
  445. USART_CTL0(usart_periph) |= wmethod;
  446. }
  447. /*!
  448. \brief enable LIN mode
  449. \param[in] usart_periph: USARTx(x=0)
  450. \param[out] none
  451. \retval none
  452. */
  453. void usart_lin_mode_enable(uint32_t usart_periph)
  454. {
  455. /* disable USART */
  456. USART_CTL0(usart_periph) &= ~(USART_CTL0_UEN);
  457. USART_CTL1(usart_periph) |= USART_CTL1_LMEN;
  458. }
  459. /*!
  460. \brief disable LIN mode
  461. \param[in] usart_periph: USARTx(x=0)
  462. \param[out] none
  463. \retval none
  464. */
  465. void usart_lin_mode_disable(uint32_t usart_periph)
  466. {
  467. /* disable USART */
  468. USART_CTL0(usart_periph) &= ~(USART_CTL0_UEN);
  469. USART_CTL1(usart_periph) &= ~(USART_CTL1_LMEN);
  470. }
  471. /*!
  472. \brief LIN break detection length
  473. \param[in] usart_periph: USARTx(x=0)
  474. \param[in] lblen: LIN break detection length
  475. only one parameter can be selected which is shown as below:
  476. \arg USART_LBLEN_10B: 10 bits break detection
  477. \arg USART_LBLEN_11B: 11 bits break detection
  478. \param[out] none
  479. \retval none
  480. */
  481. void usart_lin_break_detection_length_config(uint32_t usart_periph, uint32_t lblen)
  482. {
  483. /* disable USART */
  484. USART_CTL0(usart_periph) &= ~(USART_CTL0_UEN);
  485. USART_CTL1(usart_periph) &= ~(USART_CTL1_LBLEN);
  486. USART_CTL1(usart_periph) |= USART_CTL1_LBLEN & (lblen);
  487. }
  488. /*!
  489. \brief enable half-duplex mode
  490. \param[in] usart_periph: USARTx(x=0,1)
  491. \param[out] none
  492. \retval none
  493. */
  494. void usart_halfduplex_enable(uint32_t usart_periph)
  495. {
  496. /* disable USART */
  497. USART_CTL0(usart_periph) &= ~(USART_CTL0_UEN);
  498. USART_CTL2(usart_periph) |= USART_CTL2_HDEN;
  499. }
  500. /*!
  501. \brief disable half-duplex mode
  502. \param[in] usart_periph: USARTx(x=0,1)
  503. \param[out] none
  504. \retval none
  505. */
  506. void usart_halfduplex_disable(uint32_t usart_periph)
  507. {
  508. /* disable USART */
  509. USART_CTL0(usart_periph) &= ~(USART_CTL0_UEN);
  510. USART_CTL2(usart_periph) &= ~(USART_CTL2_HDEN);
  511. }
  512. /*!
  513. \brief enable clock
  514. \param[in] usart_periph: USARTx(x=0,1)
  515. \param[out] none
  516. \retval none
  517. */
  518. void usart_clock_enable(uint32_t usart_periph)
  519. {
  520. /* disable USART */
  521. USART_CTL0(usart_periph) &= ~(USART_CTL0_UEN);
  522. USART_CTL1(usart_periph) |= USART_CTL1_CKEN;
  523. }
  524. /*!
  525. \brief disable clock
  526. \param[in] usart_periph: USARTx(x=0,1)
  527. \param[out] none
  528. \retval none
  529. */
  530. void usart_clock_disable(uint32_t usart_periph)
  531. {
  532. /* disable USART */
  533. USART_CTL0(usart_periph) &= ~(USART_CTL0_UEN);
  534. USART_CTL1(usart_periph) &= ~(USART_CTL1_CKEN);
  535. }
  536. /*!
  537. \brief configure USART synchronous mode parameters
  538. \param[in] usart_periph: USARTx(x=0,1)
  539. \param[in] clen: last bit clock pulse
  540. only one parameter can be selected which is shown as below:
  541. \arg USART_CLEN_NONE: clock pulse of the last data bit (MSB) is not output to the CK pin
  542. \arg USART_CLEN_EN: clock pulse of the last data bit (MSB) is output to the CK pin
  543. \param[in] cph: clock phase
  544. only one parameter can be selected which is shown as below:
  545. \arg USART_CPH_1CK: first clock transition is the first data capture edge
  546. \arg USART_CPH_2CK: second clock transition is the first data capture edge
  547. \param[in] cpl: clock polarity
  548. only one parameter can be selected which is shown as below:
  549. \arg USART_CPL_LOW: steady low value on CK pin
  550. \arg USART_CPL_HIGH: steady high value on CK pin
  551. \param[out] none
  552. \retval none
  553. */
  554. void usart_synchronous_clock_config(uint32_t usart_periph, uint32_t clen, uint32_t cph, uint32_t cpl)
  555. {
  556. /* disable USART */
  557. USART_CTL0(usart_periph) &= ~(USART_CTL0_UEN);
  558. /* reset USART_CTL1 CLEN,CPH,CPL bits */
  559. USART_CTL1(usart_periph) &= ~(USART_CTL1_CLEN | USART_CTL1_CPH | USART_CTL1_CPL);
  560. USART_CTL1(usart_periph) |= (USART_CTL1_CLEN & clen);
  561. USART_CTL1(usart_periph) |= (USART_CTL1_CPH & cph);
  562. USART_CTL1(usart_periph) |= (USART_CTL1_CPL & cpl);
  563. }
  564. /*!
  565. \brief configure guard time value in smartcard mode
  566. \param[in] usart_periph: USARTx(x=0)
  567. \param[in] guat: 0x00-0xFF
  568. \param[out] none
  569. \retval none
  570. */
  571. void usart_guard_time_config(uint32_t usart_periph, uint32_t guat)
  572. {
  573. /* disable USART */
  574. USART_CTL0(usart_periph) &= ~(USART_CTL0_UEN);
  575. USART_GP(usart_periph) &= ~(USART_GP_GUAT);
  576. USART_GP(usart_periph) |= (USART_GP_GUAT & ((guat) << 8));
  577. }
  578. /*!
  579. \brief enable smartcard mode
  580. \param[in] usart_periph: USARTx(x=0)
  581. \param[out] none
  582. \retval none
  583. */
  584. void usart_smartcard_mode_enable(uint32_t usart_periph)
  585. {
  586. /* disable USART */
  587. USART_CTL0(usart_periph) &= ~(USART_CTL0_UEN);
  588. USART_CTL2(usart_periph) |= USART_CTL2_SCEN;
  589. }
  590. /*!
  591. \brief disable smartcard mode
  592. \param[in] usart_periph: USARTx(x=0)
  593. \param[out] none
  594. \retval none
  595. */
  596. void usart_smartcard_mode_disable(uint32_t usart_periph)
  597. {
  598. /* disable USART */
  599. USART_CTL0(usart_periph) &= ~(USART_CTL0_UEN);
  600. USART_CTL2(usart_periph) &= ~(USART_CTL2_SCEN);
  601. }
  602. /*!
  603. \brief enable NACK in smartcard mode
  604. \param[in] usart_periph: USARTx(x=0)
  605. \param[out] none
  606. \retval none
  607. */
  608. void usart_smartcard_mode_nack_enable(uint32_t usart_periph)
  609. {
  610. /* disable USART */
  611. USART_CTL0(usart_periph) &= ~(USART_CTL0_UEN);
  612. USART_CTL2(usart_periph) |= USART_CTL2_NKEN;
  613. }
  614. /*!
  615. \brief disable NACK in smartcard mode
  616. \param[in] usart_periph: USARTx(x=0)
  617. \param[out] none
  618. \retval none
  619. */
  620. void usart_smartcard_mode_nack_disable(uint32_t usart_periph)
  621. {
  622. /* disable USART */
  623. USART_CTL0(usart_periph) &= ~(USART_CTL0_UEN);
  624. USART_CTL2(usart_periph) &= ~(USART_CTL2_NKEN);
  625. }
  626. /*!
  627. \brief enable early NACK in smartcard mode
  628. \param[in] usart_periph: USARTx(x=0)
  629. \param[out] none
  630. \retval none
  631. */
  632. void usart_smartcard_mode_early_nack_enable(uint32_t usart_periph)
  633. {
  634. USART_RFCS(usart_periph) |= USART_RFCS_ELNACK;
  635. }
  636. /*!
  637. \brief disable early NACK in smartcard mode
  638. \param[in] usart_periph: USARTx(x=0)
  639. \param[out] none
  640. \retval none
  641. */
  642. void usart_smartcard_mode_early_nack_disable(uint32_t usart_periph)
  643. {
  644. USART_RFCS(usart_periph) &= ~USART_RFCS_ELNACK;
  645. }
  646. /*!
  647. \brief configure smartcard auto-retry number
  648. \param[in] usart_periph: USARTx(x=0)
  649. \param[in] scrtnum: 0x00000000-0x00000007, smartcard auto-retry number
  650. \param[out] none
  651. \retval none
  652. */
  653. void usart_smartcard_autoretry_config(uint32_t usart_periph, uint32_t scrtnum)
  654. {
  655. /* disable USART */
  656. USART_CTL0(usart_periph) &= ~(USART_CTL0_UEN);
  657. USART_CTL2(usart_periph) &= ~(USART_CTL2_SCRTNUM);
  658. USART_CTL2(usart_periph) |= (USART_CTL2_SCRTNUM & (scrtnum << 17));
  659. }
  660. /*!
  661. \brief configure block length
  662. \param[in] usart_periph: USARTx(x=0)
  663. \param[in] bl: 0x00000000-0x000000FF
  664. \param[out] none
  665. \retval none
  666. */
  667. void usart_block_length_config(uint32_t usart_periph, uint32_t bl)
  668. {
  669. USART_RT(usart_periph) &= ~(USART_RT_BL);
  670. USART_RT(usart_periph) |= (USART_RT_BL & ((bl) << 24));
  671. }
  672. /*!
  673. \brief enable IrDA mode
  674. \param[in] usart_periph: USARTx(x=0)
  675. \param[out] none
  676. \retval none
  677. */
  678. void usart_irda_mode_enable(uint32_t usart_periph)
  679. {
  680. /* disable USART */
  681. USART_CTL0(usart_periph) &= ~(USART_CTL0_UEN);
  682. USART_CTL2(usart_periph) |= USART_CTL2_IREN;
  683. }
  684. /*!
  685. \brief disable IrDA mode
  686. \param[in] usart_periph: USARTx(x=0)
  687. \param[out] none
  688. \retval none
  689. */
  690. void usart_irda_mode_disable(uint32_t usart_periph)
  691. {
  692. /* disable USART */
  693. USART_CTL0(usart_periph) &= ~(USART_CTL0_UEN);
  694. USART_CTL2(usart_periph) &= ~(USART_CTL2_IREN);
  695. }
  696. /*!
  697. \brief configure the peripheral clock prescaler in USART IrDA low-power or SmartCard mode
  698. \param[in] usart_periph: USARTx(x=0)
  699. \param[in] psc: 0x00000000-0x000000FF
  700. \param[out] none
  701. \retval none
  702. */
  703. void usart_prescaler_config(uint32_t usart_periph, uint32_t psc)
  704. {
  705. /* disable USART */
  706. USART_CTL0(usart_periph) &= ~(USART_CTL0_UEN);
  707. USART_GP(usart_periph) &= ~(USART_GP_PSC);
  708. USART_GP(usart_periph) |= psc;
  709. }
  710. /*!
  711. \brief configure IrDA low-power
  712. \param[in] usart_periph: USARTx(x=0)
  713. \param[in] irlp: IrDA low-power or normal
  714. only one parameter can be selected which is shown as below:
  715. \arg USART_IRLP_LOW: low-power
  716. \arg USART_IRLP_NORMAL: normal
  717. \param[out] none
  718. \retval none
  719. */
  720. void usart_irda_lowpower_config(uint32_t usart_periph, uint32_t irlp)
  721. {
  722. /* disable USART */
  723. USART_CTL0(usart_periph) &= ~(USART_CTL0_UEN);
  724. USART_CTL2(usart_periph) &= ~(USART_CTL2_IRLP);
  725. USART_CTL2(usart_periph) |= (USART_CTL2_IRLP & irlp);
  726. }
  727. /*!
  728. \brief configure hardware flow control RTS
  729. \param[in] usart_periph: USARTx(x=0,1)
  730. \param[in] rtsconfig: enable or disable RTS
  731. only one parameter can be selected which is shown as below:
  732. \arg USART_RTS_ENABLE: enable RTS
  733. \arg USART_RTS_DISABLE: disable RTS
  734. \param[out] none
  735. \retval none
  736. */
  737. void usart_hardware_flow_rts_config(uint32_t usart_periph, uint32_t rtsconfig)
  738. {
  739. /* disable USART */
  740. USART_CTL0(usart_periph) &= ~(USART_CTL0_UEN);
  741. USART_CTL2(usart_periph) &= ~(USART_CTL2_RTSEN);
  742. USART_CTL2(usart_periph) |= rtsconfig;
  743. }
  744. /*!
  745. \brief configure hardware flow control CTS
  746. \param[in] usart_periph: USARTx(x=0,1)
  747. \param[in] ctsconfig: enable or disable CTS
  748. only one parameter can be selected which is shown as below:
  749. \arg USART_CTS_ENABLE: enable CTS
  750. \arg USART_CTS_DISABLE: disable CTS
  751. \param[out] none
  752. \retval none
  753. */
  754. void usart_hardware_flow_cts_config(uint32_t usart_periph, uint32_t ctsconfig)
  755. {
  756. /* disable USART */
  757. USART_CTL0(usart_periph) &= ~(USART_CTL0_UEN);
  758. USART_CTL2(usart_periph) &= ~USART_CTL2_CTSEN;
  759. USART_CTL2(usart_periph) |= ctsconfig;
  760. }
  761. /*!
  762. \brief configure hardware flow control coherence mode
  763. \param[in] usart_periph: USARTx(x=0,1)
  764. \param[in] hcm:
  765. only one parameter can be selected which is shown as below:
  766. \arg USART_HCM_NONE: nRTS signal equals to the rxne status register
  767. \arg USART_HCM_EN: nRTS signal is set when the last data bit has been sampled
  768. \param[out] none
  769. \retval none
  770. */
  771. void usart_hardware_flow_coherence_config(uint32_t usart_periph, uint32_t hcm)
  772. {
  773. USART_CHC(usart_periph) &= ~(USART_CHC_HCM);
  774. USART_CHC(usart_periph) |= (USART_CHC_HCM & hcm);
  775. }
  776. /*!
  777. \brief enable RS485 driver
  778. \param[in] usart_periph: USARTx(x=0,1)
  779. \param[out] none
  780. \retval none
  781. */
  782. void usart_rs485_driver_enable(uint32_t usart_periph)
  783. {
  784. /* disable USART */
  785. USART_CTL0(usart_periph) &= ~(USART_CTL0_UEN);
  786. USART_CTL2(usart_periph) |= USART_CTL2_DEM;
  787. }
  788. /*!
  789. \brief disable RS485 driver
  790. \param[in] usart_periph: USARTx(x=0,1)
  791. \param[out] none
  792. \retval none
  793. */
  794. void usart_rs485_driver_disable(uint32_t usart_periph)
  795. {
  796. /* disable USART */
  797. USART_CTL0(usart_periph) &= ~(USART_CTL0_UEN);
  798. USART_CTL2(usart_periph) &= ~(USART_CTL2_DEM);
  799. }
  800. /*!
  801. \brief configure driver enable assertion time
  802. \param[in] usart_periph: USARTx(x=0,1)
  803. \param[in] deatime: 0x00000000-0x0000001F
  804. \param[out] none
  805. \retval none
  806. */
  807. void usart_driver_assertime_config(uint32_t usart_periph, uint32_t deatime)
  808. {
  809. /* disable USART */
  810. USART_CTL0(usart_periph) &= ~(USART_CTL0_UEN);
  811. USART_CTL0(usart_periph) &= ~(USART_CTL0_DEA);
  812. USART_CTL0(usart_periph) |= (USART_CTL0_DEA & ((deatime) << 21));
  813. }
  814. /*!
  815. \brief configure driver enable de-assertion time
  816. \param[in] usart_periph: USARTx(x=0,1)
  817. \param[in] dedtime: 0x00000000-0x0000001F
  818. \param[out] none
  819. \retval none
  820. */
  821. void usart_driver_deassertime_config(uint32_t usart_periph, uint32_t dedtime)
  822. {
  823. /* disable USART */
  824. USART_CTL0(usart_periph) &= ~(USART_CTL0_UEN);
  825. USART_CTL0(usart_periph) &= ~(USART_CTL0_DED);
  826. USART_CTL0(usart_periph) |= (USART_CTL0_DED & ((dedtime) << 16));
  827. }
  828. /*!
  829. \brief configure driver enable polarity mode
  830. \param[in] usart_periph: USARTx(x=0,1)
  831. \param[in] dep: DE signal
  832. only one parameter can be selected which is shown as below:
  833. \arg USART_DEP_HIGH: DE signal is active high
  834. \arg USART_DEP_LOW: DE signal is active low
  835. \param[out] none
  836. \retval none
  837. */
  838. void usart_depolarity_config(uint32_t usart_periph, uint32_t dep)
  839. {
  840. /* disable USART */
  841. USART_CTL0(usart_periph) &= ~(USART_CTL0_UEN);
  842. /* reset DEP bit */
  843. USART_CTL2(usart_periph) &= ~(USART_CTL2_DEP);
  844. USART_CTL2(usart_periph) |= (USART_CTL2_DEP & dep);
  845. }
  846. /*!
  847. \brief configure USART DMA reception
  848. \param[in] usart_periph: USARTx(x=0,1)
  849. \param[in] dmacmd: enable or disable DMA for reception
  850. only one parameter can be selected which is shown as below:
  851. \arg USART_DENR_ENABLE: DMA enable for reception
  852. \arg USART_DENR_DISABLE: DMA disable for reception
  853. \param[out] none
  854. \retval none
  855. */
  856. void usart_dma_receive_config(uint32_t usart_periph, uint32_t dmacmd)
  857. {
  858. USART_CTL2(usart_periph) &= ~USART_CTL2_DENR;
  859. /* configure DMA reception */
  860. USART_CTL2(usart_periph) |= dmacmd;
  861. }
  862. /*!
  863. \brief configure USART DMA transmission
  864. \param[in] usart_periph: USARTx(x=0,1)
  865. \param[in] dmacmd: enable or disable DMA for transmission
  866. only one parameter can be selected which is shown as below:
  867. \arg USART_DENT_ENABLE: DMA enable for transmission
  868. \arg USART_DENT_DISABLE: DMA disable for transmission
  869. \param[out] none
  870. \retval none
  871. */
  872. void usart_dma_transmit_config(uint32_t usart_periph, uint32_t dmacmd)
  873. {
  874. USART_CTL2(usart_periph) &= ~USART_CTL2_DENT;
  875. /* configure DMA transmission */
  876. USART_CTL2(usart_periph) |= dmacmd;
  877. }
  878. /*!
  879. \brief disable DMA on reception error
  880. \param[in] usart_periph: USARTx(x=0,1)
  881. \param[out] none
  882. \retval none
  883. */
  884. void usart_reception_error_dma_disable(uint32_t usart_periph)
  885. {
  886. /* disable USART */
  887. USART_CTL0(usart_periph) &= ~(USART_CTL0_UEN);
  888. USART_CTL2(usart_periph) |= USART_CTL2_DDRE;
  889. }
  890. /*!
  891. \brief enable DMA on reception error
  892. \param[in] usart_periph: USARTx(x=0,1)
  893. \param[out] none
  894. \retval none
  895. */
  896. void usart_reception_error_dma_enable(uint32_t usart_periph)
  897. {
  898. /* disable USART */
  899. USART_CTL0(usart_periph) &= ~(USART_CTL0_UEN);
  900. USART_CTL2(usart_periph) &= ~(USART_CTL2_DDRE);
  901. }
  902. /*!
  903. \brief enable USART to wakeup the mcu from deep-sleep mode
  904. \param[in] usart_periph: USARTx(x=0)
  905. \param[out] none
  906. \retval none
  907. */
  908. void usart_wakeup_enable(uint32_t usart_periph)
  909. {
  910. USART_CTL0(usart_periph) |= USART_CTL0_UESM;
  911. }
  912. /*!
  913. \brief disable USART to wakeup the mcu from deep-sleep mode
  914. \param[in] usart_periph: USARTx(x=0)
  915. \param[out] none
  916. \retval none
  917. */
  918. void usart_wakeup_disable(uint32_t usart_periph)
  919. {
  920. USART_CTL0(usart_periph) &= ~(USART_CTL0_UESM);
  921. }
  922. /*!
  923. \brief configure the USART wakeup mode from deep-sleep mode
  924. \param[in] usart_periph: USARTx(x=0)
  925. \param[in] wum: wakeup mode
  926. only one parameter can be selected which is shown as below:
  927. \arg USART_WUM_ADDR: WUF active on address match
  928. \arg USART_WUM_STARTB: WUF active on start bit
  929. \arg USART_WUM_RBNE: WUF active on RBNE
  930. \param[out] none
  931. \retval none
  932. */
  933. void usart_wakeup_mode_config(uint32_t usart_periph, uint32_t wum)
  934. {
  935. /* disable USART */
  936. USART_CTL0(usart_periph) &= ~(USART_CTL0_UEN);
  937. /* reset WUM bit */
  938. USART_CTL2(usart_periph) &= ~(USART_CTL2_WUM);
  939. USART_CTL2(usart_periph) |= USART_CTL2_WUM & (wum);
  940. }
  941. /*!
  942. \brief enable receive FIFO
  943. \param[in] usart_periph: USARTx(x=0,1)
  944. \param[out] none
  945. \retval none
  946. */
  947. void usart_receive_fifo_enable(uint32_t usart_periph)
  948. {
  949. USART_RFCS(usart_periph) |= USART_RFCS_RFEN;
  950. }
  951. /*!
  952. \brief disable receive FIFO
  953. \param[in] usart_periph: USARTx(x=0,1)
  954. \param[out] none
  955. \retval none
  956. */
  957. void usart_receive_fifo_disable(uint32_t usart_periph)
  958. {
  959. USART_RFCS(usart_periph) &= ~(USART_RFCS_RFEN);
  960. }
  961. /*!
  962. \brief read receive FIFO counter number
  963. \param[in] usart_periph: USARTx(x=0,1)
  964. \param[out] none
  965. \retval receive FIFO counter number
  966. */
  967. uint8_t usart_receive_fifo_counter_number(uint32_t usart_periph)
  968. {
  969. return (uint8_t)(GET_BITS(USART_RFCS(usart_periph), 12U, 14U));
  970. }
  971. /*!
  972. \brief get flag in STAT/CHC/RFCS register
  973. \param[in] usart_periph: USARTx(x=0,1)
  974. \param[in] flag: flag type
  975. only one parameter can be selected which is shown as below:
  976. \arg USART_FLAG_PERR: parity error flag
  977. \arg USART_FLAG_FERR: frame error flag
  978. \arg USART_FLAG_NERR: noise error flag
  979. \arg USART_FLAG_ORERR: overrun error
  980. \arg USART_FLAG_IDLE: idle line detected flag
  981. \arg USART_FLAG_RBNE: read data buffer not empty
  982. \arg USART_FLAG_TC: transmission completed
  983. \arg USART_FLAG_TBE: transmit data register empty
  984. \arg USART_FLAG_LBD: LIN break detected flag
  985. \arg USART_FLAG_CTSF: CTS change flag
  986. \arg USART_FLAG_CTS: CTS level
  987. \arg USART_FLAG_RT: receiver timeout flag
  988. \arg USART_FLAG_EB: end of block flag
  989. \arg USART_FLAG_BSY: busy flag
  990. \arg USART_FLAG_AM: address match flag
  991. \arg USART_FLAG_SB: send break flag
  992. \arg USART_FLAG_RWU: receiver wakeup from mute mode.
  993. \arg USART_FLAG_WU: wakeup from deep-sleep mode flag
  994. \arg USART_FLAG_TEA: transmit enable acknowledge flag
  995. \arg USART_FLAG_REA: receive enable acknowledge flag
  996. \arg USART_FLAG_EPERR: early parity error flag
  997. \arg USART_FLAG_RFE: receive FIFO empty flag
  998. \arg USART_FLAG_RFF: receive FIFO full flag
  999. \arg USART_FLAG_RFFINT: receive FIFO full interrupt flag
  1000. \param[out] none
  1001. \retval FlagStatus: SET or RESET
  1002. */
  1003. FlagStatus usart_flag_get(uint32_t usart_periph, usart_flag_enum flag)
  1004. {
  1005. if(RESET != (USART_REG_VAL(usart_periph, flag) & BIT(USART_BIT_POS(flag)))){
  1006. return SET;
  1007. }else{
  1008. return RESET;
  1009. }
  1010. }
  1011. /*!
  1012. \brief clear USART status
  1013. \param[in] usart_periph: USARTx(x=0,1)
  1014. \param[in] flag: flag type
  1015. only one parameter can be selected which is shown as below:
  1016. \arg USART_FLAG_PERR: parity error flag
  1017. \arg USART_FLAG_FERR: frame error flag
  1018. \arg USART_FLAG_NERR: noise detected flag
  1019. \arg USART_FLAG_ORERR: overrun error flag
  1020. \arg USART_FLAG_IDLE: idle line detected flag
  1021. \arg USART_FLAG_TC: transmission complete flag
  1022. \arg USART_FLAG_LBD: LIN break detected flag
  1023. \arg USART_FLAG_CTSF: CTS change flag
  1024. \arg USART_FLAG_RT: receiver timeout flag
  1025. \arg USART_FLAG_EB: end of block flag
  1026. \arg USART_FLAG_AM: address match flag
  1027. \arg USART_FLAG_WU: wakeup from deep-sleep mode flag
  1028. \arg USART_FLAG_EPERR: early parity error flag
  1029. \param[out] none
  1030. \retval none
  1031. */
  1032. void usart_flag_clear(uint32_t usart_periph, usart_flag_enum flag)
  1033. {
  1034. USART_INTC(usart_periph) |= BIT(USART_BIT_POS(flag));
  1035. }
  1036. /*!
  1037. \brief enable USART interrupt
  1038. \param[in] usart_periph: USARTx(x=0,1)
  1039. \param[in] inttype: interrupt type
  1040. only one parameter can be selected which is shown as below:
  1041. \arg USART_INT_IDLE: idle interrupt
  1042. \arg USART_INT_RBNE: read data buffer not empty interrupt and
  1043. overrun error interrupt enable interrupt
  1044. \arg USART_INT_TC: transmission complete interrupt
  1045. \arg USART_INT_TBE: transmit data register empty interrupt
  1046. \arg USART_INT_PERR: parity error interrupt
  1047. \arg USART_INT_AM: address match interrupt
  1048. \arg USART_INT_RT: receiver timeout interrupt
  1049. \arg USART_INT_EB: end of block interrupt
  1050. \arg USART_INT_LBD: LIN break detection interrupt
  1051. \arg USART_INT_ERR: error interrupt enable in multibuffer communication
  1052. \arg USART_INT_CTS: CTS interrupt
  1053. \arg USART_INT_WU: wakeup from deep-sleep mode interrupt
  1054. \arg USART_INT_RFF: receive FIFO full interrupt enable
  1055. \param[out] none
  1056. \retval none
  1057. */
  1058. void usart_interrupt_enable(uint32_t usart_periph, usart_interrupt_enum interrupt)
  1059. {
  1060. USART_REG_VAL(usart_periph, interrupt) |= BIT(USART_BIT_POS(interrupt));
  1061. }
  1062. /*!
  1063. \brief disable USART interrupt
  1064. \param[in] usart_periph: USARTx(x=0,1)
  1065. \param[in] inttype: interrupt type
  1066. only one parameter can be selected which is shown as below:
  1067. \arg USART_INT_IDLE: idle interrupt
  1068. \arg USART_INT_RBNE: read data buffer not empty interrupt and
  1069. overrun error interrupt
  1070. \arg USART_INT_TC: transmission complete interrupt
  1071. \arg USART_INT_TBE: transmit data register empty interrupt
  1072. \arg USART_INT_PERR: parity error interrupt
  1073. \arg USART_INT_AM: address match interrupt
  1074. \arg USART_INT_RT: receiver timeout interrupt
  1075. \arg USART_INT_EB: end of block interrupt
  1076. \arg USART_INT_LBD: LIN break detection interrupt
  1077. \arg USART_INT_ERR: error interrupt enable in multibuffer communication
  1078. \arg USART_INT_CTS: CTS interrupt
  1079. \arg USART_INT_WU: wakeup from deep-sleep mode interrupt
  1080. \arg USART_INT_RFF: receive FIFO full interrupt enable
  1081. \param[out] none
  1082. \retval none
  1083. */
  1084. void usart_interrupt_disable(uint32_t usart_periph, usart_interrupt_enum interrupt)
  1085. {
  1086. USART_REG_VAL(usart_periph, interrupt) &= ~BIT(USART_BIT_POS(interrupt));
  1087. }
  1088. /*!
  1089. \brief enable USART command
  1090. \param[in] usart_periph: USARTx(x=0,1)
  1091. \param[in] cmdtype: command type
  1092. only one parameter can be selected which is shown as below:
  1093. \arg USART_CMD_SBKCMD: send break command
  1094. \arg USART_CMD_MMCMD: mute mode command
  1095. \arg USART_CMD_RXFCMD: receive data flush command
  1096. \arg USART_CMD_TXFCMD: transmit data flush request
  1097. \param[out] none
  1098. \retval none
  1099. */
  1100. void usart_command_enable(uint32_t usart_periph, uint32_t cmdtype)
  1101. {
  1102. USART_CMD(usart_periph) |= (cmdtype);
  1103. }
  1104. /*!
  1105. \brief get USART interrupt and flag status
  1106. \param[in] usart_periph: USARTx(x=0,1)
  1107. \param[in] int_flag: interrupt and flag type, refer to usart_interrupt_flag_enum
  1108. only one parameter can be selected which is shown as below:
  1109. \arg USART_INT_FLAG_EB: end of block interrupt and flag
  1110. \arg USART_INT_FLAG_RT: receiver timeout interrupt and flag
  1111. \arg USART_INT_FLAG_AM: address match interrupt and flag
  1112. \arg USART_INT_FLAG_PERR: parity error interrupt and flag
  1113. \arg USART_INT_FLAG_TBE: transmitter buffer empty interrupt and flag
  1114. \arg USART_INT_FLAG_TC: transmission complete interrupt and flag
  1115. \arg USART_INT_FLAG_RBNE: read data buffer not empty interrupt and flag
  1116. \arg USART_INT_FLAG_RBNE_ORERR: read data buffer not empty interrupt and overrun error flag
  1117. \arg USART_INT_FLAG_IDLE: IDLE line detected interrupt and flag
  1118. \arg USART_INT_FLAG_LBD: LIN break detected interrupt and flag
  1119. \arg USART_INT_FLAG_WU: wakeup from deep-sleep mode interrupt and flag
  1120. \arg USART_INT_FLAG_CTS: CTS interrupt and flag
  1121. \arg USART_INT_FLAG_ERR_NERR: error interrupt and noise error flag
  1122. \arg USART_INT_FLAG_ERR_ORERR: error interrupt and overrun error
  1123. \arg USART_INT_FLAG_ERR_FERR: error interrupt and frame error flag
  1124. \arg USART_INT_FLAG_RFF: receive FIFO full interrupt and flag
  1125. \param[out] none
  1126. \retval FlagStatus: SET or RESET
  1127. */
  1128. FlagStatus usart_interrupt_flag_get(uint32_t usart_periph, usart_interrupt_flag_enum int_flag)
  1129. {
  1130. uint32_t intenable = 0U, flagstatus = 0U;
  1131. /* get the interrupt enable bit status */
  1132. intenable = (USART_REG_VAL(usart_periph, int_flag) & BIT(USART_BIT_POS(int_flag)));
  1133. /* get the corresponding flag bit status */
  1134. flagstatus = (USART_REG_VAL2(usart_periph, int_flag) & BIT(USART_BIT_POS2(int_flag)));
  1135. if(flagstatus && intenable){
  1136. return SET;
  1137. }else{
  1138. return RESET;
  1139. }
  1140. }
  1141. /*!
  1142. \brief clear USART interrupt flag
  1143. \param[in] usart_periph: USARTx(x=0,1)
  1144. \param[in] flag: USART interrupt flag
  1145. only one parameter can be selected which is shown as below:
  1146. \arg USART_INT_FLAG_PERR: parity error flag
  1147. \arg USART_INT_FLAG_ERR_FERR: frame error flag
  1148. \arg USART_INT_FLAG_ERR_NERR: noise detected flag
  1149. \arg USART_INT_FLAG_RBNE_ORERR: read data buffer not empty interrupt and overrun error flag
  1150. \arg USART_INT_FLAG_ERR_ORERR: error interrupt and overrun error
  1151. \arg USART_INT_FLAG_IDLE: idle line detected flag
  1152. \arg USART_INT_FLAG_TC: transmission complete flag
  1153. \arg USART_INT_FLAG_LBD: LIN break detected flag
  1154. \arg USART_INT_FLAG_CTS: CTS change flag
  1155. \arg USART_INT_FLAG_RT: receiver timeout flag
  1156. \arg USART_INT_FLAG_EB: end of block flag
  1157. \arg USART_INT_FLAG_AM: address match flag
  1158. \arg USART_INT_FLAG_WU: wakeup from deep-sleep mode flag
  1159. \arg USART_INT_FLAG_RFF: receive FIFO full interrupt and flag
  1160. \param[out] none
  1161. \retval none
  1162. */
  1163. void usart_interrupt_flag_clear(uint32_t usart_periph, usart_interrupt_flag_enum flag)
  1164. {
  1165. if(USART_INT_FLAG_RFF == flag){
  1166. USART_RFCS(usart_periph) &= (uint32_t)(~USART_RFCS_RFFINT);
  1167. }else{
  1168. USART_INTC(usart_periph) |= BIT(USART_BIT_POS2(flag));
  1169. }
  1170. }