gd32f3x0_usart.c 36 KB

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