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

📄 serial.c

📁 一个基于BIOS的串口数据和网口数据通信的程序
💻 C
字号:
#include "../inc/"
#include "../inc/config.h"
#include "../inc/board.h"
#include "../inc/utils.h"
#include "../inc/ETHER.h"
#include "../inc/def.h"
#include "../inc/IP.h"
#include "../inc/NET.h"
#include "../inc/NETUTIL.h"
#include "../inc/s3c4510b.h"
#include "../inc/UDP.h"
#include "../inc/utils.h"

static U16 SerialPortSel;

U16 SerialSwitch(U16 port)
{
#ifdef	SERIAL_PORTS_SWITCH
//	U16 old_sel = SerialPortSel;
	
	SerialPortSel = port?1:0;
#else
	SerialPortSel = 0;
#endif
	return SerialPortSel;
}

static unsigned int BaudTable[6] = {4800, 9600, 19200, 38400, 57600, 115200};
static unsigned int BaudDiv[6] = {0x1450, 0xa20, 0x500, 0x280, 0x1a0, 0xd0 };


void SerialChgBaud(U32 baud)
{
//	U32 mclk =  GetMasterClock();	
	int i;
	
	outl(0x03, ULCON0);
	outl(0x09, UCON0);
	outl(0x03, ULCON1);
	outl(0x09, UCON1);

	for(i=0; i<6; i++)
		if(baud==BaudTable[i])
			break;
	if(i>=6)
		i = 1;		
	outl(BaudDiv[i], UBRDIV0);
	outl(BaudDiv[i], UBRDIV1);	
}

void SerialTxEmpty(void)
{
//	if(SerialPortSel)
		while(!(inl(USTAT1) & 0x40)); //wait until tx shifter is empty.
//	else
		while(!(inl(USTAT0) & 0x40)); //wait until tx shifter is empty.

}

void SerialTxChar(char data)
{
	if(SerialPortSel) {
		if(data=='\n') {
			while(!(inl(USTAT1) & 0x40));
	    	Delay(1);	//because the slow response of hyper_terminal 
			outl('\r', UTXBUF1);
		}
		while(!(inl(USTAT1) & 0x40)); //Wait until THR is empty.
//		Delay(1);
		outl(data, UTXBUF1);
    } else {
		if(data=='\n') {
			while(!(inl(USTAT0) & 0x40));
			Delay(1);	//because the slow response of hyper_terminal 
			outl('\r', UTXBUF0);
		}
		while(!(inl(USTAT0) & 0x40));  //Wait until THR is empty.
//		Delay(1);
		outl(data, UTXBUF0);
    }
}

int SerialRxReady(void)
{
	if(SerialPortSel)
		return (inl(USTAT1) & 0x20);	//Receive data ready
	else 
		return (inl(USTAT0) & 0x20);	//Receive data ready
}

char SerialRxKey(void)
{
	if(SerialPortSel) {
		if(inl(USTAT1) & 0x20) //Receive data ready
			return inl(URXBUF1);
	} else {
		if(inl(USTAT0) & 0x20) //Receive data ready
			return inl(URXBUF0);
	}
	return 0;
}

char SerialRxChar(void)
{
	if(SerialPortSel) {
		while(!(inl(USTAT1) & 0x20)); //Receive data ready
		return inl(URXBUF1);
	} else {
		while(!(inl(USTAT0) & 0x20)); //Receive data ready
		return inl(URXBUF0);
	}
}

int SerialRxToBuf(char *b)
{
	if(SerialPortSel) {
		if(inl(USTAT1) & 0x20)    //Receive data ready
			*b = inl(URXBUF1);
		else
		    return 0;
    } else {
		if(inl(USTAT0) & 0x20)    //Receive data ready
		    *b = inl(URXBUF0);
		else
		    return 0;
    }
    
    return 1;
}

void SerialTxString(char *s)
{
	 while(*s)
		SerialTxChar(*s++);
}

⌨️ 快捷键说明

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