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

📄 ethernet_mini_driver.c

📁 利用 pic18f67j60 单片机来实现10M IP网络的连接
💻 C
📖 第 1 页 / 共 5 页
字号:
//////////////////////////////////////////////////////////////////////
// Ethernet MINI DRIVER
// Author: Fred Eady
// Version: 1.0
// Date: 05/06/07
// Description: ARP, PING, ECHO, TCP, UDP, DHCP
//
// THESE ARE BASIC DRIVER ROUTINES AND ARE NOT INTENDED TO BE USED IN
// A PRODUCTION ENVIRONMENT. EDTP OFFERS THESE DRIVER ROUTINES AS-IS
// AND IS NOT RESPONSIBLE FOR ANY MISUSE OR MISAPPLICATION OF THIS 
// CODE.
//////////////////////////////////////////////////////////////////////

#include <pic18.h>
#include <string.h>
#include <stdio.h>
#include "pic18f67j60_driver.h"

//******************************************************************
//*	Debugging variables
//* You can incorporate these back into their routines as locals if 
//* you wish.. I have put them here so you can see them using the
//* WATCH window of MPLAB.
//******************************************************************
char const *msgpointer;
char dhcpmsgchar;
char *packetptr;
//unsigned int tx_end,tx_cnt;
//char debugc, debuggc[400];
char incoming;
//unsigned int phstat_1,phstat_2,erxwrpt_l,erxwrpt_h,erxrdpt_l,erxrdpt_h;
//unsigned int debugi,debuggi[1];
//char econ_1,econ_2,eir_1,estat_1,eie_1;
unsigned int packet_cnt,new_rdptr,udpcnt,msecs_timer,clock_timer;
unsigned int offsetval, chksum_lo,chksum_hi,tx_end;
char lastsec,dhcpreturncode,msgtype,msglen;
unsigned long leasetime,lease_timer;
//******************************************************************
//*	Configuration Fuse Setings
//******************************************************************
// 18F67J60 FUSES
__CONFIG(1,XINSTDIS & WDTDIS & DEBUGDIS);
__CONFIG(2,HSPLL & IESODIS);

#define esc 0x1B

#define LED0	LATF1
//******************************************************************
//*	MACROS
//******************************************************************
#define rd_sram()	(EDATA)
#define wr_sram(a)	EDATA = a
//******************************************************************
//*	ETHERNET BUFFER DEFINITIONS 
//******************************************************************
#define MAXFRAME 		1518
#define TX_BUFFER_SIZE  1518
#define TXSTART			(8192 - (TX_BUFFER_SIZE + 8))
//#define TXEND			(TX_BUFFER_SIZE + 8)
#define RXSTART			0x0000
#define	RXSTOP			((TXSTART - 2) | 0x0001)
#define RXSIZE			(RXSTOP-(RXSTART+1))
//******************************************************************
//*	Flags
//******************************************************************
char flags;
#define synflag 	0x01  
#define	finflag		0x02  
#define entryflag 	0x04
#define arpflag		0x08
#define hexflag		0x10
#define bsynflag   	(flags & synflag)
#define bfinflag	(flags & finflag)
#define bentryflag	(flags & entryflag)
#define barpflag	(flags & arpflag)
#define bhexflag	(flags & hexflag)

#define clr_synflag flags &= ~synflag
#define set_synflag flags |= synflag
#define clr_finflag flags &= ~finflag
#define set_finflag flags |= finflag
#define clr_entryflag flags &= ~entryflag
#define set_entryflag flags |= entryflag
#define clr_arpflag flags &= ~arpflag
#define set_arpflag flags |= arpflag
#define clr_hexflag flags &= ~hexflag
#define set_hexflag flags |= hexflag

char dhcpflags;
#define bound 		0x01  
#define	offerrec	0x02  
#define done		0x04
#define trashit		0x08
#define newdhcppkt	0x10
#define bbound		(dhcpflags & bound)
#define bofferrec	(dhcpflags & offerrec)
#define bdone		(dhcpflags & done)
#define btrashit	(dhcpflags & trashit)
#define bnewdhcppkt	(dhcpflags & newdhcppkt)

