| MINI55_BSP V3.03.000 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
37 void UART_ClearIntFlag(UART_T* uart, uint32_t u32InterruptFlag)
38{
39
40 if(u32InterruptFlag & UART_INTSTS_RLSINT_Msk) /* clear Receive Line Status Interrupt */
41 {
42 uart->FIFOSTS = UART_FIFOSTS_BIF_Msk | UART_FIFOSTS_FEF_Msk | UART_FIFOSTS_PEF_Msk;
43 uart->FIFOSTS = UART_FIFOSTS_ADDRDETF_Msk;
44 }
45
46 if(u32InterruptFlag & UART_INTSTS_MODEMINT_Msk) /* clear Modem Interrupt */
47 uart->MODEMSTS = UART_MODEMSTS_CTSDETF_Msk;
48
49 if(u32InterruptFlag & UART_INTSTS_BUFERRINT_Msk) /* clear Buffer Error Interrupt */
50 {
51 uart->FIFOSTS = UART_FIFOSTS_RXOVIF_Msk | UART_FIFOSTS_TXOVIF_Msk;
52 }
53
54 if(u32InterruptFlag & UART_INTSTS_RXTOINT_Msk) /* clear Modem Interrupt */
55 uart->INTSTS = UART_INTSTS_RXTOIF_Msk;
56
57}
58
59
67 void UART_Close(UART_T* uart)
68{
69 uart->INTEN = 0;
70}
71
72
80 void UART_DisableFlowCtrl(UART_T* uart)
81{
82 uart->INTEN &= ~(UART_INTEN_ATORTSEN_Msk | UART_INTEN_ATOCTSEN_Msk);
83}
84
85
102 void UART_DisableInt(UART_T* uart, uint32_t u32InterruptFlag )
103{
104 uart->INTEN &= ~ u32InterruptFlag;
105}
106
107
108
116 void UART_EnableFlowCtrl(UART_T* uart )
117{
118 uart->MODEM |= UART_MODEM_RTSACTLV_Msk;
119 uart->MODEM &= ~UART_MODEM_RTS_Msk;
120 uart->MODEMSTS |= UART_MODEMSTS_CTSACTLV_Msk;
121 uart->INTEN |= UART_INTEN_ATORTSEN_Msk | UART_INTEN_ATOCTSEN_Msk;
122}
123
124
141 void UART_EnableInt(UART_T* uart, uint32_t u32InterruptFlag )
142{
143 uart->INTEN |= u32InterruptFlag;
144}
145
146
155 void 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 }
172 uart->FUNCSEL = UART_FUNC_SEL_UART;
173 uart->LINE = UART_WORD_LEN_8 | UART_PARITY_NONE | UART_STOP_BIT_1;
174 uart->FIFO = UART_FIFO_RFITL_1BYTE | UART_FIFO_RTSTRGLV_1BYTE;
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
239 void 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
286 void UART_SetTimeoutCnt(UART_T* uart, uint32_t u32TOC)
287{
288 uart->TOUT = (uart->TOUT & ~UART_TOUT_TOIC_Msk)| (u32TOC);
289 uart->INTEN |= UART_INTEN_TOCNTEN_Msk;
290}
291
292
302 void 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
345 void UART_SelectRS485Mode(UART_T* uart, uint32_t u32Mode, uint32_t u32Addr)
346{
347 uart->FUNCSEL = UART_FUNC_SEL_RS485;
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
Mini55Series.h
Mini55 series peripheral access layer header file. This file contains all the peripheral register's d...
UART_FIFOSTS_BIF_Msk
#define UART_FIFOSTS_BIF_Msk
Definition: Mini55Series.h:6717
UART_FIFOSTS_TXOVIF_Msk
#define UART_FIFOSTS_TXOVIF_Msk
Definition: Mini55Series.h:6738
UART_FIFOSTS_RXOVIF_Msk
#define UART_FIFOSTS_RXOVIF_Msk
Definition: Mini55Series.h:6705
UART_INTEN_ATORTSEN_Msk
#define UART_INTEN_ATORTSEN_Msk
Definition: Mini55Series.h:6648
UART_FUNCSEL_FUNCSEL_Pos
#define UART_FUNCSEL_FUNCSEL_Pos
Definition: Mini55Series.h:6821
UART_INTSTS_BUFERRINT_Msk
#define UART_INTSTS_BUFERRINT_Msk
Definition: Mini55Series.h:6777
UART_ALTCTL_ADDRMV_Pos
#define UART_ALTCTL_ADDRMV_Pos
Definition: Mini55Series.h:6818
UART_FIFOSTS_ADDRDETF_Msk
#define UART_FIFOSTS_ADDRDETF_Msk
Definition: Mini55Series.h:6708
UART_IRDA_RXINV_Msk
#define UART_IRDA_RXINV_Msk
Definition: Mini55Series.h:6804
UART_INTSTS_RXTOINT_Msk
#define UART_INTSTS_RXTOINT_Msk
Definition: Mini55Series.h:6774
UART_INTSTS_MODEMINT_Msk
#define UART_INTSTS_MODEMINT_Msk
Definition: Mini55Series.h:6771
UART_INTEN_ATOCTSEN_Msk
#define UART_INTEN_ATOCTSEN_Msk
Definition: Mini55Series.h:6651
UART_INTSTS_RLSINT_Msk
#define UART_INTSTS_RLSINT_Msk
Definition: Mini55Series.h:6768
UART_MODEM_RTSACTLV_Msk
#define UART_MODEM_RTSACTLV_Msk
Definition: Mini55Series.h:6690
UART_FIFOSTS_TXEMPTYF_Msk
#define UART_FIFOSTS_TXEMPTYF_Msk
Definition: Mini55Series.h:6741
UART_FIFOSTS_RXEMPTY_Msk
#define UART_FIFOSTS_RXEMPTY_Msk
Definition: Mini55Series.h:6723
UART_FIFOSTS_PEF_Msk
#define UART_FIFOSTS_PEF_Msk
Definition: Mini55Series.h:6711
UART_MODEMSTS_CTSDETF_Msk
#define UART_MODEMSTS_CTSDETF_Msk
Definition: Mini55Series.h:6696
UART_FIFOSTS_FEF_Msk
#define UART_FIFOSTS_FEF_Msk
Definition: Mini55Series.h:6714
UART_INTSTS_RXTOIF_Msk
#define UART_INTSTS_RXTOIF_Msk
Definition: Mini55Series.h:6756
UART_IRDA_TXEN_Msk
#define UART_IRDA_TXEN_Msk
Definition: Mini55Series.h:6798
UART_MODEMSTS_CTSACTLV_Msk
#define UART_MODEMSTS_CTSACTLV_Msk
Definition: Mini55Series.h:6702
UART_INTEN_TOCNTEN_Msk
#define UART_INTEN_TOCNTEN_Msk
Definition: Mini55Series.h:6645
UART_PARITY_NONE
#define UART_PARITY_NONE
Definition: uart.h:55
UART_FIFO_RTSTRGLV_1BYTE
#define UART_FIFO_RTSTRGLV_1BYTE
Definition: uart.h:42
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_FIFO_RFITL_1BYTE
#define UART_FIFO_RFITL_1BYTE
Definition: uart.h:37
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_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:345
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:286
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:362
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:239
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:302
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:205
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
CLK_CLKSEL1_UART0SEL_Msk
#define CLK_CLKSEL1_UART0SEL_Msk
Definition: Mini55Series.h:1235
CLK_CLKSEL1_UART1SEL_Msk
#define CLK_CLKSEL1_UART1SEL_Msk
Definition: Mini55Series.h:1238
CLK_CLKDIV_UART1DIV_Pos
#define CLK_CLKDIV_UART1DIV_Pos
Definition: Mini55Series.h:1246
CLK_CLKDIV_UART0DIV_Pos
#define CLK_CLKDIV_UART0DIV_Pos
Definition: Mini55Series.h:1243
CLK_CLKDIV_UART1DIV_Msk
#define CLK_CLKDIV_UART1DIV_Msk
Definition: Mini55Series.h:1247
CLK_CLKSEL1_UART1SEL_Pos
#define CLK_CLKSEL1_UART1SEL_Pos
Definition: Mini55Series.h:1237
CLK_CLKDIV_UART0DIV_Msk
#define CLK_CLKDIV_UART0DIV_Msk
Definition: Mini55Series.h:1244
CLK_CLKSEL1_UART0SEL_Pos
#define CLK_CLKSEL1_UART0SEL_Pos
Definition: Mini55Series.h:1234
UART0
#define UART0
Pointer to UART0 register structure.
Definition: Mini55Series.h:7004
CLK
#define CLK
Pointer to CLK register structure.
Definition: Mini55Series.h:7010
UART1
#define UART1
Pointer to UART1 register structure.
Definition: Mini55Series.h:7005
UART_T::INTEN
__IO uint32_t INTEN
Definition: Mini55Series.h:6229
UART_T::BAUD
__IO uint32_t BAUD
Definition: Mini55Series.h:6547
UART_T::IRDA
__IO uint32_t IRDA
Definition: Mini55Series.h:6566
UART_T::DAT
__IO uint32_t DAT
Definition: Mini55Series.h:6186
UART_T::MODEMSTS
__IO uint32_t MODEMSTS
Definition: Mini55Series.h:6352
UART_T::LINE
__IO uint32_t LINE
Definition: Mini55Series.h:6303
UART_T::FIFOSTS
__IO uint32_t FIFOSTS
Definition: Mini55Series.h:6431
UART_T::TOUT
__IO uint32_t TOUT
Definition: Mini55Series.h:6523
UART_T::FIFO
__IO uint32_t FIFO
Definition: Mini55Series.h:6268
FALSE
#define FALSE
Boolean false, define to use in API parameters or return value.
Definition: Mini55Series.h:7171
UART_T::MODEM
__IO uint32_t MODEM
Definition: Mini55Series.h:6327
UART_T::ALTCTL
__IO uint32_t ALTCTL
Definition: Mini55Series.h:6596
UART_T::FUNCSEL
__IO uint32_t FUNCSEL
Definition: Mini55Series.h:6611
UART_T::INTSTS
__IO uint32_t INTSTS
Definition: Mini55Series.h:6505
UART_T
Definition: Mini55Series.h:6171
__HSI
uint32_t __HSI
Definition: system_Mini55Series.c:21
__XTAL
#define __XTAL
Definition: system_Mini55Series.h:36