Skip to main content
Pmica.1
Associate II
May 13, 2021
Solved

STM32H743: DMA error for SAI interface

  • May 13, 2021
  • 1 reply
  • 1182 views

Hi all,

I'm currently working on a STM32H743. The idea is to use a SAI peripherals to read an Audio stream:

  • The SAI is set to be a Master receive with a circular buffer and a DMA

The issue is that as soon as the HAL_SAI_Receive_DMA is called, then the code enters into the error callback HAL_SAI_ErrorCallback with an error of type HAL_SAI_ERROR_DMA, HAL_DMA_ERROR_TE.

The SAI clock is set at 4 MHz

I tried the HAL_SAI_Receive without DMA and all works fine.

So for sure i'm doing something wrong with the DMA configuration .

static uint8_t buf_rx[2] = { 0, 0 };
 
int main(void)
{
 HAL_Init();
 
 SystemClock_Config();
 
 PeriphCommonClock_Config();
 
 MX_GPIO_Init();
 MX_DMA_Init();
 MX_SAI2_Init();
 
 HAL_SAI_Receive_DMA(&hsai_BlockA2, buf_rx, 2);
 
 while (1)
 {
 HAL_Delay(1);
 }
}
static void MX_SAI2_Init(void)
{
 /* USER CODE BEGIN SAI2_Init 0 */
 
 /* USER CODE END SAI2_Init 0 */
 
 /* USER CODE BEGIN SAI2_Init 1 */
 
 /* USER CODE END SAI2_Init 1 */
 hsai_BlockA2.Instance = SAI2_Block_A;
 hsai_BlockA2.Init.Protocol = SAI_FREE_PROTOCOL;
 hsai_BlockA2.Init.AudioMode = SAI_MODEMASTER_RX;
 hsai_BlockA2.Init.DataSize = SAI_DATASIZE_8;
 hsai_BlockA2.Init.FirstBit = SAI_FIRSTBIT_MSB;
 hsai_BlockA2.Init.ClockStrobing = SAI_CLOCKSTROBING_FALLINGEDGE;
 hsai_BlockA2.Init.Synchro = SAI_ASYNCHRONOUS;
 hsai_BlockA2.Init.OutputDrive = SAI_OUTPUTDRIVE_DISABLE;
 hsai_BlockA2.Init.NoDivider = SAI_MASTERDIVIDER_ENABLE;
 hsai_BlockA2.Init.FIFOThreshold = SAI_FIFOTHRESHOLD_FULL;
 hsai_BlockA2.Init.AudioFrequency = SAI_AUDIO_FREQUENCY_48K;
 hsai_BlockA2.Init.SynchroExt = SAI_SYNCEXT_DISABLE;
 hsai_BlockA2.Init.MonoStereoMode = SAI_MONOMODE;
 hsai_BlockA2.Init.CompandingMode = SAI_NOCOMPANDING;
 hsai_BlockA2.Init.PdmInit.Activation = DISABLE;
 hsai_BlockA2.Init.PdmInit.MicPairsNbr = 1;
 hsai_BlockA2.Init.PdmInit.ClockEnable = SAI_PDM_CLOCK1_ENABLE;
 hsai_BlockA2.FrameInit.FrameLength = 16;
 hsai_BlockA2.FrameInit.ActiveFrameLength = 1;
 hsai_BlockA2.FrameInit.FSDefinition = SAI_FS_STARTFRAME;
 hsai_BlockA2.FrameInit.FSPolarity = SAI_FS_ACTIVE_LOW;
 hsai_BlockA2.FrameInit.FSOffset = SAI_FS_FIRSTBIT;
 hsai_BlockA2.SlotInit.FirstBitOffset = 0;
 hsai_BlockA2.SlotInit.SlotSize = SAI_SLOTSIZE_DATASIZE;
 hsai_BlockA2.SlotInit.SlotNumber = 2;
 hsai_BlockA2.SlotInit.SlotActive = 0x00000003;
 if (HAL_SAI_Init(&hsai_BlockA2) != HAL_OK)
 {
 Error_Handler();
 }
 /* USER CODE BEGIN SAI2_Init 2 */
 
 /* USER CODE END SAI2_Init 2 */
}
 
/**
 * Enable DMA controller clock
 */
static void MX_DMA_Init(void)
{
 /* DMA controller clock enable */
 __HAL_RCC_DMA1_CLK_ENABLE();
 
 /* DMA interrupt init */
 /* DMA1_Stream0_IRQn interrupt configuration */
 HAL_NVIC_SetPriority(DMA1_Stream0_IRQn, 0, 0);
 HAL_NVIC_EnableIRQ(DMA1_Stream0_IRQn);
}
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);
 
 }
/* SAI2 */
 if(hsai->Instance==SAI2_Block_A)
 {
 /* Peripheral clock enable */
 if (SAI2_client == 0)
 {
 __HAL_RCC_SAI2_CLK_ENABLE();
 }
 SAI2_client ++;
 
 /**SAI2_A_Block_A GPIO Configuration
 PD11 ------> SAI2_SD_A
 PD12 ------> SAI2_FS_A
 PD13 ------> SAI2_SCK_A
 */
 GPIO_InitStruct.Pin = GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_13;
 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
 GPIO_InitStruct.Pull = GPIO_NOPULL;
 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
 GPIO_InitStruct.Alternate = GPIO_AF10_SAI2;
 HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
 
 /* Peripheral DMA init*/
 
 hdma_sai2_a.Instance = DMA1_Stream0;
 hdma_sai2_a.Init.Request = DMA_REQUEST_SAI2_A;
 hdma_sai2_a.Init.Direction = DMA_PERIPH_TO_MEMORY;
 hdma_sai2_a.Init.PeriphInc = DMA_PINC_DISABLE;
 hdma_sai2_a.Init.MemInc = DMA_MINC_ENABLE;
 hdma_sai2_a.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
 hdma_sai2_a.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
 hdma_sai2_a.Init.Mode = DMA_CIRCULAR;
 hdma_sai2_a.Init.Priority = DMA_PRIORITY_HIGH;
 hdma_sai2_a.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
 if (HAL_DMA_Init(&hdma_sai2_a) != HAL_OK)
 {
 Error_Handler();
 }
 
 /* Several peripheral DMA handle pointers point to the same DMA handle.
 Be aware that there is only one channel to perform all the requested DMAs. */
 __HAL_LINKDMA(hsai,hdmarx,hdma_sai2_a);
 
 __HAL_LINKDMA(hsai,hdmatx,hdma_sai2_a);
 
 }
}

This topic has been closed for replies.

1 reply

waclawek.jan
waclawek.janBest answer
Super User
May 20, 2021