| 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. ***/
SC1
#define SC1
Pointer to SC1 register structure.
Definition: Nano100Series.h:11686
SCUART_SetTimeoutCnt
void SCUART_SetTimeoutCnt(SC_T *sc, uint32_t u32TOC)
This function use to set receive timeout count.
Definition: scuart.c:179
CLK_CLKDIV1_SC2_N_Msk
#define CLK_CLKDIV1_SC2_N_Msk
Definition: Nano100Series.h:1435
CLK_CLKSEL2_SC_S_Pos
#define CLK_CLKSEL2_SC_S_Pos
Definition: Nano100Series.h:1401
SC_CTL_SC_CEN_Msk
#define SC_CTL_SC_CEN_Msk
Definition: Nano100Series.h:8896
SC_T::ETUCR
__IO uint32_t ETUCR
Definition: Nano100Series.h:8526
SC_T
Definition: Nano100Series.h:8273
SC_CTL_SLEN_Msk
#define SC_CTL_SLEN_Msk
Definition: Nano100Series.h:8920
CLK_CLKDIV1_SC1_N_Msk
#define CLK_CLKDIV1_SC1_N_Msk
Definition: Nano100Series.h:1432
CLK_CLKSEL2_SC_S_Msk
#define CLK_CLKSEL2_SC_S_Msk
Definition: Nano100Series.h:1402
CLK
#define CLK
Pointer to CLK register structure.
Definition: Nano100Series.h:11693
SC0
#define SC0
Pointer to SC0 register structure.
Definition: Nano100Series.h:11685
SCUART_READ
#define SCUART_READ(sc)
Read Rx data register.
Definition: scuart.h:120
SCUART_SetLineConfig
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
SCUART_Read
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
Nano100Series.h
Nano100 series peripheral access layer header file. This file contains all the peripheral register's ...
CLK_GetPLLClockFreq
uint32_t CLK_GetPLLClockFreq(void)
This function get PLL frequency. The frequency unit is Hz.
Definition: clk.c:142
CLK_CLKDIV1_SC2_N_Pos
#define CLK_CLKDIV1_SC2_N_Pos
Definition: Nano100Series.h:1434
SCUART_GET_RX_EMPTY
#define SCUART_GET_RX_EMPTY(sc)
Get RX FIFO empty flag status from register.
Definition: scuart.h:130
SCUART_PARITY_NONE
#define SCUART_PARITY_NONE
Definition: scuart.h:37
SCUART_Open
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
__HIRC12M
#define __HIRC12M
Definition: system_Nano100Series.h:28
CLK_CLKDIV0_SC0_N_Msk
#define CLK_CLKDIV0_SC0_N_Msk
Definition: Nano100Series.h:1429
SCUART_GET_TX_FULL
#define SCUART_GET_TX_FULL(sc)
Get TX FIFO full flag status from register.
Definition: scuart.h:80
SCUART_Write
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
__HXT
#define __HXT
Definition: system_Nano100Series.h:26
SCUART_CHAR_LEN_8
#define SCUART_CHAR_LEN_8
Definition: scuart.h:35
SC_T::RFTMR
__IO uint32_t RFTMR
Definition: Nano100Series.h:8506
SC_T::THR
__O uint32_t THR
Definition: Nano100Series.h:8300
CLK_CLKDIV0_SC0_N_Pos
#define CLK_CLKDIV0_SC0_N_Pos
Definition: Nano100Series.h:1428
SC_T::CTL
__IO uint32_t CTL
Definition: Nano100Series.h:8388
SC_T::IER
__IO uint32_t IER
Definition: Nano100Series.h:8582
SCUART_Close
void SCUART_Close(SC_T *sc)
The function is used to disable smartcard interface UART mode.
Definition: scuart.c:32
SC_UACTL_UA_MODE_EN_Msk
#define SC_UACTL_UA_MODE_EN_Msk
Definition: Nano100Series.h:9160
SC_ETUCR_ETU_RDIV_Msk
#define SC_ETUCR_ETU_RDIV_Msk
Definition: Nano100Series.h:8983
SC_T::UACTL
__IO uint32_t UACTL
Definition: Nano100Series.h:8857