Skip to main content
LKris.1
Associate II
January 5, 2021
Solved

NUCLEO-H743ZI USB printf works in STM32CubeIDE but not in Keil

  • January 5, 2021
  • 3 replies
  • 1656 views

I have a problem with using the printf function in Keil. I used the STM32CubeMX to setup a project and generated the code for Keil.

All settings in CubeMX is standard, only thing changed is the USB_DEVICE set to Communication Device Class (Virtual Port Com).

If this is setup and genrate for STM32CubeIDE and then in main.c I add #include "stdio.h" and within User Code 4 I insert the following, then printf is working on the USB connection.

/* USER CODE BEGIN 4 */

int __io_putchar(int ch) {

uint8_t c[1];

c[0] = ch & 0x00FF;

HAL_UART_Transmit(&huart3, &*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;

}

/* USER CODE END 4 */

If i then do the same thing for keil, its not working, anyone who can guide me how to solve this.

This topic has been closed for replies.
Best answer by Peter BENSCH

Compilers use different implementations for printf, GNU GCC calls e.g. __io_putchar, others call fputc

You could try:

#ifdef __GNUC__
 #define PUTCHAR_PROTOTYPE int __io_putchar(uint8_t ch)
#else
 #define PUTCHAR_PROTOTYPE int fputc(uint8_t ch, FILE *f)
#endif // __GNUC__
// * @brief Retargets the C library printf function to the USART.
// * @param None
// * @retval None
PUTCHAR_PROTOTYPE
{
// Place your implementation of __io_putchar/fputc here, e.g.
 HAL_UART_Transmit(&huart2, &ch, 1, 3);
 return ch;
}

Good luck!

When your question is answered, please close this topic by choosing Select as Best. This will help other users find that answer faster.

/Peter

3 replies

Peter BENSCH
Peter BENSCHBest answer
ST Technical Moderator
January 5, 2021

Compilers use different implementations for printf, GNU GCC calls e.g. __io_putchar, others call fputc

You could try:

#ifdef __GNUC__
 #define PUTCHAR_PROTOTYPE int __io_putchar(uint8_t ch)
#else
 #define PUTCHAR_PROTOTYPE int fputc(uint8_t ch, FILE *f)
#endif // __GNUC__
// * @brief Retargets the C library printf function to the USART.
// * @param None
// * @retval None
PUTCHAR_PROTOTYPE
{
// Place your implementation of __io_putchar/fputc here, e.g.
 HAL_UART_Transmit(&huart2, &ch, 1, 3);
 return ch;
}

Good luck!

When your question is answered, please close this topic by choosing Select as Best. This will help other users find that answer faster.

/Peter

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
LKris.1
LKris.1Author
Associate II
January 5, 2021

Hi Peter

I just tried this, actually i also tried this earliere, but its still not printing anything on my serial monitor.

Do you have an idea why its not working with my NUCLEO-H743ZI and Keil?

Tesla DeLorean
Guru
January 5, 2021

Keil doesn't use syscalls/write​

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..