2023-11-09 05:32 AM
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.
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.
Solved! Go to Solution.
2023-11-13 07:43 AM
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)
2023-11-14 01:30 AM - edited 2023-11-14 01:32 AM
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.
2023-11-14 05:02 AM
You got it completely wrong...
You are declaring a function within the while loop - and the compiler does not complain? Interesting.
2023-11-14 05:23 AM
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?
2023-11-14 05:26 AM
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.
2023-11-14 06:42 AM
Hi,
I'm not sure if you're right, no errors during compilation :
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.
2023-11-14 06:55 AM - edited 2023-11-14 06:57 AM
I think this function is useful to know if the transmission is going well.
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 ?
2023-11-14 06:59 AM
@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.
2023-11-14 07:26 AM - edited 2023-11-14 07:28 AM
Now it's ok
I just to change the configuration and I configure the SAI in DMA only for transmission but not for reception.
2023-11-14 08:42 AM
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.