#define clr_bound 		dhcpflags &= ~bound
#define set_bound 		dhcpflags |= bound
#define clr_offerrec 	dhcpflags &= ~offerrec
#define set_offerrec 	dhcpflags |= offerrec
#define clr_done		dhcpflags &= ~done
#define set_done 		dhcpflags |= done
#define clr_trashit		dhcpflags &= ~trashit
#define set_trashit		dhcpflags |= trashit
#define clr_newdhcppkt	dhcpflags &= ~newdhcppkt
#define set_newdhcppkt	dhcpflags |= newdhcppkt
//******************************************************************
//*	TELNET SERVER BANNER STATEMENT CONSTANT
//******************************************************************
char const telnet_banner[] = "\r\nEDTP 67J60 Driver>";
char const dhcpmacmsg[] = "\r\nMY MAC ADDRESS = 00-00-46-54-49-49";
char const dhcpipmsg[] = "\r\nNEW IP ADDRESS = ";
char const dhcpgwaymsg[] = "\r\nNEW GATEWAY IP ADDRESS = ";
char const dhcpsubnetmaskmsg[] = "\r\nNEW SUBNET MASK = ";
//******************************************************************
//*	Port Definitions
//*   This address is used by TCP and the Telnet function.
//*   This can be changed to any valid port number as long as
//*   you modify your code to recognize the new port number.
//******************************************************************
#define  MY_PORT_ADDRESS      0x1F98  // 8088 DECIMAL
#define	 MCHP_UDP_PORT_ADDRESS	0x765F //30303 DECIMAL
//******************************************************************
//*	IP ADDRESS DEFINITION
//*   These are the default Ethernet Module IP addresses.
//*   You may change these to any valid address.
//******************************************************************
char ipaddrascii[3];
char ipaddrc[4] = { 192,168,1,150 };
char tempipaddrc[4];
char remoteipaddrc[4] = {192,168,1,100};
char svridc[4];
char gwayipaddrc[4];
char tempgwayipaddrc[4];
char subnetmaskc[4];
char tempsubnetmaskc[4];
char tempsvridc[4];
char templeasetimec[4];
char broadcastipaddrc[4] = {192,168,1,255};
//******************************************************************
//*	HARDWARE (MAC) ADDRESS DEFINITION
//*   This is the default Ethernet Module hardware address.
//*   You may change this to any valid address.
//******************************************************************
char macaddrc[6] = { 0,0,'E','D','T','P'};
char remotemacaddrc[6]; 
char svrmacaddrc[6];
//******************************************************************
//*	Function Declarations
//******************************************************************
//void read_xmit_buffer(void);
void init_EUSART(void);
unsigned char CharInQueue(void);
void putch(unsigned char c);
int recvchar(void);
int sendchar(int data);
void init_67J60(void);
void arp_reply(void);
void tcp(void);
void icmp(void);
void udp(void);
void setipaddrs(void);
void cksum(void);
void send_tcp_packet(void);
void assemble_ack(void);
void send_udp_datagram(void);
void arp_request(void);
void get_frame(void);
void application_code(void);
void dhcp_state_engine(void);
void init_DHCP(void);
void send_dhcp(char dhcpmsg_type);
char receive_dhcp(void);
void send_dhcp_bound_datagram(void);
void binary_to_ascii(unsigned int binarynumber);

