| MINI51DE_BSP V3.02.004 The Board Support Package for Mini51DE Series MCU

uart.c
Go to the documentation of this file.
1/**************************************************************************/
13#include <stdio.h>
14#include "Mini51Series.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 u32ClkDiv = 0;
159 uint32_t u32Clk = 0;
160 uint32_t u32ClkTbl[4] = {__XTAL, 0, __IRC22M, __IRC22M};
161 uint32_t u32Baud_Div;
162 u8UartClkSrcSel = (CLK->CLKSEL1 & CLK_CLKSEL1_UART_S_Msk) >> CLK_CLKSEL1_UART_S_Pos;
163
164 u32ClkDiv = ( (CLK->CLKDIV & CLK_CLKDIV_UART_N_Msk) >> CLK_CLKDIV_UART_N_Pos );
165
166 uart->FUN_SEL = UART_FUNC_SEL_UART;
167 uart->LCR = UART_WORD_LEN_8 | UART_PARITY_NONE | UART_STOP_BIT_1;
168 uart->FCR = UART_FCR_RFITL_1BYTE | UART_FCR_RTS_TRI_LEV_1BYTE;
169
170 u32Clk = u32ClkTbl[u8UartClkSrcSel]/(u32ClkDiv + 1);
171
172 if(u32baudrate != 0)
173 {
174 u32Baud_Div = UART_BAUD_MODE2_DIVIDER(u32Clk, u32baudrate);
175
176 if(u32Baud_Div > 0xFFFF)
177 uart->BAUD = (UART_BAUD_MODE0 | UART_BAUD_MODE0_DIVIDER(u32Clk, u32baudrate));
178 else
179 uart->BAUD = (UART_BAUD_MODE2 | u32Baud_Div);
180 }
181
182}
183
184
195uint32_t UART_Read(UART_T* uart, uint8_t *pu8RxBuf, uint32_t u32ReadBytes)
196{
197 uint32_t u32Count, u32delayno;
198
199 for(u32Count=0; u32Count < u32ReadBytes; u32Count++)
200 {
201 u32delayno = 0;
202
203 while(uart->FSR & UART_FSR_RX_EMPTY_Msk) /* Check RX empty => failed */
204 {
205 u32delayno++;
206 if( u32delayno >= 0x40000000 )
207 return FALSE;
208 }
209 pu8RxBuf[u32Count] = uart->RBR; /* Get Data from UART RX */
210 }
211
212 return u32Count;
213
214}
215
216
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 u32ClkDiv = 0;
233 uint32_t u32Clk = 0;
234 uint32_t u32ClkTbl[4] = {__XTAL, 0, __IRC22M, __IRC22M};
235 uint32_t u32Baud_Div = 0;
236 u8UartClkSrcSel = (CLK->CLKSEL1 & CLK_CLKSEL1_UART_S_Msk) >> CLK_CLKSEL1_UART_S_Pos;
237
238 u32ClkDiv = ( (CLK->CLKDIV & CLK_CLKDIV_UART_N_Msk) >> CLK_CLKDIV_UART_N_Pos );
239
240 u32Clk = u32ClkTbl[u8UartClkSrcSel]/(u32ClkDiv + 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->LCR = u32data_width | u32parity | u32stop_bits;
253}
254
255
264 void UART_SetTimeoutCnt(UART_T* uart, uint32_t u32TOC)
265{
266 uart->TOR = (uart->TOR & ~UART_TOR_TOIC_Msk)| (u32TOC);
267 uart->IER |= UART_IER_TIME_OUT_EN_Msk;
268}
269
270
280 void UART_SelectIrDAMode(UART_T* uart, uint32_t u32Buadrate, uint32_t u32Direction)
281{
282 uint8_t u8UartClkSrcSel = 0;
283 uint32_t u32ClkDiv = 0;
284 uint32_t u32Clk = 0;
285 uint32_t u32Baud_Div = 0;
286
287 u8UartClkSrcSel = (CLK->CLKSEL1 & CLK_CLKSEL1_UART_S_Msk) >> CLK_CLKSEL1_UART_S_Pos;
288
289 u32ClkDiv = ( (CLK->CLKDIV & CLK_CLKDIV_UART_N_Msk) >> CLK_CLKDIV_UART_N_Pos );
290
291 if(u8UartClkSrcSel == 0)
292 u32Clk = __XTAL;
293 else if(u8UartClkSrcSel >= 2)
294 u32Clk = __IRC22M;
295
296 u32Clk = u32Clk/(u32ClkDiv + 1);
297
298 if(u32Buadrate != 0)
299 {
300 u32Baud_Div = UART_BAUD_MODE2_DIVIDER(u32Clk, u32Buadrate);
301
302 if(u32Baud_Div > 0xFFFF)
303 uart->BAUD = (UART_BAUD_MODE0 | UART_BAUD_MODE0_DIVIDER(u32Clk, u32Buadrate));
304 else
305 uart->BAUD = (UART_BAUD_MODE2 | u32Baud_Div);
306 }
307
308 uart->IRCR &= ~UART_IRCR_INV_TX_Msk;
309 uart->IRCR |= UART_IRCR_INV_RX_Msk;
310 uart->IRCR = u32Direction ? uart->IRCR | UART_IRCR_TX_SELECT_Msk : uart->IRCR &~ UART_IRCR_TX_SELECT_Msk;
311 uart->FUN_SEL = (0x2 << UART_FUN_SEL_FUN_SEL_Pos);
312}
313
314
324 void UART_SelectRS485Mode(UART_T* uart, uint32_t u32Mode, uint32_t u32Addr)
325{
326 uart->FUN_SEL = UART_FUNC_SEL_RS485;
327 uart->ALT_CSR = 0;
328 uart->ALT_CSR |= u32Mode | (u32Addr << UART_ALT_CSR_ADDR_MATCH_Pos);
329}
330
331
341uint32_t UART_Write(UART_T* uart,uint8_t *pu8TxBuf, uint32_t u32WriteBytes)
342{
343 uint32_t u32Count, u32delayno;
344
345 for(u32Count=0; u32Count != u32WriteBytes; u32Count++)
346 {
347 u32delayno = 0;
348 while((uart->FSR & UART_FSR_TE_FLAG_Msk) == 0) /* Wait Tx empty and Time-out manner */
349 {
350 u32delayno++;
351 if( u32delayno >= 0x40000000 )
352 return FALSE;
353 }
354 uart->THR = pu8TxBuf[u32Count]; /* Send UART Data from buffer */
355 }
356
357 return u32Count;
358
359}
360
361 /* end of group MINI51_UART_EXPORTED_FUNCTIONS */
363 /* end of group MINI51_UART_Driver */
365 /* end of group MINI51_Device_Driver */
367
368/*** (C) COPYRIGHT 2012 Nuvoton Technology Corp. ***/
369
370
371
Mini51Series.h
Mini51 series peripheral access layer header file. This file contains all the peripheral register's d...
UART_ISR_MODEM_INT_Msk
#define UART_ISR_MODEM_INT_Msk
Definition: Mini51Series.h:5500
UART_IRCR_TX_SELECT_Msk
#define UART_IRCR_TX_SELECT_Msk
Definition: Mini51Series.h:5527
UART_IRCR_INV_RX_Msk
#define UART_IRCR_INV_RX_Msk
Definition: Mini51Series.h:5533
UART_ALT_CSR_ADDR_MATCH_Pos
#define UART_ALT_CSR_ADDR_MATCH_Pos
Definition: Mini51Series.h:5547
UART_IER_TIME_OUT_EN_Msk
#define UART_IER_TIME_OUT_EN_Msk
Definition: Mini51Series.h:5374
UART_IER_AUTO_RTS_EN_Msk
#define UART_IER_AUTO_RTS_EN_Msk
Definition: Mini51Series.h:5377
UART_ISR_BUF_ERR_INT_Msk
#define UART_ISR_BUF_ERR_INT_Msk
Definition: Mini51Series.h:5506
UART_FSR_RS485_ADD_DETF_Msk
#define UART_FSR_RS485_ADD_DETF_Msk
Definition: Mini51Series.h:5437
UART_ISR_TOUT_INT_Msk
#define UART_ISR_TOUT_INT_Msk
Definition: Mini51Series.h:5503
UART_FSR_FEF_Msk
#define UART_FSR_FEF_Msk
Definition: Mini51Series.h:5443
UART_MSR_LEV_CTS_Msk
#define UART_MSR_LEV_CTS_Msk
Definition: Mini51Series.h:5431
UART_FUN_SEL_FUN_SEL_Pos
#define UART_FUN_SEL_FUN_SEL_Pos
Definition: Mini51Series.h:5550
UART_FSR_BIF_Msk
#define UART_FSR_BIF_Msk
Definition: Mini51Series.h:5446
UART_FSR_TX_OVER_IF_Msk
#define UART_FSR_TX_OVER_IF_Msk
Definition: Mini51Series.h:5467
UART_FSR_PEF_Msk
#define UART_FSR_PEF_Msk
Definition: Mini51Series.h:5440
UART_FSR_RX_EMPTY_Msk
#define UART_FSR_RX_EMPTY_Msk
Definition: Mini51Series.h:5452
UART_ISR_TOUT_IF_Msk
#define UART_ISR_TOUT_IF_Msk
Definition: Mini51Series.h:5485
UART_FSR_RX_OVER_IF_Msk
#define UART_FSR_RX_OVER_IF_Msk
Definition: Mini51Series.h:5434
UART_ISR_RLS_INT_Msk
#define UART_ISR_RLS_INT_Msk
Definition: Mini51Series.h:5497
UART_FSR_TE_FLAG_Msk
#define UART_FSR_TE_FLAG_Msk
Definition: Mini51Series.h:5470
UART_IER_AUTO_CTS_EN_Msk
#define UART_IER_AUTO_CTS_EN_Msk
Definition: Mini51Series.h:5380
UART_MCR_LEV_RTS_Msk
#define UART_MCR_LEV_RTS_Msk
Definition: Mini51Series.h:5419
UART_MSR_DCTSF_Msk
#define UART_MSR_DCTSF_Msk
Definition: Mini51Series.h:5425
CLK_CLKDIV_UART_N_Msk
#define CLK_CLKDIV_UART_N_Msk
Definition: Mini51Series.h:989
CLK_CLKDIV_UART_N_Pos
#define CLK_CLKDIV_UART_N_Pos
Definition: Mini51Series.h:988
CLK_CLKSEL1_UART_S_Pos
#define CLK_CLKSEL1_UART_S_Pos
Definition: Mini51Series.h:982
CLK_CLKSEL1_UART_S_Msk
#define CLK_CLKSEL1_UART_S_Msk
Definition: Mini51Series.h:983
CLK
#define CLK
Pointer to CLK register structure.
Definition: Mini51Series.h:5736
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_WORD_LEN_8
#define UART_WORD_LEN_8
Definition: uart.h:53
UART_FCR_RTS_TRI_LEV_1BYTE
#define UART_FCR_RTS_TRI_LEV_1BYTE
Definition: uart.h:42
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_FCR_RFITL_1BYTE
#define UART_FCR_RFITL_1BYTE
Definition: uart.h:37
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:324
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_BAUD_MODE2
#define UART_BAUD_MODE2
Calculate UART baudrate mode0 divider.
Definition: uart.h:105
UART_BAUD_MODE0
#define UART_BAUD_MODE0
Calculate UART baudrate mode0 divider.
Definition: uart.h:95
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:67
UART_BAUD_MODE0_DIVIDER
#define UART_BAUD_MODE0_DIVIDER(u32SrcFreq, u32BaudRate)
Calculate UART baudrate mode0 divider.
Definition: uart.h:117
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_DisableFlowCtrl
void UART_DisableFlowCtrl(UART_T *uart)
The function is used to disable UART auto flow control.
Definition: uart.c:80
UART_EnableFlowCtrl
void UART_EnableFlowCtrl(UART_T *uart)
The function is used to Enable UART auto flow control.
Definition: uart.c:116
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:341
UART_BAUD_MODE2_DIVIDER
#define UART_BAUD_MODE2_DIVIDER(u32SrcFreq, u32BaudRate)
Calculate UART baudrate mode2 divider.
Definition: uart.h:127
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:102
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:195
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::LCR
__IO uint32_t LCR
Definition: Mini51Series.h:5326
UART_T::FCR
__IO uint32_t FCR
Definition: Mini51Series.h:5325
UART_T::THR
__O uint32_t THR
Definition: Mini51Series.h:5322
UART_T::TOR
__IO uint32_t TOR
Definition: Mini51Series.h:5331
UART_T::FSR
__IO uint32_t FSR
Definition: Mini51Series.h:5329
UART_T::RBR
__I uint32_t RBR
Definition: Mini51Series.h:5321
UART_T::BAUD
__IO uint32_t BAUD
Definition: Mini51Series.h:5332
UART_T::FUN_SEL
__IO uint32_t FUN_SEL
Definition: Mini51Series.h:5335
UART_T::MSR
__IO uint32_t MSR
Definition: Mini51Series.h:5328
FALSE
#define FALSE
Boolean false, define to use in API parameters or return value.
Definition: Mini51Series.h:5896
UART_T::IRCR
__IO uint32_t IRCR
Definition: Mini51Series.h:5333
UART_T::ALT_CSR
__IO uint32_t ALT_CSR
Definition: Mini51Series.h:5334
UART_T::ISR
__IO uint32_t ISR
Definition: Mini51Series.h:5330
UART_T::IER
__IO uint32_t IER
Definition: Mini51Series.h:5324
UART_T::MCR
__IO uint32_t MCR
Definition: Mini51Series.h:5327
UART_T
Definition: Mini51Series.h:4931
__IRC22M
#define __IRC22M
Definition: system_Mini51Series.h:31
__XTAL
#define __XTAL
Definition: system_Mini51Series.h:33