cancel
Showing results for 
Search instead for 
Did you mean: 

STM 32 SPI COMMUNICATION PROBLEM BTW STM32F407G-DISC1 AND STM32F429I-DISC1 BOARDS

Moses
Associate II

My connection is below:

0693W00000D1piSQAR.pngI have configured STM32F407G as master and STM32F429I as slave from CUBEMX. According to reference manual "The idle state of SCK must correspond to the polarity selected in the SPI_CR1 register (by pulling up SCK if CPOL=1 or pulling down SCK if CPOL=0).", I configured my sck pins as pull down with respect to CPOL=0.

I connected my logic analayzer and MOSI line datas are perfect but MISO line datas are corrupted or another thing. I need a help. I didnot understand what is happening.

0693W00000D1pmFQAR.png

15 REPLIES 15

Initialize the CS pin as a GPIO output and manipulate it manually. Set it low before HAL_SPI_TransmitReceive and high afterwards. Don't use the "hardware NSS" mode.

Edit: This is for the master. On the slave, do use the hardware NSS pin.

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

Seems fine to me, but if the issue is data on the MISO line, the relevant code would be the slave code.

Regardless, you need a way of syncing the two, for example a CS pin.

Instead of showing a ton of data, try transferring 2 bytes with known values. Look at what appears on the line and compare it to what you're expecting. Most likely, the data will be shifted left or right by a bit, but will otherwise be correct.

If you feel a post has answered your question, please click "Accept as Solution".
//SLAVE SIDE CODE STM32F429I-DISC1
  while (1)
  {
	  ret=HAL_SPI_TransmitReceive(&hspi4, (uint8_t*)aTxBuffer, (uint8_t *)aRxBuffer, BUFFERSIZE, 5000);
	  switch(ret)
	  {
	  case HAL_OK:
	    /* Communication is completed_____________________________________________*/
	    /* Compare the sent and received buffers */
	    if(Buffercmp((uint8_t*)aTxBuffer, (uint8_t*)aRxBuffer, BUFFERSIZE))
	    {
	      /* Transfer error in transmission process */
	    	HAL_GPIO_WritePin(GPIOG, LD4_Pin, SET);
//	    	while(1);
	    }
 
	    /* Turn LED3 on: Transfer process is correct */
		HAL_GPIO_WritePin(GPIOG, LD3_Pin, SET);
	    break;
 
	  case HAL_TIMEOUT:
	    /* A Timeout occurred_____________________________________________________*/
	    /* Call Timeout Handler */
	  	HAL_GPIO_WritePin(GPIOG, LD4_Pin, SET);
		HAL_Delay(500);
		HAL_GPIO_WritePin(GPIOG, LD4_Pin, RESET);
		HAL_Delay(500);
	    break;
 
	    /* An Error occurred______________________________________________________*/
	  case HAL_ERROR:
	    /* Call Timeout Handler */
		HAL_GPIO_WritePin(GPIOG, LD4_Pin, SET);
//		while(1);
	    break;
 
	  default:
	    break;
	  }
 
    /* USER CODE END WHILE */
//    MX_USB_HOST_Process();
 
    /* USER CODE BEGIN 3 */
  }
//MASTER SIDE CODE STM32F407G-DISC1 
 /* USER CODE BEGIN WHILE */
  while (1)
  {
//	  HAL_SPI_MspInit(&hspi2);
 
	  //ENABLE SLAVE SELECT PIN
	  HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, RESET);
 
	  //FOR SIGNAL STABLIZATION
	  HAL_Delay(1);
	  ret = HAL_SPI_TransmitReceive(&hspi2, (uint8_t*)aTxBuffer, (uint8_t *)aRxBuffer, BUFFERSIZE, 5000);
	  //FOR SIGNAL STABLIZATION
	  HAL_Delay(1);
 
	  //DISABLE SLAVE SELECT PIN
	  HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, SET);
 
	  switch(ret)
	  {
	  case HAL_OK:
	    /* Communication is completed ____________________________________________*/
	    /* Compare the sent and received buffers */
	    if(Buffercmp((uint8_t*)aTxBuffer, (uint8_t*)aRxBuffer, BUFFERSIZE))
	    {
	      /* Transfer error in transmission process */
	      HAL_GPIO_WritePin(GPIOD, LD5_Pin, SET);
//	      while(1);
	    }
 
	    /* Turn LED4 on: Transfer in transmission process is correct */
	    HAL_GPIO_WritePin(GPIOD, LD4_Pin, SET);
	    /* Turn LED6 on: Transfer in reception process is correct */
	    HAL_GPIO_WritePin(GPIOD, LD6_Pin, SET);
	    break;
 
	  case HAL_TIMEOUT:
	    /* A Timeout occurred_____________________________________________________*/
	    /* Call Timeout Handler */
	    HAL_GPIO_WritePin(GPIOD, LD5_Pin, SET);
		HAL_Delay(500);
	    HAL_GPIO_WritePin(GPIOD, LD5_Pin, RESET);
		HAL_Delay(500);
	    break;
 
	    /* An Error occurred______________________________________________________*/
	  case HAL_ERROR:
	    /* Call Timeout Handler */
	    HAL_GPIO_WritePin(GPIOD, LD5_Pin, SET);
	    break;
 
	  default:
	    break;
	  }
 
	  HAL_Delay(100);
 
    /* USER CODE END WHILE */
//    MX_USB_HOST_Process();
 
    /* USER CODE BEGIN 3 */
  }

Zoom out view of logic analyzer. channel 1 is slave select pin that is configured manually as GPIO output not hardware NSS for master side. I configured hardware NSS input from cubemx for slave side0693W00000D1qidQAB.png 

Zoom in views of some transmission and reception datas. I used just "SS"(actually 'S','S','\0') string from master to slave and slave to master. Thus 3 datas will be seen every transmission and reception. MOSI datas are good and MISO datas were changed every time. I showed below pictures.

0693W00000D1qk5QAB.png0693W00000D1qkeQAB.png0693W00000D1qlIQAR.png0693W00000D1qkoQAB.png 

In addition, my sck(clock) pins gpio configurations are below code. Master and slave sides are same type configs but slave side has NSS hardware input configuration.

    /*    
    PB13     ------> SPI2_SCK
    PB14     ------> SPI2_MISO
    PB15     ------> SPI2_MOSI
    */
    GPIO_InitStruct.Pin = GPIO_PIN_13;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_PULLDOWN;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF5_SPI2;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
 
    GPIO_InitStruct.Pin = GPIO_PIN_14|GPIO_PIN_15;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF5_SPI2;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

>You still did not tell us, why do you think your SPI communication is failing. What exactly are the symptoms and how are they different from the expectations?

There must be same data on MOSI and MISO line because I send same string from slave to master and master to slave. Thus MISO line or from slave side cannot respond properly. Also, I am using polling mode to try SPI communication. I made slave select pin manually from gpio output but result is same.

In additon, I had tried ST's example(SPI_FullDuplex_ComPolling Example from STM32CubeIde) between 2 STM32f407 boards and MISO datas were meaningless.

Thank you Mr. MM..1