//******************************************************************
//*	SRAM Definitions
//******************************************************************
char aux_data[16];            //received data area
char *addr;
char byte_read,data_H,data_L,cntr,byteout;
char high_nibble, low_nibble, high_char, low_char,resend;
unsigned int txlen,rxlen,chksum16,hdrlen,tcplen,tcpdatalen_in;
unsigned int tcpdatalen_out,ISN,portaddr,ip_packet_len;
unsigned long hdr_chksum,my_seqnum,client_seqnum,incoming_ack,expected_ack;
char tx_status[6];
char hours, mins, secs;
char secs_timer;
char chr_count,bytein;
char eeaddr;
unsigned int cntri;
unsigned int parameters[11];
#define local_ipaddr				0x00
#define	local_ipaddr3				0x00
#define	local_ipaddr2				0x01
#define	local_ipaddr1				0x02
#define	local_ipaddr0				0x03
#define host_ipaddr					0x04
#define	host_ipaddr3				0x04
#define	host_ipaddr2				0x05
#define	host_ipaddr1				0x06
#define	host_ipaddr0				0x07
#define loopsecs					0x08
unsigned long udpaddrs[2];
#define local_udpaddr				0x00
#define remote_udpaddr				0x01
//******************************************************************
//*	DHCP Definitions
//******************************************************************
#define DHCP_CLIENT_PORT                	0x44//(68u)
#define DHCP_SERVER_PORT                	0x43//(67u)

#define BOOT_REQUEST                    	0x01//(1u)
#define BOOT_REPLY                      	0x02//(2u)
#define HW_TYPE                         	0x01//(1u)
#define HW_TYPE_LEN		                  	0x06//(6u)

#define DHCP_MESSAGE_TYPE               	0x35//(53u)
#define DHCP_MESSAGE_TYPE_LEN           	0x01//(1u)

#define DHCP_UNKNOWN_MESSAGE            	0x00//(0u)

#define DHCP_DISCOVER_MESSAGE           	0x01//(1u)
#define DHCP_OFFER_MESSAGE              	0x02//(2u)
#define DHCP_REQUEST_MESSAGE            	0x03//(3u)
#define DHCP_DECLINE_MESSAGE            	0x04//(4u)
#define DHCP_ACK_MESSAGE                	0x05//(5u)
#define DHCP_NAK_MESSAGE                	0x06//(6u)
#define DHCP_RELEASE_MESSAGE            	0x07//(7u)

#define DHCP_SERVER_IDENTIFIER          	0x36//(54u)
#define DHCP_SERVER_IDENTIFIER_LEN      	0x04//(4u)

#define DHCP_PARAM_REQUEST_LIST         	0x37//(55u)
#define DHCP_PARAM_REQUEST_LIST_LEN     	0x02//(2u)
#define DHCP_PARAM_REQUEST_IP_ADDRESS       0x32//(50u)
#define DHCP_PARAM_REQUEST_IP_ADDRESS_LEN   0x04//(4u)
#define DHCP_SUBNET_MASK                	0x01//(1u)
#define DHCP_ROUTER                     	0x03//(3u)
#define DHCP_IP_LEASE_TIME              	0x33//(51u)
#define DHCP_END_OPTION                 	0xFF//(255u)

typedef enum _DHCP_STATES
{
	DHCP_ENTRY,					
	DHCP_INIT,						
	DHCP_WAIT,
	DHCP_BROADCAST,
	DHCP_DISCOVER,
	DHCP_REQUEST,
	DHCP_BIND,
	DHCP_BOUND,
	DHCP_DISABLED
}DHCP_STATE_LIST;

DHCP_STATE_LIST DHCPSTATE;

