| NUC472_NUC442_BSP V3.04.000 The Board Support Package for NUC472/NUC442

scuart.c
Go to the documentation of this file.
1/**************************************************************************/
12#include "NUC472_442.h"
13
27int32_t g_SCUART_i32ErrCode = 0;
34 void SCUART_Close(SC_T* sc)
35{
36 sc->INTEN = 0;
37 sc->UARTCTL = 0;
38 sc->CTL = 0;
39
40}
42
47static uint32_t SCUART_GetClock(SC_T *sc)
48{
49 uint32_t u32ClkSrc, u32Num, u32Clk;
50
51 if(sc == SC0)
52 u32Num = 0;
53 else if(sc == SC1)
54 u32Num = 1;
55 else if(sc == SC2)
56 u32Num = 2;
57 else if(sc == SC3)
58 u32Num = 3;
59 else if(sc == SC4)
60 u32Num = 4;
61 else
62 u32Num = 5;
63
64 u32ClkSrc = (CLK->CLKSEL3 >> (2 * u32Num)) & CLK_CLKSEL3_SC0SEL_Msk;
65
66 // Get smartcard module clock
67 if(u32ClkSrc == 0)
68 u32Clk = __HXT;
69 else if(u32ClkSrc == 1)
70 u32Clk = CLK_GetPLLClockFreq();
71 else if(u32ClkSrc == 2)
72 u32Clk = CLK_GetPCLKFreq();
73 else
74 u32Clk = __HIRC;
75
76 if(u32Num < 4)
77 {
78 u32Clk /= (((CLK->CLKDIV1 >> (8 * u32Num)) & CLK_CLKDIV1_SC0DIV_Msk) + 1);
79 }
80 else
81 {
82 u32Clk /= (((CLK->CLKDIV2 >> (8 * (u32Num - 4))) & CLK_CLKDIV2_SC4DIV_Msk) + 1);
83 }
84
85 return u32Clk;
86}
87
89
104uint32_t SCUART_Open(SC_T* sc, uint32_t u32baudrate)
105{
106 uint32_t u32Clk = SCUART_GetClock(sc), u32Div;
107
108 // Calculate divider for target baudrate
109 u32Div = (u32Clk + (u32baudrate >> 1) - 1) / u32baudrate - 1;
110
111 sc->CTL = SC_CTL_SCEN_Msk | SC_CTL_NSB_Msk; // Enable smartcard interface and stop bit = 1
112 sc->UARTCTL = SCUART_CHAR_LEN_8 | SCUART_PARITY_NONE | SC_UARTCTL_UARTEN_Msk; // Enable UART mode, disable parity and 8 bit per character
113 sc->ETUCTL = u32Div;
114
115 return(u32Clk / u32Div);
116}
117
126uint32_t SCUART_Read(SC_T* sc, uint8_t *pu8RxBuf, uint32_t u32ReadBytes)
127{
128 uint32_t u32Count;
129
130 for(u32Count = 0; u32Count < u32ReadBytes; u32Count++)
131 {
132 if(SCUART_GET_RX_EMPTY(sc)) // no data available
133 {
134 break;
135 }
136 pu8RxBuf[u32Count] = SCUART_READ(sc); // get data from FIFO
137 }
138
139 return u32Count;
140}
141
166uint32_t SCUART_SetLineConfig(SC_T* sc, uint32_t u32Baudrate, uint32_t u32DataWidth, uint32_t u32Parity, uint32_t u32StopBits)
167{
168
169 uint32_t u32Clk = SCUART_GetClock(sc), u32Div;
170
171 if(u32Baudrate == 0) // keep original baudrate setting
172 {
173 u32Div = sc->ETUCTL & SC_ETUCTL_ETURDIV_Msk;
174 }
175 else
176 {
177 // Calculate divider for target baudrate
178 u32Div = (u32Clk + (u32Baudrate >> 1) - 1)/ u32Baudrate - 1;
179 sc->ETUCTL = u32Div;
180 }
181
182 sc->CTL = u32StopBits | SC_CTL_SCEN_Msk; // Set stop bit
183 sc->UARTCTL = u32Parity | u32DataWidth | SC_UARTCTL_UARTEN_Msk; // Set character width and parity
184
185 return(u32Clk / u32Div);
186}
187
198 void SCUART_SetTimeoutCnt(SC_T* sc, uint32_t u32TOC)
199{
200 sc->RXTOUT= u32TOC;
201}
202
203
213uint32_t SCUART_Write(SC_T* sc,uint8_t *pu8TxBuf, uint32_t u32WriteBytes)
214{
215 uint32_t u32Count;
216 /* Baudrate * (start bit + 8-bit data + 1-bit parity + 2-bit stop) */
217 uint32_t u32Delay = (SystemCoreClock / SCUART_GetClock(sc)) * sc->ETUCTL * 12, i;
218
219 g_SCUART_i32ErrCode = 0;
220 for(u32Count = 0UL; u32Count != u32WriteBytes; u32Count++)
221 {
222 i = 0;
223 /* Wait 'til FIFO not full */
224 while(SCUART_GET_TX_FULL(sc))
225 {
226 /* Block longer than expected. Maybe some interrupt disable SCUART clock? */
227 if(i++ > u32Delay)
228 {
229 g_SCUART_i32ErrCode = SCUART_TIMEOUT_ERR;
230 return u32Count;
231 }
232 }
233 /* Write 1 byte to FIFO */
234 sc->DAT = pu8TxBuf[u32Count];
235 }
236 return u32Count;
237}
238
239 /* end of group NUC472_442_SCUART_EXPORTED_FUNCTIONS */
241 /* end of group NUC472_442_SCUART_Driver */
243 /* end of group NUC472_442_Device_Driver */
245
246/*** (C) COPYRIGHT 2014 Nuvoton Technology Corp. ***/
NUC472_442.h
NUC472/NUC442 peripheral access layer header file. This file contains all the peripheral register's d...
CLK_CLKSEL3_SC0SEL_Msk
#define CLK_CLKSEL3_SC0SEL_Msk
Definition: NUC472_442.h:3836
SC_CTL_NSB_Msk
#define SC_CTL_NSB_Msk
Definition: NUC472_442.h:21777
CLK_CLKDIV1_SC0DIV_Msk
#define CLK_CLKDIV1_SC0DIV_Msk
Definition: NUC472_442.h:3875
SC_ETUCTL_ETURDIV_Msk
#define SC_ETUCTL_ETURDIV_Msk
Definition: NUC472_442.h:21849
CLK_CLKDIV2_SC4DIV_Msk
#define CLK_CLKDIV2_SC4DIV_Msk
Definition: NUC472_442.h:3887
SC_CTL_SCEN_Msk
#define SC_CTL_SCEN_Msk
Definition: NUC472_442.h:21753
SC_UARTCTL_UARTEN_Msk
#define SC_UARTCTL_UARTEN_Msk
Definition: NUC472_442.h:22026
CLK_GetPCLKFreq
uint32_t CLK_GetPCLKFreq(void)
This function get PCLK frequency. The frequency unit is Hz.
Definition: clk.c:104
CLK_GetPLLClockFreq
uint32_t CLK_GetPLLClockFreq(void)
This function get PLL frequency. The frequency unit is Hz.
Definition: clk.c:166
CLK
#define CLK
Definition: NUC472_442.h:28798
SC4
#define SC4
Definition: NUC472_442.h:28868
SC2
#define SC2
Definition: NUC472_442.h:28866
SC1
#define SC1
Definition: NUC472_442.h:28865
SC3
#define SC3
Definition: NUC472_442.h:28867
SC0
#define SC0
Definition: NUC472_442.h:28864
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_TIMEOUT_ERR
#define SCUART_TIMEOUT_ERR
Definition: scuart.h:44
SCUART_SetTimeoutCnt
void SCUART_SetTimeoutCnt(SC_T *sc, uint32_t u32TOC)
This function use to set receive timeout count.
Definition: scuart.c:198
SCUART_GET_TX_FULL
#define SCUART_GET_TX_FULL(sc)
Get TX FIFO full flag status from register.
Definition: scuart.h:82
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:126
SCUART_GET_RX_EMPTY
#define SCUART_GET_RX_EMPTY(sc)
Get RX FIFO empty flag status from register.
Definition: scuart.h:132
g_SCUART_i32ErrCode
int32_t g_SCUART_i32ErrCode
Definition: scuart.c:27
SCUART_READ
#define SCUART_READ(sc)
Read Rx data register.
Definition: scuart.h:122
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:166
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:104
SCUART_Write
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:213
SCUART_Close
void SCUART_Close(SC_T *sc)
The function is used to disable smartcard interface UART mode.
Definition: scuart.c:34
SC_T::UARTCTL
__IO uint32_t UARTCTL
Definition: NUC472_442.h:21714
SC_T::DAT
__IO uint32_t DAT
Definition: NUC472_442.h:21131
SC_T::CTL
__IO uint32_t CTL
Definition: NUC472_442.h:21226
SC_T::RXTOUT
__IO uint32_t RXTOUT
Definition: NUC472_442.h:21353
SC_T::INTEN
__IO uint32_t INTEN
Definition: NUC472_442.h:21427
SC_T::ETUCTL
__IO uint32_t ETUCTL
Definition: NUC472_442.h:21372
SC_T
Definition: NUC472_442.h:21114
__HIRC
#define __HIRC
Definition: system_NUC472_442.h:27
__HXT
#define __HXT
Definition: system_NUC472_442.h:25
SystemCoreClock
uint32_t SystemCoreClock
Definition: system_NUC472_442.c:17