VCP function like a printf

PHOTO EMBED

Wed Jun 09 2021 11:10:50 GMT+0000 (UTC)

Saved by @dimitri9494 #stm32 #vcp

/*
1 - Add on MAKEFILE the "-u _printf_float", example

#######################################
# CFLAGS
#######################################
# cpu
CPU = -mcpu=cortex-m4

# fpu
FPU = -mfpu=fpv4-sp-d16

# float-abi
FLOAT-ABI = -mfloat-abi=hard -u _printf_float

2 - Make our own printf function, reidrecting it to VCP or UART:
#include<stdarg.h>

*/

void vcpPrintf( const char * format, ... )
{
    char buffer[256];
    uint8_t nr_attempts = 0;

    va_list args;
    va_start (args, format);
    vsprintf (buffer,format, args);

    //avoid USB busy and add a "timout" to avoid infinite loop
    while (CDC_Transmit_FS((uint8_t *)buffer, strlen(buffer)) == USBD_BUSY && nr_attempts <= 100)
    {nr_attempts++;}
    va_end (args);
}
content_copyCOPY