Skip to main content
Visitor II
February 12, 2021
Question

STM32F407 Discovery INMP441 MEMS Read Problem

  • February 12, 2021
  • 2 replies
  • 2419 views
int main(void)
{
 /* USER CODE BEGIN 1 */
arm_cfft_radix4_instance_f32 S; 
float32_t maxValue; 
uint32_t maxIndex; 
uint16_t i;
uint16_t height[128];
 /* USER CODE END 1 */
 
 /* MCU Configuration--------------------------------------------------------*/
 
 /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
 HAL_Init();
 
 /* USER CODE BEGIN Init */
 
 /* USER CODE END Init */
 
 /* Configure the system clock */
 SystemClock_Config();
 
 /* USER CODE BEGIN SysInit */
 
 /* USER CODE END SysInit */
 
 /* Initialize all configured peripherals */
 MX_GPIO_Init();
 MX_DAC_Init();
 MX_TIM2_Init();
 MX_I2S3_Init();
 /* USER CODE BEGIN 2 */
	HAL_Delay(100);
 
 HAL_TIM_Base_Start_IT(&htim2);
	
 
 /* USER CODE END 2 */
 
 /* Infinite loop */
 /* USER CODE BEGIN WHILE */
 while (1)
 {
 /* USER CODE END WHILE */
 
 /* USER CODE BEGIN 3 */
		HAL_I2S_Receive(&hi2s3,(uint16_t *)buffer,512,100);
 
//		for(uint16_t y=0;y<SAMPLES;y++)
//		{
//			buffer[y] >>= 14;
//		}
		
		
		for (i = 0; i < SAMPLES; i++) {
 
			Input[(uint16_t)i] = (float32_t)((buffer[i] - (float32_t)16777215.0) / (float32_t)16777215.0);
 
// Input[(uint16_t)(i+1)] = 0;
		}
		
		arm_cfft_radix4_init_f32(&S, FFT_SIZE, 0, 1);
		
		arm_cfft_radix4_f32(&S, Input);
		
		arm_cmplx_mag_f32(Input, Output, FFT_SIZE);
		
		arm_max_f32(Output, FFT_SIZE, &maxValue, &maxIndex);
		
 
		for(uint16_t k=0;k<FFT_SIZE/2;k++)
		{
			deger[k]=(float32_t)Output[k];
		}
 
 }
 /* USER CODE END 3 */
}
 
/**
 * @brief System Clock Configuration
 * @retval None
 */
void SystemClock_Config(void)
{
 RCC_OscInitTypeDef RCC_OscInitStruct = {0};
 RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
 RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
 
 /** Configure the main internal regulator output voltage 
 */
 __HAL_RCC_PWR_CLK_ENABLE();
 __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
 /** Initializes the CPU, AHB and APB busses clocks 
 */
 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
 RCC_OscInitStruct.HSEState = RCC_HSE_ON;
 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
 RCC_OscInitStruct.PLL.PLLM = 4;
 RCC_OscInitStruct.PLL.PLLN = 168;
 RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
 RCC_OscInitStruct.PLL.PLLQ = 7;
 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
 {
 Error_Handler();
 }
 /** Initializes the CPU, AHB and APB busses 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_DIV4;
 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
 
 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
 {
 Error_Handler();
 }
 PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_I2S;
 PeriphClkInitStruct.PLLI2S.PLLI2SN = 98;
 PeriphClkInitStruct.PLLI2S.PLLI2SR = 2;
 if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
 {
 Error_Handler();
 }
}
 
/**
 * @brief DAC Initialization Function
 * @param None
 * @retval None
 */
static void MX_DAC_Init(void)
{
 
 /* USER CODE BEGIN DAC_Init 0 */
 
 /* USER CODE END DAC_Init 0 */
 
 DAC_ChannelConfTypeDef sConfig = {0};
 
 /* USER CODE BEGIN DAC_Init 1 */
 
 /* USER CODE END DAC_Init 1 */
 /** DAC Initialization 
 */
 hdac.Instance = DAC;
 if (HAL_DAC_Init(&hdac) != HAL_OK)
 {
 Error_Handler();
 }
 /** DAC channel OUT1 config 
 */
 sConfig.DAC_Trigger = DAC_TRIGGER_NONE;
 sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;
 if (HAL_DAC_ConfigChannel(&hdac, &sConfig, DAC_CHANNEL_1) != HAL_OK)
 {
 Error_Handler();
 }
 /* USER CODE BEGIN DAC_Init 2 */
 
 /* USER CODE END DAC_Init 2 */
 
}
 
/**
 * @brief I2S3 Initialization Function
 * @param None
 * @retval None
 */
static void MX_I2S3_Init(void)
{
 
 /* USER CODE BEGIN I2S3_Init 0 */
 
 /* USER CODE END I2S3_Init 0 */
 
 /* USER CODE BEGIN I2S3_Init 1 */
 
 /* USER CODE END I2S3_Init 1 */
 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_44K;
 hi2s3.Init.CPOL = I2S_CPOL_LOW;
 hi2s3.Init.ClockSource = I2S_CLOCK_PLL;
 hi2s3.Init.FullDuplexMode = I2S_FULLDUPLEXMODE_DISABLE;
 if (HAL_I2S_Init(&hi2s3) != HAL_OK)
 {
 Error_Handler();
 }
 /* USER CODE BEGIN I2S3_Init 2 */
 
 /* USER CODE END I2S3_Init 2 */
 
}

I am trying to read data from INMP441 mems sensor with I2S protocol with stm32f407 discovery, with 24bit 32 frame configuration at 44khz, but I think I have a problem in reading my codes above, can you help me?

    This topic has been closed for replies.

    2 replies

    Visitor II
    February 12, 2021

    Hello

    A value 96 to PLLI2S.PLLI2SN gives less clock error than 98. (for 44kHz clock freq)

    >>HAL_I2S_Receive(&hi2s3,(uint16_t *)buffer,512,100);

    this function asks for 256 samples. Is SAMPLES 256?

    gşen.1Author
    Visitor II
    February 20, 2021

    As you said, I ran it at 96 Mhz. My problem is solved. I concluded the process by reading 2 times in I2S and combining 16-bit data. Thank you.

    Visitor II
    April 3, 2025

    I am running into the same problem and I have been trying to read 2 times in I2S and combining 16-bit data with this line, and then extract the first 24 bits from the combined 32-bit data. However, so far the audio that I extracted is still quite distorted. Would you mind sharing more details onto how you got the right audio? Thank you so much!