MINI55_BSP V3.02.004
The Board Support Package for Mini55 Series MCU
uart.c
Go to the documentation of this file.
1/**************************************************************************/
13#include <stdio.h>
14#include "Mini55Series.h"
15
37void UART_ClearIntFlag(UART_T* uart, uint32_t u32InterruptFlag)
38{
39
40 if(u32InterruptFlag & UART_INTSTS_RLSINT_Msk) /* clear Receive Line Status Interrupt */
41 {
44 }
45
46 if(u32InterruptFlag & UART_INTSTS_MODEMINT_Msk) /* clear Modem Interrupt */
48
49 if(u32InterruptFlag & UART_INTSTS_BUFERRINT_Msk) /* clear Buffer Error Interrupt */
50 {
52 }
53
54 if(u32InterruptFlag & UART_INTSTS_RXTOINT_Msk) /* clear Modem Interrupt */
56
57}
58
59
67void UART_Close(UART_T* uart)
68{
69 uart->INTEN = 0;
70}
71
72
81{
83}
84
85
102void UART_DisableInt(UART_T* uart, uint32_t u32InterruptFlag )
103{
104 uart->INTEN &= ~ u32InterruptFlag;
105}
106
107
108
117{
119 uart->MODEM &= ~UART_MODEM_RTS_Msk;
122}
123
124
141void UART_EnableInt(UART_T* uart, uint32_t u32InterruptFlag )
142{
143 uart->INTEN |= u32InterruptFlag;
144}
145
146
155void UART_Open(UART_T* uart, uint32_t u32baudrate)
156{
157 uint8_t u8UartClkSrcSel = 0;
158 uint32_t u32ClkDiv = 0;
159 uint32_t u32Clk = 0;
160 uint32_t u32Baud_Div;
161
162 if(uart == UART0)
163 {
164 u8UartClkSrcSel = (CLK->CLKSEL1 & CLK_CLKSEL1_UART0SEL_Msk) >> CLK_CLKSEL1_UART0SEL_Pos;
165 u32ClkDiv = ( (CLK->CLKDIV & CLK_CLKDIV_UART0DIV_Msk) >> CLK_CLKDIV_UART0DIV_Pos );
166 }
167 else if(uart == UART1)
168 {
169 u8UartClkSrcSel = (CLK->CLKSEL1 & CLK_CLKSEL1_UART1SEL_Msk) >> CLK_CLKSEL1_UART1SEL_Pos;
170 u32ClkDiv = ( (CLK->CLKDIV & CLK_CLKDIV_UART1DIV_Msk) >> CLK_CLKDIV_UART1DIV_Pos );
171 }
175
176 if(u8UartClkSrcSel == 0)
177 u32Clk = __XTAL;
178 else if(u8UartClkSrcSel >= 2)
179 u32Clk = __HSI;
180
181 u32Clk = u32Clk/(u32ClkDiv + 1);
182
183 if(u32baudrate != 0)
184 {
185 u32Baud_Div = UART_BAUD_MODE2_DIVIDER(u32Clk, u32baudrate);
186
187 if(u32Baud_Div > 0xFFFF)
188 uart->BAUD = (UART_BAUD_MODE0 | UART_BAUD_MODE0_DIVIDER(u32Clk, u32baudrate));
189 else
190 uart->BAUD = (UART_BAUD_MODE2 | u32Baud_Div);
191 }
192}
193
194
205uint32_t UART_Read(UART_T* uart, uint8_t *pu8RxBuf, uint32_t u32ReadBytes)
206{
207 uint32_t u32Count, u32delayno;
208
209 for(u32Count=0; u32Count < u32ReadBytes; u32Count++)
210 {
211 u32delayno = 0;
212
213 while(uart->FIFOSTS & UART_FIFOSTS_RXEMPTY_Msk) /* Check RX empty => failed */
214 {
215 u32delayno++;
216 if( u32delayno >= 0x40000000 )
217 return FALSE;
218 }
219 pu8RxBuf[u32Count] = uart->DAT; /* Get Data from UART RX */
220 }
221
222 return u32Count;
223
224}
225
226
239void UART_SetLine_Config(UART_T* uart, uint32_t u32baudrate, uint32_t u32data_width, uint32_t u32parity, uint32_t u32stop_bits)
240{
241 uint8_t u8UartClkSrcSel = 0;
242 uint32_t u32ClkDiv = 0;
243 uint32_t u32Clk = 0;
244 uint32_t u32Baud_Div = 0;
245
246 if(uart == UART0)
247 {
248 u8UartClkSrcSel = (CLK->CLKSEL1 & CLK_CLKSEL1_UART0SEL_Msk) >> CLK_CLKSEL1_UART0SEL_Pos;
249 u32ClkDiv = ( (CLK->CLKDIV & CLK_CLKDIV_UART0DIV_Msk) >> CLK_CLKDIV_UART0DIV_Pos );
250 }
251 else if(uart == UART1)
252 {
253 u8UartClkSrcSel = (CLK->CLKSEL1 & CLK_CLKSEL1_UART1SEL_Msk) >> CLK_CLKSEL1_UART1SEL_Pos;
254 u32ClkDiv = ( (CLK->CLKDIV & CLK_CLKDIV_UART1DIV_Msk) >> CLK_CLKDIV_UART1DIV_Pos );
255 }
256
257 if(u8UartClkSrcSel == 0)
258 u32Clk = __XTAL;
259 else if(u8UartClkSrcSel >= 2)
260 u32Clk = __HSI;
261
262 u32Clk = u32Clk/(u32ClkDiv + 1);
263
264 if(u32baudrate != 0)
265 {
266 u32Baud_Div = UART_BAUD_MODE2_DIVIDER(u32Clk, u32baudrate);
267
268 if(u32Baud_Div > 0xFFFF)
269 uart->BAUD = (UART_BAUD_MODE0 | UART_BAUD_MODE0_DIVIDER(u32Clk, u32baudrate));
270 else
271 uart->BAUD = (UART_BAUD_MODE2 | u32Baud_Div);
272 }
273
274 uart->LINE = u32data_width | u32parity | u32stop_bits;
275}
276
277
286void UART_SetTimeoutCnt(UART_T* uart, uint32_t u32TOC)
287{
288 uart->TOUT = (uart->TOUT & ~UART_TOUT_TOIC_Msk)| (u32TOC);
290}
291
292
302void UART_SelectIrDAMode(UART_T* uart, uint32_t u32Buadrate, uint32_t u32Direction)
303{
304 uint8_t u8UartClkSrcSel = 0;
305 uint32_t u32Clk = 0;
306 uint32_t u32ClkDiv = 0;
307
308 if(uart == UART0)
309 {
310 u8UartClkSrcSel = (CLK->CLKSEL1 & CLK_CLKSEL1_UART0SEL_Msk) >> CLK_CLKSEL1_UART0SEL_Pos;
311 u32ClkDiv = ( (CLK->CLKDIV & CLK_CLKDIV_UART0DIV_Msk) >> CLK_CLKDIV_UART0DIV_Pos );
312 }
313 else if(uart == UART1)
314 {
315 u8UartClkSrcSel = (CLK->CLKSEL1 & CLK_CLKSEL1_UART1SEL_Msk) >> CLK_CLKSEL1_UART1SEL_Pos;
316 u32ClkDiv = ( (CLK->CLKDIV & CLK_CLKDIV_UART1DIV_Msk) >> CLK_CLKDIV_UART1DIV_Pos );
317 }
318
319 if(u8UartClkSrcSel == 0)
320 u32Clk = __XTAL;
321 else if(u8UartClkSrcSel >= 2)
322 u32Clk = __HSI;
323
324
325 u32Clk = u32Clk/(u32ClkDiv + 1);
326
327 uart->BAUD = UART_BAUD_MODE0 | UART_BAUD_MODE0_DIVIDER(u32Clk, u32Buadrate);
328
329 uart->IRDA &= ~UART_IRDA_TXINV_Msk;
330 uart->IRDA |= UART_IRDA_RXINV_Msk;
331 uart->IRDA = u32Direction ? uart->IRDA | UART_IRDA_TXEN_Msk : uart->IRDA &~ UART_IRDA_TXEN_Msk;
332 uart->FUNCSEL = (0x2 << UART_FUNCSEL_FUNCSEL_Pos);
333}
334
335
345void UART_SelectRS485Mode(UART_T* uart, uint32_t u32Mode, uint32_t u32Addr)
346{
348 uart->ALTCTL = 0;
349 uart->ALTCTL |= u32Mode | (u32Addr << UART_ALTCTL_ADDRMV_Pos);
350}
351
352
362uint32_t UART_Write(UART_T* uart,uint8_t *pu8TxBuf, uint32_t u32WriteBytes)
363{
364 uint32_t u32Count, u32delayno;
365
366 for(u32Count=0; u32Count != u32WriteBytes; u32Count++)
367 {
368 u32delayno = 0;
369 while((uart->FIFOSTS & UART_FIFOSTS_TXEMPTYF_Msk) == 0) /* Wait Tx empty and Time-out manner */
370 {
371 u32delayno++;
372 if( u32delayno >= 0x40000000 )
373 return FALSE;
374 }
375 uart->DAT = pu8TxBuf[u32Count]; /* Send UART Data from buffer */
376 }
377
378 return u32Count;
379
380}
381
382 /* end of group MINI55_UART_EXPORTED_FUNCTIONS */
384 /* end of group MINI55_UART_Driver */
386 /* end of group MINI55_Device_Driver */
388
389/*** (C) COPYRIGHT 2012 Nuvoton Technology Corp. ***/
390
391
392
Mini55 series peripheral access layer header file. This file contains all the peripheral register's d...
#define UART_FIFOSTS_BIF_Msk
#define UART_FIFOSTS_TXOVIF_Msk
#define UART_FIFOSTS_RXOVIF_Msk
#define UART_INTEN_ATORTSEN_Msk
#define UART_FUNCSEL_FUNCSEL_Pos
#define UART_INTSTS_BUFERRINT_Msk
#define UART_ALTCTL_ADDRMV_Pos
#define UART_FIFOSTS_ADDRDETF_Msk
#define UART_IRDA_RXINV_Msk
#define UART_INTSTS_RXTOINT_Msk
#define UART_INTSTS_MODEMINT_Msk
#define UART_INTEN_ATOCTSEN_Msk
#define UART_INTSTS_RLSINT_Msk
#define UART_MODEM_RTSACTLV_Msk
#define UART_FIFOSTS_TXEMPTYF_Msk
#define UART_FIFOSTS_RXEMPTY_Msk
#define UART_FIFOSTS_PEF_Msk
#define UART_MODEMSTS_CTSDETF_Msk
#define UART_FIFOSTS_FEF_Msk
#define UART_INTSTS_RXTOIF_Msk
#define UART_IRDA_TXEN_Msk
#define UART_MODEMSTS_CTSACTLV_Msk
#define UART_INTEN_TOCNTEN_Msk
#define UART_PARITY_NONE
Definition: uart.h:55
#define UART_FIFO_RTSTRGLV_1BYTE
Definition: uart.h:42
#define UART_STOP_BIT_1
Definition: uart.h:61
#define UART_WORD_LEN_8
Definition: uart.h:53
#define UART_FIFO_RFITL_1BYTE
Definition: uart.h:37
#define UART_FUNC_SEL_UART
Definition: uart.h:75
#define UART_FUNC_SEL_RS485
Definition: uart.h:77
void UART_SelectRS485Mode(UART_T *uart, uint32_t u32Mode, uint32_t u32Addr)
The function is used to set RS485 relative setting.
Definition: uart.c:345
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
#define UART_BAUD_MODE2
Calculate UART baudrate mode0 divider.
Definition: uart.h:105
#define UART_BAUD_MODE0
Calculate UART baudrate mode0 divider.
Definition: uart.h:95
void UART_SetTimeoutCnt(UART_T *uart, uint32_t u32TOC)
This function use to set Rx timeout count.
Definition: uart.c:286
void UART_Close(UART_T *uart)
The function is used to disable UART.
Definition: uart.c:67
#define UART_BAUD_MODE0_DIVIDER(u32SrcFreq, u32BaudRate)
Calculate UART baudrate mode0 divider.
Definition: uart.h:117
void UART_ClearIntFlag(UART_T *uart, uint32_t u32InterruptFlag)
The function is used to clear UART specified interrupt flag.
Definition: uart.c:37
void UART_DisableFlowCtrl(UART_T *uart)
The function is used to disable UART auto flow control.
Definition: uart.c:80
void UART_EnableFlowCtrl(UART_T *uart)
The function is used to Enable UART auto flow control.
Definition: uart.c:116
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:362
#define UART_BAUD_MODE2_DIVIDER(u32SrcFreq, u32BaudRate)
Calculate UART baudrate mode2 divider.
Definition: uart.h:127
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:239
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
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:302
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:205
void UART_Open(UART_T *uart, uint32_t u32baudrate)
This function use to enable UART function and set baud-rate.
Definition: uart.c:155
#define CLK_CLKSEL1_UART0SEL_Msk
#define CLK_CLKSEL1_UART1SEL_Msk
#define CLK_CLKDIV_UART1DIV_Pos
#define CLK_CLKDIV_UART0DIV_Pos
#define CLK_CLKDIV_UART1DIV_Msk
#define CLK_CLKSEL1_UART1SEL_Pos
#define CLK_CLKDIV_UART0DIV_Msk
#define CLK_CLKSEL1_UART0SEL_Pos
#define UART0
Pointer to UART0 register structure.
#define CLK
Pointer to CLK register structure.
#define UART1
Pointer to UART1 register structure.
__IO uint32_t INTEN
__IO uint32_t BAUD
__IO uint32_t IRDA
__IO uint32_t DAT
__IO uint32_t MODEMSTS
__IO uint32_t LINE
__IO uint32_t FIFOSTS
__IO uint32_t TOUT
__IO uint32_t FIFO
#define FALSE
Boolean false, define to use in API parameters or return value.
__IO uint32_t MODEM
__IO uint32_t ALTCTL
__IO uint32_t FUNCSEL
__IO uint32_t INTSTS
uint32_t __HSI
#define __XTAL