| M480 BSP V3.05.001 The Board Support Package for M480 Series

scuart.c
Go to the documentation of this file.
1 /**************************************************************************/
9 #include "NuMicro.h"
10
11 static uint32_t SCUART_GetClock(SC_T *sc);
12
31 void SCUART_Close(SC_T* sc)
32 {
33 sc->INTEN = 0UL;
34 sc->UARTCTL = 0UL;
35 sc->CTL = 0UL;
36
37 }
44 static uint32_t SCUART_GetClock(SC_T *sc)
45 {
46 uint32_t u32ClkSrc, u32Num, u32Clk;
47
48 if(sc == SC0)
49 {
50 u32Num = 0UL;
51 }
52 else if(sc == SC1)
53 {
54 u32Num = 1UL;
55 }
56 else
57 {
58 u32Num = 2UL;
59 }
60
61 u32ClkSrc = (CLK->CLKSEL3 >> (2UL * u32Num)) & CLK_CLKSEL3_SC0SEL_Msk;
62
63 /* Get smartcard module clock */
64 if(u32ClkSrc == 0UL)
65 {
66 u32Clk = __HXT;
67 }
68 else if(u32ClkSrc == 1UL)
69 {
70 u32Clk = CLK_GetPLLClockFreq();
71 }
72 else if(u32ClkSrc == 2UL)
73 {
74 if(u32Num == 1UL)
75 {
76 u32Clk = CLK_GetPCLK1Freq();
77 }
78 else
79 {
80 u32Clk = CLK_GetPCLK0Freq();
81 }
82 }
83 else
84 {
85 u32Clk = __HIRC;
86 }
87
88 u32Clk /= (((CLK->CLKDIV1 >> (8UL * u32Num)) & CLK_CLKDIV1_SC0DIV_Msk) + 1UL);
89
90
91 return u32Clk;
92 }
93
109 uint32_t SCUART_Open(SC_T* sc, uint32_t u32baudrate)
110 {
111 uint32_t u32Clk = SCUART_GetClock(sc), u32Div;
112
113 /* Calculate divider for target baudrate */
114 u32Div = (u32Clk + (u32baudrate >> 1) - 1UL) / u32baudrate - 1UL;
115
116 /* Enable smartcard interface and stop bit = 1 */
117 sc->CTL = SC_CTL_SCEN_Msk | SC_CTL_NSB_Msk;
118 /* Enable UART mode, disable parity and 8 bit per character */
119 sc->UARTCTL = SCUART_CHAR_LEN_8 | SCUART_PARITY_NONE | SC_UARTCTL_UARTEN_Msk;
120 sc->ETUCTL = u32Div;
121
122 return(u32Clk / (u32Div + 1UL));
123 }
124
133 uint32_t SCUART_Read(SC_T* sc, uint8_t pu8RxBuf[], uint32_t u32ReadBytes)
134 {
135 uint32_t u32Count;
136
137 for(u32Count = 0UL; u32Count < u32ReadBytes; u32Count++)
138 {
139 if(SCUART_GET_RX_EMPTY(sc)) /* no data available */
140 {
141 break;
142 }
143 pu8RxBuf[u32Count] = (uint8_t)SCUART_READ(sc); /* get data from FIFO */
144 }
145
146 return u32Count;
147 }
148
172 uint32_t SCUART_SetLineConfig(SC_T* sc, uint32_t u32Baudrate, uint32_t u32DataWidth, uint32_t u32Parity, uint32_t u32StopBits)
173 {
174
175 uint32_t u32Clk = SCUART_GetClock(sc), u32Div;
176
177 if(u32Baudrate == 0UL) /* keep original baudrate setting */
178 {
179 u32Div = sc->ETUCTL & SC_ETUCTL_ETURDIV_Msk;
180 }
181 else
182 {
183 /* Calculate divider for target baudrate */
184 u32Div = (u32Clk + (u32Baudrate >> 1) - 1UL)/ u32Baudrate - 1UL;
185 sc->ETUCTL = u32Div;
186 }
187 /* Set stop bit */
188 sc->CTL = u32StopBits | SC_CTL_SCEN_Msk;
189 /* Set character width and parity */
190 sc->UARTCTL = u32Parity | u32DataWidth | SC_UARTCTL_UARTEN_Msk;
191
192 return(u32Clk / (u32Div + 1UL));
193 }
194
205 void SCUART_SetTimeoutCnt(SC_T* sc, uint32_t u32TOC)
206 {
207 sc->RXTOUT= u32TOC;
208 }
209
210
219 void SCUART_Write(SC_T* sc,uint8_t pu8TxBuf[], uint32_t u32WriteBytes)
220 {
221 uint32_t u32Count;
222
223 for(u32Count = 0UL; u32Count != u32WriteBytes; u32Count++)
224 {
225 /* Wait 'til FIFO not full */
226 while(SCUART_GET_TX_FULL(sc))
227 {
228 ;
229 }
230 /* Write 1 byte to FIFO */
231 sc->DAT = pu8TxBuf[u32Count];
232 }
233 }
234
235 /* end of group SCUART_EXPORTED_FUNCTIONS */
237 /* end of group SCUART_Driver */
239 /* end of group Standard_Driver */
241
242 /*** (C) COPYRIGHT 2016 Nuvoton Technology Corp. ***/
SCUART_PARITY_NONE
#define SCUART_PARITY_NONE
Definition: scuart.h:34
CLK
#define CLK
Definition: M480.h:368
SC_T
Definition: sc_reg.h:26
SC_T::CTL
__IO uint32_t CTL
Definition: sc_reg.h:1485
SCUART_GetClock
static uint32_t SCUART_GetClock(SC_T *sc)
SC1
#define SC1
Definition: M480.h:441
SCUART_GET_TX_FULL
#define SCUART_GET_TX_FULL(sc)
Get TX FIFO full flag status from register.
Definition: scuart.h:77
SC_ETUCTL_ETURDIV_Msk
#define SC_ETUCTL_ETURDIV_Msk
Definition: sc_reg.h:1613
SCUART_Close
void SCUART_Close(SC_T *sc)
The function is used to disable smartcard interface UART mode.
Definition: scuart.c:31
NuMicro.h
NuMicro peripheral access layer header file.
SC_T::ETUCTL
__IO uint32_t ETUCTL
Definition: sc_reg.h:1489
CLK_GetPCLK1Freq
uint32_t CLK_GetPCLK1Freq(void)
Get PCLK1 frequency.
Definition: clk.c:204
SC_T::INTEN
__IO uint32_t INTEN
Definition: sc_reg.h:1490
CLK_GetPCLK0Freq
uint32_t CLK_GetPCLK0Freq(void)
Get PCLK0 frequency.
Definition: clk.c:164
SC_CTL_SCEN_Msk
#define SC_CTL_SCEN_Msk
Definition: sc_reg.h:1514
SC_UARTCTL_UARTEN_Msk
#define SC_UARTCTL_UARTEN_Msk
Definition: sc_reg.h:1796
SCUART_GET_RX_EMPTY
#define SCUART_GET_RX_EMPTY(sc)
Get RX FIFO empty flag status from register.
Definition: scuart.h:136
SCUART_READ
#define SCUART_READ(sc)
Read Rx data register.
Definition: scuart.h:126
CLK_GetPLLClockFreq
uint32_t CLK_GetPLLClockFreq(void)
Get PLL clock frequency.
Definition: clk.c:1188
SC_CTL_NSB_Msk
#define SC_CTL_NSB_Msk
Definition: sc_reg.h:1538
SC_T::DAT
__IO uint32_t DAT
Definition: sc_reg.h:1484
CLK_CLKSEL3_SC0SEL_Msk
#define CLK_CLKSEL3_SC0SEL_Msk
Definition: clk_reg.h:2580
SCUART_SetTimeoutCnt
void SCUART_SetTimeoutCnt(SC_T *sc, uint32_t u32TOC)
This function use to set receive timeout count.
Definition: scuart.c:205
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:133
SC_T::UARTCTL
__IO uint32_t UARTCTL
Definition: sc_reg.h:1497
__HIRC
#define __HIRC
Definition: system_M480.h:36
SC_T::RXTOUT
__IO uint32_t RXTOUT
Definition: sc_reg.h:1488
SC0
#define SC0
Definition: M480.h:440
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:219
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 configure smartcard UART mode line setting.
Definition: scuart.c:172
__HXT
#define __HXT
Definition: system_M480.h:29
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:109
SCUART_CHAR_LEN_8
#define SCUART_CHAR_LEN_8
Definition: scuart.h:32
CLK_CLKDIV1_SC0DIV_Msk
#define CLK_CLKDIV1_SC0DIV_Msk
Definition: clk_reg.h:2634