I2s DMA buffer size incorrect

JP5654
Posts: 17
Joined: Fri Jul 07, 2023 11:50 pm

I2s DMA buffer size incorrect

Postby JP5654 » Fri Sep 01, 2023 6:26 pm

Hello,
I'm confused about something, the DMA buffer that the i2s creates is not the size I expect.

According to the documentation:
dma_buffer_size = dma_frame_num * slot_num * slot_bit_width / 8
This equation does not hold up for a slot width of 8bits. As an example if I have slot_bit_width=8 with slot_num=9 and dma_frame_num=32 I would expect to get a dma_buffer_size of 288bytes but the function i2s_get_buf_size() will actually return a buffer size of 576bytes. So when using a slot_bit_width of 8, I will always get a buffer size that is double the dma_frame_num which is certainly not what I want. This is because of the bytes_per_sample calculation.

uint32_t bytes_per_sample = ((data_bit_width + 15) / 16) * 2;

Code: Select all

uint32_t i2s_get_buf_size(i2s_chan_handle_t handle, uint32_t data_bit_width, uint32_t dma_frame_num)
{
    uint32_t active_chan = handle->active_slot;
    uint32_t bytes_per_sample = ((data_bit_width + 15) / 16) * 2;
    uint32_t bytes_per_frame = bytes_per_sample * active_chan;
    uint32_t bufsize = dma_frame_num * bytes_per_frame;
    /* Limit DMA buffer size if it is out of range (DMA buffer limitation is 4092 bytes) */
    if (bufsize > I2S_DMA_BUFFER_MAX_SIZE) {
        uint32_t frame_num = I2S_DMA_BUFFER_MAX_SIZE / bytes_per_frame;
        bufsize = frame_num * bytes_per_frame;
        ESP_LOGW(TAG, "dma frame num is out of dma buffer size, limited to %"PRIu32, frame_num);
    }
    return bufsize;
}

Am I doing something wrong? Or is this a bug in the driver code? Thank you for your help.

MicroController
Posts: 1692
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: I2s DMA buffer size incorrect

Postby MicroController » Fri Sep 01, 2023 10:40 pm

but take care when using 8-bit and 24-bit data width. For 8-bit width, the written buffer should still use uint16_t (i.e., align with 2 bytes), and only the high 8 bits are valid while the low 8 bits are dropped

JP5654
Posts: 17
Joined: Fri Jul 07, 2023 11:50 pm

Re: I2s DMA buffer size incorrect

Postby JP5654 » Tue Sep 05, 2023 4:05 pm

Oh, well thank you! That is useful. Do you mind letting me know where you got that from? I have been using the latest version of https://docs.espressif.com/projects/esp ... s/i2s.html for documentation and I don't see anything in there that states that fact.

JP5654
Posts: 17
Joined: Fri Jul 07, 2023 11:50 pm

Re: I2s DMA buffer size incorrect

Postby JP5654 » Tue Sep 05, 2023 4:25 pm

I guess the other thing I should mention, I'm not seeing any bits being dropped. I am getting my data correctly. So maybe this comment only applied for older versions of the i2s driver?

MicroController
Posts: 1692
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: I2s DMA buffer size incorrect

Postby MicroController » Tue Sep 05, 2023 4:30 pm

Ok, now you got me ;-)
As I didn't find specifics in your post, I assumed you were asking about the ESP32; and for that one the note in the documentation talks about 16-bit alignment. For the S3, the note says otherwise, and given your original observation I don't know what's correct.

JP5654
Posts: 17
Joined: Fri Jul 07, 2023 11:50 pm

Re: I2s DMA buffer size incorrect

Postby JP5654 » Tue Sep 05, 2023 4:37 pm

Ah I'm sorry for not clarifying, you are correct, I didn't mention that I'm using ESP32-S3 with ESP-IDFv5.1.

Ya so I guess it's not clear with the ESP32-S3. Hopefully somebody from Espressif can comment on it. Thanks for your help!

Who is online

Users browsing this forum: Bing [Bot] and 281 guests