//******************************************************************
//*	Receive Ring Buffer Header Layout
//*   This is the 4-byte header that resides infront of the
//*   data packet in the receive buffer.
//******************************************************************
#define tx_control_byte	   0x0E
char  packetheader[6];
#define  nextpacket_low    0x00
#define  nextpacket_high   0x01
#define	rec_bytecnt_low    0x02
#define	rec_bytecnt_high   0x03
#define	rec_status_low 	   0x04
#define	rec_status_high	   0x05
//******************************************************************
//*	Ethernet Header Layout
//******************************************************************
char  udp_packet[100];                   
char  packet[1118];                
#define	enetpacketDest0	   0x00  //destination mac address
#define	enetpacketDest1	   0x01
#define	enetpacketDest2	   0x02
#define	enetpacketDest3	   0x03
#define	enetpacketDest4	   0x04
#define	enetpacketDest5	   0x05
#define	enetpacketSrc0	   0x06  //source mac address
#define	enetpacketSrc1	   0x07
#define	enetpacketSrc2	   0x08
#define	enetpacketSrc3	   0x09
#define	enetpacketSrc4	   0x0A
#define	enetpacketSrc5	   0x0B
#define	enetpacketType0	   0x0C  //type/length field
#define	enetpacketType1	   0x0D
#define enetpacketData     0x0E  //IP data area begins here
//******************************************************************
//*	ARP Layout
//******************************************************************
#define	arp_hwtype		   0x0E
#define	arp_prtype		   0x10
#define	arp_hwlen		   0x12
#define	arp_prlen		   0x13
#define	arp_op			   0x14
#define	arp_shaddr		   0x16   //arp source mac address
#define	arp_sipaddr		   0x1C   //arp source ip address
#define	arp_thaddr		   0x20   //arp target mac address
#define	arp_tipaddr		   0x26   //arp target ip address
//******************************************************************
//*	IP Header Layout
//******************************************************************
#define	ip_vers_len		   0x0E	//IP version and header length 1a19
#define	ip_tos			   0x0F	//IP type of service
#define	ip_pktlen		   0x10	//packet length
#define	ip_id			   0x12	//datagram id
#define	ip_frag_offset	   0x14	//fragment offset
#define	ip_ttl			   0x16	//time to live
#define	ip_proto		   0x17	//protocol (ICMP=1, TCP=6, UDP=11)
#define	ip_hdr_cksum	   0x18	//header checksum 1a23
#define	ip_srcaddr		   0x1A	//IP address of source
#define	ip_destaddr		   0x1E	//IP addess of destination
#define	ip_data			   0x22	//IP data area
//******************************************************************
//*	TCP Header Layout
//******************************************************************
#define	TCP_srcport		   0x22	//TCP source port
#define	TCP_destport   	   0x24	//TCP destination port
#define	TCP_seqnum  	   0x26	//sequence number
#define	TCP_acknum	       0x2A	//acknowledgement number
#define	TCP_hdrflags	   0x2E	//4-bit header len and flags
#define	TCP_window		   0x30	//window size
#define	TCP_cksum		   0x32	//TCP checksum
#define	TCP_urgentptr      0x34	//urgent pointer
#define TCP_data           0x36 //option/data
//******************************************************************
//*	TCP Flags
//*   IN flags represent incoming bits
//*   OUT flags represent outgoing bits
//******************************************************************
#define  FIN_IN               (packet[TCP_hdrflags+1] & 0x01)
#define  SYN_IN               (packet[TCP_hdrflags+1] & 0x02)
#define  RST_IN               (packet[TCP_hdrflags+1] & 0x04)
#define  PSH_IN               (packet[TCP_hdrflags+1] & 0x08)
#define  ACK_IN               (packet[TCP_hdrflags+1] & 0x10)
#define  URG_IN               (packet[TCP_hdrflags+1] & 0x20)
#define  FIN_OUT              packet[TCP_hdrflags+1] |= 0x01 //0000 0001 
#define  SYN_OUT              packet[TCP_hdrflags+1] |= 0x02 //0000 0010 
#define  RST_OUT              packet[TCP_hdrflags+1] |= 0x04 //0000 0100 
#define  PSH_OUT              packet[TCP_hdrflags+1] |= 0x08 //0000 1000 
#define  ACK_OUT              packet[TCP_hdrflags+1] |= 0x10 //0001 0000 
#define  URG_OUT              packet[TCP_hdrflags+1] |= 0x20 //0010 0000 
//******************************************************************
//*	IP Protocol Types
//******************************************************************
#define	PROT_ICMP			   0x01
#define	PROT_TCP			   0x06
#define	PROT_UDP			   0x11
//******************************************************************
//*	ICMP Header
//******************************************************************
#define	ICMP_type			   ip_data
#define	ICMP_code			   ICMP_type+1
#define	ICMP_cksum			   ICMP_code+1
#define	ICMP_id				   ICMP_cksum+2
#define	ICMP_seqnum			   ICMP_id+2
#define ICMP_data              ICMP_seqnum+2
//******************************************************************
//*	UDP Header
//******************************************************************
#define	UDP_srcport			   ip_data
#define	UDP_destport		   UDP_srcport+2
#define	UDP_len				   UDP_destport+2
#define	UDP_cksum			   UDP_len+2
#define	UDP_data			   UDP_cksum+2
//******************************************************************
//*	DHCP Message
//******************************************************************
#define	DHCP_op					UDP_data
#define	DHCP_htype				DHCP_op+1
#define DHCP_hlen				DHCP_htype+1
#define DHCP_hops				DHCP_hlen+1
#define DHCP_xid				DHCP_hops+1
#define DHCP_secs				DHCP_xid+4
#define DHCP_flags				DHCP_secs+2
#define DHCP_ciaddr				DHCP_flags+2
#define DHCP_yiaddr				DHCP_ciaddr+4
#define DHCP_siaddr				DHCP_yiaddr+4
#define DHCP_giaddr				DHCP_siaddr+4
#define DHCP_chaddr				DHCP_giaddr+4
#define DHCP_sname				DHCP_chaddr+16
#define DHCP_file				DHCP_sname+64
#define DHCP_options			DHCP_file+128
#define DHCP_message_end		DHCP_options+312
//******************************************************************
//*	Macros
//******************************************************************
#define bitset(var, bitno) ((var) |= 1 << (bitno))
#define bitclr(var, bitno) ((var) &= ~(1 << (bitno)))
#define make8(var,offset)	((unsigned int)var >> (offset * 8)) & 0x00FF
#define	make16(varhigh,varlow)	(((unsigned int)varhigh & 0xFF)* 0x100) + ((unsigned int)varlow & 0x00FF)
#define make32(var1,var2,var3,var4) \
		((unsigned long)var1<<24)+((unsigned long)var2<<16)+ \
		((unsigned long)var3<<8)+((unsigned long)var4)		
