'------------------------------------------------------------------------------------ ' WebCat / Ethernut 1.3F - Atmega128 and RTL8019AS - ARP - UDP - PING - HTML '------------------------------------------------------------------------------------ ' ' january 2006 - Ben Zijlstra - Netherlands - https://benshobbycorner.nl/bzijlstra ' history: ' ' 02-01-2006 tested it with Bascom-AVR 1.11.81 ' 05-01-2006 some adaptions of the UDP part ' 10-01-2006 extra comments ' ' ' Lockbits: ' Lockbit 54 : 11 ' Lockbit 32 : 11 ' Lockbit 10 : 11 ' Fusebits: ' Fusebit 7 : 0 ' Fusebit 6 : 0 ' Fusebit 98 : 11 ' Fusebit DCBA : 1111 ' Fusebits High ' Fusebits E : 1 ' Fusebits F : 1 change on the original WebCat = JTAG disabled ' Fusebits G : 0 ' Fusebits H : 0 ' Fusebits I : 0 ' Fusebits KL : 01 ' Fusebits M : 1 ' Fusebits Extended ' Fusebits P : 1 ' Fusebits Q : 1 'ARP-request werkt. Wordt toegepast bij het opzoeken van het MAC-address van de gateway $regfile "m128def.dat" $crystal = 14745600 $baud = 57600 $waitstate 'XRAM wait state $xa 'External Access enabled $default Xram 'memory map: ' '$0000-$0FFF Internal Ram Atnmega128 '$1000-$7FFF external SRAM '$8300-$831F RTL8019as ' In the EEPROM: ' ' 00 = .0 - 0 if LCD present (4 x 20) ' 00 = .1 ' 00 = .2 ' 00 = .3 ' 00 = .4 ' 00 = .5 ' 00 = .6 ' 00 = .7 ' 01 = IP-number msb ' 02 = IP-number ' 03 = IP-number ' 04 = IP-number lsb ' 05 = MAC-address ' 06 = MAC-address ' 07 = MAC-address ' 08 = MAC-address ' 09 = MAC-address ' 10 = MAC-address ' 11 = Portnumber ' 12 = Portnumber ' 13 = GW-IP-number msb ' 14 = GW-IP-number ' 15 = GW-IP-number ' 16 = GW-IP-number lsb ' MAC-address of Gateway is fetched everytime the WebCat starts ' 23 = Length URL-refresh string ' 24 = start of URL-refresh string ' .... upto end of URL-refresh string $hwstack = 128 $swstack = 128 $framesize = 128 Config Lcd = 20 * 4 Config Lcdpin = Pin , Db4 = Portb.4 , Db5 = Portb.5 , Db6 = Portb.6 , Db7 = Portb.7 , E = Portb.3 , Rs = Portb.2 Config Int5 = Falling Config Sda = Portd.6 Config Scl = Portd.7 Declare Sub Write_rtl8019as(byval Regaddr As Byte , Byval Regdata As Byte) Declare Sub Read_rtl8019as(byval Regaddr As Byte) Declare Sub Init_rtl8019as Declare Sub Getpacket Declare Sub Overrun Declare Sub Arp Declare Sub Icmp Declare Sub Udp_receive Declare Sub Udp_send Declare Sub Write_dest_mac Declare Sub Setipaddrs Declare Sub Icmp_checksum Declare Sub Echopacket Declare Sub Packetshape Declare Sub Ip_header_checksum Declare Sub General_part_checksum(byval Val1 As Byte , Byval Val2 As Word) Declare Sub Udp_checksum Declare Sub Tcp Declare Sub Http Declare Sub Tcp_checksum Declare Sub Send_tcp_packet Declare Sub Setup_packet Declare Sub Assemble_ack Declare Sub Tcpseqtomyseq Declare Sub Configure Declare Sub Default_message Declare Sub Read_ip Declare Sub Print_ip Declare Sub Read_mac Declare Sub Print_mac Declare Sub Arp_request Declare Sub Arp_reply Declare Sub Print_gwip Declare Sub Read_gwip Declare Sub Ntp Const Debug = 0 ' put 1 for debug, 0 for no debug Const Msg_initfail = "Init failed" Const Dcrval = &H58 Const Txstart = &H40 Const Rxstart = &H46 Const Rxstop = &H60 Const Imrval = &H11 Const Tcrval = &H00 'IP protocol types 'icmp Const Prot_icmp = &H01 'tcp Const Prot_tcp = &H06 'udp Const Prot_udp = &H11 Const Synflag = 0 Const Finflag = 1 'NIC_CR page 0/1/2/3 (ps1=x, ps0=x) x = depending on page ' Const Nic_cr = &H00 ' Const Nic_cr_stp = &B0000_0001 'stop command - Power up default Const Nic_cr_sta = &B0000_0010 'start command Const Nic_cr_txp = &B0000_0100 'transmit a packet Const Nic_cr_rd0 = &B0000_1000 'remote read Const Nic_cr_rd1 = &B0001_0000 'remote write Const Nic_cr_rd2 = &B0010_0000 'abort/complete remote DMA Const Nic_cr_ps0 = &B0100_0000 'register page 0 Const Nic_cr_ps1 = &B1000_0000 'register page 1 Const Nic_rdmaport = &H10 'all pages Const Nic_rstport = &H18 'all pages 'NIC_PSTART page 0 (ps1=0, ps0=0) ' Const Nic_pstart = &H01 ' 'NIC_PSTOP page 0 (ps1=0, ps0=0) ' Const Nic_pstop = &H02 ' 'NIC_BNRY page 0 (ps1=0, ps0=0) ' Const Nic_bnry = &H03 ' 'NIC_TPSR page 0 (ps1=0, ps0=0) ' Const Nic_tpsr = &H04 ' 'NIC_TBCR0 page 0 (ps1=0, ps0=0) ' Const Nic_tbcr0 = &H05 ' 'NIC_TBCR1 page 0 (ps1=0, ps0=0) ' Const Nic_tbcr1 = &H06 ' 'NIC_ISR page 0 (ps1=0, ps0=0) ' Const Nic_isr = &H07 ' Const Nic_rdc = &B0100_0000 'NIC_RSAR0 page 0 (ps1=0, ps0=0) ' Const Nic_rsar0 = &H08 ' 'NIC_RSAR1 page 0 (ps1=0, ps0=0) ' Const Nic_rsar1 = &H09 ' 'NIC_rbcr0 page 0 (ps1=0, ps0=0) ' Const Nic_rbcr0 = &H0A ' 'NIC_rbcr0 page 0 (ps1=0, ps0=0) ' Const Nic_rbcr1 = &H0B ' 'NIC_rcr page 0 (ps1=0, ps0=0) receive configuration register ' Const Nic_rcr = &H0C ' 'NIC_tcr page 0 (ps1=0, ps0=0) transmit configuration register ' Const Nic_tcr = &H0D ' 'NIC_DCR page 0 (ps1=0, ps0=0) ' Const Nic_dcr = &H0E ' 'NIC_IMR page 0 (ps1=0, ps0=0) interrupt mask register ' Const Nic_imr = &H0F ' 'NIC_CURR page 1 (ps1=0, ps0=1) ' Const Nic_curr = &H07 ' 'NIC_9346CR page 3 (ps1=1, ps0=1) ' Const Nic_9346cr = &H01 ' Const Nic_9346cr_eem1 = &B1000_0000 Const Nic_9346cr_eem0 = &B0100_0000 Const Nic_config2_bselb = &B0010_0000 'NIC_CONFIG2 page 3 (ps1=1, ps0=1) ' Const Nic_config2 = &H05 'NIC_CONFIG3 page 3 (ps1=1, ps0=1) ' Const Nic_config3 = &H06 Dim Regaddr As Byte Dim Regdata As Byte 'Ethernet header layout Dim Byte_read As Byte Dim Whulp0 As Word Dim Hulp1 As Byte Dim Hulp2 As Word Dim Hulp3 As Word Dim Hulp4 As Byte Dim Mymac(6) As Byte Dim Gwmac(6) As Byte Dim Data_l As Byte Dim Data_h As Byte Dim Bhulp0 As Byte Dim I As Integer Dim T As Byte Dim Txlen As Word Dim I_header_length As Word Dim I_odd As Byte Dim I_chksum32 As Long Dim Rxlen As Word Dim Val1 As Byte Dim Val2 As Word ' up to 65535 characters Dim Val3 As Byte Dim Val4 As Byte Dim I_x As Word Dim I_checksum16 As Word Dim Tempstring As String * 20 Dim Dsp_present As Byte Dim Idletime As Long Dim Resend As Byte Dim Tcp_fin As Sram Bit Dim Tcp_syn As Sram Bit Dim Tcp_rst As Sram Bit Dim Tcp_psh As Sram Bit Dim Tcp_ack As Sram Bit Dim Tcp_urg As Sram Bit Dim Tcpdatalen_in As Word Dim Tcpdatalen_out As Word Dim Ip_packet_len As Word Dim Flags As Byte Dim Msg_temp As String * 55 Dim Y As Word Dim Tempstring1 As String * 1 Dim Expected_ack As Long Dim Mymacs(6) As String * 3 Dim Ippart(4) As String * 4 'variables with overlays Dim Myip(4) As Byte Dim My_ip As Long At Myip Overlay Dim Pageheader(4) As Byte Dim T_enetpacketlenl As Byte At Pageheader + 2 Overlay Dim T_enetpacketlenh As Byte At Pageheader + 3 Overlay Dim Result16 As Word Dim Result16h As Byte At Result16 + 1 Overlay Dim Result16l As Byte At Result16 Overlay Dim Hulp5 As Word Dim Hulp5h As Byte At Hulp5 + 1 Overlay Dim Hulp5l As Byte At Hulp5 Overlay Dim Hulp6 As Word Dim Hulp6h As Byte At Hulp6 + 1 Overlay Dim Hulp6l As Byte At Hulp6 Overlay Dim I_value16 As Word Dim I_value16h As Byte At I_value16 + 1 Overlay Dim I_value16l As Byte At I_value16 Overlay 'ARP-request Dim Gwip(4) As Byte Dim Arpreqip As Long At Gwip Overlay 'for NTP-routine Dim S(4) As Byte Dim L1 As Long At S Overlay ' Overlay a long variable to receive-string ' with overlay you need no transfer from the byte-array to a long-variable Dim L2 As Long Dim Packet(1500) As Byte 'Ethernet packet destination Dim T_enetpacketdest0 As Byte At Packet Overlay Dim T_enetpacketdest1 As Byte At Packet + &H01 Overlay Dim T_enetpacketdest2 As Byte At Packet + &H02 Overlay Dim T_enetpacketdest3 As Byte At Packet + &H03 Overlay Dim T_enetpacketdest4 As Byte At Packet + &H04 Overlay Dim T_enetpacketdest5 As Byte At Packet + &H05 Overlay 'Ethernet packet source Dim T_enetpacketsrc0 As Byte At Packet + &H06 Overlay Dim T_enetpacketsrc1 As Byte At Packet + &H07 Overlay Dim T_enetpacketsrc2 As Byte At Packet + &H08 Overlay Dim T_enetpacketsrc3 As Byte At Packet + &H09 Overlay Dim T_enetpacketsrc4 As Byte At Packet + &H0A Overlay Dim T_enetpacketsrc5 As Byte At Packet + &H0B Overlay 'Ethernet packet type Dim T_enetpackettype As Word At Packet + &H0C Overlay Dim T_arp_hwtype1 As Byte At Packet + &H0F Overlay 'Arp Dim T_arp_prttype1 As Byte At Packet + &H11 Overlay Dim T_arp_hwlen As Byte At Packet + &H12 Overlay Dim T_arp_prlen As Byte At Packet + &H13 Overlay Dim T_arp_op1 As Byte At Packet + &H15 Overlay 'arp source ip address Dim T_arp_sipaddr0 As Byte At Packet + &H1C Overlay Dim T_arp_sipaddr1 As Byte At Packet + &H1D Overlay Dim T_arp_sipaddr2 As Byte At Packet + &H1E Overlay Dim T_arp_sipaddr3 As Byte At Packet + &H1F Overlay 'arp target IP address Dim T_arp_tipaddr As Long At Packet + &H26 Overlay 'IP header layout IP version and header length Dim T_ip_vers_len As Byte At Packet + &H0E Overlay Dim T_arp_hwtype0 As Byte At Packet + &H0E Overlay 'Arp Dim T_arp_prttype0 As Byte At Packet + &H10 Overlay Dim T_arp_op0 As Byte At Packet + &H14 Overlay 'arp source mac address Dim T_arp_src_enetpacket0 As Byte At Packet + &H16 Overlay Dim T_arp_src_enetpacket1 As Byte At Packet + &H17 Overlay Dim T_arp_src_enetpacket2 As Byte At Packet + &H18 Overlay Dim T_arp_src_enetpacket3 As Byte At Packet + &H19 Overlay Dim T_arp_src_enetpacket4 As Byte At Packet + &H1A Overlay Dim T_arp_src_enetpacket5 As Byte At Packet + &H1B Overlay 'arp source mac address Dim T_arp_dest_enetpacket0 As Byte At Packet + &H20 Overlay Dim T_arp_dest_enetpacket1 As Byte At Packet + &H21 Overlay Dim T_arp_dest_enetpacket2 As Byte At Packet + &H22 Overlay Dim T_arp_dest_enetpacket3 As Byte At Packet + &H23 Overlay Dim T_arp_dest_enetpacket4 As Byte At Packet + &H24 Overlay Dim T_arp_dest_enetpacket5 As Byte At Packet + &H25 Overlay Dim T_tos As Byte At Packet + &H0F Overlay 'packet length Dim T_ip_pktlen0 As Byte At Packet + &H10 Overlay Dim T_ip_pktlen1 As Byte At Packet + &H11 Overlay Dim T_id0 As Byte At Packet + &H12 Overlay Dim T_id1 As Byte At Packet + &H13 Overlay Dim T_flags As Byte At Packet + &H14 Overlay Dim T_offset As Byte At Packet + &H15 Overlay Dim T_ttl As Byte At Packet + &H16 Overlay 'protocol (ICMP=1, TCP=6, UDP=11) Dim T_ip_proto As Byte At Packet + &H17 Overlay 'header checksum Dim T_ip_hdr_cksum0 As Byte At Packet + &H18 Overlay Dim T_ip_hdr_cksum1 As Byte At Packet + &H19 Overlay Dim T_ip_hdr_cksum As Word At Packet + &H18 Overlay 'IP address of source Dim T_ip_srcaddr0 As Byte At Packet + &H1A Overlay Dim T_ip_srcaddr1 As Byte At Packet + &H1B Overlay Dim T_ip_srcaddr2 As Byte At Packet + &H1C Overlay Dim T_ip_srcaddr3 As Byte At Packet + &H1D Overlay Dim T_ip_srcaddr As Long At Packet + &H1A Overlay 'IP address of destination Dim T_ip_destaddr0 As Byte At Packet + &H1E Overlay Dim T_ip_destaddr1 As Byte At Packet + &H1F Overlay Dim T_ip_destaddr2 As Byte At Packet + &H20 Overlay Dim T_ip_destaddr3 As Byte At Packet + &H21 Overlay Dim T_ip_destaddr As Long At Packet + &H1E Overlay Dim T_icmp_type As Byte At Packet + &H22 Overlay Dim T_icmp_code As Byte At Packet + &H23 Overlay Dim T_icmp_cksum0 As Byte At Packet + &H24 Overlay Dim T_icmp_cksum1 As Byte At Packet + &H25 Overlay Dim T_icmp_cksum As Word At Packet + &H24 Overlay Dim Tcp_srcporth As Byte At Packet + &H22 Overlay Dim Tcp_srcportl As Byte At Packet + &H23 Overlay Dim Tcp_destporth As Byte At Packet + &H24 Overlay Dim Tcp_destportl As Byte At Packet + &H25 Overlay Dim Tcp_seqnum3 As Byte At Packet + &H26 Overlay Dim Tcp_seqnum2 As Byte At Packet + &H27 Overlay Dim Tcp_seqnum1 As Byte At Packet + &H28 Overlay Dim Tcp_seqnum0 As Byte At Packet + &H29 Overlay Dim Tcp_acknum3 As Byte At Packet + &H2A Overlay Dim Tcp_acknum2 As Byte At Packet + &H2B Overlay Dim Tcp_acknum1 As Byte At Packet + &H2C Overlay Dim Tcp_acknum0 As Byte At Packet + &H2D Overlay Dim Tcp_hdr As Byte At Packet + &H2E Overlay Dim Tcp_flags As Byte At Packet + &H2F Overlay Dim Tcp_cksumh As Byte At Packet + &H32 Overlay Dim Tcp_cksuml As Byte At Packet + &H33 Overlay Dim Tcp_cksum As Word At Packet + &H32 Overlay 'UDP header Dim T_udp_srcport0 As Byte At Packet + &H22 Overlay Dim T_udp_srcport1 As Byte At Packet + &H23 Overlay Dim T_udp_srcport As Word At Packet + &H22 Overlay Dim T_udp_destport0 As Byte At Packet + &H24 Overlay Dim T_udp_destport1 As Byte At Packet + &H25 Overlay Dim T_udp_destport As Word At Packet + &H24 Overlay Dim T_udp_len0 As Byte At Packet + &H26 Overlay Dim T_udp_len1 As Byte At Packet + &H27 Overlay Dim T_udp_chksum0 As Byte At Packet + &H28 Overlay Dim T_udp_chksum1 As Byte At Packet + &H29 Overlay Dim T_udp_data As Byte At Packet + &H2A Overlay Dim T_udp_data1 As Byte At Packet + &H2B Overlay Dim T_udp_data2 As Byte At Packet + &H2C Overlay Dim T_udp_data3 As Byte At Packet + &H2D Overlay Dim T_udp_data4 As Byte At Packet + &H2E Overlay Dim T_udp_data5 As Byte At Packet + &H2F Overlay Dim T_udp_data6 As Byte At Packet + &H30 Overlay Dim T_udp_data7 As Byte At Packet + &H31 Overlay Dim T_udp_data8 As Byte At Packet + &H32 Overlay Dim T_udp_data9 As Byte At Packet + &H33 Overlay Dim T_udp_data10 As Byte At Packet + &H34 Overlay Dim T_udp_data11 As Byte At Packet + &H35 Overlay Dim T_udp_data12 As Byte At Packet + &H36 Overlay Dim T_udp_data13 As Byte At Packet + &H37 Overlay Dim T_udp_data14 As Byte At Packet + &H38 Overlay Dim T_udp_data15 As Byte At Packet + &H39 Overlay Dim T_udp_data16 As Byte At Packet + &H3A Overlay Dim T_udp_data17 As Byte At Packet + &H3B Overlay Dim T_udp_data18 As Byte At Packet + &H3C Overlay Dim T_udp_data19 As Byte At Packet + &H3D Overlay Dim T_udp_data20 As Byte At Packet + &H3E Overlay Dim T_udp_data21 As Byte At Packet + &H3F Overlay Dim T_udp_data22 As Byte At Packet + &H40 Overlay Dim T_udp_data23 As Byte At Packet + &H41 Overlay Dim T_udp_data24 As Byte At Packet + &H42 Overlay Dim T_udp_data25 As Byte At Packet + &H43 Overlay Dim T_udp_data26 As Byte At Packet + &H44 Overlay Dim T_udp_data27 As Byte At Packet + &H45 Overlay Dim T_udp_data28 As Byte At Packet + &H46 Overlay Dim T_udp_data29 As Byte At Packet + &H47 Overlay Dim T_udp_data30 As Byte At Packet + &H48 Overlay Dim T_udp_data31 As Byte At Packet + &H49 Overlay Dim T_udp_data32 As Byte At Packet + &H4A Overlay Dim Client_seqnum As Long Dim Client_seqnum0 As Byte At Client_seqnum Overlay Dim Client_seqnum1 As Byte At Client_seqnum + 1 Overlay Dim Client_seqnum2 As Byte At Client_seqnum + 2 Overlay Dim Client_seqnum3 As Byte At Client_seqnum + 3 Overlay Dim Incoming_ack As Long Dim Incoming_ack0 As Byte At Incoming_ack Overlay Dim Incoming_ack1 As Byte At Incoming_ack + 1 Overlay Dim Incoming_ack2 As Byte At Incoming_ack + 2 Overlay Dim Incoming_ack3 As Byte At Incoming_ack + 3 Overlay Dim My_seqnum As Long Dim My_seqnum0 As Byte At My_seqnum Overlay Dim My_seqnum1 As Byte At My_seqnum + 1 Overlay Dim My_seqnum2 As Byte At My_seqnum + 2 Overlay Dim My_seqnum3 As Byte At My_seqnum + 3 Overlay ' Dim Tempword As Word Dim Tempwordh As Byte At Tempword + 1 Overlay Dim Tempwordl As Byte At Tempword Overlay 'Main $lib "datetime.lbx" Dim W1 As Word W1 = W1 / W1 ' force compiler to implement _div16 Readeeprom Hulp1 , 0 ' Should I put something on the LCD? If Hulp1.0 = 0 Then Dsp_present = 0 Cursor Off Cls Lcd "www.achatz.nl WebCat" Wait 2 Thirdline Lcd "Press a key for" Fourthline Lcd "configuration" Else Dsp_present = 1 End If Call Configure Call Default_message Call Init_rtl8019as Enable Interrupts Enable Int5 On Int5 Rtl8019as_interrupt 'start the NIC Bhulp0 = Nic_cr_rd2 Or Nic_cr_sta Call Write_rtl8019as(nic_cr , Bhulp0) Idletime = 0 Call Arp_request 'for the UDP-send routine MAC-address of Gateway Print "Gateway MAC-address: "; Print Hex(gwmac(1)) ; "-" ; Hex(gwmac(2)) ; "-" ; Hex(gwmac(3)) ; "-" ; Hex(gwmac(4)) ; "-" ; Hex(gwmac(5)) ; "-" ; Hex(gwmac(6)) Print Print "Running..." T_udp_data = Asc( "X") T_udp_data1 = &H0A ' lf T_udp_data2 = &H0D ' cr 'padding T_udp_data3 = &H00 T_udp_data4 = &H00 T_udp_data5 = &H00 T_udp_data6 = &H00 T_udp_data7 = &H00 T_udp_data8 = &H00 T_udp_data9 = &H00 T_udp_data10 = &H00 T_udp_data11 = &H00 T_udp_data12 = &H00 T_udp_data13 = &H00 T_udp_data14 = &H00 T_udp_data15 = &H00 T_udp_data16 = &H00 T_udp_data17 = &H00 T_udp_data18 = &H00 T_udp_data19 = &H00 T_udp_data20 = &H00 T_udp_data21 = &H00 T_udp_data22 = &H00 Call Udp_send Wait 2 Do If Dsp_present = 0 Then 'idle-time Incr Idletime If Idletime > 900000 Then Disable Int5 Cursor Off Cls Lcd "www.achatz.nl WebCat" Thirdline Lcd "Idle" Enable Int5 Idletime = 0 End If End If Loop End ' Routine to handle an interrupt on the RTL8019AS ' Rtl8019as_interrupt: #if Debug = 1 Print "Interrupt from RTL8019as" #endif Disable Int5 'read the interrupt status register Call Read_rtl8019as(nic_isr) 'if the receive buffer has been overrun 'page 0 - ISR OVW (OVR - receive buffer has been exhausted) If Byte_read.4 = 1 Then Call Overrun End If 'if the receive buffer holds a good packet 'page 0 - ISR PRX (PRX - received the packet with no errors) If Byte_read.0 = 1 Then Call Getpacket End If 'make sure the receive buffer ring is empty. If BNRY = CURR, the buffer is empty Call Read_rtl8019as(nic_bnry) Data_l = Byte_read Bhulp0 = Nic_cr_ps0 Or Nic_cr_rd2 Bhulp0 = Bhulp0 Or Nic_cr_sta 'switch to page 1 Call Write_rtl8019as(nic_cr , Bhulp0) Call Read_rtl8019as(nic_curr) Data_h = Byte_read Bhulp0 = Nic_cr_rd2 Or Nic_cr_sta Call Write_rtl8019as(nic_cr , Bhulp0) 'buffer is not empty, get next packet If Data_l <> Data_h Then Call Getpacket End If 'reset the interrupts bits Call Write_rtl8019as(nic_isr , &HFF) Bhulp0 = Nic_cr_rd2 Or Nic_cr_sta Call Write_rtl8019as(nic_cr , Bhulp0) Enable Int5 Return ' Routine to Write to NIC Control register ' Sub Write_rtl8019as(regaddr , Regdata) Whulp0 = Regaddr + &H8300 Out Whulp0 , Regdata End Sub ' Routine to read from NIC Control register ' Sub Read_rtl8019as(regaddr) Whulp0 = Regaddr + &H8300 Byte_read = Inp(whulp0) End Sub ' Routine to initialise the RTL8019AS ethernetchip ' Sub Init_rtl8019as #if Debug = 1 Print "Sub Init_RTL8019as" #endif Call Read_rtl8019as(nic_rstport) Call Write_rtl8019as(nic_rstport , Byte_read) Waitms 10 'check for good soft reset Call Write_rtl8019as(nic_imr , 0) 'interrupt mask register Call Write_rtl8019as(nic_isr , &HFF) Call Read_rtl8019as(nic_isr) 'page 0 RST If Byte_read.7 = 0 Then Print Msg_initfail End If 'switch to page 3 Bhulp0 = Nic_cr_rd2 Or Nic_cr_stp Bhulp0 = Bhulp0 Or Nic_cr_ps0 Bhulp0 = Bhulp0 Or Nic_cr_ps1 Call Write_rtl8019as(nic_cr , Bhulp0) Bhulp0 = Nic_9346cr_eem0 Or Nic_9346cr_eem1 Call Write_rtl8019as(nic_9346cr , Bhulp0) Call Write_rtl8019as(nic_config3 , 0) Call Write_rtl8019as(nic_config2 , Nic_config2_bselb) Call Write_rtl8019as(nic_9346cr , 0) Waitms 255 Bhulp0 = Nic_cr_rd2 Or Nic_cr_stp Call Write_rtl8019as(nic_cr , Bhulp0) Waitms 2 Call Write_rtl8019as(nic_dcr , Dcrval) '58 Call Write_rtl8019as(nic_rbcr0 , &H00) Call Write_rtl8019as(nic_rbcr1 , &H00) '0000_0100 packets with broadcast destination address are accepted Call Write_rtl8019as(nic_rcr , &H04) 'receive configuration register Call Write_rtl8019as(nic_tpsr , Txstart) '&H40 '0000_0010 internal loopback Call Write_rtl8019as(nic_tcr , &H02) 'transmit configuration register Call Write_rtl8019as(nic_pstart , Rxstart) '&H46 Call Write_rtl8019as(nic_bnry , Rxstart) '&H46 Call Write_rtl8019as(nic_pstop , Rxstop) Call Write_rtl8019as(nic_isr , &HFF) Call Write_rtl8019as(nic_cr , &H61) Waitms 2 Call Write_rtl8019as(nic_curr , Rxstart) '&H46 For Hulp1 = 1 To 6 Call Write_rtl8019as(hulp1 , Mymac(hulp1)) Next Hulp1 Call Write_rtl8019as(nic_cr , &H21) Call Write_rtl8019as(nic_dcr , Dcrval) '58 Call Write_rtl8019as(nic_cr , &H22) Call Write_rtl8019as(nic_isr , &HFF) Call Write_rtl8019as(nic_imr , Imrval) 'interrupt mask register / 0001_0001 Call Write_rtl8019as(nic_tcr , Tcrval) 'transmit configuration register / 0000_0000 Bhulp0 = Nic_cr_sta Or Nic_cr_rd2 Call Write_rtl8019as(nic_cr , Bhulp0) End Sub ' Routine to handle an overrun ' Sub Overrun #if Debug = 1 Print "Sub Overrun" #endif Call Read_rtl8019as(nic_cr) Data_l = Byte_read Call Write_rtl8019as(nic_cr , &H21) Waitms 2 Call Write_rtl8019as(nic_rbcr0 , &H00) Call Write_rtl8019as(nic_rbcr1 , &H00) Hulp1 = Data_l And &H04 If Hulp1 <> 0 Then Resend = 0 Else If Hulp1 = 0 Then Call Read_rtl8019as(nic_isr) Data_l = Byte_read Hulp1 = Data_l And &H02 Hulp4 = Data_l And &H08 Hulp3 = Hulp1 Or Hulp4 If Hulp3 > 0 Then Resend = 0 Else Resend = 1 End If End If End If Call Write_rtl8019as(nic_tcr , &H02) 'transmit configuration register Bhulp0 = Nic_cr_rd2 Or Nic_cr_sta Call Write_rtl8019as(nic_cr , Bhulp0) Call Write_rtl8019as(nic_bnry , Rxstart) '&H46 Bhulp0 = Nic_cr_ps0 Or Nic_cr_rd2 Bhulp0 = Bhulp0 Or Nic_cr_sta Call Write_rtl8019as(nic_cr , Bhulp0) Call Write_rtl8019as(nic_curr , Rxstart) '&H46 Bhulp0 = Nic_cr_rd2 Or Nic_cr_sta Call Write_rtl8019as(nic_cr , Bhulp0) '0001_0000 OVW Call Write_rtl8019as(nic_isr , &H10) Call Write_rtl8019as(nic_tcr , Tcrval) 'transmit configuration register / 0000_0000 End Sub ' Routine to execute send packet command to retrieve the packet ' Sub Getpacket #if Debug = 1 Print "Sub Getpacket" #endif Call Write_rtl8019as(nic_cr , &H1A) For I = 0 To 4 Call Read_rtl8019as(nic_rdmaport) Pageheader(i + 1) = Byte_read Next I 'watch it. overlay variables Result16h = T_enetpacketlenh Result16l = T_enetpacketlenl Rxlen = Result16 Hulp2 = Rxlen + 1 For I = 1 To Hulp2 Call Read_rtl8019as(nic_rdmaport) 'dump any bytes that will overrun the receive buffer If I < 1500 Then Packet(i + 1) = Byte_read End If Next I Hulp1 = Byte_read And Nic_rdc If Hulp1 <> 64 Then Call Read_rtl8019as(nic_isr) End If Call Write_rtl8019as(nic_isr , &HFF) ' Routine to process an ARP reply/request ' If T_enetpackettype = &H0608 Then If T_arp_hwtype1 = &H01 Then If T_arp_prttype1 = &H00 Then If T_arp_hwlen = &H06 Then If T_arp_prlen = &H04 Then If My_ip = T_arp_tipaddr Then If T_arp_op1 = &H02 Then Call Arp_reply End If If T_arp_op1 = &H01 Then Call Arp End If End If End If End If End If End If End If ' Routine to go ahead with icmp or udp ' If T_enetpackettype = &H0008 Then If T_ip_destaddr = My_ip Then Select Case T_ip_proto Case Prot_icmp : Call Icmp Case Prot_tcp : Call Tcp Case Prot_udp : Call Udp_receive End Select End If End If End Sub ' Routine to handle ARP-traffic ' Sub Arp #if Debug = 1 Print "Sub Arp" #endif 'Start the NIC Call Write_rtl8019as(nic_cr , &H22) 'load beginning page for transmit buffer Call Write_rtl8019as(nic_tpsr , Txstart) '&H40 'set start address for remote DMA operation Call Write_rtl8019as(nic_rsar0 , &H00) Call Write_rtl8019as(nic_rsar1 , &H40) 'clear the interrupts Call Write_rtl8019as(nic_isr , &HFF) 'load data byte count for remote DMA Call Write_rtl8019as(nic_rbcr0 , &H3C) '60 dec Call Write_rtl8019as(nic_rbcr1 , &H00) 'do remote write operation Call Write_rtl8019as(nic_cr , &H12) 'write destination MAC address Call Write_dest_mac 'write source address For I = 1 To 6 Call Write_rtl8019as(nic_rdmaport , Mymac(i)) Next I 'arp target IP address 'arp_op1 = packet(&h16) Packet(&H16) = &H02 For I = 1 To 10 Hulp1 = &H0C + I T = Packet(hulp1) Call Write_rtl8019as(nic_rdmaport , T) Next I 'write ethernet module mac address For I = 1 To 6 Call Write_rtl8019as(nic_rdmaport , Mymac(i)) Next I 'write myip For I = 1 To 4 Call Write_rtl8019as(nic_rdmaport , Myip(i)) Next I 'write remote mac address Call Write_dest_mac 'write remote IP address Call Write_rtl8019as(nic_rdmaport , T_arp_sipaddr0) Call Write_rtl8019as(nic_rdmaport , T_arp_sipaddr1) Call Write_rtl8019as(nic_rdmaport , T_arp_sipaddr2) Call Write_rtl8019as(nic_rdmaport , T_arp_sipaddr3) 'write som pad characters to fill out the packet to the minimum length For I = 0 To &H11 Call Write_rtl8019as(nic_rdmaport , &H00) Next I 'make sure the DMA operation has succesfully completed Byte_read = 0 Do Hulp1 = Byte_read And Nic_rdc Call Read_rtl8019as(nic_isr) Loop Until Hulp1 = 0 'load numbers of bytes to be transmitted Call Write_rtl8019as(nic_tbcr0 , &H3C) Call Write_rtl8019as(nic_tbcr1 , &H00) 'send the contents of the transmit buffer onto the network Call Write_rtl8019as(nic_cr , &H24) End Sub ' helper-routine ' Sub Write_dest_mac #if Debug = 1 Print "Sub Write_dest_mac" #endif Call Write_rtl8019as(nic_rdmaport , T_enetpacketsrc0) Call Write_rtl8019as(nic_rdmaport , T_enetpacketsrc1) Call Write_rtl8019as(nic_rdmaport , T_enetpacketsrc2) Call Write_rtl8019as(nic_rdmaport , T_enetpacketsrc3) Call Write_rtl8019as(nic_rdmaport , T_enetpacketsrc4) Call Write_rtl8019as(nic_rdmaport , T_enetpacketsrc5) End Sub ' PING-routine ' Sub Icmp #if Debug = 1 Print "Sub Icmp" #endif If Dsp_present = 0 Then Cls Lcd "PING" Idletime = 0 End If 'set echo reply T_icmp_type = &H00 T_icmp_code = &H00 'setup the IP-header Call Setipaddrs Call Icmp_checksum Call Echopacket End Sub ' Routine to handle the source/destination address ' Sub Setipaddrs #if Debug = 1 Print "Sub Setipaddrs" #endif T_ip_destaddr = T_ip_srcaddr 'make ethernet module IP address source address T_ip_srcaddr = My_ip Call Packetshape Call Ip_header_checksum End Sub ' Routine to echo a complete packet ' Sub Echopacket #if Debug = 1 Print "Sub Echopacket" #endif Call Write_rtl8019as(nic_cr , &H22) Call Write_rtl8019as(nic_tpsr , Txstart) '&H40 Call Write_rtl8019as(nic_rsar0 , &H00) Call Write_rtl8019as(nic_rsar1 , &H40) Call Write_rtl8019as(nic_isr , &HFF) Hulp1 = T_enetpacketlenl - 4 Call Write_rtl8019as(nic_rbcr0 , Hulp1) Call Write_rtl8019as(nic_rbcr1 , T_enetpacketlenh) Call Write_rtl8019as(nic_cr , &H12) Result16h = T_enetpacketlenh Result16l = T_enetpacketlenl Result16 = Result16 - 4 Txlen = Result16 'write the complete packet to the RTL8019AS from packet(1) to packet(txlen+1) Hulp2 = Txlen + 1 For I = 1 To Hulp2 Call Write_rtl8019as(nic_rdmaport , Packet(i)) Next I Byte_read = 0 While Hulp1 <> 0 Hulp1 = Byte_read And Nic_rdc Call Read_rtl8019as(nic_isr) Wend Hulp1 = T_enetpacketlenl - 4 Call Write_rtl8019as(nic_tbcr0 , Hulp1) Call Write_rtl8019as(nic_tbcr1 , T_enetpacketlenh) Call Write_rtl8019as(nic_cr , &H24) End Sub ' Routine to calculate a ICMP-checksum ' Sub Icmp_checksum #if Debug = 1 Print "Sub Icmp_checksum" #endif 'clear the ICMP checksum T_icmp_cksum = &H00 'calculate the ICMP checksum I_header_length = T_ip_pktlen1 - 20 'I_header_length = I_header_length - 20 I_odd = I_header_length Mod 2 '14 for MAC-part '20 for IP-header 'start on 35 'ip_pktlen = 00 3c (60) 'icmp-packetlengte = ip_pktlen - ip_header I_chksum32 = 0 'Total packetlength - ip_header - 1 Hulp6h = T_ip_pktlen0 Hulp6l = T_ip_pktlen1 Val1 = 35 Val2 = Hulp6 + 13 Call General_part_checksum(val1 , Val2) T_icmp_cksum0 = Val3 T_icmp_cksum1 = Val4 End Sub ' helper-routine ' Sub Packetshape #if Debug = 1 Print "Sub Packetshape" #endif 'move hardware source address to destination address T_enetpacketdest0 = T_enetpacketsrc0 T_enetpacketdest1 = T_enetpacketsrc1 T_enetpacketdest2 = T_enetpacketsrc2 T_enetpacketdest3 = T_enetpacketsrc3 T_enetpacketdest4 = T_enetpacketsrc4 T_enetpacketdest5 = T_enetpacketsrc5 ' 'Make ethernet module mac address the source address T_enetpacketsrc0 = Mymac(1) T_enetpacketsrc1 = Mymac(2) T_enetpacketsrc2 = Mymac(3) T_enetpacketsrc3 = Mymac(4) T_enetpacketsrc4 = Mymac(5) T_enetpacketsrc5 = Mymac(6) End Sub ' Routine to calculate a IP-header checksum ' Sub Ip_header_checksum #if Debug = 1 Print "Sub IP_header_checksum" #endif Local Ip_x As Byte Local Ip_hulp1 As Byte Local Ip_chksum32 As Long Local Ip_checksum16 As Word Local Ip_temp16 As Word Local Ip_header_length As Byte 'calculate the IP header checksum T_ip_hdr_cksum = &H00 ' Hdr_chksum = 0 I_chksum32 = 0 Ip_header_length = T_ip_vers_len And &H0F Ip_header_length = 4 * Ip_header_length I_chksum32 = 0 I_odd = 0 Val1 = 15 Val2 = &H0E + Ip_header_length Call General_part_checksum(val1 , Val2) T_ip_hdr_cksum0 = Val3 T_ip_hdr_cksum1 = Val4 End Sub ' Overall routine for checksum ' Sub General_part_checksum(byval Val1 As Byte , Byval Val2 As Word) #if Debug = 1 Print "Sub General_part_checksum" #endif For I_x = Val1 To Val2 Step 2 I_value16h = Packet(i_x) Hulp3 = I_x + 1 I_value16l = Packet(hulp3) I_chksum32 = I_chksum32 + I_value16 Next I_x If I_odd = 1 Then Incr Val2 I_value16h = Packet(val2) I_value16l = 0 I_chksum32 = I_chksum32 + I_value16 End If I_checksum16 = Highw(i_chksum32) I_checksum16 = I_checksum16 + I_chksum32 ' only 16 lower bits of i_chksum32 is taken... I_checksum16 = Not I_checksum16 Val3 = High(i_checksum16) Val4 = Low(i_checksum16) End Sub ' Routine to handle UDP-traffic ' Sub Udp_receive #if Debug = 1 Print "Sub Udp_receive" #endif Local Udp_port As Word Local Udp_porth As Byte Local Udp_portl As Byte Readeeprom Udp_porth , 11 Readeeprom Udp_portl , 12 Udp_port = Udp_porth Shift Udp_port , Left , 8 Udp_port = Udp_port + Udp_portl 'From within a VB-program If T_udp_destport = Udp_port Then If T_udp_srcport = &H2500 Then ' &h0025 NTP protocol Call Ntp Exit Sub End If Cursor On Select Case T_udp_data Case &H00 : Cls Case &H01 : Home Case &H02 : Lowerline Case &H03 : Thirdline Case &H04 : Fourthline Case Else : Lcd Chr(t_udp_data) End Select Idletime = 0 Call Write_rtl8019as(nic_cr , &H22) Exit Sub End If 'echo on PORT 7 If T_udp_destport = &H0700 Then If Dsp_present = 0 Then Cls Lcd "UDP request" Idletime = 0 End If 'Build The Ip Header Call Setipaddrs 'swap the UDP source and destinations port Swap T_udp_srcport0 , T_udp_destport0 Swap T_udp_srcport1 , T_udp_destport1 Call Udp_checksum Call Write_rtl8019as(nic_cr , &H22) Call Echopacket End If End Sub ' Routine to calculate the Udp-checksum ' Sub Udp_checksum #if Debug = 1 Print "Sub Udp_checksum" #endif T_udp_chksum0 = &H00 T_udp_chksum1 = &H00 'checksum TCP header I_chksum32 = 0 I_value16h = T_ip_srcaddr0 I_value16l = T_ip_srcaddr1 I_chksum32 = I_chksum32 + I_value16 I_value16h = T_ip_srcaddr2 I_value16l = T_ip_srcaddr3 I_chksum32 = I_chksum32 + I_value16 I_value16h = T_ip_destaddr0 I_value16l = T_ip_destaddr1 I_chksum32 = I_chksum32 + I_value16 I_value16h = T_ip_destaddr2 I_value16l = T_ip_destaddr3 I_chksum32 = I_chksum32 + I_value16 'proto I_chksum32 = I_chksum32 + T_ip_proto 'packet length I_value16h = T_udp_len0 I_value16l = T_udp_len1 I_chksum32 = I_chksum32 + I_value16 I_odd = T_udp_len1 Mod 2 Result16h = T_udp_len0 Result16l = T_udp_len1 'udp_srcport0 = packet(&h23) Val1 = &H23 Val2 = &H23 + Result16 Val2 = Val2 - 2 Call General_part_checksum(val1 , Val2) T_udp_chksum0 = Val3 T_udp_chksum1 = Val4 End Sub ' TCP ' Sub Tcp #if Debug = 1 Print "Sub Tcp" #endif Local Work As Byte Work = Tcp_flags Tcp_fin = Work.0 Tcp_syn = Work.1 Tcp_rst = Work.2 Tcp_psh = Work.3 Tcp_ack = Work.4 Tcp_urg = Work.5 If Tcp_destporth = 0 Then Select Case Tcp_destportl Case 80 : Call Http End Select End If End Sub Sub Http #if Debug = 1 Print "HTTP" Print Print "Destination port low " ; Tcp_destportl Print "Destination port high " ; Tcp_destporth Print Print "TCPdatalen_in " ; Tcpdatalen_in Print Print "Flags:" Print "FIN " ; Tcp_fin Print "SYN " ; Tcp_syn Print "RST " ; Tcp_rst Print "PSH " ; Tcp_psh Print "ACK " ; Tcp_ack Print "URG " ; Tcp_urg Print Print "Incoming:" Print "Tcp_seqnum3 " ; Hex(tcp_seqnum3) Print "Tcp_seqnum2 " ; Hex(tcp_seqnum2) Print "Tcp_seqnum1 " ; Hex(tcp_seqnum1) Print "Tcp_seqnum0 " ; Hex(tcp_seqnum0) Print Print "Tcp_acknum3 " ; Hex(tcp_acknum3) Print "Tcp_acknum2 " ; Hex(tcp_acknum2) Print "Tcp_acknum1 " ; Hex(tcp_acknum1) Print "Tcp_acknum0 " ; Hex(tcp_acknum0) #endif Local Msg_temp2 As String * 10 Local Tempword3 As Word 'Local Z As Word 'Local Ztemp As Word 'Local Tempstring2 As String * 5 Local Templong1 As Long Result16h = T_ip_pktlen0 Result16l = T_ip_pktlen1 ' ' calculate IP header length ' MSN is version (IPv4) ' LSN is length Hulp1 = T_ip_vers_len And &H0F Hulp1 = Hulp1 * 4 ' calculate TCP header length ' MSN is length / 4 Hulp2 = Tcp_hdr And &HF0 Shift Hulp2 , Right , 4 Hulp2 = Hulp2 * 4 Tcpdatalen_in = Result16 - Hulp1 Tcpdatalen_in = Tcpdatalen_in - Hulp2 ' If an ACK is received and the destination port address is valid ' and no data is in the packet If Tcp_ack = 1 Then If Tcpdatalen_in = 0 Then Incoming_ack0 = Tcp_acknum0 Incoming_ack1 = Tcp_acknum1 Incoming_ack2 = Tcp_acknum2 Incoming_ack3 = Tcp_acknum3 If Flags.synflag = 1 Then Reset Flags.synflag My_seqnum = Incoming_ack Tempword3 = &H37 If Dsp_present = 0 Then Cls Lcd "HTTP request" Idletime = 0 End If Restore Htmlcode Do Read Msg_temp Msg_temp2 = Right(msg_temp , 8) If Msg_temp2 = "endblock" Then Exit Do End If If Msg_temp2 = "get-ipnr" Then Readeeprom Hulp1 , 23 If Hulp1 = &HFF Then Msg_temp = "http://" + Str(myip(1)) Msg_temp = Msg_temp + "." + Str(myip(2)) Msg_temp = Msg_temp + "." + Str(myip(3)) Msg_temp = Msg_temp + "." + Str(myip(4)) Else Msg_temp = "" For Y = 1 To Hulp1 Hulp3 = Y + 23 Readeeprom Hulp2 , Hulp3 Msg_temp = Msg_temp + Chr(hulp2) Next Y End If End If If Msg_temp2 = "get-info" Then 'no relays' yet ' Else For Y = 1 To Len(msg_temp) Tempstring1 = Mid(msg_temp , Y , 1) Packet(tempword3) = Asc(tempstring1) Incr Tempword3 Next Y End If Loop Tcpdatalen_out = Tempword3 - 55 'minus headerlength ' expect to get an acknowledgment of the message Expected_ack = My_seqnum + Tcpdatalen_out ' send the TCP/IP packet Call Send_tcp_packet End If End If End If ' This code segment processes the incoming SYN from the Tenet client ' and sends back the initial sequence number (ISN) and acknowledges ' the incoming SYN packet If Tcp_syn = 1 Then #if Debug = 1 Print "Tcp_syn = 1" #endif Tcpdatalen_in = 1 Set Flags.synflag Call Setup_packet Swap Tcp_srcportl , Tcp_destportl Swap Tcp_srcporth , Tcp_destporth Call Assemble_ack Call Tcpseqtomyseq Tcp_flags = 0 Set Tcp_flags.1 Set Tcp_flags.4 Call Tcp_checksum Call Echopacket End If If Tcp_fin = 1 Then Set Flags.finflag Incr Tcpdatalen_in Incoming_ack0 = Tcp_acknum0 Incoming_ack1 = Tcp_acknum1 Incoming_ack2 = Tcp_acknum2 Incoming_ack3 = Tcp_acknum3 'Call Incomacqtotcpack If Incoming_ack <= Expected_ack Then Templong1 = Expected_ack - Incoming_ack My_seqnum = Expected_ack - Templong1 End If Expected_ack = My_seqnum + Tcpdatalen_out Call Send_tcp_packet End If End Sub Sub Tcp_checksum #if Debug = 1 Print "Sub Tcp_checksum" #endif Local Whulp1 As Word Local Whulp2 As Word Local Whulp3 As Word Local Tempword2 As Word Tcp_cksum = 0 I_chksum32 = 0 Tempwordh = T_ip_srcaddr0 Tempwordl = T_ip_srcaddr1 I_chksum32 = Tempword Tempwordh = T_ip_srcaddr2 Tempwordl = T_ip_srcaddr3 I_chksum32 = I_chksum32 + Tempword Tempwordh = T_ip_destaddr0 Tempwordl = T_ip_destaddr1 I_chksum32 = I_chksum32 + Tempword Tempwordh = T_ip_destaddr2 Tempwordl = T_ip_destaddr3 I_chksum32 = I_chksum32 + Tempword I_chksum32 = I_chksum32 + T_ip_proto Tempwordh = T_ip_pktlen0 Tempwordl = T_ip_pktlen1 Tempword2 = T_ip_vers_len And &H0F Tempword2 = Tempword2 * 4 I_chksum32 = I_chksum32 + Tempword I_chksum32 = I_chksum32 - Tempword2 Whulp2 = Tempword - 20 Whulp2 = Whulp2 + &H23 Whulp2 = Whulp2 - 2 Val1 = &H23 Val2 = Whulp2 I_odd = Val2 - Val1 I_odd = I_odd Mod 2 Call General_part_checksum(val1 , Val2) Tcp_cksumh = Val3 Tcp_cksuml = Val4 End Sub ' Send the TCP-packet, a bit different then echopacket ' Sub Send_tcp_packet #if Debug = 1 Print "Sub Send_tcp_packet" #endif Ip_packet_len = 40 + Tcpdatalen_out T_ip_pktlen1 = Low(ip_packet_len) T_ip_pktlen0 = High(ip_packet_len) Call Setup_packet Swap Tcp_srcporth , Tcp_destporth Swap Tcp_srcportl , Tcp_destportl Call Assemble_ack Call Tcpseqtomyseq Tcp_flags = 0 'fin_out Set Tcp_flags.0 ' piggyback FIN onto the page date 'ack_out Set Tcp_flags.4 If Flags.finflag = 1 Then 'fin_out Set Tcp_flags.0 'clr_finflag Reset Flags.finflag End If Call Tcp_checksum Txlen = Ip_packet_len + 14 If Txlen < 60 Then Txlen = 60 End If Call Write_rtl8019as(nic_cr , &H22) Call Write_rtl8019as(nic_tpsr , Txstart) '&H40 Call Write_rtl8019as(nic_rsar0 , &H00) Call Write_rtl8019as(nic_rsar1 , &H40) Call Write_rtl8019as(nic_isr , &HFF) Hulp1 = Low(txlen) Call Write_rtl8019as(nic_rbcr0 , Hulp1) Hulp1 = High(txlen) Call Write_rtl8019as(nic_rbcr1 , Hulp1) Call Write_rtl8019as(nic_cr , &H12) For I = 1 To Txlen Call Write_rtl8019as(nic_rdmaport , Packet(i)) Next 'make sure the DMA operation has succesfully completed Byte_read = 0 Do Hulp1 = Byte_read And Nic_rdc Call Read_rtl8019as(nic_isr) Loop Until Hulp1 = 0 Hulp1 = Low(txlen) Call Write_rtl8019as(nic_tbcr0 , Hulp1) Hulp1 = High(txlen) Call Write_rtl8019as(nic_tbcr1 , Hulp1) Call Write_rtl8019as(nic_cr , &H24) End Sub Sub Setup_packet #if Debug = 1 Print "Sub Setup_packet" #endif 'Move IP source address to destination address T_ip_destaddr = T_ip_srcaddr 'Make ethernet module IP address source address T_ip_srcaddr = My_ip 'Move hardware source address to destinatin address Call Packetshape Call Ip_header_checksum End Sub Sub Assemble_ack Client_seqnum0 = Tcp_seqnum0 Client_seqnum1 = Tcp_seqnum1 Client_seqnum2 = Tcp_seqnum2 Client_seqnum3 = Tcp_seqnum3 Client_seqnum = Client_seqnum + Tcpdatalen_in Tcp_acknum0 = Client_seqnum0 Tcp_acknum1 = Client_seqnum1 Tcp_acknum2 = Client_seqnum2 Tcp_acknum3 = Client_seqnum3 End Sub Sub Tcpseqtomyseq #if Debug = 1 Print "Sub Tcpseqtomyseq" #endif Tcp_seqnum0 = My_seqnum0 Tcp_seqnum1 = My_seqnum1 Tcp_seqnum2 = My_seqnum2 Tcp_seqnum3 = My_seqnum3 End Sub ' Routine UDP_send In this example a UDP-send to a NTP-server and receiving the exact time ' Sub Udp_send #if Debug = 1 Print "Sub UDP_send" #endif Disable Int5 T_enetpacketlenl = 66 T_enetpacketlenh = 0 'MAC-header 'Destination hardware address T_enetpacketdest0 = Gwmac(1) T_enetpacketdest1 = Gwmac(2) T_enetpacketdest2 = Gwmac(3) T_enetpacketdest3 = Gwmac(4) T_enetpacketdest4 = Gwmac(5) T_enetpacketdest5 = Gwmac(6) ' source (own source) T_enetpacketsrc0 = Mymac(1) T_enetpacketsrc1 = Mymac(2) T_enetpacketsrc2 = Mymac(3) T_enetpacketsrc3 = Mymac(4) T_enetpacketsrc4 = Mymac(5) T_enetpacketsrc5 = Mymac(6) T_enetpackettype = &H0008 ' fill IP-header T_ip_vers_len = &H45 T_tos = &H00 T_ip_pktlen0 = &H00 T_ip_pktlen1 = &H30 T_id0 = &H4A T_id1 = &HA5 T_flags = &H00 T_offset = &H00 T_ttl = &H80 'protocol (ICMP=1, TCP=6, UDP=11) T_ip_proto = &H11 'header checksum 'T_ip_hdr_cksum0 'T_ip_hdr_cksum1 'IP address of source T_ip_srcaddr0 = Myip(1) T_ip_srcaddr1 = Myip(2) T_ip_srcaddr2 = Myip(3) T_ip_srcaddr3 = Myip(4) 'IP address of destination 'you have to put the IP-number of T_ip_destaddr0 = 193 ' the NTP-server here T_ip_destaddr1 = 67 '193.67.79.202 T_ip_destaddr2 = 79 T_ip_destaddr3 = 202 'UDP-header T_udp_srcport0 = &H13 T_udp_srcport1 = &H88 T_udp_destport0 = &H00 T_udp_destport1 = &H25 T_udp_len0 = &H00 T_udp_len1 = &H1C Call Ip_header_checksum Call Udp_checksum Call Echopacket Enable Int5 End Sub ' Routine to convert the LONG from the NTP-server in to date and time ' Sub Ntp #if Debug = 1 Print "Sub Ntp" #endif S(1) = T_udp_data S(2) = T_udp_data1 S(3) = T_udp_data2 S(4) = T_udp_data3 Swap S(1) , S(4) : Swap S(2) , S(3) L2 = L1 + 1139293696 L2 = L2 + 3600 ' offset UTC + 1 hour Print "Date : " ; Date(l2) Print "Time : " ; Time(l2) Cls Lcd "Date : " ; Date(l2) Lowerline Tempstring = Time(l2) Lcd "Time : " ; Left(tempstring , 5) End Sub ' Routine to get a configuration-screen on the RS232 port ' Sub Configure #if Debug = 1 Print "Sub Configure" #endif Local Adr As Word Local Row As Byte Local Ipnr As String * 15 Local Dot1 As Byte Local Dot2 As Byte Local Dot3 As Byte Local Dot4 As Byte Local Dot5 As Byte Local Iptemp As String * 1 Local Macadr As String * 20 Local Yn As String * 1 Print Chr(&H1b) ; "[2J"; ' ANSI goto 0/0 Print "WebCat Atmega128 Loader Version 1.30" Print Print "Source written by Ben Zijlstra january 2006" Print Print "Present configuration:" Print Print "Network:" Print Print "IP-number : "; Call Read_ip Call Print_ip Call Read_mac Print Print "MAC-address : "; Call Print_mac 'Print Hex(mymac(1)) ; "-" ; Hex(mymac(2)) ; "-" ; Hex(mymac(3)) ; "-" ; Hex(mymac(4)) ; "-" ; Hex(mymac(5)) ; "-" ; Hex(mymac(6)) Print Print "Gateway IP-number : "; Call Read_gwip Call Print_gwip Print Print "Press 'y' to get into configuration mode" Test: Incr Hulp3 If Hulp3 = 65000 Then Incr Hulp1 Hulp3 = 0 End If If Hulp1 = 50 Then Goto Noresponse End If sbis USR,7 rjmp test Configuremode: If Dsp_present = 0 Then Cls Lcd "Configure-mode" End If Print Chr(&H1b) ; "[2J"; ' ANSI goto 0/0 Print Print "Configure mode:" Print Print "Network:" Print Print "IP-number: "; Print Call Print_ip Input "Change IP-number (y/n) " , Yn If Yn = "y" Then Print Print "Input IP-numbers as decimals, seperated by a dot (like 192.168.1.106)" Print Input Ipnr Hulp1 = Split(ipnr , Ippart(1) , ".") Myip(1) = Val(ippart(1)) Myip(2) = Val(ippart(2)) Myip(3) = Val(ippart(3)) Myip(4) = Val(ippart(4)) Print Print "New IP-number: "; Call Print_ip Print Input "Accept IP-number (y/n) " , Yn If Yn = "y" Then Writeeeprom Myip(1) , 1 : Writeeeprom Myip(2) , 2 Writeeeprom Myip(3) , 3 : Writeeeprom Myip(4) , 4 End If Print Print "IP-number: "; Call Read_ip Call Print_ip End If Print Print "MAC-address: "; Print Hex(mymac(1)) ; "-" ; Hex(mymac(2)) ; "-" ; Hex(mymac(3)) ; "-" ; Hex(mymac(4)) ; "-" ; Hex(mymac(5)) ; "-" ; Hex(mymac(6)) Print Input "Change MAC-address (y/n) " , Yn If Yn = "y" Then Mac: Print Print "Input MAC-address in hexadecimals, seperated by a - (like 00-34-35-36-37-48)" Print Input Macadr If Len(macadr) <> 17 Then Goto Mac Hulp1 = Split(macadr , Mymacs(1) , "-") Mymac(1) = Hexval(mymacs(1)) Mymac(2) = Hexval(mymacs(2)) Mymac(3) = Hexval(mymacs(3)) Mymac(4) = Hexval(mymacs(4)) Mymac(5) = Hexval(mymacs(5)) Mymac(6) = Hexval(mymacs(6)) Print Print "New MAC-address: "; Call Print_mac Print Input "Accept MAC-address (y/n) " , Yn If Yn = "y" Then Writeeeprom Mymac(1) , 5 : Writeeeprom Mymac(2) , 6 Writeeeprom Mymac(3) , 7 : Writeeeprom Mymac(4) , 8 Writeeeprom Mymac(5) , 9 : Writeeeprom Mymac(6) , 10 End If Print Print "MAC-address: "; Call Read_mac Call Print_mac End If Print "Gateway: "; Call Print_gwip Print Input "Change Gateway-IP-number (y/n) " , Yn If Yn = "y" Then Print Print "Input Gateway-IP-number as decimals, seperated by a dot (like 192.168.1.254)" Print Input Ipnr Hulp1 = Split(ipnr , Ippart(1) , ".") Gwip(1) = Val(ippart(1)) Gwip(2) = Val(ippart(2)) Gwip(3) = Val(ippart(3)) Gwip(4) = Val(ippart(4)) Print Print "New Gateway-IP-number: "; Call Print_gwip Print Input "Accept Gateway-IP-number (y/n) " , Yn If Yn = "y" Then Writeeeprom Gwip(1) , 13 : Writeeeprom Gwip(2) , 14 Writeeeprom Gwip(3) , 15 : Writeeeprom Gwip(4) , 16 End If Print Print "Gateway-IP-number: "; Call Read_gwip Call Print_gwip End If Print Print Input "Would you like to use it's own IP-number as refresh-URL (y/n) " , Yn If Yn = "y" Then Hulp1 = 255 Writeeeprom Hulp1 , 23 Else Print Input "Would you like to have another refresh-URL (y/n) " , Yn If Yn = "y" Then Input "Type the complete refresh URL " , Msg_temp Print Print "Refresh URL " ; Msg_temp Print Input "Accept Refresh-URL (y/n) " , Yn If Yn = "y" Then Hulp2 = Len(msg_temp) Writeeeprom Hulp2 , 23 ' write length of Refresh URL For Y = 1 To Hulp2 Hulp1 = Y + 23 Tempstring1 = Mid(msg_temp , Y , 1) Hulp2 = Asc(tempstring1) Writeeeprom Hulp2 , Hulp1 Next Y End If End If End If Print Hulp5 = 5000 Writeeeprom Hulp5h , 12 ' &h1388 = 5000 dec = 19 high, 136 low. Writeeeprom Hulp5l , 11 Input "Would you like to change to UDP Portnumber. It's default is 5000 (y/n) " , Yn If Yn = "y" Then Print Input "Type the portnumber < 65536 as decimal " , Hulp5 Print Print "Portnumber UDP " ; Hulp5 Print Input "Accept Portnumber UDP (y/n) " , Yn If Yn = "y" Then Writeeeprom Hulp5h , 12 Writeeeprom Hulp5l , 11 End If End If Print Input "Would you like to use a LCD-display? " , Yn Readeeprom Hulp1 , 0 If Yn = "y" Then Dsp_present = 0 Hulp1.0 = 0 Writeeeprom Hulp1 , 0 Print Cls Lcd "LCD Testmessage" Print Input "Any key to continue...." , Yn Else Hulp1.0 = 1 Writeeeprom Hulp1 , 0 End If Noresponse: If Dsp_present = 0 Then Cls End If Print Chr(&H1b) ; "[2J"; ' ANSI goto 0/0 Print "Summary:" Print If Dsp_present = 1 Then Print "LCD-display : not used" Else Print "LCD-display : used" Print End If Print Print "Network:" Print Print "URL-refresh : "; Readeeprom Hulp1 , 23 If Hulp1 = &HFF Then Print "http://" ; Myip(1) ; "." ; Myip(2) ; "." ; Myip(3) ; "." ; Myip(4) Else Msg_temp = "" For Y = 1 To Hulp1 Hulp3 = Y + 23 Readeeprom Hulp2 , Hulp3 Msg_temp = Msg_temp + Chr(hulp2) Next Y Print Msg_temp End If Print Print "UDP-port : "; Readeeprom Hulp5h , 12 Readeeprom Hulp5l , 11 Print Hulp5 Print Print "IP-number : " ; Myip(1) ; "." ; Myip(2) ; "." ; Myip(3) ; "." ; Myip(4) Print Print "Mac-address : " ; Hex(mymac(1)) ; "-" ; Hex(mymac(2)) ; "-" ; Hex(mymac(3)) ; "-" ; Hex(mymac(4)) ; "-" ; Hex(mymac(5)) ; "-" ; Hex(mymac(6)) Print Print "Gateway-IP-number : " ; Gwip(1) ; "." ; Gwip(2) ; "." ; Gwip(3) ; "." ; Gwip(4) Print End Sub ' Routine to read the IP-numbers from the EEPROM ' Sub Read_ip #if Debug = 1 Print "Sub Read_ip" #endif Readeeprom Myip(1) , 1 : Readeeprom Myip(2) , 2 Readeeprom Myip(3) , 3 : Readeeprom Myip(4) , 4 End Sub ' Routine to print the IP-number ' Sub Print_ip #if Debug = 1 Print "Sub Print_ip" #endif Print Myip(1) ; "." ; Myip(2) ; "." ; Myip(3) ; "." ; Myip(4) End Sub ' Routine to read the IP-numbers from the EEPROM ' Sub Read_gwip #if Debug = 1 Print "Sub Read_gwip" #endif Readeeprom Gwip(1) , 13 : Readeeprom Gwip(2) , 14 Readeeprom Gwip(3) , 15 : Readeeprom Gwip(4) , 16 End Sub ' Routine to print the GWIP-number ' Sub Print_gwip #if Debug = 1 Print "Sub Print_ip" #endif Print Gwip(1) ; "." ; Gwip(2) ; "." ; Gwip(3) ; "." ; Gwip(4) End Sub ' Routine to read the MAC-address from the EEPROM ' Sub Read_mac #if Debug = 1 Print "Sub Read_mac" #endif Readeeprom Mymac(1) , 5 : Readeeprom Mymac(2) , 6 Readeeprom Mymac(3) , 7 : Readeeprom Mymac(4) , 8 Readeeprom Mymac(5) , 9 : Readeeprom Mymac(6) , 10 End Sub ' Routine to print the MAC-address ' Sub Print_mac #if Debug = 1 Print "Sub Print_mac" #endif Print Hex(mymac(1)) ; "-" ; Hex(mymac(2)) ; "-" ; Hex(mymac(3)) ; "-" ; Hex(mymac(4)) ; "-" ; Hex(mymac(5)) ; "-" ; Hex(mymac(6)) End Sub ' Routine to send a default-message to the LCD ' Sub Default_message #if Debug = 1 Print "Sub Default_message" #endif Call Read_ip If Dsp_present = 0 Then Cursor Off Cls Lcd "www.achatz.nl WebCat" Thirdline Lcd "IP: " Tempstring = Str(myip(1)) + "." + Str(myip(2)) + "." + Str(myip(3)) + "." + Str(myip(4)) Lcd Tempstring End If Wait 2 End Sub ' Routine to get the MAC-address of the gateway ' Sub Arp_request #if Debug = 1 Print "Sub Arp_request" #endif Disable Int5 'Destination all &hFF T_enetpacketdest0 = &HFF T_enetpacketdest1 = &HFF T_enetpacketdest2 = &HFF T_enetpacketdest3 = &HFF T_enetpacketdest4 = &HFF T_enetpacketdest5 = &HFF ' source (own source) T_enetpacketsrc0 = Mymac(1) T_enetpacketsrc1 = Mymac(2) T_enetpacketsrc2 = Mymac(3) T_enetpacketsrc3 = Mymac(4) T_enetpacketsrc4 = Mymac(5) T_enetpacketsrc5 = Mymac(6) ' T_enetpackettype = &H0608 '&h0806 T_arp_hwtype0 = &H00 '&h0001 T_arp_hwtype1 = &H01 'Arp T_arp_prttype0 = &H08 ' added for ARP-request routine T_arp_prttype1 = &H00 T_arp_hwlen = &H06 T_arp_prlen = &H04 T_arp_op0 = &H00 ' &h0001 T_arp_op1 = &H01 'arp source mac address T_arp_src_enetpacket0 = Mymac(1) T_arp_src_enetpacket1 = Mymac(2) T_arp_src_enetpacket2 = Mymac(3) T_arp_src_enetpacket3 = Mymac(4) T_arp_src_enetpacket4 = Mymac(5) T_arp_src_enetpacket5 = Mymac(6) 'arp source ip address T_arp_sipaddr0 = Myip(1) T_arp_sipaddr1 = Myip(2) T_arp_sipaddr2 = Myip(3) T_arp_sipaddr3 = Myip(4) ' arp target mac address T_arp_dest_enetpacket0 = &H00 T_arp_dest_enetpacket1 = &H00 T_arp_dest_enetpacket2 = &H00 T_arp_dest_enetpacket3 = &H00 T_arp_dest_enetpacket4 = &H00 T_arp_dest_enetpacket5 = &H00 'arp target IP address T_arp_tipaddr = Arpreqip T_enetpacketlenl = 64 T_enetpacketlenh = 0 Call Echopacket Enable Int5 End Sub Sub Arp_reply #if Debug = 1 Print "ARP-reply" #endif Gwmac(1) = T_enetpacketsrc0 Gwmac(2) = T_enetpacketsrc1 Gwmac(3) = T_enetpacketsrc2 Gwmac(4) = T_enetpacketsrc3 Gwmac(5) = T_enetpacketsrc4 Gwmac(6) = T_enetpacketsrc5 End Sub ' htmlcode ' Htmlcode: Data "HTTP/1.0 200" , &H0D , &H0A , &H0D , &H0A Data "" Data "www.achatz.nl / WebCat" Data "" Data "

www.achatz.nl - WebCat" Data "" Data "" , &H0D , &H0A Data "endblock"