| Nano103 BSP V3.01.002 The Board Support Package for Nano103 Series

uart.c
Go to the documentation of this file.
1 /**************************************************************************/
13 #include <stdio.h>
14 #include "Nano103.h"
15
24
31 uint32_t _UART_GetUartClk(UART_T* uart)
32 {
33 uint8_t u8UartClkSrcSel = 0;
34 uint32_t clk =0, div = 1;
35
36 if(uart == UART0)
37 {
38 u8UartClkSrcSel = (CLK->CLKSEL1 & CLK_CLKSEL1_UART0SEL_Msk) >> CLK_CLKSEL1_UART0SEL_Pos;
39 div = ( (CLK->CLKDIV0 & CLK_CLKDIV0_UART0DIV_Msk) >> CLK_CLKDIV0_UART0DIV_Pos) + 1; /* Get uart clock divider */
40 }
41 else if(uart == UART1)
42 {
43 u8UartClkSrcSel = (CLK->CLKSEL2 & CLK_CLKSEL2_UART1SEL_Msk) >> CLK_CLKSEL2_UART1SEL_Pos;
44 div = ( (CLK->CLKDIV0 & CLK_CLKDIV0_UART1DIV_Msk) >> CLK_CLKDIV0_UART1DIV_Pos) + 1; /* Get uart clock divider */
45 }
46
47 switch (u8UartClkSrcSel) /* Get uart selected clock source */
48 {
49 case 0:
50 clk = __HXT; /* HXT */
51 break;
52 case 1:
53 clk = __LXT; /* LXT */
54 break;
55 case 2:
56 clk = SysGet_PLLClockFreq(); /* PLL */
57 break;
58 case 3:
59 if(CLK->CLKSEL0 & CLK_CLKSEL0_HIRCSEL_Msk)
60 clk = __HIRC36M; /* HIRC 36M Hz*/
61 else
62 {
63 if(CLK->PWRCTL & CLK_PWRCTL_HIRC0FSEL_Msk)
64 clk = __HIRC16M; /* HIRC 16M Hz*/
65 else
66 clk = __HIRC12M; /* HIRC 12M Hz*/
67 }
68 break;
69
70 default:
71 clk = __MIRC;
72 break;
73
74 }
75
76 clk /= div; /* calculate uart clock */
77
78 return clk;
79 }
80
82
87 extern uint32_t SysGet_PLLClockFreq(void);
88
89
90
99 void UART_ClearIntFlag(UART_T* uart, uint32_t u32InterruptFlag)
100 {
101
102 if(u32InterruptFlag & UART_INTSTS_RLSIF_Msk) /* clear Receive Line Status Interrupt */
103 {
104 uart->FIFOSTS = UART_FIFOSTS_BIF_Msk | UART_FIFOSTS_FEF_Msk | UART_FIFOSTS_PEF_Msk;
105 uart->TRSR = UART_TRSR_ADDRDETF_Msk;
106 }
107
108 if(u32InterruptFlag & UART_INTSTS_MODEMIF_Msk) /* clear Modem Interrupt */
109 uart->MODEM |= UART_MODEM_CTSDETF_Msk;
110
111 if(u32InterruptFlag & UART_INTSTS_BUFERRIF_Msk) /* clear Buffer Error Interrupt */
112 {
113 uart->FIFOSTS = UART_FIFOSTS_RXOVIF_Msk | UART_FIFOSTS_TXOVIF_Msk;
114 }
115
116 if(u32InterruptFlag & UART_INTSTS_WKUPIF_Msk) /* clear wake up Interrupt */
117 {
118 uart->INTSTS = UART_INTSTS_WKUPIF_Msk;
119 }
120
121 if(u32InterruptFlag & UART_INTSTS_ABRIF_Msk) /* clear auto-baud rate Interrupt */
122 {
123 uart->TRSR = UART_TRSR_ABRDTOIF_Msk | UART_TRSR_ABRDIF_Msk;
124 }
125
126 if(u32InterruptFlag & UART_INTSTS_LINIF_Msk) /* clear LIN break Interrupt */
127 {
128 uart->TRSR = UART_TRSR_LINTXIF_Msk | UART_TRSR_LINRXIF_Msk | UART_TRSR_BITEF_Msk;
129 }
130
131 }
132
133
141 void UART_Close(UART_T* uart)
142 {
143 uart->INTEN = 0;
144 }
145
146
154 void UART_DisableFlowCtrl(UART_T* uart)
155 {
156 uart->CTRL &= ~(UART_CTRL_ATORTSEN_Msk | UART_CTRL_ATOCTSEN_Msk);
157 }
158
159
178 void UART_DisableInt(UART_T* uart, uint32_t u32InterruptFlag )
179 {
180 uart->INTEN &= ~ u32InterruptFlag;
181 }
182
183
184
192 void UART_EnableFlowCtrl(UART_T* uart )
193 {
194 uart->MODEM |= UART_MODEM_RTSACTLV_Msk | UART_MODEM_CTSACTLV_Msk;
195 uart->CTRL |= UART_CTRL_ATORTSEN_Msk | UART_CTRL_ATOCTSEN_Msk;
196 }
197
198
217 void UART_EnableInt(UART_T* uart, uint32_t u32InterruptFlag )
218 {
219 uart->INTEN |= u32InterruptFlag;
220 }
221
222
231 void UART_Open(UART_T* uart, uint32_t u32baudrate)
232 {
233 uint32_t u32Baud_Div;
234 uint32_t u32SrcFreq;
235
236 u32SrcFreq = _UART_GetUartClk(uart);
237
238 uart->CTRL &= ~(UART_CTRL_RXOFF_Msk | UART_CTRL_TXOFF_Msk);
239
240 uart->FUNCSEL = UART_FUNCSEL_UART;
241 uart->LINE = UART_WORD_LEN_8 | UART_PARITY_NONE | UART_STOP_BIT_1 |
242 UART_LINE_RFITL_1BYTE | UART_LINE_RTS_TRI_LEV_1BYTE;
243
244 if(u32baudrate != 0)
245 {
246 u32Baud_Div = UART_BAUD_MODE0_DIVIDER(u32SrcFreq, u32baudrate);
247
248 if(u32Baud_Div > 0xFFFF)
249 uart->BAUD = (UART_BAUD_MODE1 | UART_BAUD_MODE1_DIVIDER(u32SrcFreq, u32baudrate));
250 else
251 uart->BAUD = (UART_BAUD_MODE0 | u32Baud_Div);
252 }
253 }
254
255
266 uint32_t UART_Read(UART_T* uart, uint8_t *pu8RxBuf, uint32_t u32ReadBytes)
267 {
268 uint32_t u32Count, u32delayno;
269
270 for(u32Count=0; u32Count < u32ReadBytes; u32Count++)
271 {
272 u32delayno = 0;
273
274 while(uart->FIFOSTS & UART_FIFOSTS_RXEMPTY_Msk) /* Check RX empty => failed */
275 {
276 u32delayno++;
277 if( u32delayno >= 0x40000000 )
278 return FALSE;
279 }
280 pu8RxBuf[u32Count] = uart->DAT; /* Get Data from UART RX */
281 }
282
283 return u32Count;
284
285 }
286
287
300 void UART_SetLine_Config(UART_T* uart, uint32_t u32baudrate, uint32_t u32data_width, uint32_t u32parity, uint32_t u32stop_bits)
301 {
302 uint32_t u32Baud_Div = 0;
303 uint32_t u32SrcFreq;
304
305 u32SrcFreq = _UART_GetUartClk(uart);
306
307 if(u32baudrate != 0)
308 {
309 u32Baud_Div = UART_BAUD_MODE0_DIVIDER(u32SrcFreq, u32baudrate);
310
311 if(u32Baud_Div > 0xFFFF)
312 uart->BAUD = (UART_BAUD_MODE1 | UART_BAUD_MODE1_DIVIDER(u32SrcFreq, u32baudrate));
313 else
314 uart->BAUD = (UART_BAUD_MODE0 | u32Baud_Div);
315 }
316
317 uart->LINE = u32data_width | u32parity | u32stop_bits;
318 }
319
320
329 void UART_SetTimeoutCnt(UART_T* uart, uint32_t u32TOC)
330 {
331 uart->TOUT = (uart->TOUT & ~UART_TOUT_TOIC_Msk)| (u32TOC);
332 uart->INTEN |= UART_INTEN_RXTOIEN_Msk;
333 }
334
335
345 void UART_SelectIrDAMode(UART_T* uart, uint32_t u32Buadrate, uint32_t u32Direction)
346 {
347 uint32_t u32SrcFreq;
348
349 u32SrcFreq = _UART_GetUartClk(uart);
350
351 uart->BAUD = UART_BAUD_MODE1 | UART_BAUD_MODE1_DIVIDER(u32SrcFreq, u32Buadrate);
352
353 uart->IRDA &= ~UART_IRDA_TXINV_Msk;
354 uart->IRDA |= UART_IRDA_RXINV_Msk;
355 uart->IRDA = u32Direction ? uart->IRDA | UART_IRDA_TXEN_Msk : uart->IRDA &~ UART_IRDA_TXEN_Msk;
356 uart->FUNCSEL = (0x2 << UART_FUNCSEL_FUNCSEL_Pos);
357 }
358
359
369 void UART_SelectRS485Mode(UART_T* uart, uint32_t u32Mode, uint32_t u32Addr)
370 {
371 uart->FUNCSEL = UART_FUNCSEL_RS485;
372 uart->ALTCTL = 0;
373 uart->ALTCTL |= u32Mode | (u32Addr << UART_ALTCTL_ADRMPID_Pos);
374 }
375
390 void UART_SelectLINMode(UART_T* uart, uint32_t u32Mode, uint32_t u32BreakLength)
391 {
392 /* Select LIN function mode */
393 uart->FUNCSEL = UART_FUNCSEL_LIN;
394
395 /* Select LIN function setting : Tx enable, Rx enable and break field length */
396 uart->FUNCSEL = UART_FUNCSEL_LIN;
397 uart->ALTCTL &= ~(UART_ALTCTL_BRKFL_Msk | UART_ALTCTL_LINRXEN_Msk | UART_ALTCTL_LINTXEN_Msk);
398 uart->ALTCTL |= u32BreakLength & UART_ALTCTL_BRKFL_Msk;
399 uart->ALTCTL |= u32Mode;
400 }
401
411 uint32_t UART_Write(UART_T* uart,uint8_t *pu8TxBuf, uint32_t u32WriteBytes)
412 {
413 uint32_t u32Count, u32delayno;
414
415 for(u32Count=0; u32Count != u32WriteBytes; u32Count++)
416 {
417 u32delayno = 0;
418 while((uart->FIFOSTS & UART_FIFOSTS_TXEMPTY_Msk) == 0) /* Wait Tx empty and Time-out manner */
419 {
420 u32delayno++;
421 if( u32delayno >= 0x40000000 )
422 return FALSE;
423 }
424 uart->DAT = pu8TxBuf[u32Count]; /* Send UART Data from buffer */
425 }
426
427 return u32Count;
428
429 }
430
431 /* end of group NANO103_UART_EXPORTED_FUNCTIONS */
433 /* end of group NANO103_UART_Driver */
435 /* end of group NANO103_Device_Driver */
437
438 /*** (C) COPYRIGHT 2015 Nuvoton Technology Corp. ***/
439
440
441
UART_BAUD_MODE1_DIVIDER
#define UART_BAUD_MODE1_DIVIDER(u32SrcFreq, u32BaudRate)
Calculate UART baudrate mode0 divider.
Definition: uart.h:116
UART_T::ALTCTL
__IO uint32_t ALTCTL
Definition: Nano103.h:20117
UART_LINE_RFITL_1BYTE
#define UART_LINE_RFITL_1BYTE
Definition: uart.h:53
UART_CTRL_ATOCTSEN_Msk
#define UART_CTRL_ATOCTSEN_Msk
Definition: Nano103.h:20149
CLK_PWRCTL_HIRC0FSEL_Msk
#define CLK_PWRCTL_HIRC0FSEL_Msk
Definition: Nano103.h:4957
CLK_CLKDIV0_UART1DIV_Pos
#define CLK_CLKDIV0_UART1DIV_Pos
Definition: Nano103.h:5133
UART_SetLine_Config
void UART_SetLine_Config(UART_T *uart, uint32_t u32baudrate, uint32_t u32data_width, uint32_t u32parity, uint32_t u32stop_bits)
This function use to config UART line setting.
Definition: uart.c:300
__HXT
#define __HXT
Definition: system_Nano103.h:26
UART_ClearIntFlag
void UART_ClearIntFlag(UART_T *uart, uint32_t u32InterruptFlag)
The function is used to clear UART specified interrupt flag.
Definition: uart.c:99
UART_INTEN_RXTOIEN_Msk
#define UART_INTEN_RXTOIEN_Msk
Definition: Nano103.h:20203
UART_BAUD_MODE0_DIVIDER
#define UART_BAUD_MODE0_DIVIDER(u32SrcFreq, u32BaudRate)
Calculate UART baudrate mode2 divider.
Definition: uart.h:126
UART_IRDA_TXEN_Msk
#define UART_IRDA_TXEN_Msk
Definition: Nano103.h:20335
UART_ALTCTL_LINTXEN_Msk
#define UART_ALTCTL_LINTXEN_Msk
Definition: Nano103.h:20353
UART_ALTCTL_BRKFL_Msk
#define UART_ALTCTL_BRKFL_Msk
Definition: Nano103.h:20344
UART_TRSR_LINTXIF_Msk
#define UART_TRSR_LINTXIF_Msk
Definition: Nano103.h:20257
UART_TRSR_BITEF_Msk
#define UART_TRSR_BITEF_Msk
Definition: Nano103.h:20263
UART_T::IRDA
__IO uint32_t IRDA
Definition: Nano103.h:20116
UART_INTSTS_LINIF_Msk
#define UART_INTSTS_LINIF_Msk
Definition: Nano103.h:20245
UART_CTRL_TXOFF_Msk
#define UART_CTRL_TXOFF_Msk
Definition: Nano103.h:20143
UART_T::TRSR
__IO uint32_t TRSR
Definition: Nano103.h:20108
UART_FUNCSEL_UART
#define UART_FUNCSEL_UART
Definition: uart.h:73
UART_FIFOSTS_RXEMPTY_Msk
#define UART_FIFOSTS_RXEMPTY_Msk
Definition: Nano103.h:20275
UART_INTSTS_BUFERRIF_Msk
#define UART_INTSTS_BUFERRIF_Msk
Definition: Nano103.h:20236
__HIRC16M
#define __HIRC16M
Definition: system_Nano103.h:29
UART_STOP_BIT_1
#define UART_STOP_BIT_1
Definition: uart.h:49
UART1
#define UART1
Pointer to UART1 register structure.
Definition: Nano103.h:24876
UART_IRDA_RXINV_Msk
#define UART_IRDA_RXINV_Msk
Definition: Nano103.h:20341
UART_PARITY_NONE
#define UART_PARITY_NONE
Definition: uart.h:43
UART_FUNCSEL_RS485
#define UART_FUNCSEL_RS485
Definition: uart.h:76
UART_MODEM_RTSACTLV_Msk
#define UART_MODEM_RTSACTLV_Msk
Definition: Nano103.h:20308
UART_Close
void UART_Close(UART_T *uart)
The function is used to disable UART.
Definition: uart.c:141
SysGet_PLLClockFreq
uint32_t SysGet_PLLClockFreq(void)
Calculate current PLL clock frequency.
Definition: system_Nano103.c:28
__LXT
#define __LXT
Definition: system_Nano103.h:27
UART_TRSR_ADDRDETF_Msk
#define UART_TRSR_ADDRDETF_Msk
Definition: Nano103.h:20248
UART_INTSTS_MODEMIF_Msk
#define UART_INTSTS_MODEMIF_Msk
Definition: Nano103.h:20230
UART_FIFOSTS_PEF_Msk
#define UART_FIFOSTS_PEF_Msk
Definition: Nano103.h:20281
CLK_CLKDIV0_UART0DIV_Pos
#define CLK_CLKDIV0_UART0DIV_Pos
Definition: Nano103.h:5130
UART_Read
uint32_t UART_Read(UART_T *uart, uint8_t *pu8RxBuf, uint32_t u32ReadBytes)
The function is used to read Rx data from RX FIFO and the data will be stored in pu8RxBuf.
Definition: uart.c:266
UART_WORD_LEN_8
#define UART_WORD_LEN_8
Definition: uart.h:41
UART_IRDA_TXINV_Msk
#define UART_IRDA_TXINV_Msk
Definition: Nano103.h:20338
UART0
#define UART0
Pointer to UART0 register structure.
Definition: Nano103.h:24875
UART_Write
uint32_t UART_Write(UART_T *uart, uint8_t *pu8TxBuf, uint32_t u32WriteBytes)
The function is to write data into TX buffer to transmit data by UART.
Definition: uart.c:411
UART_TRSR_ABRDTOIF_Msk
#define UART_TRSR_ABRDTOIF_Msk
Definition: Nano103.h:20254
UART_T::INTEN
__IO uint32_t INTEN
Definition: Nano103.h:20106
UART_INTSTS_ABRIF_Msk
#define UART_INTSTS_ABRIF_Msk
Definition: Nano103.h:20242
UART_SelectLINMode
void UART_SelectLINMode(UART_T *uart, uint32_t u32Mode, uint32_t u32BreakLength)
Select and configure LIN function.
Definition: uart.c:390
UART_LINE_RTS_TRI_LEV_1BYTE
#define UART_LINE_RTS_TRI_LEV_1BYTE
Definition: uart.h:58
UART_FUNCSEL_LIN
#define UART_FUNCSEL_LIN
Definition: uart.h:74
UART_BAUD_MODE1
#define UART_BAUD_MODE1
Calculate UART baudrate mode0 divider.
Definition: uart.h:104
UART_SetTimeoutCnt
void UART_SetTimeoutCnt(UART_T *uart, uint32_t u32TOC)
This function use to set Rx timeout count.
Definition: uart.c:329
UART_FIFOSTS_BIF_Msk
#define UART_FIFOSTS_BIF_Msk
Definition: Nano103.h:20287
UART_DisableFlowCtrl
void UART_DisableFlowCtrl(UART_T *uart)
The function is used to disable UART auto flow control.
Definition: uart.c:154
UART_T::BAUD
__IO uint32_t BAUD
Definition: Nano103.h:20112
UART_DisableInt
void UART_DisableInt(UART_T *uart, uint32_t u32InterruptFlag)
The function is used to disable UART specified interrupt.
Definition: uart.c:178
UART_BAUD_MODE0
#define UART_BAUD_MODE0
Calculate UART baudrate mode0 divider.
Definition: uart.h:94
UART_T::DAT
__IO uint32_t DAT
Definition: Nano103.h:20103
UART_FIFOSTS_TXOVIF_Msk
#define UART_FIFOSTS_TXOVIF_Msk
Definition: Nano103.h:20290
FALSE
#define FALSE
Boolean false, define to use in API parameters or return value.
Definition: Nano103.h:25049
UART_Open
void UART_Open(UART_T *uart, uint32_t u32baudrate)
This function use to enable UART function and set baud-rate.
Definition: uart.c:231
UART_SelectRS485Mode
void UART_SelectRS485Mode(UART_T *uart, uint32_t u32Mode, uint32_t u32Addr)
The function is used to set RS485 relative setting.
Definition: uart.c:369
UART_T::INTSTS
__IO uint32_t INTSTS
Definition: Nano103.h:20107
UART_FIFOSTS_TXEMPTY_Msk
#define UART_FIFOSTS_TXEMPTY_Msk
Definition: Nano103.h:20293
CLK_CLKSEL1_UART0SEL_Msk
#define CLK_CLKSEL1_UART0SEL_Msk
Definition: Nano103.h:5077
UART_EnableFlowCtrl
void UART_EnableFlowCtrl(UART_T *uart)
The function is used to Enable UART auto flow control.
Definition: uart.c:192
__MIRC
#define __MIRC
Definition: system_Nano103.h:32
UART_EnableInt
void UART_EnableInt(UART_T *uart, uint32_t u32InterruptFlag)
The function is used to enable UART specified interrupt.
Definition: uart.c:217
UART_ALTCTL_ADRMPID_Pos
#define UART_ALTCTL_ADRMPID_Pos
Definition: Nano103.h:20370
UART_T::TOUT
__IO uint32_t TOUT
Definition: Nano103.h:20111
UART_FIFOSTS_RXOVIF_Msk
#define UART_FIFOSTS_RXOVIF_Msk
Definition: Nano103.h:20272
UART_T::LINE
__IO uint32_t LINE
Definition: Nano103.h:20105
CLK_CLKSEL2_UART1SEL_Pos
#define CLK_CLKSEL2_UART1SEL_Pos
Definition: Nano103.h:5103
UART_TRSR_ABRDIF_Msk
#define UART_TRSR_ABRDIF_Msk
Definition: Nano103.h:20251
Nano103.h
NANO103 peripheral access layer header file. This file contains all the peripheral register's definit...
UART_MODEM_CTSDETF_Msk
#define UART_MODEM_CTSDETF_Msk
Definition: Nano103.h:20320
__HIRC36M
#define __HIRC36M
Definition: system_Nano103.h:31
CLK_CLKSEL0_HIRCSEL_Msk
#define CLK_CLKSEL0_HIRCSEL_Msk
Definition: Nano103.h:5071
UART_FIFOSTS_FEF_Msk
#define UART_FIFOSTS_FEF_Msk
Definition: Nano103.h:20284
UART_TRSR_LINRXIF_Msk
#define UART_TRSR_LINRXIF_Msk
Definition: Nano103.h:20260
UART_T::CTRL
__IO uint32_t CTRL
Definition: Nano103.h:20104
UART_CTRL_RXOFF_Msk
#define UART_CTRL_RXOFF_Msk
Definition: Nano103.h:20140
CLK_CLKDIV0_UART1DIV_Msk
#define CLK_CLKDIV0_UART1DIV_Msk
Definition: Nano103.h:5134
UART_FUNCSEL_FUNCSEL_Pos
#define UART_FUNCSEL_FUNCSEL_Pos
Definition: Nano103.h:20373
UART_SelectIrDAMode
void UART_SelectIrDAMode(UART_T *uart, uint32_t u32Buadrate, uint32_t u32Direction)
The function is used to configure IrDA relative settings. It consists of TX or RX mode and baudrate.
Definition: uart.c:345
UART_T
Definition: Nano103.h:18880
__HIRC12M
#define __HIRC12M
Definition: system_Nano103.h:28
UART_ALTCTL_LINRXEN_Msk
#define UART_ALTCTL_LINRXEN_Msk
Definition: Nano103.h:20350
UART_T::FUNCSEL
__IO uint32_t FUNCSEL
Definition: Nano103.h:20118
UART_T::MODEM
__IO uint32_t MODEM
Definition: Nano103.h:20110
CLK_CLKSEL2_UART1SEL_Msk
#define CLK_CLKSEL2_UART1SEL_Msk
Definition: Nano103.h:5104
CLK_CLKSEL1_UART0SEL_Pos
#define CLK_CLKSEL1_UART0SEL_Pos
Definition: Nano103.h:5076
CLK
#define CLK
Pointer to CLK register structure.
Definition: Nano103.h:24884
UART_CTRL_ATORTSEN_Msk
#define UART_CTRL_ATORTSEN_Msk
Definition: Nano103.h:20146
UART_T::FIFOSTS
__IO uint32_t FIFOSTS
Definition: Nano103.h:20109
UART_TOUT_TOIC_Msk
#define UART_TOUT_TOIC_Msk
Definition: Nano103.h:20323
UART_MODEM_CTSACTLV_Msk
#define UART_MODEM_CTSACTLV_Msk
Definition: Nano103.h:20314
UART_INTSTS_RLSIF_Msk
#define UART_INTSTS_RLSIF_Msk
Definition: Nano103.h:20227
CLK_CLKDIV0_UART0DIV_Msk
#define CLK_CLKDIV0_UART0DIV_Msk
Definition: Nano103.h:5131
UART_INTSTS_WKUPIF_Msk
#define UART_INTSTS_WKUPIF_Msk
Definition: Nano103.h:20239