gd32f4xx_usart.c 33 KB

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