Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

ARM HOW-TO GUIDE

Interfacing SPI-
Ethernet with LPC2148
Contents at a Glance
ARM7 LPC2148 Slicker Board ...........................................3
SPI (Serial Peripheral Interface)........................................3
Ethernet ..........................................................................4
The ENC28J60 Ethernet Controller ...................................4
Interfacing SPI-Ethernet ...................................................5
Interfacing SPI-Ethernet with LPC2148 .............................7
Pin Assignment with LPC2148 ..........................................7
Circuit Diagram to Interface SPI-Ethernet with LPC2148 ...8
Source Code ....................................................................8
C Program to interface Ethernet with LPC2148 .................9
Testing the SPI-Ethernet with LPC2148 ........................... 16
General Information ...................................................... 17

Join the Technical Community Today!


http://www.pantechsolutions.net
ARM7 LPC2148 Slicker Board

The ARM7 LPC2148 Slicker board is specifically


designed to help students to master the required skills in
the area of embedded systems. The kit is designed in such
way that all the possible features of the microcontroller will
be easily used by the students. The kit supports in system
programming (ISP) which is done through serial port.

NXP’s ARM7 (LPC2148), ARM Slicker Kit is proposed to


smooth the progress of developing and debugging of
various designs encompassing of High speed 32-bit
Microcontrollers.

SPI (Serial Peripheral Interface)

Serial Peripheral Interface (SPI) is a synchronous serial


data protocol used by microcontrollers for communicating
with one or more peripheral devices quickly over short
distances. It can also be used for communication between
two microcontrollers.

Join the Technical Community Today!


http://www.pantechsolutions.net
Ethernet

Ethernet is the most widely-installed local area network


(LAN) technology. An Ethernet LAN typically uses coaxial
cable or special grades of twisted pair wires. Ethernet is also
used in wireless LANs. The most commonly installed
Ethernet systems are called 10BASE-T and provide
transmission speeds up to 10 Mbps. Devices are connected
to the cable and compete for access using a Carrier Sense
Multiple Access with Collision Detection (CSMA/CD)
protocol.

The ENC28J60 Ethernet Controller

Microchip’s ENC28J60 controller is a 28-pin, 10BASE-T


standalone Ethernet Controller, with on board MAC & PHY,
8 Kbytes of Buffer RAM and an SPI serial interface used as
an Ethernet network interface for any microcontroller
equipped with SPI interface. So the microcontroller can
then control remotely any hardware.

Join the Technical Community Today!


http://www.pantechsolutions.net
Interfacing SPI-Ethernet

Fig. 1 shows how to interface the SPI-Ethernet to


microcontroller. With an SPI connection there is always one
master device (usually a microcontroller) which controls the
peripheral devices. Typically there are three lines common
to all the devices,

Master In Slave Out (MISO) - The Slave line for


sending data to the master,

Master Out Slave In (MOSI) - The Master line for


sending data to the peripherals,

Serial Clock (SCK) - The clock pulses which


synchronize data transmission generated by the master,
and

Slave Select pin - the pin on each device that the


master can use to enable and disable specific devices. When
a device's Slave Select pin is low, it communicates with the
master. When it's high, it ignores the master.

Join the Technical Community Today!


http://www.pantechsolutions.net
This allows you to have multiple SPI devices sharing the
same MISO, MOSI, and CLK lines.

Fig. 1 Interfacing SPI-Ethernet to Microcontroller


The Ethernet buffer contains transmit and receive
memory used by the Ethernet controller. The entire buffer
is 8 Kbytes, divided into separate receive and transmit
buffer spaces. The sizes and locations of transmit and
receive memory are fully programmable by the host
controller using the SPI interface. Any space within the 8-
Kbyte memory, which is not programmed as part of the
receive FIFO buffer, is considered to be the transmit buffer.

Join the Technical Community Today!


http://www.pantechsolutions.net
Interfacing SPI-Ethernet with LPC2148

In SPI, the clock signal is controlled by the master


device LPC2148 Slicker Board. All data is clocked in and out
using this pin. These lines need to be connected to the
relevant pins on the LPC2148 Slicker Board. Any unused GIO
pin can be used for CS, instead pull this pin high. The
ENC28J60 requires a single per packet control byte to
precede the packet for transmission to Microcontroller. An
IP address is used to access the Ethernet control. The
ENC28J60 SPI connections with LPC2148 have four I/O lines
required.

Pin Assignment with LPC2148


LPC2148
SPI Connector
Processor Lines
P0.4
UART0(P1)

SCK
ISP PGM

MOSI P0.5

MISO P0.6
UART1
(P2)

CS P0.7

Join the Technical Community Today!


