ImBr
ImBr

Reputation: 1

STM32F4 : Send/receive data to MCP25xxFD using SPI with DMA

I am interfacing the can_fd controller MCP2517FD with the STM32F4 (Cortex-M) using the SPI HAL library. using the controller driver :https://github.com/SDibla/Cortex-M4-MCP2517FD_Driver

In blocking mode, CAN-FD transmission is working as expected with the following function:

int8_t DRV_SPI_TransferData(uint8_t *SpiTxData, uint8_t *SpiRxData, uint16_t spiTransferSize)  
{  
    HAL_StatusTypeDef status;  
 
    GPIO_PinWrite(CS_GPIO, CS_PIN, 0U);  
 
    status = HAL_SPI_TransmitReceive(&hspi3, SpiTxData, SpiRxData, spiTransferSize, 10);  //blocking data-transfer
 
    GPIO_PinWrite(CS_GPIO, CS_PIN, 1U);  
 
    return (int8_t) status;  
}  

Now, I want to switch to non-blocking mode (DMA) using:

status = HAL_SPI_TransmitReceive_DMA(&hspi3, SpiTxData, SpiRxData, spiTransferSize);

instead of blocking:

`status = HAL_SPI_TransmitReceive(&hspi3, SpiTxData, SpiRxData, spiTransferSize, 10);

in the DRV_SPI_TransferData function.

Are any modifications or considerations necessary in the controller driver code to make this work?

Thanks.

Upvotes: 0

Views: 37

Answers (1)

Tagli
Tagli

Reputation: 2602

As the DMA variant of the library function doesn't block, you need to structure your program flow accordingly. Usually, you need to implement a "DMA complete ISR". Un-selecting the SPI slave (GPIO_PinWrite(CS_GPIO, CS_PIN, 1U)) goes there (DMA ISR) for example.

If such a design change is not possible or doesn't provide any performance benefits (maybe there is nothing to do during that time period) for your case, you may want to stick with the blocking variant. It all depends on the design of your overall project.

Upvotes: 0

Related Questions