cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F070CB HAL SPI2 RX interrupt

Raj Patel
Associate II
Posted on September 27, 2017 at 19:16

   I am trying to setup and run  SPI2 RX interrupt on a STM32F070CB microcontroller. I am utilizing the STM HAL drivers to setup the SPI and I am using  the STM32CubeMX to setup the code for my project. I am transitioning  my old project from the Standard Library to the HAL Library.  My project will use the  STM LIS2DE12  motion sensor connected to SPI port 2. 

   The problem is that when I enable the function  '__HAL_SPI_ENABLE_IT(&hspi2,(SPI_IT_RXNE| SPI_IT_ERR))', the microcontroller goes into hard fault and it does not get into the HAL_SPI_RxCpltCallback function.

I have displayed the important functions below to setup SPI2. I am expecting to get 8bits of  received data after every send on the SPI bus. Please advise if my setup is incorrect. 

1. In main.c 

void MX_SPI2_Init(void)

{

/* SPI2 parameter configuration*/

hspi2.Instance = SPI2;

hspi2.Init.Mode = SPI_MODE_MASTER;

hspi2.Init.Direction = SPI_DIRECTION_2LINES;

hspi2.Init.DataSize = SPI_DATASIZE_8BIT;

hspi2.Init.CLKPolarity = SPI_POLARITY_LOW;

hspi2.Init.CLKPhase = SPI_PHASE_1EDGE;

hspi2.Init.NSS = SPI_NSS_HARD_OUTPUT;

hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;

hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB;

hspi2.Init.TIMode = SPI_TIMODE_DISABLE;

hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;

hspi2.Init.CRCPolynomial = 7;

hspi2.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;

hspi2.Init.NSSPMode = SPI_NSS_PULSE_DISABLE;

if (HAL_SPI_Init(&hspi2) != HAL_OK)

{

_Error_Handler(__FILE__, __LINE__);

}

}

2. stm32f0xx_it.c 

void SPI2_IRQHandler(void)

{

   HAL_SPI_IRQHandler(&hspi2);

}

3. stm32f0xx_hal_msp.c

void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi)

{

GPIO_InitTypeDef GPIO_InitStruct;

if(hspi->Instance==SPI2)

{

   /* USER CODE BEGIN SPI2_MspInit 0 */

   /* USER CODE END SPI2_MspInit 0 */

   /* Peripheral clock enable */

   __HAL_RCC_SPI2_CLK_ENABLE();

   /**SPI2 GPIO Configuration

   PB12 ------> SPI2_NSS

   PB13 ------> SPI2_SCK

   PB14 ------> SPI2_MISO

   PB15 ------> SPI2_MOSI

   */

   GPIO_InitStruct.Pin = GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15;

   GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

   GPIO_InitStruct.Pull = GPIO_NOPULL;

   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;

   GPIO_InitStruct.Alternate = GPIO_AF0_SPI2;

   HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

   /* SPI2 interrupt Init */

   HAL_NVIC_SetPriority(SPI2_IRQn, 0, 0);

   HAL_NVIC_EnableIRQ(SPI2_IRQn);

   /* USER CODE BEGIN SPI2_MspInit 1 */

   /* USER CODE END SPI2_MspInit 1 */

}

}

4. spi2.c,  my written file

static unsigned char RxData[64];

static unsigned char RxIndex = 0;

static void SendData(uint8_t data);

static unsigned char *tail,*head;

void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi)

{

      if(hspi->Instance == SPI2)

      {

         if(__HAL_SPI_GET_FLAG(&hspi2,SPI_IT_RXNE) != RESET)

         {

             uint8_t result;

             HAL_SPI_Receive_IT(&hspi2, &result, 1);

             RxData[RxIndex++]= result; 

// RxData and RxIndex is global

         }

      }

}

void Spi2_Init(void)

{

   tail=head=RxData;

 

  /* Configure SPI */

   MX_SPI2_Init();

   HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_SET);

   __HAL_SPI_ENABLE_IT(&hspi2,(SPI_IT_RXNE| SPI_IT_ERR));

   memset(RxData, 0, 64);

   RxIndex = 0;

} 

void Spi2_ReadAndWriteArray(uint8_t* payload, uint8_t* rxPacket, uint8_t length)

{

      unsigned char i = 0, tmp = 0;

      Spi2_CS_LOW(); // CS to low.

for(i=0;i<20;i++)

{

   tmp++;

}

   tmp = 0;

for(i=0;i<length;i++)

{

      /* Wait until the transmit buffer is empty */

      while(HAL_SPI_GetState(&hspi2) != HAL_SPI_STATE_READY){};

      /* Send the byte */

      SendData(*(payload+i));

      while(i == RxIndex){}

}

      Spi2_CS_HIGH();

// CS to high.

      memcpy(rxPacket, RxData, length);

      memset(RxData, 0, RxIndex);

      RxIndex = 0;

}

void Spi2_CS_LOW(void)

{

   HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_RESET);

}

void Spi2_CS_HIGH(void)

{

   HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_SET);

}

static void SendData(uint8_t data)

{

      HAL_SPI_Transmit(&hspi2, &data, 1, 10);

}

#stmf070-spi2 #hal_spi_receive #spi_rx #stmf070-spi-interrupt
0 REPLIES 0