# Task periodicity

**URL:** <https://forums.freertos.org/t/task-periodicity/14799>\
**Category:** Kernel\
**Created:** [April 5, 2022, 9:22pm UTC](https://forums.freertos.org/t/task-periodicity/14799 "2022-04-05T21:22:47Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kishor](https://avatars.discourse-cdn.com/v4/letter/k/8edcca/32.png) [@Kishor](https://forums.freertos.org/u/Kishor)\
**Post date:** [April 5, 2022, 9:22pm UTC](https://forums.freertos.org/t/task-periodicity/14799/1 "2022-04-05T21:22:47Z")

</div>

I have created a project with below tasks and tested the periodicity of each task which was as expected, but when I changed the stack size of the tasks as shown in second part I observed task is not executing with fixed period.

Can you please let me know the exact root cause of the behavior and its solution.

```auto
#if 0
 /* all task executes with configured period*/
         xTaskCreate(vEv_20ms1,"Test_20ms1", 1000, NULL, 23, NULL);
         xTaskCreate(vEv_50ms1,"Test_50ms1", 100, NULL, 28, NULL);
         xTaskCreate(vEv_50ms2,"Test_50ms2", 100, NULL, 25, NULL);
         xTaskCreate(vEv_100ms1,"Test_100ms1", 100, NULL, 1, NULL);
         xTaskCreate(vEv_100ms2,"Test_100ms2", 100, NULL, 1, NULL);
         xTaskCreate(vEv_100ms3,"Test_100ms3", 100, NULL, 1, NULL);
         xTaskCreate(vEv_100ms4,"Test_100ms4", 100, NULL, 1, NULL);
         xTaskCreate(vEv_100ms5,"Test_100ms5", 100, NULL, 1, NULL);
         xTaskCreate(vEv_100ms6,"Test_100ms6", 100, NULL, 1, NULL);
         xTaskCreate(vEv_500ms1,"Test_500ms1", 100, NULL, 1, NULL);
         xTaskCreate(vEv_500ms2,"Test_500ms2", 100, NULL, 1, NULL);
         xTaskCreate(vEv_500ms3,"Test_500ms3", 100, NULL, 1, NULL);
         xTaskCreate(vEv_500ms4,"Test_500ms4", 100, NULL, 1, NULL);
         xTaskCreate(vEv_1000ms1,"Test_1000ms1", 100, NULL, 1, NULL);
         xTaskCreate(vEv_1000ms2,"Test_1000ms2", 100, NULL, 1, NULL);
 #else
 /* Task lost its periodicity*/
         xTaskCreate(vEv_20ms1,"Test_20ms1", 4000, NULL, 23, NULL);
         xTaskCreate(vEv_50ms1,"Test_50ms1", 200, NULL, 27, NULL);
         xTaskCreate(vEv_50ms2,"Test_50ms2", 500, NULL, 25, NULL);
         xTaskCreate(vEv_100ms1,"Test_100ms1", 1000, NULL, 21, NULL);
         xTaskCreate(vEv_100ms2,"Test_100ms2", 1000, NULL, 18, NULL);
         xTaskCreate(vEv_100ms3,"Test_100ms3", 200, NULL, 17, NULL);
         xTaskCreate(vEv_100ms4,"Test_100ms4", 4000, NULL, 16, NULL);
         xTaskCreate(vEv_100ms5,"Test_100ms5", 500, NULL, 15, NULL);
         xTaskCreate(vEv_100ms6,"Test_100ms6", 1000, NULL, 14, NULL);
         xTaskCreate(vEv_500ms1,"Test_500ms1", 500, NULL, 12, NULL);
         xTaskCreate(vEv_500ms2,"Test_500ms2", 400, NULL, 10, NULL);
         xTaskCreate(vEv_500ms3,"Test_500ms3", 400, NULL, 8, NULL);
         xTaskCreate(vEv_500ms4,"Test_500ms4", 1000, NULL, 6, NULL);
         xTaskCreate(vEv_1000ms1,"Test_1000ms1", 1000, NULL, 4, NULL);
         xTaskCreate(vEv_1000ms2,"Test_1000ms2", 500, NULL, 2, NULL);
#endif

```

FYI  
FreeRTOSConfig.h is as below

```auto
#define configUSE_PREEMPTION 1
#define configUSE_PORT_OPTIMISED_TASK_SELECTION	1
#define configUSE_FPU 1
#define configUSE_IDLE_HOOK 0
#define configUSE_TICK_HOOK 0
#define configUSE_TRACE_FACILITY 1
#define configUSE_16_BIT_TICKS 0
#define configCPU_CLOCK_HZ ( ( unsigned portLONG ) 80000000 ) /* Timer clock. */
#define configTICK_RATE_HZ ( ( TickType_t ) 1000 )
#define configMAX_PRIORITIES ( 30 )
#define configMINIMAL_STACK_SIZE ( ( unsigned portSHORT ) 128 )
#define configTOTAL_HEAP_SIZE ( ( size_t ) 250000 )
#define configMAX_TASK_NAME_LEN ( 16 )
#define configIDLE_SHOULD_YIELD 1
#define configGENERATE_RUN_TIME_STATS 1
#define configUSE_MALLOC_FAILED_HOOK 0

```

Thanks and advance,  
Kishor

---

<div class="post-metadata">

**Author:** ![jasonpcarroll](https://avatars.discourse-cdn.com/v4/letter/j/fbc32d/32.png) [@jasonpcarroll](https://forums.freertos.org/u/jasonpcarroll)\
**Post date:** [April 5, 2022, 10:07pm UTC](https://forums.freertos.org/t/task-periodicity/14799/2 "2022-04-05T22:07:27Z")

</div>

Hi @Kishor,  
Seeing as the stack sizes are much higher in the second part, my initial guess is that some of these tasks may not be created due to lack of memory. Have you tried checking the returns of all of your xTaskCreate calls to make sure that they are succeeding?  
Best,  
Jason Carroll

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [April 6, 2022, 4:41am UTC](https://forums.freertos.org/t/task-periodicity/14799/3 "2022-04-06T04:41:37Z")

</div>

> [@Kishor](#):
>
> I observed task is not executing with fixed period.

Can you elaborate on what you mean by this and how do you measure this?

---

<div class="post-metadata">

**Author:** ![Kishor](https://avatars.discourse-cdn.com/v4/letter/k/8edcca/32.png) [@Kishor](https://forums.freertos.org/u/Kishor)\
**Post date:** [April 6, 2022, 6:12am UTC](https://forums.freertos.org/t/task-periodicity/14799/4 "2022-04-06T06:12:36Z")

</div>

@jasonpcarroll sure I will check task creation return value, but the total stack is still less than heap configuration (250k). MCU has total 512k.

@aggarg I am testing periodic executions by switching GPIO and observed on logic analyzer.

Regards,  
Kishor

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [April 6, 2022, 6:33am UTC](https://forums.freertos.org/t/task-periodicity/14799/5 "2022-04-06T06:33:54Z")

</div>

I see that you are not only changing stack sizes but also priorities. Can you keep the priorities same and check?

---

<div class="post-metadata">

**Author:** ![Kishor](https://avatars.discourse-cdn.com/v4/letter/k/8edcca/32.png) [@Kishor](https://forums.freertos.org/u/Kishor)\
**Post date:** [April 6, 2022, 9:15pm UTC](https://forums.freertos.org/t/task-periodicity/14799/6 "2022-04-06T21:15:11Z")

</div>

As mentioned in query

Case 1: Working version maximum task almost with same stack and priority  
Result: Works normally  
Case 2: Changed stack and priority  
Result: Works with abnormal period

Case3: Changed only stack size with respect to working version  
Result: Get stuck at b dataEntry

Case4: Changed only priority with respect to working version  
Result: executes with expected period, normal behaviour

If its due to data exception, can you please guide me how to fix it? Its debug procedure.

heap configured is more than the total stack configured by all task and there is no function in any task so stack usage is might be around 20 30 bytes.  
How can I get max stack usage by each task?

Thank you very much  
Kishor

---

<div class="post-metadata">

**Author:** ![aggarg](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/aggarg/32/4245_2.png) [@aggarg](https://forums.freertos.org/u/aggarg)\
**Post date:** [April 7, 2022, 5:53am UTC](https://forums.freertos.org/t/task-periodicity/14799/7 "2022-04-07T05:53:47Z")

</div>

> [@Kishor](#):
>
> If its due to data exception, can you please guide me how to fix it? Its debug procedure.

That would depend on the hardware you are using and what information it provides on a crash. Which hardware are you using? Another way could be to disable/enable tasks one by one and try to determine the culprit one.

> [@Kishor](#):
>
> How can I get max stack usage by each task?

You can use the [uxTaskGetStackHighWaterMark](https://www.freertos.org/uxTaskGetStackHighWaterMark.html) API.
