cancel
Showing results for 
Search instead for 
Did you mean: 

CubeMX Initialization for SAI with MEMS microphone input - STM32F746g-disco

ALiss
Associate II

Hello,

I'm trying to get the MEMS microphone (components U21 and U20 on the STM32F746g-disco) to work with SAI. I save my microphone inputs into a buffer with 1000 8-bit values via HAL_SAI_Receive_DMA. Below I have written my MX_SAI_Init() and HAL_SAI_MspInit(). I realise that the slots in the SAI audio frame are important, and from what I have learned I have initialized 4 slots with 2 active, 1 and 3.

As of now, the input buffer fills every adress with the value 255. I have been looking around the forum for quite some time and gotten a bit further, but it would be very helpful if someone with more experience with SAI could give me feedback on my initialization. Is this initialization correct in order to get input working with the MEMS microphones?

Initialization code

static void MX_SAI1_Init(void)
{
 
  hsai_BlockA1.Instance = SAI1_Block_A;
  hsai_BlockA1.Init.Protocol = SAI_FREE_PROTOCOL;
  hsai_BlockA1.Init.AudioMode = SAI_MODEMASTER_RX;
  hsai_BlockA1.Init.DataSize = SAI_DATASIZE_8;
  hsai_BlockA1.Init.FirstBit = SAI_FIRSTBIT_MSB;
  hsai_BlockA1.Init.ClockStrobing = SAI_CLOCKSTROBING_FALLINGEDGE;
  hsai_BlockA1.Init.Synchro = SAI_ASYNCHRONOUS;
  hsai_BlockA1.Init.OutputDrive = SAI_OUTPUTDRIVE_DISABLE;
  hsai_BlockA1.Init.NoDivider = SAI_MASTERDIVIDER_ENABLE;
  hsai_BlockA1.Init.FIFOThreshold = SAI_FIFOTHRESHOLD_FULL;
  hsai_BlockA1.Init.AudioFrequency = SAI_AUDIO_FREQUENCY_44K;
  hsai_BlockA1.Init.SynchroExt = SAI_SYNCEXT_DISABLE;
  hsai_BlockA1.Init.MonoStereoMode = SAI_STEREOMODE;
  hsai_BlockA1.Init.CompandingMode = SAI_NOCOMPANDING;
  hsai_BlockA1.FrameInit.FrameLength = 32;
  hsai_BlockA1.FrameInit.ActiveFrameLength = 1;
  hsai_BlockA1.FrameInit.FSDefinition = SAI_FS_CHANNEL_IDENTIFICATION;
  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 = 4;
  hsai_BlockA1.SlotInit.SlotActive = SAI_SLOTACTIVE_1 | SAI_SLOTACTIVE_3;
  if (HAL_SAI_Init(&hsai_BlockA1) != HAL_OK)
  {
    Error_Handler();
  }
}

void HAL_SAI_MspInit(SAI_HandleTypeDef* hsai)
{
 
  GPIO_InitTypeDef GPIO_InitStruct;
/* SAI1 */
    if(hsai->Instance==SAI1_Block_A)
    {
    /* Peripheral clock enable */
    if (SAI1_client == 0)
    {
       __HAL_RCC_SAI1_CLK_ENABLE();
    }
    SAI1_client ++;
    
    /**SAI1_A_Block_A GPIO Configuration    
    PE4     ------> SAI1_FS_A
    PE5     ------> SAI1_SCK_A
    PE6     ------> SAI1_SD_A 
    */
    GPIO_InitStruct.Pin = GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
    GPIO_InitStruct.Alternate = GPIO_AF6_SAI1;
    HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
 
    /* Peripheral DMA init*/
    
    hdma_sai1_a.Instance = DMA2_Stream1;
    hdma_sai1_a.Init.Channel = DMA_CHANNEL_0;
    hdma_sai1_a.Init.Direction = DMA_PERIPH_TO_MEMORY;
    hdma_sai1_a.Init.PeriphInc = DMA_PINC_DISABLE;
    hdma_sai1_a.Init.MemInc = DMA_MINC_ENABLE;
    hdma_sai1_a.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
    hdma_sai1_a.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
    hdma_sai1_a.Init.Mode = DMA_CIRCULAR;
    hdma_sai1_a.Init.Priority = DMA_PRIORITY_HIGH;
    hdma_sai1_a.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
    if (HAL_DMA_Init(&hdma_sai1_a) != HAL_OK)
    {
      Error_Handler();
    }
 
    /* Several peripheral DMA handle pointers point to the same DMA handle.
     Be aware that there is only one stream to perform all the requested DMAs. */
    __HAL_LINKDMA(hsai,hdmarx,hdma_sai1_a);
// Comment one of the links in order to make HAL_SAI_DMAStop() working properly.
//    __HAL_LINKDMA(hsai,hdmatx,hdma_sai1_a);
 
    }
}

Thank you,

1 ACCEPTED SOLUTION

Accepted Solutions
ALiss
Associate II

Hey everyone,

I got the microphones working by implementing the BSP (Board Support Package) in order to get the codecs, components and everything else related to the STM32F746G-disco peripheries working.

The BSP components are located in .../STM32CubeIDE/Repositories/STM32Cube_FW_F7_V1.15.0/Drivers/BSP and in order to get them compiling you have to import the drivers into your project location Drivers/BSP/STM32746G-Discovery/ and the component file as well in Drivers/BSP/Components/ into the /Drivers/ file. Finally, you need to import the Utilities file (/STM32CubeIDE/Repositories/STM32Cube_FW_F7_V1.15.0/Utilities/) of the firmware and add the file to the /Utilities/ project file. To get the compiler working, include the locations in the Include Paths in project properties.

If you have built your project in CubeMX, you need to to activate some peripheries in order to get the BSP working, such as TIM, UART and DCMI. I just activated them in the Pinout view of CubeMX There might be more peripherals to add as well if you want to use all the BSP library, but since I only needed the Audio I deleted some of the other files in the BSP folder which caused compilation error.

Hope this helps someone else who has been struggling with getting the microphones working.

TL:DR - Easiest way to get MEMS microphones working: use BSP library.

View solution in original post

2 REPLIES 2
ALiss
Associate II

Hey everyone,

I got the microphones working by implementing the BSP (Board Support Package) in order to get the codecs, components and everything else related to the STM32F746G-disco peripheries working.

The BSP components are located in .../STM32CubeIDE/Repositories/STM32Cube_FW_F7_V1.15.0/Drivers/BSP and in order to get them compiling you have to import the drivers into your project location Drivers/BSP/STM32746G-Discovery/ and the component file as well in Drivers/BSP/Components/ into the /Drivers/ file. Finally, you need to import the Utilities file (/STM32CubeIDE/Repositories/STM32Cube_FW_F7_V1.15.0/Utilities/) of the firmware and add the file to the /Utilities/ project file. To get the compiler working, include the locations in the Include Paths in project properties.

If you have built your project in CubeMX, you need to to activate some peripheries in order to get the BSP working, such as TIM, UART and DCMI. I just activated them in the Pinout view of CubeMX There might be more peripherals to add as well if you want to use all the BSP library, but since I only needed the Audio I deleted some of the other files in the BSP folder which caused compilation error.

Hope this helps someone else who has been struggling with getting the microphones working.

TL:DR - Easiest way to get MEMS microphones working: use BSP library.

Hi @ALiss​ , thank you for coming back to us with the solution! Regards