2021-01-05 12:35 AM
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.
Solved! Go to Solution.
2021-01-05 01:56 AM
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
2021-01-05 01:56 AM
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
2021-01-05 02:01 AM
Keil doesn't use syscalls/write
2021-01-05 02:27 AM
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?