gd32e23x_usart.c 41 KB

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