Skip to main content
Visitor II
June 27, 2024
Solved

Using printf() with FreeRTOS sometimes causes hardfaults

  • June 27, 2024
  • 3 replies
  • 5170 views

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.
    Best answer by nouirakh

    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.

    3 replies

    Graduate
    June 27, 2024

    Perhaps the stack of the task calling printf is overflowing.

    nouirakhAnswer
    ST Employee
    June 28, 2024

    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.

    Visitor II
    June 28, 2024

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

    Graduate II
    July 1, 2024

    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.

    Graduate II
    June 28, 2024

    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.