Static variable causes hard fault?

01 Apr 2014

I am using Wifly interface and rtos. If I define like this " static TCPSocketConnection socket;" and then when i do socket.connect(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT), it will cause a hard fault. Specifically, it's IMPRECISERR. But if I define socket as a local variable. Every thing will be fine. Why does this happen??

01 Apr 2014

Hi Richard, Perhaps you could share your code? I'm using my derivative of the WiflyInterface, and with this code:

#include "mbed.h"
#include "WiflyInterface.h"

WiflyInterface eth(p28, p27, p23, p24, SSID, PASSCODE, WPA);
RawSerial pc(USBTX, USBRX);

static TCPSocketConnection socket;
#define ECHO_SERVER_ADDRESS "192.168.1.200"
#define ECHO_SERVER_PORT 80

DigitalOut myled(LED1);

int main() {
    pc.baud(460800);    // I like a snappy terminal, so crank it up!
    pc.printf("\r\nWiFly Static Socket - Build " __DATE__ " " __TIME__ "\r\n");
    do {
        eth.init(); // start it up as a client of my network using DHCP
        eth.baud(230400);
        if (eth.connect())
            break;
        pc.printf(" Failed to connect, retrying...\r\n");
        wait(1.0);
        eth.reset();
    } while (1);
    int i = socket.connect(ECHO_SERVER_ADDRESS, ECHO_SERVER_PORT);
    if (i == 0)
        pc.printf("connect success\r\n");
    while(1) {
        myled = 1;
        wait(0.2);
        myled = 0;
        wait(0.2);
    }
}

it prints this:

WiFly Static Socket - Build Apr  1 2014 15:31:25

connect success

Of course, the WiflyInterface constructor matches my baseboard and network. I also tried the same code with "static TCPSocketConnection socket;" inside main - no difference.

So, the other thing that comes to mind is if you have this defined in a thread - then even though static, perhaps the stack size may be a problem.