This tutorial was extracted from Erich Styger blog http://mcuoneclipse.wordpress.com with his agreement.
In Part 1 of this series I have covered the SD card on the Arduino Ethernet shield. In Part 2 I’m hooking up the board to the network and will be able to ping it.
List of Tutorials
- FRDM with Arduino Ethernet Shield R3, Part 1: SD Card
- FRDM with Arduino Ethernet Shield R3, Part 2: Ping
- FRDM with Arduino Ethernet Shield R3, Part 3: Embedded Web Server
Hardware
The W5100 has a TCP/IP stack built into the hardware, which makes it easy to use with any small microcontroller, including the Freescale KL25Z on the FRDM-KL25ZFRDM-KL25Z board. TheArduino Ethernet Shield R3Arduino Ethernet Shield R3 I’m using requires a hardware change to map the SPI signals to the Arduino header: See Part 1 of this series.
SPI Communication
The W5100 uses the ‘Mode 0′ SPI protocol:
- Clock Idle polarity low
- Data shifted on rising edge
After activivating the CS (Chip Select, LOW active) line, a command byte (0xF0 for write, 0x0F for read) is sent, followed by the address (16bit, most significant byte first), and then the data byte. Within a byte, the MSB (Most Significant Bit) is sent first.
Write
The screenshot below shows the write command 0xF0 which writes the value 0xC0 to address 0×0001:
So a transaction is always 32 bits: 1 command byte, 2 address bytes, one data byte. As you can see, the W5100 responds on the MISO line with increasing values to the write command.
Read
Reading from the device is very similar. For this the command 0x0F is used. Below is the read operation which reads back the value from address 0×0001:
Chip Select
The chip select for the W5100 is low active, and to abstract from the hardware, I’m using defines:
1 2 |
|
Semaphore
As the SPI bus is shared between the SD card and W5100, I’m using a semaphore to grant mutual access to the bus:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
SPI Read and Write
Using the Processor Expert SPI component, dealing with the SPI protocol gets really simple. I need to catch the hook for the finished SPI transaction and wait until the transaction is finished:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
This code polls for the DataReceivedFlag. To make the code more robust, a timeout should be used. I keep things simple here.
Read/Write Registers
To read and write the W5100 registers, this can be done like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
Read/Write Multiple Bytes
To read or write more than one byte, the single byte read/write routines are used:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
An example to read/write multiple bytes is for the network configuration:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
I will cover this more later.
Application Initialization
With this, we are ready to configure the W5100.
Mode Register
The W5100 has the MR (Mode Register) register with the following bits:
1 2 3 4 5 6 |
|
Of interest is the RST (Reset) bit. Setting it to one will reset the device:
1 2 3 4 5 |
|
Network Address
Next, I need to configure the network address information. For this I need to configure the gateway, the network mask, IP address and the MAC (Hardware Address). The MAC address I can find on the sticker of my Ethernet Shield:
With this, I set up my network configuration like this:
1 2 3 4 5 6 |
|
And configure it:
1 2 3 4 5 |
|
RX/TX Memory Buffer
The W5100 can deal with up to 4 network sockets, with a total of 2×8 KByte for RX (RMSR register) and TX (TMSR register) buffers. Default is to use 2 KByte for each socket:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
There are other things to configure like number of retry, but I keep them on the default values for now.
Status Output
I have added a command line shell interface to the W5100 driver, so I can inspect my network configuration:
Ping
Finally, time to see if the W5100 responds on ping commands. For this I ‘ping’ the IP address I have assigned:
Congratulations!
If pinging does not work: check the network address and network setup. If the W5100_MR_BIT_PB bit is set in the W5100_MR register, then the W5100 does *not* respond to pings!
Summary
I have now the FRDM-KL25Z hooked up to the network with the Arduino Ethernet shield. It does not much for now, it only responds to pings, but this is a good starter . The software project is available on GitHub.
I recommend to have a look this very good blog post by rwb.