ADVERTISEMENT

Serwer_ENC28J60.zip

Bascom Atmega 1284P - Moduł internetowy na ENC28J60

Witam Korzystając kodu Tuxgraphics zmodyfikowanego przez Kolegę Mundi1970, chciałem również postawić prosty serwer pokazujący temperatury z kilku czujników oraz wilgotność i ciśnienie. Po zmianach regfile i pinu CS nie mogę go jednak skompilować. Występują trzy błędy: Error 46 Line: 363 Assignment Error Error 242 Line: 450 Source variable does not match the target variable - to jeszcze można zrozumieć, bo nigdzie nie ma zadeklarowanej tej zmiennej, nie wiem, czy deklarować globalnie, czy tylko dla podproramu Enc28j60_linkup Erroe 229 Line: 450 End Sub or End Function Expected '****************************** ' Serwer HTTP na Atmega1284P ' RFM12B '****************************** $regfile = "m1284pdef.dat" $crystal = 16000000 $baud = 9600 $hwstack = 128 ' rozmiar stosu sprzętowego $swstack = 128 ' rozmiar stosu programowego $framesize = 128 ' rozmiar ramki $include "enc28j60.inc" $lib "tcpip.lbx" $lib "datetime.lbx" '------------------------------------------------------------------------------- ' zmienne globalne wykorzystane przez przerwania TIMER1 OVF Dim Tcp_timeout As Word , Countdown1 As Word '------------------------------------------------------------------------------- ' konfiguracja przerwania TIMER1 OVF wykorzystane do obliczania 1 sekundy Config Timer1 = Timer , Prescale = 256 ' konfiguracja Timer1 On Timer1 Odmierz1s ' wektor przerwania TIMER1 OVF Timer1 = 3036 ' przepe-nienie bŕdzie co 1s Enable Timer1 ' w-¦czenie przerwania TIMER1 OVF Enable Interrupts '------------------------------------------------------------------------------- '/* Maksymalny rozmiar ramki - Atmega168 - 1024 RAM, Atmega644 - 4096 RAM , Atmega1284P - 16384 RAM Const Max_framelen = 1514 + 4 ' ¬eby mog-y byŠ odbierane pakiety jumbo 1514 bajtˇw wartoťŠ musi wynosiŠ 1518 'Const Max_framelen = 594 '594 byte will be enough for common tasks Enc28j60_cs Alias Portd.4 Config Enc28j60_cs = Output Led Alias Portc.2 Config Led = Output Set Led 'Turn off the tuxgraphics board LED Config Date = Dmy , Separator = - Dim Licznik_odwiedzin As Long Dim Stan_tcp As Byte , Src_port As Word , Src_ip As Long Dim Src_mac0 As Byte , Src_mac1 As Byte , Src_mac2 As Byte Dim Src_mac3 As Byte , Src_mac4 As Byte , Src_mac5 As Byte Dim Tcp_err As Byte Const Listen = 0 Const Syn_received = 1 Const Established = 2 Const Close_wait = 3 Const Last_act = 4 Stan_tcp = Listen Dim I_chksum32 As Long Dim I_checksum16 As Word Dim T_ip_pktlen As Word Dim Result16 As Word Dim Result16h As Byte At Result16 + 1 Overlay Dim Result16l As Byte At Result16 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 Dim Hulp2 As Word Dim Work As Byte Dim L As Byte 'variables with overlays Dim Myip(4) As Byte Dim My_ip As Long At Myip Overlay Dim Mymac(6) As Byte 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 '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 'toegevoegd voor TCP Dim Tcp_fin As Bit Dim Tcp_syn As Bit Dim Tcp_rst As Bit Dim Tcp_psh As Bit Dim Tcp_ack As Bit Dim Tcp_urg As Bit Dim Tcpdatalen_in As Word Dim Flags As Byte 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 Tempword As Word Dim Tempwordh As Byte At Tempword + 1 Overlay Dim Tempwordl As Byte At Tempword Overlay Dim Val1 As Word Dim Val2 As Word Dim Y As Word Const Synflag = 0 Const Finflag = 1 Dim X As Word Dim A(5) As Byte Dim Enc28j60_bank As Byte Dim Enc28j60_data As Byte Dim Enc28j60_nextpacketptr As Word Dim Enc28j60_value As Byte Dim Enc28j60_wdata As Word Dim Length As Word Dim Rxstat As Word Dim Ip_id As Word Dim I_header_length As Word Dim Enc28j60_packetlen As Word Dim Enc28j60_statuslink As Byte Dim Jest_pakiet As Byte Dim Buffer(max_framelen) As Byte Dim Msg_temp As String * 55 'Ethernet packet destination Dim T_enetpacketdest0 As Byte At Buffer Overlay Dim T_enetpacketdest1 As Byte At Buffer + &H01 Overlay Dim T_enetpacketdest2 As Byte At Buffer + &H02 Overlay Dim T_enetpacketdest3 As Byte At Buffer + &H03 Overlay Dim T_enetpacketdest4 As Byte At Buffer + &H04 Overlay Dim T_enetpacketdest5 As Byte At Buffer + &H05 Overlay 'Ethernet packet source Dim T_enetpacketsrc0 As Byte At Buffer + &H06 Overlay Dim T_enetpacketsrc1 As Byte At Buffer + &H07 Overlay Dim T_enetpacketsrc2 As Byte At Buffer + &H08 Overlay Dim T_enetpacketsrc3 As Byte At Buffer + &H09 Overlay Dim T_enetpacketsrc4 As Byte At Buffer + &H0A Overlay Dim T_enetpacketsrc5 As Byte At Buffer + &H0B Overlay 'Ethernet packet type Dim T_enetpackettype As Word At Buffer + &H0C Overlay Dim T_arp_hwtype1 As Byte At Buffer + &H0F Overlay 'Arp Dim T_arp_prttype1 As Byte At Buffer + &H11 Overlay Dim T_arp_hwlen As Byte At Buffer + &H12 Overlay Dim T_arp_prlen As Byte At Buffer + &H13 Overlay Dim T_arp_op1 As Byte At Buffer + &H15 Overlay 'arp source ip address Dim T_arp_sipaddr0 As Byte At Buffer + &H1C Overlay Dim T_arp_sipaddr1 As Byte At Buffer + &H1D Overlay Dim T_arp_sipaddr2 As Byte At Buffer + &H1E Overlay Dim T_arp_sipaddr3 As Byte At Buffer + &H1F Overlay 'arp target IP address Dim T_arp_tipaddr0 As Byte At Buffer + &H26 Overlay Dim T_arp_tipaddr1 As Byte At Buffer + &H27 Overlay Dim T_arp_tipaddr2 As Byte At Buffer + &H28 Overlay Dim T_arp_tipaddr3 As Byte At Buffer + &H29 Overlay Dim T_arp_tipaddr As Long At Buffer + &H26 Overlay 'IP header layout IP version and header length Dim T_ip_vers_len As Byte At Buffer + &H0E Overlay Dim T_arp_hwtype0 As Byte At Buffer + &H0E Overlay 'Arp Dim T_arp_prttype0 As Byte At Buffer + &H10 Overlay Dim T_arp_op0 As Byte At Buffer + &H14 Overlay 'arp source mac address Dim T_arp_src_enetpacket0 As Byte At Buffer + &H16 Overlay Dim T_arp_src_enetpacket1 As Byte At Buffer + &H17 Overlay Dim T_arp_src_enetpacket2 As Byte At Buffer + &H18 Overlay Dim T_arp_src_enetpacket3 As Byte At Buffer + &H19 Overlay Dim T_arp_src_enetpacket4 As Byte At Buffer + &H1A Overlay Dim T_arp_src_enetpacket5 As Byte At Buffer + &H1B Overlay 'arp source mac address Dim T_arp_dest_enetpacket0 As Byte At Buffer + &H20 Overlay Dim T_arp_dest_enetpacket1 As Byte At Buffer + &H21 Overlay Dim T_arp_dest_enetpacket2 As Byte At Buffer + &H22 Overlay Dim T_arp_dest_enetpacket3 As Byte At Buffer + &H23 Overlay Dim T_arp_dest_enetpacket4 As Byte At Buffer + &H24 Overlay Dim T_arp_dest_enetpacket5 As Byte At Buffer + &H25 Overlay Dim T_tos As Byte At Buffer + &H0F Overlay 'Buffer length Dim T_ip_pktlen0 As Byte At Buffer + &H10 Overlay Dim T_ip_pktlen1 As Byte At Buffer + &H11 Overlay Dim T_id0 As Byte At Buffer + &H12 Overlay Dim T_id1 As Byte At Buffer + &H13 Overlay Dim T_flags As Byte At Buffer + &H14 Overlay Dim T_offset As Byte At Buffer + &H15 Overlay Dim T_ttl As Byte At Buffer + &H16 Overlay 'protocol (ICMP=1, TCP=6, UDP=11) Dim T_ip_proto As Byte At Buffer + &H17 Overlay 'header checksum Dim T_ip_hdr_cksum0 As Byte At Buffer + &H18 Overlay Dim T_ip_hdr_cksum1 As Byte At Buffer + &H19 Overlay Dim T_ip_hdr_cksum As Word At Buffer + &H18 Overlay 'IP address of source Dim T_ip_srcaddr0 As Byte At Buffer + &H1A Overlay Dim T_ip_srcaddr1 As Byte At Buffer + &H1B Overlay Dim T_ip_srcaddr2 As Byte At Buffer + &H1C Overlay Dim T_ip_srcaddr3 As Byte At Buffer + &H1D Overlay Dim T_ip_srcaddr As Long At Buffer + &H1A Overlay 'IP address of destination Dim T_ip_destaddr0 As Byte At Buffer + &H1E Overlay Dim T_ip_destaddr1 As Byte At Buffer + &H1F Overlay Dim T_ip_destaddr2 As Byte At Buffer + &H20 Overlay Dim T_ip_destaddr3 As Byte At Buffer + &H21 Overlay Dim T_ip_destaddr As Long At Buffer + &H1E Overlay Dim T_icmp_type As Byte At Buffer + &H22 Overlay Dim T_icmp_code As Byte At Buffer + &H23 Overlay Dim T_icmp_cksum0 As Byte At Buffer + &H24 Overlay Dim T_icmp_cksum1 As Byte At Buffer + &H25 Overlay Dim T_icmp_cksum As Word At Buffer + &H24 Overlay Dim Tcp_srcport As Word At Buffer + &H22 Overlay Dim Tcp_srcporth As Byte At Buffer + &H22 Overlay Dim Tcp_srcportl As Byte At Buffer + &H23 Overlay Dim Tcp_destport As Word At Buffer + &H24 Overlay Dim Tcp_destporth As Byte At Buffer + &H24 Overlay Dim Tcp_destportl As Byte At Buffer + &H25 Overlay Dim Tcp_seqnum3 As Byte At Buffer + &H26 Overlay Dim Tcp_seqnum2 As Byte At Buffer + &H27 Overlay Dim Tcp_seqnum1 As Byte At Buffer + &H28 Overlay Dim Tcp_seqnum0 As Byte At Buffer + &H29 Overlay Dim Tcp_acknum3 As Byte At Buffer + &H2A Overlay Dim Tcp_acknum2 As Byte At Buffer + &H2B Overlay Dim Tcp_acknum1 As Byte At Buffer + &H2C Overlay Dim Tcp_acknum0 As Byte At Buffer + &H2D Overlay Dim Tcp_hdr As Byte At Buffer + &H2E Overlay Dim Tcp_flags As Byte At Buffer + &H2F Overlay Dim Tcp_win_size As Word At Buffer + &H30 Overlay Dim Tcp_cksumh As Byte At Buffer + &H32 Overlay Dim Tcp_cksuml As Byte At Buffer + &H33 Overlay Dim Tcp_cksum As Word At Buffer + &H32 Overlay 'UDP header Dim T_udp_srcport0 As Byte At Buffer + &H22 Overlay Dim T_udp_srcport1 As Byte At Buffer + &H23 Overlay Dim T_udp_srcport As Word At Buffer + &H22 Overlay Dim T_udp_destport0 As Byte At Buffer + &H24 Overlay Dim T_udp_destport1 As Byte At Buffer + &H25 Overlay Dim T_udp_destport As Word At Buffer + &H24 Overlay Dim T_udp_len0 As Byte At Buffer + &H26 Overlay Dim T_udp_len1 As Byte At Buffer + &H27 Overlay Dim T_udp_chksum0 As Byte At Buffer + &H28 Overlay Dim T_udp_chksum1 As Byte At Buffer + &H29 Overlay Dim T_udp_chksum As Word At Buffer + &H28 Overlay Dim T_udp_data As Byte At Buffer + &H2A Overlay Dim T_udp_data1 As Byte At Buffer + &H2B Overlay Dim T_udp_data2 As Byte At Buffer + &H2C Overlay Dim T_udp_data3 As Byte At Buffer + &H2D Overlay Dim T_udp_data4 As Byte At Buffer + &H2E Overlay Dim T_udp_data5 As Byte At Buffer + &H2F Overlay Dim T_udp_data6 As Byte At Buffer + &H30 Overlay Dim T_udp_data7 As Byte At Buffer + &H31 Overlay Dim T_udp_data8 As Byte At Buffer + &H32 Overlay Dim T_udp_data9 As Byte At Buffer + &H33 Overlay Dim T_udp_data10 As Byte At Buffer + &H34 Overlay Dim T_udp_data11 As Byte At Buffer + &H35 Overlay Dim T_udp_data12 As Byte At Buffer + &H36 Overlay Dim T_udp_data13 As Byte At Buffer + &H37 Overlay Dim T_udp_data14 As Byte At Buffer + &H38 Overlay Dim T_udp_data15 As Byte At Buffer + &H39 Overlay Dim T_udp_data16 As Byte At Buffer + &H3A Overlay Dim T_udp_data17 As Byte At Buffer + &H3B Overlay Dim T_udp_data18 As Byte At Buffer + &H3C Overlay Dim T_udp_data19 As Byte At Buffer + &H3D Overlay Dim T_udp_data20 As Byte At Buffer + &H3E Overlay Dim T_udp_data21 As Byte At Buffer + &H3F Overlay Dim T_udp_data22 As Byte At Buffer + &H40 Overlay Dim T_udp_data23 As Byte At Buffer + &H41 Overlay Dim T_udp_data24 As Byte At Buffer + &H42 Overlay Dim T_udp_data25 As Byte At Buffer + &H43 Overlay Dim T_udp_data26 As Byte At Buffer + &H44 Overlay Dim T_udp_data27 As Byte At Buffer + &H45 Overlay Dim T_udp_data28 As Byte At Buffer + &H46 Overlay Dim T_udp_data29 As Byte At Buffer + &H47 Overlay Dim T_udp_data30 As Byte At Buffer + &H48 Overlay Dim T_udp_data31 As Byte At Buffer + &H49 Overlay Dim T_udp_data32 As Byte At Buffer + &H4A Overlay Declare Sub Enc28j60_reset Declare Sub Enc28j60_init Declare Sub Enc28j60_readcontrolregbyte(byval Register As Byte) Declare Sub Enc28j60_writecontrolregbyte(byval Register As Byte , Byval Enc28j60_value As Byte) Declare Sub Enc28j60_selectbank(byval Enc28j60_bank As Byte) Declare Sub Enc28j60_bitfield_set(byval Register As Byte , Byval Enc28j60_value As Byte) Declare Sub Enc28j60_bitfield_clear(byval Register As Byte , Byval Enc28j60_value As Byte) Declare Sub Enc28j60_readphyword(byval Phyregister As Byte) Declare Sub Enc28j60_writephyword(byval Phyregister As Byte , Byval Enc28j60_wdata As Word) Declare Sub Enc28j60_packetsend(byval Pcktlen As Word) Declare Function Enc28j60_packetreceive() As Word ' zwracana jest d-ugoťŠ pakietu Declare Sub Enc28j60_poll Declare Sub Enc28j60_version Declare Sub Enc28j60_linkup Declare Sub Arpreply Declare Sub Pingreply Declare Sub Setip_id Declare Sub Udp_receive Declare Sub Udp_checksum Declare Sub Blinky(byval L As Byte) Declare Sub Setipaddrs Declare Sub Tcp Declare Sub Echopacket Declare Sub Ip_header_checksum Declare Sub Icmp_checksum Declare Sub Ntp_get Declare Sub Ntp Declare Sub Http Declare Sub Tcp_checksum Declare Sub Src2dest Declare Sub Inc2seq Declare Sub Seq2ack Declare Sub Ack2inc Declare Sub Src2mymac Declare Sub Srcdestchksum ' Adres MAC Mymac(1) = &H00 Mymac(2) = &HC6 Mymac(3) = &HB3 Mymac(4) = &HF3 Mymac(5) = &HAB Mymac(6) = &H9E ' Adres IP Myip(1) = 192 Myip(2) = 168 Myip(3) = 0 Myip(4) = 167 ' Adres IP 'Myip(1) = 10 'Myip(2) = 0 'Myip(3) = 0 'Myip(4) = 109 ' Konfiguracja szyny SPI Config Spi = Hard , Interrupt = Off , Data Order = Msb , Master = Yes , Polarity = Low , Phase = 0 , Clockrate = 4 , Noss = 1 'Inicjowanie SPI Spiinit Spi2x0 = 0 ' podwˇjna prŕdkoťŠ SPI (czyli przy $crystal = 16000000 czŕstotliwoťŠ SPI bŕdzie = 8MHz) Call Enc28j60_reset 'clock from default divide/4 (6.25 Mhz) to divide/2 (12.5 Mhz) Call Enc28j60_writecontrolregbyte(ecocon , &B00000010) Waitms 250 Print Print "Starting Tux" Call Enc28j60_version Call Enc28j60_init 'Call Ntp_get 'get the Network Time once Do If Countdown1 = 0 Then ' je¬eli mine-a 1 sekunda to Call Enc28j60_linkup ' sprawdč LINK Countdown1 = 1 ' ustaw timer na 1 sekunde If Enc28j60_statuslink = 0 Then Print "Kabel ethernet jest odlaczony" End If If Enc28j60_statuslink = 1 Then Call Enc28j60_poll If Jest_pakiet = 1 Then 'Print "Dlugosc pakietu: " ; Enc28j60_packetlen Gosub Handle_packet End If Gosub Sprawdz_timeout End If Loop End Handle_packet: 'Uchwyt pakietu If Buffer(13) = &H08 Then If Buffer(14) = &H06 Then 'Type:ARP If Buffer(21) = &H00 Then 'ARP request If Buffer(22) = &H01 Then 'ARP request If Buffer(39) = Myip(1) Then 'ARP for us If Buffer(40) = Myip(2) Then If Buffer(41) = Myip(3) Then If Buffer(42) = Myip(4) Then Call Arpreply End If End If End If End If End If End If End If If Buffer(14) = &H00 Then 'Type:IP If Buffer(15) = &H45 Then 'We handle only simple IP packets 'If Buffer(21) = 0 Then 'We handle only non fragmented packets If Buffer(31) = Myip(1) Then 'Ip packet for us If Buffer(32) = Myip(2) Then If Buffer(33) = Myip(3) Then If Buffer(34) = Myip(4) Then If Buffer(24) = 1 Then 'Protocol:ICMP If Buffer(35) = &H08 Then 'ICMP echo request Call Pingreply End If End If If Buffer(24) = 17 Then 'Protocol:UDP Call Udp_receive End If If Buffer(24) = 6 Then 'Protocol:TCP Call Tcp End If End If End If End If 'End If End If End If End If End If Return Sub Enc28j60_linkup Call Enc28j60_readphyword(phstat2) Enc28j60_wdata = Enc28j60_wdata And Phstat2_lstat If Enc28j60_wdata = Phstat2_lstat Then Enc28j60_statuslink = 1 Else Enc28j60_statuslink = 0 End If End Sub Sub Enc28j60_reset Enc28j60_cs = 0 X = Enc28j60_soft_reset 'reset ENC28J60 Spiout X , 1 Enc28j60_cs = 1 Waitms 10 End Sub Sub Enc28j60_version 'VERSION Call Enc28j60_selectbank(3) A(1) = &B000_10010 'EREVID Enc28j60_cs = 0 Spiout A(1) , 1 Spiin A(1) , 2 Print "Enc28j60-version = " ; A(2) Enc28j60_cs = 1 End Sub Sub Enc28j60_init 'do bank 0 stuff 'inicjowanie bufora odbioru 'transfer 16-bit , napierw musi być zapisywany niski ( low )bajt Enc28j60_nextpacketptr = Rxstart_init ' ustawienie adresu poczatkowego buforu odbioru Enc28j60_value = Low(rxstart_init) Call Enc28j60_writecontrolregbyte(erxstl , Enc28j60_value) Enc28j60_value = High(rxstart_init) Call Enc28j60_writecontrolregbyte(erxsth , Enc28j60_value) Enc28j60_value = Low(rxstart_init) 'set receive pointer address Call Enc28j60_writecontrolregbyte(erxrdptl , Enc28j60_value) Enc28j60_value = High(rxstart_init) Call Enc28j60_writecontrolregbyte(erxrdpth , Enc28j60_value) Enc28j60_value = Low(rxstop_init) 'ustawienie adresu koncowego buforu odbioru Call Enc28j60_writecontrolregbyte(erxndl , Enc28j60_value) Enc28j60_value = High(rxstop_init) Call Enc28j60_writecontrolregbyte(erxndh , Enc28j60_value) Enc28j60_value = Low(txstart_init) 'ustawienie adresu poczatkowego buforu nadawczego Call Enc28j60_writecontrolregbyte(etxstl , Enc28j60_value) Enc28j60_value = High(txstart_init) Call Enc28j60_writecontrolregbyte(etxsth , Enc28j60_value) '-------- dodano Enc28j60_value = Low(txstop_init) 'ustawienie adresu koncowego buforu nadawczego Call Enc28j60_writecontrolregbyte(etxndl , Enc28j60_value) Enc28j60_value = High(txstop_init) Call Enc28j60_writecontrolregbyte(etxndh , Enc28j60_value) '-------- 'do bank 2 stuff Enc28j60_value = 0 ' enable MAC receive Enc28j60_value.macon1_marxen = 1 Enc28j60_value.macon1_txpaus = 1 Enc28j60_value.macon1_rxpaus = 1 Call Enc28j60_writecontrolregbyte(macon1 , Enc28j60_value) Enc28j60_value = 0 Enc28j60_value.macon3_padcfg0 = 1 'bring MAC out of reset Enc28j60_value.macon3_txcrcen = 1 'enable automatic padding and CRC operations Enc28j60_value.macon3_frmlnen = 1 Call Enc28j60_writecontrolregbyte(macon3 , Enc28j60_value) Call Enc28j60_writecontrolregbyte(maipgl , &H12) 'set inter-frame gap (non-back-to-back) Call Enc28j60_writecontrolregbyte(maipgh , &H0C) Call Enc28j60_writecontrolregbyte(mabbipg , &H12) 'set inter-frame gap (back-to-back) Enc28j60_value = Low(max_framelen) 'set the maximum packet size which the controller will accept Call Enc28j60_writecontrolregbyte(mamxfll , Enc28j60_value) Enc28j60_value = High(max_framelen) Call Enc28j60_writecontrolregbyte(mamxflh , Enc28j60_value) 'bank 3 stuff Call Enc28j60_writecontrolregbyte(maadr5 , Mymac(1)) Call Enc28j60_writecontrolregbyte(maadr4 , Mymac(2)) Call Enc28j60_writecontrolregbyte(maadr3 , Mymac(3)) Call Enc28j60_writecontrolregbyte(maadr2 , Mymac(4)) Call Enc28j60_writecontrolregbyte(maadr1 , Mymac(5)) Call Enc28j60_writecontrolregbyte(maadr0 , Mymac(6)) Call Enc28j60_writephyword(phcon2 , Phcon2_hdldis) 'no loopback of transmitted frames Call Enc28j60_selectbank(0) 'switch to bank 0 Enc28j60_value = 0 'enable interrupts Enc28j60_value.eie_intie = 1 Enc28j60_value.eie_pktie = 1 Call Enc28j60_bitfield_set(eie , Enc28j60_value) 'filters according to Guido Call Enc28j60_writecontrolregbyte(epmm0 , &H3F) Call Enc28j60_writecontrolregbyte(epmm1 , &H30) Call Enc28j60_writecontrolregbyte(epmcsl , &HF9) Call Enc28j60_writecontrolregbyte(epmcsh , &HF7) Enc28j60_value = 0 Enc28j60_value.erxfcon_pmen = 1 'Pattern Match enable (ARP only) Enc28j60_value.erxfcon_ucen = 1 'Unicast enable Call Enc28j60_bitfield_set(erxfcon , Enc28j60_value) Enc28j60_value = 0 Enc28j60_value.erxfcon_bcen = 1 'Broadcast disable Call Enc28j60_bitfield_clear(erxfcon , Enc28j60_value) 'CRC check is enabled by default 'Something is wrong with the Broadcast filter (or the whole theory), it seems 'like every packet is coming in Enc28j60_value = 0 'enable packet reception Enc28j60_value.econ1_rxen = 1 Call Enc28j60_bitfield_set(econ1 , Enc28j60_value) '------- zmieniono ' Enc28j60_value = 0 'Reset transmit logic ' Enc28j60_value.econ1_txrst = 1 ' Call Enc28j60_bitfield_set(econ1 , Enc28j60_value) ' Call Enc28j60_bitfield_clear(econ1 , Enc28j60_value) Call Enc28j60_readcontrolregbyte(econ1) X = Enc28j60_data.econ1_txrts While X = 1 ' Check no transmit in progress Call Enc28j60_readcontrolregbyte(eir) X = Enc28j60_data.eir_txerif If X = 1 Then Enc28j60_value = 0 'Reset transmit logic Enc28j60_value.econ1_txrst = 1 Call Enc28j60_bitfield_set(econ1 , Enc28j60_value) Call Enc28j60_bitfield_clear(econ1 , Enc28j60_value) End If Call Enc28j60_readcontrolregbyte(econ1) X = Enc28j60_data.econ1_txrts Wend '-------- End Sub Sub Enc28j60_selectbank(enc28j60_bank As Byte) 'get ECON1 (BSEL1 en BSEL0) A(1) = &B000_11111 Enc28j60_cs = 0 Spiout A(1) , 1 Spiin A(1) , 2 Enc28j60_cs = 1 A(2) = A(2) And &B1111_1100 'strip bank part A(2) = A(2) Or Enc28j60_bank A(1) = &B010_11111 Enc28j60_cs = 0 Spiout A(1) , 2 Enc28j60_cs = 1 End Sub Sub Enc28j60_writecontrolregbyte(register As Byte , Enc28j60_value As Byte) Enc28j60_bank = 0 If Register.7 = 1 Then Enc28j60_bank = 2 If Register.6 = 1 Then Enc28j60_bank = Enc28j60_bank + 1 Register = Register And &B00011111 Call Enc28j60_selectbank(enc28j60_bank) Register.6 = 1 'to get a 010_register A(1) = Register A(2) = Enc28j60_value Enc28j60_cs = 0 Spiout A(1) , 2 Enc28j60_cs = 1 End Sub Sub Enc28j60_readcontrolregbyte(register As Byte) Local Mcphy As Byte Enc28j60_bank = 0 Mcphy = 0 If Register.7 = 1 Then Enc28j60_bank = 2 If Register.6 = 1 Then Enc28j60_bank = Enc28j60_bank + 1 If Register.5 = 1 Then Mcphy = 1 Register = Register And &B00011111 Call Enc28j60_selectbank(enc28j60_bank) A(1) = Register Enc28j60_cs = 0 Spiout A(1) , 1 Spiin A(1) , 3 Enc28j60_cs = 1 'Depending of register (E, MAC, MII) yes or no dummybyte '---------- zmieniono If Mcphy = 1 Then Enc28j60_data = A(2) Else ' Enc28j60_data = A(3) Enc28j60_data = A(1) End If '---------- End Sub Sub Enc28j60_bitfield_set(register As Byte , Enc28j60_value As Byte) Enc28j60_bank = 0 If Register.7 = 1 Then Enc28j60_bank = 2 If Register.6 = 1 Then Enc28j60_bank = Enc28j60_bank + 1 Register = Register And &B00011111 Call Enc28j60_selectbank(enc28j60_bank) Register = Register Or &B100_00000 A(1) = Register A(2) = Enc28j60_value Enc28j60_cs = 0 Spiout A(1) , 2 Enc28j60_cs = 1 End Sub Sub Enc28j60_bitfield_clear(register As Byte , Enc28j60_value As Byte) Enc28j60_bank = 0 If Register.7 = 1 Then Enc28j60_bank = 2 If Register.6 = 1 Then Enc28j60_bank = Enc28j60_bank + 1 Register = Register And &B00011111 Call Enc28j60_selectbank(enc28j60_bank) Register = Register Or &B101_00000 A(1) = Register A(2) = Enc28j60_value Enc28j60_cs = 0 Spiout A(1) , 2 Enc28j60_cs = 1 End Sub Sub Enc28j60_readphyword(phyregister As Byte) Call Enc28j60_writecontrolregbyte(miregadr , Phyregister) 'set the right address and start the register read operation Call Enc28j60_writecontrolregbyte(micmd , Micmd_miird) Do 'wait until the PHY read complets Call Enc28j60_readcontrolregbyte(mistat) Loop Until Enc28j60_data.mistat_busy = 0 Call Enc28j60_writecontrolregbyte(micmd , 0) Call Enc28j60_readcontrolregbyte(mirdh) 'get data value (zmieniono) Enc28j60_wdata = Enc28j60_data Shift Enc28j60_wdata , Left , 8 Call Enc28j60_readcontrolregbyte(mirdl) Enc28j60_wdata = Enc28j60_wdata + Enc28j60_data End Sub Sub Enc28j60_writephyword(phyregister As Byte , Enc28j60_wdata As Word) Call Enc28j60_readphyword(phyregister) Local Temp As Byte Call Enc28j60_writecontrolregbyte(miregadr , Phyregister) 'set the PHY register address Call Enc28j60_readcontrolregbyte(miregadr) Temp = Miregadr Enc28j60_value = Low(enc28j60_wdata) Call Enc28j60_writecontrolregbyte(miwrl , Enc28j60_value) Enc28j60_value = High(enc28j60_wdata) Call Enc28j60_writecontrolregbyte(miwrh , Enc28j60_value) Do Call Enc28j60_readcontrolregbyte(mistat) Loop Until Enc28j60_data.mistat_busy = 0 End Sub Sub Enc28j60_poll Call Enc28j60_readcontrolregbyte(epktcnt) If Enc28j60_data > 0 Then Jest_pakiet = 1 Enc28j60_packetlen = Enc28j60_packetreceive() Else Jest_pakiet = 0 End If End Sub Function Enc28j60_packetreceive() As Word Local Length As Word Local Wtemp As Word Local Rxstat As Word Enc28j60_value = Low(enc28j60_nextpacketptr) 'set the read pointer to the start of the received packet Call Enc28j60_writecontrolregbyte(erdptl , Enc28j60_value) Enc28j60_value = High(enc28j60_nextpacketptr) Call Enc28j60_writecontrolregbyte(erdpth , Enc28j60_value) Enc28j60_cs = 0 Spdr = &H3A 'Send Read Buffer Memory command Do Loop Until Spsr.spif = 1 For X = 1 To 6 'Get the first 6 byte (3 word: Nextpacketptr, Packetlength, Rxstat) Spdr = &HFF 'SPI read Do Loop Until Spsr.spif = 1 'SPI ready Buffer(x) = Spdr Next X Enc28j60_nextpacketptr = Buffer(2) * 256 Enc28j60_nextpacketptr = Enc28j60_nextpacketptr + Buffer(1) Length = Buffer(4) * 256 Length = Length + Buffer(3) Rxstat = Buffer(6) * 256 Rxstat = Rxstat + Buffer(5) Length = Length - 4 'Discard CRC ' -------------- dodano i zmieniono Const Mfl = Max_framelen - 1 If Length > Mfl Then Length = Mfl ' sprawdč czy d-ugoťŠ pakietu nie jest wiŕksza od bufora Wtemp = Rxstat And &H0080 ' maskuj bit Received Ok If Wtemp = 0 Then Length = 0 Else For X = 1 To Length 'Get the payload Spdr = &HFF 'SPI read Do Loop Until Spsr.spif = 1 'SPI ready Buffer(x) = Spdr Next X End If Enc28j60_cs = 1 '------------- zmieniono Wtemp = Enc28j60_nextpacketptr - 1 If Wtemp > Rxstop_init Then Enc28j60_value = Low(rxstop_init) 'set receive buffer end Call Enc28j60_writecontrolregbyte(erxrdptl , Enc28j60_value) Enc28j60_value = High(rxstop_init) Call Enc28j60_writecontrolregbyte(erxrdpth , Enc28j60_value) Else Enc28j60_value = Low(wtemp) 'move the rx read pointer to the start of the next received packet this frees the memory we just read out Call Enc28j60_writecontrolregbyte(erxrdptl , Enc28j60_value) Enc28j60_value = High(wtemp) Call Enc28j60_writecontrolregbyte(erxrdpth , Enc28j60_value) End If '------------- Enc28j60_value = 0 'decrement the packet counter indicate we are done with this packet Enc28j60_value.econ2_pktdec = 1 Call Enc28j60_bitfield_set(econ2 , Enc28j60_value) Enc28j60_packetreceive = Length End Function Sub Enc28j60_packetsend(pcktlen As Word) '------------ dodano Call Enc28j60_readcontrolregbyte(econ1) X = Enc28j60_data.econ1_txrts While X = 1 ' Check no transmit in progress Call Enc28j60_readcontrolregbyte(eir) X = Enc28j60_data.eir_txerif If X = 1 Then Enc28j60_value = 0 ' Reset transmit logic Enc28j60_value.econ1_txrst = 1 Call Enc28j60_bitfield_set(econ1 , Enc28j60_value) Call Enc28j60_bitfield_clear(econ1 , Enc28j60_value) End If Call Enc28j60_readcontrolregbyte(econ1) X = Enc28j60_data.econ1_txrts Wend '----------- Enc28j60_cs = 0 'Load packet into the Enc28j60 Spdr = Enc28j60_write_buf_mem Do Loop Until Spsr.spif = 1 Spdr = &B000_1110 'per packet byte Do Loop Until Spsr.spif = 1 For X = 1 To Pcktlen Spdr = Buffer(x) Do Loop Until Spsr.spif = 1 Next X Enc28j60_cs = 1 ' If Pcktlen < 60 Then Pcktlen = 60 'Minimum packet length is 60 Enc28j60_value = 0 'Reset transmit logic Enc28j60_value.econ1_txrst = 1 Call Enc28j60_bitfield_set(econ1 , Enc28j60_value) Call Enc28j60_bitfield_clear(econ1 , Enc28j60_value) Enc28j60_value = Low(txstart_init) 'set the write pointer to start of transmit buffer area Call Enc28j60_writecontrolregbyte(ewrptl , Enc28j60_value) Enc28j60_value = High(txstart_init) Call Enc28j60_writecontrolregbyte(ewrpth , Enc28j60_value) Enc28j60_value = Low(txstart_init) 'set the TXND pointer to correspond to the packet size given Enc28j60_value = Enc28j60_value + Low(pcktlen) Call Enc28j60_writecontrolregbyte(etxndl , Enc28j60_value) Enc28j60_value = High(txstart_init) Enc28j60_value = Enc28j60_value + High(pcktlen) Call Enc28j60_writecontrolregbyte(etxndh , Enc28j60_value) Enc28j60_value = 0 'write per-packet control byte has been put in the writeroutine send the contents of the transmit buffer onto the network Enc28j60_value.econ1_txrts = 1 Call Enc28j60_bitfield_set(econ1 , Enc28j60_value) End Sub Sub Arpreply 'The original request packet is in the buffer, we just change some things 'Swap MAC addresses Call Src2dest 'Set source MAC in ethernet frame, pos 7 Call Src2mymac 'Copy target MAC in ARP packet T_arp_dest_enetpacket0 = T_enetpacketdest0 T_arp_dest_enetpacket1 = T_enetpacketdest1 T_arp_dest_enetpacket2 = T_enetpacketdest2 T_arp_dest_enetpacket3 = T_enetpacketdest3 T_arp_dest_enetpacket4 = T_enetpacketdest4 T_arp_dest_enetpacket5 = T_enetpacketdest5 'Set target IP in ARP packet T_arp_tipaddr0 = T_arp_sipaddr0 T_arp_tipaddr1 = T_arp_sipaddr1 T_arp_tipaddr2 = T_arp_sipaddr2 T_arp_tipaddr3 = T_arp_sipaddr3 'Copy source MAC to ARP T_arp_src_enetpacket0 = T_enetpacketsrc0 T_arp_src_enetpacket1 = T_enetpacketsrc1 T_arp_src_enetpacket2 = T_enetpacketsrc2 T_arp_src_enetpacket3 = T_enetpacketsrc3 T_arp_src_enetpacket4 = T_enetpacketsrc4 T_arp_src_enetpacket5 = T_enetpacketsrc5 'Set source IP to ARP packet pos 29 T_arp_sipaddr0 = Myip(1) T_arp_sipaddr1 = Myip(2) T_arp_sipaddr2 = Myip(3) T_arp_sipaddr3 = Myip(4) 'Set ARP type from Request to Reply T_arp_op1 = 2 'Send the reply packet Call Enc28j60_packetsend(42) End Sub Sub Pingreply Local Packetlength As Word Packetlength = Buffer(17) * 256 Packetlength = Packetlength + Buffer(18) Packetlength = Packetlength + 13 'We are going to calculate the checksum till the end of packet (IP length + 14 byte of the ethernet stuff), -1 to get word start 'set echo reply T_icmp_type = &H00 T_icmp_code = &H00 'setup the IP-header Call Setipaddrs Call Icmp_checksum Packetlength = Packetlength + 1 'Send the reply packet Call Enc28j60_packetsend(packetlength) End Sub Sub Setip_id Buffer(19) = High(ip_id) Buffer(20) = Low(ip_id) Ip_id = Ip_id + 1 End Sub ' Routine to handle UDP-traffic ' Sub Udp_receive Local Udp_port As Word Udp_port = 5000 Swap Udp_port 'From within a VB-program 'non-echo on PORT 5000 If T_udp_destport = Udp_port Then If T_udp_srcport = &H2500 Then ' 37 NTP protocol Call Ntp Exit Sub End If Select Case T_udp_data Case "1" : Call Blinky(1) Case "2" : Call Blinky(2) Case "3" : Call Blinky(3) Case "4" : Call Blinky(4) Case "5" : Call Blinky(5) End Select Exit Sub End If 'echo on PORT 7 If T_udp_destport = &H0700 Then '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 Echopacket End If End Sub Sub Blinky(l As Byte) Local Count As Byte For Count = 1 To L Reset Led Waitms 250 Set Led Waitms 250 Next Count End Sub ' Routine to calculate a IP-header checksum ' Sub Ip_header_checksum Local Ip_checksum16 As Word Local Ip_header_length As Byte T_ip_hdr_cksum = &H00 'Calc starts with chksum=0 'Calculate IP header length Ip_header_length = T_ip_vers_len And &H0F 'Number of 32 bit words Ip_header_length = 4 * Ip_header_length 'Calc number of bytes Ip_checksum16 = Tcpchecksum(buffer(15) , Ip_header_length ) 'Store the checksum value in the packet buffer T_ip_hdr_cksum1 = High(ip_checksum16) T_ip_hdr_cksum0 = Low(ip_checksum16) End Sub ' Routine to calculate a ICMP-checksum ' Sub Icmp_checksum 'Clear the ICMP checksum before starting calculation T_icmp_cksum = &H00 'Calculate the ICMP checksum I_header_length = T_ip_pktlen1 - 20 I_checksum16 = Tcpchecksum(buffer(35) , I_header_length ) 'built-in way T_icmp_cksum1 = High(i_checksum16) T_icmp_cksum0 = Low(i_checksum16) End Sub ' Routine to echo packet ' Sub Echopacket 'packetlengte Hulp2 = T_udp_len0 * 256 Hulp2 = Hulp2 + T_udp_len1 Hulp2 = Hulp2 + 34 Call Enc28j60_packetsend(hulp2) End Sub ' Routine to handle the source/destination address ' Sub Setipaddrs T_ip_destaddr = T_ip_srcaddr 'make ethernet module IP address source address T_ip_srcaddr = My_ip Call Src2dest 'Set source MAC in ethernet frame, pos 7 Call Src2mymac Call Ip_header_checksum End Sub ' Routine to get the NetWork Time from a time-server ' Sub Ntp_get 'MAC-naglowek 'Docelowy adres sprzetowy ' You have to put your router-mac-address here T_enetpacketdest0 = &HB0 ' B0-48-7A-D6-83-D8 adres mac routera T_enetpacketdest1 = &H48 T_enetpacketdest2 = &H7A T_enetpacketdest3 = &HD6 T_enetpacketdest4 = &H83 T_enetpacketdest5 = &HD8 ' source (own source) Call Src2mymac T_enetpackettype = &H0008 ' = &h0800 ' 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 '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 = 153 ' adres IP serwera NTP ntp.task.gda.pl (153.19.250.123) T_ip_destaddr1 = 19 T_ip_destaddr2 = 250 T_ip_destaddr3 = 123 'UDP-header T_udp_srcport0 = &H13 ' port 5000 T_udp_srcport1 = &H88 T_udp_destport0 = &H00 ' port 0025 = 37 NTP T_udp_destport1 = &H25 T_udp_len0 = &H00 T_udp_len1 = &H1C T_udp_data = Asc( "X") T_udp_data1 = &H0A ' lf T_udp_data2 = &H0D ' cr Call Ip_header_checksum Call Udp_checksum Call Echopacket End Sub ' Routine to convert the LONG from the NTP-server in to date and time ' Sub Ntp 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) End Sub 'Tcp-checksum ' Sub Tcp_checksum Local Whulp1 As Word Local Tempword2 As Word Tcp_cksum = 0 Call Srcdestchksum 'resultaat in I_chksum32 Tempwordh = T_ip_pktlen0 Tempwordl = T_ip_pktlen1 I_chksum32 = I_chksum32 + Tempword Tempword2 = T_ip_vers_len And &H0F Tempword2 = Tempword2 * 4 I_chksum32 = I_chksum32 - Tempword2 Whulp1 = Tempword - 20 Val2 = Highw(i_chksum32) Val1 = I_chksum32 I_checksum16 = Tcpchecksum(buffer(&H23) , Whulp1 , Val2 , Val1) Tcp_cksuml = High(i_checksum16) Tcp_cksumh = Low(i_checksum16) End Sub ' Routine to calculate the IP-checkum ' Sub Udp_checksum T_udp_chksum = &H00 Call Srcdestchksum 'resultaat in I_chksum32 'packet length I_value16h = T_udp_len0 I_value16l = T_udp_len1 I_chksum32 = I_chksum32 + I_value16 Result16h = T_udp_len0 Result16l = T_udp_len1 Val2 = Highw(i_chksum32) Val1 = I_chksum32 I_checksum16 = Tcpchecksum(buffer(&H23) , Result16 , Val1 , Val2) T_udp_chksum1 = High(i_checksum16) T_udp_chksum0 = Low(i_checksum16) End Sub Sub Srcdestchksum '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 End Sub Wyslij_tcp: '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 Swap Tcp_srcportl , Tcp_destportl Swap Tcp_srcporth , Tcp_destporth Set Tcp_flags.4 'ack T_ip_pktlen0 = &H00 T_ip_pktlen1 = 40 Tcp_win_size = 2000 ' ustaw okno na 2000 bajtˇw Swap Tcp_win_size '5 x 4 = 20 bytes Buffer(47) = &H50 'padding Buffer(55) = 0 Buffer(56) = 0 Buffer(57) = 0 Buffer(58) = 0 Buffer(59) = 0 Buffer(60) = 0 Call Ip_header_checksum Call Tcp_checksum Call Enc28j60_packetsend(60) Return Sprawdz_timeout: If Tcp_timeout = 0 Then ' je¬eli timeout (mine-o 3 sekundy) If Stan_tcp <> Listen Then ' sprawdč czy stan TCP jest inny ni¬ LISTEN 'Print "timeout" ' zresetuj po-¦czenie 'MAC-header 'Destination hardware address T_enetpacketdest0 = Src_mac0 ' zapamiŕtany mac klienta T_enetpacketdest1 = Src_mac1 T_enetpacketdest2 = Src_mac3 T_enetpacketdest3 = Src_mac4 T_enetpacketdest4 = Src_mac5 T_enetpacketdest5 = Src_mac5 ' source (own source) Call Src2mymac T_enetpackettype = &H0008 ' = &h0800 ' fill IP-header T_ip_vers_len = &H45 T_tos = &H00 T_ip_pktlen0 = &H00 ' d-ugoťŠ pakietu T_ip_pktlen1 = &H28 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 = &H06 '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 T_ip_destaddr = Src_ip ' zapamiŕtany adres IP ostatniego klienta Tcp_srcporth = 0 ' port 80 Tcp_srcportl = 80 Tcp_destport = Src_port ' zapamiŕtany port klienta Tcp_flags = 0 Set Tcp_flags.4 ' ack Set Tcp_flags.2 ' rst T_ip_pktlen0 = &H00 T_ip_pktlen1 = 40 Tcp_seqnum0 = Incoming_ack0 Tcp_seqnum1 = Incoming_ack1 Tcp_seqnum2 = Incoming_ack2 Tcp_seqnum3 = Incoming_ack3 'Call Inc2seq Tcp_acknum0 = Client_seqnum0 Tcp_acknum1 = Client_seqnum1 Tcp_acknum2 = Client_seqnum2 Tcp_acknum3 = Client_seqnum3 '5 x 4 = 20 bytes Buffer(47) = &H50 'padding Buffer(55) = 0 Buffer(56) = 0 Buffer(57) = 0 Buffer(58) = 0 Buffer(59) = 0 Buffer(60) = 0 Call Ip_header_checksum Call Tcp_checksum Call Enc28j60_packetsend(60) Gosub Czysc_bufor Stan_tcp = Listen Src_port = 0 Src_ip = 0 Tcp_err = 1 End If End If Return Czysc_bufor: Dim Q As Word For Q = 1 To 70 'For Q = 1 To Max_framelen Buffer(q) = 0 Next Q Return 'HTTP ' Sub Http Local Tempb As Byte Local Ackpsh As Byte Local Pkthulp As Word Local Tempword1 As Word Local Tempword3 As Word Local Z As Byte Local Ztemp As Byte Local Msg_temp2 As String * 10 Local Tempstring1 As String * 1 Local Tempstring2 As String * 13 Tempb = Tcp_flags Tcp_fin = Tempb.0 Tcp_syn = Tempb.1 Tcp_rst = Tempb.2 Tcp_psh = Tempb.3 Tcp_ack = Tempb.4 Tcp_urg = Tempb.5 Ackpsh = 0 Tcp_err = 0 Gosub Sprawdz_timeout If Tcp_err = 0 Then If Stan_tcp <> Listen Then ' sprawdč czy stan TCP jest inny ni¬ LISTEN 'If Src_ip <> 0 And Src_port <> 0 Then ' je¬eli Src_ip i src_port s¦ rˇ¬ne od 0 If Src_port <> Tcp_srcport Then ' je¬eli port jest inny od portu aktualnego po-¦czenia If Tcp_syn = 1 And Tcp_ack = 0 Then ' je¬eli nowy klient prˇbuje nawi¦zaŠ po-¦czenie Tcpdatalen_in = 1 ' to odrzuŠ po-¦czenie, wyťlij reset RST 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 Tcp_seqnum0 = &HFF 'Initial sequencenumber Tcp_seqnum1 = &HFF ' Tcp_seqnum2 = &H10 ' Tcp_seqnum3 = &H11 ' Call Src2dest 'Set Flags.synflag Tcp_flags = 0 Set Tcp_flags.2 ' rst Gosub Wyslij_tcp Tcp_err = 1 End If End If End If End If If Tcp_err = 0 Then If T_ip_srcaddr = Src_ip And Tcp_srcport = Src_port Then ' sprawdč czy adres IP i port klienta siŕ zgadza If Tcp_rst = 1 Then Tcp_timeout = 0 Stan_tcp = Listen ' sprawdč czy klient nie zresetowa- po-¦czenia Src_port = 0 Src_ip = 0 Tcp_timeout = 0 Tcp_err = 1 End If End If End If If Tcp_err = 0 Then Select Case Stan_tcp Case Listen ' This code segment processes the incoming SYN from the client ' and sends back the initial sequence number (ISN) and acknowledges ' the incoming SYN packet If Tcp_syn = 1 And Tcp_ack = 0 Then Tcp_timeout = 3 ' ustaw timeout na 3 sekundy Src_mac0 = T_enetpacketsrc0 ' zapamiŕtaj MAC klienta Src_mac1 = T_enetpacketsrc1 Src_mac2 = T_enetpacketsrc2 Src_mac3 = T_enetpacketsrc3 Src_mac4 = T_enetpacketsrc4 Src_mac5 = T_enetpacketsrc5 Src_port = Tcp_srcport ' zapamiŕtaj port klienta Src_ip = T_ip_srcaddr ' zapamiŕtaj adres IP klienta Tcpdatalen_in = 1 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 Tcp_seqnum0 = &HFF 'Initial sequencenumber Tcp_seqnum1 = &HFF ' Tcp_seqnum2 = &H10 ' Tcp_seqnum3 = &H11 ' Call Src2dest 'Set Flags.synflag Tcp_flags = 0 Set Tcp_flags.1 Gosub Wyslij_tcp Incoming_ack0 = Tcp_seqnum0 Incoming_ack1 = Tcp_seqnum1 Incoming_ack2 = Tcp_seqnum2 Incoming_ack3 = Tcp_seqnum3 Stan_tcp = Syn_received End If Case Syn_received If Tcp_timeout > 0 Then If T_ip_srcaddr = Src_ip And Tcp_srcport = Src_port Then ' sprawdč czy adres IP i port klienta siŕ zgadza If Tcp_syn = 0 And Tcp_ack = 1 Then Client_seqnum0 = Tcp_seqnum0 Client_seqnum1 = Tcp_seqnum1 Client_seqnum2 = Tcp_seqnum2 Client_seqnum3 = Tcp_seqnum3 Incoming_ack0 = Tcp_acknum0 Incoming_ack1 = Tcp_acknum1 Incoming_ack2 = Tcp_acknum2 Incoming_ack3 = Tcp_acknum3 Stan_tcp = Established End If End If End If Case Established If Tcp_timeout > 0 Then If T_ip_srcaddr = Src_ip And Tcp_srcport = Src_port Then ' sprawdč czy adres IP i port klienta siŕ zgadza 'If an ACK and PSH is received and the destination port address is valid 'If Tcp_ack = 1 Then If Tcp_psh = 1 And Tcp_ack = 1 Then 'search for GET / and check for a parameter ' ' walk through the packet until GET / is found For Z = 40 To 200 Ztemp = Z Tempstring2 = Chr(buffer(ztemp)) Incr Ztemp Tempstring2 = Tempstring2 + Chr(buffer(ztemp)) Incr Ztemp Tempstring2 = Tempstring2 + Chr(buffer(ztemp)) Incr Ztemp Tempstring2 = Tempstring2 + Chr(buffer(ztemp)) Incr Ztemp Tempstring2 = Tempstring2 + Chr(buffer(ztemp)) Incr Ztemp Tempstring2 = Tempstring2 + Chr(buffer(ztemp)) Incr Ztemp Tempstring2 = Tempstring2 + Chr(buffer(ztemp)) Incr Ztemp Tempstring2 = Tempstring2 + Chr(buffer(ztemp)) Incr Ztemp Tempstring2 = Tempstring2 + Chr(buffer(ztemp)) Incr Ztemp Tempstring2 = Tempstring2 + Chr(buffer(ztemp)) Incr Ztemp Tempstring2 = Tempstring2 + Chr(buffer(ztemp)) Incr Ztemp Tempstring2 = Tempstring2 + Chr(buffer(ztemp)) Incr Ztemp Tempstring2 = Tempstring2 + Chr(buffer(ztemp)) If Left(tempstring2 , 5) = "GET /" Then Incr Licznik_odwiedzin Exit For End If Next Z If Mid(tempstring2 , 6 , 5) = "LEDON" Then Reset Led End If If Mid(tempstring2 , 6 , 6) = "LEDOFF" Then Set Led End If 'set flags Tcp_flags = 0 'swap mac Call Src2dest Call Inc2seq T_ip_pktlen = T_ip_pktlen0 * 256 T_ip_pktlen = T_ip_pktlen + T_ip_pktlen1 T_ip_pktlen = T_ip_pktlen - 20 ' odejmij od ca-kowitej d-ugoťci pakietu IP d-ugoťŠ nag-ˇwka IP (20 bajtˇw) Tempb = Tcp_hdr : Tempb = Tempb And &HF0 : Swap Tempb : Tempb = Tempb * 4 ' oblicz d-ugoťŠ nag-ˇwka tcp T_ip_pktlen = T_ip_pktlen - Tempb ' odejmij od ca-kowitej d-ugoťci pakietu IP d-ugoťŠ nag-ˇwka TCP, wynik to d-ugoťŠ danych Incoming_ack = Incoming_ack + T_ip_pktlen Call Seq2ack Call Ack2inc Gosub Wyslij_tcp Ackpsh = 1 End If 'End If 'If Tcp_ack = 1 Then 'If Buffer(17) = 0 Then 'If Buffer(18) = 40 Then If Ackpsh = 1 Then 'ACK after a ACK-PSH 'set flags Tcp_flags = 0 Set Tcp_flags.3 'psh Set Tcp_flags.0 'fin Set Tcp_flags.4 'ack Tempword3 = &H37 Restore Htmlcode Do Read Msg_temp Msg_temp2 = Right(msg_temp , 8) If Msg_temp2 = "endblock" Then Exit Do End If If Msg_temp2 = "sizeused" Then Tempword1 = Tempword3 + 24 Msg_temp = "<br>Used: " + Str(tempword1) + " bytes" End If If Msg_temp2 = "counter " Then Msg_temp = "<br>Licznik odwiedzin: " + Str(licznik_odwiedzin) End If If Msg_temp2 = "date " Then Msg_temp = Date(l2) + " " End If If Msg_temp2 = "time " Then Msg_temp = Time(l2) + " " End If If Msg_temp2 = "copyleft" Then Msg_temp = "<br>www.members.home.nl/bzijlstra<br>" End If For Y = 1 To Len(msg_temp) Tempstring1 = Mid(msg_temp , Y , 1) Buffer(tempword3) = Asc(tempstring1) Incr Tempword3 Next Y Loop Buffer(tempword3) = &H0D Incr Tempword3 Buffer(tempword3) = &H0A Tempword3 = Tempword3 - 14 Pkthulp = Tempword3 / 256 T_ip_pktlen0 = Pkthulp Pkthulp = T_ip_pktlen0 * 256 T_ip_pktlen1 = Tempword3 - Pkthulp Tempword3 = Tempword3 + 14 Call Ip_header_checksum Call Tcp_checksum Call Enc28j60_packetsend(tempword3) Client_seqnum0 = Tcp_acknum0 Client_seqnum1 = Tcp_acknum1 Client_seqnum2 = Tcp_acknum2 Client_seqnum3 = Tcp_acknum3 Incoming_ack0 = Tcp_seqnum0 Incoming_ack1 = Tcp_seqnum1 Incoming_ack2 = Tcp_seqnum2 Incoming_ack3 = Tcp_seqnum3 Stan_tcp = Close_wait End If 'End If 'End If 'End If End If End If Case Close_wait If Tcp_timeout > 0 Then If T_ip_srcaddr = Src_ip And Tcp_srcport = Src_port Then ' sprawdč czy adres IP i port klienta siŕ zgadza If Tcp_ack = 1 Then Stan_tcp = Last_act Client_seqnum0 = Tcp_seqnum0 Client_seqnum1 = Tcp_seqnum1 Client_seqnum2 = Tcp_seqnum2 Client_seqnum3 = Tcp_seqnum3 Incoming_ack0 = Tcp_acknum0 Incoming_ack1 = Tcp_acknum1 Incoming_ack2 = Tcp_acknum2 Incoming_ack3 = Tcp_acknum3 End If End If End If Case Last_act If Tcp_timeout > 0 Then If T_ip_srcaddr = Src_ip And Tcp_srcport = Src_port Then ' sprawdč czy adres IP i port klienta siŕ zgadza 'If Tcp_ack = 1 Then If Tcp_fin = 1 Then 'set flags Tcp_flags = 0 'swap mac Call Src2dest Tcpdatalen_in = 1 Client_seqnum0 = Tcp_seqnum0 Client_seqnum1 = Tcp_seqnum1 Client_seqnum2 = Tcp_seqnum2 Client_seqnum3 = Tcp_seqnum3 Client_seqnum = Client_seqnum + Tcpdatalen_in Tcp_seqnum0 = Tcp_acknum0 Tcp_seqnum1 = Tcp_acknum1 Tcp_seqnum2 = Tcp_acknum2 Tcp_seqnum3 = Tcp_acknum3 Tcp_acknum0 = Client_seqnum0 Tcp_acknum1 = Client_seqnum1 Tcp_acknum2 = Client_seqnum2 Tcp_acknum3 = Client_seqnum3 Gosub Wyslij_tcp Stan_tcp = Listen Src_port = 0 Src_ip = 0 Tcp_timeout = 0 End If 'End If End If End If End Select Gosub Czysc_bufor End If End Sub ' TCP ' Sub Tcp If Tcp_destporth = 0 Then If Tcp_destportl = 80 Then Call Http End If End If End Sub Sub Inc2seq Incoming_ack0 = Tcp_seqnum0 Incoming_ack1 = Tcp_seqnum1 Incoming_ack2 = Tcp_seqnum2 Incoming_ack3 = Tcp_seqnum3 End Sub Sub Seq2ack Tcp_seqnum0 = Tcp_acknum0 Tcp_seqnum1 = Tcp_acknum1 Tcp_seqnum2 = Tcp_acknum2 Tcp_seqnum3 = Tcp_acknum3 End Sub Sub Ack2inc Tcp_acknum0 = Incoming_ack0 Tcp_acknum1 = Incoming_ack1 Tcp_acknum2 = Incoming_ack2 Tcp_acknum3 = Incoming_ack3 End Sub Sub Src2mymac '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 Sub Src2dest '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 Call Src2mymac End Sub '************ htmlcode Htmlcode: Data "HTTP/1.0 200" , &H0D , &H0A , &H0D , &H0A Data "<html><head><meta http-equiv=" , &H22 Data "Pragma" , &H22 , " Content=" , &H22 , "no cache" , &H22 , ">" Data "<title>Tux</title></head><body>" Data "<img src=" , &H22 , "http://members.home.nl/bzijlstra/einstein.jpg" , &H22 , ">" Data "<body link=" , &H22 , "#FFFFFF " , &H22 , "vlink=" , &H22 Data "#FFFFFF " , &H22 , "alink=" , &H22 , "#FFFFFF>" Data "<div align=" , &H22 , "left" , &H22 , ">" Data "<table border=" , &H22 , "1" , &H22 , " width=" , &H22 , "21%" , &H22 , ">" Data "<tr><td width=" , &H22 , "98" , &H22 , " align=" , &H22 , "left" , &H22 , ">" Data "<p align=" , &H22 , "center" , &H22 , ">" Data "<a href=" , &H22 , "LEDON" , &H22 Data "><font color=" , &H22 , "#990033" , &H22 , ">LED ON</font></a></td>" Data "<td align=" , &H22 , "left" , &H22 , ">" Data "<p align=" , &H22 , "center" , &H22 , ">" Data "<a href=" , &H22 , "LEDOFF" , &H22 , "><font color=" Data &H22 , "#008080" , &H22 , ">LED OFF</font></a></td>" Data "</tr></table></div>" Data "sizeused" Data "counter " Data "endblock" '------------------------------------------------------------------------------- 'przerwanie TIMER1 OVF Odmierz1s: Timer1 = 3036 If Tcp_timeout <> 0 Then Decr Tcp_timeout ' je¬eli tcp_timeout jest rˇ¬ne od zera to zmniejsz o 1 If Countdown1 <> 0 Then Decr Countdown1 ' je¬eli Countdown1 jest rˇ¬ne od zera to zmniejsz o 1 Return


Download file - link to post
  • Serwer_ENC28J60.zip
    • Atmega_1284P_Serwer_HTTP_v1.bas
    • enc28j60.inc