i2s: Sleep when TX FIFO fills - #52
Conversation
soypat
left a comment
There was a problem hiding this comment.
Yes! Very nice. I've been thinking about how to add precalculated sleep to the rest of these drivers, especially when doing DMA. Might be worth doing something with the dma and deadline abstractions in the future. For now this is an excellent addition. Thank you!
|
Actually this is causing me problems now :( If two other things are happening when the sleep finishes it isn't coming back to the sleeping routine fast enough to avoid the audio dropping out. Or, at least I think that's what's happening. I have three goroutines: one filling audio buffers, one writing them, and one updating a little I2C screen, and I'm now getting clicks whenever the screen updates. Please could you revert it while I figure things out? Sorry! |
|
(I added |
|
OK, changes reverted- If you want strict synchronization you should follow an event-loop architecture or develop a priority thread abstraction over the goroutines. I guess the change maybe does not belong in this repo since audio requires strict timing and the best way to meet strict schedules is to just thrash runtime.Gosched. This topic is worthy of a new discussion on tinygo-org/tinygo |
i2sWritespins callingruntime.Goschedin a tight loop if the TX FIFO fills. This pull request makes it precalculate how long the FIFO takes to become writable andtime.Sleepfor slightly longer than that time instead. It also joins the FIFOs to bump the TX FIFO from 4 to 8 words.On TinyGo at least, I'm 99% sure that
time.Sleepyields the CPU In the same way asruntime.Gosched(i.e. it won't deadlock when running on a single core) and, one core or two, with 44.1kHz audio the sleep time is 3,109 cycles at 133MHz (3,507 at 150MHz) so you get a lot of CPU back (or burn less power, if you just wait it out).