📄 ethernet_mini_driver.c
字号:
{
EDATA = *bufferptr;
bufferptr++;
tx_cnt--;
};
//send the contents of the transmit buffer onto the network
//temp = EIR);
if(1 == (EIR & EIR_TXERIF))
{
TXERIF = 0;
TXRTS = 1;
TXRTS = 0;
}
TXIF = 0;
TXRTS = 1;
//do{
// temp = ECON1);
// }while(1 == temp & ECON1_TXRTS));
while(1 == (ECON1 & ECON1_TXRTS));
//++tx_end;
////banksel(ERDPTL);
//ERDPTL= LOW_BYTE(tx_end);
//ERDPTH= HIGH_BYTE(tx_end);
//for(i=0;i<7;++i)
//tx_status[i]= rd_sram();
//temp = ESTAT);
//if(1 == ( ESTAT) & ESTAT_TXABRT))
//printf("\r\nTransmission Aborted..");
//else
//printf("\r\nTCP Message Sent..");
}
//******************************************************************
//* init_udp Function
//* This function sends UDP packet
//******************************************************************
void send_udp_datagram()
{
char *bufferptr,temp;
unsigned int buffer_addr,i,tx_end,tx_cnt;
tx_end = TXSTART;
//load beginning page for transmit buffer
//banksel(EWRPTL);
EWRPTL = LOW_BYTE(TXSTART);
EWRPTH = HIGH_BYTE(TXSTART);
//write ENC28J60 control byte
wr_sram(tx_control_byte);
++tx_end;
//build destination MAC address
udp_packet[enetpacketDest0]=remotemacaddrc[0];
udp_packet[enetpacketDest1]=remotemacaddrc[1];
udp_packet[enetpacketDest2]=remotemacaddrc[2];
udp_packet[enetpacketDest3]=remotemacaddrc[3];
udp_packet[enetpacketDest4]=remotemacaddrc[4];
udp_packet[enetpacketDest5]=remotemacaddrc[5];
tx_end += 6;
//build source MAC address
udp_packet[enetpacketSrc0]=macaddrc[0];
udp_packet[enetpacketSrc1]=macaddrc[1];
udp_packet[enetpacketSrc2]=macaddrc[2];
udp_packet[enetpacketSrc3]=macaddrc[3];
udp_packet[enetpacketSrc4]=macaddrc[4];
udp_packet[enetpacketSrc5]=macaddrc[5];
tx_end += 6;
//build packet type (IP)
udp_packet[enetpacketType0] = 0x08;
udp_packet[enetpacketType1] = 0x00;
tx_end += 2;
//build version, length,tos,packet length
udp_packet[ip_vers_len] = 0x45;
udp_packet[ip_tos] = 0x00;
udp_packet[ip_pktlen] = 0x00;
udp_packet[ip_pktlen+1] = 0x24;
tx_end += 4;
//build packet ID
++cntri;
udp_packet[ip_id] = make8(cntri,0);
udp_packet[ip_id+1] = make8(cntri,1);
tx_end += 2;
//build fragment offset
udp_packet[ip_frag_offset] = 0x00;
udp_packet[ip_frag_offset+1] = 0x00;
tx_end += 2;
//build time to live, UDP protocol ID
udp_packet[ip_ttl] = 0x80;
udp_packet[ip_proto] = 0x11;
//build IP source and destination addresses
for(i=0;i<4;++i)
{
udp_packet[ip_srcaddr+i]=ipaddrc[i];
udp_packet[ip_destaddr+i]=remoteipaddrc[i];
tx_end += 2;
}
//calculate the IP header checksum
udp_packet[ip_hdr_cksum]=0x00;
udp_packet[ip_hdr_cksum+1]=0x00;
hdr_chksum =0;
hdrlen = (udp_packet[ip_vers_len] & 0x0F) * 4;
addr = &udp_packet[ip_vers_len];
cksum();
chksum16= ~(hdr_chksum + ((hdr_chksum & 0xFFFF0000) >> 16));
udp_packet[ip_hdr_cksum] = make8(chksum16,1);
udp_packet[ip_hdr_cksum+1] = make8(chksum16,0);
tx_end += 4;
//build UDP source port address
udp_packet[UDP_srcport] = 0x13;
udp_packet[UDP_srcport+1] = 0x88;
tx_end += 2;
//build UDP destination port address
udp_packet[UDP_destport] = 0x00;
udp_packet[UDP_destport+1] = 0x07;
tx_end += 2;
//build UDP packet length
udp_packet[UDP_len] = 0x00;
udp_packet[UDP_len+1] = 0x10;
tx_end += 2;
//udp_packet[UDP_cksum] goes here
tx_end += 2;
//UDP data goes here
for(i=0;i<8;++i)
udp_packet[UDP_data+i] = 0x30 + i;
tx_end += 8;
//calculate the UDP checksum
udp_packet[UDP_cksum] = 0x00;
udp_packet[UDP_cksum+1] = 0x00;
hdr_chksum =0;
hdrlen = 0x08;
addr = &udp_packet[ip_srcaddr];
cksum();
hdr_chksum = hdr_chksum + udp_packet[ip_proto];
hdrlen = 0x02;
addr = &udp_packet[UDP_len];
cksum();
hdrlen = make16(udp_packet[UDP_len],udp_packet[UDP_len+1]);
addr = &udp_packet[UDP_srcport];
cksum();
chksum16= ~(hdr_chksum + ((hdr_chksum & 0xFFFF0000) >> 16));
udp_packet[UDP_cksum] = make8(chksum16,1);
udp_packet[UDP_cksum+1] = make8(chksum16,0);
//mark end of UDP datagram
//banksel(ETXNDL);
ETXNDL = LOW_BYTE(tx_end);
ETXNDH = HIGH_BYTE(tx_end);
tx_cnt = tx_end - TXSTART;
//write UDP datagram to ENC28J60 Transmit Buffer
//for(buffer_addr=0;buffer_addr<tx_cnt+1;++buffer_addr)
//wr_sram(udp_packet[enetpacketDest0 + buffer_addr]);
bufferptr = packet;
while(tx_cnt)
{
EDATA = *bufferptr;
bufferptr++;
tx_cnt--;
};
//send the contents of the transmit buffer onto the network
// temp = EIR);
if(1 == (EIR & EIR_TXERIF))
{
TXERIF = 0;
TXRTS = 1;
TXRTS = 0;
}
TXIF = 0;
TXRTS = 1;
//do{
// temp = ECON1);
// }while((temp & ECON1_TXRTS) == 1);
while(1 == (ECON1 & ECON1_TXRTS));
++tx_end;
//banksel(ERDPTL);
ERDPTL= LOW_BYTE(tx_end);
ERDPTH= HIGH_BYTE(tx_end);
for(i=0;i<7;++i)
tx_status[i]= rd_sram();
//temp = ESTAT);
if(1 == (ESTAT & ESTAT_TXABRT))
printf("\r\nUDP Datagram Transmission Aborted..");
else
//printf("\r\nUDP Message Sent..");
++udpcnt;
}
//******************************************************************
//* UDP Function
//* This function uses a Visual Basic UDP program to echo the
//* data back to the VB program .
//******************************************************************
void udp()
{
unsigned int buffer_addr,i,tx_end;
char *bufferptr,temp;
//port 7 is the well-known echo port
if(packet[UDP_destport] == 0x00 && packet[UDP_destport+1] ==0x07)
{
//build the IP header
setipaddrs();
//swap the UDP source and destination ports
temp = packet[UDP_srcport];
packet[UDP_srcport] = packet[UDP_destport];
packet[UDP_destport] = temp;
temp = packet[UDP_srcport+1];
packet[UDP_srcport+1] = packet[UDP_destport+1];
packet[UDP_destport+1] = temp;
//calculate the UDP checksum
packet[UDP_cksum] = 0x00;
packet[UDP_cksum+1] = 0x00;
hdr_chksum =0;
hdrlen = 0x08;
addr = &packet[ip_srcaddr];
cksum();
hdr_chksum = hdr_chksum + packet[ip_proto];
hdrlen = 0x02;
addr = &packet[UDP_len];
cksum();
hdrlen = make16(packet[UDP_len],packet[UDP_len+1]);
addr = &packet[UDP_srcport];
cksum();
chksum16= ~(hdr_chksum + ((hdr_chksum & 0xFFFF0000) >> 16));
packet[UDP_cksum] = make8(chksum16,1);
packet[UDP_cksum+1] = make8(chksum16,0);
//echo the incoming data back to the VB program
tx_end = TXSTART;
//load beginning page for transmit buffer
//banksel(EWRPTL);
EWRPTL = LOW_BYTE(TXSTART);
EWRPTH = HIGH_BYTE(TXSTART);
//write control byte
wr_sram(tx_control_byte);
++tx_end;
tx_end = tx_end + make16(packetheader[rec_bytecnt_high],packetheader[rec_bytecnt_low] - 4);
//banksel(ETXNDL);
ETXNDL = LOW_BYTE(tx_end);
ETXNDH = HIGH_BYTE(tx_end);
//for(buffer_addr=0;buffer_addr<tx_end;++buffer_addr)
//wr_sram(packet[enetpacketDest0 + buffer_addr]);
bufferptr = packet;
while(tx_end)
{
EDATA = *bufferptr;
bufferptr++;
tx_end--;
};
//send the contents of the transmit buffer onto the network
temp = EIR;
if(1 == (EIR & EIR_TXERIF))
{
TXERIF = 0;
TXRTS = 1;
TXRTS = 0;
}
TXIF = 0;
TXRTS = 1;
//do{
// temp = ECON1);
// }while((temp & ECON1_TXRTS) == 1);
while(1 == (ECON1 & ECON1_TXRTS));
//++tx_end;
////banksel(ERDPTL);
//ERDPTL= LOW_BYTE(tx_end);
//ERDPTH= HIGH_BYTE(tx_end);
//for(i=0;i<7;++i)
//tx_status[i]= rd_sram();
//temp = ESTAT);
//if(1 == ( ESTAT) & ESTAT_TXABRT))
//printf("\r\nUDP Echo Transmission Aborted..");
//else
//printf("\r\nUDP Message Sent..");
}
//else
//LATD6 = 1;
}
//******************************************************************
//* Perform ICMP Function
//* This routine responds to a ping.
//******************************************************************
void icmp(void)
{
unsigned int buffer_addr,i,tx_end;
char temp,*bufferptr;
//set echo reply
packet[ICMP_type]=0x00;
packet[ICMP_code]=0x00;
//clear the ICMP checksum
packet[ICMP_cksum]=0x00;
packet[ICMP_cksum+1]=0x00;
//setup the IP header
setipaddrs();
//calculate the ICMP checksum
hdr_chksum =0;
hdrlen = (make16(packet[ip_pktlen],packet[ip_pktlen+1])) - ((packet[ip_vers_len] & 0x0F) * 4);
addr = &packet[ICMP_type];
cksum();
chksum16= ~(hdr_chksum + ((hdr_chksum & 0xFFFF0000) >> 16));
packet[ICMP_cksum] = make8(chksum16,1);
packet[ICMP_cksum+1] = make8(chksum16,0);
/*
//calculate the ICMP checksum
hdrlen = (make16(packet[ip_pktlen],packet[ip_pktlen+1])) - ((packet[ip_vers_len] & 0x0F) * 4);
addr = &packet[ICMP_type];
//banksel(EDMASTL);
EDMASTL = LOW_BYTE((unsigned int) addr));
EDMASTH = HIGH_BYTE((unsigned int) addr));
//debuggc[1] = EDMASTL);
//debuggc [0]= EDMASTH);
buffer_addr = ((unsigned int) addr + hdrlen)-1;
EDMANDL = LOW_BYTE(buffer_addr));
EDMANDH = HIGH_BYTE(buffer_addr));
//debuggc[3] = EDMANDL);
//debuggc[2] = EDMANDH);
ECON1 |= 0x30);
while(ECON1 & 0x20);
packet[ICMP_cksum] = EDMACSH);
packet[ICMP_cksum+1] = EDMACSL);
*/
//send the ICMP packet along on its way
tx_end = TXSTART;
//load beginning page for transmit buffer
//banksel(EWRPTL);
EWRPTL = LOW_BYTE(TXSTART);
EWRPTH = HIGH_BYTE(TXSTART);
//write control byte
wr_sram(tx_control_byte);
++tx_end;
tx_end = (tx_end) + make16(packetheader[rec_bytecnt_high],packetheader[rec_bytecnt_low] - 4);//4 is checksum of incoming packet
//banksel(ETXNDL);
ETXNDL = LOW_BYTE(tx_end);
ETXNDH = HIGH_BYTE(tx_end);
bufferptr = packet;
while(tx_end--)
{
EDATA = *bufferptr++;
//bufferptr++;
// tx_end--;
};
//for(buffer_addr=0;buffer_addr<tx_end;++buffer_addr)
//wr_sram(packet[enetpacketDest0 + buffer_addr]);
//send the contents of the transmit buffer onto the network
//temp = EIR);
//if(1 == (EIR & EIR_TXERIF))
if(TXERIF)
{
TXERIF = 0;
TXRST = 1;
TXRST = 0;
}
TXRTS = 1;
// ++tx_end;
////banksel(ERDPTL);
//ERDPTL= LOW_BYTE(tx_end);
//ERDPTH= HIGH_BYTE(tx_end);
//for(i=0;i<7;++i)
//tx_status[i]= rd_sram();
//temp = ESTAT);
//if((temp & ESTAT_TXABRT) == 1)
// printf("\r\nICMP Transmission Aborted..");
//else
// printf("\r\nICMP Message Sent..");
}
//******************************************************************
//* SETIPADDRS
//* This function builds the IP header.
//******************************************************************
void setipaddrs()
{
//move IP source address to destination address
packet[ip_destaddr]=packet[ip_srcaddr];
packet[ip_destaddr+1]=packet[ip_srcaddr+1];
packet[ip_destaddr+2]=packet[ip_srcaddr+2];
packet[ip_destaddr+3]=packet[ip_srcaddr+3];
//make ethernet module IP address source address
packet[ip_srcaddr]=ipaddrc[0];
packet[ip_srcaddr+1]=ipaddrc[1];
packet[ip_srcaddr+2]=ipaddrc[2];
packet[ip_srcaddr+3]=ipaddrc[3];
//move hardware source address to destinatin address
packet[enetpacketDest0]=packet[enetpacketSrc0];
packet[enetpacketDest1]=packet[enetpacketSrc1];
packet[enetpacketDest2]=packet[enetpacketSrc2];
packet[enetpacketDest3]=packet[enetpacketSrc3];
packet[enetpacketDest4]=packet[enetpacketSrc4];
packet[enetpacketDest5]=packet[enetpacketSrc5];
//make ethernet module mac address the source address
packet[enetpacketSrc0]=macaddrc[0];
packet[enetpacketSrc1]=macaddrc[1];
packet[enetpacketSrc2]=macaddrc[2];
packet[enetpacketSrc3]=macaddrc[3];
packet[enetpacketSrc4]=macaddrc[4];
packet[enetpacketSrc5]=macaddrc[5];
//calculate the IP header checksum
packet[ip_hdr_cksum]=0x00;
packet[ip_hdr_cksum+1]=0x00;
hdr_chksum =0;
hdrlen = (packet[ip_vers_len] & 0x0F) * 4;
addr = &packet[ip_vers_len];
cksum();
chksum16= ~(hdr_chksum + ((hdr_chksum & 0xFFFF0000) >> 16));
packet[ip_hdr_cksum] = make8(chksum16,1);
packet[ip_hdr_cksum+1] = make8(chksum16,0);
}
//******************************************************************
//* CHECKSUM CALCULATION ROUTINE
//******************************************************************
void cksum()
{
while(hdrlen > 1)
{
data_H=*addr++;
data_L=*addr++;
chksum16=make16(data_H,data_L);
hdr_chksum = hdr_chksum + chksum16;
hdrlen -=2;
}
if(hdrlen >
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -