NANO102/112 BSP V3.03.003
The Board Support Package for Nano102/112 Series
scuart.c
Go to the documentation of this file.
1/**************************************************************************/
12#include "Nano1X2Series.h"
13
33{
34 sc->IER = 0;
35 sc->UACTL = 0;
36 sc->CTL = 0;
37
38}
39
41
46static 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 {
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
184void SCUART_SetTimeoutCnt(SC_T* sc, uint32_t u32TOC)
185{
186 sc->RFTMR = u32TOC;
187}
188
189
198void 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. ***/
Nano102/112 peripheral access layer header file. This file contains all the peripheral register's def...
#define SC_CTL_SLEN_Msk
#define SC_UACTL_UA_MODE_EN_Msk
#define SC_CTL_SC_CEN_Msk
#define SYS_IRCTRIMCTL_TRIM_SEL_Msk
#define SC_ETUCR_ETU_RDIV_Msk
uint32_t CLK_GetHCLKFreq(void)
This function get HCLK frequency. The frequency unit is Hz.
Definition: clk.c:192
uint32_t CLK_GetPLLClockFreq(void)
This function get PLL frequency. The frequency unit is Hz.
Definition: clk.c:228
#define CLK_CLKSEL2_SC_S_Msk
#define CLK_CLKDIV0_SC0_N_Pos
#define CLK_CLKDIV0_SC0_N_Msk
#define CLK_CLKSEL2_SC_S_Pos
#define CLK_CLKDIV1_SC1_N_Msk
#define CLK
Pointer to CLK register structure.
#define SC0
Pointer to SC0 register structure.
#define SYS
Pointer to SYS register structure.
#define SCUART_CHAR_LEN_8
Definition: scuart.h:35
#define SCUART_PARITY_NONE
Definition: scuart.h:37
void SCUART_SetTimeoutCnt(SC_T *sc, uint32_t u32TOC)
This function use to set receive timeout count.
Definition: scuart.c:184
#define SCUART_GET_TX_FULL(sc)
Get TX FIFO full flag status from register.
Definition: scuart.h:80
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
#define SCUART_GET_RX_EMPTY(sc)
Get RX FIFO empty flag status from register.
Definition: scuart.h:130
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
#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:153
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
void SCUART_Close(SC_T *sc)
The function is used to disable smartcard interface UART mode.
Definition: scuart.c:32
#define SYS_IRCTRIMCTL_TRIM_16M
Definition: sys.h:78
__IO uint32_t UACTL
__IO uint32_t ETUCR
__IO uint32_t CTL
__IO uint32_t RFTMR
__O uint32_t THR
__IO uint32_t IER
#define __HXT
uint32_t SystemCoreClock
#define __HIRC16M
#define __HIRC12M