Ethernet TCP/IP Source Code Driver Project
Section 08. Information

08. Information

a) Frequently Asked Questions

This driver seems harder to use than some other drivers?

Different TCP/IP stacks take different approaches and our driver has been deliberately designed to give programmers low level access to all functionality.  By their nature TCP/IP stacks are complex and when implementing them on 8, 16 and 32 bit microcontrollers we prefer to be able to program “to the metal” to obtain optimum performance and versatility.  This means that to carry out an operation like sending a UDP packet for instance, you may need to do slightly more work than if using some other drivers, but with the advantage that you know exactly what is happening, rather than it being hidden “under the hood”, and your not wasting memory or resources using a higher level function designed to cover all sorts of eventualities not relevant to your application.

Can a low end microcontroller really provide full TCP/IP functionality?

Yes. The real issue around TCP/IP functionality for embedded applications is typically not the capability of the microcontroller to provide the required functionality in a timely manor, but is the code space required for it. As microcontroller memory sizes get bigger and bigger this is no longer an issue even for really low cost devices. Here at embedded-code our engineers have designed many devices that use a simple 8 bit microcontroller and communicate using TCP/IP without any problems.

Is TCP/IP communications hard?

Absolutely not. Getting a communication link between two devices or PC’s using UDP is incredibly simple and very much like getting a communication link between two devices using RS232 serial ports. There’s a couple more things to setup and understand but other than that its simply a case of outputting some data byte by byte and at the far end receiving it byte by byte. However unlike RS232 your communicating over Ethernet, which means your two devices or PC’s can be in the same room or across the world from each other with no additional work required by you (well OK, you’ll have to setup your router to allow the incoming connection, but other than that there’s nothing more you have to do!).

TCP / UDP – Which Is Best?

You’ll probably come across programmers who say this isn’t even a question – use TCP because all the hard work is done for you. In many applications this is true and using TCP can make your life as a programmer very easy. However TCP has one very big drawback in embedded applications – when a packet gets lost or a remote device doesn’t respond you have to wait for the TCP stack to retry the communication several times, each after successively longer delays before it finally gives up. This is TCP’s big advantage and drawback at the same time. With UDP there is no re-try – if a packet gets lost you need to notice that you’ve not had a reply and send it again. To the hardened PC programmer this is task often not worthy of their genius, but to an embedded programmer this is something that can make UDP incredibly powerful and fast in time critical applications. Was it ever really a problem having to notice that a remote device hadn’t responded when talking to it using RS232? Well its no different when talking using UDP and you can use the same simple approaches to defining your communications link. UDP also has one other great advantage – it allows you to broadcast packets so that every device on the local network or in the devices sub net receives them – you can’t do that with TCP.

So the answer is, it depends on what you are doing. In terms of how the packets of data travel over the Ethernet links and Internet there is no difference – both get routed in exactly the same way. TCP is fantastic at offloading work from you as a programmer for many applications, but UDP is the obvious choice in a great many other applications where you need that raw control of what you are sending and receiving.

Can I Open And Use Multiple Sockets At The Same Time?

Yes. You specify the maximum number of UDP and TCP sockets that the driver will provide.

Is the driver suitable for 64 bit architectures?

The driver uses structures to efficiently match the layout of several TCP/IP packet headers. The contents of all the structures that can be read or written using pointers by the driver are correctly aligned for 8, 16 and 32 architectures, except the ARP packet structure (which the driver reads and writes per variable and avoids using pointers or sizeof() for 32 bit compilers that typically insert padding). The structures used are not however safe for 64bit architectures unless you compiler provides the option to force structures to not be padded. This is not an ANSI requirement, however many compilers will provide this option.

What’s Difference The Between Ethernet Switches & Hubs

First there we’re Ethernet hubs, then came expensive switches and now you are hard pushed to find a hub! Ethernet hubs are simple dumb devices. A transmission from a device on any of its ports is ‘seen’ by every other device. This is fine as Ethernet is designed based on this, with your nic dealing with detecting packet collisions etc. However it can start to be inefficient if you have a lot of devices all connected by hubs. Ethernet switches on the other hand are intelligent. A switch learns (or is configured to know in the case of managed switches) what devices are connected to each of its ports and when a device transmits if the switch knows on which of its ports the device that the packet is intended for is, it only sends the packet to that port. This means that all the other ports are not blocked because one device is transmitting and they can transmit themselves at the same time with the switch handling buffering of packets if need be. In terms of improving a networks bandwidth switches are great. However they have two drawbacks.

