Skip to content

Instantly share code, notes, and snippets.

@mnemocron
Created May 17, 2018 13:48
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mnemocron/46e0466637a6610026edb4e915c53e2e to your computer and use it in GitHub Desktop.
Save mnemocron/46e0466637a6610026edb4e915c53e2e to your computer and use it in GitHub Desktop.
Redirect printf() to UART on STM32 microcontroller
/* USER CODE BEGIN Includes */
#include <stdio.h>
/* USER CODE BEGIN 0 */
/* PRINTF REDIRECT to UART BEGIN */
// @see http://www.keil.com/forum/60531/
// @see https://stackoverflow.com/questions/45535126/stm32-printf-redirect
struct __FILE{
int handle;
/* Whatever you require here. If the only file you are using is */
/* standard output using printf() for debugging, no file handling */
/* is required. */
};
FILE __stdout;
int fputc(int ch, FILE *f){
HAL_UART_Transmit(&huart3, (uint8_t *)&ch, 1, 0xFFFF);
return ch;
}
int ferror(FILE *f){
/* Your implementation of ferror(). */
return 0;
}
/* PRINTF REDIRECT to UART END */
/* USER CODE END 0 */
@SoftHard-dev
Copy link

How it is works?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment