SD card web server demo

From DP

Jump to: navigation , search

Sdserver-cover-490.jpg

Contents

The goal

The goal of this firmware demo is to serve files to a web browser from a microSD card using the web platform hardware design. Microchip’s TCP/IP stack demonstration includes an HTTP server that reads files from FAT-formatted SD cards, so there’s no need to write our own.

The preparation

If you have not already setup your Microchip programming environment then you will need to install the:

The latest "legacy" Microchip TCP/IP framework has changes that break the web platform demos without some bug fixes. For the simple life you might wish to grab the older v5.20b (v2010-02-09) instead:

You will also need to download the project archive which contains, among other things, the source code for the SD card server minus the Microchip library code. Note: the firmware HEX file included in the project archive will lock up your web platform, so you will need to make some changes and recompile it from source - full details for doing this are included below.

If you have not already connected your web platform to your computer via USB, then you will need to do so. You might also need to install the FTDI driver if it is not already installed so that you can upload firmware to your web platform.

Compiling the firmware

Copy our open source replacement files from the project archive which you downloaded earlier into the Microchip SD card server directory (usually .\Microchip Solutions\TCPIP\Demo App MDD\). That’s it. You’ve got the latest source, and we’re compliant with the license. See the instructions in MainDemo.c for more details.

Before compiling:

  • in the TCPIPConfig.h file omit the second occurrence of {TCP_PURPOSE_HTTP_SERVER, TCP_ETH_RAM, 200, 200}, and reduce MAX_HTTP_CONNECTIONS from 2 to 1 to ensure stability and avoid lockups. This is because only one TCP Socket initialization is supported (the HTTP2 server will still handle multiple HTTP client connections). Tested with TCP/IP stack versions 5.20, 5.31 and 5.36.2.
  • please read Compiler Issues and Application Library issues below.

Compiler issues

Version 3.23 of the C30 compiler was originally used to compile the firmware. Unfortunately the free versions (lite, academic and evaluation) are no longer available from the Microchip site. If you do not have, and cannot find, v3.23 you could try: (1) lodging a support ticket with Microchip Technical Support; or (2) asking in the forum or (3) simply use the current version available from the Microchip site.

Version 3.25 of the C30 compiler does not produce a working firmware HEX file even though it will compile the code without errors. Avoid.

Version 3.30b of the C30 compiler is the current version as of August 2011 and does produce a working firmware HEX file.

All Depending on where you locate the various Microchip directories and your project directory, you may encounter compiler errors due to files not being found. To resolve these you will need to go to the IDE Project -> Build Options -> Project -> Show directories, then:

1. select include search path and add file paths to:

  • \Microchip Solutions\Microchip\Include directory;
  • \Microchip Solutions\Microchip\Include\TCPIP Stack directory;
  • \Microchip Solutions\TCPIP\Include\MDD Demo App directory; and
  • \[Your project directory].

2. select library search path and add file paths to:

  • \Microchip Solutions\Microchip\TCPIP Stack directory; and
  • \Microchip Solutions\TCPIP MDD Demo App directory.

Application Library issues

TCP/IP stack v5.20 You will need to make one of the following changes to the TCP/IP stack source file ENC28J60.c to prevent the web platform from locking up:

  • change the statement ENC_SPICON1bits.CKE = 1; to ENC_SPICON1bits.CKE = 0; or
  • add the statement ENC_SPICON1bits.SMP = 1; after the ENC_SPICON1bits.MSTEN = 1; statement; or
  • add a delay using 9 Nop()s in the WriteReg() function after the statement Dummy = ENC_SSPBUF; to ensure the required CS hold time of 210ns is met (per page 82 of ENC28J60 Rev C datasheet). This is possibly the more 'correct' fix.

TCP/IP stack v5.31 is reported to require the same change as v5.20 to prevent the web platform from locking up plus a few additional changes related to the MDD file system code.

TCP/IP stack v5.36.2. The ENC28J60 issues are fixed in this stack version, but you will still need to make the following changes:

  • In MainDemo.h replace the line #define SaveAppConfig() NOP() with #define SaveAppConfig(dummy) Nop().
  • There are bugs in Microchip's MDD file system code in HTTP2_MDD.c that cause random lockups by freeing memory or file pointers that are null. Basically, everywhere there is a free(...) in that file without a check for the memory or file pointer being null, you need to wrap it in an if statement that checks that the memory or file pointer being freed is not null. For example:

