miketeachman / micropython-esp32-i2s-examples

Usage and examples for I2S support on the ESP32 microcontroller
MIT License
152 stars 29 forks source link

Strange issue with I2S.ALL_LEFT #25

Closed cgreening closed 3 years ago

cgreening commented 3 years ago

I have a working configuration with C++

i2s_config_t i2s_mic_Config = {
    .mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_RX),
    .sample_rate = SAMPLE_RATE,
    .bits_per_sample = I2S_BITS_PER_SAMPLE_32BIT,
    .channel_format = I2S_CHANNEL_FMT_ONLY_LEFT,
    .communication_format = I2S_COMM_FORMAT_I2S,
    .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
    .dma_buf_count = 4,
    .dma_buf_len = 1024,
    .use_apll = false,
    .tx_desc_auto_clear = false,
    .fixed_mclk = 0};

When I try and recreate this in python:

audio_in = I2S(I2S.NUM1,
               bck=mic_bck_pin,
               ws=mic_ws_pin,
               sdin=mic_sdin_pin,
               standard=I2S.PHILIPS,
               mode=I2S.MASTER_RX, 
               dataformat=I2S.B32,                       
               channelformat=I2S.ONLY_LEFT,
               samplerate=16000, 
               dmacount=5,
               dmalen=1024)

I don't get any values - everything is zero.

My microphone board is configured to be on the left channel so ONLY_LEFT should work - and it does work in my IDF project. I've looked through the code you've added to Micropython and I can't see any reason for it not to work.

Here's my complete test code:

from machine import I2S
from machine import Pin
import time

mic_bck_pin = Pin(26)
mic_ws_pin = Pin(22)
mic_sdin_pin = Pin(21)

record_pin = Pin(23, Pin.IN, Pin.PULL_DOWN)

def wait_for_button():
    while record_pin.value() == 0:
        time.sleep_ms(100)
    time.sleep_ms(100)

audio_in = I2S(I2S.NUM1,
               bck=mic_bck_pin,
               ws=mic_ws_pin,
               sdin=mic_sdin_pin,
               standard=I2S.PHILIPS,
               mode=I2S.MASTER_RX, 
               dataformat=I2S.B32,                       
               channelformat=I2S.ONLY_LEFT,
               samplerate=16000, 
               dmacount=5,
               dmalen=1024)

print(audio_in)

recorded_data = []

print("Press and hold button to record")

wait_for_button()

print("Recording")

while record_pin.value() == 1:
    in_samples = bytearray(2048)
    read_bytes = audio_in.readinto(in_samples)
    print(in_samples[0], in_samples[4])
    recorded_data.append(in_samples)

print("Finished Recording")
cgreening commented 3 years ago

I'm wondering if it's something to do with this bug and the workaround that is being applied?

https://github.com/espressif/esp-idf/issues/6625

cgreening commented 3 years ago

Removing the bug fix lines seems to make my code work. I'm using IDF 4.2 to build micropython.

cgreening commented 3 years ago

For anyone else ending up here from Google - this code and PR has been superseded by a new one - https://github.com/miketeachman/micropython-i2s-examples