NANO103 BSP V3.01.004
The Board Support Package for Nano103 Series
scuart.c
Go to the documentation of this file.
1/**************************************************************************/
12#include "Nano103.h"
13
34{
35 sc->INTEN = 0;
36 sc->UARTCTL = 0;
37 sc->CTL = 0;
38
39}
40
42
47static uint32_t SCUART_GetClock(SC_T *sc)
48{
49 uint32_t u32Reg;
50 uint32_t u32Clk;
51
52
53 if(sc == (SC_T *)SC0)
54 {
55 u32Reg = (CLK->CLKSEL2 & CLK_CLKSEL2_SC0SEL_Msk) >> CLK_CLKSEL2_SC0SEL_Pos;
56 }
57 else
58 {
59 u32Reg = (CLK->CLKSEL2 & CLK_CLKSEL2_SC1SEL_Msk) >> CLK_CLKSEL2_SC1SEL_Pos;
60 }
61
63 {
64 u32Clk = __HXT;
65 }
67 {
68 u32Clk = CLK_GetPLLClockFreq();
69 }
71 {
72 if(CLK->CLKSEL0 & CLK_CLKSEL0_HIRCSEL_Msk)
73 {
74 u32Clk = __HIRC36M;
75 }
76 else
77 {
78 u32Clk = __HIRC12M;
79 }
80 }
82 {
83 u32Clk = __MIRC;
84 }
85 else
86 u32Clk = SystemCoreClock;
87
88 if(sc == (SC_T *)SC0)
89 {
90 u32Clk /= (((CLK->CLKDIV0 & CLK_CLKDIV0_SC0DIV_Msk) >> (CLK_CLKDIV0_SC0DIV_Pos)) + 1);
91 }
92 else
93 {
94 u32Clk /= (((CLK->CLKDIV1 & CLK_CLKDIV1_SC1DIV_Msk) >> (CLK_CLKDIV1_SC1DIV_Pos)) + 1);
95 }
96 return u32Clk;
97}
98
100
101
116uint32_t SCUART_Open(SC_T* sc, uint32_t u32baudrate)
117{
118 uint32_t u32Clk = SCUART_GetClock(sc), u32Div;
119
120 // Calculate divider for target baudrate
121 u32Div = (u32Clk + (u32baudrate >> 1) - 1) / u32baudrate - 1;
122
123 sc->CTL = SC_CTL_SCEN_Msk | SC_CTL_NSB_Msk; // Enable smartcard interface and stop bit = 1
124 sc->UARTCTL = SCUART_CHAR_LEN_8 | SCUART_PARITY_NONE | SC_UARTCTL_UARTEN_Msk; // Enable UART mode, disable parity and 8 bit per character
125 sc->ETUCTL = u32Div;
126
127 return(u32Clk / (u32Div + 1));
128}
129
138uint32_t SCUART_Read(SC_T* sc, uint8_t *pu8RxBuf, uint32_t u32ReadBytes)
139{
140 uint32_t u32Count;
141
142 for(u32Count = 0; u32Count < u32ReadBytes; u32Count++)
143 {
144 if(SCUART_GET_RX_EMPTY(sc)) // no data available
145 {
146 break;
147 }
148 pu8RxBuf[u32Count] = SCUART_READ(sc); // get data from FIFO
149 }
150
151 return u32Count;
152}
153
178uint32_t SCUART_SetLineConfig(SC_T* sc, uint32_t u32Baudrate, uint32_t u32DataWidth, uint32_t u32Parity, uint32_t u32StopBits)
179{
180 uint32_t u32Clk = SCUART_GetClock(sc), u32Div;
181
182 if(u32Baudrate == 0) // keep original baudrate setting
183 {
184 u32Div = sc->ETUCTL & SC_ETUCTL_ETURDIV_Msk;
185 }
186 else
187 {
188 // Calculate divider for target baudrate
189 u32Div = (u32Clk + (u32Baudrate >> 1) - 1)/ u32Baudrate - 1;
190 sc->ETUCTL = u32Div;
191 }
192
193 sc->CTL = u32StopBits | SC_CTL_SCEN_Msk; // Set stop bit
194 sc->UARTCTL = u32Parity | u32DataWidth | SC_UARTCTL_UARTEN_Msk; // Set character width and parity
195
196 return(u32Clk / (u32Div + 1));
197}
198
209void SCUART_SetTimeoutCnt(SC_T* sc, uint32_t u32TOC)
210{
211 sc->RXTOUT = u32TOC;
212}
213
214
224uint32_t SCUART_Write(SC_T* sc,uint8_t *pu8TxBuf, uint32_t u32WriteBytes)
225{
226 uint32_t u32Count;
227 /* Baudrate * (start bit + 8-bit data + 1-bit parity + 2-bit stop) */
228 uint32_t u32Delay = (SystemCoreClock / SCUART_GetClock(sc)) * sc->ETUCTL * 12, i;
229
231 for(u32Count = 0; u32Count != u32WriteBytes; u32Count++)
232 {
233 i = 0;
234 /* Wait 'til FIFO not full */
235 while(SCUART_GET_TX_FULL(sc))
236 {
237 /* Block longer than expected. Maybe some interrupt disable SCUART clock? */
238 if(i++ > u32Delay)
239 {
241 return u32Count;
242 }
243 }
244 sc->DAT = pu8TxBuf[u32Count]; // Write 1 byte to FIFO
245 }
246
247 return u32Count;
248}
249
250 /* end of group NANO103_SCUART_EXPORTED_FUNCTIONS */
252 /* end of group NANO103_SCUART_Driver */
254 /* end of group NANO103_Device_Driver */
256
257/*** (C) COPYRIGHT 2015 Nuvoton Technology Corp. ***/
NANO103 peripheral access layer header file. This file contains all the peripheral register's definit...
#define SC_CTL_NSB_Msk
Definition: Nano103.h:11838
#define SC_ETUCTL_ETURDIV_Msk
Definition: Nano103.h:11907
#define SC_CTL_SCEN_Msk
Definition: Nano103.h:11814
#define SC_UARTCTL_UARTEN_Msk
Definition: Nano103.h:12090
#define CLK_CLKSEL2_SC0SEL_HXT
Definition: clk.h:214
#define CLK_CLKSEL2_SC0SEL_PLL
Definition: clk.h:215
#define CLK_CLKSEL2_SC0SEL_HIRC
Definition: clk.h:216
#define CLK_CLKSEL2_SC0SEL_MIRC
Definition: clk.h:217
uint32_t CLK_GetPLLClockFreq(void)
This function get PLL frequency. The frequency unit is Hz.
Definition: clk.c:177
#define CLK_CLKSEL2_SC1SEL_Pos
Definition: Nano103.h:2843
#define CLK_CLKDIV1_SC1DIV_Pos
Definition: Nano103.h:2867
#define CLK_CLKDIV0_SC0DIV_Msk
Definition: Nano103.h:2865
#define CLK_CLKSEL2_SC0SEL_Msk
Definition: Nano103.h:2841
#define CLK_CLKSEL2_SC1SEL_Msk
Definition: Nano103.h:2844
#define CLK_CLKSEL2_SC0SEL_Pos
Definition: Nano103.h:2840
#define CLK_CLKDIV1_SC1DIV_Msk
Definition: Nano103.h:2868
#define CLK_CLKDIV0_SC0DIV_Pos
Definition: Nano103.h:2864
#define CLK_CLKSEL0_HIRCSEL_Msk
Definition: Nano103.h:2796
#define CLK
Pointer to CLK register structure.
Definition: Nano103.h:13802
#define SC0
Pointer to SC0 register structure.
Definition: Nano103.h:13797
int32_t g_SCUART_i32ErrCode
Definition: scuart.c:22
#define SCUART_CHAR_LEN_8
Definition: scuart.h:35
#define SCUART_PARITY_NONE
Definition: scuart.h:37
#define SCUART_TIMEOUT_ERR
Definition: scuart.h:44
void SCUART_SetTimeoutCnt(SC_T *sc, uint32_t u32TOC)
This function use to set receive timeout count.
Definition: scuart.c:209
#define SCUART_GET_TX_FULL(sc)
Get TX FIFO full flag status from register.
Definition: scuart.h:81
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:138
#define SCUART_GET_RX_EMPTY(sc)
Get RX FIFO empty flag status from register.
Definition: scuart.h:131
#define SCUART_READ(sc)
Read Rx data register.
Definition: scuart.h:121
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:178
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:116
uint32_t 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:224
void SCUART_Close(SC_T *sc)
The function is used to disable smartcard interface UART mode.
Definition: scuart.c:33
__IO uint32_t DAT
Definition: Nano103.h:11784
__IO uint32_t ETUCTL
Definition: Nano103.h:11789
__IO uint32_t INTEN
Definition: Nano103.h:11790
__IO uint32_t CTL
Definition: Nano103.h:11785
__IO uint32_t UARTCTL
Definition: Nano103.h:11797
__IO uint32_t RXTOUT
Definition: Nano103.h:11788
#define __HXT
uint32_t SystemCoreClock
#define __HIRC36M
#define __HIRC12M
#define __MIRC