cancel
Showing results for 
Search instead for 
Did you mean: 

Issues with SPI comms between two STM32s

Hrishikesh
Senior

I'm trying out simple SPI comms between two STM32s. The TX is STM32F407 and the RX is STM32G070. The data that I seem to be receiving is different from what I'm sending. What am I missing here?

For the RX, I've set the NSS to software. I've configured the 'chip select' to be an external interrupt on the falling edge. In the ISR I receive the bytes from the TX. This is what the code for the RX looks like. SPI1 is configured as full duplex slave.

/* USER CODE BEGIN PV */
uint8_t spi_rx_buffer[8] = {0, 0, 0, 0, 0, 0, 0, 0};
/* USER CODE END PV */
.
.
.
.
/* USER CODE BEGIN 4 */
void HAL_GPIO_EXTI_Falling_Callback(uint16_t GPIO_Pin)
{
	if (GPIO_Pin == SPI1_CSN_Pin)
	{
		if (HAL_SPI_Receive(&hspi1, spi_rx_buffer, 8, HAL_MAX_DELAY) != HAL_OK)
		{
			Error_Handler();
		}
	}
}
/* USER CODE END 4 */

For the TX, SPI is configured to be a full duplex master with software chip select. With the 64 divider, the data rate configured as 1.3125 Mbit/s. In the TX, I'm simply sending two buffers each of 8 bytes at a time. The code for TX is as follows:

/* USER CODE BEGIN PV */
uint8_t spi_tx_buffer1[8] = {0, 1, 2, 3, 4, 5, 6, 7};
uint8_t spi_tx_buffer2[8] = {8, 9, 10, 11, 12, 13, 14, 15};
/* USER CODE END PV */
.
.
.
.
.
/* USER CODE BEGIN WHILE */
while (1)
{
  /* USER CODE END WHILE */
 
  /* USER CODE BEGIN 3 */
  HAL_GPIO_WritePin(SPI1_CSN_GPIO_Port, SPI1_CSN_Pin, GPIO_PIN_RESET);
  HAL_SPI_Transmit(&hspi1, spi_tx_buffer1, 8, HAL_MAX_DELAY);
  HAL_GPIO_WritePin(SPI1_CSN_GPIO_Port, SPI1_CSN_Pin, GPIO_PIN_SET);
 
  HAL_Delay(5000);
 
  HAL_GPIO_WritePin(SPI1_CSN_GPIO_Port, SPI1_CSN_Pin, GPIO_PIN_RESET);
  HAL_SPI_Transmit(&hspi1, spi_tx_buffer2, 8, HAL_MAX_DELAY);
  HAL_GPIO_WritePin(SPI1_CSN_GPIO_Port, SPI1_CSN_Pin, GPIO_PIN_SET);
 
  HAL_Delay(5000);
}
/* USER CODE END 3 */

The data that I receive is as below,

0693W000006Gw3KQAS.png0693W000006Gw4RQAS.png

1 ACCEPTED SOLUTION

Accepted Solutions
TDK
Guru

Looks like your data is just shifted a few bits. Probably the receiver is not ready to receive before bits are sent, so it misses the first few.

Put a HAL_Delay(1) after you set CS low, but before you send data.

Also, ensure the slave SPI clock is at least twice as fast as the master clock.

Might be something else going on here as well.

If you feel a post has answered your question, please click "Accept as Solution".

View solution in original post

2 REPLIES 2
TDK
Guru

Looks like your data is just shifted a few bits. Probably the receiver is not ready to receive before bits are sent, so it misses the first few.

Put a HAL_Delay(1) after you set CS low, but before you send data.

Also, ensure the slave SPI clock is at least twice as fast as the master clock.

Might be something else going on here as well.

If you feel a post has answered your question, please click "Accept as Solution".

That worked! Thank you for the quick response =)