cancel
Showing results for 
Search instead for 
Did you mean: 

Unable to communicate between two Nucleo board using SPI

ercjethava1992
Associate II

I am writing sample demo Application to test data transfer between SPI Master(Nucleo F411RE) and SPI Slavee(Nucleo H755ZI-Q) device.

Master SPI:
/**SPI2 GPIO Configuration
PC2 ------> SPI2_MISO
PC3 ------> SPI2_MOSI
PB10 ------> SPI2_SCK

Slave SPI:
/**SPI1 GPIO Configuration
PA5 ------> SPI1_SCK
PA6 ------> SPI1_MISO
PB5 ------> SPI1_MOSI

For validating what data Slave SPI device receiving over SPI I am sending this data over UART to display it on Host PC 

Here is SLAVE SPI device UART response:
What I am seeing is that SPI receives wrong data instead of 0x41 (Send by master)

 

 

Slave Received SPI data: 0x60
Slave Transmit SPI data: 0xAA
Slave Received SPI data: 0x60
Slave Transmit SPI data: 0xAA
Slave Received SPI data: 0x60
Slave Transmit SPI data: 0xAA
Slave Received SPI data: 0x60
Slave Transmit SPI data: 0xAA
Slave Received SPI data: 0x60
Slave Transmit SPI data: 0xAA
Slave Received SPI data: 0x60
Slave Transmit SPI data: 0xAA
Slave Received SPI data: 0x60
Slave Transmit SPI data: 0xAA

 

 

 


Master SPI Code:

 

 

  uint8_t message[1] = {0x41};
  int counter = 0;
  while (1)
  {
	  HAL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin);
	  HAL_SPI_Transmit(&hspi2, message, 1, HAL_MAX_DELAY);
	  HAL_Delay(500);
	  counter++;
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }

static void MX_SPI2_Init(void)
{

  /* USER CODE BEGIN SPI2_Init 0 */

  /* USER CODE END SPI2_Init 0 */

  /* USER CODE BEGIN SPI2_Init 1 */

  /* USER CODE END SPI2_Init 1 */
  /* 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_SOFT;
  hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32;
  hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB;
  hspi2.Init.TIMode = SPI_TIMODE_DISABLE;
  hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  hspi2.Init.CRCPolynomial = 10;
  if (HAL_SPI_Init(&hspi2) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN SPI2_Init 2 */

  /* USER CODE END SPI2_Init 2 */

}

 

 

 


SLAVE SPI code:

 

 

uint8_t txData[1] = { 0xAA }; // Data to send to the master
uint8_t rxData[1] = { 0x00 }; // Buffer to store received data

  uint8_t tx_uart_buff[50];
  while (1)
  {
	  // Wait for data from the Master
      if (HAL_SPI_Receive(&hspi1, rxData, 1, HAL_MAX_DELAY) == HAL_OK) {
          // Data received successfully, respond to master

    	  snprintf((char*)tx_uart_buff, sizeof(tx_uart_buff), "Slave Received SPI data: 0x%02X\r\n", rxData[0]);
	  	  HAL_UART_Transmit(&huart3,tx_uart_buff,sizeof(tx_uart_buff),10);

    	  HAL_GPIO_TogglePin(LD1_GPIO_Port, LD1_Pin); // Example: Toggle an LED
          HAL_SPI_Transmit(&hspi1, txData, 1, HAL_MAX_DELAY); // Send response to the master
          snprintf((char*)tx_uart_buff, sizeof(tx_uart_buff), "Slave Transmit SPI data: 0x%02X\r\n", txData[0]);
	  	  HAL_UART_Transmit(&huart3,tx_uart_buff,sizeof(tx_uart_buff),10);
      } else {
    	  snprintf((char*)tx_uart_buff, sizeof(tx_uart_buff), "Slave Receive SPI error\r\n");
    	  HAL_UART_Transmit(&huart3,tx_uart_buff,sizeof(tx_uart_buff),10);
      }

    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }


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_SOFT;
  hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
  hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  hspi1.Init.CRCPolynomial = 0x0;
  hspi1.Init.NSSPMode = SPI_NSS_PULSE_DISABLE;
  hspi1.Init.NSSPolarity = SPI_NSS_POLARITY_LOW;
  hspi1.Init.FifoThreshold = SPI_FIFO_THRESHOLD_01DATA;
  hspi1.Init.TxCRCInitializationPattern = SPI_CRC_INITIALIZATION_ALL_ZERO_PATTERN;
  hspi1.Init.RxCRCInitializationPattern = SPI_CRC_INITIALIZATION_ALL_ZERO_PATTERN;
  hspi1.Init.MasterSSIdleness = SPI_MASTER_SS_IDLENESS_00CYCLE;
  hspi1.Init.MasterInterDataIdleness = SPI_MASTER_INTERDATA_IDLENESS_00CYCLE;
  hspi1.Init.MasterReceiverAutoSusp = SPI_MASTER_RX_AUTOSUSP_DISABLE;
  hspi1.Init.MasterKeepIOState = SPI_MASTER_KEEP_IO_STATE_DISABLE;
  hspi1.Init.IOSwap = SPI_IO_SWAP_DISABLE;
  if (HAL_SPI_Init(&hspi1) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN SPI1_Init 2 */

  /* USER CODE END SPI1_Init 2 */

}

 

 

 


