cancel
Showing results for 
Search instead for 
Did you mean: 

SPI Master-Slave Sync Issues

Imran Arshad
Associate II
Posted on May 05, 2017 at 07:07

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?

3 REPLIES 3
Posted on May 05, 2017 at 10:10

Also I am not using any NSS line.

Then  use it, in the slave.

JW

Posted on May 05, 2017 at 11:17

Currently it might not be possible to use NSS line.Isn't it supposed to be used with multiple clients? I have only one master and one client... Furthermore whenever I start my client first and the master later the communication is perfect... I know that this is how it should be i.e always start the client first and then start the master but it will not be possible in my scenario. Both my cards will be powered up at the same time from same supply.

Posted on May 05, 2017 at 11:27

Well then reset (disable/enable, using SPI_CR1.SPE) the slave's SPI just after the 'select' line goes active, just before master starts transmitting.

Allow enough time between those two events in the master of course.

JW