cancel
Showing results for 
Search instead for 
Did you mean: 

pdm2pcm, pcm data noisy , pdm mic nucleo f413zh

zohbou1
Associate

hello tout le monde, im worknig on audio project im trying to get data from a pdm microphone as pcm 8 bit and transmit the data via uart  as binary but when i transfer the data from binary to audio via audacity i can hear just noise nothing else, also i can hear when i stop the microphone ,im using pcm2pdm library my i2s clock frequency is 3mhz which still in the range of the microphone .uart baud rate is 115200 and over sampling is 16 , i will be grateful if someone can help with this issue thakns,

 

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2023 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 "crc.h"
#include "dma.h"
#include "i2s.h"
#include "pdm2pcm.h"
#include "usart.h"
#include "usb_device.h"
#include "gpio.h"
 
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
 
/* Private variables ---------------------------------------------------------*/
 
/* USER CODE BEGIN PV */
 
/* USER CODE END PV */
 
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
 
/* USER CODE BEGIN PFP */
 
/* USER CODE END PFP */
 
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
 
/* USER CODE END 0 */
 
/**
  * @brief  The application entry point.
  * @retval int
  */
uint16_t txBuf[128];
uint16_t pdmRxBuf[128];
uint16_t MidBuffer[16];
uint8_t txstate = 0;
uint8_t rxstate = 0;
 
 
uint16_t fifobuf[256];
uint8_t fifo_w_ptr = 0;
uint8_t fifo_r_ptr = 0;
uint8_t fifo_read_enabled = 0;
 
void FifoWrite(uint16_t data) {
fifobuf[fifo_w_ptr] = data;
fifo_w_ptr++;
}
 
uint16_t FifoRead() {
uint16_t val = fifobuf[fifo_r_ptr];
fifo_r_ptr++;
return val;
}
 
/* USER CODE END Includes */
 
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
 
/* USER CODE END PTD */
 
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
 
/* USER CODE END PD */
 
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
 
/* USER CODE END PM */
 
/* Private variables ---------------------------------------------------------*/
 
/* USER CODE BEGIN PV */
 
/* USER CODE END PV */
 
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
 
/* USER CODE END PFP */
 
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
 
/* 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_CRC_Init();
  MX_I2S1_Init();
  MX_UART5_Init();
  MX_USART3_UART_Init();
  MX_PDM2PCM_Init();
  MX_USB_DEVICE_Init();
  /* USER CODE BEGIN 2 */
  HAL_UART_Transmit_DMA(&huart5, (uint8_t *) txBuf, sizeof(&txbuf));
     HAL_I2S_Receive_DMA(&hi2s1, &pdmRxBuf[0],64);
 
 
 
  /* USER CODE END 2 */
 
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
 
     while (1)
     {
       if (rxstate==1) {
        PDM_Filter(&pdmRxBuf[0],&MidBuffer[0], &PDM1_filter_handler);
        for (int i=0; i<16;i++) { FifoWrite(MidBuffer[i]); }
        if (fifo_w_ptr-fifo_r_ptr > 128) fifo_read_enabled=1;
        rxstate=0;
 
       }
 
       if (rxstate==2) {
        PDM_Filter(&pdmRxBuf[64],&MidBuffer[0], &PDM1_filter_handler);
        for (int i=0; i<16;i++) { FifoWrite(MidBuffer[i]); }
        rxstate=0;
 
       }
 
       if (txstate==1) {
        if (fifo_read_enabled==1) {
    for (int i=0; i<64;i=i+4) {
    uint16_t data = FifoRead();
    txBuf[i] = data;
    txBuf[i+2] = data;
    }
        }
        txstate=0;
       }
 
       if (txstate==2) {
        if (fifo_read_enabled==1) {
    for (int i=64; i<128;i=i+4) {
    uint16_t data = FifoRead();
    txBuf[i] = data;
    txBuf[i+2] = data;
    }
 
    }
        txstate=0;
       }
     }
     /* USER CODE END 3 */
   }
 
   /**
     * @brief System Clock Configuration
     * @retval None
     */
 
 
 
 
     void HAL_UART_TxHalfCpltCallback(UART_HandleTypeDef *huart)
     {
      txstate = 1;
     }
 
     void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
     {
      txstate= 2;
     }
 
     void HAL_I2S_RxHalfCpltCallback (I2S_HandleTypeDef *hi2s) {
      rxstate = 1;
     }
 
     void HAL_I2S_RxCpltCallback (I2S_HandleTypeDef *hi2s)
     {
      rxstate = 2;
  }
  /* 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_HSI|RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS;
  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_HSE;
  RCC_OscInitStruct.PLL.PLLM = 4;
  RCC_OscInitStruct.PLL.PLLN = 72;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 3;
  RCC_OscInitStruct.PLL.PLLR = 2;
  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_DIV2;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
 
  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  {
    Error_Handler();
  }
  HAL_RCC_MCOConfig(RCC_MCO1, RCC_MCO1SOURCE_HSI, RCC_MCODIV_1);
}
 
/* USER CODE BEGIN 4 */
 
 
 
 
int _write(int file, char *ptr, int len)
{
  (void)file;
  int DataIdx;
 
  for (DataIdx = 0; DataIdx < len; DataIdx++)
  {
    ITM_SendChar(*ptr++);
  }
  return len;
}
 
/**
  * @brief  Regular conversion complete callback.
  * @note   In interrupt mode, user has to read conversion value in this function
            using HAL_DFSDM_FilterGetRegularValue.
  * @PAram  hdfsdm_filter : DFSDM filter handle.
  * @retval None
  */
 
 
/* 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 */

 

1 REPLY 1
niccolò
ST Employee

Hi @zohbou1 ,

can you confirm that you see data coming from the mic? also, is the timing of the acquisitions right?
if you can check with an oscilloscope, we can be sure that the problem is not on the timings =)

also, what board and mic are you using?
maybe ther is an example available for that board

Niccolò