Nano100BN Series BSP  V3.03.002
The Board Support Package for Nano100BN Series
uart.c
Go to the documentation of this file.
1 /**************************************************************************/
13 #include <stdio.h>
14 #include "Nano100Series.h"
15 
28 extern uint32_t SysGet_PLLClockFreq(void);
31 
32 
41 void UART_ClearIntFlag(UART_T* uart, uint32_t u32InterruptFlag)
42 {
43 
44  if(u32InterruptFlag & UART_ISR_RLS_IS_Msk) /* clear Receive Line Status Interrupt */
45  {
48  }
49 
50  if(u32InterruptFlag & UART_ISR_MODEM_IS_Msk) /* clear Modem Interrupt */
51  uart->MCSR |= UART_MCSR_DCT_F_Msk;
52 
53  if(u32InterruptFlag & UART_ISR_BUF_ERR_IS_Msk) /* clear Buffer Error Interrupt */
54  {
56  }
57 
58  if(u32InterruptFlag & UART_ISR_WAKE_IS_Msk) /* clear wake up Interrupt */
59  {
60  uart->ISR |= UART_ISR_WAKE_IS_Msk;
61  }
62 
63  if(u32InterruptFlag & UART_ISR_ABAUD_IS_Msk) /* clear auto-baud rate Interrupt */
64  {
66  }
67 
68  if(u32InterruptFlag & UART_ISR_LIN_IS_Msk) /* clear LIN break Interrupt */
69  {
71  }
72 
73 }
74 
75 
83 void UART_Close(UART_T* uart)
84 {
85  uart->IER = 0;
86 }
87 
88 
97 {
99 }
100 
101 
120 void UART_DisableInt(UART_T* uart, uint32_t u32InterruptFlag )
121 {
122  uart->IER &= ~ u32InterruptFlag;
123 }
124 
125 
126 
135 {
138 }
139 
140 
159 void UART_EnableInt(UART_T* uart, uint32_t u32InterruptFlag )
160 {
161  uart->IER |= u32InterruptFlag;
162 }
163 
164 
173 void UART_Open(UART_T* uart, uint32_t u32baudrate)
174 {
175  uint8_t u8UartClkSrcSel;
176  uint32_t u32ClkTbl[4] = {__HXT, __LXT, 0, __HIRC12M};
177  uint32_t u32Baud_Div;
178  uint32_t u32SrcFreq;
179  uint32_t u32SrcFreqDiv;
180 
181  u8UartClkSrcSel = (CLK->CLKSEL1 & CLK_CLKSEL1_UART_S_Msk) >> CLK_CLKSEL1_UART_S_Pos;
182 
183  u32SrcFreq = u32ClkTbl[u8UartClkSrcSel];
184 
185  u32SrcFreqDiv = (((CLK->CLKDIV0 & CLK_CLKDIV0_UART_N_Msk) >> CLK_CLKDIV0_UART_N_Pos) + 1);
186 
187  if(u32SrcFreq == 0)
188  {
189  u32SrcFreq = SysGet_PLLClockFreq() / u32SrcFreqDiv;
190  }
191  else
192  {
193  u32SrcFreq = u32SrcFreq / u32SrcFreqDiv;
194  }
195 
196  uart->FUN_SEL = UART_FUNC_SEL_UART;
199 
200  if(u32baudrate != 0)
201  {
202  u32Baud_Div = UART_BAUD_MODE0_DIVIDER(u32SrcFreq, u32baudrate);
203 
204  if(u32Baud_Div > 0xFFFF)
205  uart->BAUD = (UART_BAUD_MODE1 | UART_BAUD_MODE1_DIVIDER(u32SrcFreq, u32baudrate));
206  else
207  uart->BAUD = (UART_BAUD_MODE0 | u32Baud_Div);
208  }
209 }
210 
211 
222 uint32_t UART_Read(UART_T* uart, uint8_t *pu8RxBuf, uint32_t u32ReadBytes)
223 {
224  uint32_t u32Count, u32delayno;
225 
226  for(u32Count=0; u32Count < u32ReadBytes; u32Count++)
227  {
228  u32delayno = 0;
229 
230  while(uart->FSR & UART_FSR_RX_EMPTY_F_Msk) /* Check RX empty => failed */
231  {
232  u32delayno++;
233  if( u32delayno >= 0x40000000 )
234  return FALSE;
235  }
236  pu8RxBuf[u32Count] = uart->RBR; /* Get Data from UART RX */
237  }
238 
239  return u32Count;
240 
241 }
242 
243 
256 void UART_SetLine_Config(UART_T* uart, uint32_t u32baudrate, uint32_t u32data_width, uint32_t u32parity, uint32_t u32stop_bits)
257 {
258  uint8_t u8UartClkSrcSel;
259  uint32_t u32ClkTbl[4] = {__HXT, __LXT, 0, __HIRC12M};
260  uint32_t u32Baud_Div = 0;
261  uint32_t u32SrcFreq;
262  uint32_t u32SrcFreqDiv;
263 
264  u8UartClkSrcSel = (CLK->CLKSEL1 & CLK_CLKSEL1_UART_S_Msk) >> CLK_CLKSEL1_UART_S_Pos;
265 
266  u32SrcFreq = u32ClkTbl[u8UartClkSrcSel];
267 
268  u32SrcFreqDiv = (((CLK->CLKDIV0 & CLK_CLKDIV0_UART_N_Msk) >> CLK_CLKDIV0_UART_N_Pos) + 1);
269 
270  if(u32SrcFreq == 0)
271  {
272  u32SrcFreq = SysGet_PLLClockFreq() / u32SrcFreqDiv;
273  }
274  else
275  {
276  u32SrcFreq = u32SrcFreq / u32SrcFreqDiv;
277  }
278 
279  if(u32baudrate != 0)
280  {
281  u32Baud_Div = UART_BAUD_MODE0_DIVIDER(u32SrcFreq, u32baudrate);
282 
283  if(u32Baud_Div > 0xFFFF)
284  uart->BAUD = (UART_BAUD_MODE1 | UART_BAUD_MODE1_DIVIDER(u32SrcFreq, u32baudrate));
285  else
286  uart->BAUD = (UART_BAUD_MODE0 | u32Baud_Div);
287  }
288 
289  uart->TLCTL = u32data_width | u32parity | u32stop_bits;
290 }
291 
292 
301 void UART_SetTimeoutCnt(UART_T* uart, uint32_t u32TOC)
302 {
303  uart->TMCTL = (uart->TMCTL & ~UART_TMCTL_TOIC_Msk)| (u32TOC);
304  uart->IER |= UART_IER_RTO_IE_Msk;
305 }
306 
307 
317 void UART_SelectIrDAMode(UART_T* uart, uint32_t u32Buadrate, uint32_t u32Direction)
318 {
319  uint8_t u8UartClkSrcSel;
320  uint32_t u32ClkTbl[4] = {__HXT, __LXT, 0, __HIRC12M};
321  uint32_t u32SrcFreq;
322  uint32_t u32SrcFreqDiv;
323 
324  u8UartClkSrcSel = (CLK->CLKSEL1 & CLK_CLKSEL1_UART_S_Msk) >> CLK_CLKSEL1_UART_S_Pos;
325 
326  u32SrcFreq = u32ClkTbl[u8UartClkSrcSel];
327 
328  u32SrcFreqDiv = (((CLK->CLKDIV0 & CLK_CLKDIV0_UART_N_Msk) >> CLK_CLKDIV0_UART_N_Pos) + 1);
329 
330  if(u32SrcFreq == 0)
331  {
332  u32SrcFreq = SysGet_PLLClockFreq() / u32SrcFreqDiv;
333  }
334  else
335  {
336  u32SrcFreq = u32SrcFreq / u32SrcFreqDiv;
337  }
338 
339  uart->BAUD = UART_BAUD_MODE1 | UART_BAUD_MODE1_DIVIDER(u32SrcFreq, u32Buadrate);
340 
341  uart->IRCR &= ~UART_IRCR_INV_TX_Msk;
342  uart->IRCR |= UART_IRCR_INV_RX_Msk;
343  uart->IRCR = u32Direction ? uart->IRCR | UART_IRCR_TX_SELECT_Msk : uart->IRCR &~ UART_IRCR_TX_SELECT_Msk;
344  uart->FUN_SEL = (0x2 << UART_FUN_SEL_FUN_SEL_Pos);
345 }
346 
347 
357 void UART_SelectRS485Mode(UART_T* uart, uint32_t u32Mode, uint32_t u32Addr)
358 {
360  uart->ALT_CTL = 0;
361  uart->ALT_CTL |= u32Mode | (u32Addr << UART_ALT_CTL_ADDR_PID_MATCH_Pos);
362 }
363 
378 void UART_SelectLINMode(UART_T* uart, uint32_t u32Mode, uint32_t u32BreakLength)
379 {
380  /* Select LIN function mode */
381  uart->FUN_SEL = UART_FUNC_SEL_LIN;
382 
383  /* Select LIN function setting : Tx enable, Rx enable and break field length */
384  uart->FUN_SEL = UART_FUNC_SEL_LIN;
386  uart->ALT_CTL |= u32BreakLength & UART_ALT_CTL_LIN_TX_BCNT_Msk;
387  uart->ALT_CTL |= u32Mode;
388 }
389 
399 uint32_t UART_Write(UART_T* uart,uint8_t *pu8TxBuf, uint32_t u32WriteBytes)
400 {
401  uint32_t u32Count, u32delayno;
402 
403  for(u32Count=0; u32Count != u32WriteBytes; u32Count++)
404  {
405  u32delayno = 0;
406  while((uart->FSR & UART_FSR_TX_EMPTY_F_Msk) == 0) /* Wait Tx empty and Time-out manner */
407  {
408  u32delayno++;
409  if( u32delayno >= 0x40000000 )
410  return FALSE;
411  }
412  uart->THR = pu8TxBuf[u32Count]; /* Send UART Data from buffer */
413  }
414 
415  return u32Count;
416 
417 }
418 
419  /* end of group NANO100_UART_EXPORTED_FUNCTIONS */
421  /* end of group NANO100_UART_Driver */
423  /* end of group NANO100_Device_Driver */
425 
426 /*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/
427 
428 
429 
#define UART_FUNC_SEL_LIN
Definition: uart.h:74
#define UART_FUN_SEL_FUN_SEL_Pos
#define UART_TMCTL_TOIC_Msk
#define UART_WORD_LEN_8
Definition: uart.h:41
__IO uint32_t ALT_CTL
#define UART_CTL_AUTO_RTS_EN_Msk
#define UART_FSR_RX_OVER_F_Msk
__IO uint32_t FUN_SEL
#define UART_ALT_CTL_ADDR_PID_MATCH_Pos
#define UART_ISR_BUF_ERR_IS_Msk
#define UART_BAUD_MODE0_DIVIDER(u32SrcFreq, u32BaudRate)
Calculate UART baudrate mode2 divider.
Definition: uart.h:126
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:399
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:256
#define UART_ISR_MODEM_IS_Msk
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:317
#define UART_FSR_TX_OVER_F_Msk
#define UART_ALT_CTL_LIN_TX_BCNT_Msk
#define UART_ISR_LIN_IS_Msk
#define CLK_CLKSEL1_UART_S_Pos
#define UART_BAUD_MODE0
Calculate UART baudrate mode0 divider.
Definition: uart.h:94
uint32_t SysGet_PLLClockFreq(void)
Calculate current PLL clock frequency.
#define UART_TLCTL_RTS_TRI_LEV_1BYTE
Definition: uart.h:58
#define FALSE
Boolean false, define to use in API parameters or return value.
#define CLK
Pointer to CLK register structure.
#define UART_FSR_RX_EMPTY_F_Msk
__IO uint32_t FSR
void UART_Close(UART_T *uart)
The function is used to disable UART.
Definition: uart.c:83
__IO uint32_t TRSR
__IO uint32_t IER
#define UART_BAUD_MODE1
Calculate UART baudrate mode0 divider.
Definition: uart.h:104
#define CLK_CLKDIV0_UART_N_Msk
#define UART_ISR_ABAUD_IS_Msk
#define UART_MCSR_LEV_RTS_Msk
#define UART_ALT_CTL_LIN_TX_EN_Msk
__I uint32_t RBR
#define CLK_CLKSEL1_UART_S_Msk
Nano100 series peripheral access layer header file. This file contains all the peripheral register's ...
__IO uint32_t ISR
#define UART_IRCR_INV_TX_Msk
__IO uint32_t TMCTL
#define UART_ISR_WAKE_IS_Msk
void UART_SelectLINMode(UART_T *uart, uint32_t u32Mode, uint32_t u32BreakLength)
Select and configure LIN function.
Definition: uart.c:378
#define UART_PARITY_NONE
Definition: uart.h:43
__IO uint32_t CTL
#define UART_FSR_PE_F_Msk
#define CLK_CLKDIV0_UART_N_Pos
#define __HIRC12M
#define UART_TRSR_RS485_ADDET_F_Msk
void UART_DisableFlowCtrl(UART_T *uart)
The function is used to disable UART auto flow control.
Definition: uart.c:96
__IO uint32_t MCSR
#define UART_TRSR_LIN_RX_F_Msk
void UART_EnableInt(UART_T *uart, uint32_t u32InterruptFlag)
The function is used to enable UART specified interrupt and disable NVIC UART IRQ.
Definition: uart.c:159
#define UART_IRCR_INV_RX_Msk
#define __HXT
#define UART_ISR_RLS_IS_Msk
__IO uint32_t TLCTL
void UART_DisableInt(UART_T *uart, uint32_t u32InterruptFlag)
The function is used to disable UART specified interrupt and disable NVIC UART IRQ.
Definition: uart.c:120
#define UART_MCSR_DCT_F_Msk
#define __LXT
#define UART_TRSR_ABAUD_TOUT_F_Msk
#define UART_IER_RTO_IE_Msk
#define UART_FUNC_SEL_RS485
Definition: uart.h:76
#define UART_ALT_CTL_LIN_RX_EN_Msk
void UART_SetTimeoutCnt(UART_T *uart, uint32_t u32TOC)
This function use to set Rx timeout count.
Definition: uart.c:301
#define UART_MCSR_LEV_CTS_Msk
#define UART_TRSR_LIN_TX_F_Msk
#define UART_CTL_AUTO_CTS_EN_Msk
#define UART_TRSR_BIT_ERR_F_Msk
#define UART_STOP_BIT_1
Definition: uart.h:49
void UART_ClearIntFlag(UART_T *uart, uint32_t u32InterruptFlag)
The function is used to clear UART specified interrupt flag.
Definition: uart.c:41
void UART_SelectRS485Mode(UART_T *uart, uint32_t u32Mode, uint32_t u32Addr)
The function is used to set RS485 relative setting.
Definition: uart.c:357
#define UART_FSR_FE_F_Msk
__IO uint32_t IRCR
#define UART_FSR_TX_EMPTY_F_Msk
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:222
#define UART_TRSR_ABAUD_F_Msk
__O uint32_t THR
void UART_Open(UART_T *uart, uint32_t u32baudrate)
This function use to enable UART function and set baud-rate.
Definition: uart.c:173
#define UART_FSR_BI_F_Msk
void UART_EnableFlowCtrl(UART_T *uart)
The function is used to Enable UART auto flow control.
Definition: uart.c:134
#define UART_FUNC_SEL_UART
Definition: uart.h:73
#define UART_TLCTL_RFITL_1BYTE
Definition: uart.h:53
#define UART_IRCR_TX_SELECT_Msk
__IO uint32_t BAUD
#define UART_BAUD_MODE1_DIVIDER(u32SrcFreq, u32BaudRate)
Calculate UART baudrate mode0 divider.
Definition: uart.h:116