I have also attached full Master and Slave CubeIDE Project.
If anyone can help me to figure out issue here as SPI Slave always reads wrong data.
@Andrew Neil @tjaekel @Peter BENSCH @B.Montanari 

10 REPLIES 10
ercjethava1992
Associate II

@tjaekel  Master is sending correct data which I have validated based on loopback mode: Master and Slave SPI configuration generated automatically based on STM32CubeMX tool I have provided both Master and SPI configure settings in which you can see NNS settings as SPI_NSS_SOFT which sets that bit which you are referring 

 

 

 uint8_t tx_uart_buff[50];
 uint8_t message_tx[1] = {0xBB};
 uint8_t message_rx[1] = {0x0};

  while (1)
  {
		  if(HAL_SPI_TransmitReceive(&hspi2,message_tx,message_rx,1,HAL_MAX_DELAY) == HAL_OK) {
			  snprintf((char*)tx_uart_buff, sizeof(tx_uart_buff), "Received SPI data: 0x%02X\r\n", message_rx[0]);
			  HAL_UART_Transmit(&huart2,tx_uart_buff,sizeof(tx_uart_buff),10);
		  } else {
			  snprintf((char*)tx_uart_buff, sizeof(tx_uart_buff), "Received SPI Error:\r\n");
			  HAL_UART_Transmit(&huart2,tx_uart_buff,sizeof(tx_uart_buff),10);
		  }
	  HAL_Delay(500);
	  counter++;
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }

// This Master SPI setting generated based on CubeMX IDE which sets CS to SPI_NSS_SOFT Mode
 void MX_SPI2_Init(void)
{

  /* USER CODE BEGIN SPI2_Init 0 */

  /* USER CODE END SPI2_Init 0 */

  /* USER CODE BEGIN SPI2_Init 1 */

  /* USER CODE END SPI2_Init 1 */
  /* 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_SOFT;
  hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32;
  hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB;
  hspi2.Init.TIMode = SPI_TIMODE_DISABLE;
  hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  hspi2.Init.CRCPolynomial = 10;
  if (HAL_SPI_Init(&hspi2) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN SPI2_Init 2 */

  /* USER CODE END SPI2_Init 2 */

}


Received SPI data: 0xBB
Received SPI data: 0xBB
Received SPI data: 0xBB
Received SPI data: 0xBB
Received SPI data: 0xBB
Received SPI data: 0xBB
Received SPI data: 0xBB
Received SPI data: 0xBB
Received SPI data: 0xBB

 

 


-> I have connected same:

MOSI Master to Slave MOSI 
MISO Master to Slave MISO
SCL Master to Slave SCL

-> I have also verified based on initial code  that If I stop sending data from Master to SLAVE MCU , Slave SPI doesn't receive anything, and when Master sends one byte data to Slave then only Slave receives 1 byte however its wrong byte I am receiving which is an issue I have


# Master
-> SPI Baud Rate = 1.3125 Mbps
-> Full-Duplex Master
-> Hardware control CS : Disabled 

Master-SPI-config.png

Master-Clock-Config.png



# Slave
-> Slave Peripheral Clock Frequency: 5Mz
-> Full-Duplex Slave
-> Hardware control CS : Disabled

SPI-Slave-Config.png


Slave-Clock-Config.png