cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F0 SPI transmit via interrupt

PaKi
Associate II
Posted on August 09, 2017 at 11:30

Hello everyone,

I'm useing the NUCLEO-F072RB and I'm trying to get the SPI via interrupts to work.

Here is the CubeMX initialization of the SPI:

/* SPI1 init function */

void MX_SPI1_Init(void)

{

      hspi1.Instance = SPI1;

      hspi1.Init.Mode = SPI_MODE_MASTER;

      hspi1.Init.Direction = SPI_DIRECTION_2LINES;

     

hspi1.Init.DataSize = SPI_DATASIZE_8BIT;

     

hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;

     

hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;

     

hspi1.Init.NSS = SPI_NSS_HARD_INPUT;

     

hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32;

     

hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;

     

hspi1.Init.TIMode = SPI_TIMODE_DISABLE;

     

hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;

     

hspi1.Init.CRCPolynomial = 7;

     

hspi1.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;

     

hspi1.Init.NSSPMode = SPI_NSS_PULSE_ENABLE;

     

if (HAL_SPI_Init(&hspi1) != HAL_OK)

     

{

     

     

_Error_Handler(__FILE__, __LINE__);

     

}

}

In my main I start sending 3 bytes every 10 seconds by calling:

if (!(HAL_GetTick() % 10000))

{

     u8_spi_tx_buffer[0] = 0xaa;

     

u8_spi_tx_buffer[1] = 0x00;

     

u8_spi_tx_buffer[2] = 0xff;

     

if(HAL_SPI_Transmit_IT(&hspi1,u8_spi_tx_buffer,3) != HAL_OK)

     

{

     

     

/* Transfer error in transmission process */

     

     

Error_Handler();

     

}

}

The 3 bytes are getting transmitted. However after about 30�s they are getting transmitted again. This happens about 20 times before they are getting stopped transmitting.

Do I have to do anything in the HAL_SPI_TxCpltCallback function to prevent this?

#stm32f0 #spi #stm32-spi-interrupts
2 REPLIES 2
PaKi
Associate II
Posted on August 09, 2017 at 13:02

I was so focused on the initialization of the SPI thinking there has to be something wrong, that i didn't see the mistake is actually '

if (!(HAL_GetTick() % 10000))'...

Better going for a break and clean my mind...

Willem La Grange
Associate II
Posted on August 10, 2017 at 15:40

I would do the following:

/* Declare two variables or two bits volatile */

volatile uint8_t SPIsending, NeedToSendSPI = false;

if (!(HAL_GetTick() % 10000))

{

      if(!SPIsending)

             NeedToSendSPI = true;

}

if(NeedToSendSPI)

{

      /* Will try recursively until SPI sending is accepted */

     u8_spi_tx_buffer[0] = 0xaa;

     

u8_spi_tx_buffer[1] = 0x00;

     

u8_spi_tx_buffer[2] = 0xff;

     

if(HAL_SPI_Transmit_IT(&hspi1,u8_spi_tx_buffer,3) == HAL_OK)

     

{

            SPIsending = true;

     

       NeedToSendSPI = false;

     

}

      else

      {

         /* Transfer error in transmission process */

     

     

Error_Handler();

            /* Deinit, Init, etc processes here */

      }

}

void HAL_SPI_TxCpltCallback(void)

{

         SPIsending = false;

         /* 1-2 ms to prevent sending again */

         HAL_Delay(2);  /* 1-2 ms to prevent sending again from 'if (!(HAL_GetTick() % 10000))' */

        Do_WhateverAfter();

}