Firstly when debugging you may want to monitor the traffic being transmitted between other devices. You can’t do this using a switch, unless the packets are broadcast UDP packets. However with a hub every device see’s everything being sent so any device can monitor the traffic. Incidentally this is why many hotels are a hackers paradise as often cheaper or older hubs are installed so connecting a sniffer in any room allows you to see everyone else’s traffic. The same is true of public WiFi hotspots and its why if you are doing anything at all sensitive on public Internet connections (even just checking your email) you should use https or a VPN service to encrypt your traffic.

Secondly switches often have to buffer packets. This means that an element of uncertainly can be introduced in very time critical applications as a busy switch may buffer a packet for an unknown amount of time before it is finally passed to the remote device.

b) Specifications

Using The Driver With a RTOS or Kernel

The stack / driver is implemented as a single thread so you just need to make sure it is always called from a single thread (it is not designed to be thread safe).

c) Code and Data Memory Requirements

Ram Space

The amount of static ram memory used by the driver will vary greatly depending on the individual stack components that are selected. Below is an approximate guide for each of the stack components:-

General Stack and nic 128 bytes

UDP

5 bytes
Plus 20 bytes per socket

TCP

19 bytes
Plus 38 bytes per socket

HTTP

11 bytes
Plus 14 bytes per socket if web files converted to a C header file, or 17 bytes per socket otherwise.
Plus 149 bytes if POST inputs are enabled (the GET input method is still available if not)

DHCP

29 bytes

NetBIOS

55 bytes

DNS

83 bytes

POP3

142 bytes

SMTP

52 bytes

ICMP

0 bytes

SNTP

14 bytes

The driver requires a small to moderate amount of temporary variable storage space from the stack for its functions.

Program Memory Space

The amount of program memory used by the driver will vary greatly depending on the individual stack components that are selected. The following program memory sizes are based on compiling with optimisations turned off, and therefore would obviously be reduced, significantly in many cases, with compiler optimising enabled. We present them in this un-optimised state so that you can gauge for yourself the likely program memory size for your specific application, without worrying that our figures have been made under perfect optimising conditions as a sales technique!

Using the Microchip C18 Compiler for PIC18 8 bit microcontrollers – Optimisations off

General Stack and nic

11280 program memory bytes (5640 x 16 bit instructions)

UDP

4548 program memory bytes (2274 x 16 bit instructions)

TCP

13970 program memory bytes (6985 x 16 bit instructions)

HTTP

8366 program memory bytes (4183 x 16 bit instructions)
3530 program memory bytes (1765 x 16 bit instructions) additional if POST inputs are enabled

DHCP

3948 program memory bytes (1974 x 16 bit instructions)

NetBIOS

1252 program memory bytes (626 x 16 bit instructions)

DNS

3216 program memory bytes (1608 x 16 bit instructions)

POP3

4280 program memory bytes (2140 x 16 bit instructions)

SMTP

8801 program memory bytes (4402 x 16 bit instructions)

ICMP

916 program memory bytes (458 x 16 bit instructions)

SNTP

2278 program memory bytes (1139 x 16 bit instructions)

Using the Microchip C30 Compiler for PIC24 & dsPIC 16 bit microcontrollers – Optimisations off

General Stack and nic

5490 program memory words (2745 x 24 bit instructions)

UDP

1608 program memory words (804 x 24 bit instructions)

TCP

6738 program memory words (3369 x 24 bit instructions)

HTTP

4526 rogram memory words (2263 x 24 bit instructions)
2396 program memory words (1198 x 24 bit instructions) additional if POST inputs are enabled

DHCP

2036 program memory words (1018 x 24 bit instructions)

NetBIOS

634 program memory words (318 x 24 bit instructions)

DNS

1450 program memory words (725 x 24 bit instructions)

POP3

2214 program memory words (1107 x 24 bit instructions)

SMTP

4512 program memory words (2256 x 24 bit instructions)

ICMP

266 program memory words (133 x 24 bit instructions)

SNTP

914 program memory words (457 x 24 bit instructions)