Skip to main content
Visitor II
October 7, 2024
Solved

SPI Master using HAL_SPI_TransmitReceive_DMA

  • October 7, 2024
  • 2 replies
  • 1328 views

Hi everyone,

I am a new user of ST microcontroller. I am not familiar with DMA. 

Currently I am working on to pull the data from the AFE as slave using DMA method using CUBE generate code:

HAL_StatusTypeDef HAL_SPI_TransmitReceive_DMA(SPI_HandleTypeDef *hspi, uint8_t *pTxData, uint8_t *pRxData,uint16_t Size).

I am able to get the correct value. 

My problem is when I tried to read the data continuously, I will get the error code. Is there anything that I need to care first before pulling another data?

example:  

state = SingleRegCommVerify(AFE_ConfigStart, &afe_read_buf[0], AFE_READ);//this will call HAL_SPI routine
if(state == COMM_DONE) //comm error
{
stat1 = afe_read_buf[0];
}//it will skip this 

state = SingleRegCommVerify(AFE_CalStart, &afe_read_buf[0], AFE_READ);
if(state == COMM_DONE)
{
stat2 = afe_read_buf[0];  //correct value
}

Thanks for the help.

-illa-

    This topic has been closed for replies.
    Best answer by TDK

    You can use HAL_SPI_GetState() to read the current state.

    // start operation
    HAL_SPI_TransmitReceive_DMA(&hspi1, ...);
    // wait for it to complete
    while (HAL_SPI_GetState(&hspi1) != HAL_SPI_STATE_READY) {
    }

     

    2 replies

    Super User
    October 7, 2024

    HAL_SPI_TransmitReceive_DMA is a non-blocking call, which means it works in the background and takes time to complete. If you call it again before it is completed, it will return HAL_BUSY and not work.

    Nabu .1Author
    Visitor II
    October 8, 2024

    So, It means I need to wait longer? How do I know the process is complete? 

    TDKAnswer
    Super User
    October 8, 2024

    You can use HAL_SPI_GetState() to read the current state.

    // start operation
    HAL_SPI_TransmitReceive_DMA(&hspi1, ...);
    // wait for it to complete
    while (HAL_SPI_GetState(&hspi1) != HAL_SPI_STATE_READY) {
    }