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[0] = 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 = 0xAA; // Data to send to the master
uint8_t rxData = 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);
	  	  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);
	  	  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 

4 REPLIES 4
gbm
Lead III

The data received is the data you send. And what you send is the least significant byte of the address of "message" variable. That's what happens if you ignore the compiler warnings instead of fixing the problems with your code.

Correction - edited: you are sending some data from the address which least significant byte is stored in "message" variable.

Also, please read this topic:

https://community.st.com/t5/stm32-mcus-products/regarding-function-calls-when-performing-spi-communication-in/m-p/738412#M265178

 

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice

Wouldn't TransmitReceive be a more appropriate method where you're concurrently collecting bits on the back edge of the clock that's pushing data out?

Observe what's going on over the wire with a logic analyze so you can visualize the interaction and relate it to the data you see and expectations.

 

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

@gbm 
yes that was an issue instead of sending data was sending Address
However after fixing that original issue still persist Slave receives wrong data:

 

SLAVE Data:

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

 

 

You didn't fix the error - you've introduced another. Look at the compiler messages and fix the problem. First, get the code which compiles without warnings.

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice