| NANO102/112 BSP V3.04.000 The Board Support Package for Nano102/112 Series

scuart.c
Go to the documentation of this file.
1/**************************************************************************/
12#include "Nano1X2Series.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 if(u32ClkSrc == 2)
57 {
58 if((SYS->IRCTRIMCTL & SYS_IRCTRIMCTL_TRIM_SEL_Msk) == SYS_IRCTRIMCTL_TRIM_16M)
59 u32Clk = __HIRC16M;
60 else
61 u32Clk = __HIRC12M;
62 }
63 else
64 u32Clk = CLK_GetHCLKFreq();
65
66 if(sc == SC0)
67 u32Clk /= ((CLK->CLKDIV0 & CLK_CLKDIV0_SC0_N_Msk) >> CLK_CLKDIV0_SC0_N_Pos) + 1;
68 else
69 u32Clk /= (CLK->CLKDIV1 & CLK_CLKDIV1_SC1_N_Msk) + 1;
70
71 return u32Clk;
72}
73
75
76
91uint32_t SCUART_Open(SC_T* sc, uint32_t u32baudrate)
92{
93 uint32_t u32Clk = SCUART_GetClock(sc), u32Div;
94
95 // Calculate divider for target baudrate
96 u32Div = (u32Clk + (u32baudrate >> 1) - 1) / u32baudrate - 1;
97
98 sc->CTL = SC_CTL_SC_CEN_Msk | SC_CTL_SLEN_Msk; // Enable smartcard interface and stop bit = 1
99 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
100 sc->ETUCR = u32Div;
101
102 return(u32Clk / (u32Div + 1));
103}
104
113uint32_t SCUART_Read(SC_T* sc, uint8_t *pu8RxBuf, uint32_t u32ReadBytes)
114{
115 uint32_t u32Count;
116
117 for(u32Count = 0; u32Count < u32ReadBytes; u32Count++)
118 {
119 if(SCUART_GET_RX_EMPTY(sc)) // no data available
120 {
121 break;
122 }
123 pu8RxBuf[u32Count] = SCUART_READ(sc); // get data from FIFO
124 }
125
126 return u32Count;
127}
128
153uint32_t SCUART_SetLineConfig(SC_T* sc, uint32_t u32Baudrate, uint32_t u32DataWidth, uint32_t u32Parity, uint32_t u32StopBits)
154{
155 uint32_t u32Clk = SCUART_GetClock(sc), u32Div;
156
157 if(u32Baudrate == 0) // keep original baudrate setting
158 {
159 u32Div = sc->ETUCR & SC_ETUCR_ETU_RDIV_Msk;
160 }
161 else
162 {
163 // Calculate divider for target baudrate
164 u32Div = (u32Clk + (u32Baudrate >> 1) - 1)/ u32Baudrate - 1;
165 sc->ETUCR = u32Div;
166 }
167
168 sc->CTL = u32StopBits | SC_CTL_SC_CEN_Msk; // Set stop bit
169 sc->UACTL = u32Parity | u32DataWidth | SC_UACTL_UA_MODE_EN_Msk; // Set character width and parity
170
171 return(u32Clk / (u32Div + 1));
172}
173
184 void SCUART_SetTimeoutCnt(SC_T* sc, uint32_t u32TOC)
185{
186 sc->RFTMR = u32TOC;
187}
188
189
198 void SCUART_Write(SC_T* sc,uint8_t *pu8TxBuf, uint32_t u32WriteBytes)
199{
200 uint32_t u32Count;
201 int32_t i32TimeoutCnt = SystemCoreClock/10;
202
203 for(u32Count = 0; u32Count != u32WriteBytes; u32Count++)
204 {
205 while(SCUART_GET_TX_FULL(sc)) { // Wait 'til FIFO not full
206 if(i32TimeoutCnt-- <= 0)
207 break;
208 }
209 sc->THR = pu8TxBuf[u32Count]; // Write 1 byte to FIFO
210 }
211}
212
213 /* end of group NANO1X2_SCUART_EXPORTED_FUNCTIONS */
215 /* end of group NANO1X2_SCUART_Driver */
217 /* end of group NANO1X2_Device_Driver */
219
220/*** (C) COPYRIGHT 2014 Nuvoton Technology Corp. ***/
Nano1X2Series.h
Nano102/112 peripheral access layer header file. This file contains all the peripheral register's def...
SC_CTL_SLEN_Msk
#define SC_CTL_SLEN_Msk
Definition: Nano1X2Series.h:8190
SC_UACTL_UA_MODE_EN_Msk
#define SC_UACTL_UA_MODE_EN_Msk
Definition: Nano1X2Series.h:8430
SC_CTL_SC_CEN_Msk
#define SC_CTL_SC_CEN_Msk
Definition: Nano1X2Series.h:8166
SYS_IRCTRIMCTL_TRIM_SEL_Msk
#define SYS_IRCTRIMCTL_TRIM_SEL_Msk
Definition: Nano1X2Series.h:4035
SC_ETUCR_ETU_RDIV_Msk
#define SC_ETUCR_ETU_RDIV_Msk
Definition: Nano1X2Series.h:8253
CLK_GetHCLKFreq
uint32_t CLK_GetHCLKFreq(void)
This function get HCLK frequency. The frequency unit is Hz.
Definition: clk.c:192
CLK_GetPLLClockFreq
uint32_t CLK_GetPLLClockFreq(void)
This function get PLL frequency. The frequency unit is Hz.
Definition: clk.c:228
CLK_CLKSEL2_SC_S_Msk
#define CLK_CLKSEL2_SC_S_Msk
Definition: Nano1X2Series.h:1607
CLK_CLKDIV0_SC0_N_Pos
#define CLK_CLKDIV0_SC0_N_Pos
Definition: Nano1X2Series.h:1624
CLK_CLKDIV0_SC0_N_Msk
#define CLK_CLKDIV0_SC0_N_Msk
Definition: Nano1X2Series.h:1625
CLK_CLKSEL2_SC_S_Pos
#define CLK_CLKSEL2_SC_S_Pos
Definition: Nano1X2Series.h:1606
CLK_CLKDIV1_SC1_N_Msk
#define CLK_CLKDIV1_SC1_N_Msk
Definition: Nano1X2Series.h:1628
CLK
#define CLK
Pointer to CLK register structure.
Definition: Nano1X2Series.h:10651
SC0
#define SC0
Pointer to SC0 register structure.
Definition: Nano1X2Series.h:10646
SYS
#define SYS
Pointer to SYS register structure.
Definition: Nano1X2Series.h:10650
SCUART_CHAR_LEN_8
#define SCUART_CHAR_LEN_8
Definition: scuart.h:35
SCUART_PARITY_NONE
#define SCUART_PARITY_NONE
Definition: scuart.h:37
SCUART_SetTimeoutCnt
void SCUART_SetTimeoutCnt(SC_T *sc, uint32_t u32TOC)
This function use to set receive timeout count.
Definition: scuart.c:184
SCUART_GET_TX_FULL
#define SCUART_GET_TX_FULL(sc)
Get TX FIFO full flag status from register.
Definition: scuart.h:80
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:113
SCUART_GET_RX_EMPTY
#define SCUART_GET_RX_EMPTY(sc)
Get RX FIFO empty flag status from register.
Definition: scuart.h:130
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:198
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:153
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:91
SCUART_Close
void SCUART_Close(SC_T *sc)
The function is used to disable smartcard interface UART mode.
Definition: scuart.c:32
SYS_IRCTRIMCTL_TRIM_16M
#define SYS_IRCTRIMCTL_TRIM_16M
Definition: sys.h:78
SC_T::UACTL
__IO uint32_t UACTL
Definition: Nano1X2Series.h:8127
SC_T::ETUCR
__IO uint32_t ETUCR
Definition: Nano1X2Series.h:7796
SC_T::CTL
__IO uint32_t CTL
Definition: Nano1X2Series.h:7658
SC_T::RFTMR
__IO uint32_t RFTMR
Definition: Nano1X2Series.h:7776
SC_T::THR
__O uint32_t THR
Definition: Nano1X2Series.h:7570
SC_T::IER
__IO uint32_t IER
Definition: Nano1X2Series.h:7852
SC_T
Definition: Nano1X2Series.h:7544
__HXT
#define __HXT
Definition: system_Nano1X2Series.h:26
SystemCoreClock
uint32_t SystemCoreClock
Definition: system_Nano1X2Series.c:20
__HIRC16M
#define __HIRC16M
Definition: system_Nano1X2Series.h:29
__HIRC12M
#define __HIRC12M
Definition: system_Nano1X2Series.h:28