Problem with I2S irq on second thread - RP2040
Posted: Tue Jul 05, 2022 12:34 pm
I will try to keep it simple and explain the problem without going into too much detail.
I have a WavePlayer class that plays wave files using I2S (the builtin I2S object). The WavePlayer can play on the main thread or create a second thread and play on it (utilizing the second core). The main way I want to use the WavePlayer is on a second thread.
In the first implementation, I used a loop that reads a chunk from the wave file and sends it to the I2S in blocking mode. It was not fast enough, wave files with higher frame rates could not be played properly.
To speed up the playing loop, I used the I2S irq function. The irq raised a flag, indicating that the I2S object is ready for more data. This way, in the playing loop I could start reading a new chunk from the file before the I2S is ready, thus increasing the WavePlayer's max playing speed. I used a thread lock for the flag because the irq seems to run randomly on core0 or on core1 (regardless of where I set the irq).
The irq version worked great on the main thread, but on a second thread it would get stuck after some time, usually a few seconds. After some debugging, it seemed that the problem was that the irq stopped executing.
Note: I am using MicroPython v1.19.1 where rp2 memory corruption bug in thread was fixed.
This problem may be related to another problem I posted:
Timer gets stuck when thread is running - RP2040
I have a WavePlayer class that plays wave files using I2S (the builtin I2S object). The WavePlayer can play on the main thread or create a second thread and play on it (utilizing the second core). The main way I want to use the WavePlayer is on a second thread.
In the first implementation, I used a loop that reads a chunk from the wave file and sends it to the I2S in blocking mode. It was not fast enough, wave files with higher frame rates could not be played properly.
To speed up the playing loop, I used the I2S irq function. The irq raised a flag, indicating that the I2S object is ready for more data. This way, in the playing loop I could start reading a new chunk from the file before the I2S is ready, thus increasing the WavePlayer's max playing speed. I used a thread lock for the flag because the irq seems to run randomly on core0 or on core1 (regardless of where I set the irq).
The irq version worked great on the main thread, but on a second thread it would get stuck after some time, usually a few seconds. After some debugging, it seemed that the problem was that the irq stopped executing.
Note: I am using MicroPython v1.19.1 where rp2 memory corruption bug in thread was fixed.
This problem may be related to another problem I posted:
Timer gets stuck when thread is running - RP2040