@AdamHaun's answer is all you need, with sprintf()
it is easy to create a string and then send it. But if you really want a printf()
function of your own, then Variable Argument Functions (va_list) is the way.
With va_list
a custom print function looks like the following:
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
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(fmt, argp);
va_end(argp);
}
Usage example:
uint16_t year = 2015;
uint8_t month = 12;
uint8_t day = 18;
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, but it is slower than sending raw data or using even sprintf()
. With high datarates I think it won't be sufficient.
Another option, and probably better option is to use ST-Link, SWD debugger along with ST-Link Utility. And use Printf via SWO viewer, here is the manual of ST-Link Utility, relevant part starts on page 31.
The Printf via SWO Viewer displays the printf data sent from the target through SWO. It allows to display some useful information on the running firmware.
_write()
function is what I did. Details in my answer below. \$\endgroup\$