| NUC472_NUC442_BSP V3.03.003 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
43 void UART_ClearIntFlag(UART_T* uart, uint32_t u32InterruptFlag)
44{
45 if(u32InterruptFlag & UART_INTSTS_RLSINT_Msk) /* clear Receive Line Status Interrupt */
46 {
47 uart->FIFOSTS |= UART_FIFOSTS_BIF_Msk | UART_FIFOSTS_FEF_Msk | UART_FIFOSTS_PEF_Msk;
48 uart->FIFOSTS |= UART_FIFOSTS_ADDRDETF_Msk;
49 }
50
51 if(u32InterruptFlag & UART_INTSTS_MODEMINT_Msk) /* clear Modem Interrupt */
52 uart->MODEMSTS |= UART_MODEMSTS_CTSDETF_Msk;
53
54 if(u32InterruptFlag & UART_INTSTS_BUFERRINT_Msk) /* clear Buffer Error Interrupt */
55 {
56 uart->FIFOSTS |= UART_FIFOSTS_RXOVIF_Msk | UART_FIFOSTS_TXOVIF_Msk;
57 }
58
59 if(u32InterruptFlag & UART_INTSTS_RXTOINT_Msk) /* clear Modem Interrupt */
60 uart->INTSTS |= UART_INTSTS_RXTOIF_Msk;
61
62}
63
64
72 void UART_Close(UART_T* uart)
73{
74 uart->INTEN = 0;
75}
76
77
85 void UART_DisableFlowCtrl(UART_T* uart)
86{
87 uart->INTEN &= ~(UART_INTEN_ATORTSEN_Msk | UART_INTEN_ATOCTSEN_Msk);
88}
89
90
107 void UART_DisableInt(UART_T* uart, uint32_t u32InterruptFlag )
108{
109 uart->INTEN &= ~ u32InterruptFlag;
110}
111
112
113
121 void UART_EnableFlowCtrl(UART_T* uart )
122{
123 uart->MODEM |= UART_MODEM_RTSACTLV_Msk;
124 uart->MODEM &= ~UART_MODEM_RTS_Msk;
125 uart->MODEMSTS |= UART_MODEMSTS_CTSACTLV_Msk;
126 uart->INTEN |= UART_INTEN_ATORTSEN_Msk | UART_INTEN_ATOCTSEN_Msk;
127}
128
129
146 void UART_EnableInt(UART_T* uart, uint32_t u32InterruptFlag )
147{
148 uart->INTEN |= u32InterruptFlag;
149}
150
151
160 void 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
171 uart->FUNCSEL = UART_FUNCSEL_UART;
172 uart->LINE = UART_WORD_LEN_8 | UART_PARITY_NONE | UART_STOP_BIT_1;
173 uart->FIFO = UART_FIFO_RFITL_1BYTE | UART_FIFO_RTSTRGLV_1BYTE;
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
229 void 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
264 void UART_SetTimeoutCnt(UART_T* uart, uint32_t u32TOC)
265{
266 uart->TOUT = (uart->TOUT & ~UART_TOUT_TOIC_Msk)| (u32TOC);
267 uart->INTEN |= UART_INTEN_TOCNTEN_Msk;
268}
269
270
280 void 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
310 void UART_SelectRS485Mode(UART_T* uart, uint32_t u32Mode, uint32_t u32Addr)
311{
312 uart->FUNCSEL = UART_FUNCSEL_RS485;
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_442.h
NUC472/NUC442 peripheral access layer header file. This file contains all the peripheral register's d...
UART_FIFOSTS_BIF_Msk
#define UART_FIFOSTS_BIF_Msk
Definition: NUC472_442.h:25917
UART_FIFOSTS_TXOVIF_Msk
#define UART_FIFOSTS_TXOVIF_Msk
Definition: NUC472_442.h:25938
UART_FIFOSTS_RXOVIF_Msk
#define UART_FIFOSTS_RXOVIF_Msk
Definition: NUC472_442.h:25902
UART_INTEN_ATORTSEN_Msk
#define UART_INTEN_ATORTSEN_Msk
Definition: NUC472_442.h:25839
UART_FUNCSEL_FUNCSEL_Pos
#define UART_FUNCSEL_FUNCSEL_Pos
Definition: NUC472_442.h:26063
UART_INTSTS_BUFERRINT_Msk
#define UART_INTSTS_BUFERRINT_Msk
Definition: NUC472_442.h:25980
UART_FIFOSTS_TXFULL_Msk
#define UART_FIFOSTS_TXFULL_Msk
Definition: NUC472_442.h:25935
UART_ALTCTL_ADDRMV_Pos
#define UART_ALTCTL_ADDRMV_Pos
Definition: NUC472_442.h:26060
UART_FIFOSTS_ADDRDETF_Msk
#define UART_FIFOSTS_ADDRDETF_Msk
Definition: NUC472_442.h:25908
CLK_CLKDIV0_UARTDIV_Pos
#define CLK_CLKDIV0_UARTDIV_Pos
Definition: NUC472_442.h:3865
UART_IRDA_RXINV_Msk
#define UART_IRDA_RXINV_Msk
Definition: NUC472_442.h:26034
UART_INTSTS_RXTOINT_Msk
#define UART_INTSTS_RXTOINT_Msk
Definition: NUC472_442.h:25977
UART_INTSTS_MODEMINT_Msk
#define UART_INTSTS_MODEMINT_Msk
Definition: NUC472_442.h:25974
UART_INTEN_ATOCTSEN_Msk
#define UART_INTEN_ATOCTSEN_Msk
Definition: NUC472_442.h:25842
UART_INTSTS_RLSINT_Msk
#define UART_INTSTS_RLSINT_Msk
Definition: NUC472_442.h:25971
UART_MODEM_RTSACTLV_Msk
#define UART_MODEM_RTSACTLV_Msk
Definition: NUC472_442.h:25887
CLK_CLKSEL1_UARTSEL_Msk
#define CLK_CLKSEL1_UARTSEL_Msk
Definition: NUC472_442.h:3809
UART_FIFOSTS_RXEMPTY_Msk
#define UART_FIFOSTS_RXEMPTY_Msk
Definition: NUC472_442.h:25923
UART_FIFOSTS_PEF_Msk
#define UART_FIFOSTS_PEF_Msk
Definition: NUC472_442.h:25911
CLK_CLKDIV0_UARTDIV_Msk
#define CLK_CLKDIV0_UARTDIV_Msk
Definition: NUC472_442.h:3866
UART_MODEMSTS_CTSDETF_Msk
#define UART_MODEMSTS_CTSDETF_Msk
Definition: NUC472_442.h:25893
UART_FIFOSTS_FEF_Msk
#define UART_FIFOSTS_FEF_Msk
Definition: NUC472_442.h:25914
CLK_CLKSEL1_UARTSEL_Pos
#define CLK_CLKSEL1_UARTSEL_Pos
Definition: NUC472_442.h:3808
UART_INTSTS_RXTOIF_Msk
#define UART_INTSTS_RXTOIF_Msk
Definition: NUC472_442.h:25956
UART_IRDA_TXEN_Msk
#define UART_IRDA_TXEN_Msk
Definition: NUC472_442.h:26028
UART_MODEMSTS_CTSACTLV_Msk
#define UART_MODEMSTS_CTSACTLV_Msk
Definition: NUC472_442.h:25899
UART_INTEN_TOCNTEN_Msk
#define UART_INTEN_TOCNTEN_Msk
Definition: NUC472_442.h:25836
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
UART_BAUD_MODE2
#define UART_BAUD_MODE2
Definition: uart.h:91
UART_PARITY_NONE
#define UART_PARITY_NONE
Definition: uart.h:57
UART_BAUD_MODE0
#define UART_BAUD_MODE0
Definition: uart.h:90
UART_FIFO_RTSTRGLV_1BYTE
#define UART_FIFO_RTSTRGLV_1BYTE
Definition: uart.h:44
UART_STOP_BIT_1
#define UART_STOP_BIT_1
Definition: uart.h:63
UART_WORD_LEN_8
#define UART_WORD_LEN_8
Definition: uart.h:55
UART_FIFO_RFITL_1BYTE
#define UART_FIFO_RFITL_1BYTE
Definition: uart.h:39
UART_FUNCSEL_RS485
#define UART_FUNCSEL_RS485
Definition: uart.h:85
UART_FUNCSEL_UART
#define UART_FUNCSEL_UART
Definition: uart.h:83
UART_SelectRS485Mode
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
UART_EnableInt
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
UART_SetTimeoutCnt
void UART_SetTimeoutCnt(UART_T *uart, uint32_t u32TOC)
This function use to set Rx timeout count.
Definition: uart.c:264
UART_Close
void UART_Close(UART_T *uart)
The function is used to disable UART.
Definition: uart.c:72
UART_BAUD_MODE0_DIVIDER
#define UART_BAUD_MODE0_DIVIDER(u32SrcFreq, u32BaudRate)
Calculate UART baudrate mode0 divider.
Definition: uart.h:113
UART_ClearIntFlag
void UART_ClearIntFlag(UART_T *uart, uint32_t u32InterruptFlag)
The function is used to clear UART specified interrupt flag.
Definition: uart.c:43
UART_DisableFlowCtrl
void UART_DisableFlowCtrl(UART_T *uart)
The function is used to disable UART auto flow control.
Definition: uart.c:85
UART_EnableFlowCtrl
void UART_EnableFlowCtrl(UART_T *uart)
The function is used to Enable UART auto flow control.
Definition: uart.c:121
UART_Write
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
UART_BAUD_MODE2_DIVIDER
#define UART_BAUD_MODE2_DIVIDER(u32SrcFreq, u32BaudRate)
Calculate UART baudrate mode2 divider.
Definition: uart.h:124
UART_SetLine_Config
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
UART_DisableInt
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
UART_SelectIrDAMode
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
UART_Read
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
UART_Open
void UART_Open(UART_T *uart, uint32_t u32baudrate)
This function use to enable UART function and set baud-rate.
Definition: uart.c:160
UART_T::INTEN
__IO uint32_t INTEN
Definition: NUC472_442.h:25242
UART_T::BAUD
__IO uint32_t BAUD
Definition: NUC472_442.h:25573
UART_T::IRDA
__IO uint32_t IRDA
Definition: NUC472_442.h:25593
UART_T::DAT
__IO uint32_t DAT
Definition: NUC472_442.h:25188
UART_T::MODEMSTS
__IO uint32_t MODEMSTS
Definition: NUC472_442.h:25363
UART_T::LINE
__IO uint32_t LINE
Definition: NUC472_442.h:25321
UART_T::FIFOSTS
__IO uint32_t FIFOSTS
Definition: NUC472_442.h:25425
UART_T::TOUT
__IO uint32_t TOUT
Definition: NUC472_442.h:25548
UART_T::FIFO
__IO uint32_t FIFO
Definition: NUC472_442.h:25288
UART_T::MODEM
__IO uint32_t MODEM
Definition: NUC472_442.h:25344
UART_T::ALTCTL
__IO uint32_t ALTCTL
Definition: NUC472_442.h:25635
UART_T::FUNCSEL
__IO uint32_t FUNCSEL
Definition: NUC472_442.h:25651
UART_T::INTSTS
__IO uint32_t INTSTS
Definition: NUC472_442.h:25529
UART_T
Definition: NUC472_442.h:25170
__HIRC
#define __HIRC
Definition: system_NUC472_442.h:27
__HXT
#define __HXT
Definition: system_NUC472_442.h:25