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