gd32f3x0_usart.c 42 KB

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