cancel
Showing results for 
Search instead for 
Did you mean: 

SPI reception, data shifted by one bit

gavin2
Associate II
Posted on June 08, 2012 at 17:42

Hi,

I am having some difficulty receiving SPI data correctly and I am hoping someone could help.

I currently have two STM32L-Discovery boards connected via SPI with one acting as master and one as slave. My SPI transmissions from the master appear fine and I am able to see data being collected in the SPI receive buffer on the slave.

The problem I have is that there appears to be a shift in the received data, e.g:

If I Send:

'00110011'

I receive:

'01100110'

Similarly, if I send:

'01000110'

I receive:

'10001100'

I am pretty sure this is a clock polarity issue but having tried various different combinations of CPOL/CPHA on both the slave and master side, I am still unable to read back exactly what I have sent. Does anyone have any suggestions as to why this may be occurring? Is there anything aside from the CPOL/CPHA setup that I may be missing?

So that I do not add a whole pile of code, I have only added the SPI setups for master and slave. If any other info is required, I will gladly add to this.

-----

MASTER  device

/**********************************************/

  /* Configure SPI2 as transmitter            */

  /**********************************************/

  SPI_I2S_DeInit(SPI2);                                                        

  SPI_InitStructure.SPI_Direction         = SPI_Direction_2Lines_FullDuplex;   

  SPI_InitStructure.SPI_Mode              = SPI_Mode_Master;                    

  SPI_InitStructure.SPI_DataSize          = SPI_DataSize_8b;                   

  SPI_InitStructure.SPI_CPOL              = SPI_CPOL_Low;                     

  SPI_InitStructure.SPI_CPHA              = SPI_CPHA_2Edge;                    

  SPI_InitStructure.SPI_NSS               = SPI_NSS_Soft; 

  SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;          

  SPI_InitStructure.SPI_FirstBit          = SPI_FirstBit_LSB;                 

  SPI_InitStructure.SPI_CRCPolynomial     = 7;  

  SPI_Init(SPI2, &SPI_InitStructure);                                          

  SPI_I2S_ITConfig(SPI2, SPI_I2S_IT_TXE, ENABLE); 

  SPI_SSOutputCmd(SPI2, ENABLE);                                               

  SPI_Cmd(SPI2, ENABLE);                                                        

  /*******************************************/

SLAVE device

  /**********************************************/

  /* Configure SPI2 as receiver               */

  /**********************************************/

  SPI_I2S_DeInit(SPI2);                                                        

  SPI_InitStructure.SPI_Direction         = SPI_Direction_2Lines_FullDuplex;    

  SPI_InitStructure.SPI_Mode              = SPI_Mode_Slave;                   

  SPI_InitStructure.SPI_DataSize          = SPI_DataSize_8b;                  

  SPI_InitStructure.SPI_CPOL              = SPI_CPOL_Low;                   

  SPI_InitStructure.SPI_CPHA              = SPI_CPHA_2Edge;                   

  SPI_InitStructure.SPI_NSS               = SPI_NSS_Soft; 

  SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;           

  SPI_InitStructure.SPI_FirstBit          = SPI_FirstBit_LSB;                 

  SPI_InitStructure.SPI_CRCPolynomial     = 7;  

  SPI_I2S_ITConfig(SPI2, SPI_I2S_IT_RXNE, ENABLE); 

  SPI_Cmd(SPI2, ENABLE);   

  /*******************************************/

---

Any suggestions/input would be greatly appreciated

0 REPLIES 0