stm32g474ceux - printf() works only once with USB -CDC_Transmit_FS() - Buffering issue
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-05-28 3:18 AM - edited ‎2025-05-28 7:17 AM
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,
- Labels:
-
STM32CubeIDE
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-05-28 6:06 AM
What you send on printf() and how ?
try:
CDC_Transmit_FS((uint8_t *)chrstring , sizeof(chrstring));
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-05-28 6:07 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-05-28 7:05 AM - last edited on ‎2025-05-28 7:23 AM by Andrew Neil
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-05-28 7:14 AM
..and if you put "\n" at end of string to send -> "check ! \n" (Its the standard way to get it printed out.)
And then ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-05-28 7:36 AM
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.
Up vote any posts that you find helpful, it shows what's working..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-05-28 7:38 AM - edited ‎2025-05-28 7:44 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-05-28 8:17 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-05-28 8:44 AM
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 */
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2025-05-28 1:32 PM
>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.
