Skip to main content
Graduate
May 28, 2025
Question

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

  • May 28, 2025
  • 3 replies
  • 931 views

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,

    This topic has been closed for replies.

    3 replies

    Super User
    May 28, 2025

    What you send on printf() and how ?

    try:

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

     

    mdudhatAuthor
    Graduate
    May 28, 2025

    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.

    Super User
    May 28, 2025

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

    And then ?

    Super User
    May 28, 2025

    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.

    Graduate II
    May 28, 2025

    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.

    Super User
    May 28, 2025

    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 */
     }

     

    mdudhatAuthor
    Graduate
    May 30, 2025

    Hi @AScha.3, thanks.

    but i already said in one of my previous answer that i am getting all these printed messages from different printf() statements,  only in while loop. that i do not want to print continuously. so my attempt was to do it once during the callback function execution.

    Super User
    May 28, 2025

    >Hi @  johncharles011, 

    >thanks. In fact it also did not execute CDC_Transmit_FS()

    It is likely a chatgpt zombie. No need to thank AI agents, this only wastes energy. Especially when they failed to understand the question.