5 years, 11 months ago.  This question has been closed. Reason: Off Topic

Use of Thread.start() on STM32L072CZ never returns

Attempting to write a simple blinky using main() and a separate thread. Calling thread.start() simply never returns. If I comment out the call to ledThread.start(), main executes as expected.

#include "mbed.h"

#define X1(x) #x
#define XPAND(x) X1(x)

#pragma message(XPAND(MBED_VERSION))

#define LED_MASK 0b00100000

//Serial pc(USBTX, USBRX);
//#define print(...) pc.printf(__VA_ARGS__)
//

#ifdef TARGET_STM32L072CZ
#warning correct target
#endif

PortOut pa(PortA, LED_MASK);
PortOut pb(PortB, LED_MASK);

void ledTask() {

    while (true) {
        wait(1);
    }
    for(;;) {
        pa = LED_MASK;
        Thread::wait(100);
        pa = 0;
        Thread::wait(100);
        Thread::yield();
    }
}

int main() {

    printf("version %d %s %s\n", MBED_VERSION, __DATE__, __TIME__);
    Thread ledThread;

    printf("hi there thread id: %X\n", Thread::gettid());
    printf("About to start thread\n");

    ledThread.start(callback(ledTask));
    printf("After start thread\n");
    int cnt = 0;

    for(;;) {
        printf("main count: %d\n", ++cnt);
        pb = 0;
        Thread::wait(100);
        pb = LED_MASK;
        Thread::wait(100);
        Thread::yield();
    }
}

Serial output:

Terminal ready
version 50802 May  3 2018 13:42:08
hi there thread id: 20002760
About to start thread

I'm only having this problem with the default constructor, even using the same default values:

Thread ledThread(osPriorityNormal, 2048); < This works Thread ledThread; < this doesn't return

posted by Eric Poulsen 07 May 2018

Let's try this again why doesn't the "edit" link work??

<<code>> Thread ledThread(osPriorityNormal, 2048); < this works Thread ledThread; < this doesn't return <</code>

posted by Eric Poulsen 07 May 2018

Hi Eric, are you sure 2048 is the default? I just checked the code I pulled last week and I can see OS_STACK_SIZE == MBED_CONF_APP_THREAD_STACK_SIZE == 4096. Maybe the default size is larger than the target can take and the constructor is asserting? (Sorry but I don't have this target to try on.)

posted by Leon Ree 07 May 2018

Hello Eric, as Leon suggested, you might try to set the default stack size to 2048 and see how it performs with the default constructor:

int main() { 
    printf("version %d %s %s\n", MBED_VERSION, __DATE__, __TIME__);
    //Thread ledThread(osPriorityNormal, 2048);
    #define MBED_CONF_APP_THREAD_STACK_SIZE  2048
    Thread ledThread;
//...
}
posted by Zoltan Hudak 08 May 2018

@Leon, I think you're right. Using 4096 as the stack size causes the same problem

posted by Eric Poulsen 08 May 2018

1 Answer

5 years, 11 months ago.

Hi Eric,

It's not clear what's going wrong. The call to wait() in ledTask() should not be blocking so the main thread should be making progress. Could you add a print to the top of ledTask() to display the thread id as you are doing in main()?

We've taken this to another target and see the proper output:

version 50803 May  7 2018 17:06:15
hi there thread id: 20004D78
About to start thread
After start thread
ledTask - thread id: 20004D1C
main count: 1
main count: 2
main count: 3
main count: 4
main count: 5
main count: 6

I can't seem to get this comment system to format correctly. As I was trying to say above, if you use the default (no parens) constructor, start() never returns.

posted by Eric Poulsen 07 May 2018

It never prints.

As Leon above pointed out, the default stack size is 4096, which indeed does cause the same symptoms.

posted by Eric Poulsen 08 May 2018