I2S Playback Latency

Hi,

  I am using SGTL5000 CODEC to playback the LINEIN and I am using the SGTL5000 driver from this link

I am using this I2S configuration

For I2S Loopback code itself the latency is around 18ms,

Can you guide me on how can i reduce the latency down to 5ms

  • Hi,

    It's an unofficial example, and I don't have the HW to test here. The only thing I can see that could perhaps affect this is what AUDIO_FRAME_WORDS is set to, and what frequency you run the I2S at.

  • Hi Sigurd,

    Here is the configure of I2S

     // Initialize I2S 
        m_i2s_config.sck_pin      = DRV_SGTL5000_I2S_PIN_BCLK;
        m_i2s_config.lrck_pin     = DRV_SGTL5000_I2S_PIN_LRCLK;
        m_i2s_config.mck_pin      = DRV_SGTL5000_I2S_PIN_MCLK;
        m_i2s_config.sdout_pin    = DRV_SGTL5000_I2S_PIN_TX;
        m_i2s_config.sdin_pin     = DRV_SGTL5000_I2S_PIN_RX;
        m_i2s_config.irq_priority = DRV_SGTL5000_I2S_IRQPriority;
        m_i2s_config.mode         = NRF_I2S_MODE_MASTER;
        m_i2s_config.format       = NRF_I2S_FORMAT_I2S;
        m_i2s_config.alignment    = NRF_I2S_ALIGN_LEFT;
        m_i2s_config.sample_width = NRF_I2S_SWIDTH_16BIT;
        m_i2s_config.channels     = NRF_I2S_CHANNELS_LEFT;
        m_i2s_config.mck_setup    = NRF_I2S_MCK_32MDIV8; 
        m_i2s_config.ratio        = NRF_I2S_RATIO_96X;

    and the Frame size is

    #define AUDIO_FRAME_WORDS                   320
    #define AUDIO_SAMPLE_LEN                  AUDIO_FRAME_WORDS/2
    #define I2S_BUFFER_SIZE_WORDS               AUDIO_FRAME_WORDS * 2   // Double buffered - I2S lib will switch between using first and second half
    static uint32_t  m_i2s_tx_buffer[I2S_BUFFER_SIZE_WORDS];
    static uint32_t  m_i2s_rx_buffer[I2S_BUFFER_SIZE_WORDS];
    
    
    int main()
    {
    ..
     // Enable audio
        drv_sgtl5000_init_t sgtl_drv_params;
        sgtl_drv_params.i2s_tx_buffer           = (void*)m_i2s_tx_buffer;
        sgtl_drv_params.i2s_rx_buffer           = (void*)m_i2s_rx_buffer;
        sgtl_drv_params.i2s_buffer_size_words   = I2S_BUFFER_SIZE_WORDS/2;
        sgtl_drv_params.i2s_evt_handler         = i2s_sgtl5000_driver_evt_handler;
        sgtl_drv_params.fs                      = DRV_SGTL5000_FS_31250HZ;
        
        m_i2s_tx_buffer[0] = 167;
        m_i2s_tx_buffer[I2S_BUFFER_SIZE_WORDS/2] = 167;
        NRF_LOG_INFO("size of  m_i2s_tx_buffer %d, %d", sizeof(m_i2s_tx_buffer) / sizeof(uint32_t), I2S_BUFFER_SIZE_WORDS);
        NRF_LOG_INFO("i2s_initial_tx_buffer addr1: %d, addr2: %d", m_i2s_tx_buffer, m_i2s_tx_buffer + I2S_BUFFER_SIZE_WORDS/2);
        NRF_LOG_INFO("i2s_initial_Rx_buffer addr1: %d, addr2: %d", m_i2s_rx_buffer, m_i2s_rx_buffer + I2S_BUFFER_SIZE_WORDS/2);
        ..
        ..
        drv_sgtl5000_start();
        }
        
        uint32_t drv_sgtl5000_start(void)
    {
        if (m_state == SGTL5000_STATE_IDLE)
        {
            m_state = SGTL5000_STATE_RUNNING;
            nrf_drv_i2s_buffers_t const initial_buffers = {
                .p_tx_buffer = m_external_i2s_buffer.tx_buffer,
                .p_rx_buffer = m_external_i2s_buffer.rx_buffer,
            };
            (void)nrf_drv_i2s_start(&initial_buffers, (m_external_i2s_buffer.buffer_size_words/2), 0);
            
            return NRF_SUCCESS;
        }
        
        return NRF_ERROR_INVALID_STATE;
    }

    I have check that we are initializing the I2S with 160 words.

Related