Skip to content

I2sOut on UM BLING causes hard fault (8.2.9) #8865

@scogswell

Description

@scogswell

CircuitPython version

Adafruit CircuitPython 8.2.9 on 2023-12-06; BLING! with ESP32S3

Code/REPL

# SPDX-FileCopyrightText: 2023 Kattni Rembor for Adafruit Industries
# SPDX-License-Identifier: MIT
"""
CircuitPython I2S Tone playback example.
"""
import time
import array
import math
import audiocore
import board
import audiobusio
import neopixel
import microcontroller


BLING = neopixel.NeoPixel(board.MATRIX_DATA,320,brightness=0.05,auto_write=False)

TONE_VOLUME = 0.1  # Increase this to increase the volume of the tone.
FREQUENCY = 600  # Set this to the Hz of the tone you want to generate. 

# Comment out this line and the hard fault goes away 
audio = audiobusio.I2SOut(microcontroller.pin.GPIO2, microcontroller.pin.GPIO1, microcontroller.pin.GPIO3) 

length = 8000 // FREQUENCY
sine_wave = array.array("h", [0] * length)
for i in range(length):
    sine_wave[i] = int((math.sin(math.pi * 2 * i / length)) * TONE_VOLUME * (2 ** 15 - 1))
sine_wave_sample = audiocore.RawSample(sine_wave)

while True:
    # audio.play(sine_wave_sample, loop=True)  # leave commented out
    BLING.fill(0x00FF00)
    BLING.show()
    time.sleep(0.1)
    # audio.stop()        # leave commented out 
    BLING.fill(0x0000FF)
    BLING.show()
    time.sleep(0.1)

Behavior

Running the above on BLING, which has a built-in i2s amp, will eventually hard fault circuitpython and drop into safe mode.
Hard fault: memory access or instruction error, sometimes Internal watchdog timer expired. Sometimes the CIRCUITPY drive becomes inaccessible after the reset OSError: [Errno 5] Input/Output error, sometimes everything disappears including the CIRCUITPY drive label:

$ ls -l /media/cogswell/35D5-F6A3/
total 1
-rw-r--r-- 1 cogswell cogswell 115 Dec 31  1999 boot_out.txt

After that I do a REPL

import storage
storage.erase_filesystem()

to reset things.

I have boot.py set to not autoreload:

import supervisor
supervisor.runtime.autoreload=False

No libraries present in /lib (all the required ones are built-ins with bling).

The process of having the hard fault happen can be exacerbated by doing filesystem access on CIRCUITPY while the program is running. I used this little python program on linux to automate it, just touches a file until it crashes, usually within 10-20 iterations:

import subprocess
import time

c=0
r=0
while(r == 0 ):
    result = subprocess.run(["touch","/media/cogswell/CIRCUITPY/foo.txt"]) 
    r=result.returncode
    print(c)
    c += 1 
    time.sleep(1)

Comment out the line audio = audiobusio.I2SOut(microcontroller.pin.GPIO2, microcontroller.pin.GPIO1, microcontroller.pin.GPIO3) and the touch program runs a long time without crashes (at least ten minutes for the test I did, 700+ iterations).

Note you don't have to be actually playing the audio (audio.play() are commented out), just creating the i2sOut seems to be enough. It takes longer but it will also eventually crash if I just have the audio calls active and comment out all the bling neopixel stuff but I perceive - right or wrong - that "more" i/o activity makes it more likely to hard-fault.

I can cut down the number of leds addressed in the neopixel call from 320 to 1 and it will still hard-fault.

Audio does play fine if the audio.play() calls are uncommented, but aren't seemingly required to make the fault.

Description

No response

Additional information

This is a boiled-down example to show this happens. I was running into this coding a longer program for bling which played audio and would hard-fault crash every now when when I saved (ctrl+s) code.py using vscode on linux (Ubuntu 22.04.3 LTS). In that program if I removed the audio calls it would appear run fine without hard-faulting.

I briefly tried using the Circuitpython 9.0.0-beta0 (from circuitpython.org) but that will throw an IDFerror on audio.play() so I didn't keep at that.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions