cancel
Showing results for 
Search instead for 
Did you mean: 

SAI configuration with PDM interface

Diego93
Associate

Hi, I want to acquire an audio signal from the microphone IMP34DT05 which is connected to the SAI interface of my STM32WB5MM-DK demo board in this way: L/R -> VDD; CLK -> SAI_CK2; DATA -> SAI_D2.
With the oscilloscope I noticed that I generate correctly the clock signal and the microphone send out data to the SAI but the data are not acquired into the reciver buffer.
I tried different configuration in the SAI_Init() function but in any case it doesn't work.

This is the configuration i'm using:

hsai_BlockA1.Instance = SAI1_Block_A;

hsai_BlockA1.Init.AudioMode = SAI_MODEMASTER_RX;
hsai_BlockA1.Init.Synchro = SAI_ASYNCHRONOUS;
hsai_BlockA1.Init.SynchroExt = SAI_SYNCEXT_OUTBLOCKA_ENABLE;
hsai_BlockA1.Init.MckOutput = SAI_MCK_OUTPUT_DISABLE;
hsai_BlockA1.Init.OutputDrive = SAI_OUTPUTDRIVE_DISABLE;
hsai_BlockA1.Init.NoDivider = SAI_MASTERDIVIDER_DISABLE;
hsai_BlockA1.Init.FIFOThreshold = SAI_FIFOTHRESHOLD_EMPTY;
hsai_BlockA1.Init.AudioFrequency= SAI_AUDIO_FREQUENCY_8K;
hsai_BlockA1.Init.Mckdiv = 0;
hsai_BlockA1.Init.MckOverSampling = SAI_MCK_OVERSAMPLING_DISABLE;
hsai_BlockA1.Init.MonoStereoMode = SAI_MONOMODE;
hsai_BlockA1.Init.CompandingMode = SAI_NOCOMPANDING;
hsai_BlockA1.Init.TriState = SAI_OUTPUT_NOTRELEASED;
hsai_BlockA1.Init.PdmInit.MicPairsNbr = 1;
hsai_BlockA1.Init.PdmInit.ClockEnable = SAI_PDM_CLOCK2_ENABLE;
hsai_BlockA1.Init.PdmInit.Activation = ENABLE;
hsai_BlockA1.Init.Protocol = SAI_FREE_PROTOCOL;
hsai_BlockA1.Init.DataSize = SAI_DATASIZE_16;
hsai_BlockA1.Init.FirstBit = SAI_FIRSTBIT_MSB;
hsai_BlockA1.Init.ClockStrobing = SAI_CLOCKSTROBING_FALLINGEDGE;

hsai_BlockA1.FrameInit.FrameLength = 16;
hsai_BlockA1.FrameInit.ActiveFrameLength = 1;
hsai_BlockA1.FrameInit.FSDefinition = SAI_FS_STARTFRAME;
hsai_BlockA1.FrameInit.FSPolarity = SAI_FS_ACTIVE_HIGH;
hsai_BlockA1.FrameInit.FSOffset = SAI_FS_FIRSTBIT;

hsai_BlockA1.SlotInit.FirstBitOffset = 0;
hsai_BlockA1.SlotInit.SlotSize = SAI_SLOTSIZE_DATASIZE;
hsai_BlockA1.SlotInit.SlotNumber = 1;
hsai_BlockA1.SlotInit.SlotActive = 0x00000000;

What is the problem? 
What am I doing wrong?

Thanks for any assistance.

6 REPLIES 6
Federica Bossi
ST Employee

Hi @Diego93 ,

Welcome to ST Community!

Have a look at this example done with NUCLEO-WB55RG.

Otherwise you can look at this one that uses your board but there is a microphone and also the BLE.

If this helps you, please mark my answer as "Best Answer" by clicking on the "Accept as Solution" button, this can be helpful for Community users to find this solution faster.

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.

Thanks for replying, I finally solved that problem but unfortunately I'm having other problems.

On my board is mounted the IMP34DT05 microphone which requires a clock frequency between 1.2MHz and 3.25MHz to work. I can generate a 2 MHz clock signal from the SAI and the microphone outputs the data correctly which I can see with the oscilloscope.

The problem is that I need to generate a 2MHz clock signal in order to let the microhone work but, to do that, I violate the constraints written in the datasheet of the STM32 in the SAI paragraph.
I set the frame lenght = 256 bits but in the datasheet it is written that, using 1 or 2 microphones, the frame lenght must be 16 bits following the formula FRL = ( 16 * MICNBR + 1) - 1, where MICNBR is 0 in my case (1-2 microphones).

I also tried to implement the example that is written in the global manual (see the highlighted row of the table in attached image).
When I used 16 bits frame, 2 slots of 8 bits with sampling frequency 16 KHz and the clock signal to the SAI which minimize the sampling frequency error (= 0.05%), I expected to see on my oscilloscope SAI_CK = 1.024MHz but i see 128 KHz.

I really don't understand how to find the right balance between the needed frequency for the microphone and the constraints for the SAI.

In other words the problem is that I can't send the right clock signal to the microphone and have the right frame size simoultaneously.
I read the STM32 global manual for the SAI interface.
I read the IMP34DT05 datasheet for the microphone.
I read examples on the internet (ST Forum, ST examples and github examples).

Is there any other documentation?
Have you ever had the same problem?
The IMP34DT025 microphone is 8 bits or 16 bits frame?
How can I correctly set the paramter of the SAI interface and generate the needed 2 MHz clock signal?

I also noticed that in the STM32WB5MM-DK demo board datasheet is written that the micrphone is connected to SAI1_CK2 and SAI1_D2 pins but in the CubeMX when I set the SAI into PDM mode using CK2 with 1-2 microphones, it sets the data pin as SAI1_D1. Is it an error? When I select 3-4 microphone it set both D1 and D2 as data pins. I really don't understand, how can I set the right SAI parameters for this microphone?

azaan70
Associate

Hi Diego,
I am facing the exact same issue as you where I am expecting 1.024MHz and I also see 128Khz. Did you find a workaround for this?
I followed the exact same steps in the user manual but to no avail. Would appreciate any hints to go in the right direction!
Thank You!

Federica Bossi
ST Employee

Hi @Diego93 , @azaan70 ,

Are you using the official libraries on Github?

In addition, which is your clock frequency?

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.
azaan70
Associate

@Diego93 
How did you solve your initial issue. Any help would be appreciated 🙂

sonic1
Associate

This is a bug of CubeMx code generation, I figured it out from the example Federica Bossi provided. 
The value generated below can't be used. 

hsai_BlockA1.Init.AudioFrequency= SAI_AUDIO_FREQUENCY_8K;

Use the equation in NUCLEO-WB55RG example to change it to right value.

For example, if the desired mic sampling freq is 48kHz, the value here should be 384000U.