http://www.pantechsolutions.net
Circuit Diagram to Interface SPI-Ethernet with LPC2148

+5V
3.3V
+5V

C20
D12 D13 J10 0.1uF U23

20
LED LED +3.3V
63
51
43
23
7

U16 2 18

VCC
1 2 ETH_3.3V 3 A0 B0 17
VDD1
VDD2
VDD3
VDDA
VREF

C19 A1 A2 3 4 4 A1 B1 16 INT*_ETH
R49 1K ETH_INT
6 10UF 5 6 5 A2 B2 15 WOL*_ETH

11
22
21
18
ETH_WOL

2
18 VSS1 U18 R50 R56 7 8 6 A3 B3 14 MISO_ETH
25 VSS2 1 330E 330E R58 100E 7 A4 B4 13
R52 1K

VSS
VSSRX

VSSPLL
VSSTX
VSSOSC
42 VSS3 14 VCAP MISO 8 A5 B5 12
+5V
50 VSS4 RBIAS 27 9 A6 B6 11
59 VSS5 3 LEDA 26 C21 22pf HEADER 4X2 A7 B7
VSSA 41 INT*_ETH 4 CLKOUT LEDB R61 1

GND
LPC2148 P0.14 45 WOL*_ETH 5 /I/N/T 24 10K 19 DIR
P0.15 /WO/O/L OSC2

25MHz
30 MISO_ETH 6 23 Y2 G
P0.6 29 R55 100E 7 SO OSC1

10
P0.5 27 R57 100E 8 SI ENC28J60 28 74LS245
P0.4 38 9 SCK VDD 25 ETH_CS
C22 22pf
P0.12 39 10 /C/S VDDOSC 20

TPOUT+
P0.13 RESET VDDPLL

TPOUT-
19
XTAL2

XTAL1

RD- 12 VDDRX 15
RD+ 13 TPIN- VDDTX
TPIN+ ETH_3.3V
61

62

16
17
12MHz ETH_3.3V
TD+
TD-

C62 X26 C63

A2
L2
22pf 22pf F-BEAD
TD+

11
12
U21
1
K2
A2
R53 56E 4 TD+
2 CT
TD- RJ45
3 8
R54 56E 5 RD+ GND
TD-
6 CT
A1
K1

RD+ RD-
9
10

R59 56E
C23
0.1uF
A1

R60 56E
RD-

C24
0.1uF

Source Code

The Interfacing UART with LPC2148 program is very


simple and straight forward, which controls the LED &
Switches in LPC2148 Slicker Board from Ethernet through
SPI. In C programs are written in Keil software. When we
select LED or Switch in Internet Explorer by using IP address
then the output is enabled in LPC2148 controller.

Join the Technical Community Today!


http://www.pantechsolutions.net
C Program to interface Ethernet with LPC2148

***************************************************************************************
Title : Program to control LED & Switches from Ethernet using SPI
***************************************************************************************

#include <LPC214X.H>
#include "string.h"

#include "ENC28J60.h"
#include "c_ctype.h"
#include "conv.h"
#include "Utility.h"

#define ENC28J60_HALFDUPLEX 0
#define ENC28J60_FULLDUPLEX 1

const unsigned char httpHeader[] = "HTTP/1.1 200 OK\n


Content-type: "; // HTTP header
const unsigned char httpMimeTypeHTML[] = "text/html\n\n";

const unsigned char httpMimeTypeScript[] = "text/plain\n\n";


unsigned char httpMethod[] = "GET /";

const char *indexPage = "<HTML><HEAD></HEAD><BODY>\


<h1> ARM& LPC214x </h1>\
<h2> WEB SERVER </h2>\
<a href=/>Reload</a>\
<script src=/s></script>\
<table><tr><td valign=top><table border=2 style=
\"font-size:25px ;font-family: terminal ;\">\
<tr><th colspan=3>ADC</th></tr>\
<tr><td>AN0</td><td><script>document.write(AN0)
</script></td></tr>\
<tr><td>AN1</td><td><script>document.write(AN1)
</script></td></tr>\

Join the Technical Community Today!


