← index #11635Issue #11245
Related · high · value 0.989
QUERY · ISSUE

I2S MONO format does not work

openby StreakingJerryopened 2023-05-27updated 2024-07-07
bug

I am running MicroPython v1.20.0 on 2023-04-26; ESP32S3 module (spiram octal) with ESP32S3

Here is my code:

from machine import Pin, I2S
import array

sck_pin = Pin(14)   # Serial clock output
ws_pin = Pin(13)    # Word clock output
sd_pin = Pin(12)    # Serial data output

audio = I2S(0,
            sck=sck_pin, ws=ws_pin, sd=sd_pin,
            mode=I2S.TX,
            bits=16,
            format=I2S.MONO,
            rate=8000,
            ibuf=20000)

d = array.array("H", [0]*2)
d[0] = 0b1111111111111111
d[1] = 0b1000100010001000
print(d)
while True:
    audio.write(d)

According to the I2S protocol, 16 bits data should be transfered to both left channel and right channel. Therefore d[0] should be transfered twice in a cycle of Word clock, and also the d[1].

However, This is what I get from oscilloscope:
002

The yellow line is the ws_pin and the green line is sd_pin.

Obviousely it transfer the d[0] to left channel and d[1] to right channel. This is the Stereo format.

Also, May I ask what is the first parameter id of I2S() stand for? The document is quite unclear. Something weird with this parameter.

If I set it to 0 or 1, there is nothing different. I can't set it to 2.

However, If you put this code in the beginning:

from machine import Pin, I2S
import array

sck_pin = Pin(14)   # Serial clock output
ws_pin = Pin(13)    # Word clock output
sd_pin = Pin(12)    # Serial data output

audio = I2S(1,
            sck=sck_pin, ws=ws_pin, sd=sd_pin,
            mode=I2S.TX,
            bits=16,
            format=I2S.MONO,
            rate=8000,
            ibuf=20000)

aduio.deinit()

audio = I2S(0,
            sck=sck_pin, ws=ws_pin, sd=sd_pin,
            mode=I2S.TX,
            bits=16,
            format=I2S.MONO,
            rate=8000,
            ibuf=20000)

you will surprisingly find on the oscilloscope that the i2s protocol turn from the general Phillips Standard to the left-justified. This only occur when you define the object twice with different id (of course deinit it in between)

CANDIDATE · ISSUE

I2S switches from MSB-first to LSB-first on an ESP32-S3

closedby ma261065opened 2023-04-12updated 2024-04-08
bug

Using the latest nightly build (v1.19.1-1014-gbde222ce8 (2023-04-11) ) on an ESP32-S3 with SPIRAM, I am sending a wav file that is stored on the onboard filesystem to a PCM5102A DAC via I2S.

When I run the code the first time after power on or reset (soft or hard) the audio is heard clearly.

If I then run the same code again, it is just a mess of static - I can just hear the beat of the music in all the noise though.

The only way to get it working again is to reboot. This happens on I2S0 as well as I2S1, and have tried different pins in case I was clashing with one of the ESP32 pins that has alternative functions.

Also, when rebooting I see the following message:
E (130390) I2S: i2s_driver_uninstall(2047): I2S port 0 has not installed
just before the MPY: soft reboot message

I am not getting this problem on a non-S3 variant.

Code below:

from machine import I2S, Pin
import io

sck_pin = Pin(13)   # Serial clock output
ws_pin = Pin(14)    # Word clock output
sd_pin = Pin(17)    # Serial data output

buf = bytearray(1024)

audio_out = I2S(0, sck=sck_pin, ws=ws_pin, sd=sd_pin, mode=I2S.TX, bits=16, format=I2S.STEREO, rate=16000, ibuf=20000)

with io.open("example.wav", 'rb') as fp:
    size = fp.readinto(buf)

    while (size > 0):
        print('.', end='')
        audio_out.write(buf)
        size = fp.readinto(buf)
    
print("Finished")    
fp.close()
audio_out.deinit()

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied