Loading...
 

Zephyr project on STM32

   Zephyr Workbench, a VSCode extension to manage Zephyr on STM32.
It enables users to easily create, develop, and debug Zephyr applications.
Main features:
  • Install host dependencies.
  • Import toolchain and SDK.
  • Create, configure, build and manage apps.
  • Debug STM32.
You can directly download it from the VSCode marketplace
For more details, visit the Zephyr Workbench

System Workbench for STM32


Redirect Printf through UART2

HI there,

I’m looking for the redirection of printf to UART2.

My MCU is a STM32F103VDT and I use UART2 to print debug on serial console.

On Keil I used this code to redirect the printf trough UART2:

int fputc(int ch, FILE *f)
{
/* Place your implementation of fputc here */
/* e.g. write a character to the USART */

if ((char)ch == ‘\n’) /* Add a LF before \n — LF -> CR LF */
{
HAL_UART_Transmit(&huart2,(uint8_t*)”\r”,1,1);
}

HAL_UART_Transmit(&huart2,(uint8_t*)&ch,1,1); // 1 tick waiting (1ms) enough for 87us/byte at 115200

return ch;
}



But now this code not works when I compile with System WorkBench AC6.

Exploring the forum I found a hint regarding the redirection of UART.

It’s suggest to use this code to redirect the printf (I add my code into the function):

void __io_putchar(uint8_t ch)
{
/* Place your implementation of fputc here */
/* e.g. write a character to the USART */

if ((char)ch == ‘\n’) /* Add a LF before \n — LF -> CR LF */
{
HAL_UART_Transmit(&huart2,(uint8_t*)”\r”,1,1);
}

HAL_UART_Transmit(&huart2,(uint8_t*)&ch,1,1); // 1 tick waiting (1ms) enough for 87us/byte at 115200

//return ch;
}


But this code not works.

Any suggestion?

Thanks


Are you creating the project through CubeMX then importing it in?
Im using the F7 and also had some headaches to get the printf working, but have finally suceeded ;

When you make a project via SW4STM32, it includes a file in there called syscalls.c . the CubeMX generated project in my case at least, does not have it. Within is the extern definition for the _ _io_putchar() also the _write(0 and _read(). So I created a basic project for the same target then located this file and copy it into the project Src folder.

In main.c , #include “stdio.h” , also create the new implementation for this method ;

void __io_putchar(uint8_t ch) {
HAL_UART_Transmit(&huart3, &ch, 1, 1);
}


For me this sort of worked - it did spit out the printf output but only after 1024bytes had been buffered, then write() was called!
To resolve that “mode” we need the printf to flush immediately, can be done using the following call. I inserted

setbuf(stdout, NULL);

after the uart init is done but prior to using printf().
hope it helps....

I have syscall.c in my SW4STM32 project.
I do this below:
“In main.c , #include “stdio.h” , also create the new implementation for this method ;
void __io_putchar(uint8_t ch) {
HAL_UART_Transmit(&huart1, &ch, 1, 1);
}

However I get no printf output . Calling HAL_UART_Transmit(&huart3, &ch, 1, 1); directly works.

I get a warning message that probably explains the problem:
’__io_putchar’ defined but not used -Wunused-function main.c /myproject/Core/Src line 150 C/C++ Problem

Why is __io_putchar not being used ?


Thank you vikas, searched for hours. Your answer helped me get it working.

 

Newest Forum Posts

  1. Монтаж камина с грилем в Москве - установка и барбекю by KpddomErorb, 2025-05-10 18:28
  2. SPI on Nucleo_STMH533RE by royjamil, 2025-05-04 20:13
  3. SPI on Nucleo_STMH533RE by higginsa1, 2025-03-25 07:37
  4. SPI on Nucleo_STMH533RE by royjamil, 2025-03-23 11:31
  5. SPI on Nucleo_STMH533RE by higginsa1, 2025-03-23 09:33
  6. Configuring DMA for ADC in SW? by sam.hodgson, 2025-03-04 12:58
  7. Build a project in "release" mode by info@creosrl.it, 2025-02-20 18:12
  8. Build a project in "release" mode by info@creosrl.it, 2025-02-20 17:05
  9. Build a project in "release" mode by tang, 2025-02-20 10:36
  10. Build a project in "release" mode by info@creosrl.it, 2025-02-19 17:35

Last-Modified Blogs