Hello,
I am using stm32wb board and freertos cmsis 2.0. I have implemented printf so it does print data into uart. In main function printf works fine, however if I try to print something from thread it hangs. Can nyone explain me why it happens and how can I fix it? This is only for debugging reason however it makes life easier. Hal_uart_transmit works fine from threads. Please take a look at my code below:
int __io_putchar(int ch)
{
uint8_t c[1];
c[0] = ch & 0x00FF;
HAL_UART_Transmit(&huart1, &*c, 1, 10);
return ch;
}
int _write(int file,char *ptr, int len)
{
int DataIdx;
for(DataIdx= 0; DataIdx< len; DataIdx++)
{
__io_putchar(*ptr++);
}
return len;
}
Whenever you access any IO port, be that a console or a UART or anything else, you are going to have to serialise the access. There are a few ways you can do that, for example use a mutex, but the mutex would have to be around the entire string write not just the individual characters. Another method would be to use something like a stream buffer - anything that wants to write to the output writes the message to the stream buffer, and then you have a task that drains the stream buffer and writes anything it obtains from the buffer to the output. Note stream buffers only expect one writer though, so then the mutex has to be around the stream buffer (so maybe that doesn’t gain anything?).
I am using sucessfully printf outside of the thread. There are no other threads than main with printf function. I don’t get why data serialization in this case might be a problem?
You mention it is handing, can you give more detail? For example, if
you break on the debugger, what is the code executing? Could it be
printf() is overflowing the stack and you are hanging in the stack
overflow hook function, or hitting an assert, or something similar?
Thanks Hein for your feedback! After spending several hours reading and getting into that solution I finally managed to get it working! Really helpful was this link http://www.nadler.com/embedded/newlibAndFreeRTOS.html