Problem using ethernet and sd-card together (FRDM-K64F)

09 Jun 2014

I'm trying to get a simple web server woking on a FRDM-K64F board.

I'm using the HTTPServer_echoback program as a starting point. I have replaced mbed with mbed-src and updated all the libraries. The HTTPServer_echoback program works as expected.

I then modified the code to read a file from an SD-card (using SDFileSystem) and return that instead of echoing the header. My code for reading the SD-card works in a separate program, but in the modified HTTP server program it does not work.

With my extra code it will not get an IP trough DHCP, it will not answer ping and the LED indicating that the server is listening for HTTP requests does not blink. Note that it does not work normally until it gets to my code and then fails. If my code is there it will not work at all.

Anyone know what's going on?

#include "mbed.h"
#include "EthernetInterface.h"
#include <stdio.h>
#include <string.h>
#include <SDFileSystem.h>

#define PORT   80

SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); //MOSI, MISO, SCLK, SSEL. Tested on K64F, correct pins.

EthernetInterface eth;

TCPSocketServer svr;
bool serverIsListened = false;

TCPSocketConnection client;
bool clientIsConnected = false;

DigitalOut led1(LED1); //server listning status
DigitalOut led2(LED2); //socket connecting status

Ticker ledTick;

int i = 0;
char c;
char fileContent[1024] = {};

void ledTickfunc()
{
    if(serverIsListened)  {
        led1 = !led1;
    } else {
        led1 = false;
    }
}

int main (void)
{
    ledTick.attach(&ledTickfunc,0.5);

    //setup ethernet interface
    eth.init(); //Use DHCP
    eth.connect();
    printf("IP Address is %s\n\r", eth.getIPAddress());

    //setup tcp socket
    if(svr.bind(PORT)< 0) {
        printf("tcp server bind failed.\n\r");
        return -1;
    } else {
        printf("tcp server bind successed.\n\r");
        serverIsListened = true;
    }

    if(svr.listen(1) < 0) {
        printf("tcp server listen failed.\n\r");
        return -1;
    } else {
        printf("tcp server is listening...\n\r");
    }

    //listening for http GET request
    while (serverIsListened) {
        //blocking mode(never timeout)
        if(svr.accept(client)<0) {
            printf("failed to accept connection.\n\r");
        } else {
            printf("connection success!\n\rIP: %s\n\r",client.get_address());
            clientIsConnected = true;
            led2 = true;
            
            while(clientIsConnected) {
                char buffer[1024] = {};
                switch(client.receive(buffer, 1023)) {
                    case 0:
                        printf("recieved buffer is empty.\n\r");
                        clientIsConnected = false;
                        break;
                    case -1:
                        printf("failed to read data from client.\n\r");
                        clientIsConnected = false;
                        break;
                    default:
                        printf("Recieved Data: %d\n\r\n\r%.*s\n\r",strlen(buffer),strlen(buffer),buffer);
                        if(buffer[0] == 'G' && buffer[1] == 'E' && buffer[2] == 'T' ) {
                            printf("GET request incomming.\n\r");
                            char echoHeader[256] = {};  
                            
                            if (buffer[5] == ' ') {     //No file name specified, let's return index.html
                                
                                //Uncoment the next block to make the program stop working.
                                //The program will then fail to get an IP trough DHCP, it will not answer ping and the LED will not blink.
                                
                                /*                                             
                                i = 0;                                   
                                FILE *fp = fopen("/sd/index.html", "r");
                                while (!feof(fp)){                        
                                    c = fgetc(fp);                         
                                    fileContent[i] = c;
                                    i++;
                                }                                                          
                                fclose(fp);                               /                               
                                fileContent[i] = NULL;                                                       
                                */
                                
                                sprintf(echoHeader,"HTTP/1.1 200 OK\n\rContent-Length: %d\n\rContent-Type: text\n\rConnection: Close\n\r\n\r",strlen(fileContent));
                                client.send(echoHeader,strlen(echoHeader)); 
                                client.send(fileContent,strlen(fileContent));
                                                               
                            } else {
                                //Just echos the request if it's something else.                                                                                     
                                sprintf(echoHeader,"HTTP/1.1 200 OK\n\rContent-Length: %d\n\rContent-Type: text\n\rConnection: Close\n\r\n\r",strlen(buffer));
                                client.send(echoHeader,strlen(echoHeader)); 
                                client.send(buffer,strlen(buffer));
                                clientIsConnected = false;
                                printf("echo back done.\n\r");                               
                            }                          
                            
                        }
                        break;
                }
            }
            printf("close connection.\n\rtcp server is listening...\n\r");
            client.close();
            led2 = false;
        }
    }
}

10 Jun 2014

I'v done some more testing and it looks like this is not just using an SD card that can be a problem together with Ethernet.

I declared a digital output and toggled it in the main loop, planning to measure how fast and consistently it could be toggled while using ethernet.

DigitalOut testpin(PTB2);

In the main loop:

testpin = !testpin;

This code casues the same problem as described in my first post. If I remove the line toggling the pin, leaving only the declaratin, it still causes the same problem..

10 Jun 2014

Hello,

what version of mbed-src does your program use?

10 Jun 2014

Thank you for your reply,

It used version 222 but I updated to 223 today with no change.

19 Jun 2014

Hi,

how did you get this HTTPServer_echoback running with the FRDM-K64F board? I also tried it and it does not compile because of Error: #error directive: The Ethernet Interface library is supported only on the mbed NXP LPC1768 in "EthernetInterface/EthernetInterface.h", Line: 24, Col: 2

19 Jun 2014

I updated all the libraries in the project to the latest version and replaced mbed with mbed-src.

20 Jun 2014

I tried this too now. I received now another error: Error: Undefined symbol k64f_init_eth_hardware (referred from k64f_emac.c.K64F.o).

Frustrating...