adafruit / circuitpython

CircuitPython - a Python implementation for teaching coding with microcontrollers
https://circuitpython.org
Other
4.11k stars 1.22k forks source link

audiobusio.I2SOut has invalid pins on Teensy 4.0 with audio adapter board #9439

Open perspector opened 3 months ago

perspector commented 3 months ago

CircuitPython version

Adafruit CircuitPython 9.1.0 on 2024-07-10; Teensy 4.0 with IMXRT1062DVJ6A

Code/REPL

import audiobusio
import audiocore
import board
import array
import time
import math

# Generate one period of sine wave.
length = 8000 // 440
sine_wave = array.array("H", [0] * length)
for i in range(length):
    sine_wave[i] = int(math.sin(math.pi * 2 * i / length) * (2 ** 15) + 2 ** 15)

sine_wave = audiocore.RawSample(sine_wave, sample_rate=8000)

i2s = audiobusio.I2SOut(bit_clock=board.D21, word_select=board.D20, data=board.D7, main_clock=board.D23)

i2s.play(sine_wave, loop=True)
time.sleep(1)
i2s.stop()

Behavior

Traceback (most recent call last): File "main.py", line 17, in ValueError: Invalid bit_clock pin

Description

First of all, CircuitPython is amazing! I really appreciate all of the hard work put into it!

There is a slight issue with the Teensy Audio Adapter board though. The code above uses the default pins for the teensy audio adapter board. Teensy 4.x audio adapter board Rev. D2: https://www.pjrc.com/store/teensy3_audio.html

I have also tried using different pins and assigning them manually, but still receive ValueError: Invalid bit_clock pin

The code is nearly identical to the example given in the CircuitPython docs. https://docs.circuitpython.org/en/latest/shared-bindings/audiobusio/index.html#audiobusio.I2SOut

Any help would be greatly appreciated!

Additional information

I have tried defining different pins for bit_clock, word_select, data, and main_clock. Pins that were not used for audio by the audio adapter board by default on the Teensy pinout did not raise an error. Default pins for the audio adapter board raised a ValueError

tannewt commented 3 months ago

D21 is GPIO_AD_B1_11 and is muxed to RX_BCLK. Our current I2SOut assume it connects to TX_BCLK. I'm not sure how to use RX_BCLK when transmitting. GPIO_AD_B1_14 is TX_BCLK and D26 on the teensy4.0.

You can cross reference the pin mux info with the board pin mapping: https://github.com/adafruit/circuitpython/blob/2f626121867efa429b7484fa3ca5a315021cc506/ports/mimxrt10xx/peripherals/mimxrt10xx/MIMXRT1062/periph.c#L219-L265 https://github.com/adafruit/circuitpython/blob/2f626121867efa429b7484fa3ca5a315021cc506/ports/mimxrt10xx/boards/teensy40/pins.c#L12-L144