cancel
Showing results for 
Search instead for 
Did you mean: 

How to get printf example working in another project

Drake Miller
Associate II

Hi, 

I am working with the Nucleo-F401RE board and I am trying to implement the printf function to work with UART2. I can load and run the example project, but implementing the same code does not work in a separate project.  

I have had zero luck getting this work after having read and implementing everything that I can find. 

I have the following code in the main function taken from the example. 

I am missing something crucial here and could use some insight. 

Thanks,

/* USER CODE END PFP */

#ifdef __GNUC__

 /* With GCC, small printf (option LD Linker->Libraries->Small printf

   set to 'Yes') calls __io_putchar() */

 #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)

#else

 #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)

#endif /* __GNUC__ */

/* USER CODE BEGIN 0 */

PUTCHAR_PROTOTYPE

{

 /* Place your implementation of fputc here */

 /* e.g. write a character to the EVAL_COM1 and Loop until the end of transmission */

 HAL_UART_Transmit(&huart2, (uint8_t *)&ch, 1, 0xFFFF);

 return ch;

}

10 REPLIES 10
HSh..2
Associate II

Hi every body.

I have a crazy problem and I`m stuck in a nerve-racking simple work.

working with STM32CubeIDE 1.5.1, code created from stm32cubemx 6.5.0

project converted to c++

wanna retarget printf to uart

in a .h file have this:

#ifdef __GNUC__

 #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)

#else

 #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)

#endif

and in the .cpp file with the same name, have this:

PUTCHAR_PROTOTYPE

{

while( !(USART1->ISR & 0x80) ); //TXE = 0: data is not transferred to the shift register

USART1->TDR = ch;

 return ch;

}

apart from the odd problem of jumping into HardFault_Handler() when using HAL_UART_Transmit()

I can never build the project successfully, I do have lots of different errors when changing all files to .cpp. the only file I dont change to .cpp is syscall.c, with that I get the first_error.

0693W00000SvlBWQAZ.pngand when changing to syscalls.cpp, I get the second_error.

0693W00000SvlBRQAZ.png