Showing results for 
Search instead for 
Did you mean: 

PDM to PCM via DFSDM getting nothing but static

Associate II

On the BL47E-IOT01A2 there are two mems microphones (MP34DT01) both connected to DFSDM1_DATIN2. I'm trying to extract the data from just one of those boards and convert it to PCM using a DFSDM filter. I've configured DFSDM Channel 2 to connect to PDM/SPI and trigger on the rising edge, and filter 0 I've configured in one-shot mode, connected to DMA, with an oversampling value of 154, and an integral oversampling value of 3. The internal SPI clock on the board is configured to 1/4 the system clock which is 80MHz, thus the SPI clock should be 20MHz. The code to capture the audio is here:

void HAL_DFSDM_FilterRegConvCpltCallback(DFSDM_Filter_HandleTypeDef *hdfsdm_filter) {
	micAudioBuf[micAudioBufIdx] = micRecSample << 8;
	if (micAudioBufIdx >= AUDIO_LEN) {
		micAudioBufIdx = 0;
		audioFilterCplt = TRUE;
	} else {
void micReset(DFSDM_Filter_HandleTypeDef *hdfsdm_filter) {
	HAL_DFSDM_FilterRegularStart_DMA(hdfsdm_filter, &micRecSample, 1);

And the transmission code is in the main loop here:

  while (1) {
    // toggle LED2 to indicate activity
    HAL_GPIO_TogglePin(LED2_GPIO_Port, LED2_Pin);
    // if programming the transmitter
    if (audioFilterCplt == TRUE) {
      // reset the flag
      audioFilterCplt = FALSE;
      CDC_Transmit_FS((uint8_t*)micAudioBuf, AUDIO_LEN);
      // Reset the mic and start recording again

I'm able to get the data by connecting to the serial port in python, but the output is just static. Here is the code I'm using to capture the output:

import serial
import wave
import struct
import numpy as np
if __name__ == '__main__':
    # Replace '/dev/ttyACM0' with the appropriate device path for your system
    ser = serial.Serial('/dev/ttyACM1', timeout=1)
    # Configure the WAV file settings
    num_channels = 1
    sample_width = 2  # 16-bit audio
    frame_rate = 44100
    num_frames = 0  # Will be updated as data is received
    compression_type = 'NONE'
    compression_name = 'not compressed'
    # Create a new WAV file
    wav_file ='output.wav', 'wb')
    wav_file.setparams((num_channels, sample_width, frame_rate, num_frames, compression_type, compression_name))
        while True:
            # Read data from the serial port
            data =  # Read 2 bytes at a time (16-bit samples)
            if data:
                # Unpack the received data as a signed 16-bit integer
                sample = struct.unpack('<h', data)[0]
                # Write the sample to the WAV file
                wav_file.writeframesraw(struct.pack('<h', sample))
        # Close the serial port and WAV file

I'm not sure what else to try at this point

Federica Bossi
ST Employee

Hi @RBair.3​ ,

Have you set a CLK > 2MHz?

In addition I suggest you to use the channel 1 of DFSDM (DFSDM1_CKOUT).

If not, please try to do it and let me know if you solve the problem 🙂

In order to give better visibility on the answered topics, please click on 'Accept as Solution' on the reply which solved your issue or answered your question.