cancel
Showing results for 
Search instead for 
Did you mean: 

slave Interrupt in SPI

shayangh
Associate II

hey every one .I'm working on SPI and configuring a slave which should answer to the master request . slave reception process work with interrupt after 2 successful reception. 

THIS IS MY CODE :

 

 

 

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2024 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
SPI_HandleTypeDef hspi1;

/* USER CODE BEGIN PV */
 uint8_t SPI_RX_Buffer;


uint8_t data5A = 0X50;
uint8_t data31 = 0x30;
volatile uint8_t receive_Data_Package;
volatile uint8_t RX_SPI_num = 0;


uint8_t RequestFor0x50= 0;
uint8_t RequestFor0x31= 0;
uint8_t ResponsWith0x00= 0;

uint8_t Global_pRxBuffe;


uint8_t Tx_State;
uint8_t Rx_State;

#define SPI_BUSY_IN_RX  2
#define SPI_BUSY_IN_TX 1
#define SPI_FREE 0
/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_SPI1_Init(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_SPI1_Init();
  /* USER CODE BEGIN 2 */

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */

  while(RequestFor0x50 == 0)
  {

  HAL_SPI_TransmitReceive(&hspi1, &data5A, &SPI_RX_Buffer, 1, 100);
  HAL_SPIEx_FlushRxFifo(&hspi1);
if(SPI_RX_Buffer == 0x5a)
{
RequestFor0x50 = 1;
RX_SPI_num++;
}

  };
  while(RequestFor0x31 == 0)
  {

  HAL_SPI_TransmitReceive(&hspi1, &data31, &SPI_RX_Buffer, 1, 100);
  HAL_SPIEx_FlushRxFifo(&hspi1);
if(SPI_RX_Buffer == 0x30)
{
RequestFor0x31 = 1;
receive_Data_Package = 1;
RX_SPI_num++;
}
  };

  HAL_SPI_TransmitReceive_IT(&hspi1, &ResponsWith0x00, &SPI_RX_Buffer, 1);
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */


  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  {
    Error_Handler();
  }
}

/**
  * @brief SPI1 Initialization Function
  *  None
  * @retval None
  */
static void MX_SPI1_Init(void)
{

  /* USER CODE BEGIN SPI1_Init 0 */

  /* USER CODE END SPI1_Init 0 */

  /* USER CODE BEGIN SPI1_Init 1 */

  /* USER CODE END SPI1_Init 1 */
  /* SPI1 parameter configuration*/
  hspi1.Instance = SPI1;
  hspi1.Init.Mode = SPI_MODE_SLAVE;
  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.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_DISABLE;
  if (HAL_SPI_Init(&hspi1) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN SPI1_Init 2 */

  /* USER CODE END SPI1_Init 2 */

}

/**
  * @brief GPIO Initialization Function
  *  None
  * @retval None
  */
static void MX_GPIO_Init(void)
{
/* USER CODE BEGIN MX_GPIO_Init_1 */
/* USER CODE END MX_GPIO_Init_1 */

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOF_CLK_ENABLE();
  __HAL_RCC_GPIOA_CLK_ENABLE();

/* USER CODE BEGIN MX_GPIO_Init_2 */
/* USER CODE END MX_GPIO_Init_2 */
}

/* USER CODE BEGIN 4 */
void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi)
{

HAL_SPI_TransmitReceive_IT(&hspi1, &ResponsWith0x00, &SPI_RX_Buffer, 1);


}
//void SPI1_IRQHandler(void)
//{
//
// SPI_RX_Buffer = *( volatile uint8_t *)&SPI1 -> DR;
// RX_SPI_num++;
//
//}
/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  *   file: pointer to the source file name
  *   line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

 

 

 

 

the software algorithm is like getting 2 bytes and answering them. after that software captures 131 bytes with interrupt but the first byte that I capture is the 27th byte that I expect.

why this is happening is my interrupt can't follow the master clock speed or somehow in my program I'm skipping 27 bytes

here is an image in which I monitored the transaction with Logic Analyzer :

(first 2 bytes)

image.png

 

(131 bytes : )

 

image.png

 

 note : 0x38 is 27th bytes

