Arm Mbed OS support forum

Issue with ThisThread::sleep_for when used in main()

Here is the code:

// Blinking rate in milliseconds
#define BLINKING_RATE 500ms
DigitalOut led(P_LEDG);
DigitalOut hw_ledr(P_LEDR);

void blink()
{
while(true)
{
led = 1;
ThisThread::sleep_for(BLINKING_RATE);
led = 0;
ThisThread::sleep_for(BLINKING_RATE);
}
}

int main()
{
Thread thread;
thread.start(callback(blink));
while (true) {
hw_ledr = 0;
// wait_us(500000);
ThisThread::sleep_for(BLINKING_RATE);
hw_ledr = 1;
// wait_us(500000);
ThisThread::sleep_for(BLINKING_RATE);
}
}

When compiled with ‘mbed compile’ using GCC compiler and run on LPC1768, this never returns from the first ThisThread::sleep_for(BLINKING_RATE); in main().

If I replace ThisThread::sleep_for(BLINKING_RATE); with wait_us(500000) in main() both leds flash as expected.

So ThisThread::sleep_for works in the blink() thread but not in main()
All the examples show Thread::sleep_for being used in main() - but for me it does not work!

Any ideas? what am I doing wrong?

Hello Andrew,

There are no P_LEDG, P_LEDR PinNames defined for the LPC1768.

  • After replacing P_LEDG with LED1 and P_LEDR with LED2
  • Adding
#include "mbed.h"
  • and compiling with GCC ARM compiler (make sure you use mbed-os 6),

both LEDs flash as expected on LPC1768.

A firmware update might help.

Thanks for your comment.

OK, my bad, I missed out the lines defining P_LEDG and P_LEDR - they are defined as port pins and there is, of course, #include “mbed.h” otherwise it wouldn’t compile!

#define P_LEDG P0_16
#define P_LEDR P2_8

I also missed out that this is our custom LPC1768 board. So there is no firmware to load - I am loading the output from compiling this code (the *.elf file) directly into the board using JTAG.

My observation is that

ThisThread::sleep_for

works fine when it is in the thread method.
But when it is used in main() it never returns. It’s almost like main() is not in a thread??

I originally started with the mbed-os-example-blinky and just modified for my led ports.

To have the LED1 in the main thread I replaced P_LEDR with LED1 and P_LEDG with LED2 and
it works also on these LPC1768 custom boards (with no firmware):

https://os.mbed.com/users/hudakz/code/LPCmini_Hello/

https://os.mbed.com/users/hudakz/notebook/home-made-mbed-board-with-lpc1768/