Questions about PDM MEMS microphones

48 views Asked by At

I am using STM32F407 Discovery Board for my audio project. There is a PDM mems microphone on this board. I want to get sound signal over I2S with DMA. For that I am using SWV graph in İDE. I tried all I found on the internet. I have a few questions to make sure I am right. I think I need to convert PDM data to PCM for meaningful graph. Is that right? I actually used the pdm2pcm library to do that. But all I get is random numbers, it seems. Then, I try to observe the PDM signal with oscilloscope. The data signal was coming in two voltage levels. Is that normal for the PDM signal?

yellow signal is I2S CK(pb10), Cyan signal is data(pc3)
image is taken at start of the run. 1 2 3

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2024 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "adc.h"
#include "crc.h"
#include "dma.h"
#include "i2s.h"
#include "pdm2pcm.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#define ARM_MATH_CM4
#include "arm_math.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
arm_rfft_fast_instance_f32 fft_instance;

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

#define FFT_SIZE  1024
#define BUFFER_SIZE (FFT_SIZE*2)
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */
uint32_t ADC_Double_Buffer[BUFFER_SIZE];
float FFT_InBuffer[FFT_SIZE];
float FFT_OutBuffer[FFT_SIZE];
float FFT_Mag[FFT_SIZE/2];

uint32_t Peak_Frequency;
uint32_t Peak_High_Frequency;

uint8_t Data_Ready = 0;

static volatile uint32_t *FFT_HalfBufferPtr;


uint16_t I2S_Double_Buffer[BUFFER_SIZE];
uint16_t I2S_PCM_Double_Buffer[16];

uint16_t pdmdata;
uint16_t pcmdata;
/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
void Process_Data(void);
/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
uint32_t PDM_Filter(void *pDataIn, void *pDataOut, PDM_Filter_Handler_t *pHandler);
void HAL_I2S_RxCpltCallback(I2S_HandleTypeDef *hi2s);
/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */

  /* 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_DMA_Init();
  MX_ADC1_Init();
  MX_I2S2_Init();
  MX_CRC_Init();
  MX_PDM2PCM_Init();
  /* USER CODE BEGIN 2 */

  //PDM_Filter_Init(&PDM1_filter_handler);

  arm_rfft_fast_init_f32(&fft_instance, FFT_SIZE);
  //HAL_ADC_Start_DMA(&hadc1, (uint32_t *)ADC_Double_Buffer, BUFFER_SIZE);

  HAL_I2S_Receive_DMA(&hi2s2, (uint16_t *)I2S_Double_Buffer, BUFFER_SIZE);




  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
//    if(Data_Ready){
//      Process_Data();
//    }
  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
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_SCALE1);

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  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 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_DIV4;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
  {
    Error_Handler();
  }
}

/* USER CODE BEGIN 4 */
//void HAL_ADC_ConvHalfCpltCallback(ADC_HandleTypeDef *hadc){
//
//  if(hadc == &hadc1){
//      FFT_HalfBufferPtr = &ADC_Double_Buffer[0];
//      Data_Ready = 1;
//  }
//}
//void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc){
//
//  if(hadc == &hadc1){
//
//
//      FFT_HalfBufferPtr = &ADC_Double_Buffer[BUFFER_SIZE/2];
//      Data_Ready = 1;
//  }
//
//}

void HAL_I2S_RxCpltCallback(I2S_HandleTypeDef *hi2s){
    if(hi2s == &hi2s2){
        pdmdata = I2S_Double_Buffer[0];
        PDM_Filter(&I2S_Double_Buffer[0], &I2S_PCM_Double_Buffer[0], &PDM1_filter_handler);
        pcmdata = I2S_PCM_Double_Buffer[0];

    }
}

void Process_Data(void){
    for (int n = 0; n < FFT_SIZE; n++) {

        FFT_InBuffer[n] = ((float)FFT_HalfBufferPtr[n])/5000.0f;
    }
    arm_rfft_fast_f32(&fft_instance, FFT_InBuffer, FFT_OutBuffer, 0);
    arm_cmplx_mag_f32(FFT_OutBuffer, FFT_Mag,FFT_SIZE/2);
    FFT_Mag[0]=0;

    float Mag_Max = FFT_Mag[0];
    int32_t Mag_Max_İndex;
    for (int16_t i = 1; i < FFT_SIZE/2; i++){
        if (FFT_Mag[i] > Mag_Max){
            Mag_Max = FFT_Mag[i];
            Mag_Max_İndex = i;
        }
    }
    Peak_Frequency = Mag_Max_İndex * 42424.2f / FFT_SIZE;

    Mag_Max = FFT_Mag[170];
    for (int16_t i = 171; i < FFT_SIZE/2; i++){
        if (FFT_Mag[i] > Mag_Max){
            Mag_Max = FFT_Mag[i];
            Mag_Max_İndex = i;
        }
    }

    if(Mag_Max_İndex != FFT_SIZE/2 - 1 && Mag_Max > 0.4)    Peak_High_Frequency = Mag_Max_İndex * 42424.2f / FFT_SIZE;

    else Peak_High_Frequency = Peak_Frequency;


    Data_Ready = 0;


}
/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

I started this project with an analog mic. It was success. The ADC codes not necessary for I2S.

0

There are 0 answers