in debug mode, RX_SPI_num is 2 (which indicates the first 2 bytes received successfully and terminated related loops), and SPI_RX_Bufferis 0x38 when I get into a receiving call back for the first time.

shayangh_1-1714914231663.png

don't care about my MISO. I've got some data probably because I can't flush the shift register for Spi. I searched around the net and probably even ST doesn't have a proper way to do it(right now I'm working with Hal driver) but if you know how to flush the data register or related shift register that makes MISO equal to 0, tell me because it's reducing my program complexity 

 

1 ACCEPTED SOLUTION

Accepted Solutions

hi @OSAKE.1 

thanks for response.

the problem was I couldn't exit from my loop so the interrupt couldn't exit from my loop( IDK why that would be helpful if you can tell me !!!). 

anyway by defining the exit condition like below I successfully managed to exit from my loop and capture the data without any loss 

#define THIRTY 0X30
#define FIFTY 0X5A
  while(RequestFor0x50 == 0)
  {

  HAL_SPI_TransmitReceive(&hspi1, &data5A, &SPI_RX_Buffer, 1, 100);
  HAL_SPIEx_FlushRxFifo(&hspi1);
if(SPI_RX_Buffer == FIFTY )
{
RequestFor0x50 = 1;
RX_SPI_num++;
}

  };
  while(RequestFor0x31 == 0)
  {

  HAL_SPI_TransmitReceive(&hspi1, &data31, &SPI_RX_Buffer, 1, 100);
  HAL_SPIEx_FlushRxFifo(&hspi1);
if(SPI_RX_Buffer == THIRTY )
{
RequestFor0x31 = 1;
receive_Data_Package = 1;
RX_SPI_num++;
}
  };

 

View solution in original post

6 REPLIES 6
OSAKE.1
ST Employee

Hello @shayangh 

 

Please ensure that there is no synchronization issue between the master and the slave. This means that the slave should be ready to receive data before the master starts sending it.

If your question is answered, please close this topic by clicking "Accept as Solution".

Thanks
Omar

hi @OSAKE.1 

thanks for response.

the problem was I couldn't exit from my loop so the interrupt couldn't exit from my loop( IDK why that would be helpful if you can tell me !!!). 

anyway by defining the exit condition like below I successfully managed to exit from my loop and capture the data without any loss 

#define THIRTY 0X30
#define FIFTY 0X5A
  while(RequestFor0x50 == 0)
  {

  HAL_SPI_TransmitReceive(&hspi1, &data5A, &SPI_RX_Buffer, 1, 100);
  HAL_SPIEx_FlushRxFifo(&hspi1);
if(SPI_RX_Buffer == FIFTY )
{
RequestFor0x50 = 1;
RX_SPI_num++;
}

  };
  while(RequestFor0x31 == 0)
  {

  HAL_SPI_TransmitReceive(&hspi1, &data31, &SPI_RX_Buffer, 1, 100);
  HAL_SPIEx_FlushRxFifo(&hspi1);
if(SPI_RX_Buffer == THIRTY )
{
RequestFor0x31 = 1;
receive_Data_Package = 1;
RX_SPI_num++;
}
  };

 

Hello @shayangh 

 

You didn't share the exit condition.

When HAL_SPI_TransmitReceive_IT() is called within HAL_SPI_TxRxCpltCallback(), it leads to a recursive call pattern that prevents the loop from exiting.

 

 

If your question is answered, please close this topic by clicking "Accept as Solution".

Thanks
Omar

@shayangh wrote:

I couldn't exit from my loop so the interrupt couldn't exit from my loop( IDK why that would be helpful if you can tell me !!!). 


Which loop?

What were you expecting to be the exit condition?

Were you relying on a flag that should have been 'volatile' - but wasn't ... ?

🤔

@OSAKE.1 Thanks osake ...

How should I share the exit condition? 

what is the proper way to do it?

is it casting like what did? or something else?

btw I didn't start the interrupt before the loops. so while loops (first two whiles)happen after that I initial the interrupt and expect to receive data.

if i receive data with interrupts this means the program exited from the while loops!

hey @Andrew Neil

thanks for your response . IDK how to bold texts at code blocks.

but the first two while that check for RequestFor0x50 and RequestFor0x30 variables. 

nah the variables are not changing in the interrupts so they don't need to be defined as volatile.