cancel
Showing results for 
Search instead for 
Did you mean: 

SPI DMA with STM32U5

tuna
Associate

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

6 REPLIES 6
KDJEM.1
ST Employee

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

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

vDuong1302
Associate II

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?

KDJEM.1
ST Employee

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

 

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

Hi Kaouthar,

When I debug on the slave side (STM32U5G9) the return status of the second call to HAL_SPI_TransmitReceive_DMA is HAL_BUSY. I think the delay time between two calls to this function is 5 ms which is enough to finish a transmit - receive process.

vDuong1302_0-1728094066970.png

 

The test code of both master and slave is attached below. Please show me anything innormal in these test code

Thank you.

Duong

 

KDJEM.1
ST Employee

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

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

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