NUC472_NUC442_BSP V3.03.005
The Board Support Package for NUC472/NUC442
uart.c
Go to the documentation of this file.
1/**************************************************************************/
13#include <stdio.h>
14#include "NUC472_442.h"
15
16/*---------------------------------------------------------------------------------------------------------*/
17/* Includes of local headers */
18/*---------------------------------------------------------------------------------------------------------*/
19
20
43void UART_ClearIntFlag(UART_T* uart, uint32_t u32InterruptFlag)
44{
45 if(u32InterruptFlag & UART_INTSTS_RLSINT_Msk) /* clear Receive Line Status Interrupt */
46 {
49 }
50
51 if(u32InterruptFlag & UART_INTSTS_MODEMINT_Msk) /* clear Modem Interrupt */
53
54 if(u32InterruptFlag & UART_INTSTS_BUFERRINT_Msk) /* clear Buffer Error Interrupt */
55 {
57 }
58
59 if(u32InterruptFlag & UART_INTSTS_RXTOINT_Msk) /* clear Modem Interrupt */
61
62}
63
64
72void UART_Close(UART_T* uart)
73{
74 uart->INTEN = 0;
75}
76
77
86{
88}
89
90
107void UART_DisableInt(UART_T* uart, uint32_t u32InterruptFlag )
108{
109 uart->INTEN &= ~ u32InterruptFlag;
110}
111
112
113
122{
124 uart->MODEM &= ~UART_MODEM_RTS_Msk;
127}
128
129
146void UART_EnableInt(UART_T* uart, uint32_t u32InterruptFlag )
147{
148 uart->INTEN |= u32InterruptFlag;
149}
150
151
160void UART_Open(UART_T* uart, uint32_t u32baudrate)
161{
162 uint8_t u8UartClkSrcSel;
163 uint32_t u32ClkTbl[4] = {__HXT, 0, __HIRC, __HIRC};
164 uint32_t u32Clk;
165 uint32_t u32Baud_Div;
166
167 u32ClkTbl[1] = CLK_GetPLLClockFreq();
168
169 u8UartClkSrcSel = (CLK->CLKSEL1 & CLK_CLKSEL1_UARTSEL_Msk) >> CLK_CLKSEL1_UARTSEL_Pos;
170
174
175 u32Clk = (u32ClkTbl[u8UartClkSrcSel]) / (((CLK->CLKDIV0 & CLK_CLKDIV0_UARTDIV_Msk) >> CLK_CLKDIV0_UARTDIV_Pos) + 1);
176
177 if(u32baudrate != 0)
178 {
179 u32Baud_Div = UART_BAUD_MODE2_DIVIDER(u32Clk, u32baudrate);
180
181 if(u32Baud_Div > 0xFFFF)
182 uart->BAUD = (UART_BAUD_MODE0 | UART_BAUD_MODE0_DIVIDER(u32Clk, u32baudrate));
183 else
184 uart->BAUD = (UART_BAUD_MODE2 | u32Baud_Div);
185 }
186}
187
188
199uint32_t UART_Read(UART_T* uart, uint8_t *pu8RxBuf, uint32_t u32ReadBytes)
200{
201 uint32_t u32Count;
202
203 for(u32Count=0; u32Count < u32ReadBytes; u32Count++)
204 {
205 if(uart->FIFOSTS & UART_FIFOSTS_RXEMPTY_Msk) /* Check RX empty => failed */
206 {
207 return u32Count;
208 }
209 pu8RxBuf[u32Count] = uart->DAT; /* Get Data from UART RX */
210 }
211
212 return u32Count;
213}
214
215
229void UART_SetLine_Config(UART_T* uart, uint32_t u32baudrate, uint32_t u32data_width, uint32_t u32parity, uint32_t u32stop_bits)
230{
231 uint8_t u8UartClkSrcSel;
232 uint32_t u32ClkTbl[4] = {__HXT, 0, __HIRC, __HIRC};
233 uint32_t u32Clk;
234 uint32_t u32Baud_Div = 0;
235
236 u32ClkTbl[1] = CLK_GetPLLClockFreq();
237
238 u8UartClkSrcSel = (CLK->CLKSEL1 & CLK_CLKSEL1_UARTSEL_Msk) >> CLK_CLKSEL1_UARTSEL_Pos;
239
240 u32Clk = (u32ClkTbl[u8UartClkSrcSel]) / (((CLK->CLKDIV0 & CLK_CLKDIV0_UARTDIV_Msk) >> CLK_CLKDIV0_UARTDIV_Pos) + 1);
241
242 if(u32baudrate != 0)
243 {
244 u32Baud_Div = UART_BAUD_MODE2_DIVIDER(u32Clk, u32baudrate);
245
246 if(u32Baud_Div > 0xFFFF)
247 uart->BAUD = (UART_BAUD_MODE0 | UART_BAUD_MODE0_DIVIDER(u32Clk, u32baudrate));
248 else
249 uart->BAUD = (UART_BAUD_MODE2 | u32Baud_Div);
250 }
251
252 uart->LINE = u32data_width | u32parity | u32stop_bits;
253}
254
255
264void UART_SetTimeoutCnt(UART_T* uart, uint32_t u32TOC)
265{
266 uart->TOUT = (uart->TOUT & ~UART_TOUT_TOIC_Msk)| (u32TOC);
268}
269
270
280void UART_SelectIrDAMode(UART_T* uart, uint32_t u32Buadrate, uint32_t u32Direction)
281{
282 uint8_t u8UartClkSrcSel;
283 uint32_t u32ClkTbl[4] = {__HXT, 0, __HIRC, __HIRC};
284 uint32_t u32Clk;
285
286 u32ClkTbl[1] = CLK_GetPLLClockFreq();
287
288 u8UartClkSrcSel = (CLK->CLKSEL1 & CLK_CLKSEL1_UARTSEL_Msk) >> CLK_CLKSEL1_UARTSEL_Pos;
289
290 u32Clk = (u32ClkTbl[u8UartClkSrcSel]) / (((CLK->CLKDIV0 & CLK_CLKDIV0_UARTDIV_Msk) >> CLK_CLKDIV0_UARTDIV_Pos) + 1);
291
292 uart->BAUD = UART_BAUD_MODE0 | UART_BAUD_MODE0_DIVIDER(u32Clk, u32Buadrate);
293
294 uart->IRDA &= ~UART_IRDA_TXINV_Msk;
295 uart->IRDA |= UART_IRDA_RXINV_Msk;
296 uart->IRDA = u32Direction ? uart->IRDA | UART_IRDA_TXEN_Msk : uart->IRDA &~ UART_IRDA_TXEN_Msk;
297 uart->FUNCSEL = (0x2 << UART_FUNCSEL_FUNCSEL_Pos);
298}
299
300
310void UART_SelectRS485Mode(UART_T* uart, uint32_t u32Mode, uint32_t u32Addr)
311{
313 uart->ALTCTL = 0;
314 uart->ALTCTL |= u32Mode | (u32Addr << UART_ALTCTL_ADDRMV_Pos);
315}
316
317
329uint32_t UART_Write(UART_T* uart,uint8_t *pu8TxBuf, uint32_t u32WriteBytes)
330{
331 uint32_t u32Count;
332
333 for(u32Count=0; u32Count != u32WriteBytes; u32Count++)
334 {
335 if(uart->FIFOSTS & UART_FIFOSTS_TXFULL_Msk) /* Wait Tx empty and Time-out manner */
336 {
337 return u32Count;
338 }
339 uart->DAT = pu8TxBuf[u32Count]; /* Send UART Data from buffer */
340 }
341
342 return u32Count;
343}
344
345 /* end of group NUC472_442_UART_EXPORTED_FUNCTIONS */
347 /* end of group NUC472_442_UART_Driver */
349 /* end of group NUC472_442_Device_Driver */
351
352/*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/
353
354
NUC472/NUC442 peripheral access layer header file. This file contains all the peripheral register's d...
#define UART_FIFOSTS_BIF_Msk
Definition: NUC472_442.h:25917
#define UART_FIFOSTS_TXOVIF_Msk
Definition: NUC472_442.h:25938
#define UART_FIFOSTS_RXOVIF_Msk
Definition: NUC472_442.h:25902
#define UART_INTEN_ATORTSEN_Msk
Definition: NUC472_442.h:25839
#define UART_FUNCSEL_FUNCSEL_Pos
Definition: NUC472_442.h:26063
#define UART_INTSTS_BUFERRINT_Msk
Definition: NUC472_442.h:25980
#define UART_FIFOSTS_TXFULL_Msk
Definition: NUC472_442.h:25935
#define UART_ALTCTL_ADDRMV_Pos
Definition: NUC472_442.h:26060
#define UART_FIFOSTS_ADDRDETF_Msk
Definition: NUC472_442.h:25908
#define CLK_CLKDIV0_UARTDIV_Pos
Definition: NUC472_442.h:3865
#define UART_IRDA_RXINV_Msk
Definition: NUC472_442.h:26034
#define UART_INTSTS_RXTOINT_Msk
Definition: NUC472_442.h:25977
#define UART_INTSTS_MODEMINT_Msk
Definition: NUC472_442.h:25974
#define UART_INTEN_ATOCTSEN_Msk
Definition: NUC472_442.h:25842
#define UART_INTSTS_RLSINT_Msk
Definition: NUC472_442.h:25971
#define UART_MODEM_RTSACTLV_Msk
Definition: NUC472_442.h:25887
#define CLK_CLKSEL1_UARTSEL_Msk
Definition: NUC472_442.h:3809
#define UART_FIFOSTS_RXEMPTY_Msk
Definition: NUC472_442.h:25923
#define UART_FIFOSTS_PEF_Msk
Definition: NUC472_442.h:25911
#define CLK_CLKDIV0_UARTDIV_Msk
Definition: NUC472_442.h:3866
#define UART_MODEMSTS_CTSDETF_Msk
Definition: NUC472_442.h:25893
#define UART_FIFOSTS_FEF_Msk
Definition: NUC472_442.h:25914
#define CLK_CLKSEL1_UARTSEL_Pos
Definition: NUC472_442.h:3808
#define UART_INTSTS_RXTOIF_Msk
Definition: NUC472_442.h:25956
#define UART_IRDA_TXEN_Msk
Definition: NUC472_442.h:26028
#define UART_MODEMSTS_CTSACTLV_Msk
Definition: NUC472_442.h:25899
#define UART_INTEN_TOCNTEN_Msk
Definition: NUC472_442.h:25836
uint32_t CLK_GetPLLClockFreq(void)
This function get PLL frequency. The frequency unit is Hz.
Definition: clk.c:166
#define CLK
Definition: NUC472_442.h:28798
#define UART_BAUD_MODE2
Definition: uart.h:102
#define UART_PARITY_NONE
Definition: uart.h:57
#define UART_BAUD_MODE0
Definition: uart.h:101
#define UART_FIFO_RTSTRGLV_1BYTE
Definition: uart.h:44
#define UART_STOP_BIT_1
Definition: uart.h:63
#define UART_WORD_LEN_8
Definition: uart.h:55
#define UART_FIFO_RFITL_1BYTE
Definition: uart.h:39
#define UART_FUNCSEL_RS485
Definition: uart.h:86
#define UART_FUNCSEL_UART
Definition: uart.h:83
void UART_SelectRS485Mode(UART_T *uart, uint32_t u32Mode, uint32_t u32Addr)
The function is used to set RS485 relative setting.
Definition: uart.c:310
void UART_EnableInt(UART_T *uart, uint32_t u32InterruptFlag)
The function is used to enable UART specified interrupt and disable NVIC UART IRQ.
Definition: uart.c:146
void UART_SetTimeoutCnt(UART_T *uart, uint32_t u32TOC)
This function use to set Rx timeout count.
Definition: uart.c:264
void UART_Close(UART_T *uart)
The function is used to disable UART.
Definition: uart.c:72
#define UART_BAUD_MODE0_DIVIDER(u32SrcFreq, u32BaudRate)
Calculate UART baudrate mode0 divider.
Definition: uart.h:124
void UART_ClearIntFlag(UART_T *uart, uint32_t u32InterruptFlag)
The function is used to clear UART specified interrupt flag.
Definition: uart.c:43
void UART_DisableFlowCtrl(UART_T *uart)
The function is used to disable UART auto flow control.
Definition: uart.c:85
void UART_EnableFlowCtrl(UART_T *uart)
The function is used to Enable UART auto flow control.
Definition: uart.c:121
uint32_t UART_Write(UART_T *uart, uint8_t *pu8TxBuf, uint32_t u32WriteBytes)
The function is to write data into TX buffer to transmit data by UART.
Definition: uart.c:329
#define UART_BAUD_MODE2_DIVIDER(u32SrcFreq, u32BaudRate)
Calculate UART baudrate mode2 divider.
Definition: uart.h:135
void UART_SetLine_Config(UART_T *uart, uint32_t u32baudrate, uint32_t u32data_width, uint32_t u32parity, uint32_t u32stop_bits)
This function use to config UART line setting.
Definition: uart.c:229
void UART_DisableInt(UART_T *uart, uint32_t u32InterruptFlag)
The function is used to disable UART specified interrupt and disable NVIC UART IRQ.
Definition: uart.c:107
void UART_SelectIrDAMode(UART_T *uart, uint32_t u32Buadrate, uint32_t u32Direction)
The function is used to configure IrDA relative settings. It consists of TX or RX mode and baudrate.
Definition: uart.c:280
uint32_t UART_Read(UART_T *uart, uint8_t *pu8RxBuf, uint32_t u32ReadBytes)
The function is used to read Rx data from RX FIFO and the data will be stored in pu8RxBuf.
Definition: uart.c:199
void UART_Open(UART_T *uart, uint32_t u32baudrate)
This function use to enable UART function and set baud-rate.
Definition: uart.c:160
__IO uint32_t INTEN
Definition: NUC472_442.h:25242
__IO uint32_t BAUD
Definition: NUC472_442.h:25573
__IO uint32_t IRDA
Definition: NUC472_442.h:25593
__IO uint32_t DAT
Definition: NUC472_442.h:25188
__IO uint32_t MODEMSTS
Definition: NUC472_442.h:25363
__IO uint32_t LINE
Definition: NUC472_442.h:25321
__IO uint32_t FIFOSTS
Definition: NUC472_442.h:25425
__IO uint32_t TOUT
Definition: NUC472_442.h:25548
__IO uint32_t FIFO
Definition: NUC472_442.h:25288
__IO uint32_t MODEM
Definition: NUC472_442.h:25344
__IO uint32_t ALTCTL
Definition: NUC472_442.h:25635
__IO uint32_t FUNCSEL
Definition: NUC472_442.h:25651
__IO uint32_t INTSTS
Definition: NUC472_442.h:25529
#define __HIRC
#define __HXT