Posted: December 14, 2019
A few years ago I got a SparkFun breakout board for a Microchip ENC28J60 Ethernet chip. My goal for it was just to send a UDP packet from an MSP430G2553 microcontroller to my Linux desktop. I actually built the circuit a few years ago, but was having trouble with the SPI so I put it away for a while. A couple days ago I took the board out and realized I had a couple mistakes in my code, fix it, and got it working.
I included the source code in the samples/msp430 directory of naken_asm git repository (linked to below). There's also an enc28j60.inc include file for all the definitions in the include/ethernet directory.
Here's a picture of the Ethernet interface board soldered to the circuit I built around the MSP430G2553. The whole circuit is connected by the TEST, /RST, GND, and Vcc wires to an MSP430G Launchpad so I can power, program the flash, and debug it.
Probably the start of this project is creating a IP/UDP packet to test with. I wrote a C program to make it a little easier and originally had the checksum for the IP header hardcoded. I also decided to not include a UDP checksum (by setting that 16 bit field to 0). The current MSP430 assembly code computes and sets the IP header checksum. The ENC28J60 chip also doesn't send an Ethernet frame header on its own so I had to hardcode that too, along with a control byte the ENC28J60 chip uses to know if it needs to add an Ethernet checksum and such. The hardcoded Ethernet frame looks like this:
The destination MAC address is the address of my desktop and the source MAC address is just something I made up. When making up the MAC address, it seems the first byte has to be an even number or it's treated as a multicast.
To test the firmware, on my Linux desktop I ran netcat with:
I also used Wireshark for some debugging when things weren't working, but in the end netcat was enough. If it appeared in Wireshark, it showed up from netcat.
Communication with the chip is actually not too bad... once getting past all the awkward stuff. There are multiple sets of registers that are accessed in different ways. When reading from a MAC / MII register there needs to be a dummy byte read in before the real data shows up. Also the PHY registers are accessed through some M based registers in an odd way. The chip's registers are also "banked" so in order to access the correct register, the bank has to be set. Kind of reminds me of Microchip's 8 bit PIC chips.
To send an Ethernet frame, first the frame needs to be transferred to the 8k buffer in the chip. A pointer to where to write the next byte to is set to where the packet should be placed. To transmit the packet, a pointer to the start packet must be set along with a pointer to the last byte in the packet.
So after getting the SPI working and proving that after I wrote to a register that I read the same value back that I wrote to it, I was having trouble seeing the packet being received in Wireshark. I thought maybe (although it didn't make sense) that I might have needed to set up an arp mapping on the desktop, so I did the arp -s thing to add the MAC address of 192.168.2.15. When that didn't work, I tried a delay between setup and transmitting a packet and that seemed to fix it. What the issue was is the PHY needs to be polled to know if it's in a ready state. I added code to do that and it appears to fix it. Now I kind of don't know if the arp -s thing was really needed. I'd put money down that it's not. It seems that arp should be more for trying to figure out the MAC address for a specific IP address. I already knew the destination MAC address so arp seems pointless.
Copyright 1997-2020 - Michael Kohn