'buffer boundaries applied to internal 8K ram 'entire available packet buffer space is allocated Txstart_init = 0x1800 ' start TX buffer at 0 Rxstart_init = 0x0000 ' give TX buffer space for one full ethernet frame (~1500 bytes) Rxstop_init = 0x17FE ' receive buffer gets the rest ... Sub Enc28j60init() NextPacketPointer = Rxstart_init 'do bank 0 stuff 'initialize receive buffer '16-bit transfers, must write low byte first 'set receive buffer start address Value = Low(Rxstart_init) Enc28j60writecontrolregbyte(Erxstl , Value) Value = High(Rxstart_init) Enc28j60writecontrolregbyte(Erxsth , Value) 'set receive pointer address Value = Low(Rxstart_init) Enc28j60writecontrolregbyte(Erxrdptl , Value) Value = High(Rxstart_init) Enc28j60writecontrolregbyte(Erxrdpth , Value) 'set receive buffer end Value = Low(Rxstop_init) Enc28j60writecontrolregbyte(Erxndl , Value) Value = High(Rxstop_init) Enc28j60writecontrolregbyte(Erxndh , Value) 'set transmit buffer start Value = Low(Txstart_init) Enc28j60writecontrolregbyte(Etxstl , Value) Value = High(Txstart_init) Enc28j60writecontrolregbyte(Etxsth , Value) 'do bank 2 stuff 'enable MAC receive Value = 0 'Value.macon1_marxen = 1 Value = set_Bit_in_Byte(Value,Macon1_marxen,1) 'Value.macon1_txpaus = 1 Value = set_Bit_in_Byte(Value,Macon1_txpaus,1) 'Value.macon1_rxpaus = 1 Value = set_Bit_in_Byte(Value,Macon1_txpaus,1) Enc28j60writecontrolregbyte(Macon1 , Value) 'bring MAC out of reset 'enable automatic padding and CRC operations Value = 0 'Value.Macon3_padcfg0 = 1 Value = set_Bit_in_Byte(Value,Macon3_padcfg0,1) 'Value.Macon3_txcrcen = 1 Value = set_Bit_in_Byte(Value,Macon3_txcrcen,1) 'Value.Macon3_frmlnen = 1 Value = set_Bit_in_Byte(Value,Macon3_frmlnen,1) 'Duplex 0=Halb 1=Full Value = set_Bit_in_Byte(Value,Macon3_fuldpx,0) Enc28j60writecontrolregbyte(Macon3 , Value) 'set inter-frame gap (non-back-to-back) Enc28j60writecontrolregbyte(Maipgl , &H12) Enc28j60writecontrolregbyte(Maipgh , &H0C) 'set inter-frame gap (back-to-back) Enc28j60writecontrolregbyte(Mabbipg , &H12) 'set the maximum packet size which the controller will accept Value = Low(max_framelen) Enc28j60writecontrolregbyte(Mamxfll , Value) Value = High(max_framelen) Enc28j60writecontrolregbyte(Mamxflh , Value) 'bank 3 stuff Enc28j60writecontrolregbyte(Maadr5 , Ethaddr0) Enc28j60writecontrolregbyte(Maadr4 , Ethaddr1) Enc28j60writecontrolregbyte(Maadr3 , Ethaddr2) Enc28j60writecontrolregbyte(Maadr2 , Ethaddr3) Enc28j60writecontrolregbyte(Maadr1 , Ethaddr4) Enc28j60writecontrolregbyte(Maadr0 , Ethaddr5) 'no loopback of transmitted frames Enc28j60writephyword(Phcon2 , Phcon2_hdldis) 'switch to bank 0 Enc28j60selectbank(0) 'enable interrupts Value = 0 'Value.Eie_intie = 1 Value = set_Bit_in_Byte(Value,Eie_intie,1) 'Value.Eie_pktie = 1 Value = set_Bit_in_Byte(Value,Eie_pktie,1) Enc28j60bitfield_set(Eie , Value) 'enable packet reception Value = 0 'Value.Econ1_rxen = 1 Value = set_Bit_in_Byte(Value,Econ1_rxen,1) Enc28j60bitfield_set(Econ1 , Value) End Sub Sub Enc28j60packetreceive() Dim Wtemp As Word Dim Rxstat As Word Dim x_int As Integer Dim i As Integer 'Reset Led 'Print "EPKTCNT = " ; Bin(enc28j60_data) 'set the read pointer to the start of the received packet Value = Low(NextPacketPointer) Enc28j60writecontrolregbyte(Erdptl , Value) 'Call Value = High(NextPacketPointer) Enc28j60writecontrolregbyte(Erdpth , Value) 'Call 'Print "Present nextpacketpntr " ; Hex(nextpacketptr) 'Enc28j60_cs = 0 Port_WriteBit(13,1) 'PortB.5 = CS - SD Card - 0 = selected Slave Port_WriteBit(14,0) 'PortB.6 = CS - ENC28J60 - 0 = selected Slave AbsDelay(1) 'Send Read Buffer Memory command 'Spdr = &H3A SPI_Write(&H3A) SPI_ReadBuf(Buffer,6) 'Integer = MSB * 256 + LSB '1. Word NextPacketPointer = Buffer(1) * 256 NextPacketPointer = NextPacketPointer + Buffer(0) '2. Word Length = Buffer(3) * 256 Length = Length + Buffer(2) '3. Word Rxstat = Buffer(5) * 256 Rxstat = Rxstat + Buffer(4) 'Msg_WriteText("New Nextpacketpntr ") 'Msg_WriteHex(Nextpacketptr) 'Msg_WriteText("Packetlength = ") 'Msg_WriteInt(Length) 'Msg_WriteText("Rxstat = ") 'Msg_WriteInt(Rxstat) 'Get the payload Length = Length - 4 'Discard CRC SPI_ReadBuf(Buffer,Length) 'Enc28j60_cs = 1 Port_WriteBit(13,1) 'PortB.5 = CS - SD Card - 0 = selected Slave Port_WriteBit(14,1) 'PortB.6 = CS - ENC28J60 - 0 = selected Slave 'move the rx read pointer to the start of the next received packet 'this frees the memory we just read out 'Errata workaround #13, Packetpointer must Not contain even number If (NextPacketPointer - 1) >= Rxstop_init Then '0xFFFF Then 'enc28j60Write(ERXRDPTL, LO(RXSTOP_INIT)) 'enc28j60Write(ERXRDPTH, HI(RXSTOP_INIT)) Value = Low(Rxstop_init) Enc28j60writecontrolregbyte(Erxrdptl , Value) 'Call Value = High(Rxstop_init) Enc28j60writecontrolregbyte(Erxrdpth , Value) 'Call NextPacketPointer = Rxstop_init Else Value = Low(NextPacketPointer) Enc28j60writecontrolregbyte(Erxrdptl, Value) Value = High(NextPacketPointer) Enc28j60writecontrolregbyte(Erxrdpth, Value) End If 'decrement the packet counter indicate we are done with this packet 'enc28j60WriteOp(ENC28J60_BIT_FIELD_SET, ECON2, ECON2_PKTDEC) Enc28j60bitfield_set( Econ2, Econ2_pktdec) Value = Low(NextPacketPointer) Enc28j60writecontrolregbyte(Erxrdptl , Value) 'Call Value = High(NextPacketPointer) Enc28j60writecontrolregbyte(Erxrdpth , Value) 'Call 'decrement the packet counter indicate we are done with this packet Value = 0 Value = set_Bit_in_Byte(Value,Econ2_pktdec,1) Enc28j60bitfield_set(Econ2 , Value) 'Call 'Set Led ''Print the packet content 'Print "The packet:" 'For X = 1 To Length 'Print Hex(buffer(x)) ; " " ; 'Next 'X 'Print ' Serial_Write(0,Ethernet_Packet_Count) ' Serial_Write(0,Length) ' For i = 0 To 60 ' Serial_Write(0,Buffer(i)) ' Next 'Serial_Write(0,LF) 'Handle the packet If Buffer(12) = &H08 Then If Buffer(13) = &H06 Then 'Type:ARP If Buffer(20) = &H00 Then 'ARP request If Buffer(21) = &H01 Then 'ARP request If Buffer(38) = Ipaddr0 Then 'ARP for us If Buffer(39) = Ipaddr1 Then If Buffer(40) = Ipaddr2 Then If Buffer(41) = Ipaddr3 Then Arpreply() 'Call End If End If End If End If End If End If End If If Buffer(13) = &H00 Then 'Type:IP If Buffer(14) = &H45 Then 'We handle only simple IP packets If Buffer(20) = 0 Then 'We handle only non fragmented packets If Buffer(30) = Ipaddr0 Then 'Ip packet for us If Buffer(31) = Ipaddr1 Then If Buffer(32) = Ipaddr2 Then If Buffer(33) = Ipaddr3 Then If Buffer(23) = 1 Then 'Protocol:ICMP If Buffer(34) = &H08 Then 'ICMP echo request Pingreply() 'Call End If End If If Buffer(23) = 17 Then 'Protocol:UDP End If If Buffer(23) = 6 Then 'Protocol:TCP End If End If End If End If End If End If End If End If End If Lab Ende: End Sub