Mbed RTOS Multiple tasks are not running Nucleo-F072RB

Hi,

I’m starting to understand and learn about RTOS and I wanted to test a simple code to run two tasks in a Nucleo-F072RB, however, I am only able to run 1 task and I am not able to get to my task #2. I may need something else in my code but I would like to get some support from you guys and that way I could understand it better. (Hopefully).

#include "mbed.h"

 

DigitalOut led1(LED1);

DigitalOut led2(PC_1);

Thread thread1;

Thread thread2(osPriorityBelowNormal);

#define WAIT_T1 100

#define WAIT_T2 500

 

void led1_thread() {

    while (true) {

        led1 = !led1;

        ThisThread::sleep_for(WAIT_T1);

    }

}

 

void led2_thread() {

    while (true) {

        led2 = !led2;

        ThisThread::sleep_for(WAIT_T2);

    }

}

 

int main() {

    thread1.start(led1_thread);

    thread2.start(led2_thread);

}

What I want to do is to run both tasks, I know there is no simultaneously tasks running at the same time, but I am hoping to get one task… then run the other… and so on.

I am also aware that main() function is a main thread but I was hoping to handle tasks in different handlers (functions).

How can I do that?

I think that main needs a while loop with a sleep inside.

Hello Fabian,

Your code runs OK on a NUCLEO-F103RB (20kB SRAM). Maybe the NUCLEO-F072RB (16kB SRAM) doesn’t have sufficient SRAM to run correctly such program. To learn RTOS I would suggest to get a board with more SRAM (32kB and more). At the moment the STM32F401 (96kB SRAM, 4.55$ on eBay) and STM32F411 (128kB SRAM, 4.88$ on eBay) development boards seem to have a very good price-to-SRAM ratio. The STM32F407VET6 (128kB + 64kB SRAM) development board, with on-board SD card slot, 16Mbit SPI Flash memory and battery holder, could be also a good (low cost) choice. Then you can use the NUCLEO-F072RB as a programmer.

Best regards, Zoltan

Ahoj,

If it really the issue about memory resources, how Zoltan wrote, as an alternative you can try the EventQueue under the Mbed OS bare metal profile.

BR, Jan

Hello,

Thanks for your reply and considerations. I changed my code to print messages to my serial terminal:

#include "mbed.h"

 

//DigitalOut led1(LED1);
//
//DigitalOut led2(PC_1);

Thread thread1;

Thread thread2(osPriorityBelowNormal);

#define WAIT_T1 100

#define WAIT_T2 500

Serial pc(USBTX, USBRX);

void led1_thread() {

    while (true) {

//        led1 = !led1;
        pc.printf("hello-task-1\r\n");

        ThisThread::sleep_for(WAIT_T1);

    }

}

 

void led2_thread() {

    while (true) {

//        led2 = !led2;
        pc.printf("hello-task-2\r\n");

        ThisThread::sleep_for(WAIT_T2);

    }

}

 

int main() {

    thread1.start(led1_thread);

    thread2.start(led2_thread);

}

I also have other STM32 boards here to test, I used a Nucleo-F411RE and the code run it succesfully!

Then I tested my code in the Nucleo F072RB and I saw this in the terminal:

++ MbedOS Error Info ++
Error Status: 0x8001011F Code: 287 Module: 1
Error Message: Operator new[] out of memory

Location: 0x8004D31
Error Value: 0x1000hell
Current Thread: main  Id: 0x200014D4 Entry: 0x8005E79 StackSize: 0xC00 StackMem: 0x200003C8 SP: 0x20000F18

So yes, it is running out of memory.

I was thinking and I would like to know from your side if is possible to change the stack size and then maybe able to run several tasks in this board ?

Mostly because I previously tested FreeRTOS in STM32Cube framework and there I was able to run multiple tasks/threads (I simply ran 2 threads, did not test more than that).

You can supply the stacksize in the constructor of the Thread class, the default is 4 kB.