Skip to main content
Associate
Jun 27, 2024 9:32 PM UTC
Solved

Using printf() with FreeRTOS sometimes causes hardfaults

I am working on using FreeRTOS for a project, and have implemented printf() debugging by redirecting it to a uart. This works great most of the time, and is very helpful for FreeRTOS debugging, as FreeRTOS_printf() and FreeRTOS_debug_printf() will leverage this functionality.

Unfortunately, it seems to cause hard faults frequently, but not in every case. 

One call that consistently caused a hard fault was 

FreeRTOS_printf( ( "FreeRTOS_connect: %u to %xip:%u\n", [...]);

Another one I had to manually remove was

FreeRTOS_debug_printf( ( "Socket %u -> [%s]:%u State %s->%s\n", [...]);

I came across a previous post which seemed to have a similar issue, but it appeared to have been resolved. 

Is there more configuration that I need to implement to have this work in every case? Or is there something else I am missing?

 

Some more details about my implementation:

I am using CubeMX to generate my code

I am using OpenOCD and GCC

My printf implementation uses the following code

#ifdef __GNUC__
# define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
# define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
PUTCHAR_PROTOTYPE
{
HAL_UART_Transmit(&huart3, (uint8_t *)&ch, 1, 0xFFFF);

return ch;
}

 Thanks for reading, any insight is appreciated!

This topic has been closed for replies.
nouirakh
nouirakhBest answer
ST Employee
Jun 28, 2024 9:44 AM UTC

Hello @skygreenslade 

Could you please verify that you give the task that's calling FreeRTOS_printf() enough stack space. Printing functions often use a lot of stack space to format the string.

6 replies

Chris21
Associate II
Jun 27, 2024 9:37 PM UTC

Perhaps the stack of the task calling printf is overflowing.

.
nouirakh
nouirakhBest answer
ST Employee
Jun 28, 2024 9:44 AM UTC

Hello @skygreenslade 

Could you please verify that you give the task that's calling FreeRTOS_printf() enough stack space. Printing functions often use a lot of stack space to format the string.

Associate
Jun 28, 2024 6:24 PM UTC

It appears that this was the problem. Increasing my stack size has resolved the issue, thanks for everyone's help!

unsigned_char_array
Lead III
Jul 1, 2024 8:16 AM UTC

Could you tell us what exact stack size you used before and what you have now? This can help others with the same problem in the future. Thanks in advance.

"Kudo posts if you have the same problem and kudo replies if the solution works.Click ""Accept as Solution"" if a reply solved your problem. If no solution was posted please answer with your own."
AMars.4
Associate III
Jun 28, 2024 12:49 PM UTC

Are you calling the print function from multiple tasks? I use a print from multiple tasks with no issues, maybe try something like below:

 

static void debugPrintUART(const char *pData);


void myprintf(const char *fmt, ...)
{
 if(osMutexAcquire(debugPrint_MutexHandle, osWaitForever) == osOK)
 {
 static char buffer[256];
 va_list args;
 va_start(args, fmt);
 vsnprintf(buffer, sizeof(buffer), fmt, args);
 va_end(args);

 debugPrintUART(buffer);
 osMutexRelease(debugPrint_MutexHandle);
 }
}

static void debugPrintUART(const char *pData)
{
 HAL_UART_Transmit(&huart1, (uint8_t*)&pData[0], strlen(pData), 100);
}

 

Or you could have the debug print in a single task, and have the other tasks just message this one to do the print.. this may actually be a better alternative.

Task stack size maybe an issue as others mention.