Use I2S in I2S_MODE_DAC_BUILT_IN Mode

BuddyCasino
Posts: 263
Joined: Sun Jun 19, 2016 12:00 am

Use I2S in I2S_MODE_DAC_BUILT_IN Mode

Postby BuddyCasino » Fri Jan 20, 2017 2:21 pm

I found the sample code in i2s.h, and noticed that we can use the built-in DAC to render I2S audio.
However I can't seem to get it working.

This is my init function:

Code: Select all

static void init_i2s_dac()
{

    i2s_config_t i2s_config = {
        .mode = I2S_MODE_MASTER | I2S_MODE_TX | I2S_MODE_DAC_BUILT_IN,          // Only TX
        .sample_rate = 44100,
        .bits_per_sample = 8,                                                   // Only 8-bit DAC support
        .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,                           // 2-channels
        .communication_format = I2S_COMM_FORMAT_I2S_MSB,
        .dma_buf_count = 14,                                                    // number of buffers, 128 max.
        .dma_buf_len = 32*2,                                                    // size of each buffer
        .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1                                // Interrupt level 1
    };

    i2s_pin_config_t pin_config = {
        .bck_io_num = 26,
        .ws_io_num = 25,
        .data_out_num = 22,
        .data_in_num = I2S_PIN_NO_CHANGE                                          // Not used
    };

    i2s_driver_install(I2S_NUM, &i2s_config, 0, NULL);
    i2s_set_pin(I2S_NUM, &pin_config);
}
My normal I2S pipeline works fine, but when I use the DAC, I get DMA and buffer underruns.
I feed it samples like this:

Code: Select all

static int convert_sample(short s) {

    // convert 16bit to 8bit and duplicate mono sample to both channels
    char eightBit = s >> 8;
    short samp = eightBit;
    samp = (samp) & 0xff;
    samp = (samp << 8) | samp;
    return samp;
}
Any idea whats wrong?

telanoc
Posts: 5
Joined: Fri Jan 13, 2017 1:02 am

Re: Use I2S in I2S_MODE_DAC_BUILT_IN Mode

Postby telanoc » Wed Jan 25, 2017 4:05 am

Doesn't this do the same thing?

Code: Select all

static int convert_sample(short s) {

    return s & 0xff00 | s >> 8;
  
}

telanoc
Posts: 5
Joined: Fri Jan 13, 2017 1:02 am

Re: Use I2S in I2S_MODE_DAC_BUILT_IN Mode

Postby telanoc » Wed Jan 25, 2017 8:49 pm

Hmm... I think I should have typed

Code: Select all

return (unsigned short) s & 0xff00 | (unsigned short) s >> 8;
:oops:

BuddyCasino
Posts: 263
Joined: Sun Jun 19, 2016 12:00 am

Re: Use I2S in I2S_MODE_DAC_BUILT_IN Mode

Postby BuddyCasino » Fri Jan 27, 2017 9:19 am

Probably, yes. I'm not very experienced with C though, so the commonly known bit-twiddling idioms don't come easy to me, yet. This method should return a short btw (2 x 8 Bit), my mistake. Not that this fixes anything.

ESP_Sprite
Posts: 9769
Joined: Thu Nov 26, 2015 4:08 am

Re: Use I2S in I2S_MODE_DAC_BUILT_IN Mode

Postby ESP_Sprite » Sun Jan 29, 2017 3:48 am

Fyi, it seems the I2S driver has a bug in DAC mode: the sample rate it sets is actually 8 times the sample rate you tell it to use. I have a bug fix in the internal merge queue, it will probably be merged after the holidays.

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: Use I2S in I2S_MODE_DAC_BUILT_IN Mode

Postby WiFive » Sun Jan 29, 2017 5:15 am

ESP_Sprite wrote:Fyi, it seems the I2S driver has a bug in DAC mode: the sample rate it sets is actually 8 times the sample rate you tell it to use. I have a bug fix in the internal merge queue, it will probably be merged after the holidays.
Interesting is that because all the bits are latched into the DAC register on one clock edge?

ESP_Sprite
Posts: 9769
Joined: Thu Nov 26, 2015 4:08 am

Re: Use I2S in I2S_MODE_DAC_BUILT_IN Mode

Postby ESP_Sprite » Sun Jan 29, 2017 7:20 am

I think the DAC mode is one of the odd ones that uses BCLK as the latch clock. All other parallel modes seem to use WS for that. Not entirely sure; I can't really ask the hw people at this moment.

BuddyCasino
Posts: 263
Joined: Sun Jun 19, 2016 12:00 am

Re: Use I2S in I2S_MODE_DAC_BUILT_IN Mode

Postby BuddyCasino » Mon Jan 30, 2017 9:56 am

Thanks for the feedback! Bit of a relief to finally know the cause.

Who is online

Users browsing this forum: Google [Bot], michcfr and 97 guests