-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
printf not waiting before leaving function #227
Comments
If I put this in the for loop it is solved. It looks like a buffering issue. No idea how to increase the stdio buffer.
|
I think, this is caused because default console use BufferedSerial. Try to set the default console to UnbufferedSerial. |
I could also use something like this:
but if I move from this:
to this:
it is also solved. The idea was that if you use a very high serial speed the buffer is flushed faster if you use higher clockrates, but this is clearly not the case. It is just the opposite. I'm not so familiar with what is happening with the serial port behind the scene. I tested the speed limits and in both modes (buffered/non buffered) 67800 is a safe margin. Lower is also working and higher gives sometimes or always a risk of data loss. There is not much difference between buffered and non buffered. |
This looks weird because if you do bulk transfer you should be able to use higher speeds. Maybe there is something in the uart clock config? |
have you tried a fflush(stdio) after the print? |
I don't think in this case but I remember another case if you are printing and doing a sleep immediately after the printf it will only work if you do a flush first. It means that sleeping blocks the serial transfer while it's still in the buffer. Here there is no sleep, it's a timing/speed issue. I experienced that the faster you send data, the more chance you have that the serial communication is blocking at a certain point with bulk transfer. Maybe the buffer is filled in chunks and the CPU can not follow or something like this. I think the printf is also thread safe but with other interfaces (like the self made SPI) you didn't made thread safe you have to derefer all SPI transactions to the main thread with a queue injected from the main in the class, otherwise it will also be meshed up. |
@lefebvresam did you found the root cause? |
No, but I use the restrictions mentioned above and have no issues anymore, despite the fact that I could potentially use higher serial speeds. To investigate this further I should create a striped down test project for it and dive into the depth of the mbed serial stuff. I'm currently limited in time to do this. |
I made new issue for better tracking. Closed |
I have a function that is called in a loop:
I tried several posibilities but the last lines at the end of the loop are not printed.
It looks like the memory reservation of the string is away when the loop terminates and the printf is still sending data. When I put a delay function in the loop it is solved but this is not the best way to solve this I think. If I printed twice in the loop the issue stays at the end. So it's not a buffering issue. If I replace the data with fixed value the issue stays.
The text was updated successfully, but these errors were encountered: