Tag Archives: HAL Library Printf

How to Use Printf in HAL Library

1. Include header files

#include "stdio.h"

2. Redefine the serial port

int fputc( int ch, FILE * f) 
{ 
 while ((USART1->SR & 0X40 ) == 0 ); // Send in a loop until the sending is complete 
 USART1->DR = (uint8_t) ch;
  return ch; 
}

3. If only the first two steps are completed, the emulator can be used to run, but the program cannot run by itself. Also add the following code

#pragma import(__use_no_semihosting)
void _sys_exit(int x)
{
    x = x;
}
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;    /* FILE is typedef’ d in stdio.h. */

This setting can avoid using the LIB library