Nano100BN Series BSP  V3.03.002
The Board Support Package for Nano100BN Series
scuart.c
Go to the documentation of this file.
1 /**************************************************************************/
12 #include "Nano100Series.h"
13 
32 void SCUART_Close(SC_T* sc)
33 {
34  sc->IER = 0;
35  sc->UACTL = 0;
36  sc->CTL = 0;
37 
38 }
39 
41 
46 static uint32_t SCUART_GetClock(SC_T *sc)
47 {
48  uint32_t u32ClkSrc = (CLK->CLKSEL2 & CLK_CLKSEL2_SC_S_Msk) >> CLK_CLKSEL2_SC_S_Pos;
49  uint32_t u32Clk;
50 
51  // Get smartcard module clock
52  if(u32ClkSrc == 0)
53  u32Clk = __HXT;
54  else if(u32ClkSrc == 1)
55  u32Clk = CLK_GetPLLClockFreq();
56  else
57  u32Clk = __HIRC12M;
58 
59  if(sc == SC0)
60  u32Clk /= ((CLK->CLKDIV0 & CLK_CLKDIV0_SC0_N_Msk) >> CLK_CLKDIV0_SC0_N_Pos) + 1;
61  else if(sc == SC1)
62  u32Clk /= (CLK->CLKDIV1 & CLK_CLKDIV1_SC1_N_Msk) + 1;
63  else // SC2
64  u32Clk /= ((CLK->CLKDIV1 & CLK_CLKDIV1_SC2_N_Msk) >> CLK_CLKDIV1_SC2_N_Pos) + 1;
65 
66  return u32Clk;
67 }
68 
70 
85 uint32_t SCUART_Open(SC_T* sc, uint32_t u32baudrate)
86 {
87  uint32_t u32Clk = SCUART_GetClock(sc), u32Div;
88 
89  // Calculate divider for target baudrate
90  u32Div = (u32Clk + (u32baudrate >> 1) - 1) / u32baudrate - 1;
91 
92  sc->CTL = SC_CTL_SC_CEN_Msk | SC_CTL_SLEN_Msk; // Enable smartcard interface and stop bit = 1
93  sc->UACTL = SCUART_CHAR_LEN_8 | SCUART_PARITY_NONE | SC_UACTL_UA_MODE_EN_Msk; // Enable UART mode, disable parity and 8 bit per character
94  sc->ETUCR = u32Div;
95 
96  return(u32Clk / (u32Div + 1));
97 }
98 
107 uint32_t SCUART_Read(SC_T* sc, uint8_t *pu8RxBuf, uint32_t u32ReadBytes)
108 {
109  uint32_t u32Count;
110 
111  for(u32Count = 0; u32Count < u32ReadBytes; u32Count++)
112  {
113  if(SCUART_GET_RX_EMPTY(sc)) // no data available
114  {
115  break;
116  }
117  pu8RxBuf[u32Count] = SCUART_READ(sc); // get data from FIFO
118  }
119 
120  return u32Count;
121 }
122 
147 uint32_t SCUART_SetLineConfig(SC_T* sc, uint32_t u32Baudrate, uint32_t u32DataWidth, uint32_t u32Parity, uint32_t u32StopBits)
148 {
149 
150  uint32_t u32Clk = SCUART_GetClock(sc), u32Div;
151 
152  if(u32Baudrate == 0) // keep original baudrate setting
153  {
154  u32Div = sc->ETUCR & SC_ETUCR_ETU_RDIV_Msk;
155  }
156  else
157  {
158  // Calculate divider for target baudrate
159  u32Div = (u32Clk + (u32Baudrate >> 1) - 1)/ u32Baudrate - 1;
160  sc->ETUCR = u32Div;
161  }
162 
163  sc->CTL = u32StopBits | SC_CTL_SC_CEN_Msk; // Set stop bit
164  sc->UACTL = u32Parity | u32DataWidth | SC_UACTL_UA_MODE_EN_Msk; // Set character width and parity
165 
166  return(u32Clk / (u32Div + 1));
167 }
168 
179 void SCUART_SetTimeoutCnt(SC_T* sc, uint32_t u32TOC)
180 {
181  sc->RFTMR = u32TOC;
182 }
183 
184 
193 void SCUART_Write(SC_T* sc,uint8_t *pu8TxBuf, uint32_t u32WriteBytes)
194 {
195  uint32_t u32Count;
196 
197  for(u32Count = 0; u32Count != u32WriteBytes; u32Count++)
198  {
199  while(SCUART_GET_TX_FULL(sc)); // Wait 'til FIFO not full
200  sc->THR = pu8TxBuf[u32Count]; // Write 1 byte to FIFO
201  }
202 }
203 
204  /* end of group NANO100_SCUART_EXPORTED_FUNCTIONS */
206  /* end of group NANO100_SCUART_Driver */
208  /* end of group NANO100_Device_Driver */
210 
211 /*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/
#define SC1
Pointer to SC1 register structure.
void SCUART_SetTimeoutCnt(SC_T *sc, uint32_t u32TOC)
This function use to set receive timeout count.
Definition: scuart.c:179
#define CLK_CLKDIV1_SC2_N_Msk
#define CLK_CLKSEL2_SC_S_Pos
#define SC_CTL_SC_CEN_Msk
__IO uint32_t ETUCR
#define SC_CTL_SLEN_Msk
#define CLK_CLKDIV1_SC1_N_Msk
#define CLK_CLKSEL2_SC_S_Msk
#define CLK
Pointer to CLK register structure.
#define SC0
Pointer to SC0 register structure.
#define SCUART_READ(sc)
Read Rx data register.
Definition: scuart.h:120
uint32_t SCUART_SetLineConfig(SC_T *sc, uint32_t u32Baudrate, uint32_t u32DataWidth, uint32_t u32Parity, uint32_t u32StopBits)
This function use to config smartcard UART mode line setting.
Definition: scuart.c:147
uint32_t SCUART_Read(SC_T *sc, uint8_t *pu8RxBuf, uint32_t u32ReadBytes)
The function is used to read Rx data from RX FIFO.
Definition: scuart.c:107
Nano100 series peripheral access layer header file. This file contains all the peripheral register's ...
uint32_t CLK_GetPLLClockFreq(void)
This function get PLL frequency. The frequency unit is Hz.
Definition: clk.c:142
#define CLK_CLKDIV1_SC2_N_Pos
#define SCUART_GET_RX_EMPTY(sc)
Get RX FIFO empty flag status from register.
Definition: scuart.h:130
#define SCUART_PARITY_NONE
Definition: scuart.h:37
uint32_t SCUART_Open(SC_T *sc, uint32_t u32baudrate)
This function use to enable smartcard module UART mode and set baudrate.
Definition: scuart.c:85
#define __HIRC12M
#define CLK_CLKDIV0_SC0_N_Msk
#define SCUART_GET_TX_FULL(sc)
Get TX FIFO full flag status from register.
Definition: scuart.h:80
void SCUART_Write(SC_T *sc, uint8_t *pu8TxBuf, uint32_t u32WriteBytes)
This function is to write data into transmit FIFO to send data out.
Definition: scuart.c:193
#define __HXT
#define SCUART_CHAR_LEN_8
Definition: scuart.h:35
__IO uint32_t RFTMR
__O uint32_t THR
#define CLK_CLKDIV0_SC0_N_Pos
__IO uint32_t CTL
__IO uint32_t IER
void SCUART_Close(SC_T *sc)
The function is used to disable smartcard interface UART mode.
Definition: scuart.c:32
#define SC_UACTL_UA_MODE_EN_Msk
#define SC_ETUCR_ETU_RDIV_Msk
__IO uint32_t UACTL