http://www.pantechsolutions.net
</table></td><td><table border=1 style=
\"font-size:15px ;font-family: terminal ;\">\
<tr><th colspan=3>P1[31..24]</th></tr>\
<script>\
var str,i;\
str=\"\";\
for(i=0;i<8;i++)\
{str+=\"<tr><td bgcolor=blue>BUTTON #\"+i+\"</td>\";\
if(PORT1_31_24&(1<<i)){str+=\"<td bgcolor=green>ON\";}\
else {str+=\"<td bgcolor=#cccccc>OFF\";}\
str+=\"</td></tr>\";}\
document.write(str) ;\
</script>\
</table></td><td>\
<table border=2 style=\"font-size:15px ;font-family:
terminal ;\">\
<tr><th colspan=4>P1[23..16]</th></tr>\
<script>\
var str,i;\
str=\"\";\
for(i=0;i<8;i++)\
{str+=\"<tr><td bgcolor=orange>LED #\"+i+\"</td>\";\
if(PORT1_23_16&(1<<i)){str+=\"<td bgcolor=blue>ON\";}\
else {str+=\"<td bgcolor=#cccccc>OFF\";}\
str+=\"</td><td><a href=/t\"+i+\">Toggle</a></td></tr>\";}\
document.write(str) ;\
</script>\
</table></td></tr></table>\
This is HTTP request
#<script>document.write(REQ)</script></BODY></HTML>\
";

unsigned char myMacAddr[6] = {0x00, 0x1f, 0xD0, 0xE3,


0x90, 0xDD}; // my MAC address
unsigned char myIpAddr[4] = {192, 168, 1, 60}; //IP address
unsigned char getRequest[15]; // HTTP request buffer
unsigned char dyna[31]; // buffer for dynamic response
unsigned long httpCounter = 0; // counter of HTTP requests

Join the Technical Community Today!


http://www.pantechsolutions.net
int main (void)
{
PINSEL0 = 0;
PINSEL1 = 0;
PINSEL2 &= 0x0000000C;
delay_Nx10cyc(599999); // Delay 0,1s

IODIR1 &= 0x00FFFFFF; // Set P1[31..24] as inputs


IODIR1 |= 0x00FF0000; // Set P1[23..16] as outputs
IOCLR1 |= 0x00FF0000; // Turn off LEDs

// starts ENC28J60 with : RST bit on IOPIN0.13,


CS bit on IOPIN0.12, my MAC & IP address, full duplex
ENC28J60_Init(&IOPIN0, 13, &IOPIN0, 12, myMacAddr,
myIpAddr, ENC28J60_FULLDUPLEX) ;

while (1)
{
ENC28J60_doPacket(); // incoming Ethernet packets
/*
* add your stuff here if needed
* ENC28J60_doPacket() must be called as often as possible
* otherwise packets could be lost
*/
}
}

unsigned long putConstString(const char *s)


{
unsigned long ctr;

ctr = 0;
while(*s)
{
ENC28J60_putByte(*s++);
ctr++;
}
return(ctr);
}

Join the Technical Community Today!


http://www.pantechsolutions.net
unsigned long putString(char *s)
{
unsigned long ctr;

ctr = 0;
while(*s)
{
ENC28J60_putByte(*s++);
ctr++;
}
return(ctr);
}

unsigned long ENC28J60_userTCP(unsigned char *remoteHost,


unsigned long remotePort, unsigned long localPort,
unsigned long reqLength)
{
unsigned long
len, // my reply length
i, // general purpose integer
bitMask; // for bit mask

i = (unsigned long) remoteHost;


i = remotePort;
i = reqLength;

len = 0;
bitMask = 0;

if (localPort != 80)
return(0) ;

for (i = 0; i < 10; i++)


getRequest[i] = ENC28J60_getByte();
getRequest[i] = 0;

if (memcmp(getRequest, httpMethod, 5))


return(0);

Join the Technical Community Today!


http://www.pantechsolutions.net
httpCounter++; // one more request done

if (getRequest[5] == 's')
{
len = putConstString(httpHeader); // HTTP header
len += putConstString(httpMimeTypeScript);
IntToStr(0, dyna);
len += putConstString("var AN0=");
len += putString(dyna);
len += putConstString(";");

// add AN1 value to reply


IntToStr(0, dyna);
len += putConstString("var AN1=");
len += putString(dyna);
len += putConstString(";");

// add PORT1[31..24] value (buttons) to reply


len += putConstString("var PORT1_31_24=");
IntToStr(((IOPIN1 >> 24) & 0xFF), dyna);
len += putString(dyna);
len += putConstString(";");

// add PORT1[23..16] value (LEDs) to reply


len += putConstString("var PORT1_23_16=");
IntToStr(((IOPIN1 >> 16) & 0xFF), dyna);
len += putString(dyna);
len += putConstString(";");

// add HTTP requests counter to reply


IntToStr(httpCounter, dyna);
len += putConstString("var REQ=");
len += putString(dyna);
len += putConstString(";");
}
else if (getRequest[5] == 't')
{
if (isdigit(getRequest[6]))
{
bitMask = getRequest[6] - '0';// ASCII to integer

Join the Technical Community Today!


http://www.pantechsolutions.net
bitMask = 1 << (bitMask + 16);// create bit mask
if ((IOPIN1 & bitMask) != 0) // Toggled LED
IOCLR1 |= bitMask;
else
IOSET1 |= bitMask;
}
}

if (len == 0) // what do to by default


{
len = putConstString(httpHeader); // HTTP header
len += putConstString(httpMimeTypeHTML);// MIME type
len += putConstString(indexPage); // HTML page
}

return (len);
}

