cancel
Showing results for 
Search instead for 
Did you mean: 

stm32 as a SPI slave data transmit error

ykn
Senior

hi there, 

I want to communicate between two Stm32 using SPI, when i transmit date from master to slave, it received(using received interrupt). but when want to transmit date from slave side it gives HAL_error,

master side code

 

while (1)
  {
	  for(int i=0; i<9;i++)
	  {
		  HAL_GPIO_WritePin(SPI1_NSS_GPIO_Port, SPI1_NSS_Pin, GPIO_PIN_RESET);
		 // HAL_Delay(10);
		  HAL_UART_Transmit (&huart1, "OK1\r\n", 5, 10);
		  data[0] = 0x40 + i;  // multibyte write
		  data[1] = 0x60 ;
		  status = HAL_SPI_Transmit (&hspi1, data, 2, 1000);  // write data to register
		 // HAL_Delay(10);
		  status = HAL_SPI_Receive (&hspi1, Rxdata, 2, 5000);  // write data to register
		  HAL_GPIO_WritePin(SPI1_NSS_GPIO_Port, SPI1_NSS_Pin, GPIO_PIN_SET);
		  if(Rxdata[0] == 0x11)
		  {
			  HAL_UART_Transmit (&huart1, "Received11\r\n", 12, 10);
		  }
		  HAL_Delay(1000);
	  }

Slave side code

void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
	HAL_SPI_Receive(&hspi1, data, 2, 5000);  // write data to register
	Spi_rcvr_flag= 1 ;
}

whenever the CS goes to low state, slave become ready to receive the data.

 while(hspi1.State != HAL_SPI_STATE_READY);
if(Spi_rcvr_flag){
	  if(data[1] == 0x60 && data[0] == 0x40 )
	  	{
		  status =  HAL_SPI_Transmit (&hspi1, (uint8_t)*TXdata, 2, 5000);
		  while(hspi1.State != HAL_SPI_STATE_READY);
		  if(status == HAL_OK){
			  HAL_UART_Transmit (&huart1, "Received 0\r\n", 13, 10);}
	  	}
	  else if(data[1] == 0x60 && data[0] == 0x41 )
	  	{
		    HAL_SPI_Transmit (&hspi1, TXdata, 2, 1000);
	  		HAL_UART_Transmit (&huart1, "Received 1\r\n", 13, 10);
	  	}
	  else if(data[1] == 0x60 && data[0] == 0x42 )
	  	  	{
	  		    HAL_SPI_Transmit (&hspi1, TXdata, 2, 1000);
	  	  		HAL_UART_Transmit (&huart1, "Received 2\r\n", 13, 10);
	  	  	}
	  else if(data[1] == 0x60 && data[0] == 0x43 )
	  	  	{
	  		    HAL_SPI_Transmit (&hspi1, TXdata, 2, 1000);
	  	  		HAL_UART_Transmit (&huart1, "Received 3\r\n", 13, 10);
	  	  	}
	  else if(data[1] == 0x60 && data[0] == 0x44 )
	  	  	{
	  		    HAL_SPI_Transmit (&hspi1, TXdata, 2, 1000);
	  	  		HAL_UART_Transmit (&huart1, "Received 4\r\n", 13, 10);
	  	  	}
	  else if(data[1] == 0x60 && data[0] == 0x45 )
	  	  	{
	  		    HAL_SPI_Transmit (&hspi1, TXdata, 2, 1000);
	  	  		HAL_UART_Transmit (&huart1, "Received 5\r\n", 13, 10);
	  	  	}
	  else if(data[1] == 0x60 && data[0] == 0x46 )
	  	  	{
	  		    HAL_SPI_Transmit (&hspi1, TXdata, 2, 1000);
	  	  		HAL_UART_Transmit (&huart1, "Received 6\r\n", 13, 10);
	  	  	}
	  else if(data[1] == 0x60 && data[0] == 0x47 )
	  	  	{
	  		    HAL_SPI_Transmit (&hspi1, TXdata, 2, 1000);
	  	  		HAL_UART_Transmit (&huart1, "Received 7\r\n", 13, 10);
	  	  	}
	  else if(data[1] == 0x60 && data[0] == 0x48 )
	  	  	{
	  		  status =  HAL_SPI_Transmit (&hspi1, TXdata, 2, 5000);
	  		  if(status == HAL_OK){
	  	  		HAL_UART_Transmit (&huart1, "Received 8\r\n", 13, 10);}
	  	  	}
	  else if(data[1] == 0x60 && data[0] == 0x49 )
	  	  	{
	  		    HAL_SPI_Transmit (&hspi1, TXdata, 2, 1000);
	  	  		HAL_UART_Transmit (&huart1, "Received 9\r\n", 13, 10);
	  	  	}
	  Spi_rcvr_flag= 0 ;
}

but at the receiver side it HAl_Error and in master side both byte recievd same data.

please guide how to make SPI slave library,

thanking you

2 REPLIES 2
ykn
Senior

as I am transmitting 0x11 and 0x22 data from slave side with HAL_error.

then master side i received 0x22 and 0x22 in both pck byte.

now after number of try and error method I am able to transmit 16 byte s of data,

why this happened I am not going to understands,

please guide  some one guide me who already gone through this problem or have done this communication,

 

void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
	if(!HAL_GPIO_ReadPin (GPIOG, GPIO_PIN_10)){
	HAL_SPI_Receive(&hspi1, data, 98, 1000);  // write data to register
	while(hspi1.State != HAL_SPI_STATE_READY);
	//HAL_SPI_Transmit (&hspi1, (uint8_t)*TXdata, 2, 5000);
	Spi_rcvr_flag= 1 ;
	}
 if(HAL_GPIO_ReadPin (GPIOG, GPIO_PIN_10))
	{
	//	while(hspi1.State != HAL_SPI_STATE_READY);
		HAL_SPI_Transmit (&hspi1, &TXdata, 16, 1000);
		while(hspi1.State != HAL_SPI_STATE_READY);
	}

 

Thanking You,

Yogesh Nandurkar