Ethernet

Today's embedded systems designers and developers are increasingly asked to incorporate Ethernet connectivity into their systems.

TCP/IP, a widely deployed standard, lets you connect and control devices and communicate with software on almost every operating system over most transport media. Microcontroller A key to embedded connectivity is the infrastructure. Most workplaces have Ethernet networks to plug devices into.

Ethernet is currently the most commonly used Local Area Network (LAN). A LAN is a network of computers that covers a small area like a room, an office, a building or a campus. It is used in contrast with WAN (wide area network) which spans for much larger geographical areas. Ethernet is a network protocol that controls how data is transmitted over a LAN. Technically it is referred to as IEEE 802.3.

Embedded Ethernet

Embedded Ethernet is a single-chip implementation of the Ethernet networking standard. It is used to connect devices such as environmental monitors, sensors, media streaming devices, and Webcams directly to an Ethernet LAN without requiring a computer for connection. Which brings us to the point of this article ...

PIC Ethernet v1.0

PIC Ethernet v1.0 is a small Assembler code implementation of Ethernet designed to exchange data between a PIC microcontroller and a client over Ethernet. The project consists of a main program, a packet processor, optional peripheral routines, and protocol functions to transmit packets and display status information. PIC FilesSupported protocols include ARP, DHCP, TCP/IPv4, and HTTP.

The PIC microcontroller serves a webpage to clients on the network which allows monitoring and controlling of the PIC via the HTTP POST request method.

The source code can be modified to work with most any implementation via various calls to external code throughout the program. Display and port configuration code can easily be substituted to work with almost any peripheral device or implementation.

PIC Ethernet v1.0 consists of eight source files. The main procedure, PIC_Ethernet, intializes the hardware, services the calls to intialize the peripherals, leases an IP from the network, and monitors network traffic and connection status. Packets.asm is the packet processor which processes all incoming packets, detects the type of protocol, and calls the appropriate routine to handle the payload. Support for protocols like UDP, ICMP, FTP, and others can easily be included and assembled with little to no modification to the original project code.PIC Memory Data

Since PIC Ethernet v1.0 is written in Assembler a minimal amount of memory is required and uses approximately 200 bytes of data memory and about 21kB of program memory.

1. The Microcontroller

I developed this code on Microchip's PIC18F97J60 microcontroller. The 18F97J60 is a 100-Pin High-Performance, 1-Mbit Flash Microcontroller with an integrated IEEE 802.3 compatible Ethernet controller and is fully compatible PIC18F97J60PQFP-100with 10/100/1000 Base-T networks. The Ethernet module contains an integrated MAC and 10Base-T PHY. In Ethernet the MAC and PHY make up the Physical Layer.

The PIC18F97J60 has 128Kb of program flash memory, 3808 bytes of SRAM data memory, and 8192 bytes of Ethernet buffer space.

2. Operation

Upon power up, or Reset, the hardware and the Ethernet controller are initialized. The Physical Layer and MAC are configured for full duplex operation.

Next, a DHCP Discovery packet is sent out on the network. Providing there is a DHCP server and a valid response is detected, a DHCP Request is sent. Initially, the requested IP is 0.0.0.0, however, once on the network, subsequent IP lease requests use the last IP used. Once an IP lease has been negotiated, the Renewal (T1) Time Value is stored into a 32 bit asynchronous decade counter. The counter is cleared and the count is started and will continue to count until the count matches the Renewal Time Value or a Reset occurs.

After the DHCP process three ARP probes, each separated by differing lengths of time, are sent out to check for a duplicate IP. If a duplicate IP is detected, a software Reset is initiated and the DHCP process is restarted. If no IP conflict is detected a Gratuitous ARP is sent out to announce the current IP is referenced to the current MAC.

The Main program loop begins.

3. The Code

The first initialization is to configure the ports. I do this by calling an external function contained in Ports.asm. In this version, built specifically for the PIC-WEB-EK development board, PortJ is used as an 8-bit status register that indicates initial and runtime status. PortH is dedicated to the LCD control bus. PortG.5 is used to indicate whether the code is running in the Main procedure loop or servicing packets. PortF is configured as an input port to read 4 switches that send status data to the display and portJ. PortE is the LCD data bus. PortA.0 and 1 are used for the Ethernet LEDs.

Basically, I use two of the micrcontroller's timers, Timer0 and Timer1. Timer0 is used for a programmable time delay routine called throughout the program whenever a time delay is needed. Timer1 is used as both a random number generator for the DHCP XID value and a timer to sequence Gratuitous ARPs and DHCP lease renwals. I use the 4-byte Lease Renewal value from the DHCP-ACK packet that was received from the DHCP server in response to the DHCP-Request previously sent. Timer1 begins decrementing this value until zeroed out. The Main function then sends a new DHCP-Request.