Showing results for 
Search instead for 
Did you mean: 

printf over USB VCP

Associate II
Posted on June 19, 2014 at 13:37

Hi all,

I am trying to retarget printf over USB-VCP and I am stcuk !

I implemented the putc function in the following way:


int fputc(int ch, FILE *f)


  USBD_CDC_SetTxBuffer(&hUSBDDevice, (uint8_t *)&ch, 1);

  if(USBD_CDC_TransmitPacket(&hUSBDDevice) != USBD_OK)


    /* Device is busy */

    /* what should I return ? */


  return ch;



As you know fputc is called by printf for printing a single character and it is called in a loop until all characters are printed.

What I am facing is that it may happen the USB is busy while sending the previous character. What do I have to do at this condition? fputc return -1 ?

PS: I am STM32 Cube V2.0.0 ''18-February-2014''


Posted on June 19, 2014 at 16:37

Consider whether you can buffer outgoing characters, send multiple at a time, or wait until a later time if the USB is currently busy.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..