0
votes

Today I developing RTOS (CMSIS RTOS) for kit STM32 nucleo L053R8. I have issue relate to multiple task.

I create 4 task(task_1, task_2, task_3, task_4), however only 3 task run.

This is part of my code:

#include "main.h"
#include "stm32l0xx_hal.h"
#include "cmsis_os.h"

osMutexId stdio_mutex;
osMutexDef(stdio_mutex);
int main(void){
     .....
     stdio_mutex = osMutexCreate(osMutex(stdio_mutex));
     osThreadDef(defaultTask_1, StartDefaultTask_1, osPriorityNormal, 0, 128);
     defaultTaskHandle = osThreadCreate(osThread(defaultTask_1), NULL);

     osThreadDef(defaultTask_2, StartDefaultTask_2, osPriorityNormal, 0, 128);
     defaultTaskHandle = osThreadCreate(osThread(defaultTask_2), NULL);


     osThreadDef(defaultTask_3, StartDefaultTask_3, osPriorityNormal, 0, 128);
     defaultTaskHandle = osThreadCreate(osThread(defaultTask_3), NULL);

     osThreadDef(defaultTask_4, StartDefaultTask_4, osPriorityNormal, 0, 600);
     defaultTaskHandle = osThreadCreate(osThread(defaultTask_4), NULL);
}

void StartDefaultTask_1(void const * argument){
    for(;;){
        osMutexWait(stdio_mutex, osWaitForever);
        printf("%s\n\r", __func__);
        osMutexRelease(stdio_mutex);
        osDelay(1000);
    }
}

void StartDefaultTask_2(void const * argument){
    for(;;){
        osMutexWait(stdio_mutex, osWaitForever);
        printf("%s\n\r", __func__);
        osMutexRelease(stdio_mutex);
        osDelay(1000);
    }
}

void StartDefaultTask_3(void const * argument){
    for(;;){
        osMutexWait(stdio_mutex, osWaitForever);
        printf("%s\n\r", __func__);
        osMutexRelease(stdio_mutex);
        osDelay(1000);
    }
}

void StartDefaultTask_4(void const * argument){
    for(;;){
        osMutexWait(stdio_mutex, osWaitForever);
        printf("%s\n\r", __func__);
        osMutexRelease(stdio_mutex);
        osDelay(1000);
    }
}

this is reslut in console (uart):

enter image description here

when I change stack size for task 4 from 600 -> 128 as below:

osThreadDef(defaultTask_4, StartDefaultTask_4, osPriorityNormal, 0, 128);
defaultTaskHandle = osThreadCreate(osThread(defaultTask_4), NULL);

then don't have any task run.

Actualy I want make many thread for my application, however this issue cause difficult to implement.

Could you let me know root cause of prblem? and how to resolve it.

Thank you advance!!

2
It smells like a stack problems. Check your settings. I would suggest something else than printf anyway. It is very stack & heap greedy and you run it on tiny micro, not on the PC0___________
first of all, thank you for respond, I use KITT STM32L053R8 and this is my setting _Min_Heap_Size = 0x200; _Min_Stack_Size = 0x400(1024 byte); configMINIMAL_STACK_SIZE = 128Cuong Hoang Van

2 Answers

1
votes

There is no common easy method of the stack calculation. It depends on many factors.

I would suggest to avoid stack greedy functions like printf scanf etc. Write your own ones, not as "smart" and universal but less resources greedy.

Avoid large local variables. Be very careful when you allocate the memory

0
votes

As your Suggestions, I checked by debug and see root cause is heap size is small.

I resolve by 2 method

  1. increase heap size: #define configTOTAL_HEAP_SIZE ((size_t)5120)

  2. decrease stack size: #define configMINIMAL_STACK_SIZE ((uint16_t)64)

    osThreadDef(defaultTask_6, StartDefaultTask_6, osPriorityNormal, 0, 64);

Do you know how to determine max of heap size? Please let me known.

Thank you so much