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  {
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  {
114  }
115 
116  if(u32InterruptFlag & UART_INTSTS_WKUPIF_Msk) /* clear wake up Interrupt */
117  {
119  }
120 
121  if(u32InterruptFlag & UART_INTSTS_ABRIF_Msk) /* clear auto-baud rate Interrupt */
122  {
124  }
125 
126  if(u32InterruptFlag & UART_INTSTS_LINIF_Msk) /* clear LIN break Interrupt */
127  {
129  }
130 
131 }
132 
133 
141 void UART_Close(UART_T* uart)
142 {
143  uart->INTEN = 0;
144 }
145 
146 
155 {
157 }
158 
159 
178 void UART_DisableInt(UART_T* uart, uint32_t u32InterruptFlag )
179 {
180  uart->INTEN &= ~ u32InterruptFlag;
181 }
182 
183 
184 
193 {
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 
239 
240  uart->FUNCSEL = UART_FUNCSEL_UART;
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;
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 
#define UART_BAUD_MODE1_DIVIDER(u32SrcFreq, u32BaudRate)
Calculate UART baudrate mode0 divider.
Definition: uart.h:116
__IO uint32_t ALTCTL
Definition: Nano103.h:20117
#define UART_LINE_RFITL_1BYTE
Definition: uart.h:53
#define UART_CTRL_ATOCTSEN_Msk
Definition: Nano103.h:20149
#define CLK_PWRCTL_HIRC0FSEL_Msk
Definition: Nano103.h:4957
#define CLK_CLKDIV0_UART1DIV_Pos
Definition: Nano103.h:5133
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
#define __HXT
void UART_ClearIntFlag(UART_T *uart, uint32_t u32InterruptFlag)
The function is used to clear UART specified interrupt flag.
Definition: uart.c:99
#define UART_INTEN_RXTOIEN_Msk
Definition: Nano103.h:20203
#define UART_BAUD_MODE0_DIVIDER(u32SrcFreq, u32BaudRate)
Calculate UART baudrate mode2 divider.
Definition: uart.h:126
#define UART_IRDA_TXEN_Msk
Definition: Nano103.h:20335
#define UART_ALTCTL_LINTXEN_Msk
Definition: Nano103.h:20353
#define UART_ALTCTL_BRKFL_Msk
Definition: Nano103.h:20344
#define UART_TRSR_LINTXIF_Msk
Definition: Nano103.h:20257
#define UART_TRSR_BITEF_Msk
Definition: Nano103.h:20263
__IO uint32_t IRDA
Definition: Nano103.h:20116
#define UART_INTSTS_LINIF_Msk
Definition: Nano103.h:20245
#define UART_CTRL_TXOFF_Msk
Definition: Nano103.h:20143
__IO uint32_t TRSR
Definition: Nano103.h:20108
#define UART_FUNCSEL_UART
Definition: uart.h:73
#define UART_FIFOSTS_RXEMPTY_Msk
Definition: Nano103.h:20275
#define UART_INTSTS_BUFERRIF_Msk
Definition: Nano103.h:20236
#define __HIRC16M
#define UART_STOP_BIT_1
Definition: uart.h:49
#define UART1
Pointer to UART1 register structure.
Definition: Nano103.h:24876
#define UART_IRDA_RXINV_Msk
Definition: Nano103.h:20341
#define UART_PARITY_NONE
Definition: uart.h:43
#define UART_FUNCSEL_RS485
Definition: uart.h:76
#define UART_MODEM_RTSACTLV_Msk
Definition: Nano103.h:20308
void UART_Close(UART_T *uart)
The function is used to disable UART.
Definition: uart.c:141
uint32_t SysGet_PLLClockFreq(void)
Calculate current PLL clock frequency.
#define __LXT
#define UART_TRSR_ADDRDETF_Msk
Definition: Nano103.h:20248
#define UART_INTSTS_MODEMIF_Msk
Definition: Nano103.h:20230
#define UART_FIFOSTS_PEF_Msk
Definition: Nano103.h:20281
#define CLK_CLKDIV0_UART0DIV_Pos
Definition: Nano103.h:5130
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
#define UART_WORD_LEN_8
Definition: uart.h:41
#define UART_IRDA_TXINV_Msk
Definition: Nano103.h:20338
#define UART0
Pointer to UART0 register structure.
Definition: Nano103.h:24875
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
#define UART_TRSR_ABRDTOIF_Msk
Definition: Nano103.h:20254
__IO uint32_t INTEN
Definition: Nano103.h:20106
#define UART_INTSTS_ABRIF_Msk
Definition: Nano103.h:20242
void UART_SelectLINMode(UART_T *uart, uint32_t u32Mode, uint32_t u32BreakLength)
Select and configure LIN function.
Definition: uart.c:390
#define UART_LINE_RTS_TRI_LEV_1BYTE
Definition: uart.h:58
#define UART_FUNCSEL_LIN
Definition: uart.h:74
#define UART_BAUD_MODE1
Calculate UART baudrate mode0 divider.
Definition: uart.h:104
void UART_SetTimeoutCnt(UART_T *uart, uint32_t u32TOC)
This function use to set Rx timeout count.
Definition: uart.c:329
#define UART_FIFOSTS_BIF_Msk
Definition: Nano103.h:20287
void UART_DisableFlowCtrl(UART_T *uart)
The function is used to disable UART auto flow control.
Definition: uart.c:154
__IO uint32_t BAUD
Definition: Nano103.h:20112
void UART_DisableInt(UART_T *uart, uint32_t u32InterruptFlag)
The function is used to disable UART specified interrupt.
Definition: uart.c:178
#define UART_BAUD_MODE0
Calculate UART baudrate mode0 divider.
Definition: uart.h:94
__IO uint32_t DAT
Definition: Nano103.h:20103
#define UART_FIFOSTS_TXOVIF_Msk
Definition: Nano103.h:20290
#define FALSE
Boolean false, define to use in API parameters or return value.
Definition: Nano103.h:25049
void UART_Open(UART_T *uart, uint32_t u32baudrate)
This function use to enable UART function and set baud-rate.
Definition: uart.c:231
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
__IO uint32_t INTSTS
Definition: Nano103.h:20107
#define UART_FIFOSTS_TXEMPTY_Msk
Definition: Nano103.h:20293
#define CLK_CLKSEL1_UART0SEL_Msk
Definition: Nano103.h:5077
void UART_EnableFlowCtrl(UART_T *uart)
The function is used to Enable UART auto flow control.
Definition: uart.c:192
#define __MIRC
void UART_EnableInt(UART_T *uart, uint32_t u32InterruptFlag)
The function is used to enable UART specified interrupt.
Definition: uart.c:217
#define UART_ALTCTL_ADRMPID_Pos
Definition: Nano103.h:20370
__IO uint32_t TOUT
Definition: Nano103.h:20111
#define UART_FIFOSTS_RXOVIF_Msk
Definition: Nano103.h:20272
__IO uint32_t LINE
Definition: Nano103.h:20105
#define CLK_CLKSEL2_UART1SEL_Pos
Definition: Nano103.h:5103
#define UART_TRSR_ABRDIF_Msk
Definition: Nano103.h:20251
NANO103 peripheral access layer header file. This file contains all the peripheral register's definit...
#define UART_MODEM_CTSDETF_Msk
Definition: Nano103.h:20320
#define __HIRC36M
#define CLK_CLKSEL0_HIRCSEL_Msk
Definition: Nano103.h:5071
#define UART_FIFOSTS_FEF_Msk
Definition: Nano103.h:20284
#define UART_TRSR_LINRXIF_Msk
Definition: Nano103.h:20260
__IO uint32_t CTRL
Definition: Nano103.h:20104
#define UART_CTRL_RXOFF_Msk
Definition: Nano103.h:20140
#define CLK_CLKDIV0_UART1DIV_Msk
Definition: Nano103.h:5134
#define UART_FUNCSEL_FUNCSEL_Pos
Definition: Nano103.h:20373
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
#define __HIRC12M
#define UART_ALTCTL_LINRXEN_Msk
Definition: Nano103.h:20350
__IO uint32_t FUNCSEL
Definition: Nano103.h:20118
__IO uint32_t MODEM
Definition: Nano103.h:20110
#define CLK_CLKSEL2_UART1SEL_Msk
Definition: Nano103.h:5104
#define CLK_CLKSEL1_UART0SEL_Pos
Definition: Nano103.h:5076
#define CLK
Pointer to CLK register structure.
Definition: Nano103.h:24884
#define UART_CTRL_ATORTSEN_Msk
Definition: Nano103.h:20146
__IO uint32_t FIFOSTS
Definition: Nano103.h:20109
#define UART_TOUT_TOIC_Msk
Definition: Nano103.h:20323
#define UART_MODEM_CTSACTLV_Msk
Definition: Nano103.h:20314
#define UART_INTSTS_RLSIF_Msk
Definition: Nano103.h:20227
#define CLK_CLKDIV0_UART0DIV_Msk
Definition: Nano103.h:5131
#define UART_INTSTS_WKUPIF_Msk
Definition: Nano103.h:20239