Hey everyone. I need a bit help with understanding audio callback and main loop.
I have couple of questions:
Can there be a data race between audio callback and main loop? I have a big audio buffer which I’m clearing by running a for loop that sets all the values to 0. Can it be that while this happens the audio callback writes something to this buffer causing troubles?
I have a flag that is set to true in the audio callback but this flag is held true for a duration of one sample. Can I expect that my main loop would reliably always read it? Because in my case it’s kinda working but at some point stops.
So in general I’m interested in all the info there is about audio callback vs main loop, all gotchas, how-to’s etc. i remember there used to be some kind of wiki with intro topics like that but I literally spent 30 minutes yesterday trying to find it, but every link kept landing me on the same page with how to install tollchain, how to flash, etc.
main loop is running normally.
It is at arbitrary points interrupted every millisecond (after 48 samples) by a hardware interrupt which starts Callback. It is absolutely necessary, that the callback needs less than 1 millisecond. After it is done one time, the interrupted main loop continues.
So a main loop might be not completely finished between 2 Callbacks or also several main loops might be done. This depends on what is to be done in one main loop.
You can gain some information with the cpu load meter.
Yeah, I plan to try it tonight. I hooked it up to the led brightness last night to kinda test it without overhead of serial printing but it stayed black So either my code is blazingly fast (less likely) or I messed up somewhere along the way (most likely)
Thanks for the info!
So I tried to serial print the cpu utilization last night and it was floating at about 0.01 - 0.03 which is very good.
The problem that I had with the flag was solved by resetting it within the main loop to make sure it was getting read in my main function. Because before when it was managed within the audio callback it was happening too fast for the main loop to pick up.
Thanks for your help!
If the flag was being set and cleared in each run of the callback, the loop in main() would NEVER see it. It’s not a matter of utilization.
The audio callback always runs to completion before the main loop is allowed to run.