circuitpython: audiopwmio freezes/crashes CircuitPython sometimes with MSD access
CircuitPython version
Adafruit CircuitPython 7.1.1 on 2022-01-14; Adafruit ItsyBitsy RP2040 with rp2040
Code/REPL
import time
import board
import audiocore
import audiomixer
from audiopwmio import PWMAudioOut as AudioOut # for RP2040 etc
#from audioio import AudioOut as AudioOut # for SAMD51 etc
# audio pin is almost any pin on RP2040, let's do RX (pin 1) (RP2040 GPIO1) or A0 (RP2040 GPIO26)
# audio pin is A0 on SAMD51 (Trelllis M4, Itsy M4, etc)
audio = AudioOut(board.A0)
mixer = audiomixer.Mixer(voice_count=1, sample_rate=22050, channel_count=1,
bits_per_sample=16, samples_signed=True)
audio.play(mixer) # attach mixer to audio playback
print("audio is now playing")
# start drum loop playing
wave0 = audiocore.WaveFile(open("wav/drumsacuff_22k_s16.wav","rb"))
mixer.voice[0].play( wave0, loop=True )
while True:
time.sleep(0.1)
Behavior
When an RP2040-based device running the above audiopwmio
code is plugged into a USB host or USB MSD activity (saving code.py
), extremely loud audio glitches result as the host does various USB activity (presumably mostly MSD). Sometimes CircuitPython will freeze and crash entirely with the REPL message:
Code stopped by auto-reload.
Description
Sometimes, the audio glitching causes all of CircuitPython to freeze: REPL dead, cannot access CIRCUITPY drive, etc. In those cases, the host will eventually register a disconnect from the device.
The attached video shows what I’m seeing: the glitches can be heard, CircuitPython locks up, and glitches are heard during code.py
saving.
Warning: loud sounds
https://user-images.githubusercontent.com/274093/153290112-28180f6a-af25-471b-8fb8-aba473b3e6e7.mp4
Additional information
This has been experienced on ItsyBitsy RP2040, KB2040, and Raspberry Pi Pico.
On audioio
boards like SAMD51 ItsyBitsy M4 with DAC, instead of audiopwmio
, there is minimal to no glitching.
Suggestion: maybe MSD access can pause audiopwmio
(if active) for 500 milliseconds? This would reduce most glitches and maybe “get rid” of source of crash by eliminating DMA/interrupt contention?
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 16 (5 by maintainers)
I tried to reproduce this in
Adafruit CircuitPython 7.3.3 on 2022-08-29; Adafruit QT Py RP2040 with rp2040
using @todbot’s original reproducer script but failed.To simulate interaction with the CIRCUITPY filesystem I ran a script which writes 16kB of random data and then waits 4 seconds before doing it again. I let it run for dozens of iterations.
When I flashed back to 7.1.0, the reproducer DID work, corrupting CIRCUTIPY.
I suspect that the re-worked auto-reload in 7.2.x probably played a part in fixing this.
I didn’t actually listen to the audio, since this issue is about crashing. Audio playback of wav files during flash writing will not work properly, because the wave data can’t be read during a flash erase or flash write operation.
Closing as I verified the reproducer no longer causes CIRCUITPY to become corrupted.