MINI58_BSP V3.01.004
The Board Support Package for Mini58 Series MCU
uart.c
Go to the documentation of this file.
1/**************************************************************************/
13#include <stdio.h>
14#include "Mini58Series.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;
158 uint32_t u32Clk = 0;
159 uint32_t u32ClkDiv = 0;
160 uint32_t u32Baud_Div;
161
162 u8UartClkSrcSel = (CLK->CLKSEL1 & CLK_CLKSEL1_UARTSEL_Msk) >> CLK_CLKSEL1_UARTSEL_Pos;
166
167 if(u8UartClkSrcSel == 0)
168 u32Clk = __XTAL;
169 else if(u8UartClkSrcSel == 1)
170 u32Clk = CLK_GetPLLClockFreq();
171 else if(u8UartClkSrcSel >= 2)
172 u32Clk = __HSI;
173
174 u32ClkDiv = ( (CLK->CLKDIV & CLK_CLKDIV_UARTDIV_Msk) >> CLK_CLKDIV_UARTDIV_Pos );
175 u32Clk = u32Clk/(u32ClkDiv + 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, u32delayno;
202
203 for (u32Count = 0; u32Count < u32ReadBytes; u32Count++)
204 {
205 u32delayno = (SystemCoreClock / 10);
206
207 while ((uart->FIFOSTS & UART_FIFOSTS_RXEMPTY_Msk) && /* Check RX empty => failed */
208 (u32delayno-- > 0)) ;
209
211 return 0;
212
213 pu8RxBuf[u32Count] = uart->DAT; /* Get Data from UART RX */
214 }
215
216 return u32Count;
217
218}
219
220
233void UART_SetLine_Config(UART_T* uart, uint32_t u32baudrate, uint32_t u32data_width, uint32_t u32parity, uint32_t u32stop_bits)
234{
235 uint8_t u8UartClkSrcSel;
236 uint32_t u32Clk = 0;
237 uint32_t u32ClkDiv = 0;
238 uint32_t u32Baud_Div = 0;
239
240 u8UartClkSrcSel = (CLK->CLKSEL1 & CLK_CLKSEL1_UARTSEL_Msk) >> CLK_CLKSEL1_UARTSEL_Pos;
241
242 if(u8UartClkSrcSel == 0)
243 u32Clk = __XTAL;
244 else if(u8UartClkSrcSel == 1)
245 u32Clk = CLK_GetPLLClockFreq();
246 else if(u8UartClkSrcSel >= 2)
247 u32Clk = __HSI;
248
249 u32ClkDiv = ( (CLK->CLKDIV & CLK_CLKDIV_UARTDIV_Msk) >> CLK_CLKDIV_UARTDIV_Pos );
250 u32Clk = u32Clk/(u32ClkDiv + 1);
251
252 if(u32baudrate != 0)
253 {
254 u32Baud_Div = UART_BAUD_MODE2_DIVIDER(u32Clk, u32baudrate);
255
256 if(u32Baud_Div > 0xFFFF)
257 uart->BAUD = (UART_BAUD_MODE0 | UART_BAUD_MODE0_DIVIDER(u32Clk, u32baudrate));
258 else
259 uart->BAUD = (UART_BAUD_MODE2 | u32Baud_Div);
260 }
261
262 uart->LINE = u32data_width | u32parity | u32stop_bits;
263}
264
265
274void UART_SetTimeoutCnt(UART_T* uart, uint32_t u32TOC)
275{
276 uart->TOUT = (uart->TOUT & ~UART_TOUT_TOIC_Msk)| (u32TOC);
278}
279
280
290void UART_SelectIrDAMode(UART_T* uart, uint32_t u32Buadrate, uint32_t u32Direction)
291{
292 uint8_t u8UartClkSrcSel;
293 uint32_t u32Clk = 0;
294 uint32_t u32ClkDiv = 0;
295
296 u8UartClkSrcSel = (CLK->CLKSEL1 & CLK_CLKSEL1_UARTSEL_Msk) >> CLK_CLKSEL1_UARTSEL_Pos;
297
298 if(u8UartClkSrcSel == 0)
299 u32Clk = __XTAL;
300 else if(u8UartClkSrcSel == 1)
301 u32Clk = CLK_GetPLLClockFreq();
302 else if(u8UartClkSrcSel >= 2)
303 u32Clk = __HSI;
304
305 u32ClkDiv = ( (CLK->CLKDIV & CLK_CLKDIV_UARTDIV_Msk) >> CLK_CLKDIV_UARTDIV_Pos );
306 u32Clk = u32Clk/(u32ClkDiv + 1);
307
308 uart->BAUD = UART_BAUD_MODE0 | UART_BAUD_MODE0_DIVIDER(u32Clk, u32Buadrate);
309
310 uart->IRDA &= ~UART_IRDA_TXINV_Msk;
311 uart->IRDA |= UART_IRDA_RXINV_Msk;
312 uart->IRDA = u32Direction ? uart->IRDA | UART_IRDA_TXEN_Msk : uart->IRDA &~ UART_IRDA_TXEN_Msk;
313 uart->FUNSEL = (0x2 << UART_FUNSEL_FUN_SEL_Pos);
314}
315
316
326void UART_SelectRS485Mode(UART_T* uart, uint32_t u32Mode, uint32_t u32Addr)
327{
329 uart->ALTCTL = 0;
330 uart->ALTCTL |= u32Mode | (u32Addr << UART_ALTCTL_ADDRMV_Pos);
331}
332
333
343uint32_t UART_Write(UART_T* uart,uint8_t *pu8TxBuf, uint32_t u32WriteBytes)
344{
345 uint32_t u32Count, u32delayno;
346
347 for(u32Count=0; u32Count != u32WriteBytes; u32Count++)
348 {
349 u32delayno = (SystemCoreClock / 10);
350
351 while (((uart->FIFOSTS & UART_FIFOSTS_TXEMPTYF_Msk) == 0) && /* Wait Tx empty and Time-out manner */
352 (u32delayno-- > 0)) ;
353
354 if ((uart->FIFOSTS & UART_FIFOSTS_TXEMPTYF_Msk) == 0)
355 return 0;
356
357 uart->DAT = pu8TxBuf[u32Count]; /* Send UART Data from buffer */
358 }
359 return u32Count;
360}
361
362 /* end of group Mini58_UART_EXPORTED_FUNCTIONS */
364 /* end of group Mini58_UART_Driver */
366 /* end of group Mini58_Device_Driver */
368
369/*** (C) COPYRIGHT 2022 Nuvoton Technology Corp. ***/
370
Mini58 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_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_FUNSEL_FUN_SEL_Pos
#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
__STATIC_INLINE uint32_t CLK_GetPLLClockFreq(void)
Get PLL clock frequency.
Definition: clk.h:197
#define CLK_CLKSEL1_UARTSEL_Msk
#define CLK_CLKSEL1_UARTSEL_Pos
#define CLK_CLKDIV_UARTDIV_Pos
#define CLK_CLKDIV_UARTDIV_Msk
#define CLK
Pointer to CLK register structure.
#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:326
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:274
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:343
#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:233
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:290
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
void UART_Open(UART_T *uart, uint32_t u32baudrate)
This function use to enable UART function and set baud-rate.
Definition: uart.c:155
__IO uint32_t INTEN
__IO uint32_t BAUD
__IO uint32_t IRDA
__IO uint32_t DAT
__IO uint32_t FUNSEL
__IO uint32_t MODEMSTS
__IO uint32_t LINE
__IO uint32_t FIFOSTS
__IO uint32_t TOUT
__IO uint32_t FIFO
__IO uint32_t MODEM
__IO uint32_t ALTCTL
__IO uint32_t INTSTS
uint32_t __HSI
#define __XTAL
uint32_t SystemCoreClock