//try to open the file
curHTTP.file = FileOpen((const char *)tempPtr, "r");
free(tempPtr);

needs to become:

//try to open the file
curHTTP.file = FileOpen((const char *)tempPtr, "r");
if(tempPtr != NULL)
    free(tempPtr);

Load the demo firmware

The firmware .HEX file can be flashed to the PIC chip with the ds30 Loader bootloader, or it can be burned directly with a PIC programmer. See the web platform firmware article for detailed bootloader instructions. A copy of the ds30 Loader is included in the project archive.

FAT format

The server is compatible with FAT 12/16/32 formatted microSD cards. It’s crucial that your SD card be formatted correctly. FAT is a very common file system used by computers, digital cameras, and MP3 players, but operating systems don’t always apply correct FAT formatting to SD cards. Windows, for example, formats SD cards with a non-standard arrangement of file tables.

We use the freeware SDFormatter V2.0.0.3 utility to format cards correctly in Windows. A digital camera’s SD card format function will usually write the correct file table types too.

The web platform has been successfully tested with 4Gb and 8Gb micro-SDHC cards.

Using it

Use a PC to copy some files to a microSD card. All the files will be available from the server. The project download includes a simple website you can use. Put the card into the holder on the web platform before turning on the power.

Mchpdetect1.png

Apply power to the web platform. The server will announce its IP address at startup. Run Microchip’s MCHPDetect utility or a packet sniffer to grab the address or check your DCHP server's log file.

Ping1.png

The demo firmware includes an ICMP (ping) server. Ping it from the command line to verify that the server is alive and responding.

Browser.png

Now it’s time for the good stuff. Open a web browser and enter the server’s IP as the web address (http://192.168.1.135, for example). If no file is specified with the address, the server looks for index.htm in the root directory of the SD card. If you used the included demo site, you should see something like the image above. Click the links to test image and file transfers.


Downloadspeed.png

How fast will it go? We copied a 1.4MB file to the SD card, then downloaded it from the server with Firefox. The maximum download speed was 77 KB/sec, and it took a total of 17 seconds to complete the transfer. We ran a parity check against a PAR2 set to verify that the file arrived intact.

Browser2.png

If you used the Microchip demo web pages, then you should see something similar to the image above. Click the LEDs to turn them on/off and see them update in realtime. Note: not all the lefthand page links will work unless you configure additional options. Refer to the original Microchip TCPIPConfig.h file for more configuration details.

Dynamic variables

Microchip’s v2 HTTP server can display the status of pins, and control pins from web pages. Dynamic pages must first be processed with a utility that locates the variables and generates a helper index for the microcontroller. See .\Microchip Solutions\Microchip\Help\ in the Microchip TCP/IP stack source for an overview of the dynamic variable utility.

We tested the dynamic site demo included with the TCP/IP stack, but the dynamic features didn’t work. You need to copy the DynRcrd.bin and FileRcrd.bin files as well as the web pages onto the SD card to make the dynamic variables work.

Note 1: You need to run the convert-WebPages-to-MPFS.bat to recreate the file system if you change the files. The MPFS2.exe utility called from that batch file generates the needed DynRcrd.bin and FileRcrd.bin files. You need to do this before you (re)compile the project as it also (re)creates the HTTPPrint.h file (in theory, sometimes you need to manually delete this file first or it is not recreated). The HTTPPrint.h file is needed by the HTTP server.

Note 2: From Microchip TCP/IP Stack v5.36 (June 2011), the MPFS2.exe utility has morphed into the multi-platform MPFS2.jar which requires an installed Java runtime, but otherwise works the same except there's no need for a batch file. Be sure to check the MDD option.

microSD cards

Not all microSD cards are created equal. And they die. A test of the firmware that had worked flawlessly a day prior now reset constantly and died during pings. Single stepping with a debugger proved impossible because the PIC crashed randomly.

Eventually, after several hours of rechecking schematics, datasheets, and code, we tried a different microSD card. The server immediately roared to life. The ancient SD card we use for testing, predating even the web server on a business card, failed. It still appears to work in a PC, but it causes problems with the web platform circuit. SD cards purchased in the last year all seem to work fine.

Is there a lesson? When debugging, check everything once, even the unlikely stuff. If you have problems with the SD server demo, try a different SD card.