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

📄 global.c

📁 ADC图形图像例子
💻 C
📖 第 1 页 / 共 2 页
字号:
/********************************************************************
 * File: GLOBAL.c
 * 
 * Copyright: Peak Microtech Corporation
 *
 *******************************************************************/


#define GLOBAL_GLOBALS
#include "net_cfg.h"

/****************************************************************/

uchar Ping_Count; // ping number
uchar Ping_IP_TTL = 0;

uchar Gateway_IP_TTL = 0;

uchar Udp_Count;
uchar Msec = 0; //10MS
uchar Sec = 0; 
uchar Min = 0;
uchar i = 0;
uchar bnry; // read point
uchar curr; // write point

uchar RtTime = 20; 
uchar Tcp_Drop_Count = 0; 
uint FrameIndex = 0; 


U8 ComTxdBufempty = 0; 
U8 Tx_Buff_Sel = 0; 
U8 BiSecond; 
U8 Tcp_Timeout; 
U8 TcpConnected = 0;

uchar MacID[6] = MY_MAC_ID; 
uchar HexTable[] =
{
	"0123456789ABCDEF"
}; 
uchar Command_Buf[COMMAND_BUFF_SIZE]; // command buffer
uint Command_Len; 

unsigned char IpStr[16];
unsigned char PortStr[5];
struct Socket_Type TCP1024;

uchar ComRxdBuf[COM_RXD_BUFF_SIZE]; 
uchar ComTxdBuf[COM_TXD_BUFF_SIZE]; 

union Netcard RxdNetBuff;
union Netcard TxdNetBuff;
union Netcard Resend_Buff;

union Ethernet_Address_Type My_MAC;
union Ethernet_Address_Type Gateway_MAC;
union Ethernet_Address_Type Ping_MAC;

union Ip_Address_Type My_Ip_Address;
union Ip_Address_Type Gateway_Ip_Address;
union Ip_Address_Type Mask_Ip_Address;
union Ip_Address_Type Ping_Ip_Address; 

union u RemotePort;
union w CrcTemp;


void Delay_MS(uint ms_number)
{
	uint i;
	uint j;
	for (j = 0; j < ms_number; j++)
		for (i = 0; i < 229; i++)
			;
}


void page(uchar pagenumber)
{
	U8 temp;

	temp = netreadw(REG_NET_CR);
	temp &= 0x3B;
	temp |= (pagenumber << 6);
	netwritew(REG_NET_CR, temp);
}


void SetMacID()
{
	My_MAC.bytes[0] = MacID[0]; 
	My_MAC.bytes[1] = MacID[1];
	My_MAC.bytes[2] = MacID[2];
	My_MAC.bytes[3] = MacID[3];
	My_MAC.bytes[4] = MacID[4];
	My_MAC.bytes[5] = MacID[5];
	page(1);

	netwritew(REG_NET_PAR0, My_MAC.bytes[0]);
	netwritew(REG_NET_PAR1, My_MAC.bytes[1]);
	netwritew(REG_NET_PAR2, My_MAC.bytes[2]);
	netwritew(REG_NET_PAR3, My_MAC.bytes[3]);
	netwritew(REG_NET_PAR4, My_MAC.bytes[4]);
	netwritew(REG_NET_PAR5, My_MAC.bytes[5]);
	page(0);
}


void Send_Packet(union Netcard *TxdNetBuff, uint length)
{
	uchar i;
	uint ii;
	page(0); 
	if (length < 60)
		length = 60;	
	for (i = 0; i < 3; i++)	
	{
		TxdNetBuff->EtherFrame.SourceMacId[i] = My_MAC.words[i];
	} Tx_Buff_Sel = !Tx_Buff_Sel;
	if (Tx_Buff_Sel)
	{		
		netwritew(REG_NET_RSAR1, 0x40); 
	}
	else
	{		
		netwritew(REG_NET_RSAR1, 0x46);
	}
	
	netwritew(REG_NET_RSAR0, 0x00); //read page address low
	netwritew(REG_NET_RBCR1, length >> 8); //read count high
	netwritew(REG_NET_RBCR0, length &0xff); //read count low;
	netwritew(REG_NET_CR, 0x12); //write dma, page0

	for (ii = 4; ii < length + 4; ii++)
	{
		netwritew(REG_NET_DMAPORT, TxdNetBuff->bytes.bytebuf[ii]);
	}

	netwritew(REG_NET_RBCR1, 0x00);
	netwritew(REG_NET_RBCR0, 0x00);
	netwritew(REG_NET_CR, 0x22); // DMA operation complete
	/***************************************/

	for (i = 0; i < 6; i++)	
	{
		for (ii = 0; ii < 1000; ii++)
		{			
			if ((netreadw(REG_NET_CR) &ISR_RXE) == 0)
			{
				break;
			}
		}
		if ((netreadw(REG_NET_TSR) &ISR_PRX) != 0)		
		{
			break;
		};		
		netwritew(REG_NET_CR, 0x3E);
	}
	
	netwritew(REG_NET_ISR, 0xFF);
	if (Tx_Buff_Sel)
	{
		netwritew(REG_NET_TPSR, 0x40); //txd packet start;
	}
	else
	{
		netwritew(REG_NET_TPSR, 0x46); //txd packet start;
	}



	netwritew(REG_NET_TBCR1, length >> 8); //high byte counter	
	netwritew(REG_NET_TBCR0, length &0xFF); //low byte counter
	netwritew(REG_NET_ISR, 0xFF);
	netwritew(REG_NET_CR, 0x3E); //to sendpacket;
}


