mbed Reset Issue

07 Oct 2010

Hey,

I have a short bit of code that is causing my mbed to restart and I'm not sure why.

Every time the code returns from the sendCmd function, the chip resets. As you can see from the code, I get stuck in a infinite loop where the code almost finishes executing, but at the return from sendCmd, it resets (the power light flashes), and then it starts executing from the beginning. Sample output following the code.

Also, I inlined the sendCmd function and everything worked fine. I need to call the sendCmd function about 10 times, so I would hate to copy and paste that bit of code over and over.

 

#include "mbed.h"

Serial port(p9,p10);
Serial pc(USBTX, USBRX); // tx, rx
DigitalOut myled1(LED1);
DigitalOut myled2(LED2);

void sendCmd(char* cmd)
{
    char res[25];
    
    pc.printf("Sending\n\r");
    pc.printf(cmd);
    pc.printf("\n\r");
    
    wait_ms(50);
    port.printf(cmd);
    
    pc.printf("Sent. Waiting for response\n\r");
    
    while(res[0] != '+')
    {
        pc.printf("READ\n\r");
        
        port.scanf("%s", res);
    
        pc.printf("Got:");
        pc.printf(res);
        pc.printf("\n\r");
    }
    
    pc.printf("sendCmd RETURN\n\r");
    
    return;
}

int main() {

    pc.printf("HI!\n\r");
    
    char getFirmwareInfoCMD[] = "$4F00\r";
    char getCapabilitiesCMD[] = "$5000\r";
    
    char rfMode14443ACMD[] = "$58020C01\r";
    char rfFieldOnCMD[] = "$58020A01\r";
    
    char activateIdleCMD[] = "$4D00\r";
    
    char waitOneCardCMD[] = "$60070001FFFF0000FA\r";
    
    port.baud(38400);
    
    sendCmd( getFirmwareInfoCMD ); 
    
    pc.printf("DONE!\n\r");
    
    return 0;
}

Sample output:

 

HI!
Sending
$4F00

Sent. Waiting for response
READ
Got:$4F00
READ
Got:+00104B36333201532430FFFF0F043014E059

sendCmd RETURN
HI!
Sending
$4F00
Sent. Waiting
for response
READ
Got:$4F00
READ
Got:+00104B36333201532430FFFF0F043014E059
sendCmd RETUR
N
HI!
Sending
$4F00
Sent. Waiting for response

READ
Got:$4F00
READ
Got:+00104B36333201532430FFFF0F043014E059
sendCmd RETURN

07 Oct 2010 . Edited: 07 Oct 2010

Wow, so in simplifying the code to help someone else figure this out, I fixed the issue, sort of.

New code:

 

#include "mbed.h"

Serial port(p9,p10);
Serial pc(USBTX, USBRX); // tx, rx
DigitalOut myled1(LED1);

void sendCmd(char* cmd)
{
    char res[25];
    
    port.printf(cmd);
    
    port.scanf("%s", res);
    port.scanf("%s", res);
    pc.printf(res);
    pc.printf("\n\r");
    
    return;
}

int main() {

    //myled1 = 0;
    
    pc.printf("HI!\n\r");
    
    char getFirmwareInfoCMD[] = "$4F00\r";
    
    port.baud(38400);
    
    sendCmd( getFirmwareInfoCMD );
    
    myled1 = 1;
    
    return 0;
}

For some reason, this code still causes the reset, but if I uncomment that first line in main, "myled1 = 0", the mbed only resets 5 or 6 times, then it's fine.

 

Another fix is to exit out of Tera Term or whatever program has an open connection to the mbed's USB serial port. That is, if the mbed is in its loop where it is reseting like crazy, if I close the listener program on my PC, the mbed immediately stops resetting.

Any explanations?

Edit: It's also important to note that the problem is also realted to the data I am getting from my serial device. If I take out the second scanf in the sendCmd, things seem to work.

07 Oct 2010

Looks as though scanf might be overruning your 25-character buffer, "res," and therefore overwriting the stack.

07 Oct 2010

Geez. Not sure how I missed that. Thanks for catching it. Guess I just needed a second pair of eyes.