| Nano102_112 Series BSP V3.03.002 The Board Support Package for Nano102_112 Series

uart.c
Go to the documentation of this file.
1 /**************************************************************************/
13 #include <stdio.h>
14 #include "Nano1X2Series.h"
15
29 extern uint32_t SysGet_PLLClockFreq(void);
30
31
32
41 void UART_ClearIntFlag(UART_T* uart , uint32_t u32InterruptFlag)
42 {
43
44 if(u32InterruptFlag & UART_ISR_RLS_IS_Msk) /* clear Receive Line Status Interrupt */
45 {
46 uart->FSR = UART_FSR_BI_F_Msk | UART_FSR_FE_F_Msk | UART_FSR_PE_F_Msk;
47 uart->TRSR = UART_TRSR_RS485_ADDET_F_Msk;
48 }
49
50 if(u32InterruptFlag & UART_ISR_MODEM_IS_Msk) /* clear Modem Interrupt */
51 uart->MCSR |= UART_MCSR_DCT_F_Msk;
52
53 if(u32InterruptFlag & UART_ISR_BUF_ERR_IS_Msk) /* clear Buffer Error Interrupt */
54 {
55 uart->FSR = UART_FSR_RX_OVER_F_Msk | UART_FSR_TX_OVER_F_Msk;
56 }
57
58 if(u32InterruptFlag & UART_ISR_WAKE_IS_Msk) /* clear wake up Interrupt */
59 {
60 uart->ISR = UART_ISR_WAKE_IS_Msk;
61 }
62
63 if(u32InterruptFlag & UART_ISR_ABAUD_IS_Msk) /* clear auto-baud rate Interrupt */
64 {
65 uart->TRSR = UART_TRSR_ABAUD_TOUT_F_Msk | UART_TRSR_ABAUD_F_Msk;
66 }
67
68 if(u32InterruptFlag & UART_ISR_LIN_IS_Msk) /* clear LIN break Interrupt */
69 {
70 uart->TRSR = UART_TRSR_LIN_TX_F_Msk | UART_TRSR_LIN_RX_F_Msk | UART_TRSR_BIT_ERR_F_Msk;
71 }
72
73 }
74
75
83 void UART_Close(UART_T* uart)
84 {
85 uart->IER = 0;
86 }
87
88
96 void UART_DisableFlowCtrl(UART_T* uart)
97 {
98 uart->CTL &= ~(UART_CTL_AUTO_RTS_EN_Msk | UART_CTL_AUTO_CTS_EN_Msk);
99 }
100
101
119 void UART_DisableInt(UART_T* uart, uint32_t u32InterruptFlag )
120 {
121 uart->IER &= ~ u32InterruptFlag;
122 }
123
124
125
133 void UART_EnableFlowCtrl(UART_T* uart )
134 {
135 uart->MCSR |= UART_MCSR_LEV_RTS_Msk | UART_MCSR_LEV_CTS_Msk;
136 uart->CTL |= UART_CTL_AUTO_RTS_EN_Msk | UART_CTL_AUTO_CTS_EN_Msk;
137 }
138
139
157 void UART_EnableInt(UART_T* uart, uint32_t u32InterruptFlag )
158 {
159 uart->IER |= u32InterruptFlag;
160 }
161
162
171 void UART_Open(UART_T* uart, uint32_t u32baudrate)
172 {
173 uint8_t u8UartClkSrcSel;
174 uint32_t u32ClkTbl[4] = {__HXT, __LXT, 0, __HIRC12M};
175 uint32_t u32Baud_Div;
176 uint32_t u32SrcFreq;
177 uint32_t u32SrcFreqDiv;
178
179 u8UartClkSrcSel = (CLK->CLKSEL1 & CLK_CLKSEL1_UART_S_Msk) >> CLK_CLKSEL1_UART_S_Pos;
180
181 if( (u8UartClkSrcSel == 0x3) && (CLK->PWRCTL & CLK_PWRCTL_HIRC_FSEL_Msk) )
182 u32SrcFreq = __HIRC16M;
183 else
184 u32SrcFreq = u32ClkTbl[u8UartClkSrcSel];
185
186 u32SrcFreqDiv = (((CLK->CLKDIV0 & CLK_CLKDIV0_UART_N_Msk) >> CLK_CLKDIV0_UART_N_Pos) + 1);
187
188 if(u32SrcFreq == 0)
189 {
190 u32SrcFreq = SysGet_PLLClockFreq() / u32SrcFreqDiv;
191 }
192 else
193 {
194 u32SrcFreq = u32SrcFreq / u32SrcFreqDiv;
195 }
196
197 uart->FUN_SEL = UART_FUNC_SEL_UART;
198 uart->TLCTL = UART_WORD_LEN_8 | UART_PARITY_NONE | UART_STOP_BIT_1 |
199 UART_TLCTL_RFITL_1BYTE | UART_TLCTL_RTS_TRI_LEV_1BYTE;
200
201 if(u32baudrate != 0)
202 {
203 u32Baud_Div = UART_BAUD_MODE0_DIVIDER(u32SrcFreq, u32baudrate);
204
205 if(u32Baud_Div > 0xFFFF)
206 uart->BAUD = (UART_BAUD_MODE1 | UART_BAUD_MODE1_DIVIDER(u32SrcFreq, u32baudrate));
207 else
208 uart->BAUD = (UART_BAUD_MODE0 | u32Baud_Div);
209 }
210 }
211
212
223 uint32_t UART_Read(UART_T* uart, uint8_t *pu8RxBuf, uint32_t u32ReadBytes)
224 {
225 uint32_t u32Count, u32delayno;
226
227 for(u32Count=0; u32Count < u32ReadBytes; u32Count++)
228 {
229 u32delayno = 0;
230
231 while(uart->FSR & UART_FSR_RX_EMPTY_F_Msk) /* Check RX empty => failed */
232 {
233 u32delayno++;
234 if( u32delayno >= 0x40000000 )
235 return FALSE;
236 }
237 pu8RxBuf[u32Count] = uart->RBR; /* Get Data from UART RX */
238 }
239
240 return u32Count;
241
242 }
243
244
257 void UART_SetLine_Config(UART_T* uart, uint32_t u32baudrate, uint32_t u32data_width, uint32_t u32parity, uint32_t u32stop_bits)
258 {
259 uint8_t u8UartClkSrcSel;
260 uint32_t u32ClkTbl[4] = {__HXT, __LXT, 0, __HIRC12M};
261 uint32_t u32Baud_Div = 0;
262 uint32_t u32SrcFreq;
263 uint32_t u32SrcFreqDiv;
264
265 u8UartClkSrcSel = (CLK->CLKSEL1 & CLK_CLKSEL1_UART_S_Msk) >> CLK_CLKSEL1_UART_S_Pos;
266
267 if( (u8UartClkSrcSel == 0x3) && (CLK->PWRCTL & CLK_PWRCTL_HIRC_FSEL_Msk) )
268 u32SrcFreq = __HIRC16M;
269 else
270 u32SrcFreq = u32ClkTbl[u8UartClkSrcSel];
271
272 u32SrcFreqDiv = (((CLK->CLKDIV0 & CLK_CLKDIV0_UART_N_Msk) >> CLK_CLKDIV0_UART_N_Pos) + 1);
273
274 if(u32SrcFreq == 0)
275 {
276 u32SrcFreq = SysGet_PLLClockFreq() / u32SrcFreqDiv;
277 }
278 else
279 {
280 u32SrcFreq = u32SrcFreq / u32SrcFreqDiv;
281 }
282
283 if(u32baudrate != 0)
284 {
285 u32Baud_Div = UART_BAUD_MODE0_DIVIDER(u32SrcFreq, u32baudrate);
286
287 if(u32Baud_Div > 0xFFFF)
288 uart->BAUD = (UART_BAUD_MODE1 | UART_BAUD_MODE1_DIVIDER(u32SrcFreq, u32baudrate));
289 else
290 uart->BAUD = (UART_BAUD_MODE0 | u32Baud_Div);
291 }
292
293 uart->TLCTL = u32data_width | u32parity | u32stop_bits;
294 }
295
296
305 void UART_SetTimeoutCnt(UART_T* uart, uint32_t u32TOC)
306 {
307 uart->TMCTL = (uart->TMCTL & ~UART_TMCTL_TOIC_Msk)| (u32TOC);
308 uart->IER |= UART_IER_RTO_IE_Msk;
309 }
310
311
321 void UART_SelectIrDAMode(UART_T* uart, uint32_t u32Buadrate, uint32_t u32Direction)
322 {
323 uint8_t u8UartClkSrcSel;
324 uint32_t u32ClkTbl[4] = {__HXT, __LXT, 0, __HIRC12M};
325 uint32_t u32SrcFreq;
326 uint32_t u32SrcFreqDiv;
327
328 u8UartClkSrcSel = (CLK->CLKSEL1 & CLK_CLKSEL1_UART_S_Msk) >> CLK_CLKSEL1_UART_S_Pos;
329
330 u32SrcFreq = u32ClkTbl[u8UartClkSrcSel];
331
332 u32SrcFreqDiv = (((CLK->CLKDIV0 & CLK_CLKDIV0_UART_N_Msk) >> CLK_CLKDIV0_UART_N_Pos) + 1);
333
334 if(u32SrcFreq == 0)
335 {
336 u32SrcFreq = SysGet_PLLClockFreq() / u32SrcFreqDiv;
337 }
338 else
339 {
340 u32SrcFreq = u32SrcFreq / u32SrcFreqDiv;
341 }
342
343 uart->BAUD = UART_BAUD_MODE1 | UART_BAUD_MODE1_DIVIDER(u32SrcFreq, u32Buadrate);
344
345 uart->IRCR &= ~UART_IRCR_INV_TX_Msk;
346 uart->IRCR |= UART_IRCR_INV_RX_Msk;
347 uart->IRCR = u32Direction ? uart->IRCR | UART_IRCR_TX_SELECT_Msk : uart->IRCR &~ UART_IRCR_TX_SELECT_Msk;
348 uart->FUN_SEL = (0x2 << UART_FUN_SEL_FUN_SEL_Pos);
349 }
350
351
361 void UART_SelectRS485Mode(UART_T* uart, uint32_t u32Mode, uint32_t u32Addr)
362 {
363 uart->FUN_SEL = UART_FUNC_SEL_RS485;
364 uart->ALT_CTL = 0;
365 uart->ALT_CTL |= u32Mode | (u32Addr << UART_ALT_CTL_ADDR_PID_MATCH_Pos);
366 }
367
382 void UART_SelectLINMode(UART_T* uart, uint32_t u32Mode, uint32_t u32BreakLength)
383 {
384 /* Select LIN function mode */
385 uart->FUN_SEL = UART_FUNC_SEL_LIN;
386
387 /* Select LIN function setting : Tx enable, Rx enable and break field length */
388 uart->FUN_SEL = UART_FUNC_SEL_LIN;
389 uart->ALT_CTL &= ~(UART_ALT_CTL_LIN_TX_BCNT_Msk | UART_ALT_CTL_LIN_RX_EN_Msk | UART_ALT_CTL_LIN_TX_EN_Msk);
390 uart->ALT_CTL |= u32BreakLength & UART_ALT_CTL_LIN_TX_BCNT_Msk;
391 uart->ALT_CTL |= u32Mode;
392 }
393
403 uint32_t UART_Write(UART_T* uart,uint8_t *pu8TxBuf, uint32_t u32WriteBytes)
404 {
405 uint32_t u32Count, u32delayno;
406
407 for(u32Count=0; u32Count != u32WriteBytes; u32Count++)
408 {
409 u32delayno = 0;
410 while((uart->FSR & UART_FSR_TX_EMPTY_F_Msk) == 0) /* Wait Tx empty and Time-out manner */
411 {
412 u32delayno++;
413 if( u32delayno >= 0x40000000 )
414 return FALSE;
415 }
416 uart->THR = pu8TxBuf[u32Count]; /* Send UART Data from buffer */
417 }
418
419 return u32Count;
420
421 }
422
423 /* end of group NANO1X2_UART_EXPORTED_FUNCTIONS */
425 /* end of group NANO1X2_UART_Driver */
427 /* end of group NANO1X2_Device_Driver */
429
430 /*** (C) COPYRIGHT 2014 Nuvoton Technology Corp. ***/
431
432
433
UART_CTL_AUTO_RTS_EN_Msk
#define UART_CTL_AUTO_RTS_EN_Msk
Definition: Nano1X2Series.h:10056
UART_TMCTL_TOIC_Msk
#define UART_TMCTL_TOIC_Msk
Definition: Nano1X2Series.h:10236
UART_SelectLINMode
void UART_SelectLINMode(UART_T *uart, uint32_t u32Mode, uint32_t u32BreakLength)
Select and configure LIN function.
Definition: uart.c:382
UART_T::ALT_CTL
__IO uint32_t ALT_CTL
Definition: Nano1X2Series.h:10000
CLK_CLKSEL1_UART_S_Msk
#define CLK_CLKSEL1_UART_S_Msk
Definition: Nano1X2Series.h:1574
UART_MCSR_LEV_RTS_Msk
#define UART_MCSR_LEV_RTS_Msk
Definition: Nano1X2Series.h:10221
UART_T::FUN_SEL
__IO uint32_t FUN_SEL
Definition: Nano1X2Series.h:10015
UART_EnableFlowCtrl
void UART_EnableFlowCtrl(UART_T *uart)
The function is used to Enable UART auto flow control.
Definition: uart.c:133
UART_CTL_AUTO_CTS_EN_Msk
#define UART_CTL_AUTO_CTS_EN_Msk
Definition: Nano1X2Series.h:10059
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:361
CLK
#define CLK
Pointer to CLK register structure.
Definition: Nano1X2Series.h:10651
UART_FUN_SEL_FUN_SEL_Pos
#define UART_FUN_SEL_FUN_SEL_Pos
Definition: Nano1X2Series.h:10286
UART_T::FSR
__IO uint32_t FSR
Definition: Nano1X2Series.h:9855
UART_DisableInt
void UART_DisableInt(UART_T *uart, uint32_t u32InterruptFlag)
The function is used to disable UART specified interrupt.
Definition: uart.c:119
UART_T::TRSR
__IO uint32_t TRSR
Definition: Nano1X2Series.h:9784
UART_T::IER
__IO uint32_t IER
Definition: Nano1X2Series.h:9666
UART_ISR_BUF_ERR_IS_Msk
#define UART_ISR_BUF_ERR_IS_Msk
Definition: Nano1X2Series.h:10152
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:257
UART_T::RBR
__I uint32_t RBR
Definition: Nano1X2Series.h:9496
UART_T::ISR
__IO uint32_t ISR
Definition: Nano1X2Series.h:9732
UART_MCSR_LEV_CTS_Msk
#define UART_MCSR_LEV_CTS_Msk
Definition: Nano1X2Series.h:10227
UART_ISR_WAKE_IS_Msk
#define UART_ISR_WAKE_IS_Msk
Definition: Nano1X2Series.h:10155
UART_T::TMCTL
__IO uint32_t TMCTL
Definition: Nano1X2Series.h:9909
SysGet_PLLClockFreq
uint32_t SysGet_PLLClockFreq(void)
Calculate current PLL clock frequency.
Definition: system_Nano1X2Series.c:28
UART_TRSR_ABAUD_F_Msk
#define UART_TRSR_ABAUD_F_Msk
Definition: Nano1X2Series.h:10167
__LXT
#define __LXT
Definition: system_Nano1X2Series.h:27
CLK_PWRCTL_HIRC_FSEL_Msk
#define CLK_PWRCTL_HIRC_FSEL_Msk
Definition: Nano1X2Series.h:1469
UART_T::CTL
__IO uint32_t CTL
Definition: Nano1X2Series.h:9581
UART_FSR_BI_F_Msk
#define UART_FSR_BI_F_Msk
Definition: Nano1X2Series.h:10200
UART_FUNC_SEL_LIN
#define UART_FUNC_SEL_LIN
Definition: uart.h:74
UART_TLCTL_RTS_TRI_LEV_1BYTE
#define UART_TLCTL_RTS_TRI_LEV_1BYTE
Definition: uart.h:58
UART_BAUD_MODE1_DIVIDER
#define UART_BAUD_MODE1_DIVIDER(u32SrcFreq, u32BaudRate)
Calculate UART baudrate mode0 divider.
Definition: uart.h:116
UART_DisableFlowCtrl
void UART_DisableFlowCtrl(UART_T *uart)
The function is used to disable UART auto flow control.
Definition: uart.c:96
UART_EnableInt
void UART_EnableInt(UART_T *uart, uint32_t u32InterruptFlag)
The function is used to enable UART specified interrupt.
Definition: uart.c:157
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:403
__HIRC16M
#define __HIRC16M
Definition: system_Nano1X2Series.h:29
__HIRC12M
#define __HIRC12M
Definition: system_Nano1X2Series.h:28
UART_FUNC_SEL_RS485
#define UART_FUNC_SEL_RS485
Definition: uart.h:76
UART_PARITY_NONE
#define UART_PARITY_NONE
Definition: uart.h:43
Nano1X2Series.h
Nano102/112 peripheral access layer header file. This file contains all the peripheral register's def...
UART_ISR_MODEM_IS_Msk
#define UART_ISR_MODEM_IS_Msk
Definition: Nano1X2Series.h:10146
UART_FSR_RX_OVER_F_Msk
#define UART_FSR_RX_OVER_F_Msk
Definition: Nano1X2Series.h:10185
UART_ISR_ABAUD_IS_Msk
#define UART_ISR_ABAUD_IS_Msk
Definition: Nano1X2Series.h:10158
UART_T::MCSR
__IO uint32_t MCSR
Definition: Nano1X2Series.h:9888
UART_IRCR_INV_RX_Msk
#define UART_IRCR_INV_RX_Msk
Definition: Nano1X2Series.h:10254
UART_STOP_BIT_1
#define UART_STOP_BIT_1
Definition: uart.h:49
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:321
UART_BAUD_MODE0_DIVIDER
#define UART_BAUD_MODE0_DIVIDER(u32SrcFreq, u32BaudRate)
Calculate UART baudrate mode2 divider.
Definition: uart.h:126
UART_Close
void UART_Close(UART_T *uart)
The function is used to disable UART.
Definition: uart.c:83
UART_TRSR_LIN_RX_F_Msk
#define UART_TRSR_LIN_RX_F_Msk
Definition: Nano1X2Series.h:10176
UART_T::TLCTL
__IO uint32_t TLCTL
Definition: Nano1X2Series.h:9629
FALSE
#define FALSE
Boolean false, define to use in API parameters or return value.
Definition: Nano1X2Series.h:10816
UART_FSR_FE_F_Msk
#define UART_FSR_FE_F_Msk
Definition: Nano1X2Series.h:10197
UART_IER_RTO_IE_Msk
#define UART_IER_RTO_IE_Msk
Definition: Nano1X2Series.h:10122
UART_TRSR_BIT_ERR_F_Msk
#define UART_TRSR_BIT_ERR_F_Msk
Definition: Nano1X2Series.h:10179
UART_ALT_CTL_LIN_TX_BCNT_Msk
#define UART_ALT_CTL_LIN_TX_BCNT_Msk
Definition: Nano1X2Series.h:10257
UART_FUNC_SEL_UART
#define UART_FUNC_SEL_UART
Definition: uart.h:73
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:171
UART_MCSR_DCT_F_Msk
#define UART_MCSR_DCT_F_Msk
Definition: Nano1X2Series.h:10233
UART_ISR_RLS_IS_Msk
#define UART_ISR_RLS_IS_Msk
Definition: Nano1X2Series.h:10143
UART_FSR_PE_F_Msk
#define UART_FSR_PE_F_Msk
Definition: Nano1X2Series.h:10194
UART_FSR_RX_EMPTY_F_Msk
#define UART_FSR_RX_EMPTY_F_Msk
Definition: Nano1X2Series.h:10188
CLK_CLKDIV0_UART_N_Msk
#define CLK_CLKDIV0_UART_N_Msk
Definition: Nano1X2Series.h:1619
UART_BAUD_MODE1
#define UART_BAUD_MODE1
Calculate UART baudrate mode0 divider.
Definition: uart.h:104
CLK_CLKDIV0_UART_N_Pos
#define CLK_CLKDIV0_UART_N_Pos
Definition: Nano1X2Series.h:1618
UART_TRSR_RS485_ADDET_F_Msk
#define UART_TRSR_RS485_ADDET_F_Msk
Definition: Nano1X2Series.h:10164
UART_SetTimeoutCnt
void UART_SetTimeoutCnt(UART_T *uart, uint32_t u32TOC)
This function use to set Rx timeout count.
Definition: uart.c:305
UART_IRCR_TX_SELECT_Msk
#define UART_IRCR_TX_SELECT_Msk
Definition: Nano1X2Series.h:10248
UART_BAUD_MODE0
#define UART_BAUD_MODE0
Calculate UART baudrate mode0 divider.
Definition: uart.h:94
UART_ALT_CTL_ADDR_PID_MATCH_Pos
#define UART_ALT_CTL_ADDR_PID_MATCH_Pos
Definition: Nano1X2Series.h:10283
UART_FSR_TX_OVER_F_Msk
#define UART_FSR_TX_OVER_F_Msk
Definition: Nano1X2Series.h:10203
UART_T
Definition: Nano1X2Series.h:9479
UART_T::IRCR
__IO uint32_t IRCR
Definition: Nano1X2Series.h:9947
__HXT
#define __HXT
Definition: system_Nano1X2Series.h:26
UART_ISR_LIN_IS_Msk
#define UART_ISR_LIN_IS_Msk
Definition: Nano1X2Series.h:10161
UART_FSR_TX_EMPTY_F_Msk
#define UART_FSR_TX_EMPTY_F_Msk
Definition: Nano1X2Series.h:10206
UART_TLCTL_RFITL_1BYTE
#define UART_TLCTL_RFITL_1BYTE
Definition: uart.h:53
UART_T::THR
__O uint32_t THR
Definition: Nano1X2Series.h:9509
UART_WORD_LEN_8
#define UART_WORD_LEN_8
Definition: uart.h:41
UART_ClearIntFlag
void UART_ClearIntFlag(UART_T *uart, uint32_t u32InterruptFlag)
The function is used to clear UART specified interrupt flag.
Definition: uart.c:41
CLK_CLKSEL1_UART_S_Pos
#define CLK_CLKSEL1_UART_S_Pos
Definition: Nano1X2Series.h:1573
UART_TRSR_ABAUD_TOUT_F_Msk
#define UART_TRSR_ABAUD_TOUT_F_Msk
Definition: Nano1X2Series.h:10170
UART_IRCR_INV_TX_Msk
#define UART_IRCR_INV_TX_Msk
Definition: Nano1X2Series.h:10251
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:223
UART_TRSR_LIN_TX_F_Msk
#define UART_TRSR_LIN_TX_F_Msk
Definition: Nano1X2Series.h:10173
UART_ALT_CTL_LIN_TX_EN_Msk
#define UART_ALT_CTL_LIN_TX_EN_Msk
Definition: Nano1X2Series.h:10266
UART_ALT_CTL_LIN_RX_EN_Msk
#define UART_ALT_CTL_LIN_RX_EN_Msk
Definition: Nano1X2Series.h:10263
UART_T::BAUD
__IO uint32_t BAUD
Definition: Nano1X2Series.h:9925