| MINI58_BSP V3.01.003 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
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;
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;
163 uart->FUNSEL = UART_FUNC_SEL_UART;
164 uart->LINE = UART_WORD_LEN_8 | UART_PARITY_NONE | UART_STOP_BIT_1;
165 uart->FIFO = UART_FIFO_RFITL_1BYTE | UART_FIFO_RTSTRGLV_1BYTE;
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
210 if (uart->FIFOSTS & UART_FIFOSTS_RXEMPTY_Msk)
211 return 0;
212
213 pu8RxBuf[u32Count] = uart->DAT; /* Get Data from UART RX */
214 }
215
216 return u32Count;
217
218}
219
220
233 void 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
274 void UART_SetTimeoutCnt(UART_T* uart, uint32_t u32TOC)
275{
276 uart->TOUT = (uart->TOUT & ~UART_TOUT_TOIC_Msk)| (u32TOC);
277 uart->INTEN |= UART_INTEN_TOCNTEN_Msk;
278}
279
280
290 void 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
326 void UART_SelectRS485Mode(UART_T* uart, uint32_t u32Mode, uint32_t u32Addr)
327{
328 uart->FUNSEL = UART_FUNC_SEL_RS485;
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
Mini58Series.h
Mini58 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: Mini58Series.h:11262
UART_FIFOSTS_TXOVIF_Msk
#define UART_FIFOSTS_TXOVIF_Msk
Definition: Mini58Series.h:11283
UART_FIFOSTS_RXOVIF_Msk
#define UART_FIFOSTS_RXOVIF_Msk
Definition: Mini58Series.h:11250
UART_INTEN_ATORTSEN_Msk
#define UART_INTEN_ATORTSEN_Msk
Definition: Mini58Series.h:11193
UART_INTSTS_BUFERRINT_Msk
#define UART_INTSTS_BUFERRINT_Msk
Definition: Mini58Series.h:11322
UART_ALTCTL_ADDRMV_Pos
#define UART_ALTCTL_ADDRMV_Pos
Definition: Mini58Series.h:11366
UART_FIFOSTS_ADDRDETF_Msk
#define UART_FIFOSTS_ADDRDETF_Msk
Definition: Mini58Series.h:11253
UART_IRDA_RXINV_Msk
#define UART_IRDA_RXINV_Msk
Definition: Mini58Series.h:11352
UART_INTSTS_RXTOINT_Msk
#define UART_INTSTS_RXTOINT_Msk
Definition: Mini58Series.h:11319
UART_INTSTS_MODEMINT_Msk
#define UART_INTSTS_MODEMINT_Msk
Definition: Mini58Series.h:11316
UART_FUNSEL_FUN_SEL_Pos
#define UART_FUNSEL_FUN_SEL_Pos
Definition: Mini58Series.h:11369
UART_INTEN_ATOCTSEN_Msk
#define UART_INTEN_ATOCTSEN_Msk
Definition: Mini58Series.h:11196
UART_INTSTS_RLSINT_Msk
#define UART_INTSTS_RLSINT_Msk
Definition: Mini58Series.h:11313
UART_MODEM_RTSACTLV_Msk
#define UART_MODEM_RTSACTLV_Msk
Definition: Mini58Series.h:11235
UART_FIFOSTS_TXEMPTYF_Msk
#define UART_FIFOSTS_TXEMPTYF_Msk
Definition: Mini58Series.h:11286
UART_FIFOSTS_RXEMPTY_Msk
#define UART_FIFOSTS_RXEMPTY_Msk
Definition: Mini58Series.h:11268
UART_FIFOSTS_PEF_Msk
#define UART_FIFOSTS_PEF_Msk
Definition: Mini58Series.h:11256
UART_MODEMSTS_CTSDETF_Msk
#define UART_MODEMSTS_CTSDETF_Msk
Definition: Mini58Series.h:11241
UART_FIFOSTS_FEF_Msk
#define UART_FIFOSTS_FEF_Msk
Definition: Mini58Series.h:11259
UART_INTSTS_RXTOIF_Msk
#define UART_INTSTS_RXTOIF_Msk
Definition: Mini58Series.h:11301
UART_IRDA_TXEN_Msk
#define UART_IRDA_TXEN_Msk
Definition: Mini58Series.h:11346
UART_MODEMSTS_CTSACTLV_Msk
#define UART_MODEMSTS_CTSACTLV_Msk
Definition: Mini58Series.h:11247
UART_INTEN_TOCNTEN_Msk
#define UART_INTEN_TOCNTEN_Msk
Definition: Mini58Series.h:11190
CLK_GetPLLClockFreq
__STATIC_INLINE uint32_t CLK_GetPLLClockFreq(void)
Get PLL clock frequency.
Definition: clk.h:197
CLK_CLKSEL1_UARTSEL_Msk
#define CLK_CLKSEL1_UARTSEL_Msk
Definition: Mini58Series.h:1281
CLK_CLKSEL1_UARTSEL_Pos
#define CLK_CLKSEL1_UARTSEL_Pos
Definition: Mini58Series.h:1280
CLK_CLKDIV_UARTDIV_Pos
#define CLK_CLKDIV_UARTDIV_Pos
Definition: Mini58Series.h:1292
CLK_CLKDIV_UARTDIV_Msk
#define CLK_CLKDIV_UARTDIV_Msk
Definition: Mini58Series.h:1293
CLK
#define CLK
Pointer to CLK register structure.
Definition: Mini58Series.h:11730
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:326
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:274
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:343
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:233
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:290
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:199
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::INTEN
__IO uint32_t INTEN
Definition: Mini58Series.h:10762
UART_T::BAUD
__IO uint32_t BAUD
Definition: Mini58Series.h:11092
UART_T::IRDA
__IO uint32_t IRDA
Definition: Mini58Series.h:11111
UART_T::DAT
__IO uint32_t DAT
Definition: Mini58Series.h:10719
UART_T::FUNSEL
__IO uint32_t FUNSEL
Definition: Mini58Series.h:11156
UART_T::MODEMSTS
__IO uint32_t MODEMSTS
Definition: Mini58Series.h:10887
UART_T::LINE
__IO uint32_t LINE
Definition: Mini58Series.h:10837
UART_T::FIFOSTS
__IO uint32_t FIFOSTS
Definition: Mini58Series.h:10969
UART_T::TOUT
__IO uint32_t TOUT
Definition: Mini58Series.h:11068
UART_T::FIFO
__IO uint32_t FIFO
Definition: Mini58Series.h:10802
UART_T::MODEM
__IO uint32_t MODEM
Definition: Mini58Series.h:10863
UART_T::ALTCTL
__IO uint32_t ALTCTL
Definition: Mini58Series.h:11141
UART_T::INTSTS
__IO uint32_t INTSTS
Definition: Mini58Series.h:11049
UART_T
Definition: Mini58Series.h:10702
__HSI
uint32_t __HSI
Definition: system_Mini58Series.c:21
__XTAL
#define __XTAL
Definition: system_Mini58Series.h:31
SystemCoreClock
uint32_t SystemCoreClock
Definition: system_Mini58Series.c:20