Skip to main content
sarling
Associate III
Apr 6, 2009 12:02 PM UTC
Question

What is going on?

Posted on April 06, 2009 at 14:02

What is going on?

    This topic has been closed for replies.

    16 replies

    jj_it
    Associate II
    May 17, 2011 11:08 AM UTC
    Posted on May 17, 2011 at 13:08

    @sarling

    Hi - interesting topic - thank you.

    Have one squawk - when one of our forum ''crew'' NEEDS this info next week/month - HOW will they find it with such (forgive me) ill-defined topic? Your findings are indeed of interest - of valu
    sarling
    sarlingAuthor
    Associate III
    May 17, 2011 11:08 AM UTC
    Posted on May 17, 2011 at 13:08

    Yes, that is the problem.

    I first tried with:

    Code:

    short *buf = (short *) malloc(SMPL2 * sizeof(short));

    which caused the stm32 to freeze, but:

    Code:

    short *buf = (short *) malloc(100 * sizeof(short));

    did not. Declaring buf as global also worked.

    Thank you! Damn, this one was hard to track down :)

    trevor1
    Associate III
    May 17, 2011 11:08 AM UTC
    Posted on May 17, 2011 at 13:08

    Declaring a large buffer like ''buf'' on the stack (i.e. local in a function) could be the problem. I suggest you check your stack size or declare the buffer global (or static).

    sarling
    sarlingAuthor
    Associate III
    May 17, 2011 11:08 AM UTC
    Posted on May 17, 2011 at 13:08

    This code:

    Code:

    const u16 SMPL2 = 1024;

    const u16 SMPL2 = 1000;

    short acc_y_raw[SMPL2];

    short vel_y[SMPL];

    .

    .

    void afunction() {

    short buf[SMPL2];

    for (i = 0; i < SMPL2; i++) {

    sum = 0;

    if (i < SMPL)

    {

    sum = 0;

    for (j = i; j < i+16; j++) {

    sum += acc_y_raw[j];

    }

    sum >>= 4;

    buf[i] = sum;

    sum = 0;

    }

    }

    }

    is what contaminates vel_y, if I remove buf[i] = sum; it doesn't. What am I missing?

    Even if I don't use buf[i]:

    Code:

    for (i = 0; i < SMPL; i++) {

    vel_y[i] = acc_y_raw[i];

    }

    I don't know if this is a hardware or a software error.

    sarling
    sarlingAuthor
    Associate III
    May 17, 2011 11:08 AM UTC
    Posted on May 17, 2011 at 13:08

    >This is why I asked you ''how is buf created'' and you dismissed it

    Ah. I apologize for that.

    Thanks for the tips.

    Kind regards,

    sarling

    trevor1
    Associate III
    May 17, 2011 11:08 AM UTC
    Posted on May 17, 2011 at 13:08

    Good news.

    This is why I asked you ''how is buf created'' and you dismissed it :(

    malloc will have failed (and crashed because you didn't check the return value) because the heap is not large enough. Like the stack size this is for you to configure.

    In embedded systems it is best to avoid using malloc if you can. Also I always fill the stack with a known value and check how much stack I have left unused after running the system for some time (aim for at least 20% free). You don't want release your product only to find that it crashes in the field due to stack overrun.

    Regards

    Trevor