AnsweredAssumed Answered

I2S Full Duplex Mode + DMA + HAL_Cube library

Question asked by Pachchigar.Pinkesh on Nov 30, 2016
Latest reply on Nov 30, 2016 by FTITI.Walid
Hello,

In our application for audio codec IC, we have used I2S in full duplex mode with DMA.
I have copied code for I2S . Can anyone suggest me what could be wrong with this ?

static void I2SInit()
{
     // Enable PLLI2S
     __HAL_RCC_PLLI2S_ENABLE();

     // DMA controller clock enable
     __HAL_RCC_DMA1_CLK_ENABLE();

     // DMA1_Stream0_IRQn interrupt configuration
     HAL_NVIC_SetPriority(DMA1_Stream0_IRQn, 0, 0);
     HAL_NVIC_EnableIRQ(DMA1_Stream0_IRQn);

     // DMA1_Stream7_IRQn interrupt configuration 
     HAL_NVIC_SetPriority(DMA1_Stream7_IRQn, 0, 0);
     HAL_NVIC_EnableIRQ(DMA1_Stream7_IRQn);

     hi2s3.Instance = SPI3;
     hi2s3.Init.Mode = I2S_MODE_MASTER_TX;
     hi2s3.Init.Standard = I2S_STANDARD_PHILIPS;
     hi2s3.Init.DataFormat = I2S_DATAFORMAT_16B;
     hi2s3.Init.MCLKOutput = I2S_MCLKOUTPUT_DISABLE;
     hi2s3.Init.AudioFreq = I2S_AUDIOFREQ_8K;
     hi2s3.Init.CPOL = I2S_CPOL_LOW;
     hi2s3.Init.ClockSource = I2S_CLOCK_PLL;
     hi2s3.Init.FullDuplexMode = I2S_FULLDUPLEXMODE_ENABLE;

    if (HAL_I2S_Init(&hi2s3) != HAL_OK)
        while(1);
}


void HAL_I2S_MspInit(I2S_HandleTypeDef* hi2s)
{
    GPIO_InitTypeDef gpio;


    if(hi2s->Instance == SPI3)
    {
        // Peripheral clock enable
        __HAL_RCC_SPI3_CLK_ENABLE();
        __HAL_RCC_GPIOA_CLK_ENABLE();
        __HAL_RCC_GPIOC_CLK_ENABLE();
  
        /**I2S3 GPIO Configuration    
        PA15     ------> I2S3_WS
        PC10     ------> I2S3_CK
        PC11     ------> I2S3_ext_SD
        PC12     ------> I2S3_SD 
        */
        gpio.Pin = GPIO_PIN_15;
        gpio.Mode = GPIO_MODE_AF_PP;
        gpio.Pull = GPIO_NOPULL;
        gpio.Speed = GPIO_SPEED_FREQ_LOW;
        gpio.Alternate = GPIO_AF6_SPI3;
        HAL_GPIO_Init(GPIOA, &gpio);


        gpio.Pin = GPIO_PIN_10|GPIO_PIN_12;
        gpio.Mode = GPIO_MODE_AF_PP;
        gpio.Pull = GPIO_NOPULL;
        gpio.Speed = GPIO_SPEED_FREQ_LOW;
        gpio.Alternate = GPIO_AF6_SPI3;
        HAL_GPIO_Init(GPIOC, &gpio);


        gpio.Pin = GPIO_PIN_11;
        gpio.Mode = GPIO_MODE_AF_PP;
        gpio.Pull = GPIO_NOPULL;
        gpio.Speed = GPIO_SPEED_FREQ_LOW;
        gpio.Alternate = GPIO_AF5_I2S3ext;
        HAL_GPIO_Init(GPIOC, &gpio);


        // Peripheral DMA init
        hdma_spi3_tx.Instance = DMA1_Stream7;
        hdma_spi3_tx.Init.Channel = DMA_CHANNEL_0;
        hdma_spi3_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
        hdma_spi3_tx.Init.PeriphInc = DMA_PINC_DISABLE;
        hdma_spi3_tx.Init.MemInc = DMA_MINC_ENABLE;
        hdma_spi3_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
        hdma_spi3_tx.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
        hdma_spi3_tx.Init.Mode = DMA_CIRCULAR;
        hdma_spi3_tx.Init.Priority = DMA_PRIORITY_LOW;
        hdma_spi3_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
        if (HAL_DMA_Init(&hdma_spi3_tx) != HAL_OK)
            while(1);


        __HAL_LINKDMA(hi2s,hdmatx,hdma_spi3_tx);


        hdma_i2s3_ext_rx.Instance = DMA1_Stream0;
        hdma_i2s3_ext_rx.Init.Channel = DMA_CHANNEL_3;
        hdma_i2s3_ext_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
        hdma_i2s3_ext_rx.Init.PeriphInc = DMA_PINC_DISABLE;
        hdma_i2s3_ext_rx.Init.MemInc = DMA_MINC_ENABLE;
        hdma_i2s3_ext_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
        hdma_i2s3_ext_rx.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
        hdma_i2s3_ext_rx.Init.Mode = DMA_CIRCULAR;
        hdma_i2s3_ext_rx.Init.Priority = DMA_PRIORITY_LOW;
        hdma_i2s3_ext_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
        if (HAL_DMA_Init(&hdma_i2s3_ext_rx) != HAL_OK)
            while(1);


        __HAL_LINKDMA(hi2s,hdmarx,hdma_i2s3_ext_rx);
    }   
}


void DMA1_Stream0_IRQHandler(void)
{
    HAL_DMA_IRQHandler(&hdma_i2s3_ext_rx);
}


void DMA1_Stream7_IRQHandler(void)
{
    HAL_DMA_IRQHandler(&hdma_spi3_tx);
}


void HAL_I2S_RxCpltCallback(I2S_HandleTypeDef *hi2s)
{
    // Task
}


void HAL_I2S_RxHalfCpltCallback(I2S_HandleTypeDef *hi2s)
{
    // Task
}


void HAL_I2S_TxCpltCallback(I2S_HandleTypeDef *hi2s)
{
    // Task
}


void HAL_I2S_TxHalfCpltCallback(I2S_HandleTypeDef *hi2s)
{
    // Task
}


//Based on User action
static void EnableRxDma()
{
    HAL_I2S_Receive_DMA(&hi2s3, _buffer, (2 * I2S_DMA_BUF_LEN));
}


static void DisableRxDma()
{
     HAL_I2S_DMAPause(&hi2s3);
}


static void EnableTxDma()
{
      HAL_I2S_Transmit_DMA(&hi2s3, _buffer, (2 * I2S_DMA_BUF_LEN));
}


static void DisableTxDma()
{
     HAL_I2S_DMAPause(&hi2s3);
}





Outcomes