# 2 tasks work, but 3 and more no

**URL:** <https://forums.freertos.org/t/2-tasks-work-but-3-and-more-no/5569>\
**Category:** Kernel\
**Created:** [November 18, 2013, 1:11pm UTC](https://forums.freertos.org/t/2-tasks-work-but-3-and-more-no/5569 "2013-11-18T13:11:01Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [November 18, 2013, 1:11pm UTC](https://forums.freertos.org/t/2-tasks-work-but-3-and-more-no/5569/1 "2013-11-18T13:11:01Z")

</div>

**[miklpolikov](https://sourceforge.net/u/miklpolikov/)** wrote on [Monday, November 18, 2013](https://sourceforge.net/p/freertos/discussion/382005/thread/f6d2a1df/#1197):

Good day everybody !

I am starting to use FreeRTOS. My configuration : CPU STM32L151, compiler KEIL, using heap\_2  
I have meat the problem : 1 or 2 tasks are work well, but three and more are not. After execute vTaskStartScheduler(); program get in while(1){} in the wery end of main{}.  
What am I do wrong ?

And the other question, where to find file errors.h with error codes that xTaskCreate( ) return ?  
I have find some file error.h in folder freertos, but there is no erroe with code 1 or -1 that I see.

I’m sorry, but I do not know why some lines are in so large font.

Thanks in advance!

#define configUSE\_PREEMPTION 1  
#define configUSE\_IDLE\_HOOK 0  
#define configUSE\_TICK\_HOOK 0  
#define configCPU\_CLOCK\_HZ ( ( unsigned long ) 16000000 )   
#define configTICK\_RATE\_HZ ( ( portTickType ) 1000 )  
#define configMAX\_PRIORITIES ( ( unsigned portBASE\_TYPE ) 2 )  
#define configMINIMAL\_STACK\_SIZE ( ( unsigned short ) 128 )  
#define configTOTAL\_HEAP\_SIZE ( ( size\_t ) ( 17\*128) )  
#define configMAX\_TASK\_NAME\_LEN ( 16 )  
#define configUSE\_TRACE\_FACILITY 0  
#define configUSE\_16\_BIT\_TICKS 0  
#define configIDLE\_SHOULD\_YIELD 1

void Task1 (void \*pvParameters)  
{  
char x;  
while(1)  
{  
x++;  
}  
}

void Task2 (void \*pvParameters)  
{  
char x;  
while(1)  
{  
x++;  
}  
}

void Task3 (void \*pvParameters)  
{  
char x;  
while(1)  
{  
x++;  
}  
}

void Task4 (void \*pvParameters)  
{  
char x;  
while(1)  
{  
x++;  
}  
}

void main()  
{  
xTaskCreate(Task1,(signed char\*)“Task1”,configMINIMAL\_STACK\_SIZE,NULL,tskIDLE\_PRIORITY + 1,NULL);  
xTaskCreate(Task2,(signed char\*)“Task2”,configMINIMAL\_STACK\_SIZE,NULL,tskIDLE\_PRIORITY + 1,NULL);  
xTaskCreate(Task3,(signed char\*)“Task3”,configMINIMAL\_STACK\_SIZE,NULL,tskIDLE\_PRIORITY + 1,NULL);  
xTaskCreate(Task4,(signed char\*)“Task4”, onfigMINIMAL\_STACK\_SIZE,NULL,tskIDLE\_PRIORITY + 1,NULL);

vTaskStartScheduler();

while(1){}  
}

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [November 18, 2013, 1:21pm UTC](https://forums.freertos.org/t/2-tasks-work-but-3-and-more-no/5569/2 "2013-11-18T13:21:31Z")

</div>

**[davedoors](https://sourceforge.net/u/davedoors/)** wrote on [Monday, November 18, 2013](https://sourceforge.net/p/freertos/discussion/382005/thread/f6d2a1df/#39f4):

Step through the function in the debugger, you will soon see why it returns. I expect you have run out of heap space and that is preventing the idle task (or the timer task) being created. [http://www.freertos.org/a00111.html](http://www.freertos.org/a00111.html)

---

<div class="post-metadata">

**Author:** ![richard-damon](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/richard-damon/32/38_2.png) [@richard-damon](https://forums.freertos.org/u/richard-damon)\
**Post date:** [November 18, 2013, 1:23pm UTC](https://forums.freertos.org/t/2-tasks-work-but-3-and-more-no/5569/3 "2013-11-18T13:23:39Z")

</div>

**[richard\_damon](https://sourceforge.net/u/richard_damon/)** wrote on [Monday, November 18, 2013](https://sourceforge.net/p/freertos/discussion/382005/thread/f6d2a1df/#6f8f):

The normal cause of this is running out of heap. Which heap routine are you linking with your code?

Note that Calling vTaskStartScheduler() (which I don’t see in your above sample) needs to create 1 or 2 more processes, the Idle task, and maybe the Timer task. Task stack sizes are specified in “words” but heap size in in bytes. I think your processor uses 32 bit stack words, so your 17\*128 byte heap has room for only 4 task stacks + 128 bytes, which may not be enough for 4 TCBs (or just barely and you are creating the timer task)

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [November 18, 2013, 1:49pm UTC](https://forums.freertos.org/t/2-tasks-work-but-3-and-more-no/5569/4 "2013-11-18T13:49:08Z")

</div>

**[miklpolikov](https://sourceforge.net/u/miklpolikov/)** wrote on [Monday, November 18, 2013](https://sourceforge.net/p/freertos/discussion/382005/thread/f6d2a1df/#6f8f/b62c):

Did I understand correctly ? :  
configMINIMAL\_STACK\_SIZE ( ( unsigned short ) 128 )  
means that one task nead 128 4-byte words stack becouse my CPU is 32-bit, and  
configTOTAL\_HEAP\_SIZE ( ( size\_t ) ( 17 \* 128) ) means that total heap is 17 \* 128=2176 bytes ?

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [November 18, 2013, 2:01pm UTC](https://forums.freertos.org/t/2-tasks-work-but-3-and-more-no/5569/5 "2013-11-18T14:01:40Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Monday, November 18, 2013](https://sourceforge.net/p/freertos/discussion/382005/thread/f6d2a1df/#917f):

> configMINIMAL\_STACK\_SIZE

The only place this constant is used by the kernel itself is when the idle task is created. It is representative of the smallest stack a task can realistically have. Naturally if you task calls library functions, or has a deep function call nesting depth, it will need to be larger. The demo applications use the constant to, for convenience, as they run on lots of different architectures.

As described [in the API documentation](http://www.freertos.org/a00125.html) the stack size is specified in the number of variables the stack can hold. On you device each stack variable is 4 bytes wide, so specifying that the stack should hold 100 values will result in a stack of 400 bytes being allocated.

> configTOTAL\_HEAP\_SIZE ( ( size\_t ) ( 17128) ) means that total heap is 17128=2176 bytes ?

I don’t understand this part of your comment. heap\_2 effectively uses a statically allocated char array as the heap. Each char is one bytes, so a heap of 17128 will be 17128 bytes.

Regards.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [November 18, 2013, 2:43pm UTC](https://forums.freertos.org/t/2-tasks-work-but-3-and-more-no/5569/6 "2013-11-18T14:43:15Z")

</div>

**[miklpolikov](https://sourceforge.net/u/miklpolikov/)** wrote on [Monday, November 18, 2013](https://sourceforge.net/p/freertos/discussion/382005/thread/f6d2a1df/#6400):

The sign \* was lost, I write that as I understand  
configTOTAL\_HEAP\_SIZE ( ( size\_t ) ( 17 \* 128) ) means that total heap is 17 \* 128=2176 bytes .

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [November 18, 2013, 3:17pm UTC](https://forums.freertos.org/t/2-tasks-work-but-3-and-more-no/5569/7 "2013-11-18T15:17:19Z")

</div>

**[miklpolikov](https://sourceforge.net/u/miklpolikov/)** wrote on [Monday, November 18, 2013](https://sourceforge.net/p/freertos/discussion/382005/thread/f6d2a1df/#ed61):

Thank You for explanations ! Thay help me !

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [November 18, 2013, 3:50pm UTC](https://forums.freertos.org/t/2-tasks-work-but-3-and-more-no/5569/8 "2013-11-18T15:50:31Z")

</div>

**[miklpolikov](https://sourceforge.net/u/miklpolikov/)** wrote on [Monday, November 18, 2013](https://sourceforge.net/p/freertos/discussion/382005/thread/f6d2a1df/#da51):

One more question.  
Why when I try to create one more task in body of runing task, program fails ?

#define configMINIMAL\_STACK\_SIZE ( ( unsigned short ) 128)  
#define configTOTAL\_HEAP\_SIZE ( ( size\_t ) ( 10 \* 128 \* 4) )

void Task2 (void \*pvParameters);

void Task1 (void _pvParameters)  
{  
char x;  
xTaskCreate(Task2,(signed char_)“Task2”,configMINIMAL\_STACK\_SIZE,NULL,tskIDLE\_PRIORITY+ 1, NULL);  
vTaskStartScheduler();  
while(1)  
{  
x++;  
}  
}

void Task2 (void \*pvParameters)  
{  
char x1;  
while(1)  
{  
x1++;  
}  
}

void main()  
{  
xTaskCreate(Task1,(signed char\*)“Task1”, configMINIMAL\_STACK\_SIZE,NULL,tskIDLE\_PRIORITY + 1, NULL);  
vTaskStartScheduler();

}

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [November 18, 2013, 4:02pm UTC](https://forums.freertos.org/t/2-tasks-work-but-3-and-more-no/5569/9 "2013-11-18T16:02:32Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Monday, November 18, 2013](https://sourceforge.net/p/freertos/discussion/382005/thread/f6d2a1df/#633b):

The formatting is not easy to follow - but it looks like you are calling vTaskStartScheduler() from a task. vTaskStartScheduler() should only ever be called once. Once the scheduler is running it cannot be re-started.

Regards.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [November 18, 2013, 4:17pm UTC](https://forums.freertos.org/t/2-tasks-work-but-3-and-more-no/5569/10 "2013-11-18T16:17:30Z")

</div>

**[miklpolikov](https://sourceforge.net/u/miklpolikov/)** wrote on [Monday, November 18, 2013](https://sourceforge.net/p/freertos/discussion/382005/thread/f6d2a1df/#b1e7):

Thank You, before I think that vTaskStartScheduler() should be implement after every xTaskCreate( )

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [November 18, 2013, 4:46pm UTC](https://forums.freertos.org/t/2-tasks-work-but-3-and-more-no/5569/11 "2013-11-18T16:46:44Z")

</div>

**[miklpolikov](https://sourceforge.net/u/miklpolikov/)** wrote on [Monday, November 18, 2013](https://sourceforge.net/p/freertos/discussion/382005/thread/f6d2a1df/#0166):

And the other one question :

Why my task work correctly only with static variables ? As I understand, all contest of task, i.e. CPU core registers and task variables are saved in stack, so directive static should influence nothing ?

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [November 18, 2013, 5:01pm UTC](https://forums.freertos.org/t/2-tasks-work-but-3-and-more-no/5569/12 "2013-11-18T17:01:19Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Monday, November 18, 2013](https://sourceforge.net/p/freertos/discussion/382005/thread/f6d2a1df/#b638):

Each task maintains its own context, and has its own stack, so declaring variables on the stack should not be a problem. If it works when you make them static then it probably means you have overflowed your stack. How big was the variable? Have you turned stack overflow protection on? Do you have configASSERT() defined?

Regards.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [November 18, 2013, 5:24pm UTC](https://forums.freertos.org/t/2-tasks-work-but-3-and-more-no/5569/13 "2013-11-18T17:24:46Z")

</div>

**[miklpolikov](https://sourceforge.net/u/miklpolikov/)** wrote on [Monday, November 18, 2013](https://sourceforge.net/p/freertos/discussion/382005/thread/f6d2a1df/#ea67):

The question:  
If I use in task, for exumple, 13 variables(char, int, long int ) and I shure that I not use there any libraries and so on, it means that when this task create by xTaskCreate( ) I can specify the value of parameter usStackDepth =13 , and it will be tha best way to economize memory space ? This is right ?

And If I use In this task xTaskCreate( ), vTaskDelete( ) how many stack size will need them ?

I use heap\_2.c

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [November 18, 2013, 6:01pm UTC](https://forums.freertos.org/t/2-tasks-work-but-3-and-more-no/5569/14 "2013-11-18T18:01:39Z")

</div>

**[dumarjo](https://sourceforge.net/u/dumarjo/)** wrote on [Monday, November 18, 2013](https://sourceforge.net/p/freertos/discussion/382005/thread/f6d2a1df/#ea67/bc65):

hi,

Le 2013-11-18 12:24, Mikhail\_P a écrit :

> The question:  
> If I use in task, for exumple, 13 variables(char, int, long int ) and  
> I shure that I not use there any libraries and so on, it means that  
> when this task create by xTaskCreate( ) I can specify the value of  
> parameter usStackDepth =13 , and it will be tha best way to economize  
> memory space ? This is right ?

no. You should have enough stackdepth to handle also the calling for  
function. Normay you should put a bigger number on your stackdepth and  
when you can get your stats working, you will be able to see you free  
stack from there.

Regards  
Jonathan
