PIC18Fxxxx / ENC28J60 Proof of Concept UDP Client and UDP Server Solution 28 August 2006 2003-2006 Andrew Smallridge asmallri@brushelectronics.com www.brushelectronics.com This Technical note describes the Brush Electronics PIC18Fxxxx / ENC28J60 Proof of Concept UDP Client and UDP Server Solution. This proof of concept (POC) solution comprises the PIC software, implementing a UDP Client and UDP Server on a Microchip PIC18F series microcontroller with an associated Microchip ENC28J60 Ethernet controller, and PC software implementing a UDP client application. A UDP client implemented on a PIC requires more resources from the PIC to implement than a UDP server application. A client requires more RAM, Program Memory and a richer implementation of the IP protocol stack. The UDP server implementation described in this technical note is more flexible than the standalone UDP server described in the Brush Electronic s technical note PIC18Fxxxx / ENC28J60 Proof of Concept UDP Server Solution as a result of the more extensive protocol implementation in this version. The IP stack used in the proof of concept solution has been developed by Brush Electronics. Considerable effort has been made to simplify the developers interface by off loading tasks from the developer to the driver and by simplifying the state machine. For example, the user s application program does not need to resolve the IP to MAC address binding before attempting to send a UDP packet. The driver will attempt to resolve the binding from the ARP cache and, if no matching entry is found, will automatically perform ARP discover. The driver also avoids the lock-step problem than can occur when an application is blocking pending an ARP resolution which cannot be resolved because other frames proceed the required ARP response packet in the Ethernet controller s receive buffer. Protocols supported in this implementation include: DHCP client ARP Request ARP Response ICMP Echo Request (ping) ICMP Echo Reply (ping reply) UDP Client UDP Server An ARP cache has been implemented with appropriate aging timers. Copyright 2004-2006 Andrew Smallridge Page 1 of 6
Note: For applications that have severe resource constraints, the reader may want to investigate the implementation of a tiny UDP client as described in the Brush Electronic s Technical note Implementing a tiny UDP client with UDP Server Code. The PIC code includes the necessary network protocols to implement a UDP client and UDP server application enabling the PIC to service requests from one or more network clients. The PIC code implements three UDP sockets, two sockets for the UDP server (listens to two UDP ports) and one socket for the UDP Client function. The two UDP server sockets are the CONTROL port and the DATA port. The CONTROL port (0x4000) is used for receiving commands from the PC POC application. The DATA port (0x4001) is used for receiving data from the PC POC application. The UDP CLIENT port (0x5000) is used to connecting to a remote UDP server application. The supplied PC software implements the UDP client and UDP Server functions. The PC software UDP server client function is hard coded to use the IP address of 192.168.2.200 as the IP address for the PIC however, on reception of a UDP packet from the PIC to the UDP Server port on the PC, the target address of the PC s UDP Client sockets (control and data) will remap to use this learnt address for the PIC. If the proof of concept UDP Client and UDP Server application is used in conjunction with the Brush Electronic s Ethernet Bootloader, and the IP address of the Bootloader is to be the same as the IP address of the UDP application, then the MAC address used by the PIC software should be changed to be the same as the MAC address used by the Ethernet Bootloader. This will simplify application development and debugging negating the need to clear the controlling hosts ARP cache whenever the PIC is rebooted. PIC software user interface: The PIC software uses the serial port for accepting commands from the user, displaying status information in debug mode, and displaying messages sent over the data socket from the PC application. Serial port configuration: 115200bps, 8 data bits, 1 stop bit, no parity. The following five serial interface commands are supported by the PIC software: ARP Displays the contents of the ARP cache including the age of the entry in minutes and the time in minutes since the entry was last accessed. The Last Access time is used by the PIC software to remove least recently used entries from the ARP table in the event the ARP table is full. ARP Table: IP Addr MAC Addr AGE Last Access 192.168.002.254 00:14:6C:AC:6A:38 0 1 192.168.002.051 00:11:2F:84:5D:55 0 0 Copyright 2004-2006 Andrew Smallridge Page 2 of 6
ARP d Deletes the contents of the ARP cache. ARP Table: IP Addr MAC Addr AGE Last Access ECHO The character string immediately following the ECHO keyword is sent, via a UDP data packet, to the PC POC application s data socket. The message will be displayed in the DATA Channel window. Note that the PC POC application must have previously sent a packet to the PIC to enable the PC s IP address and PC s UDP source port number to be learnt by the PIC.. DEBUG Puts the PIC application into debug mode. Debug mode provides diagnostic information to the serial port on receipt of network traffic. INIT Reinitializes the ENC28J60 Ethernet controller subsystem. IPCONFIG Displays IP address configuration parameters. If DHCP is enabled, this command also displays the DHCP timers. IP Config: Assigned IP addr: 192.168.2.3 Assigned GW addr: 192.168.2.254 Assigned Mask: 255.255.255.0 Derived subnet: 192.168.2.0 DHCP Timers: Lease=82202, T1=39002, T2=71402 PING Issues an ICMP Echo Request (ping). By your command> ping 66.102.7.104 By your command> ICMP Echo Reply from 66.102.7.104 time=291ms RESET Resets (reboots) the PIC hardware including the Ethernet controller. SEND Sends a string via UDP to a designated IP address (demonstrates UDP client). send 192.168.2.100 Hello Copyright 2004-2006 Andrew Smallridge Page 3 of 6
UNDEBUG Exits debug mode. The following commands are DHCP specific. These commands would typically be used for debugging DHCP and are available when the code has been compiled with the #define USE_DHCP_CLIENT directive. LEASE Set the DHCP Lease time to 10 seconds T1 Set the DHCP T1 timer to 10 seconds. T2 Set the DHCP T2 timer to 10 seconds. RENEW Forces a DHCP RENEW cycle. PC Proof of Concept Application This PC application is a multithreaded application implementing UDP Server and UDP Client sockets. The PC application implements two UDP Client sockets to support the PIC s UDP Server Control and Data ports. The PC application also implements a UDP Server socket to support the PIC s UDP Client s Client port. PC application s Control and Data UDP Client sockets destination IP address of the PIC is hard coded to 192.168.2.200 however will automatically be remapped to the source address of the first UDP packet received from the PIC s UDP Client on the PC s UDP Server port. This enables the PC application to dynamically reconfigure itself to map the outgoing destination IP address to the IP address of the PIC. The application is implemented with Borland s C++ compiler. The user interface is shown in the following screen capture: Copyright 2004-2006 Andrew Smallridge Page 4 of 6
The PC POC application sends and receives data via the windows sockets interface. One UDP client socket, designated the control socket, is used for sending commands to the PIC. Another UDP client socket, designated the data socket, is used for exchanging data strings with the PIC s serial port. A UDP Server socket is used to display strings sent from the PIC via the PIC s SEND command. The PC POC application has nine visual controls: Data Sequence (Edit box): Characters to be send to the PIC s serial port are entered into this field. The characters are transmitted via the data socket when the Send Data button is clicked. Send Data (Button): The string in the Data Sequence edit box is sent via the data socket to the PIC s serial port. Copyright 2004-2006 Andrew Smallridge Page 5 of 6
UDP Server (from Remote UDP client) (Memo): This windows displays the payload contents of UDP packets received sent from the PIC using the SEND command. Data Channel (Memo): This windows displays the payload contents of UDP data packets received from the PIC. This is the character sequence immediately following the ECHO keyword received by the PIC on its serial interface. Control Channel (Memo): Displays status information about packets received from the PIC via the control socket. LIA Status (Button): Initiates the status request command UDP control message which is sent via the control socket to the PIC. The PIC will respond with a Hello, world message embedded in the UDP payload of a packet sent to the control port. This message will be displayed in the Control Channel window. Dummy (Button): Initiates a command UDP control message which is sent via the control socket to the PIC however, in this case, the command is not a valid command. The PIC will respond with a UNKNOWN command received message embedded in the UDP payload of a packet sent to the control port. This message will be displayed in the Control Channel window. RESET (Button): Initiates the reset request command UDP control message sent via the control socket to the PIC. The PIC will respond with a RESET command received message embedded in the UDP payload of a packet sent to the control port and, after a two second delay, will reset (reboot) the PIC. This message will be displayed in the Control Channel window. Clear Screen (Button): This button will clear the Data Channel and Control Channel windows. No network traffic is generated. Copyright 2004-2006 Andrew Smallridge Page 6 of 6