AnsweredAssumed Answered

SPI Master-Slave Sync Issues

Question asked by Arshad.Imran on May 5, 2017
Latest reply on May 5, 2017 by waclawek.jan

Hi there,

I am trying to implement a SPI master-slave system. The master will continuously send data and toggle one pin high/low after sending 1200 bytes so that the client can get synced and always read the data while properly aligned.  The master is working perfectly as it is sending data and also when I hook up a scope it shows that the pin toggles after 1200 bytes are send.

The problem is with the slave controller. It sometimes receives the data properly and after that keeps receiving it properly. But most of the times I receive bit shifted data i.e garbage. When I reset the slave 5-6 times I again start receiving data properly. My board is STM32F7 Discovery both for Master and slave. Also I am not using any NSS line. Code for Slave is as follows.

 

  SpiHandle.Instance               = SPIx;
  SpiHandle.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
  SpiHandle.Init.Direction         = SPI_DIRECTION_2LINES;
  SpiHandle.Init.CLKPhase          = SPI_PHASE_1EDGE;
  SpiHandle.Init.CLKPolarity       = SPI_POLARITY_LOW;
  SpiHandle.Init.DataSize          = SPI_DATASIZE_8BIT;//SPI_DATASIZE_16BIT;
  SpiHandle.Init.FirstBit          = SPI_FIRSTBIT_MSB;
  SpiHandle.Init.TIMode            = SPI_TIMODE_DISABLE;
  SpiHandle.Init.CRCCalculation    = SPI_CRCCALCULATION_DISABLE;
  SpiHandle.Init.CRCPolynomial     = 7;
  SpiHandle.Init.NSS               = SPI_NSS_SOFT;
  SpiHandle.Init.Mode                                = SPI_MODE_SLAVE;
     
     
     
  __HAL_RCC_GPIOA_CLK_ENABLE();
  GPIO_Start_Sync.Pin = GPIO_PIN_15 ;
  GPIO_Start_Sync.Mode = GPIO_MODE_INPUT;
  GPIO_Start_Sync.Pull = GPIO_PULLDOWN;
  HAL_GPIO_Init(GPIOA , &GPIO_Start_Sync);


          HAL_SPI_Init(&SpiHandle);
          
       while(1) { //Wait for a 0 -> 1 transition on the GPIO Line. So that both client and server are synced.
          if(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_15) == 0)
               {
                    while(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_15) == 0){}
                    break;
               }     
     }
     HAL_SPI_Receive_DMA(&SpiHandle,(uint8_t*)aRxBuffer,1200); //Enable SPI receive in circular mode.
     while(1)
     {}

 

I think that the problem lies somewhere in this HAL_SPI_Receive_DMA function. After syncing with the client using the GPIO line the function takes more time starting the DMA  and during this a few bits/bytes are passed and the alignment is lost How can I work around this problem?

Outcomes