📄 ata.c
字号:
//******************************************************************
//* Delay microsecond Function
//* This function uses Timer 3 and the A compare registers
//* to produce microsecond delays.
//*
//******************************************************************
void delay_us(unsigned int delay)
{
unsigned int i;
OCR3AH = 0x00;
OCR3AL = 0x0E;
TCCR3B = 0x00; // Stop Timer3
for(i=0;i<delay;++i)
{
TCCR3B = 0x00; // Stop Timer3
TCNT3H = 0x00; // Clear Timer3
TCNT3L = 0x00;
TCCR3B = 0x09; // Start Timer3 with clk/1
while(!(ETIFR & 0x10));
ETIFR |= 0x10;
}
}
//******************************************************************
//* Flags
//******************************************************************
#define synflag 0x01 //00000001
#define finflag 0x02 //00000010
#define synflag_bit flags & synflag
#define finflag_bit flags & finflag
//******************************************************************
//* IP ADDRESS DEFINITION
//* This is the Ethernet Module IP address.
//* You may change this to any valid address.
//******************************************************************
unsigned char MYIP[4] = { 192,168,1,150 };
//******************************************************************
//* HARDWARE (MAC) ADDRESS DEFINITION
//* This is the Ethernet Module hardware address.
//* You may change this to any valid address.
//******************************************************************
char MYMAC[6] = { 0,'A','T','M','E','L' };
//******************************************************************
//* Receive Ring Buffer Header Layout
//* This is the 4-byte header that resides infront of the
//* data packet in the receive buffer.
//******************************************************************
unsigned char pageheader[4];
#define enetpacketstatus 0x00
#define nextblock_ptr 0x01
#define enetpacketLenL 0x02
#define enetpacketLenH 0x03
//******************************************************************
//* Ethernet Header Layout
//******************************************************************
#define packetlen 1518
unsigned char packet[packetlen];
#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
#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
#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 //00000001
#define SYN_OUT packet[TCP_hdrflags+1] |= 0x02 //00000010
#define RST_OUT packet[TCP_hdrflags+1] |= 0x04 //00000100
#define PSH_OUT packet[TCP_hdrflags+1] |= 0x08 //00001000
#define ACK_OUT packet[TCP_hdrflags+1] |= 0x10 //00010000
#define URG_OUT packet[TCP_hdrflags+1] |= 0x20 //00100000
//******************************************************************
//* 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
//******************************************************************
//* 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
//******************************************************************
//* REALTEK CONTROL REGISTER OFFSETS
//* All offsets in Page 0 unless otherwise specified
//******************************************************************
#define CR 0x00
#define PSTART 0x01
#define PAR0 0x01 // Page 1
#define CR9346 0x01 // Page 3
#define PSTOP 0x02
#define BNRY 0x03
#define TSR 0x04
#define TPSR 0x04
#define TBCR0 0x05
#define NCR 0x05
#define TBCR1 0x06
#define ISR 0x07
#define CURR 0x07 // Page 1
#define RSAR0 0x08
#define CRDA0 0x08
#define RSAR1 0x09
#define CRDAL 0x09
#define RBCR0 0x0A
#define RBCR1 0x0B
#define RSR 0x0C
#define RCR 0x0C
#define TCR 0x0D
#define CNTR0 0x0D
#define DCR 0x0E
#define CNTR1 0x0E
#define IMR 0x0F
#define CNTR2 0x0F
#define RDMAPORT 0X10
#define RSTPORT 0x18
//******************************************************************
//* RTL8019AS INITIAL REGISTER VALUES
//******************************************************************
#define rcrval 0x04
#define tcrval 0x00
#define dcrval 0x58 // was 0x48
#define imrval 0x11 // PRX and OVW interrupt enabled
#define txstart 0x40
#define rxstart 0x46
#define rxstop 0x60
//******************************************************************
//* RTL8019AS DATA/ADDRESS PIN DEFINITIONS
//******************************************************************
#define rtldata PORTA
#define tortl DDRA = 0xFF
#define fromrtl DDRA = 0x00; \
rtldata = 0xFF;
//******************************************************************
//* RTL8019AS 9346 EEPROM PIN DEFINITIONS
//******************************************************************
//#define EESK 0x08 //PORTD3 00001000
//#define EEDI 0x10 //PORTD4 00010000
//#define EEDO 0x20 //PORTD5 00100000
//******************************************************************
//* RTL8019AS PIN DEFINITIONS
//******************************************************************
#define ETH_PORT_DDR DDRE
#define ETH_PORT_OUT PORTE
#define ETH_PORT_IN PINE
#define ETH_RSTDRV 0x04
#define ETH_IOWB 0x08
#define ETH_IORB 0x10
#define ETH_INT0 0x20
//******************************************************************
//* RTL8019AS PIN MACROS
//******************************************************************
#define SET_ETH_IORB ETH_PORT_OUT |= ETH_IORB
#define CLR_ETH_IORB ETH_PORT_OUT &= ~ETH_IORB
#define SET_ETH_IOWB ETH_PORT_OUT |= ETH_IOWB
#define CLR_ETH_IOWB ETH_PORT_OUT &= ~ETH_IOWB
#define SET_ETH_RSTDRV ETH_PORT_OUT |= ETH_RSTDRV
#define CLR_ETH_RSTDRV ETH_PORT_OUT &= ~ETH_RSTDRV
//#define clr_EEDO eeprom &= ~EEDO
//#define set_EEDO eeprom |= EEDO
#define clr_synflag flags &= ~synflag
#define set_synflag flags |= synflag
#define clr_finflag flags &= ~finflag
#define set_finflag flags |= finflag
#define set_packet32(d,s) packet[d] = make8(s,3); \
packet[d+1] = make8(s,2); \
packet[d+2] = make8(s,1); \
packet[d+3]= make8(s,0);
#define make8(var,offset) (var >> (offset * 8)) & 0xFF
#define make16(varhigh,varlow) ((varhigh & 0xFF)* 0x100) + (varlow & 0xFF)
#define make32(var1,var2,var3,var4) \
((unsigned long)var1<<24)+((unsigned long)var2<<16)+ \
((unsigned long)var3<<8)+((unsigned long)var4)
//******************************************************************
//* RTL8019AS ISR REGISTER DEFINITIONS
//******************************************************************
#define RST 0x80 //1000000
#define RDC 0x40 //0100000
#define OVW 0x10 //0001000
#define PRX 0x01 //0000001
//******************************************************************
//* Application Code
//* Your application code goes here.
//* This particular code echos the incoming Telnet data to the LCD
//******************************************************************
void application_code()
{
nop;
}
//******************************************************************
//* UDP Function
//* This function uses a Visual Basic UDP program to echo the
//* data back to the VB program and talk to the LCD.
//******************************************************************
void udp()
{
//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
data_L = packet[UDP_srcport];
packet[UDP_srcport] = packet[UDP_destport];
packet[UDP_destport] = data_L;
data_L = packet[UDP_srcport+1];
packet[UDP_srcport+1] = packet[UDP_destport+1];
packet[UDP_destport+1] = data_L;
//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
echo_packet();
}
//buttons on the VB GUI are pointed towards port address 5000 decimal
else if(packet[UDP_destport] == 0x13 && packet[UDP_destport+1] == 0x88)
{
// switch (packet[UDP_data])
// {
// case 0:
// lcdcls;
// break;
// case 1:
// line1;
// break;
// case 2:
// line2;
// break;
// case 3:
// line3;
// break;
// case 4:
// line4;
// break;
// default:
// lcd_send_byte(1,packet[UDP_data]);
// }
nop;
}
}
//******************************************************************
//* TCP Function
//* This function uses TCP protocol to act as a Telnet server on
//* port 8088 decimal. The application function is called with
//* every incoming character.
//******************************************************************
void tcp()
{
//assemble the destination port address from the incoming packet
portaddr = make16(packet[TCP_destport],packet[TCP_destport+1]);
//calculate the length of the data coming in with the packet
//tcpdatalen_in = incoming packet length - incoming ip header length -
//incoming tcp header length
tcpdatalen_in = (make16(packet[ip_pktlen],packet[ip_pktlen+1]))- \
((packet[ip_vers_len] & 0x0F)* 4)-(((packet[TCP_hdrflags] & 0xF0) >> 4) * 4);
//If an ACK is received and the destination port address is valid
//and no data is in the packet
if(ACK_IN && portaddr == MY_PORT_ADDRESS && tcpdatalen_in == 0x00)
{
//assemble the acknowledgment number from the incoming packet
incoming_ack =make32(packet[TCP_acknum],packet[TCP_acknum+1], \
packet[TCP_acknum+2],packet[TCP_acknum+3]);
//if the incoming packet is a result of session establishment
if(synflag_bit)
{
//clear the SYN flag
clr_synflag;
//the incoming acknowledgment is my new sequence number
my_seqnum = incoming_ack;
//send the Telnet server banner
//limit the character count to 40 decimal
strcpy(&packet[TCP_data],"EDTP AVR Telnet SERVER>");
//length of the banner message
tcpdatalen_out = 23;
//expect to get an acknowledgment of the banner message
expected_ack = my_seqnum +tcpdatalen_out;
//send the TCP/IP packet
send_tcp_packet();
}
}
//if an ack is received and the port address is valid and there is data
//in the incoming packet
if((ACK_IN) && portaddr == MY_PORT_ADDRESS && tcpdatalen_in)
{
for(i=0;i<tcpdatalen_in;++i)
//receive the data and put it into the incoming data buffer
aux_data[i] = packet[TCP_data+i];
application_code();
//assemble the acknowledgment number from the incoming packet
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -