cancel
Showing results for 
Search instead for 
Did you mean: 

SPH0645LM4H-B interface with STM32L476RG on SAI

ajay.sharma
Associate II

Hi,

I am trying to interface SPH0645LM4H-B mic sensor with STM32L476RG on SAI.

I have configured the SAI2 block A with following configuration.

Master clock :- 2.826 Mhz

Sampling rate :- 44KHZ

//////////////////// PLL configuration /////////////////////

RCC_ExCLKInitStruct.PeriphClockSelection = RCC_PERIPHCLK_SAI2;

RCC_ExCLKInitStruct.Sai2ClockSelection = RCC_SAI2CLKSOURCE_PLLSAI2;

RCC_ExCLKInitStruct.PLLSAI2.PLLSAI2Source = RCC_PLLSOURCE_MSI;

 RCC_ExCLKInitStruct.PeriphClockSelection    = RCC_PERIPHCLK_SAI2;

 //RCC_ExCLKInitStruct.PLLSAI2.PLLSAI2M     = 0; 

 RCC_ExCLKInitStruct.PLLSAI2.PLLSAI2N     = 24;

 RCC_ExCLKInitStruct.PLLSAI2.PLLSAI2P     = 17;

 RCC_ExCLKInitStruct.PLLSAI2.PLLSAI2ClockOut = RCC_PLLSAI2_SAI2CLK;

 RCC_ExCLKInitStruct.Sai2ClockSelection    = RCC_SAI2CLKSOURCE_PLLSAI2;

//////////////////////////////////////////////////////////////////////

////////////////// SAI CONFIGURATION ////////////////////////

BSP_AUDIO_hSai.Instance = AUDIO_SAIx; /*this is SAI2 block A*/

  BSP_AUDIO_hSai.hdmarx = &hDmaSai;

  /* Configure SAI_Block_x 

LSBFirst: Disabled 

DataSize: 24 */

  BSP_AUDIO_hSai.Init.AudioMode    = SAI_MODEMASTER_RX;

  BSP_AUDIO_hSai.Init.Synchro     = SAI_ASYNCHRONOUS;

  BSP_AUDIO_hSai.Init.SynchroExt    = SAI_SYNCEXT_DISABLE;

  BSP_AUDIO_hSai.Init.OutputDrive   = SAI_OUTPUTDRIVE_DISABLE;

  BSP_AUDIO_hSai.Init.NoDivider    = SAI_MASTERDIVIDER_ENABLE;

  BSP_AUDIO_hSai.Init.FIFOThreshold  = SAI_FIFOTHRESHOLD_HF;

  BSP_AUDIO_hSai.Init.AudioFrequency = AudioFreq;

  BSP_AUDIO_hSai.Init.Mckdiv      = SAIClockDivider(AudioFreq);

  //BSP_AUDIO_hSai.Init.MonoStereoMode = SAI_MONOMODE;

  BSP_AUDIO_hSai.Init.MonoStereoMode = SAI_MONOMODE;

  BSP_AUDIO_hSai.Init.CompandingMode = SAI_NOCOMPANDING;

  BSP_AUDIO_hSai.Init.TriState     = SAI_OUTPUT_NOTRELEASED;

  

if (HAL_SAI_InitProtocol(&BSP_AUDIO_hSai, SAI_I2S_STANDARD, SAI_PROTOCOL_DATASIZE_24BIT, 2) != HAL_OK)

  {

    return AUDIO_ERROR;

  }

  /* Enable SAI peripheral to generate MCLK */

  __HAL_SAI_ENABLE(&BSP_AUDIO_hSai);

////////////////////////////////////////////////////////////////////////////////////////

I am reading data directly from the sai data register after reading status register.

  if ((sai->SR)&0x1) {

    sai->CLRFR |= 1; // Clear Overdue

  }

  Timer t;

  int start = millis();

  while (i < len) {

    if (millis() - start > timeout) return -1;

    int printed = 0;

    while ((((sai->SR)>>16) & 7) < 4) {

      if (!printed) {

        while(j < i) {

          pc.printf("%d\n", buf[j++]);

        }

        printed = 1;

      }

      i = j = 0;

    }

    uint32_t data = sai->DR;

    buf[i++] = (data << 8) >> 14;

  }

The issue i am getting here is that the data i am getting has variation in only 8 bits like the following:

data is in hexadecimal

255360

255365

255364

255355

255350

255349

255342

255347

255346

255344

255341

255346

255356

255348

255348

255346

255343

255340

255361

255355

I have checked the clocks with CRO and i am getting 44khz clock at data pin and 2.8Mhz clock at master clock but unable to get any clock at WS pin.

Not sure if i am doing something wrong over here.

Although when i try to plot the data received on Arduino serial plotter, there is change in waveform when i blow into the mic but when i tried to create a file from the data and play it (on audacity) i get nothing but noises.

A little help would be appreciable, I am blocked at this.

Thanks

4 REPLIES 4

> unable to get any clock at WS pin

Read out and check/post the content of relevant GPIO and SAI registers.

JW

ajay.sharma
Associate II

Hi,

Following are the SAI registers values:

CR1 0x110c1

CR2 0x2

FRCR 0x51f3f

SLOTR 0xffff0180

IMR 0x0

CLRFR 0x0

ajay.sharma
Associate II

Hi,

I confirmed today with the hardware team FS is good at 44kHz and master clock is at 2.8 kHz . But still not getting the right data. Is there something wrong with the way I am reading data?

Those settings look good (although I don't have experience with MONO). I don't understand the reason for (data << 8) >> 14; and I would be wary of the printf too.

Could you simply store a piece of data - say some 16 words - without any processing, as read from SAI->DR, into the RAM in a very simple loop, and after that read them out using debugger or printf?

Also, could you read the waveforms using a LA and post them?

JW