Sunday, July 8, 2012

ENC28J60 - PSoC3 - Part 4 Packet Handling & ARP

We'll talk about the first steps into networking with the ENC28J60.

For Transmitting a packet,
You create a packet (based on its established structure,depending on the protocol etc) and then write that to the ENC28J60's buffer,and have it sent.Check the status vectors to see how that transmission went.



For processing replies to packets that you may have sent,you poll the EPKTCNT register,and if that happens to be non zero,read in the packet adjusting the necessary buffer pointers,checking the quality of the received packet using the status vectors.


So those will be our two basic functions,apart from the initialization one,of course.

For a great introduction to the Address Resolution protocol (popularly shortened to 'ARP') and its packet format,I suggest you visit this page from the 'The TCP/IP Guide'.Its truly a one-of-a-kind reference for TCP/IP stuff on the internet,and I highly recommend reading it whenever in doubt.

What ARP basically does is,it finds the MAC Address for a particular IP address on a local area network.
So,say my router(or any connected network device.The router is just an example) has the IP address 192.168.1.1,and my ENC28J60 is 192.168.153.If I(the ENC chip),wants to find out the MAC address of the router,I send out an ARP Request,to which I get an ARP reply from the router,which will bear its MAC Address.

Why would I(the ENC chip) need to know the MAC Address of the connected router? Well,future packet transactions will require this basic information.

Since packets are basically structured data,a very natural way to represent them in code,is via structures.For an ARP packet,we define the following structs:

The ethernet header will have the DestAddrs as FF:FF:FF:FF:FF:FF,since we 'broadcast' our ARP request.The SrcAddrs will be our ENC chip's MAC address,since it is the source of the request.The type field will be set to 0x0806,since its a packet of type ARP.

For the rest of the fields,after the IP header,we set the hardware to 6,since we are talking about a LAN ..basically an IEEE 802 network.
The protocol will be IPv4,which means we set protocol to 0x800 which corresponds to the EtherType code for the Internet Protocol.
hardwareSize is the length of the MAC Address in bytes.It shall be 6.
protocolSize is the length of the IP Address in bytes.For us,its IPv4,so 4.
opCode decides if its an ARP Request or Reply.Since we will be putting in a request,it should be 0x0001.For a Reply,that is 0x0002.
senderMAC is the ENC's MAC Address.senderIP is the ENC's IP.
targetMAC will be kept at 00:00:00:00:00:00,since we do not know this,at this point.The last field,is the targetIP one,which will be the IP Address of the device whose MAC Address you want to find.

So,after declaring a variable of type ARP and filling its fields with relevant info,all we need to do is call the MACWrite function,and have that packet sent.
After we've sent it out,we wait for a reply in an endless loop,and process the reply when it comes in.Heres what the code for that looks like:

Thats all theres to it,for an ARP Request.
Heres what a Wireshark session with that ARP request looks like
You may check out the ARP example in my github repo,here.

We'll do ICMP(Ping) and then UDP in the coming posts.

No comments:

Post a Comment