📄 global.c
字号:
}
checksum = ~temp;
return (checksum);
}
uint CreateIcmpCrc()
{
volatile int i;
volatile U32 temp = 0;
volatile U32 checksum;
for (i = 19; i < 39; i++)
{
temp = temp + TxdNetBuff.words.wordbuf[i];
}
while (temp >> 16)
{
temp = (temp &0xffff) + (temp >> 16);
}
checksum = ~temp;
return checksum;
}
unsigned char VerifyUdpCrc()
{
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 CreateTcpCrc()
{
uint crc;
crc = CheckSum(&TxdNetBuff.IpPacket.IpPacket[4], TxdNetBuff.IpFrame.Crc + 12);
return (crc);
}
uint CreateUdpCrc()
{
TxdNetBuff.IpFrame.ttl = 0;
TxdNetBuff.IpFrame.Crc = TxdNetBuff.UdpFrame.length;
return CheckSum(&TxdNetBuff.IpPacket.IpPacket[4], converU16(TxdNetBuff.UdpFrame.length)+12);
}
void Copy_To_Resend_Buff()
{
uint ii;
uchar *txd;
uchar *rt;
txd = (U8*) &TxdNetBuff;
if (Resend_Buff.ResendFrame.RtStatus == 0)
{
rt = (U8*) &Resend_Buff.bytes.bytebuf;
for (ii = 0; ii < Resend_Buff.ResendFrame.length + 4; ii++)
{
(*rt) = (*txd);
rt++;
txd++;
}
Resend_Buff.ResendFrame.RtStatus = 1;
Resend_Buff.ResendFrame.timeout = RtTime;
}
}
void Create_Ip_Frame(uint length, uint IPH, uint IPL, uchar NextProtocal)
{
TxdNetBuff.IpFrame.VerandIphLen = 0x45;
TxdNetBuff.IpFrame.ttl = 128; //TTL
TxdNetBuff.IpFrame.ServerType = 0x00;
TxdNetBuff.IpFrame.Crc = 0;
TxdNetBuff.IpFrame.FrameIndex = converU16(FrameIndex++);
TxdNetBuff.IpFrame.Segment = 0x0000; // IP Segment flag
length = length + 20;
TxdNetBuff.IpFrame.TotalLen = length << 8;
TxdNetBuff.IpFrame.NextProtocal = NextProtocal;
TxdNetBuff.IpFrame.DestId[0] = IPH;
TxdNetBuff.IpFrame.DestId[1] = IPL;
TxdNetBuff.IpFrame.SourceIp[0] = My_Ip_Address.words[0];
TxdNetBuff.IpFrame.SourceIp[1] = My_Ip_Address.words[1];
TxdNetBuff.IpFrame.Crc = CreateIpHeadCrc();
length = length + 14; //6+6+2
Send_Packet(&TxdNetBuff, length);
if (NextProtocal == 0x11)
{
UartPrintfCh1("\r\n 1 dup packet has been send!\r\n");
}
TxdNetBuff.EtherFrame.length = length;
if (NextProtocal == 6)
if (TxdNetBuff.TcpFrame.control &(TCP_SYN | TCP_FIN | TCP_PSH | TCP_URG))
{
Copy_To_Resend_Buff();
}
}
union Ip_Address_Type *IP_Str2Hex(unsigned char *Str)
{
unsigned char i, j, ch, x;
union Ip_Address_Type ip;
ch = *Str++;
for (j = 0; j < 3; j++)
{
x = 0;
for (i = 0; i < 4; i++)
{
if (ch == '.')
{
ch = *Str++;
break;
}
else if (i == 3)
return 0;
else if (ch < 0 && ch > 9)
return 0;
else
x = 10 * x + (ch - '0');
ch = *Str++;
}
ip.bytes[j] = x;
}
x = 0;
for (i = 0; i < 4; i++)
{
if (ch == '\0')
{
ch = *Str++;
break;
}
else if (i == 3)
return 0;
else if (ch < 0 && ch > 9)
return 0;
else
x = 10 * x + (ch - '0');
ch = *Str++;
}
ip.bytes[3] = x;
return &(ip.bytes[0]);
}
void IP_Hex2Str(union Ip_Address_Type ip)
{
unsigned char i;
unsigned char x, y;
unsigned char *Str;
Str = IpStr;
for (i = 0; i < 4; i++)
{
x = ip.bytes[i];
if (x > 99)
{
y = x / 100;
*Str++ = y + '0';
x = x - 100 * y;
y = x / 10;
*Str++ = y + '0';
x = x - 10 * y;
*Str++ = x + '0';
if (i == 3)
*Str++ = '\0';
else
*Str++ = '.';
}
else if (x > 9)
{
y = x / 10;
*Str++ = y + '0';
x = x - 10 * y;
*Str++ = x + '0';
if (i == 3)
*Str++ = '\0';
else
*Str++ = '.';
}
else
{
*Str++ = x + '0';
if (i == 3)
*Str++ = '\0';
else
*Str++ = '.';
}
}
}
void Port_Hex2Str(U16 portnum)
{
U16 x;
unsigned char y;
unsigned char *Str;
Str = PortStr;
x = portnum;
if (x > 9999)
{
y = x / 10000;
*Str++ = y + '0';
x = x - 10000 * y;
y = x / 1000;
*Str++ = y + '0';
x = x - 1000 * y;
y = x / 100;
*Str++ = y + '0';
x = x - 100 * y;
y = x / 10;
*Str++ = y + '0';
x = x - 10 * y;
*Str++ = x + '0';
*Str++ = '\0';
}
else if (x > 999)
{
y = x / 1000;
*Str++ = y + '0';
x = x - 1000 * y;
y = x / 100;
*Str++ = y + '0';
x = x - 100 * y;
y = x / 10;
*Str++ = y + '0';
x = x - 10 * y;
*Str++ = x + '0';
*Str++ = '\0';
}
else if (x > 99)
{
y = x / 100;
*Str++ = y + '0';
x = x - 100 * y;
y = x / 10;
*Str++ = y + '0';
x = x - 10 * y;
*Str++ = x + '0';
*Str++ = '\0';
}
else
{
y = x / 10;
*Str++ = y + '0';
x = x - 10 * y;
*Str++ = x + '0';
*Str++ = '\0';
}
}
void initial_system()
{
Command_Len = 0; // initialize Command_Len
Delay_MS(1000); //
Net_Interrupt_Init();
InitNic();
Delay_MS(10);
Ping_Ip_Address.dwords = 0x00000000; //
Delay_MS(10);
for (i = 0; i < 3; i++)
{
TxdNetBuff.EtherFrame.SourceMacId[i] = My_MAC.words[i];
}
}
void NetReset(void)
{
unsigned short val;
// Set PS1=0 PS0=0
val = netreadw(REG_NET_CR);
netwritew(REG_NET_CR, val &0xFF3F);
netwritew(REG_NET_ISR, 0x0FF);
val = netreadw(REG_NET_RSTPORT);
netwritew(REG_NET_RSTPORT, val);
/* check if reset */
while ((netreadw(REG_NET_ISR) &0x80) == 0)
{
Delay(1);
UartPrintfCh1("8019as_reset not complete! \r\n");
}
/* Ack the reset intr */
netwritew(REG_NET_ISR, 0x80);
}
void Delay(int ms)
{
U16 i, ii;
for (i = 0; i < ms; i++)
{
for (ii = 0; ii < 0x4fff; ii++)
{
ii = ii;
}
ms = ms;
}
}
void PrintDataAsAsc(U8 *buf, U16 iBufLen)
{
U16 iLine, i, j;
UartPrintfCh1(
" 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F\r\n");
UartPrintfCh1(
"--------------------------------------------------------------------------------\r\n");
if ((iBufLen % 16) == 0)
{
iLine = iBufLen / 16;
}
else
{
iLine = iBufLen / 16+1;
}
// Print data
for (j = 0; j < iLine; j++)
{
UartPrintfCh1(" ");
for (i = 0; i < 16; i++)
{
if ((j *16+i) < iBufLen)
{
UartPrintfCh1("0x%02X ", *(buf + j * 16+i));
}
else
{
UartPrintfCh1("\r\n");
UartPrintfCh1("\r\n");
UartPrintfCh1("\r\n");
return ;
}
}
UartPrintfCh1("\r\n");
}
UartPrintfCh1("\r\n");
}
void TIMER1_Event()
{
Msec++;
Tcp_Timeout = 1;
if (Msec == 100)
{
Msec = 0;
Sec++;
BiSecond = 1; // 2s set
if (Sec == 60)
{
Sec = 0;
Min++;
if (Min == 60)
Min = 0;
if (Gateway_IP_TTL > 0)
Gateway_IP_TTL = Gateway_IP_TTL - 1;
if (Ping_IP_TTL > 0)
Ping_IP_TTL = Ping_IP_TTL - 1;
}
}
}
U16 converU16(U16 data)
{
U16 high, low;
high = data << 8;
low = data >> 8;
return high + low;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -