# task creation problem

**URL:** <https://forums.freertos.org/t/task-creation-problem/1653>\
**Category:** Kernel\
**Created:** [October 9, 2014, 11:08am UTC](https://forums.freertos.org/t/task-creation-problem/1653 "2014-10-09T11:08:32Z")\
**Posts on this page:** 16\
**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:** [October 9, 2014, 11:08am UTC](https://forums.freertos.org/t/task-creation-problem/1653/1 "2014-10-09T11:08:32Z")

</div>

**[tomirtos](https://sourceforge.net/u/tomirtos/)** wrote on [Thursday, October 09, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/47ed0312/#2959):

Hi everyone!

I had a hard time to get to this point in my project, and I’ve been halted again.  
My simple code compiles fine, yet when I upload it to my microcontroller it won’t do anything it is supposed to do.

I am using a dsPIC33FJ128MC802 microcontroller with MPLABx xc16 compiler and microstick2 test board. I managed to configure one of the PIC24 sample project to my dsPIC. Also I can blink leds on their own and I included my own UART functions that send characters to the console and that also works fine under the RTOS.

The problem starts when I want to start making tasks. I have no idea how to even start looking for the source of the error. Any help would be really appreciated!

So here is my line of code:

void send\_task(void \*p)  
{  
while (1)  
{

```
   unsigned char data='k';
    UART1PutChar(data);
   
}

```

}

int main (void)  
{

```
pll_setup(); // makes 40 MIPS
init_UART1(); // Initialize UART1 for 9600,8,N,1 TX/RX
prvSetupHardware();

//make task
xTaskCreate(send_task, "send_task", 4096, 0, 1, 0);

vTaskStartScheduler();

//UART TEST, this works by itself

```

/\* while (1)  
{  
char data=‘k’;  
UART1PutChar(data);

```
}*/

return 0;

```

}

---

<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:** [October 9, 2014, 11:22am UTC](https://forums.freertos.org/t/task-creation-problem/1653/2 "2014-10-09T11:22:13Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Thursday, October 09, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/47ed0312/#90f7):

I’ve read this twice so am pretty sure you make no mention of what the problem actually is, so I’m left at guessing…

…and my guess would be that the task is not even being created because you have requested a massive stack size - which by itself would consume half of the RAM available on that part.

Have you:

1. Checked the return value of [xTaskCreate()](http://www.freertos.org/a00125.html).
2. Stepped into the xTaskCreate() function in the debugger to actually see why it is failing, if indeed it is failing.
3. Defined a malloc failed hook to see when you run out of RAM?

Regards.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [October 9, 2014, 11:42am UTC](https://forums.freertos.org/t/task-creation-problem/1653/3 "2014-10-09T11:42:49Z")

</div>

**[tomirtos](https://sourceforge.net/u/tomirtos/)** wrote on [Thursday, October 09, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/47ed0312/#4791):

Sorry you are right, I’ll try and explain what it should do:

So I should be able to see from my serial port monitor the letter “k” beeing sent. If I am not using my uart function in a task it does just that, but if I put it in a task, nothing appears on the serial monitor. In my compiler the xTaskCreate line is underlined in red, meaning it is not recognised. But as I said the code compiles without errors.

If I set a breakpoint at xTaskCreate and save it to a variable the variable will be equal to 0.

If I enter debugging mode, the program halts and I have to manually press the green continue button. I see these messages on the debugger console:

No source code lines were found at current PC 0x3d0  
User program stopped  
User program running  
No source code lines were found at current PC 0x0  
User program stopped  
User program running  
No source code lines were found at current PC 0x3d0  
User program stopped

---

<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:** [October 9, 2014, 11:57am UTC](https://forums.freertos.org/t/task-creation-problem/1653/4 "2014-10-09T11:57:23Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Thursday, October 09, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/47ed0312/#4791/5dd8):

> If I set a breakpoint at xTaskCreate and save it to a variable the  
> variable will be equal to 0.

Does that mean if you save the return value of xTaskCreate() to a  
variable, then the return value is 0? In which case xTaskCreate() is  
failing probably for the reason stated in my previous reply.

Regards.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [October 9, 2014, 12:16pm UTC](https://forums.freertos.org/t/task-creation-problem/1653/5 "2014-10-09T12:16:03Z")

</div>

**[tomirtos](https://sourceforge.net/u/tomirtos/)** wrote on [Thursday, October 09, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/47ed0312/#8bb2):

Thank you so far for the help.  
So I’ve reduced the stack to 512. If I step into the xTaskCreate function during debugging I can see it returns with the xReturn variable beeing 1. So I guess that means it is beeing created. Unfortunately still nothing appears on my console.

---

<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:** [October 9, 2014, 12:49pm UTC](https://forums.freertos.org/t/task-creation-problem/1653/6 "2014-10-09T12:49:33Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Thursday, October 09, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/47ed0312/#8bb2/510c):

…and you are writing to the UART from inside the task? The code you  
pasted shows the UART being used after vTaskStartScheduler() is called.  
If all is well vTaskStartScheduler() will not return, so any code  
after that will not execute.

Regards,  
Richard.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [October 9, 2014, 1:21pm UTC](https://forums.freertos.org/t/task-creation-problem/1653/7 "2014-10-09T13:21:41Z")

</div>

**[tomirtos](https://sourceforge.net/u/tomirtos/)** wrote on [Thursday, October 09, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/47ed0312/#0ecf):

No, i was not writing the UART after the vTaskStartScheduler. It is commented out. I left it in there only to show that that line of code was working on its own without beeing in a task. So my UART functions are working fine and the problem is with them beeing in a task.

I am writing to the UART in the send\_task function.

---

<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:** [October 9, 2014, 1:36pm UTC](https://forums.freertos.org/t/task-creation-problem/1653/8 "2014-10-09T13:36:34Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Thursday, October 09, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/47ed0312/#0ecf/cbce):

If you put a break point inside the task - does it get hit (showing the  
task executes)? Another way of seeing if the task is executing is to  
declare a global volatile variable, then increment the variable on each  
loop of the task.

If the task is being executed, can you post the code for UART1PutChar()

- or at least say whether it is using interrupt or not.

Regards.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [October 9, 2014, 1:47pm UTC](https://forums.freertos.org/t/task-creation-problem/1653/9 "2014-10-09T13:47:15Z")

</div>

**[tomirtos](https://sourceforge.net/u/tomirtos/)** wrote on [Thursday, October 09, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/47ed0312/#52aa):

I tried putting a break point inside sender\_task function. It seems my program never reaches that break point. This is probably not a good sign…

This is the code for the UART1PutChar: It utilizes no interrupts.

void UART1PutChar(char Ch)  
{  
//transmit ONLY if TX buffer is empty  
while(U1STAbits.UTXBF == 1);  
U1TXREG = Ch;  
}

---

<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:** [October 9, 2014, 1:53pm UTC](https://forums.freertos.org/t/task-creation-problem/1653/10 "2014-10-09T13:53:24Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Thursday, October 09, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/47ed0312/#52aa/bcd6):

UART1PutChar() is as simple as can be, so can’t be the cause of the  
problem - but it seems the task is not actually running in the first  
place. The next thing to check is whether vTaskStartScheduler()  
returns. If it returns it would again be a symptom of there not being  
enough heap space. Did you define a malloc failed hook so you would  
know immediately if this was the case?

Regards.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [October 9, 2014, 7:57pm UTC](https://forums.freertos.org/t/task-creation-problem/1653/11 "2014-10-09T19:57:26Z")

</div>

**[tomirtos](https://sourceforge.net/u/tomirtos/)** wrote on [Thursday, October 09, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/47ed0312/#a5a9):

Sorry I had to leave and couldn’t continue working on my project. I’m afraid I don’t think I know how to define a malloc failed hook, or understand what that really means. I will try out the global volatile variable solution.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [October 9, 2014, 8:04pm UTC](https://forums.freertos.org/t/task-creation-problem/1653/12 "2014-10-09T20:04:42Z")

</div>

**[edwards3](https://sourceforge.net/u/edwards3/)** wrote on [Thursday, October 09, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/47ed0312/#fe12):

Set configUSE\_MALLOC\_FAILED\_HOOK to 1 in FreeRTOSConfig.h, then add a function called vApplicationMallocFailedHook() into your code. If a call to pvPortMalloc in the xTaskCreate() function fails it will call vApplicationMallocFailedHook() so if you implement it like this

void vApplicationMallocFailedHook(void){  
portDISABLE\_INTERRUPTS();  
while(1);  
}

you will end up in the while(1) if it is called.

See [http://www.freertos.org/a00016.html](http://www.freertos.org/a00016.html)

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [October 10, 2014, 8:48am UTC](https://forums.freertos.org/t/task-creation-problem/1653/13 "2014-10-10T08:48:06Z")

</div>

**[tomirtos](https://sourceforge.net/u/tomirtos/)** wrote on [Friday, October 10, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/47ed0312/#8d55):

Thank you for the explanation! I will try it out now, but first I did the global variables:

I declared three variables. ‘A’ is placed inside the send\_task, and it never gets incremented, so the program never gets there.  
B is between the taskcreate and taskscheduler, it only gets incremented once and C is after the task scheduler, which never gets incremented. So this means that the program is in vTaskStartScheduler.

I also get this message when in debugger mode in call stack view:  
No source code lines were found at current PC 0x29e . When I hit continue button it stops again with the same message except now PC is at 0x00. And if I keep hitting continue it keeps on toggling between these to addresses. Maybe this is the source of the problem aswell.

This is my source code for global variables.

volatile static int A=0;  
volatile static int B=0;  
volatile static int C=0;

void send\_task(void \*p)  
{  
while (1)  
{  
A=A+1; //never gets incremented  
unsigned char data=‘k’;  
UART1PutChar(data);  
vTaskDelay(500);  
}  
}

int main (void)

{

```
pll_setup(); // set internal oscillator to 80MHz-->40MIPS FCY=Fosc/2
init_UART1(); // UART enable

```

xTaskCreate(send\_task, “send\_task”, 512, NULL, 1, NULL);

B=B+1; // only gets incremented once

vTaskStartScheduler();

C=C+1; //never gets incremented

return 0;

}

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [October 10, 2014, 9:03am UTC](https://forums.freertos.org/t/task-creation-problem/1653/14 "2014-10-10T09:03:27Z")

</div>

**[tomirtos](https://sourceforge.net/u/tomirtos/)** wrote on [Friday, October 10, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/47ed0312/#ea6b):

I tried out the malloc failed hook function. Implemented as you suggested. One thing was strange that I didn’t have a predefined configUSE\_MALLOC\_FAILED\_HOOK in my FreeRTOSConfig.h file, but I created one.

So when I put a break point in vApplicationMallocFailedHook function the compiler never hits it and also I never enter the while(1) loop. So I don’t think its a memory allocation problem.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [October 10, 2014, 9:10am UTC](https://forums.freertos.org/t/task-creation-problem/1653/15 "2014-10-10T09:10:19Z")

</div>

**[davedoors](https://sourceforge.net/u/davedoors/)** wrote on [Friday, October 10, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/47ed0312/#c64f):

> No source code lines were found at current PC 0x29e

I think you need to find the reason for that first. It sounds like the code is running up to a point, then simply crashing. Could be a linker script problem. You know the code runs up to B=B+1, so step through the code from there until you find the code line that jumps into the weeds.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [October 10, 2014, 9:50am UTC](https://forums.freertos.org/t/task-creation-problem/1653/16 "2014-10-10T09:50:56Z")

</div>

**[tomirtos](https://sourceforge.net/u/tomirtos/)** wrote on [Friday, October 10, 2014](https://sourceforge.net/p/freertos/discussion/382005/thread/47ed0312/#b009):

Thank you so far for the help. I tried what you suggested and I think this is where I lose where the program is going:  
The program enters xPortStartScheduler. It reaches the break point at portRESTORE\_CONTEXT();  
If I set the next breakpoint to be at asm volatile (“return”); it will never get hit.  
If I set the break point at return pdTRUE it wont get hit either.

I tried setting breakpoints where portRESTORE\_CONTEXT(); is defined but for some reason I can’t make any breakpoints there.

BaseType\_t xPortStartScheduler( void )  
{  
/\* Setup a timer for the tick ISR. \*/  
vApplicationSetupTickTimerInterrupt();

```
/* Restore the context of the first task to run. */
portRESTORE_CONTEXT();

/* Simulate the end of the yield function. */
asm volatile ( "return" );

/* Should not reach here. */
return pdTRUE;

```

}
