⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 drvuart.c

📁 cortex-m0 LCD1602程序
💻 C
📖 第 1 页 / 共 4 页
字号:
/*---------------------------------------------------------------------------------------------------------*/
/*                                                                                                         */
/* Copyright(c) 2009 Nuvoton Technology Corp. All rights reserved.                                         */
/*                                                                                                         */
/*---------------------------------------------------------------------------------------------------------*/
#include <stdio.h>
#include "NUC1xx.h"

/*---------------------------------------------------------------------------------------------------------*/
/* Includes of local headers                                                                               */
/*---------------------------------------------------------------------------------------------------------*/
#include "DrvUART.h"
#include "DrvSYS.h"


/*---------------------------------------------------------------------------------------------------------*/
/* Macro, type and constant definitions                                                                    */
/*---------------------------------------------------------------------------------------------------------*/


/*---------------------------------------------------------------------------------------------------------*/
/* Global variables                                                                                        */
/*---------------------------------------------------------------------------------------------------------*/
static PFN_DRVUART_CALLBACK *g_pfnUART0callback = NULL;
static PFN_DRVUART_CALLBACK *g_pfnUART1callback = NULL;
static PFN_DRVUART_CALLBACK *g_pfnUART2callback = NULL;



/*---------------------------------------------------------------------------------------------------------*/
/* Interrupt Handler                                                                                 	   */
/*---------------------------------------------------------------------------------------------------------*/
void UART02_IRQHandler(void)
{
    uint32_t u32uart0IntStatus,u32uart2IntStatus;

    u32uart0IntStatus = inpw(&UART0->ISR) ;
	
	u32uart2IntStatus = inpw(&UART2->ISR) ;
	
    if(g_pfnUART0callback != NULL)
	{
        g_pfnUART0callback(u32uart0IntStatus);
    }
	if(g_pfnUART2callback != NULL)
    {
        g_pfnUART2callback(u32uart2IntStatus);
    }
}


void UART1_IRQHandler(void)
{
    uint32_t u32IntStatus;

	u32IntStatus = inpw(&UART1->ISR) ;

    if(g_pfnUART1callback != NULL)
    {
       g_pfnUART1callback(u32IntStatus);
    }

}


 
/*---------------------------------------------------------------------------------------------------------*/
/* Function:     BaudRateCalculator                                                                        */
/*                                                                                                         */
/* Parameter:        																					   */	
/*	             clk          		-[in] Uart Source Clock                                                */
/*	             baudRate     		-[in] User seting BaudRate                                             */
/*	             UART_BAUD_T *baud  -[in] Get User Settings                                                */
/* Returns:                                                                                                */
/*               None                                                                                      */
/* Description:                                                                                            */
/*               The function is used to set Baud Rate register according user's settings                  */
/*---------------------------------------------------------------------------------------------------------*/

/*-----BaudRate Configure----------------------------------------------------------------------------------*/

/*

Mode	DIV_X_EN	DIV_X_ONE	Divider X	BRD	(Baud rate equation)
-----------------------------------------------------------------------
0	    Disable	0	B	        A	        UART_CLK / [16 * (A+2)]
1	    Enable	0	B	        A	        UART_CLK / [(B+1) * (A+2)] , B must >= 8
2	    Enable	1	Don't care	A	        UART_CLK / (A+2), A must >=3

*/


static void BaudRateCalculator(uint32_t clk, uint32_t baudRate, E_UART_PORT u32Port)
{
  	int32_t tmp;
	int32_t div;
  
  	UART_T * tUART;
	tUART = (UART_T *)((uint32_t)UART0 + u32Port);  

	if(((clk / baudRate)%16)<3)	      /* Source Clock mod 16 <3 => Using Divider X =16 (MODE#0) */ 
	{								  
		tUART->BAUD.DIV_X_EN = 0;
	    tUART->BAUD.DIV_X_ONE   = 0;
		tmp = clk / baudRate/16  -2;
	}
	else							  /* Source Clock mod 16 >3 => Up 5% Error BaudRate */
	{
	    tUART->BAUD.DIV_X_EN = 1;			  /* Try to Set Divider X = 1 (MODE#2)*/
	    tUART->BAUD.DIV_X_ONE   = 1;
		tmp = clk / baudRate  -2;

		if(tmp > 0xFFFF)			  /* If Divider > Range  */
		{
			tUART->BAUD.DIV_X_EN = 1;		  /* Try to Set Divider X up 10 (MODE#1) */
			tUART->BAUD.DIV_X_ONE   = 0;

			for(div = 8; div <16;div++)
			{
				if(((clk / baudRate)%(div+1))<3)
				{
					tUART->BAUD.DIVIDER_X   = div;
					tmp = clk / baudRate / (div+1) -2;
					break;
				}
			}
		}
	}

	tUART->BAUD.BRD = tmp; 

}