U8 Rec_NewPacket()
{
	uchar i;
	uint ii;
	U16 rec_count;
	page(0);	
	bnry = netreadw(REG_NET_BNRY); //bnry page have read 
	page(1);	
	curr = netreadw(REG_NET_CURR); //curr writepoint 	
	page(0);
	if ((curr == 0))
		return (0);	
	bnry = bnry + 1;
	if (bnry > 0x7f)
		bnry = 0x4c;	
	if (bnry != curr)
	// have new data
	{		
		page(0);
		
		netwritew(REG_NET_RSAR1, bnry);
		netwritew(REG_NET_RSAR0, 0x00);
		netwritew(REG_NET_RBCR1, 0x00);
		netwritew(REG_NET_RBCR0, 18);
		netwritew(REG_NET_CR, 0x0A);

		for (i = 0; i < 18; i++)		
		{			
			RxdNetBuff.bytes.bytebuf[i] = netreadw(REG_NET_DMAPORT);
		}	
		
		netwritew(REG_NET_RBCR1, 0x00); //
		netwritew(REG_NET_RBCR0, 0x00); //
		netwritew(REG_NET_CR, 0x22); //
		//=======================================
		i = RxdNetBuff.bytes.bytebuf[3];
		RxdNetBuff.bytes.bytebuf[3] = RxdNetBuff.bytes.bytebuf[2];
		RxdNetBuff.bytes.bytebuf[2] = i; 
		
		RxdNetBuff.bytes.bytebuf[3] = RxdNetBuff.bytes.bytebuf[3] - 4;		

		if (((RxdNetBuff.bytes.bytebuf[0] &0x01) == 0) ||
	(RxdNetBuff.bytes.bytebuf[1] > 0x7f) || (RxdNetBuff.bytes.bytebuf[1] < 0x4c)
	|| (RxdNetBuff.bytes.bytebuf[2] > 0x06))
		{			
			page(1);			
			curr = netreadw(REG_NET_CURR); //page1
			page(0); 
			bnry = curr - 1;
			if (bnry < 0x4c)
				bnry = 0x7f;			
			netwritew(REG_NET_BNRY, bnry); //write to bnry			
			netwritew(REG_NET_ISR, 0xff);// 			
			return (0);
		}
		//=============================================

		else
		// data packet is good
		{
			
			if ((RxdNetBuff.EtherFrame.NextProtocal == 0x0008) ||
	(RxdNetBuff.EtherFrame.NextProtocal == 0x0608))
			{				
				netwritew(REG_NET_RSAR1, bnry); //read page address high
				netwritew(REG_NET_RSAR0, 4); //read page address low
				
				//read count high
				netwritew(REG_NET_RBCR1, RxdNetBuff.EtherFrame.length >> 8);
				//read count low;
				netwritew(REG_NET_RBCR0, RxdNetBuff.EtherFrame.length &0xff);
	
				netwritew(REG_NET_CR, 0x0A); //read dma				

				rec_count = RxdNetBuff.EtherFrame.length;
				rec_count = rec_count >> 8;
				for (ii = 4; ii < rec_count + 4; ii++)
				{
					RxdNetBuff.bytes.bytebuf[ii] = netreadw(REG_NET_DMAPORT);
				}
				
				netwritew(REG_NET_RBCR1, 0x00); //
				netwritew(REG_NET_RBCR0, 0x00); //
				netwritew(REG_NET_CR, 0x22); //				
			}
			//=========================================
			bnry = RxdNetBuff.bytes.bytebuf[1] - 1;
			
			if (bnry < 0x4c)
			{
				bnry = 0x7f;
			}			
			netwritew(REG_NET_BNRY, bnry); //write to bnry			
			netwritew(REG_NET_ISR, 0xFF);
			if ((TCP1024.State == TCP_STATE_ESTABLISHED) &&
				(RxdNetBuff.IpFrame.NextProtocal == 6))
				if (RxdNetBuff.IpFrame.SourceIp[0] != TxdNetBuff.IpFrame.DestId[0])
					if (RxdNetBuff.IpFrame.SourceIp[1] != TxdNetBuff.IpFrame.DestId[1])			
						return (0);
			return (1); //have new packet
		}
	}
	return (0);
}


