title: STM32: I2S input not working when using DMA
body: <p>I am trying to interface a 'STM32F401RET6 Nucleo-64' microcontroller with an <a href="https://www.adafruit.com/product/3421#technical-details" rel="nofollow noreferrer">Adafruit I2S microphone</a> in a mono setup. To accomplish this task, I would like to have DMA enabled.</p>
  <p>I used the Device Configuration Tool in STM32 Cube IDE to activate I2S3 using the following parameters:</p>
  <p><strong>I2S3</strong></p>
  <ul>
  <li>Full Duplex Master</li>
  <li>Transmission mode: Mode Master Receive;</li>
  <li>Communication standard: MSB First (Left Justified);</li>
  <li>Data and Frame Format: 24 Bits Data on 32 Bits Frame;</li>
  <li>Selected Audio Frequency: 48 kHz;</li>
  <li>Clock Source: I2S PLL Clock;</li>
  <li>Clock Polarity: Low;</li>
  <li>Master clock output disabled.</li>
  </ul>
  <p><strong>DMA</strong></p>
  <ul>
  <li>SPI_RX3, DMA 1 Stream 2, Peripheral to Memory, High Priority;</li>
  <li>FIFO, Threshold = Full, Data Width = Half Word, Burst Size = Single;</li>
  <li>In the NVIC settings, interrupts are enabled for both DMA 1 Stream 2 and SPI3.</li>
  </ul>
  <p>Next, the Code Generator Tool was used to automatically generate starting code. Some changes were made to this starting code:</p>
  <ul>
  <li>Set GPIO_PULL_DOWN so that tri-state always reads in 0;</li>
  </ul>
  <p>I already used an oscilloscope to plot the digital data waveform coming from the microphone. This seemed to be correct, i.e., sound triggered the microphone and this was visible in the most significant bits. This makes that the error is in reading in the data into the correct format, if I'm correct.
  To perform mono measurements, the datasheet states that one should use a 100k Ohm resistor, which is present in my setup.</p>
  <p>In my main.c program, I'm using the HAL function '<em>HAL_I2S_Receive_DMA</em>' to try to fill my array of 500 samples.</p>
  <p>main.c:</p>
  <pre><code>    /* Includes ------------------------------------------------------------------*/
  #include &quot;main.h&quot;
  I2S_HandleTypeDef hi2s3;
  DMA_HandleTypeDef hdma_spi3_rx;
  
  
  /* Private function prototypes -----------------------------------------------*/
  void SystemClock_Config(void);
  static void MX_GPIO_Init(void);
  static void MX_I2S3_Init(void);
  static void MX_DMA_Init(void);
  
  
  int main(void)
  {
    HAL_Init();
  
    /* Configure the system clock */
    SystemClock_Config();
  
    /* Initialize all configured peripherals */
    MX_GPIO_Init();
    MX_I2S3_Init();
    MX_DMA_Init();
  
    /* Infinite loop */
    HAL_StatusTypeDef retval; // return value
  
    volatile int16_t data[500] = {0};
    int16_t data_shifted[500];
  
    while (1)
    {
        retval = HAL_I2S_Receive_DMA(&amp;hi2s3, data, 500);
  //    for(short i=0; i&lt;500; i++){
  //        data_shifted[i] = data[i] &gt;&gt; 14;
  //    }
  
    HAL_Delay(1000);
    }
  }
  
  
  void SystemClock_Config(void)
  {
    RCC_OscInitTypeDef RCC_OscInitStruct = {0};
    RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  
    /* Configure the main internal regulator output voltage */
    __HAL_RCC_PWR_CLK_ENABLE();
    __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);
    /* Initializes the RCC Oscillators according to the specified parameters
       in the RCC_OscInitTypeDef structure.
    */
    RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
    RCC_OscInitStruct.HSIState = RCC_HSI_ON;
    RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
    RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
    RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
    RCC_OscInitStruct.PLL.PLLM = 8;
    RCC_OscInitStruct.PLL.PLLN = 84;
    RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
    RCC_OscInitStruct.PLL.PLLQ = 4;
    if (HAL_RCC_OscConfig(&amp;RCC_OscInitStruct) != HAL_OK)
    {
      Error_Handler();
    }
    /* Initializes the CPU, AHB and APB buses clocks */
    RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                                |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
    RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
    RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
    RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
    RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  
    if (HAL_RCC_ClockConfig(&amp;RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
    {
      Error_Handler();
    }
  }
  
  
  static void MX_I2S3_Init(void)
  {
    hi2s3.Instance = SPI3;
    hi2s3.Init.Mode = I2S_MODE_MASTER_RX;
    hi2s3.Init.Standard = I2S_STANDARD_MSB;
    hi2s3.Init.DataFormat = I2S_DATAFORMAT_24B;
    hi2s3.Init.MCLKOutput = I2S_MCLKOUTPUT_DISABLE;
    hi2s3.Init.AudioFreq = I2S_AUDIOFREQ_48K;
    hi2s3.Init.CPOL = I2S_CPOL_LOW;
    hi2s3.Init.ClockSource = I2S_CLOCK_PLL;
    hi2s3.Init.FullDuplexMode = I2S_FULLDUPLEXMODE_ENABLE;
    if (HAL_I2S_Init(&amp;hi2s3) != HAL_OK)
    {
      Error_Handler();
    }
  }
  
  // Enable DMA controller clock
  static void MX_DMA_Init(void)
  {
  
    /* DMA controller clock enable */
    __HAL_RCC_DMA1_CLK_ENABLE();
  
    /* DMA interrupt init */
    /* DMA1_Stream2_IRQn interrupt configuration */
    HAL_NVIC_SetPriority(DMA1_Stream2_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(DMA1_Stream2_IRQn);
  
  }
  
  /*GPIO Initialization Function */
  static void MX_GPIO_Init(void)
  {
  
    /* GPIO Ports Clock Enable */
    __HAL_RCC_GPIOA_CLK_ENABLE();
    __HAL_RCC_GPIOC_CLK_ENABLE();
  
  }
  
  /* This function is executed in case of error occurrence. */
  void Error_Handler(void)
  {
    /* Error_Handler_Debug */
    /* User can add his own implementation to report the HAL error return state */
    __disable_irq();
    while (1)
    {
    }
  }
  
  #ifdef  USE_FULL_ASSERT
  /**
    Reports the name of the source file and the source line number
    where the assert_param error has occurred.
    */
  void assert_failed(uint8_t *file, uint32_t line)
  {
    /* User can add his own implementation to report the file name and line number,
       ex: printf(&quot;Wrong parameters value: file %s on line %d\r\n&quot;, file, line) */
  }
  #endif /* USE_FULL_ASSERT */
  </code></pre>
  <p>When I debug my code and put a breakpoint on line 34 in 'main.c', then the microcontroller memory does not update its values accordingly. All values stay equal to zero. Both DMA Status Flags are set to 1.
  I guessing that the problem has something to do with timings, but I wasn't able to solve this problem until now.</p>
  <p><a href="https://github.com/michiel-j/STM32F401RE_I2S" rel="nofollow noreferrer">Link to complete source code on GitHub</a></p>
  <p><a href="https://github.com/cnoviello/stm32-nucleof4/blob/53951f1447bda2d928ee2401ee6e4bf9d93444d3/stm32-nucleof4-uart/system/src/stm32f4-hal/stm32f4xx_hal_i2s.c#L893" rel="nofollow noreferrer">Link to HAL_I2S_Receive_DMA function on GitHub</a></p>
  <p>Thanks in advance.</p>