Save current changes before reorganizing drivers and cleaning workspace
This commit is contained in:
+41
-34
@@ -79,24 +79,30 @@ def record_audio(duration_seconds=5, filename='recording.pcm'):
|
||||
duration_seconds (int): How long to record in seconds.
|
||||
filename (str): Name of output raw PCM file on the device.
|
||||
"""
|
||||
# 1. Start I2C Control Bus (SDA=13, SCL=14)
|
||||
i2c = I2C(0, sda=Pin(13), scl=Pin(14))
|
||||
# 1. Start I2C Control Bus (SDA=16, SCL=15)
|
||||
i2c = I2C(0, sda=Pin(16), scl=Pin(15))
|
||||
|
||||
# 1a. Setup Master Clock (MCLK) on GPIO 4 using PWM (needed for ES8311 ADC)
|
||||
mclk_pin = Pin(4, Pin.OUT)
|
||||
mclk_pwm = machine.PWM(mclk_pin)
|
||||
mclk_pwm.freq(6144000)
|
||||
mclk_pwm.duty_u16(32768)
|
||||
|
||||
# 2. Configure I2S Receiver
|
||||
# Pins: sck=BCLK (GPIO 9), ws=WS/LRCK (GPIO 45), sd=DIN (GPIO 10)
|
||||
# Pins: sck=BCLK (GPIO 5), ws=WS/LRCK (GPIO 7), sd=DIN (GPIO 6)
|
||||
i2s = I2S(1,
|
||||
sck=Pin(9),
|
||||
ws=Pin(45),
|
||||
sd=Pin(10),
|
||||
sck=Pin(5),
|
||||
ws=Pin(7),
|
||||
sd=Pin(6),
|
||||
mode=I2S.RX,
|
||||
ibuf=16000,
|
||||
rate=16000,
|
||||
bits=16,
|
||||
format=I2S.STEREO)
|
||||
|
||||
# 3. Wake up and configure the ES7210 microphone chip
|
||||
mic_adc = ES7210(i2c)
|
||||
if not mic_adc.init(sample_rate=16000, bit_width=16):
|
||||
# 3. Wake up and configure the ES8311 codec chip
|
||||
mic_adc = ES8311(i2c)
|
||||
if not mic_adc.init(sample_rate=16000):
|
||||
i2s.deinit()
|
||||
return False
|
||||
|
||||
@@ -125,7 +131,8 @@ def record_audio(duration_seconds=5, filename='recording.pcm'):
|
||||
finally:
|
||||
# Always release the I2S peripheral resources
|
||||
i2s.deinit()
|
||||
print("I2S receiver deinitialized.")
|
||||
mclk_pwm.deinit()
|
||||
print("I2S receiver and MCLK deinitialized.")
|
||||
|
||||
|
||||
class ES8311:
|
||||
@@ -155,11 +162,11 @@ class ES8311:
|
||||
self._write(0x00, 0x00)
|
||||
time.sleep_ms(10)
|
||||
|
||||
# Clock Configuration (16kHz sample rate, MCLK=12.288MHz)
|
||||
# Clock Configuration (16kHz sample rate, MCLK=6.144MHz)
|
||||
self._write(0x01, 0x3F) # Enable all clocks, use MCLK pin
|
||||
self._write(0x02, 0x48) # pre_div=3, pre_mult=1
|
||||
self._write(0x03, 0x10) # fs_mode=0, adc_osr=16
|
||||
self._write(0x04, 0x20) # dac_osr=32
|
||||
self._write(0x04, 0x10) # dac_osr=16
|
||||
self._write(0x05, 0x00) # adc_div=1, dac_div=1
|
||||
self._write(0x06, 0x03) # bclk_div=4 (4-1=3)
|
||||
self._write(0x07, 0x00) # lrck_h=0
|
||||
@@ -219,14 +226,14 @@ def play_tone(frequency=440, duration_ms=1000, volume=50):
|
||||
|
||||
print(f"Playing tone: {frequency}Hz for {duration_ms}ms (vol={volume})...")
|
||||
|
||||
# 1. Setup Master Clock (MCLK) on GPIO 16 using PWM
|
||||
mclk_pin = Pin(16, Pin.OUT)
|
||||
# 1. Setup Master Clock (MCLK) on GPIO 4 using PWM
|
||||
mclk_pin = Pin(4, Pin.OUT)
|
||||
mclk_pwm = machine.PWM(mclk_pin)
|
||||
mclk_pwm.freq(12288000)
|
||||
mclk_pwm.freq(6144000)
|
||||
mclk_pwm.duty_u16(32768)
|
||||
|
||||
# 2. Start I2C Control Bus (SDA=13, SCL=14)
|
||||
i2c = I2C(0, sda=Pin(13), scl=Pin(14))
|
||||
# 2. Start I2C Control Bus (SDA=16, SCL=15)
|
||||
i2c = I2C(0, sda=Pin(16), scl=Pin(15))
|
||||
|
||||
# 3. Initialize the ES8311 DAC
|
||||
dac = ES8311(i2c)
|
||||
@@ -237,10 +244,10 @@ def play_tone(frequency=440, duration_ms=1000, volume=50):
|
||||
dac.set_volume(volume)
|
||||
|
||||
# 4. Configure I2S TX
|
||||
# Pins: sck=BCLK (GPIO 9), ws=WS/LRCK (GPIO 45), sd=DOUT (GPIO 8)
|
||||
# Pins: sck=BCLK (GPIO 5), ws=WS/LRCK (GPIO 7), sd=DOUT (GPIO 8)
|
||||
i2s = I2S(1,
|
||||
sck=Pin(9),
|
||||
ws=Pin(45),
|
||||
sck=Pin(5),
|
||||
ws=Pin(7),
|
||||
sd=Pin(8),
|
||||
mode=I2S.TX,
|
||||
ibuf=8000,
|
||||
@@ -248,8 +255,8 @@ def play_tone(frequency=440, duration_ms=1000, volume=50):
|
||||
bits=16,
|
||||
format=I2S.STEREO)
|
||||
|
||||
# 5. Enable Speaker Amplifier (GPIO 46)
|
||||
amp_pin = Pin(46, Pin.OUT, value=1)
|
||||
# 5. Enable Speaker Amplifier (GPIO 1, Active Low)
|
||||
amp_pin = Pin(1, Pin.OUT, value=0)
|
||||
|
||||
# 6. Generate sine wave cycle
|
||||
# Approximate frequency to make integer number of samples per cycle
|
||||
@@ -277,7 +284,7 @@ def play_tone(frequency=440, duration_ms=1000, volume=50):
|
||||
|
||||
# 7. Clean up
|
||||
time.sleep_ms(100) # Let the remaining buffer play out
|
||||
amp_pin.value(0)
|
||||
amp_pin.value(1) # Disable amp
|
||||
i2s.deinit()
|
||||
mclk_pwm.deinit()
|
||||
print("Tone playback complete.")
|
||||
@@ -337,14 +344,14 @@ def play_wav(filename, volume=50):
|
||||
|
||||
print(f"WAV Info: {sample_rate}Hz, {bits} bits, {'Mono' if channels == 1 else 'Stereo'}")
|
||||
|
||||
# 2. Setup Master Clock (MCLK) on GPIO 16 using PWM
|
||||
mclk_pin = Pin(16, Pin.OUT)
|
||||
# 2. Setup Master Clock (MCLK) on GPIO 4 using PWM
|
||||
mclk_pin = Pin(4, Pin.OUT)
|
||||
mclk_pwm = machine.PWM(mclk_pin)
|
||||
mclk_pwm.freq(12288000)
|
||||
mclk_pwm.freq(6144000)
|
||||
mclk_pwm.duty_u16(32768)
|
||||
|
||||
# 3. Start I2C Control Bus (SDA=13, SCL=14)
|
||||
i2c = I2C(0, sda=Pin(13), scl=Pin(14))
|
||||
# 3. Start I2C Control Bus (SDA=16, SCL=15)
|
||||
i2c = I2C(0, sda=Pin(16), scl=Pin(15))
|
||||
|
||||
# 4. Initialize the ES8311 DAC
|
||||
dac = ES8311(i2c)
|
||||
@@ -356,11 +363,11 @@ def play_wav(filename, volume=50):
|
||||
dac.set_volume(volume)
|
||||
|
||||
# 5. Configure I2S TX
|
||||
# Pins: sck=BCLK (GPIO 9), ws=WS/LRCK (GPIO 45), sd=DOUT (GPIO 8)
|
||||
# Pins: sck=BCLK (GPIO 5), ws=WS/LRCK (GPIO 7), sd=DOUT (GPIO 8)
|
||||
i2s_format = I2S.MONO if channels == 1 else I2S.STEREO
|
||||
i2s = I2S(1,
|
||||
sck=Pin(9),
|
||||
ws=Pin(45),
|
||||
sck=Pin(5),
|
||||
ws=Pin(7),
|
||||
sd=Pin(8),
|
||||
mode=I2S.TX,
|
||||
ibuf=4096,
|
||||
@@ -368,8 +375,8 @@ def play_wav(filename, volume=50):
|
||||
bits=bits,
|
||||
format=i2s_format)
|
||||
|
||||
# 6. Enable Speaker Amplifier (GPIO 46)
|
||||
amp_pin = Pin(46, Pin.OUT, value=1)
|
||||
# 6. Enable Speaker Amplifier (GPIO 1, Active Low)
|
||||
amp_pin = Pin(1, Pin.OUT, value=0)
|
||||
|
||||
# 7. Read and stream chunks to I2S
|
||||
buf = bytearray(2048)
|
||||
@@ -381,7 +388,7 @@ def play_wav(filename, volume=50):
|
||||
|
||||
# 8. Clean up
|
||||
time.sleep_ms(100) # Let the remaining buffer play out
|
||||
amp_pin.value(0)
|
||||
amp_pin.value(1) # Disable amp
|
||||
i2s.deinit()
|
||||
mclk_pwm.deinit()
|
||||
f.close()
|
||||
|
||||
Reference in New Issue
Block a user