Skip to main content
Visitor II
September 12, 2024
Question

SPI DMA with STM32U5

  • September 12, 2024
  • 4 replies
  • 2868 views

Hi everyone,

I want to connect 2 stm32 via SPI DMA mode. My wish is that the master first sends 512 bytes of command to the slave, then the slave processes this data to respond to the master. On the master side, the delay between sending 512 command bytes and sending 512 dummy bytes is 10 (ms). As for the slave side, the delay time between receiving 512 command bytes and 512 dummy bytes from the master is 5 (ms), but currently the system is not working as I want when the slave cannot respond data to the master the second time it receives a clock pulse from the master. 

P/s: on the slave side I use SPI DMA mode and on the master side I use SPI Polling. I tried to change the delay time between transmission and reception on both the master and slave sides but all failed. I think the problem is that I call the HAL_SPI_TransmitReceive_DMA function twice within the while loop, do I need to disable DMA after the first receive and re-enable DMA to call the HAL_SPI_TransmitReceive_DMA function again the second time? I have attached the test code in the main function of both slave and master below:

Master:

WhatsApp Image 2024-09-12 at 16.00.19.jpeg

Slave:

WhatsApp Image 2024-09-12 at 15.59.03.jpeg

    This topic has been closed for replies.

    4 replies

    Technical Moderator
    September 18, 2024

    Hello @tuna and welcome to the community ,

     

    Could you please precise which STM32U5 and SPI pins are you using? 

    Are you use a STM32U5 boards?

     

    Could you please check these examples may help you:

     

    I hope this help you.

    Thank you.

    Kaouthar

    Visitor II
    October 4, 2024

    Hi Kaouthar,

    Thanks for your reply.

    This is my 2nd ST account. I use SPI1 port for both STM32U585 (Master - SPI Polling mode) and STM32U5G9 (Slave - SPI DMA) boards.

    Pin config:

    WhatsApp Image 2024-10-04 at 14.47.04.jpeg

    Flow chart:

    WhatsApp Image 2024-10-04 at 17.09.37.jpeg

    The flow chart is shown above, but currently the master cannot receive data from the slave in response (2). Do I need to disable - enable DMA every time I call the HAL_SPI_TransmitReceive_DMA function?

    Technical Moderator
    October 4, 2024

    Hello @vDuong1302 ,

     

    Thank you for updating post and come back to the community.

    > currently the master cannot receive data from the slave in response (2)

    What error did you receive: error handler, Flag?

    Could you please share your project to check the issue?

    To call another time the function HAL_SPI_TransmitReceive_DMA, you have to wait the complete process completion.

     

    Thank you.

    Kaouthar

     

    Technical Moderator
    October 7, 2024

    Hello @vDuong1302 ,

     

    >I think the delay time between two calls to this function is 5 ms which is enough to finish a transmit - receive process.

    To call another time the function HAL_SPI_TransmitReceive_DMA, you have to wait the complete process completion. To do this, you have to implement a wait loop like this:

     

     

    stSPI = HAL_SPI_TransmitReceive_DMA(&hspi1, Dat_send, aRxBuffer, BUFFER_SIZE);
    if (stSPI == HAL_OK)
    {
    while (hSPI1.State == HAL_SPI_STATE_BUSY_TX_RX); /* wait end of transfer */
    st2SPI = HAL_SPI_TransmitReceive_DMA(&hspi1, Dat_send1, Dat_receive, BUFFER_SIZE);
    while (hSPI1.State == HAL_SPI_STATE_BUSY_TX_RX); /* wait end of transfer */
    }
    

     

     

    Also, please take a look at  'HAL_SPI_TransmitReceive_DMA()' issue reported in Why check only the 'hspi->hdmarx' in the 'HAL_SPI_TransmitReceive_DMA()' for STM32U5?

    Thank you.

    Kaouthar

    Visitor II
    October 8, 2024

    Hi Kaouthar,

    On the master side if I do as below then no data will be received in dat_rec_second buffer:

    WhatsApp Image 2024-10-08 at 11.05.01.jpeg


    But if I do as this then data will be received in dat_rec_first buffer and not in dat_rec_second buffer.

    WhatsApp Image 2024-10-08 at 11.12.41.jpeg

    Is there any problem in the way I implement on the master side?

    Thank you,

    Duong