cancel
Showing results for 
Search instead for 
Did you mean: 

stm32g474ceux - printf() works only once with USB -CDC_Transmit_FS() - Buffering issue

mdudhat
Associate II

Hi all.

I am using printf() in my USB-CDC VCP based stm32cubeide project, for printing the messages via USB to the serial terminal. But it prints only once in CDC_ReceiveCallBack () function and then i do not see other messages printed. In fact in while loop (in main function), i get all printf function results (all messages getting printed on serial terminal).

Moreover, I tried to use flush function - fflush(stdout)  after printf() function, but the result is the same..May be i did not use it properly.?

Can anyone please help me out for this. ?

Many thanks in advance,

10 REPLIES 10
AScha.3
Super User

What you send on printf() and how ?

try:

  CDC_Transmit_FS((uint8_t *)chrstring , sizeof(chrstring));

 

If you feel a post has answered your question, please click "Accept as Solution".
TDK
Super User

Get a CubeMX example up and running.

Consider adding relevant details to your post. Going off of "it doesn't work" doesn't lead to much useful feedback because we can't see what you're doing. You haven't even mentioned the chip you are using.

If you feel a post has answered your question, please click "Accept as Solution".

Hi,

my answer : one more thing, in fact  i am getting  all printf functions results  in while loop only, but in following case only once.!!!

1. syscalls.c :

 
__attribute__((weak)) int _write(int file, char *ptr, int len)
{
  .....
  {
    __io_putchar(*ptr++);
  }
  .....
}

 

2. main.c :

 
int _write(int file, char *ptr, int len) {
  CDC_Transmit_FS((uint8_t*) ptr, len);
  return len;
}

and then i am using printf() in

void CDC_ReceiveCallBack (uint8_t * buffer, uint32_t len){
  printf("helloo"); // this is just example - this gets printed
  printf("Hi");     // this is just example - this does NOT get printed
}

Edited to apply source code formatting - please see How to insert source code for future reference.

..and if you put "\n" at end of string to send -> "check ! \n"   (Its the standard way to get it printed out.)

And then ?

If you feel a post has answered your question, please click "Accept as Solution".

I expect you're going to need to add your own buffering, and sequencing so that you only have one operation occurring at a time.

Need to watch if the function blocks, returns errors, or is passed buffers which remain in-scope.

Don't call blocking functions in interrupt/callback context.

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

Hi, in fact in my message i had \r\n at the end but it did not work. and now i tried only with \n at the end. and it still did not work out too.

johncharles011
Associate

It seems like CDC_ReceiveCallback() is blocking or running in an interrupt context, which can prevent printf() from working as expected.

To fix it:

  1. Avoid blocking: Ensure USB communication isn’t blocking in CDC_ReceiveCallback().

  2. Flush properly: Make sure fflush(stdout) is used correctly after printf().

  3. Move printf() to the main loop: If in an interrupt context, use a flag to print in the main loop instead.

This should help get the messages printed consistently.

Hi @  johncharles011, 

thanks. In fact it also did not execute CDC_Transmit_FS()  written after printf() function. It only executes ONCE any function written first. For example: 

printf();  //executed, getting result on serial terminal.

printf();  //Not executed, Not getting result on terminal.

CDC_Transmit_FS() ; //Not executed, Not getting result on terminal.

 

So try just to send/print in main loop, not in callback :

  while (1)
  {
	  CDC_Transmit_FS(...);
	  HAL_Delay(500);
 
   /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }

 

If you feel a post has answered your question, please click "Accept as Solution".