multiThreading not working

i am new to mbed os and was trying to get two threads running at the same time.
the idea is that two leds blink at a different interval.

this is what i came up with:

#include <mbed.h>
#include <rtos.h>
DigitalOut redLed(PTB18);
DigitalOut greenLed(PTB19);

Thread redLed_thread;
Thread greenLed_thread;

void thread_redLed()
{
  while (1)
  {
    redLed.write(1);
    ThisThread::sleep_for(1s);
    redLed.write(0);
    ThisThread::sleep_for(1s);
  }
}
void thread_greenLed()
{
  while (1)
  {
    greenLed.write(1);
    ThisThread::sleep_for(1s);
    greenLed.write(0);
    ThisThread::sleep_for(1s);
  }
}

int main()
{

  // put your setup code here, to run once:
  greenLed_thread.start(thread_greenLed);
  redLed_thread.start(thread_redLed);

  while (1)
  {

  }
}

if i upload it only the red led turns on.
what should i do?

thomas

Hello,

what is your target?
With Nucleo-F767ZI is working as expected.

BR, Jan

Hardware problem? Can you turn on the green LED by greenLed=0 or =1 in main?
Is the red LED always on or blinking?

It should be a hw problem. On one of my DISCO boards one of the LEDs is distorted. GND side of the LED is not soldered to the pad. If you are using an STM32 board you can simply try to print different messages in your therads. Here you can find an example of how to do it. Do not forget to override FileHandle* mbed::mbed_override_console(int)

thanks for responding,

my target is a NXP/Freescale FRDM KL25Z.

thanks for responding,

it is not a hardware problem. both leds work(i tested it)
the Red led is always on.

thanks for responding,

as i mentioned in the reply to @JojoS it is not a hardware problem.
about the FileHandle* mbed::mbed_override_console(int) is it a thing if i use platformIO is VScode?

ok, then it maybe a low memory problem. The default stacksize is 4 kB, try to start the threads with lower stack space. Jan’s F767 is a Jumbo compared to the KL25Z :slight_smile:
It can be set in the Thread constructor:
https://os.mbed.com/docs/mbed-os/v6.15/mbed-os-api-doxy/classrtos_1_1_thread.html#acd57cc8e3c47a4c0e400f80005426ade
or the defalt stack size can be changed, see Memory considerations:
https://os.mbed.com/docs/mbed-os/v6.15/apis/thread.html

but for a target with 16 k RAM I would use bare-metal profile and use Events. There you can schedule also cyclic activity.

1 Like

This override sipmply redirects the output of printf to wherever you want. My reply is a bit misleading. Any dev board should allow you to get the debug messages through a serial monitor. In the worst case you can use a ttl converter connected to one of the uarts to display the debug messages on your screen but I assumed that you do not have it. ST link allows you to do this through the programmer.

I know DAP link has also this feature but I never tried a freedom board with mbed. I recommended to try this just to double check whether your thread is working as expected. You can also try to toggle another pin if you have any instrument to verify whether it’s status is changing.

I would also recommend you to put a delay in your main thread. An infinite loop without any operation or delay keeps your MCU constantly busy. If you create more threads with simple operations but without delay you might start observing small drifts at the physical outputs.

usualy you can use simply printf(), that is redirected to a standard serial port. STM boards have a virtual COM where you can receive the messages. I don’t know if the FRDM boards have this feature also. Otherwise a serial USB converter is really helpful.
But printf needs also some more memory, so a too low stack setting gives the next problem… 2 kB stack should work.

Edit:
the board description in mbed says it has a VCOM, so simply add a printf(“hello from KL25Z”); at the begining of main or your threads.

I FOUND THE SOLUTION.

#include <mbed.h>

Thread RedLed(osPriorityNormal, 300, nullptr, nullptr);
Thread GreenLed(osPriorityNormal, 300, nullptr, nullptr);

void GreenBlink();
void RedBlink();


int main()
{

  // put your setup code here, to run once:
  RedLed.start(RedBlink);
  GreenLed.start(GreenBlink);

  while (1)
  {
    thread_sleep_for(10000);
  }
}

void RedBlink()
{

  DigitalOut RedLed(PTB18);

  while (1)
  {
    RedLed.write(1);
    thread_sleep_for(2000);
    RedLed.write(0);
    thread_sleep_for(2000);
  }
}

void GreenBlink()
{

  DigitalOut GreenLed(PTB19);

  while (1)
  {
    GreenLed.write(1);
    thread_sleep_for(100);
    GreenLed.write(0);
    thread_sleep_for(100);
  }
}

i changed the stack size and structured the code better. it now works like a charm!
thanks yall for helping me.

1 Like