cancel
Showing results for 
Search instead for 
Did you mean: 

printf usage in stm32

Kumar B
Associate II

Hi,

I am using printf for my application to debug the output using UART.

But I am facing the problem.

void StartSender1(void const * argument)
 
{
 
/* USER CODE BEGIN 5 */
/* Infinite loop */
for(;;)
{
	printf("Sender1 Sending\n");
	osMessagePut(Queue1Handle,0x1,200);
	printf("Sender1 Delay\n");
	osDelay(1000);
}
/* USER CODE END 5 */
 
}
 
void StartSender2(void const * argument)
{
 
/* USER CODE BEGIN 5 */
/* Infinite loop */
for(;;)
{
	printf("Sender2 Sending\n");
	osMessagePut(Queue1Handle,0x2,200);
	printf("Sender2 Delay\n");
	osDelay(2000);
}
/* USER CODE END 5 */
 
}
 
/* USER CODE BEGIN Header_StartReceiver /
/*
 
    @brief Function implementing the Receiver thread.
 
    @param argument: Not used
 
    @retval None
    /
    / USER CODE END Header_StartReceiver /
    void StartReceiver(void const * argument)
    {
    / USER CODE BEGIN StartReceiver */
    osEvent retvalue;
 
    /* Infinite loop */
    for(;:wink:
    {
 
     retvalue=osMessageGet(Queue1Handle,2000);
     printf("Start Receiver Value : %d\n",(int)retvalue.value.p);
 
    }
    /* USER CODE END StartReceiver */
    }`

Now, I have asked in some forum and suggested this.

The library printf may not be thread safe, and may use excessive CPU time spin waiting on the communications channel, possibly even with interrupts disabled.

I personally NEVER use printf in embedded code. I have my own serial output routines that I can send message through to track the program, that I know have been made thread safe, and have know blocking characteristics.

Can printf be implemented in a thread safe manner?

Regards,

Kumar

2 REPLIES 2
Pavel A.
Evangelist III

> Can printf be implemented in a thread safe manner?

Yes, it can. But it is obviously not the most frugal thing in embedded. If you have other (smaller, cleaner) way to do the job and are comfortable with it, by all means use that.

-- pa

Piranha
Chief II

The printf() output can be targeted anywhere with your code. But in RTOS you'll most likely want to wrap it with some mutex protection anyway to not mix partial texts from different threads.