void InitNic()
{
	Delay_MS(10);	
	netwritew(REG_NET_CR, 0x21);
	Delay_MS(10); 
	page(0);	
	netwritew(REG_NET_RBCR0, 0x00);
	netwritew(REG_NET_RBCR1, 0x00);
	netwritew(REG_NET_RCR, 0xE0);
	netwritew(REG_NET_TCR, 0xE2);
	
	netwritew(REG_NET_PSTART, 0x4C);
	netwritew(REG_NET_PSTOP, 0x80);
	netwritew(REG_NET_BNRY, 0x4C);
	netwritew(REG_NET_TPSR, 0x40);
	netwritew(REG_NET_ISR, 0xFF);
	netwritew(REG_NET_IMR, 0x00);
	
	netwritew(REG_NET_DCR, 0xC8);
	page(1);
	netwritew(REG_NET_CURR, 0x4D);
	netwritew(REG_NET_MAR0, 0x00);
	netwritew(REG_NET_MAR1, 0x00);
	netwritew(REG_NET_MAR2, 0x00);
	netwritew(REG_NET_MAR3, 0x00);
	netwritew(REG_NET_MAR4, 0x00);
	netwritew(REG_NET_MAR5, 0x00);
	netwritew(REG_NET_MAR6, 0x00);
	netwritew(REG_NET_MAR7, 0x00);
	netwritew(REG_NET_CR, 0x22);

	SetMacID(); // Set MAC address

	page(0);	
	netwritew(REG_NET_RCR, 0xCC);
	netwritew(REG_NET_TCR, 0xE0);
	netwritew(REG_NET_CR, 0x22);
	netwritew(REG_NET_ISR, 0xFF);

}


U16 CheckSum(U16 *check, U16 length)
{
	volatile U32 sum = 0;
	volatile U16 i;
	
	for (i = 0; i < (length) / 2; i++)
	{
		sum +=  *check++;
	}
	
	if (length &0x01)	
	{
		sum = sum + ((*check) &0x00ff);
	}
	
	while (sum >> 16)
	{
		sum = (sum &0xffff) + (sum >> 16);
	}	
	
	return ((U16)(~sum));
}


unsigned char VerifyIpHeadCrc()
{
	U16 crc;
	U8 length, temp;

	temp = RxdNetBuff.IpFrame.VerandIphLen;
	length = (temp &0x0f) *4;
	crc = CheckSum(&RxdNetBuff.IpPacket.IpPacket[0], length); 
	if (crc == 0)
	{
		return (1);
	}
	return (0);
}


unsigned char VerifyTcpCrc()
{
	volatile U16 crc;
	volatile U16 length, temp;

	RxdNetBuff.IpFrame.ttl = 0;
	temp = RxdNetBuff.IpFrame.TotalLen;
	temp = temp >> 8;
	temp = temp - (RxdNetBuff.IpFrame.VerandIphLen &0x0f) *4;

	RxdNetBuff.IpFrame.Crc = temp << 8;

	length = temp + 12;
	crc = CheckSum(&RxdNetBuff.IpPacket.IpPacket[4], length);

	if (crc == 0)
	{
		return (1);
	}
	return (0);


}


uint CreateIpHeadCrc()
{
	volatile int i;
	volatile U32 temp = 0;
	volatile U32 checksum;

	for (i = 9; i < 19; i++)	
	{
		temp = temp + TxdNetBuff.words.wordbuf[i];
	}
	while (temp >> 16)
	{
		temp = (temp &0xffff) + (temp >> 16);

⌨️ 快捷键说明

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