Issues with PDM mono microphone with I2S
Dear all,
I have some trouble setting up the correct acquisition of the MEMS microphone over I2S.
Consider the STM32F401RE Nucleo board connected to the IMP34DT05 PDM microphone with L/R to GND.
Through Mx, the I2S peripheral is configured as follows:
Then, the DMA is configured as a circular buffer, half-word:
Finally, I have configured the PDM2PCM library as follows:
Starting from that configuration I'm having trouble understanding why the PDM2PCM is not working as expected.
Here is the skeleton of the code:
uint16_t pdmRxBuf[128];
uint16_t MidBuffer[16];
uint16_t pcm_data[4096];
uint8_t txstate = 0;
uint8_t rxstate = 0;
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_I2S2_Init();
MX_I2S3_Init();
MX_CRC_Init();
MX_PDM2PCM_Init();
/* USER CODE BEGIN 2 */
pcm_index = 0;
/* USER CODE END 2 */
HAL_I2S_Receive_DMA(&hi2s2, &pdmRxBuf[0],128);
/* Infinite loop */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
if (rxstate==1) {
PDM_Filter(&pdmRxBuf[0],&MidBuffer[0], &PDM1_filter_handler);
for(uint8_t i = 0; i < 16 ; i++){
pcm_data[pcm_index] = mid_buffer[i];
pcm_index++;
}
rxstate=0;
}
if (rxstate==2) {
PDM_Filter(&pdmRxBuf[64],&MidBuffer[0], &PDM1_filter_handler);
for(uint8_t i = 0; i < 16 ; i++){
pcm_data[pcm_index] = mid_buffer[i];
pcm_index++;
}
rxstate=0;
}
if(pcm_index==4096){
//Stop the DMA
HAL_I2S_DMAPause(&hi2s2);
// Send to UART or Store Data
pcm_index = 0;
}
/* USER CODE END 3 */
}
void HAL_I2S_RxHalfCpltCallback(I2S_HandleTypeDef *hi2s){
rx_state = 1;
}
void HAL_I2S_RxCpltCallback(I2S_HandleTypeDef *hi2s){
rx_state = 2;
}The program is very stupid, tries to get 4096 samples into the pcm_data to be saved or sent through the UART.
Having configured the DMA as a circular buffer, I would expect that at every interrupt Half of the buffer is filled with new data, while the other half is used to run the PDM_Filter function and complete the PCM conversion and copy the data out to the pcm_data.
However, before starting the execution I emit from the PC a sine wave at a specific frequency that should be recorded by the 4096 samples acquired by the microphone.
Unfortunately, when I reproduce the 4096 samples, there is only noise.
Did I misunderstand some configurations?
Thanks for your help
