cancel
Showing results for 
Search instead for 
Did you mean: 

DMA with SAI

DYann.1
Senior

Hello,

I would like to use DMA with SAI but I am having difficulty writing the code. Can you check if my code is correct and why I cannot retrieve the RX values ?

 

  /* USER CODE BEGIN 2 */

  HAL_SAI_RegisterCallback(&hsai_BlockA1, HAL_SAI_RX_HALFCOMPLETE_CB_ID, HAL_SAI_RxHalfCpltCallback);
  HAL_SAI_RegisterCallback(&hsai_BlockA1, HAL_SAI_RX_COMPLETE_CB_ID, HAL_SAI_RxCpltCallback);
  fresult= HAL_SAI_Init(&hsai_BlockA1);
  if (fresult != HAL_OK)
  	{
  	return HAL_ERROR;
  	}
  fresult = HAL_SAI_Receive_DMA(&hsai_BlockA1, (uint8_t *)playbuf_RX, (sizeof(playbuf_RX))/4);

  HAL_SAI_RegisterCallback(&hsai_BlockB1, HAL_SAI_TX_HALFCOMPLETE_CB_ID, HAL_SAI_TxHalfCpltCallback);
  HAL_SAI_RegisterCallback(&hsai_BlockB1, HAL_SAI_TX_COMPLETE_CB_ID, HAL_SAI_TxCpltCallback);
  fresult= HAL_SAI_Init(&hsai_BlockB1);
  if (fresult != HAL_OK)
	{
  	return HAL_ERROR;
	}

  fresult = HAL_SAI_Transmit_DMA(&hsai_BlockB1, (uint8_t *)playbuf , (sizeof(playbuf))/4);

 

With CubeMX I have configured 2 DMAs.

DYann1_0-1699536737285.png

One to send the data and the other to read the data again.
I think I'm missing something for the callback but I don't know how to use it to retrieve the values.

Thank you for your helps. 

Regards.

30 REPLIES 30
LCE
Lead III

Look at the examples, step by step:

1) What are they doing there in general?

2) How's RX set up and started?

3) How's TX set up, then started?

4) What's happening in the callbacks / ISRs? (which they hopefully use, meaning that DMA with interrupts is used)

Hi,

Thank you for your helps, I'll look for the documentation for that. I modified my code to know if the transmission is correct via the HAL_SAI_TxHalfCpltCallback function :

 

  while (1)
  {
    /* USER CODE END WHILE */
	void HAL_SAI_TxHalfCpltCallback (SAI_HandleTypeDef *hsai)
	  {HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_3);
	   HAL_Delay(200);}

    /* USER CODE BEGIN 3 */
  }

 

So for me something is wrong with the transmission since the LED never flashes.

LCE
Lead III

You got it completely wrong...
You are declaring a function within the while loop - and the compiler does not complain? Interesting.

LCE
Lead III

And again, usually callback functions are not for you to call them, it's another function that does that, in the HAL case it should be called by the DMA / SAI complete ISR. Try to find where the callback is called within the HAL functions.

Maybe the interrupts you need are not active?

LCE
Lead III

Maybe you should forget about audio for some time and work on the basics.

Make an LED blink.
Then turn it on / off if some GPIO input is high or low.
Then use that GPIO with an interrupt, and so on.

Start simple, and understand that first.

Hi,

I'm not sure if you're right, no errors during compilation :

DYann1_0-1699972697337.png

I saw a tutorial and you can do this to find out if the transmission is going well. The proof is that I have no errors during compilation.

I think this function is useful to know if the transmission is going well. 

DYann1_1-1699973137662.png

Why am I looking at this function ? Because the DMA is configured in circular. After I can check for RX.

Normally the interrupts is automatically generated by cubemx right ? How to check this interruption ?


@LCE wrote:

Maybe you should forget about audio for some time and work on the basics.

Make an LED blink.
Then turn it on / off if some GPIO input is high or low.
Then use that GPIO with an interrupt, and so on.

Start simple, and understand that first.


I'm a beginner but I've already done all this.

DYann.1
Senior

Now it's ok

DYann1_2-1699975477522.png

DYann1_3-1699975686560.png

I just to change the configuration and I configure the SAI in DMA only for transmission but not for reception.

LCE
Lead III

Congratulations!

But how did you configure the SAI in RX?

You are mostly showing not so useful pictures, easier to read would be parts of the code with the (initially hidden) </> code insert button.
So it would be interesting to see the SAI inits, and the part where you start TX / RX.

But good that it's working now.