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

📄 drv_uart.c

📁 基于S3C2440处理器的sd卡驱动程序
💻 C
字号:
/*------------------------------------------------------------------------------
[BASIC]
{
	[FILENAME]  drv_uart.c 
	[CONTENT]   uart driver on S3C2440A
	[AUTHOR]    xucao
	[VERSION]   01.0.00.051121
	[COMPANY]   APLUS COMMUNICATION TECHNOLOGY (BeiJing) CO.,LTD
}

[MOD]
{
	01.0.00.051121:
		1.Create initial version, for printf fuction
}
------------------------------------------------------------------------------*/

#include <assert.h>            /* for assert */
#include <stdarg.h>
#include <stddef.h>				/* for va_list and etc. */
#include <string.h>				/* for strlen and etc. */
#include <stdlib.h>				/* for atoi */

#include "drv_defs.h"          /* drvier SFR define */

int Uart_Printf(const char *fmt, ...);
void Uart_GetString(char *string);

/*------------------------------------------------------------------------------
Function:       uart_init
Date:           2005-11-21
Description:    Initialization of uart
Arguments:      None
Return Value:   None
------------------------------------------------------------------------------*/
void uart_init(void)
{
	/*------------------ uart1 ----------------------*/
	ULCON1   = 0x00000003;         /* 8bit data, 1bit stop, No parity */
	UCON1    = 0x000003C5;         /* use PCLK, level triger, Enable time out and
	                                  error interrupt, no loopback and break, 
	                                  use interrupt no DMA */
	UFCON1   = 0x000000A1;         /* 32-byte for TX, 16-byte for RX, enable FIFO */
	UBRDIV1  = 0x00000023;         /* config div for 115200 from 66.7MHz */

	/* enable UART1's pin */
	{
		unsigned int port_con;      /* for save port configure */
		port_con  = GPHCON;         /* get port H configuration */
		port_con &= 0xFFFFF0FF;     /* clear UART1 pin setting */
		port_con |= 0xA00;          /* enable UART1 pin */
		GPHCON    = port_con;       /* configure it */
	}
}


/*------------------------------------------------------------------------------
Function:       uart1_send_char
Date:           2005-11-21
Description:    send a char by uart1, it will be used by printf
Arguments:      
	ch          pointer to the char need to send
Return Value:   None
------------------------------------------------------------------------------*/
void uart1_send_char( char *ch )
{
	unsigned int fifo_staus = 0;    /* uart fifo status */

	fifo_staus = UFSTAT1;           /* get uart1 fifo status */
	while(fifo_staus & 0x4000)      /* fifo is full */
	{
		fifo_staus = UFSTAT1;       /* get uart1 fifo status */
	}
	
	UTXH1 = *ch;                    /* send the char */
}




/*------------------------------------------------------------------------------
Function:       Uart_Send_Char
Date:           2005-11-21
Description:    wRITE a char TO UART
Arguments:      
	ch          data you want to write
Return Value:   None
------------------------------------------------------------------------------*/

void Uart_Send_Char(char data)
{

	unsigned int fifo_staus = 0;    /* uart fifo status */

	fifo_staus = UFSTAT1;           /* get uart1 fifo status */
	while(fifo_staus & 0x4000)      /* fifo is full */
	{
		fifo_staus = UFSTAT1;       /* get uart1 fifo status */
	}
	
	UTXH1 = data;                    /* send the char */

}


/*------------------------------------------------------------------------------
Function:       Uart_Getch
Date:           2005-11-21
Description:    Read a char from  UART
Arguments:      
	None
Return Value:   data you read
------------------------------------------------------------------------------*/

char Uart_Getch(void)
{
	while(!(UTRSTAT1 & 0x1)); //Receive data ready
	        return URXH1;
}


/*------------------------------------------------------------------------------
Function:       Uart_GetIntNum
Date:           2005-11-21
Description:    Read a int data from  UART
Arguments:      
	String		
Return Value:   your input 
------------------------------------------------------------------------------*/
int Uart_GetIntNum(void)
{
    char str[32];
    int base     = 10;
    int result   = 0;
    memset(str,0, 30);
    Uart_GetString(str);
    if(base==10)
    {
        //sscanf(str, "%d", &result);
        //strcpy(str, "1");
		result = atoi(str);
        //result = minus ? (-1*result):result;
    }
    return result;
}


/*------------------------------------------------------------------------------
Function:       Uart_GetString
Date:           2005-11-21
Description:    Read a string from  UART
Arguments:      
	String		pointer to a buffer  to store your input
Return Value:   
------------------------------------------------------------------------------*/

void Uart_GetString(char *string)
{
    char c;
    while((c = Uart_Getch())!=0xd )
    {
    	if(c>0x20)
        	*string = c;
        Uart_Send_Char(c);
		string++;
    }
	if( c == 0xd )
	{
		c = Uart_Getch();
		//c = Uart_Getch();
	}
    *string='\0';
}


extern int vsprintf(char * buf, const char * fmt, va_list args);

#define UART_LENGTH 1024
void Uart_SendString(char *pt)
{
    while(*pt)
    {
        Uart_Send_Char(*pt);
		pt++;
    }
}

int Uart_Printf(const char *fmt, ...)
{
	va_list args;
	int i = 0;
	char buf[UART_LENGTH];
	memset(buf, 0, UART_LENGTH);
	
	va_start(args, fmt);
	i=vsprintf(buf,fmt,args);
	Uart_SendString(buf);
	va_end(args);
	//printf("%s\n", buf);
	
	return i;
}



⌨️ 快捷键说明

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