| NUC029FAE BSP V3.01.002 The Board Support Package for NUC029FAE

uart.c
Go to the documentation of this file.
1 /**************************************************************************/
13 #include <stdio.h>
14 #include "NUC029FAE.h"
15
37 void UART_ClearIntFlag(UART_T* uart, uint32_t u32InterruptFlag)
38 {
39
40 if(u32InterruptFlag & UART_ISR_RLS_INT_Msk) /* clear Receive Line Status Interrupt */
41 {
42 uart->FSR |= UART_FSR_BIF_Msk | UART_FSR_FEF_Msk | UART_FSR_PEF_Msk;
43 uart->FSR |= UART_FSR_RS485_ADD_DETF_Msk;
44 }
45
46 if(u32InterruptFlag & UART_ISR_MODEM_INT_Msk) /* clear Modem Interrupt */
47 uart->MSR |= UART_MSR_DCTSF_Msk;
48
49 if(u32InterruptFlag & UART_ISR_BUF_ERR_INT_Msk) /* clear Buffer Error Interrupt */
50 {
51 uart->FSR |= UART_FSR_RX_OVER_IF_Msk | UART_FSR_TX_OVER_IF_Msk;
52 }
53
54 if(u32InterruptFlag & UART_ISR_TOUT_INT_Msk) /* clear Modem Interrupt */
55 uart->ISR |= UART_ISR_TOUT_IF_Msk;
56
57 }
58
59
67 void UART_Close(UART_T* uart)
68 {
69 uart->IER = 0;
70 }
71
72
80 void UART_DisableFlowCtrl(UART_T* uart)
81 {
82 uart->IER &= ~(UART_IER_AUTO_RTS_EN_Msk | UART_IER_AUTO_CTS_EN_Msk);
83 }
84
85
102 void UART_DisableInt(UART_T* uart, uint32_t u32InterruptFlag )
103 {
104 uart->IER &= ~ u32InterruptFlag;
105 }
106
107
108
116 void UART_EnableFlowCtrl(UART_T* uart )
117 {
118 uart->MCR |= UART_MCR_LEV_RTS_Msk;
119 uart->MCR &= UART_MCR_RTS_Msk;
120 uart->MSR |= UART_MSR_LEV_CTS_Msk;
121 uart->IER |= UART_IER_AUTO_RTS_EN_Msk | UART_IER_AUTO_CTS_EN_Msk;
122 }
123
124
141 void UART_EnableInt(UART_T* uart, uint32_t u32InterruptFlag )
142 {
143 uart->IER |= u32InterruptFlag;
144 }
145
146
155 void UART_Open(UART_T* uart, uint32_t u32baudrate)
156 {
157 uint8_t u8UartClkSrcSel;
158 uint32_t u32ClkTbl[4] = {__XTAL, 0, __IRC22M, __IRC22M};
159 uint32_t u32Baud_Div;
160 u8UartClkSrcSel = (CLK->CLKSEL1 & CLK_CLKSEL1_UART_S_Msk) >> CLK_CLKSEL1_UART_S_Pos;
161
162 uart->FUN_SEL = UART_FUNC_SEL_UART;
163 uart->LCR = UART_WORD_LEN_8 | UART_PARITY_NONE | UART_STOP_BIT_1;
164 uart->FCR = UART_FCR_RFITL_1BYTE | UART_FCR_RTS_TRI_LEV_1BYTE;
165
166 if(u32baudrate != 0)
167 {
168 u32Baud_Div = UART_BAUD_MODE2_DIVIDER(u32ClkTbl[u8UartClkSrcSel], u32baudrate);
169
170 if(u32Baud_Div > 0xFFFF)
171 uart->BAUD = (UART_BAUD_MODE0 | UART_BAUD_MODE0_DIVIDER(u32ClkTbl[u8UartClkSrcSel], u32baudrate));
172 else
173 uart->BAUD = (UART_BAUD_MODE2 | u32Baud_Div);
174 }
175 }
176
177
188 uint32_t UART_Read(UART_T* uart, uint8_t *pu8RxBuf, uint32_t u32ReadBytes)
189 {
190 uint32_t u32Count, u32delayno;
191
192 for(u32Count=0; u32Count < u32ReadBytes; u32Count++)
193 {
194 u32delayno = 0;
195
196 while(uart->FSR & UART_FSR_RX_EMPTY_Msk) /* Check RX empty => failed */
197 {
198 u32delayno++;
199 if( u32delayno >= 0x40000000 )
200 return FALSE;
201 }
202 pu8RxBuf[u32Count] = uart->RBR; /* Get Data from UART RX */
203 }
204
205 return u32Count;
206
207 }
208
209
222 void UART_SetLine_Config(UART_T* uart, uint32_t u32baudrate, uint32_t u32data_width, uint32_t u32parity, uint32_t u32stop_bits)
223 {
224 uint8_t u8UartClkSrcSel;
225 uint32_t u32ClkTbl[4] = {__XTAL, 0, __IRC22M, __IRC22M};
226 uint32_t u32Baud_Div = 0;
227 u8UartClkSrcSel = (CLK->CLKSEL1 & CLK_CLKSEL1_UART_S_Msk) >> CLK_CLKSEL1_UART_S_Pos;
228
229 if(u32baudrate != 0)
230 {
231 u32Baud_Div = UART_BAUD_MODE2_DIVIDER(u32ClkTbl[u8UartClkSrcSel], u32baudrate);
232
233 if(u32Baud_Div > 0xFFFF)
234 uart->BAUD = (UART_BAUD_MODE0 | UART_BAUD_MODE0_DIVIDER(u32ClkTbl[u8UartClkSrcSel], u32baudrate));
235 else
236 uart->BAUD = (UART_BAUD_MODE2 | u32Baud_Div);
237 }
238
239 uart->LCR = u32data_width | u32parity | u32stop_bits;
240 }
241
242
251 void UART_SetTimeoutCnt(UART_T* uart, uint32_t u32TOC)
252 {
253 uart->TOR = (uart->TOR & ~UART_TOR_TOIC_Msk)| (u32TOC);
254 uart->IER |= UART_IER_TIME_OUT_EN_Msk;
255 }
256
257
267 void UART_SelectIrDAMode(UART_T* uart, uint32_t u32Buadrate, uint32_t u32Direction)
268 {
269 uart->BAUD = UART_BAUD_MODE0 | UART_BAUD_MODE0_DIVIDER(12000000, 57600);
270
271 uart->IRCR &= ~UART_IRCR_INV_TX_Msk;
272 uart->IRCR |= UART_IRCR_INV_RX_Msk;
273 uart->IRCR = u32Direction ? uart->IRCR | UART_IRCR_TX_SELECT_Msk : uart->IRCR &~ UART_IRCR_TX_SELECT_Msk;
274 uart->FUN_SEL = (0x2 << UART_FUN_SEL_FUN_SEL_Pos);
275 }
276
277
287 void UART_SelectRS485Mode(UART_T* uart, uint32_t u32Mode, uint32_t u32Addr)
288 {
289 uart->FUN_SEL = UART_FUNC_SEL_RS485;
290 uart->ALT_CSR = 0;
291 uart->ALT_CSR |= u32Mode | (u32Addr << UART_ALT_CSR_ADDR_MATCH_Pos);
292 }
293
294
304 uint32_t UART_Write(UART_T* uart,uint8_t *pu8TxBuf, uint32_t u32WriteBytes)
305 {
306 uint32_t u32Count, u32delayno;
307
308 for(u32Count=0; u32Count != u32WriteBytes; u32Count++)
309 {
310 u32delayno = 0;
311 while((uart->FSR & UART_FSR_TE_FLAG_Msk) == 0) /* Wait Tx empty and Time-out manner */
312 {
313 u32delayno++;
314 if( u32delayno >= 0x40000000 )
315 return FALSE;
316 }
317 uart->THR = pu8TxBuf[u32Count]; /* Send UART Data from buffer */
318 }
319
320 return u32Count;
321
322 }
323
324 /* end of group NUC029FAE_UART_EXPORTED_FUNCTIONS */
326 /* end of group NUC029FAE_UART_Driver */
328 /* end of group NUC029FAE_Device_Driver */
330
331 /*** (C) COPYRIGHT 2012 Nuvoton Technology Corp. ***/
332
333
334
UART_T::LCR
__IO uint32_t LCR
Definition: NUC029FAE.h:2876
UART_T::TOR
__IO uint32_t TOR
Definition: NUC029FAE.h:2881
UART_FCR_RFITL_1BYTE
#define UART_FCR_RFITL_1BYTE
Definition: uart.h:37
UART_T::FUN_SEL
__IO uint32_t FUN_SEL
Definition: NUC029FAE.h:2885
UART_BAUD_MODE0_DIVIDER
#define UART_BAUD_MODE0_DIVIDER(u32SrcFreq, u32BaudRate)
Calculate UART baudrate mode0 divider.
Definition: uart.h:117
NUC029FAE.h
NUC029FAE peripheral access layer header file. This file contains all the peripheral register's defin...
UART_T::MSR
__IO uint32_t MSR
Definition: NUC029FAE.h:2878
UART_BAUD_MODE2_DIVIDER
#define UART_BAUD_MODE2_DIVIDER(u32SrcFreq, u32BaudRate)
Calculate UART baudrate mode2 divider.
Definition: uart.h:127
UART_DisableFlowCtrl
void UART_DisableFlowCtrl(UART_T *uart)
The function is used to disable UART auto flow control.
Definition: uart.c:80
UART_ClearIntFlag
void UART_ClearIntFlag(UART_T *uart, uint32_t u32InterruptFlag)
The function is used to clear UART specified interrupt flag.
Definition: uart.c:37
UART_T::ALT_CSR
__IO uint32_t ALT_CSR
Definition: NUC029FAE.h:2884
UART_PARITY_NONE
#define UART_PARITY_NONE
Definition: uart.h:55
UART_STOP_BIT_1
#define UART_STOP_BIT_1
Definition: uart.h:61
UART_T::FSR
__IO uint32_t FSR
Definition: NUC029FAE.h:2879
UART_T::IER
__IO uint32_t IER
Definition: NUC029FAE.h:2874
UART_T::RBR
__I uint32_t RBR
Definition: NUC029FAE.h:2871
UART_T::ISR
__IO uint32_t ISR
Definition: NUC029FAE.h:2880
UART_FCR_RTS_TRI_LEV_1BYTE
#define UART_FCR_RTS_TRI_LEV_1BYTE
Definition: uart.h:42
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:287
UART_EnableFlowCtrl
void UART_EnableFlowCtrl(UART_T *uart)
The function is used to Enable UART auto flow control.
Definition: uart.c:116
UART_BAUD_MODE2
#define UART_BAUD_MODE2
Calculate UART baudrate mode0 divider.
Definition: uart.h:105
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:304
__IRC22M
#define __IRC22M
Definition: system_NUC029FAE.h:31
UART_Close
void UART_Close(UART_T *uart)
The function is used to disable UART.
Definition: uart.c:67
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:267
CLK
#define CLK
Pointer to CLK register structure.
Definition: NUC029FAE.h:3228
UART_SetTimeoutCnt
void UART_SetTimeoutCnt(UART_T *uart, uint32_t u32TOC)
This function use to set Rx timeout count.
Definition: uart.c:251
__XTAL
#define __XTAL
Definition: system_NUC029FAE.h:33
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:102
UART_BAUD_MODE0
#define UART_BAUD_MODE0
Calculate UART baudrate mode0 divider.
Definition: uart.h:95
UART_T::FCR
__IO uint32_t FCR
Definition: NUC029FAE.h:2875
UART_FUNC_SEL_UART
#define UART_FUNC_SEL_UART
Definition: uart.h:75
UART_FUNC_SEL_RS485
#define UART_FUNC_SEL_RS485
Definition: uart.h:77
UART_T
Definition: NUC029FAE.h:2867
UART_T::IRCR
__IO uint32_t IRCR
Definition: NUC029FAE.h:2883
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:141
UART_WORD_LEN_8
#define UART_WORD_LEN_8
Definition: uart.h:53
UART_T::MCR
__IO uint32_t MCR
Definition: NUC029FAE.h:2877
UART_T::THR
__O uint32_t THR
Definition: NUC029FAE.h:2872
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:222
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:188
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:155
UART_T::BAUD
__IO uint32_t BAUD
Definition: NUC029FAE.h:2882
FALSE
#define FALSE
Boolean false, define to use in API parameters or return value.
Definition: NUC029FAE.h:3393