/*---------------------------------------------------------------------------------------------------------*/
/* Function:     GetUartClk                                                                       		   */
/*                                                                                                         */
/* Parameter:        																					   */	
/*               None                                                                                      */
/* Returns:                                                                                                */
/*               Current Uart Clock  (Hz)                                                                  */
/* Description:                                                                                            */
/*               The function is used to get Uart clock                                                    */
/*---------------------------------------------------------------------------------------------------------*/
static uint32_t GetUartClk(void)
{
	uint32_t clk =0 , div;
													/* Check UART Clock Source Setting */
	if(SYSCLK->CLKSEL1.UART_S == 0)			
	{
		clk = DrvSYS_GetExtClockFreq();			/* Get External Clock From DrvSYS Setting */
	}
	else if(SYSCLK->CLKSEL1.UART_S == 1)
	{
	 	div = SYSCLK->CLKDIV.UART_N;				/* According PLL Clock and UART_Divider to get clock */
	
		clk = DrvSYS_GetPLLClockFreq()/ (div+1);
	}
	else
		clk = __IRC22M;								/* Clock 22Mhz  */

	return clk;
}

 

/*---------------------------------------------------------------------------------------------------------*/
/* Function:     DrvUART_SetRTS				                                                               */
/*                                                                                                         */
/* Parameter:        																					   */	
/*				 u32Port 		-[in]   UART Channel:  UART_PORT0 / UART_PORT1                             */
/*               uint8_t	   	-[in]   RTS Value 					          	         				   */
/*                           Set 0: Drive RTS pin to logic 1 (If the LEV_RTS set to low level triggered).  */
/*                                  Drive RTS pin to logic 0 (If the LEV_RTS set to high level triggered)  */
/*                           Set 1: Drive RTS pin to logic 0 (If the LEV_RTS set to low level triggered)   */
/*                                   Drive RTS pin to logic 1 (If the LEV_RTS set to high level triggered) */
/*                           Note. Lev_RTS is RTS Trigger Level. 0 is low level and 1 is high level        */
/*                                                                                                         */
/*               u16TriggerLevel - [in]   RTS Trigger Level :DRVUART_FIFO_1BYTES to DRVUART_FIFO_62BYTES   */
/* Returns:                                                                                                */
/*               None                                                                                      */
/*                                                                                                         */
/* Description:                                                                                            */
/*               The function is used to set RTS information	                                           */
/*---------------------------------------------------------------------------------------------------------*/
void DrvUART_SetRTS(E_UART_PORT u32Port,uint8_t u8Value,uint16_t u16TriggerLevel)
{
	
	UART_T * tUART;

	tUART = (UART_T *)((uint32_t)UART0 + u32Port);  

	tUART->MCR.RTS = u8Value;

	tUART->FCR.RTS_TRI_LEV = u16TriggerLevel;
}	



/*---------------------------------------------------------------------------------------------------------*/
/* Function:     DrvUART_Open                                                                        	   */
/*                                                                                                         */
/* Parameter:        																					   */	
/*				 u32Port -[in] UART Channel:  UART_PORT0 / UART_PORT1 /UART_PORT2                  		   */
/*               sParam  -[in] the struct parameter to configure UART                                      */
/*                         include of                                                                      */
/*                             u32BaudRate    - Baud rate 												   */									        
/*                                                                                                         */
/*                             u8cParity - DRVUART_PARITY_NONE / DRVUART_PARITY_EVEN / DRVUART_PARITY_ODD  */
/*                                                                                                         */
/*                             u8cDataBits - DRVUART_DATA_BITS_5 / DRVUART_DATA_BITS_6 		               */
/*									  	 	      DRVUART_DATA_BITS_7 / DRVUART_DATA_BITS_8        		   */
/*                                                                                                         */
/*                             u8cStopBits - DRVUART_STOPBITS_1 / STOPBITS_1_5 / STOPBITS_2   		       */
/*                                                                                                         */
/*                             u8cRxTriggerLevel   - DRVUART_FIFO_1BYTES to DRVUART_FIFO_62BYTES           */
/*                                                                                                         */
/*                             u8TimeOut - Time out value	                                     		   */
/*                                                                                                         */
/*                                                                                                         */
/* Returns:                                                                                                */
/*               E_DRVUART_ERR_PORT_INVALID         												       */
/*               E_DRVUART_ERR_PARITY_INVALID                    										   */
/*               E_DRVUART_ERR_DATA_BITS_INVALID               											   */
/*               E_DRVUART_ERR_STOP_BITS_INVALID                										   */
/*               E_DRVUART_ERR_TRIGGERLEVEL_INVALID      												   */
/*               E_DRVUART_ERR_ALLOC_MEMORY_FAIL                 										   */
/*               E_SUCCESS                                                                          	   */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -