Skip to main content
Visitor II
February 24, 2024
Question

SPI Interrupt & DMA not working, polling is

  • February 24, 2024
  • 2 replies
  • 1070 views

hi, I m using H7A3 nucleo, manage to get SPI polling working, enclose code below, when i try in implement Interrupt and DMA , the code compile successfully with no error,  but SPI output no signal, SPI slave device is a TI ADC...I have set SPi global interrupt and DMA circular

HAL_StatusTypeDef spi_write(reg, *pData)
{
 HAL_StatusTypeDef ret;
 uint8_t sendData[2] = {reg, *pData};
 
 HAL_GPIO_WritePin(...); // CS pull low
 ret = HAL_SPI_Transmit_IT(&hspi1, sendData, 2, 20);
// ret = HAL_SPI_Transmit_DMA(&hspi1, sendData, 2, 20);
 if (ret != HAL_OK)
 {
 HAL_GPIO_WritePin(..); // CS pull high
 return ret;
 }

in .ioc.  Anyone can kindly advice?

    This topic has been closed for replies.

    2 replies

    Technical Moderator
    February 24, 2024

    Hello @StanCosgrove 

    In interrupt mode, please make sure you correctly configured the SPI interrupt vector 

    • Link the SPI interrupt line to NVIC as follow (usually done in msp.c file, you can do it in the main.c file)
     /*##-3- Configure the NVIC for SPI #########################################*/
     /* NVIC for SPI */
     HAL_NVIC_SetPriority(SPIx_IRQn, 1, 0);
     HAL_NVIC_EnableIRQ(SPIx_IRQn);
    • Add the IRQ Handler of the SPI peripheral (in it.c file).
    /**
     * @brief This function handles SPI interrupt request.
     * @param None
     * @retval None
     */
    void SPIx_IRQHandler(void)
    {
     HAL_SPI_IRQHandler(&SpiHandle);
    }
    

     All the configuration steps can be generated by the STM32CubeMX

    You can also refer to the example provided by the STM32CubeFW in your repository path: ~\STM32Cube_FW_H7_V1.11.1\Projects\NUCLEO-H7A3ZI-Q\Examples\SPI\SPI_FullDuplex_ComIT

    Best Regards,

    Younes

    Visitor II
    February 26, 2024

    @CMYL wrote:

    Hi, yes I have all these you mentioned generated from the .ioc configuration, and the interrupt and DMA portion is not working, albert only the polling method is working. Can anyone please advise.. 

    Technical Moderator
    February 24, 2024

    Hi @StanCosgrove 

    In DMA mode you need to associate the initialized DMA handle to the the SPI handle, Configure the NVIC for DMA, then enable DMA transfer complete event and configure the NVIC line for SPI as described below. You can also refer to this example provided in CubeFW (~\Repository\STM32Cube_FW_H7_V1.11.1\Projects\NUCLEO-H7A3ZI-Q\Examples\SPI\SPI_FullDuplex_ComDMA)  or use the CubeMx tool:

     

    /**
     * @brief This function handles DMA Rx interrupt request.
     * @param None
     * @retval None
     */
    void SPIx_DMA_RX_IRQHandler(void)
    {
     HAL_DMA_IRQHandler(SpiHandle.hdmarx);
    }
    
    /**
     * @brief This function handles DMA Tx interrupt request.
     * @param None
     * @retval None
     */
    void SPIx_DMA_TX_IRQHandler(void)
    {
     HAL_DMA_IRQHandler(SpiHandle.hdmatx);
    }
    
    /**
     * @brief This function handles SPIx interrupt request.
     * @param None
     * @retval None
     */
    void SPIx_IRQHandler(void)
    {
     HAL_SPI_IRQHandler(&SpiHandle);
    }

    In case of DMA you have 3 interrupt vectors, then 3 IRQ handler to add in it.c file:

     /* Associate the initialized DMA handle to the the SPI handle */
     __HAL_LINKDMA(hspi, hdmarx, hdma_rx);
     
     /*##-4- Configure the NVIC for DMA #########################################*/ 
     /* NVIC configuration for DMA transfer complete interrupt (SPI1_TX) */
     HAL_NVIC_SetPriority(SPIx_DMA_TX_IRQn, 1, 1);
     HAL_NVIC_EnableIRQ(SPIx_DMA_TX_IRQn);
     
     /* NVIC configuration for DMA transfer complete interrupt (SPI1_RX) */
     HAL_NVIC_SetPriority(SPIx_DMA_RX_IRQn, 1, 0);
     HAL_NVIC_EnableIRQ(SPIx_DMA_RX_IRQn);
    
     /*##-5- Configure the NVIC for SPI #########################################*/
     /* NVIC configuration for SPI transfer complete interrupt (SPI1) */
     HAL_NVIC_SetPriority(SPIx_IRQn, 1, 0);
     HAL_NVIC_EnableIRQ(SPIx_IRQn);

     Best Regards,

    Younes