cancel
Showing results for 
Search instead for 
Did you mean: 

How to use SPI with DMA as slave

FDi L.228
Associate II

Hello, I'm trying to understand how the HAL_SPI_TransmitReceive_DMA() function works when my mcu is in slave configuration.

If I want to transmit and receive data multiple times do I have to call the HAL_SPI_TransmitReceive_DMA() after each transfer or I just need to call it once at the beginning?

So would it be like:

int main(){
 HAL_SPI_TransmitReceive_DMA(&hspi1, spi_buffer_tx_data,
				spi_buffer_rx_data, 8) 
}
 
void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi) {
 //operations to handle the data
}

or:

int main(){
 HAL_SPI_TransmitReceive_DMA(&hspi1, spi_buffer_tx_data,
				spi_buffer_rx_data, 8) 
}
 
void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi) {
 //operations to handle the data
HAL_SPI_TransmitReceive_DMA(&hspi1, spi_buffer_tx_data,
				spi_buffer_rx_data, 8) 
 
}

1 REPLY 1
TDK
Guru

You need to call it multiple times. It will stop after the first 8 bytes are sent. The second case will probably work, depending on how quickly the master communicates.

You should never return from main() in a microcontroller. May or may not be fine depending on your startup script.

edit: there may be a way to flag it as circular, in which case you don't need to call it every loop, but that may not give you enough time to update the data before it is queued to be sent out.

If you feel a post has answered your question, please click "Accept as Solution".