Task overflow while sending message through UART


I am using FreeRTOS and want to add a task to be used for UART communication.
But once the program goes this piece of code, which is used to send message to PC through UART, the vApplicationStackOverflowhook() is called.

    for(i = 0U; i < length; i++)
        // Wait until space is available in the transmit FIFO.
        while(SCI_getTxFIFOStatus(base) == SCI_FIFO_TX15)

        // Send a char.
        HWREGH(base + SCI_O_TXBUF) = array[i];

The function where this piece of code is at works fine before all tasks are created.

And no matter the stack overflow check method is set as 1 or 2, the same happens.

Does anyone know what might cause this? Thanks!


This means that you need to increase the stack size. vApplicationStackOverflowHook fucntion has pcTaskName parameter which tells you about which task’s stack is overflowing.

Thank you Gaurav for your reply.
Yeah, it looks like that, but it is a newly created task, still very small, without much space needed and doesn’t call any function recursively. In addition, I set a stack of 2K byte, it still goes there.

Did you check the name of the task in the stack overflow hook? Can you share the snippets of the code where you create your tasks?

Yes I checked the task in the stack overflow hook, it is the task.
Now I found that it is caused by the function which called the function I posted at the beginning. After the function is replaced, it works fine now. I haven’t found which part in the function caused that. It is a function I designed myself which is a simpler version of printf(). It works in other scenarios, but it caused stack overflow when it is called in the task. Probably it caused recursive calling somewhere, I am not sure. But anyway, I have something else to replace it for now. Thanks a lot Gaurav!

This is how the task is created:

#define STACK_SIZE 256U
static StaticTask_t uartTaskBuffer;
static StackType_t uartTaskStack[STACK_SIZE*8];
xTaskCreateStatic(Uart_TaskConfig, // Function that implements the task.
“UART Config task”, // Text name for the task.
STACK_SIZE, // Number of indexes in the xStack array.
( void * ) 1, // Parameter passed into the task.
tskIDLE_PRIORITY + 1, // Priority at which the task is created.
uartTaskStack, // Array to use as the task’s stack.
&uartTaskBuffer ); // Variable to hold the task’s data structure.

Glad that you figured it.

I still need to use my own function. Further investigation finds that some code in it causes stack overflow.
Some functions work before but don’t work any more when being called from another function. Some functions work occasionally and some work a few times. Some functions work when they are separated into small functions and don’t work when the code in small functions are putt together in one function.
When the stack size is reduced to 256 bytes as usual, some functions that worked before don’t work anymore and some functions that worked a few times before don’t work anymore.

well yes, that is the very nature of concurrent systems: Errors yield unpredictable and apparently random behavior due to different concurrent runtime execution sequences. Only way to prevent that is doing it right. :innocent:

Yes, understand.
I need to clarify that the functions I mentioned are all in one task and this task is not communicating with other tasks yet. So, in this case, might the cause of this stack overflow be relevant to concurrent situation? Anyway, I need to investigate why these functions are using so much space in stack.