#define make32i(var1,var2) ((unsigned long)var1<<16)+((unsigned long)var2)

#define  set_packet32(d,s) packet[d]   = (s & 0xFF000000) >> 24; \
                           packet[d+1] = (s & 0x00FF0000) >> 16; \
                           packet[d+2] = (s & 0x0000FF00) >> 8;  \
                           packet[d+3] = (s & 0x000000FF);
//******************************************************************
//*	USART Definitions
//******************************************************************
#define BAUD	57600		//desired baud rate
#define FOSC	41666667	//oscillator frequency
#define DIVIDER ((unsigned int)(FOSC/(16 * BAUD) -1))
//1,2,4,8,16,32,64,128 or 256 bytes 
#define USART_RX_BUFFER_SIZE  256      
#define USART_RX_BUFFER_MASK ( USART_RX_BUFFER_SIZE - 1 )
// 1,2,4,8,16,32,64,128 or 256 bytes 
#define USART_TX_BUFFER_SIZE  256      
#define USART_TX_BUFFER_MASK ( USART_TX_BUFFER_SIZE - 1 )

unsigned char USART_RxBuf[USART_RX_BUFFER_SIZE],USART_TxBuf[USART_TX_BUFFER_SIZE];
unsigned char USART_TxHead,USART_TxTail,USART_RxHead,USART_RxTail;
char remainder;
//******************************************************************
//*	BINARY TO ASCII CONVERSION
//******************************************************************
void binary_to_ascii(unsigned int binarynumber)
{
	char i;
	
	for(i=0;i<3;++i)
		ipaddrascii[i] = 0x30;
	if(binarynumber > 255)
	return;
	if(binarynumber >= 100)
	{
	do{
			binarynumber-=100;
			ipaddrascii[0]+= 1;
	  }while(binarynumber >= 100);
	}
	if(binarynumber >= 10)
	{
	  do{
			binarynumber-=10;

⌨️ 快捷键说明

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