I use this code for retarget printf()
, but it does not work
#ifdef __GNUC__
/* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->
As an alternative, you could write your own printf()
function using, Variable Argument Functions (va_list).
With va_list
a custom print function looks like the following:
#include
#include
#include
void vprint(const char *fmt, va_list argp)
{
char string[200];
if(0 < vsprintf(string,fmt,argp)) // build string
{
HAL_UART_Transmit(&huart1, (uint8_t*)string, strlen(string), 0xffffff); // send message via UART
}
}
void my_printf(const char *fmt, ...) // custom printf() function
{
va_list argp;
va_start(argp, fmt);
vprint(target, fmt, argp);
va_end(argp);
}
Usage example:
uint16_t year = 2016;
uint8_t month = 10;
uint8_t day = 02;
char* date = "date";
// "Today's date: 2015-12-18"
my_printf("Today's %s: %d-%d-%d\r\n", date, year, month, day);
Note that while this solution gives you convenient function to use, it is slower than sending raw data or using even sprintf()
. I have used this solution both on AVR and on STM32 microcontrollers.
You could further modify the vprint
like this, where periphery_t
is a simple enum
type:
void vprint(periphery_t target, const char *fmt, va_list argp)
{
char string[200];
if(0 < vsprintf(string,fmt,argp))
{
switch(target)
{
case PC: PC_send_str(string);
break;
case GSM: GSM_send_str(string);
break;
case LCD: LCD_print_str(string);
break;
default: LCD_print_str(string);
break;
}
}
}