Basic Web Server Development
Basic Web Server Development
Introduction
Ethernet connectivity of embedded devices is a growing trend in industrial and consumer applications.
Ethernet is a medium of choice because of its competitive performance, relatively low price of
implementation, established infrastructure, and inter-operability. Ethernet is also easy to use, widely
available, and scalable. Ethernet is described by IEEE Standard 802.3™ .
With Ethernet and TCP/IP data transmission, embedded devices can be connected to the Internet, which
allows access to the embedded device from across the world. Figure 1 shows a simplified illustration of
an embedded device that is connected to a remote host via the Internet. Figure 1 shows that the
embedded device and remote host can operate on different networks, but the connection between the
devices is transparent.
EMBEDDED DEVICE
CONTROL INTERNET
BOARD
MOTOR
REMOTE HOST
Basic Web Server with MC9S12NE64 and CMX-MicroNet TCP/IP Stack, Rev. 0.3
2 Freescale Semiconductor
Scope of This Application Note
This application note details the creation of a basic web server for an embedded device. The application
note is a follow-up to AN2624/D: Basic Web Server Development with the CMX-MicroNet™ TCP/IP Stack.
CMX-MicroNet development with the MC9S12NE64 is very similar to CMX-MicroNet development
MC9S12E128 Ethernet reference design. The discussion will provide an overview of development with
the MC9S12NE64 and the CMX-MicroNet TCP/IP stack. This application note specifically addresses the
following:
• MC9S12NE64 microcontroller unit (MCU)
• Axiom EVB9S12NE64 evaluation board
• CMX-MicroNet TCP/IP stack
This document was created to help familiarize first-time users of the MC9S12NE64 and CMX-MicroNet
TCP/IP stack with the development environment. This understanding will help speed initial MC9S12NE64
and CMX-MicroNet TCP/IP stack application development. To do this, a walk-through of developing a
simple web server is provided. This also includes reviewing some basics of the CMX-MicroNet API and
its Metrowerks® CodeWarrior® project organization. Network-specific acronyms and terms used in this
document are described in Table 1.
Figure 2 is a simplified diagram of the web server that will be developed. This diagram shows a PC remote
host that requests a web page from an embedded device running on a CMX-MicroNet web server. The
embedded device is able to serve the requested information back to the PC. The remote host can also
GET and POST information to the embedded device.
CMX-MicroNet
SERVES HTML
ANY WEB BROWSER WEB SERVER
APPLICATION
HTTP GET/POST
HTML
VIRTUAL FILE
Basic Web Server with MC9S12NE64 and CMX-MicroNet TCP/IP Stack, Rev. 0.3
Freescale Semiconductor 3
Scope of This Application Note
Connectivity systems that use the TCP/IP stack model (see TCP/IP Stack Model Refresher), such as the
example in Figure 1, can be implemented for a wide range of applications, including:
• Database data logging or queries
• Web servers for remote embedded devices
• Remote monitoring (data collection/diagnostics)
• Remote control of devices in the field
• Use of email by remote device
• Remote reprogramming of FLASH memory
The TCP/IP stack model is derived from the OSI 7-layer communications development methodology. The
TCP stack model defines both TCP/IP stack software and the network interface (as shown in Figure 3).
In this discussion, the network interface is Ethernet, which is implemented by the MC9S12NE64
integrated Ethernet controller and Ethernet controller device drivers.
A TCP/IP stack defines a set of protocols that allows network devices to connect to a specific device and
exchange data on a network. These protocols, defined by RFC (request for comments), enable an
embedded device to send email, serve web pages, transfer files, and provide other basic connectivity
functions. Figure 3 is a simplified illustration of a user application working through the TCP stack model
and illustrates how a TCP/IP stack and the MC9S12NE64 Ethernet controller fit into the system.
USER APPLICATION
APPLICATION API
DHCP/FTP/HTTP
TCP STACK
SOCKET API
SOFTWARE
TCP/UDP/IP
DEVICE DRIVER API
MC9S12NE64 INTERGRATED
ETHERNET CONTROLLER MC9S12NE64 NETWORK INTERFACE
AND PHYSICAL INTERFACE
NETWORK
Basic Web Server with MC9S12NE64 and CMX-MicroNet TCP/IP Stack, Rev. 0.3
4 Freescale Semiconductor
MC9S12NE64 MCU with Integrated Ethernet Controller
This section introduces the MC9S12NE64 MCU and provides a brief overview of the MC9S12NE64
Ethernet controller.
MC9S12NE64
The MC9S12NE64 is a 16-bit MCU based on Freescale Semiconductor’s HCS12 CPU platform. It
includes 8K of RAM and 64K of FLASH. In the 80-pin TQFP-EP package, the MC9S12NE64 has other
standard on-chip peripherals including two asynchronous serial communications interface modules
(SCIs), a serial peripheral interface (SPI), an inter-integrated circuit bus (IIC), a 4-channel/16-bit timer
module (TIM), an 8-channel/10-bit analog-to-digital converter (ATD), and up to 18 pins available as
keypad wake-up inputs (KWUs). In addition, an expanded bus that can be operated at 16 MHz1 is
available.
The MC9S12NE64 introduces a new peripheral for the HCS12 CPU platform, the integrated Ethernet
controller. The MC9S12NE64 integrates an Ethernet controller that includes a media access controller
(MAC) and a physical transceiver (PHY) in one die with the CPU, memory, and other HCS12 standard
on-chip peripherals. The MC9S12NE64 integrated Ethernet controller is compatible with IEEE 802.3 and
802.3u specifications for 10-Mbps or 100-Mbps operation, respectively.
The MC9S12NE64 can be targeted at low-throughput connectivity applications that require operation
from a 3.15-V to 3.45-V external supply range. With an on-chip bandgap-based voltage regulator (VREG),
the internal digital supply voltage of 2.5 V (VDD) can also be generated.
A block diagram of the MC9S12NE64 is provided in Figure 4. More information on the MC9S12NE64 is
available from the Freescale Semiconductor website: http://freescale.com.
1. At a 16-MHz internal bus speed, the MC9S12NE64 integrated Ethernet controller is limited to 10-Mbps operation. A 25-MHz
internal bus speed is required for 100-Mbps operation.
Basic Web Server with MC9S12NE64 and CMX-MicroNet TCP/IP Stack, Rev. 0.3
Freescale Semiconductor 5
Axiom Ethernet Development Board for the MC9S12NE64, EVB9S12NE64
This section describes the EVB9S12NE64 and how it must be configured for the web server
demonstration.
EVB9S12NE64
Axiom Manufacturing provides the EVB9S12NE64, a fully featured development board for the
MC9S12NE64. The EVB9S12NE64, shown in Figure 5, includes the following:
• MC9S12NE64 single chip Ethernet solution
• RJ45 connector with integrated Ethernet high-speed LAN magnetics isolation module
• 25-MHz crystal
• Reset button
• BDM connector
• Two RS-232C interfaces (with one configurable to an IrDA transceiver)
• Four user LEDs
• Four user buttons
• Potentiometer
• 256K external RAM (accessible via the external bus)
• Prototype area
• Access to all MC9S12NE64 pins
Basic Web Server with MC9S12NE64 and CMX-MicroNet TCP/IP Stack, Rev. 0.3
6 Freescale Semiconductor
Axiom Ethernet Development Board for the MC9S12NE64, EVB9S12NE64
See the Axiom Manufacturing website, http://www.axman.com, for more information. Because the
MC9S12NE64 connects directly to an Ethernet connector and high-speed LAN magnetic isolation
module, the MC9S12NE64 is a true single-chip Ethernet system solution.
The PCB version number of the EVB9S12NE64 used in this application note is revision B. For this version
of EVB9S12NE64, important jumper settings for the EVB9S12NE64 are provided in Table 2. The
simplified layouts of the EVB9S12NE64 top layer and board silkscreen are provided in Figure 6.
The EVB9S12NE64 must be configured for normal single-chip mode (MODA=MODB=0, MODC=1) for
the CMX-MicroNet stack operation. For detailed information about this evaluation board, see the
EVB9S12NE64 user manual from the Axiom Manufacturing web site.
Basic Web Server with MC9S12NE64 and CMX-MicroNet TCP/IP Stack, Rev. 0.3
Freescale Semiconductor 7
Axiom Ethernet Development Board for the MC9S12NE64, EVB9S12NE64
Basic Web Server with MC9S12NE64 and CMX-MicroNet TCP/IP Stack, Rev. 0.3
8 Freescale Semiconductor
CMX Stack Overview
CMX Introduction
CMX-MicroNet is a TCP/IP stack implementation that is tailored for 8-bit and 16-bit embedded
processors. Other CMX features include:
• Compatible with CodeWarrior tools
• Works with MCUs with low RAM/ROM resources
• Written entirely in standard C code
• Allows web pages to contain CGI calls
• Allows sending email
• Can serve Java applets
• Runs stand-alone or with a real-time operating system (RTOS)
• Supports as many as 16 sockets (mixed or matched with TCP or UDP)
To reduce the CMX-MicroNet code footprint in FLASH, ROM, and RAM, CMX-MicroNet has made several
TCP/IP stack design choices that deviate from TCP/IP’s RFC standards while still maintaining high
TCP/IP stack functionality. These include:
• No support for IEEE 802.3 type packets
• No IP option support
• No support to handle fragmented packets
• ICMP supports only echo reply
• Ignores all TCP options
• TCP respects other side’s window, but uses a fixed window itself
• Every TCP packet must be acknowledged with an ACK before another one can be received
Basic Web Server with MC9S12NE64 and CMX-MicroNet TCP/IP Stack, Rev. 0.3
Freescale Semiconductor 9
CMX Stack Overview
Powering the CMX-MicroNet TCP/IP stack is a low-level Ethernet driver for the MC9S12NE64 integrated
Ethernet controller. This driver is integrated within the CMX-MicroNet TCP/IP stack source code.
However, a Freescale Semiconductor stand-alone (without a TCP/IP stack) version of the low-level
Ethernet driver is available for stand-alone development.
CMX delivers CMX-MicroNet source code as an installation file. After this file is installed, several
directories are placed on the target PC. Figure 7 illustrates the CMX-MicroNet project directories that are
installed. The CMX-MicroNet main project directory is {Project Directory}\NE64_MICRONET, where
{Project Directory} is the installation directory.
Basic Web Server with MC9S12NE64 and CMX-MicroNet TCP/IP Stack, Rev. 0.3
10 Freescale Semiconductor
CMX Stack Overview
Sub-Directory Description
{Project Directory}\NE64_MICRONET\netlib Contains the source files for the CMX-MicroNet API.
Contains the HTML, JPG, GIF, and other files developed for the
{Project Directory}\NE64_MICRONET\Webpage
embedded web server.
More information on examplee_ne64.mcp and netlibe_ne64.mcp and their individual files is provided in
the following sections. Figure 8 shows both of these projects opened in the CodeWarrior IDE (integrated
development environment).
Basic Web Server with MC9S12NE64 and CMX-MicroNet TCP/IP Stack, Rev. 0.3
Freescale Semiconductor 11
CMX Stack Overview
The netlibe_ne64.mcp project creates a library (object) file of the CMX-MicroNet API, and the
examplee_ne64.mcp project contains the specific user application. This section covers these projects and
specific files in each project. The specific files that are discussed are the files that will likely require
modification for the simple web server described by this application note.
Basic Web Server with MC9S12NE64 and CMX-MicroNet TCP/IP Stack, Rev. 0.3
12 Freescale Semiconductor
CMX Stack Overview
Examplee Project (examplee_ne64.mcp) — Contains the source code of the specific user application.
The source code in this project includes main() for the TCP/IP stack solution.
• example.c — Contains the user application including main().
• callback.c — Among other things, contains settings for:
– MAC hardware and IP addresses
– SMTP (simple mail transfer protocol) IP address
– Gateway IP address
– Subnet mask
• _MC9S12NE64_BANKED.prm — Contains Ethernet controller buffer map configuration settings,
BUFMAP. The user must modify this file to the user’s desired buffer size settings for the Ethernet
controller buffers. This setting should match the BUFMAP value in emac_fifo_cfg.h.
• Files in webpage directory — Contain HTML, GIF, JPG, Java applets, and other files used in the
web server. This directory also contains the files that result when html2C.exe is executed (see
Table 3).
Table 4 provides a brief description of the CMX-MicroNet API functions that are required to develop the
simple web server described in this application note. For complete documentation of the CMX-MicroNet
API, please reference the CMX-MicroNet user guide.
Basic Web Server with MC9S12NE64 and CMX-MicroNet TCP/IP Stack, Rev. 0.3
Freescale Semiconductor 13
CMX Stack Overview
Function Description
These are virtual file system functions that are located in vfile.c in the
netlibe_ne64.mcp project. These functions are designed to make it
mn_vf_set_entry(arguments) easy to retrieve arrays associated with web pages and function
mn_pf_set_entry(arguments)
mn_gf_set_entry(arguments) pointers used by server-side-includes and HTTP post routines. For
mn_gf_set_entry, the #define SERVER_SIDE_INCLUDES code in
the mnconfig.h file must be set to 1.
Located in http.c. For HTTP functionality, the main web page must be
mn_http_find_value(arguments) called index.htm or index.html (unless the main page name is
mn_http_set_file(arguments)
mn_http_set_message(arguments) changed in http.c by modifying the default_page1 or default_page2
variable names).
Basic Web Server with MC9S12NE64 and CMX-MicroNet TCP/IP Stack, Rev. 0.3
14 Freescale Semiconductor
Preparing for CMX-MicroNet TCP/IP Stack Development
This section describes how to prepare for web server development with CMX-MicroNet software. The
following topics are included:
• Development environment and tools
• Connecting the evaluation board to a development PC
• Configuring the MAC hardware and IP addresses
• Configuring TCP/IP protocol in Microsoft Windows® operating system
The CMX-MicroNet TCP/IP stack software can be modified and compiled with the CodeWarrior
environment. Below are specific details about the development environment and tools required to develop
the web server described in this application note.
• Microsoft Windows 2000 with Microsoft Internet Explorer 5.5 or later
• Microsoft FrontPage® 2000 for web page development
• CodeWarrior HCS12 tool version 2 (with MC9S12NE64 patch) or later
• P&E BDM MultiLink® pod, category 5 (cat5) crossover cable, and optional DB9 serial cable (as
described in the next section)
Figure 9 shows the basic connection of a PC running CodeWarrior software to the EVB9S12NE64. For
development, a PC must connect to the target board with the following:
• BDM MultiLink pod (BDM) — Provides a link to the embedded device and provides an interface to
program and debug the software on the MC9S12NE64 MCU. On the EVB9S12NE64 evaluation
board, the BDM connector is labeled BDM_PORT.
• Crossover cat5 cable (XCAT5) — Required to form a local, isolated network between the PC and
the target. With an Ethernet link, the network application can be tested and debugged on a network.
Alternatively, a straight-through cable with a hub can be used.
• DB9 serial cable (COMM) — CMX-MicroNet program has a debug mode that sends real-time
messages about stack activity through one of the MC9S12NE64 SCI ports. By using
HyperTerminal1 and a serial cable, these debug messages can be captured.
Basic Web Server with MC9S12NE64 and CMX-MicroNet TCP/IP Stack, Rev. 0.3
Freescale Semiconductor 15
Preparing for CMX-MicroNet TCP/IP Stack Development
DEVELOPMENT DEVELOPMENT
HOST TARGET
SERIAL COMM
PORT
BDM EVB9S12NE64
PC PARALLEL
PORT
NETWORK XCAT5
PORT (RJ45)
This development setup should be used until the CMX-MicroNet application is completed. This provides
an easier interface for debugging the application than connecting directly to a real network (because the
development target is isolated). To make the application compatible with a real network, changes should
be required to only the MAC hardware and IP addresses.
After the web server is complete, it can be configured to operate on a real network by changing the MAC
hardware and IP addresses to be compatible with the real network. A brief discussion of MAC hardware
and IP addresses is provided in the following sections.
A valid MAC hardware address for the EVB9S12NE64 should be assigned by the developer. This address
is used by the datalink layer, which is implemented by the MC9S12NE64 integrated Ethernet controller
and the low-level drivers. This can be configured in callback.c. If the device is not connected to a real
network, a random MAC hardware address can be used as long as it is not connected on a network that
has a device with the same 48-bit MAC hardware address.
When developing an application off a real network and on a developer’s PC, the developer is the network
administrator. The developer must create a local network between the PC and the target board. A network
consists of nodes that are on the same network subnet. To set up the subnet for the demo, the developer
must use compatible IP address settings between the developer’s PC and the target board.
Basic Web Server with MC9S12NE64 and CMX-MicroNet TCP/IP Stack, Rev. 0.3
16 Freescale Semiconductor
Preparing for CMX-MicroNet TCP/IP Stack Development
When setting up IP addresses, it is preferable to configure or use the development target and
development host manually with non-routable IP addresses (i.e., 10.x.x.x, 90.0.0.x, 172.16.x.x through
172.32.x.x, or 192.168.x.x).
NOTE
These IP settings and others must be reflected in the Windows network
settings.
When the application is developed and ready for a real network, the IP address settings must be
configured to be compatible with the real network on which the node will reside. Recall, for the
CMX-MicroNet code, IP addresses and MAC hardware addresses are configured in callback.c. In this
web server example, a static IP address is used. For a real network, recall that the IP address should be
assigned by the network administrator. Optionally, the DHCP capability of CMX-MicroNet software can
be used. With DHCP, a DHCP server will automatically assign a leased IP address to the embedded
device.
To set up the IP address for the development host in Windows, the IP address network settings of the
development host must be accessed in its operating system. For recent Windows releases, these settings
are located in the control panel. In the control panel, select network settings. A typical network settings
dialog box is shown in Figure 10.
Basic Web Server with MC9S12NE64 and CMX-MicroNet TCP/IP Stack, Rev. 0.3
Freescale Semiconductor 17
Preparing for CMX-MicroNet TCP/IP Stack Development
The network settings window shows all devices that can be used to form network connections. Figure 10
shows two network devices defined for the PC. Network devices can also include modems.
The network settings window also shows the status of the network device. This status indicates whether
the network device has an Ethernet connection. Having an Ethernet connection does not necessarily
mean other Windows network settings for that device are set correctly (see Debugging Networks).
To access the IP address setting via the network settings dialog box, you must select the desired network
device and configure its properties. In the properties dialog box, select the TCP/IP protocol network
component for the TCP/IP adapter (see Figure 11) and click Properties.
In the Internet Protocol (TCP/IP) Properties dialog box, manually enter a subnet mask and specific IP
address. Recall that the IP address used for the development host in this example is 192.168.1.1.
Basic Web Server with MC9S12NE64 and CMX-MicroNet TCP/IP Stack, Rev. 0.3
18 Freescale Semiconductor
Debugging Networks
Debugging Networks
Issues
Issues with network connectivity are typically due to an error in the network setup and configuration of
either the network or the remote devices. Three main network connectivity issues and their possible
solutions are described in this section:
• Ethernet connection not established
• Network connection cannot be established
• Network connection is established at the IP layer with Ping, but the devices are not communicating
Ethernet transceiver issues at startup occur if the embedded device was not initialized correctly by the
program. First, visually verify that the devices are physically connected. On the PC and the target
evaluation board, the link and speed LEDs should be active.
If the physical connection is visually verified and the problem still exists, check the status of the link in the
network settings dialog box as shown in Figure 10. Also, if the network is configured correctly, Windows
may show the status of the link on the task bar as shown in Figure 12. The task bar shows the link as
“Network Cable Unplugged.”
NETWORK
CONNECTION
STATUS ICON
(UNPLUGGED AS SHOWN)
Basic Web Server with MC9S12NE64 and CMX-MicroNet TCP/IP Stack, Rev. 0.3
Freescale Semiconductor 19
Debugging Networks
In these two cases, the network IP address, network port information, network components, network
protocols, and server type must be reviewed.
This section deals with the issue of the network higher level protocols not establishing a connection, such
as an Internet protocol (TCP/IP) connection. One way this can be checked is with Ping. Ping is actually
an IPv4 ICMP (Internet control management protocol) echo request that is defined by the TCP/IP stack
protocol. Because Ping functionality is included in CMX-MicroNet and Windows, it can be used to debug
the network connection. If the command confirms a valid connection to a remote device by replying to the
ICMP echo request, the network is configured correctly. If, however, the Windows command does not
confirm a network connection to the remote device, the network is not configured correctly.
The key step to resolve this type of network bug is to determine how the network is designed and how the
remote device must be configured to accept a connection. Remote devices must be compliant with the
network’s design structure and protocols. When the network is set up and configured correctly, the
devices will connect. This problem is usually associated with incorrect and incompatible IP address
settings (see Configuring the IP Addresses).
Network Connection is Established at the IP Layer with Ping, but the Devices are Not Talking
This problem is usually difficult to debug. There may be a conflict with other protocols settings. Other
possible causes can be a firewall, proxy server settings, duplex mismatch, or invalid server settings. With
an understanding of the network design and its connection capabilities, network restrictions, and
underlying communication protocols (for example, TCP/IP and NETBEUI), a user can configure the
network and the remote devices to ensure connectivity. This issue may require assistance from a system
administrator to resolve.
A network protocol analyzer is a powerful and useful tool for network debugging. The network protocol
analyzer enables more visibility of packet traffic on the network connection. A network protocol analyzer
is used to monitor the connectivity of the Internet or a local area network (LAN).
The tool is capable of non-intrusively attaching itself and monitoring a dial-up or Ethernet connection. The
network protocol analyzer can be an in-house, commercial, or downloadable freeware software package.
A network protocol analyzer can be implemented in hardware also.
The overriding feature of the network protocol analyzer is its ability to capture, analyze, and decode
network packets. The network protocol analyzer must be capable of determining the communication
protocol of the network data packets. In addition, the program must be able to display a list of network
connections, the IP addresses of the connections, the data direction, and the network data port
information. The network protocol analyzer provides the detailed network information required to debug
a network.
Basic Web Server with MC9S12NE64 and CMX-MicroNet TCP/IP Stack, Rev. 0.3
20 Freescale Semiconductor
Overview of a Web Server Developed Using CMX-MicroNet TCP/IP Stack
This section provides an overview of a web server developed with CMX-MicroNet software. This simple
web server was developed for distribution with the EVB9S12NE64 as an S-record file. Figure 13 shows
the web server.
This section will overview the files that were modified during development of the CMX-MicroNet web
server demo. Acronyms and terms used in this section are defined in Table 1.
Web Pages
The HTML code for the main frame of the web page shown in Figure 13 is provided in this section. Web
page development can be assisted with a tool, such as FrontPage by Microsoft, but it is not required. The
web page can contain standard HTML components including, but not limited to:
• Frames
• Tables
• Forms
• Embedded Java script
• Applets
Basic Web Server with MC9S12NE64 and CMX-MicroNet TCP/IP Stack, Rev. 0.3
Freescale Semiconductor 21
Overview of a Web Server Developed Using CMX-MicroNet TCP/IP Stack
When these web components are completed and tested, the next step is to convert them to the equivalent
CMX-MicroNet C files using CMX’s html2C utility.
<html><head>
<SCRIPT LANGUAGE="JavaScript">
function checkInfo(form) {
var ok = true;
var valid;
var temp;
if (form.webvar.value == "")
ok = false;
if (ok)
{
valid = "0123456789";
for (var i=0; i<5; i++)
{
temp = "" + form.webvar.value.substring(i, i+1);
if (valid.indexOf(temp) == "-1")
ok = false;
}
}
if (ok)
{
var check_num = parseInt(form.webvar.value,10);
ok = (!isNaN(check_num) && (check_num >= 0) && (check_num <= 32767));
}
if (!ok)
{
alert("Invalid number entered.\r\nPlease enter a number between 0 and 32767.");
form.webvar.focus();
return false;
}
return (ok);
}
</SCRIPT></head>
<BODY text="#000000" vlink="#990099" alink="#990099" bgcolor="#FFFFFF" link="#0000CC"">
<CENTER><table align="center"><tr>
<td align="center" width="50%"><FONT SIZE="+1" COLOR="BLUE"><B>Enter a number between 0 and
32767 in the box below.
This will change the variable demo_var located at address 0x2000. This address can be
examined to monitor the changes to the variable.</B></FONT></td>
<td align="center" width="50%"><FONT SIZE="+1" COLOR="RED"><B>JAVA Applet</B></FONT></td>
</TR><tr>
<td align="center"><FORM NAME="var_info" ACTION="set_demo_var" METHOD=POST onSubmit="return
checkInfo(var_info)">
<INPUT NAME="webvar" TYPE="Text" MAXLENGTH=5 VALUE="<!--#exec cgi="get_demo_var"-->"><P>
<INPUT TYPE="Submit" VALUE="Update"></FORM></td>
<TD ALIGN="center" width="50%"><applet code="JavaCl.class" width="120" height="120">
</applet></TD></tr></table>
<br><br><FONT SIZE="+1"><B>Note: you must have Javascript enabled for this demo.</B></FONT>
</CENTER></body></html>
Basic Web Server with MC9S12NE64 and CMX-MicroNet TCP/IP Stack, Rev. 0.3
22 Freescale Semiconductor
Overview of a Web Server Developed Using CMX-MicroNet TCP/IP Stack
examplee.c
examplee.c contains main() for the user application. In main(), the MC9S12NE64 MCU and its integrated
Ethernet controller are configured and enabled. main() also initializes the CMX-MicroNet TCP/IP stack
then waits and serves a web page on request.
Note that mn_init() must be called before using any other CMX-MicroNet function. Refer to CMX-MicroNet
TCP/IP Stack API for more information about the CMX-MicroNet API functions used in the provided code.
/*********************************************************
Copyright (c) CMX Systems, Inc. 2003. All rights reserved
*********************************************************/
#include "micronet.h"
/* Local functions */
void set_demo_var_func(PSOCKET_INFO socket_ptr) cmx_reentrant;
word16 get_demo_var_func(byte **) cmx_reentrant;
#define MSG_BUFF_SIZE 17
byte msg_buff[MSG_BUFF_SIZE];
int main(void)
{
/* call mn_init before using any other MicroNet API functions */
if (mn_init() < 0)
EXIT(1);
Basic Web Server with MC9S12NE64 and CMX-MicroNet TCP/IP Stack, Rev. 0.3
Freescale Semiconductor 23
Overview of a Web Server Developed Using CMX-MicroNet TCP/IP Stack
memset(msg_buff,0,sizeof(msg_buff));
demo_var = 12345;
return(0);
}
/* --------------------------------------------------------------------- */
Basic Web Server with MC9S12NE64 and CMX-MicroNet TCP/IP Stack, Rev. 0.3
24 Freescale Semiconductor
Overview of a Web Server Developed Using CMX-MicroNet TCP/IP Stack
callback.c
An excerpt of the source code for callback.c is provided. This section of callback.c is important because
it includes the MAC hardware and IP address settings. This file must be configured correctly. Review
Configuring the MAC Hardware and IP Addresses for details. The list below provides a simplified
description of the code provided.
• ip_dest_addr[IP_ADDR_LEN] — If HTTP or FTP is not being used, an IP address for a destination
node must be provided. If HTTP or FTP is used, this variable can be ignored. HTTP and FTP are
configured in mnconfig.h.
• ip_src_addr[IP_ADDR_LEN] — Embedded device IP address. If DHCP is configured and used,
this variable can be ignored. DHCP is configured in mnconfig.h.
• byte eth_src_hw_addr[ETH_ADDR_LEN] — This MAC hardware address for the embedded
device should be a unique 48-bit number as specified by IEEE.
• byte eth_dest_hw_addr[ETH_ADDR_LEN] — If ARP is not being used, a MAC hardware address
for a destination node must be provided in this variable.
The SMTP server IP address can also be set up in callback.c if the CMX-MicroNet SMTP client is used.
SMTP is used for sending email on the Internet. For this example, SMTP is not used.
#if Ethernet
byte ip_dest_addr[IP_ADDR_LEN] = {192,168,2,2};
#if (PING_GLEANING)
byte ip_src_addr[IP_ADDR_LEN] = {0,0,0,0};
#else
byte ip_src_addr[IP_ADDR_LEN] = {192,168,2,3}; // static IP address of embedded device
#endif /* PING_GLEANING */
#else
byte ip_dest_addr[IP_ADDR_LEN] = {192,6,94,5};
#if (PING_GLEANING)
byte ip_src_addr[IP_ADDR_LEN] = {0,0,0,0};
#else
byte ip_src_addr[IP_ADDR_LEN] = {192,6,94,2};
#endif /* PING_GLEANING */
#endif /* Ethernet */
#if (SMTP)
/* replace the ip address below with the ip address of your SMTP server */
byte ip_smtp_addr[IP_ADDR_LEN] = {216,148,227,71};
#endif /* (SMTP) */
#if Ethernet
/************************************************************
if using a chip with EEPROM you may need to write a routine
to take the value of the hw_addr in EEPROM and put it into
the array below on startup, otherwise replace eth_src_hw_addr
below with the proper Ethernet hardware address.
*************************************************************/
byte eth_src_hw_addr[ETH_ADDR_LEN] = {0x00,0x00,0x12,0x34,0x56,0x78};
/************************************************************
Basic Web Server with MC9S12NE64 and CMX-MicroNet TCP/IP Stack, Rev. 0.3
Freescale Semiconductor 25
Overview of a Web Server Developed Using CMX-MicroNet TCP/IP Stack
/************************************************************
If a gateway is being used set the gateway IP address and
subnet mask below.
#endif /* Ethernet */
mnconfig.h
Editing mnconfig.h is required to select the protocols used, number of interfaces, number of sockets, sizes
of transmit and receive buffers, etc. To minimize code size, only the protocols that are required for the
application should be used. An excerpt of mnconfig.h is provided. Important settings for the web server
example are:
• Enable TCP (enabling UDP is optional)
• Enable Ethernet
• Enable PING
• Enable ARP
• Disable DHCP
• Enable HTTP and SERVER_SIDE_INCLUDES
• Enable VIRTUAL_FILE
In mnconfig.h, defining a value of 1 enables a feature, and a value of 0 disables a feature. When editing
mnconfig.h, recall that some protocols are dependent on each other. For instance, the HTTP requires the
TCP. In addition, for CMX-MicroNet software, either UDP or TCP must be enabled for compilation.
Figure 14 shows several popular network protocols and their dependence.
Basic Web Server with MC9S12NE64 and CMX-MicroNet TCP/IP Stack, Rev. 0.3
26 Freescale Semiconductor
Overview of a Web Server Developed Using CMX-MicroNet TCP/IP Stack
USER APPLICATION
APPLICATION API
SMTP
APPLICATION
SOCKET API
TCP UDP
TRANSPORT
ARP IP PING
NETWORK
TCP/UDP/IP
DEVICE DRIVER API
ETHERNET
NETWORK INTERFACE
#ifndef MNCONFIG_H_INC
#define MNCONFIG_H_INC 1
/* Protocols */
#define TCP 1
#define UDP 1
#define UDP_CHKSUM 1
#define ETHERNET 1
#define SLIP 0
#define PPP 0
#define PING 1
#define IGMP 0
/* Sockets */
#define NUM_SOCKETS 5
#define SOCKET_WAIT_TICKS 600
#define RECV_BUFF_SIZE 900
#define XMIT_BUFF_SIZE 570
#define SOCKET_INACTIVITY_TIME 0
/* TCP/IP options */
Basic Web Server with MC9S12NE64 and CMX-MicroNet TCP/IP Stack, Rev. 0.3
Freescale Semiconductor 27
Overview of a Web Server Developed Using CMX-MicroNet TCP/IP Stack
#define TIME_TO_LIVE 64
#define TCP_WINDOW 512
#define TCP_RESEND_TICKS 600
#define TCP_RESEND_TRYS 12
#define PING_GLEANING 0
#define PING_BUFF_SIZE 32
#define ALLOW_BROADCAST 0
#define ALLOW_MULTICAST 0
#define MULTICAST_TTL 1
#define IGMP_LIST_SIZE 4
/* Ethernet */
#define POLLED_ETHERNET 0
#define ETHER_WAIT_TICKS 5
/* ARP */
#define ARP 1
#define ARP_WAIT_TICKS 600
#define ARP_TIMEOUT 0
#define ARP_AUTO_UPDATE 0
#define ARP_CACHE_SIZE 4
#define ARP_KEEP_TICKS 6000
#define ARP_RESEND_TRYS 6
/* DHCP */
#define DHCP 0
#define DHCP_RESEND_TRYS 4
#define DHCP_DEFAULT_LEASE_TIME 36000
/* BOOTP */
#define BOOTP 0
#define BOOTP_RESEND_TRYS 6
#define BOOTP_REQUEST_IP 1
/* PPP options */
#define USE_PAP 1
#define PAP_USER_LEN 10
#define PAP_PASSWORD_LEN 10
#define PAP_NUM_USERS 1
#define PPP_RESEND_TICKS 300
#define PPP_RESEND_TRYS 6
#define PPP_TERMINATE_TRYS 2
#define FAST_FCS 1
/* Modem */
#define MODEM 0
#define DIRECT_CONNECT 1
#define NULL_MODEM 1
#define REMOTE_IS_NT 1
#define USE_PASSWORD 0
/* HTTP */
#define HTTP 1
#define SERVER_SIDE_INCLUDES 1
#define INCLUDE_HEAD 0
#define URI_BUFFER_LEN 52
#define BODY_BUFFER_LEN 52
#define HTTP_BUFFER_LEN 512
Basic Web Server with MC9S12NE64 and CMX-MicroNet TCP/IP Stack, Rev. 0.3
28 Freescale Semiconductor
Overview of a Web Server Developed Using CMX-MicroNet TCP/IP Stack
/* FTP */
#define FTP 0
#define FTP_SERVER 1
#define FTP_MAX_PARAM 24
#define FTP_BUFFER_LEN 512
#define FTP_USER_LEN 10
#define FTP_PASSWORD_LEN 10
#define FTP_NUM_USERS 1
#define NEED_MEM_POOL 0
#define MEM_POOL_SIZE 4096
/* TFTP */
#define TFTP 0
#define TFTP_RESEND_TRYS 3
#define TFTP_USE_FLASH 1
/* SMTP */
#define SMTP 0
#define SMTP_BUFFER_LEN 512
Ether_Init.h
An excerpt of the source code for Ether_Init.h is provided. Ether_Init.h allows the user to configure
initialization options for the MC9S12NE64 EMAC and EPHY. The excerpt from Ether_Init.h shows a
partial list of the configurations a user may desire to modify. These configurations are provided below with
brief descriptions:
• AUTO_NEGOTIATE — Sets up ne64_init() to initialize the NE64 in auto-negotiation mode if
asserted. If not asserted, the developer must manually set up the speed and duplex for the NE64
using the full_duplex and speed_100 variables.
• HALF10, FULL10, HALF100, and FULL100 — Set the auto-negotiation advertisements and should
be set to 1 if that mode is to be advertised in auto-negotiation.
• SPEED100 — if auto-negotiation is disabled (AUTO_NEG = 0), this bit manually sets the device
link speed.
• FULL_DUPLEX — if auto-negotiation is disabled (AUTO_NEG = 0), this bit manually sets the
device link speed.
• ETYPE_PET, ETYPE_EMW, ETYPE_IPV6, ETYPE_ARP, ETYPE_IPV4, ETYPE_IEEE, and
ETYPE_ALL — Set the EMAC Ethertype filtering modes.
• BRODC_REJ, CON_MULTIC, and PROM_MODE — Set the EMAC MAC address filtering modes.
• PTIME — EMAC flow control setting.
• RX_MAX_FL — Sets a maximum frame length by assigning a value to this identifier.
Basic Web Server with MC9S12NE64 and CMX-MicroNet TCP/IP Stack, Rev. 0.3
Freescale Semiconductor 29
Overview of a Web Server Developed Using CMX-MicroNet TCP/IP Stack
#if AUTO_NEG
/* what I advertise */
#define HALF100 1 /**< Configure mode that the device should advertise (advertise=1
& not= 0... */
#define FULL100 1 /**< Configure mode that the device should advertise (advertise=1
& not= 0... */
#define HALF10 1 /**< Configure mode that the device should advertise (advertise=1
& not= 0... */
#define FULL10 1 /**< Configure mode that the device should advertise (advertise=1
& not= 0... */
#else /* AUTO_NEG */
#define SPEED100 0 /**< 1 - enable 100 MBps / 0 - enable 10 MBps */
#define FULL_DUPLEX 0 /**< 1 - enable full duplex / 0 - disable */
#endif /* AUTO_NEG */
/* EMAC CONTROL */
/* =============== */
/* Address Filtering; RXMODE setting: PAUSE frame supported, Accept Unique, Brodcast, MultiCast
*/
#define BRODC_REJ 0 /**< 1 = All broadcast address frames are rejected.
*/
#define CON_MULTIC 0 /**< 1 = Multicast hash table is used for checking multicast
addresses. */
#define PROM_MODE 0 /**< 1 = All frames are received regardless of address.
*/
/* Ethertype Control */
#define ETYPE_PET 0 /**< 1 = accept Programmable Ethertype, 'etype' parameter is used */
#define ETYPE_EMW 0 /**< 1 = accept Emware Ethertype */
#define ETYPE_IPV6 0 /**< 1 = accept Internet IP version (IPV6) Ethertype */
#define ETYPE_ARP 1 /**< 1 = accept Address Resolution Protocol (ARP) Ethertype */
#define ETYPE_IPV4 1 /**< 1 = accept Internet IP version 4 (IPV6) Ethertype */
#define ETYPE_IEEE 0 /**< 1 = accept IEEE802.3 Length Field Ethertype */
#define ETYPE_ALL 0 /**< 1 = accept Accept all ethertypes. THIS OVERRIDES OTHER SETTINGS */
/* Programable Ethertype */
#define ETYPE_PRG 0 /**< Enter Value if ETYPE_PET is set for filter target */
Basic Web Server with MC9S12NE64 and CMX-MicroNet TCP/IP Stack, Rev. 0.3
30 Freescale Semiconductor
Overview of a Web Server Developed Using CMX-MicroNet TCP/IP Stack
emac_fifo_cfg.h
An excerpt of the source code for emac_fifo_cfg.h is provided. The code shows configurations for the
EMAC Ethernet buffer allocation.
For the MC9S12NE64, 8K of RAM is available and shared between user RAM and the EMAC Ethernet
buffer space. The EMAC Ethernet buffer space consists of three buffers:
• One transmit buffer
• Two receive buffers
Each buffer is designed to hold only one Ethernet frame. Table 5 provides the configurations for the
shared RAM usage within the MC9S12NE64 MCU.
Because the maximum size of an Ethernet frame is approximately 1.5 Kbytes, a setting of BUFMAP = 4
would allow each of the three MC9S12NE64 buffers to hold one frame of the maximum allowable size as
dictated by the IEEE 802.3 specification. Settings for BUFMAP less than 4 are provided to:
• Maximize user RAM
• Provide a filtering mechanism based on Ethernet packet size
The setting of BUFMAP is a system/network design decision. If devices on the network should accept only
Ethernet packets of a certain size, BUFMAP should be configured accordingly.
Setting BUFMAP to create a buffer size limit reduces the burden of the CPU by ignoring packets that are
too large. If a receive frame exceeds the receive buffer size, the frame is not accepted, and neither the
receive complete flag nor the receive error flag is set. No CPU bandwidth is used because the EMAC state
machine does all packet filtering.
Basic Web Server with MC9S12NE64 and CMX-MicroNet TCP/IP Stack, Rev. 0.3
Freescale Semiconductor 31
Overview of a Web Server Developed Using CMX-MicroNet TCP/IP Stack
/******************************************************************************
*
* Freescale Semiconductor, Inc. 2003 All rights reserved
*
*******************************************************************************
*
* $File Name : emac_fifo_cfg.h$
* Description : definition of EMAC Ethernet buffer allocation
*
*
* $Version : 1.0.3.0$
* $Date : Sep-23-2003$
*
* $Last Modified By : r29303$
*
******************************************************************************/
#ifndef EMAC_FIFO_CFG_H
#define EMAC_FIFO_CFG_H
#define BUFMAP 4
#if BUFMAP == 0
#define EMAC_RX_SZ 128
#define EMAC_TX_SZ 128
#elif BUFMAP == 1
#define EMAC_RX_SZ 256
#define EMAC_TX_SZ 256
#elif BUFMAP == 2
#define EMAC_RX_SZ 512
#define EMAC_TX_SZ 512
#elif BUFMAP == 3
#define EMAC_RX_SZ 1024
#define EMAC_TX_SZ 1024
#elif BUFMAP == 4
#define EMAC_RX_SZ 1536
#define EMAC_TX_SZ 1536
#endif
#endif /* EMAC_FIFO_CFG_H */
Basic Web Server with MC9S12NE64 and CMX-MicroNet TCP/IP Stack, Rev. 0.3
32 Freescale Semiconductor
CMX-MicroNet Project Configuration to Optimize the Stack Solution
_MC9S12NE64_BANKED.prm.h
An excerpt of the source code for _MC9S12NE64_BANKED.prm is provided. The code shows
configurations for the EMAC Ethernet buffer allocation. The RAM segments definition should match the
corresponding configurations of BUFMAP in emac_fifo_cfg.h.
SEGMENTS
When developing a web server, there are several strategies to follow with the project to ensure that the
code size of the solution does not exceed the available resources:
• Minimize web page content
• Modify mnconfig.c to use only required network protocols
• Set buffers to appropriate values
A fully featured web page for an application uses valuable FLASH and RAM resources. Before
implementation, it is important to understand the resources that the application will require and balance
them with the web page features. Each web page graphic, for example, can easily require 6 to 8 Kbytes
of FLASH.
See the mnconfig.h section for details. If using Ethernet without DHCP or BOOTP, these protocols should
not be enabled in the stack. A complete TCP/IP stack consists of a large set of networking protocols that
require large memory and CPU resources. For resource-constrained TCP/IP stack implementations, such
as implementing a TCP/IP stack on an 8-/16-bit embedded system, it is not always best to implement the
complete set of networking protocols.
Basic Web Server with MC9S12NE64 and CMX-MicroNet TCP/IP Stack, Rev. 0.3
Freescale Semiconductor 33
CMX-MicroNet Project Configuration to Optimize the Stack Solution
Figure 15 illustrates a simplified or partial TCP/IP stack implementation. This stack uses only the UDP
protocol and the user applications.
USER APPLICATION
UDP
ARP IP
NETWORK INTERFACE
The major disadvantage of TCP/IP stack implementations that are customized to a specific application is
that they are not complete TCP/IP stack implementations. So, if changes to the TCP stack functionality
are required after product deployment, making updates would require recompiling the TCP stack code to
include the missing components and reprogramming the device in the field.
Zero Copy refers to the TCP/IP stack's ability to send and receive from the Ethernet buffers’ stack without
copying data to user RAM. CMX's zero copy functionality was introduced in CMX-Micronet version 3.03
to optimize RAM usage with the MC9S12NE64. The zero copy mode of the CMX stack can be disabled
if the user chooses. Because the MC9S12NE64 has an integrated Ethernet buffer, there is no need to
make a second copy of the Ethernet buffers. Instead, these Ethernet buffers can be directly accessed.
Not making a copy of the data saves user RAM space and improves performance. Performance is
improved because, in most cases, the data can be completely processed in much less of time that would
be required to make a copy of the data between the user RAM and Ethernet buffer RAM. It is possible
that enabling zero copy could cause the HTTP server to drop packets because, in zero copy mode, only
two incoming packets will be stored at the same time.
To enable zero copy mode in the CMX-MicroNet TCP/IP stack, the USE_SEND_BUFF and
USE_RECV_BUFF variables in mn_env.h of the netlib directory must be cleared to 0. If set to 1,
CMX-MicroNet makes a copy of data in the Tx and Rx Ethernet buffers in user RAM.
RAM for Tx and Rx Ethernet buffers should be balanced with user application RAM. If large Ethernet
buffers are not required for the user application, set the Ethernet buffer values (in the BUFMAP register)
so that the user application uses only the necessary RAM resources. The Tx and Rx Ethernet buffer sizes
are controlled in emac_fifo_cfg.h and _MC9S12NE64_BANKED.prm .
Basic Web Server with MC9S12NE64 and CMX-MicroNet TCP/IP Stack, Rev. 0.3
34 Freescale Semiconductor
Conclusion
Conclusion
Combining the MC9S12NE64 and the CMX-MicroNet stack software provides a single-chip Ethernet
system solution that is low cost and easy to use. The CMX-MicroNet web server described in this
application note is only one of the many network applications possible with the MC9S12NE64.The
MC9S12NE64 provides developers with the means to add Ethernet functionality to everyday applications
and/or design innovative, network-enabled applications.
NOTE
With the exception of mask set errata documents, if any other
Freescale Semiconductor document contains information that conflicts with
the information in the device user guide, the user guide should be
considered to have the most current and correct data.
Although specific methods and tools are used to develop and debug this
demo, Freescale Semiconductor does not recommend or endorse any
particular methodology, tool, or vendor. These methods and tools are
provided only to describe the generic principles and features that may be
required for development of a networked device.
Basic Web Server with MC9S12NE64 and CMX-MicroNet TCP/IP Stack, Rev. 0.3
Freescale Semiconductor 35
How to Reach Us: Information in this document is provided solely to enable system and software implementers to use
Freescale Semiconductor products. There are no express or implied copyright licenses granted
hereunder to design or fabricate any integrated circuits or integrated circuits based on the information
USA/Europe/Locations not listed:
in this document.
Freescale Semiconductor Literature Distribution
P.O. Box 5405, Denver, Colorado 80217 Freescale Semiconductor reserves the right to make changes without further notice to any products
1-800-521-6274 or 480-768-2130 herein. Freescale Semiconductor makes no warranty, representation or guarantee regarding the
suitability of its products for any particular purpose, nor does Freescale Semiconductor assume any
Japan: liability arising out of the application or use of any product or circuit, and specifically disclaims any
Freescale Semiconductor Japan Ltd. and all liability, including without limitation consequential or incidental damages. “Typical” parameters
SPS, Technical Information Center
which may be provided in Freescale Semiconductor data sheets and/or specifications can and do
3-20-1, Minami-Azabu
vary in different applications and actual performance may vary over time. All operating parameters,
Minato-ku
Tokyo 106-8573, Japan including “Typicals” must be validated for each customer application by customer’s technical experts.
81-3-3440-3569 Freescale Semiconductor does not convey any license under its patent rights nor the rights of others.
Freescale Semiconductor products are not designed, intended, or authorized for use as components
Asia/Pacific: in systems intended for surgical implant into the body, or other applications intended to support or
Freescale Semiconductor H.K. Ltd. sustain life, or for any other application in which the failure of the Freescale Semiconductor product
2 Dai King Street could create a situation where personal injury or death may occur. Should Buyer purchase or use
Tai Po Industrial Estate Freescale Semiconductor products for any such unintended or unauthorized application, Buyer shall
Tai Po, N.T. Hong Kong
indemnify and hold Freescale Semiconductor and its officers, employees, subsidiaries, affiliates, and
852-26668334
distributors harmless against all claims, costs, damages, and expenses, and reasonable attorney
Learn More: fees arising out of, directly or indirectly, any claim of personal injury or death associated with such
For more information about Freescale unintended or unauthorized use, even if such claim alleges that Freescale Semiconductor was
Semiconductor products, please visit negligent regarding the design or manufacture of the part.
http://www.freescale.com Freescale™ and the Freescale logo are trademarks of Freescale Semiconductor, Inc. All other
product or service names are the property of their respective owners. Metrowerks and CodeWarrior
are registered trademarks of Metrowerks, Inc., a wholly owned subsidiary of Freescale Inc.
CMX TCP/IP and CMX-MicroNet are trademarks of CMX Systems, Inc. Microsoft, Windows, Internet
Explorer, and FrontPage are either registered trademarks or trademarks of Microsoft Corporation in
the U.S. and other countries. MultiLink is a trademark of P&E Microcomputer Systems, Inc.
AN2700
Rev. 0.3, 9/2004