Home > other >  Stm32 formatting output printf () function in the keil and redefine the output on the eclipse platfo
Stm32 formatting output printf () function in the keil and redefine the output on the eclipse platfo

Time:09-26

1. In the keil:
In the following function is accurate atomic usart.

//add the following code, support the printf function, without the need to select the use MicroLIB
If # 1
# pragma import (__use_no_semihosting)
//the standard library needs the support of function
Struct __FILE
{
Int handle;
};

The FILE __stdout;
//define _sys_exit () to avoid using half a host mode
_sys_exit (int x)
{
X=x;
}
//redefine fputc function
Int fputc (int ch, FILE * f)
{
While ((USART1 - & gt; SR& 0 x40)==0);//cycle to send, until completion of the send
USART1 - & gt; DR=(u8) ch;
return ch;
}
# endif

2. In eclipse:
But I have found in eclipse using the above method is not print, the last use of the solution is the write printf function, the code is as follows:
# include & lt; Stdio. H>
# include & lt; String. H>
# include & lt; Stdarg. H>
Char dat_buf [128].//custom output size
Int printFormat (const char * FMT,... )
{
Int len.
Va_list args.//define the va_list type variable arg
Va_start (args, FMT);//arg initialization, arg is pointing to the first optional parameter
Len=vsprintf (dat_buf, FMT, args);
Va_end (args);
HAL_UART_Transmit (& amp; Huart1 (uint8_t *) dat_buf, len, 0 XFFFF);
Return len.
}
Using direct call the function, use the format consistent with printf,
  • Related