unsigned long ENC28J60_userUDP(unsigned char *remoteHost,


unsigned long remotePort, unsigned long destPort,
unsigned long reqLength)
{
unsigned long
len; // my reply length
unsigned char
*ptr;
ByteToStr(remoteHost[0], dyna); // first IP address byte
dyna[3] = '.';
ByteToStr(remoteHost[1], dyna + 4); // second
dyna[7] = '.';
ByteToStr(remoteHost[2], dyna + 8); // third
dyna[11] = '.';
ByteToStr(remoteHost[3], dyna + 12); // fourth

dyna[15] = ':'; // add separator

IntToStr(remotePort, dyna + 16);


dyna[22] = '[';
IntToStr(destPort, dyna + 23);
dyna[29] = ']';

Join the Technical Community Today!


http://www.pantechsolutions.net
dyna[30] = 0;

// the total length of the request is the length of the


dynamic string plus the text of the request
len = 30 + reqLength;

// puts the dynamic string into the transmit buffer


ptr = dyna;
while (*ptr)
ENC28J60_putByte(*ptr++);

// then puts the request string converted into upper char


into the transmit buffer
while (reqLength--)
ENC28J60_putByte(toupper(ENC28J60_getByte()));

return (len); // back to the library with the length of the


UDP reply
}

To compile the above C code you need the KEIL software.


They must be properly set up and a project with correct
settings must be created in order to compile the code. To
compile the above code, the C file must be added to the
project.

In KEIL, you want to develop or debug the project


without any hardware setup. You must compile the code for

Join the Technical Community Today!


http://www.pantechsolutions.net
generating HEX file. In debugging Mode, you want to check
the port output without LPC2148 Slicker Board.

The Flash Magic software is used to download the hex


file into your microcontroller IC LPC2148 through UART0.

Testing the SPI-Ethernet with LPC2148

Give +3.3V power supply to LPC2148 Slicker Board; the


SPI-Ethernet is connected with LPC2148 Slicker Board.
Connect your board to a hub with a straight cable. LEDA
should now turn on with LEDB still blinking. LEDA ON means
that the adapter is correctly linked to the network. The
network link LED of the other side hub should also turn on.

Open the Internet Explorer window and give the IP


address. If the entire connections are connected correctly,
then the IP address display the LED, switch levels.

Now you can control the input & output port lines (LED
& switch) of LPC2148 Slicker Board from Internet Explorer

Join the Technical Community Today!


http://www.pantechsolutions.net
through SPI - Ethernet. If you are not reading any output
from LED, then you just check the jumper connections &
check the LED is working.

If you are not controlled the I/O port lines of LPC2148


Slicker Board from Internet Explorer, then you just check
the IP address & Ethernet connections. Otherwise you just
check the code with debugging mode in KEIL. If you want to
see more details about debugging just see the videos in
below link.

 How to Create & Debug a Project in KEIL.

General Information

For proper working use the components of exact values


as shown in Circuit file. Wherever possible use new
components.

Join the Technical Community Today!


http://www.pantechsolutions.net
Solder everything in a clean way. A major problem
arises due to improper soldering, solder jumps and
loose joints.
Use the exact value crystal shown in schematic.
The straight cable only used between LPC2148 Slicker
Board hub & the network cable hub.

Don't plug the ENC28J60 in its socket, then power to


the board and verify the 3.3V power supply on each pin
of the ENC.
More instructions are available in following articles,

 User Manual of LPC2148 Slicker Board.


 Tutorial of how to create & Debug a project in
KEIL.
 Interfacing LED with LPC2148.
 Interfacing switch with LPC2148.

Join the Technical Community Today!


http://www.pantechsolutions.net
Did you enjoy the read?
Pantech solutions creates information packed technical
documents like this one every month. And our website is a rich
and trusted resource used by a vibrant online community of
more than 1,00,000 members from organization of all shapes
and sizes.

Join the Technical Community Today!


http://www.pantechsolutions.net
What do we sell?
Our products range from Various Microcontroller
development boards, DSP Boards, FPGA/CPLD boards,
Communication Kits, Power electronics, Basic electronics,
Robotics, Sensors, Electronic components and much more . Our
goal is to make finding the parts and information you need
easier and affordable so you can create awesome projects and
training from Basic to Cutting edge technology.

Join the Technical Community Today!


http://www.pantechsolutions.net

You might also like