AnsweredAssumed Answered

SPI communication between two STM32F3 problem

Question asked by bandari.franck on Nov 21, 2013
Latest reply on Nov 21, 2013 by bandari.franck
Hi all,
I am trying to interface two STM32F3 boards with SPI port in full duplex.
One board acts as Master and the other one as a Slave.
The Master transmit a command, the slave receives this command and then sends an array of 32 value.
The problem is when the Master receives this array, the 2 first values aren't received, i have zero instead, the other value are correct.
Below is the codes i used for both Master an Slave boards:

/*---------------- The slave Board -----------*/
int main(void)
{
   
   STM_EVAL_LEDInit(LED6);
  /* System clocks configuration ---------------------------------------------*/
  RCC_Configuration();

  /* GPIO configuration ------------------------------------------------------*/
  GPIO_Configuration();
 
  /* SPI1 Config -------------------------------------------------------------*/
  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_High;
  SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
  SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
  SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8;
  SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_LSB;
  SPI_InitStructure.SPI_CRCPolynomial = 7;
  SPI_Init(SPI1, &SPI_InitStructure);

  /* Enable SPI1 */
  SPI_Cmd(SPI1, ENABLE);

     /* Clear the RxBuffer */
  Fill_Buffer(SPI1_Buffer_Rx, RXBUFFERSIZE);
    Fill_Buffer(SPI1_Buffer_Error, RXBUFFERSIZE);
    
    while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
        CmdReceived= SPI_ReceiveData8(SPI1);
    
    switch (CmdReceived)
    {
       // Turn on LED5
                STM_EVAL_LEDOn(LED5);

    }
     
    break;
                
      /* Measure command received  */
    case CMD_Mesure:
      RxIdx=0;
      while (RxIdx <BufferSize){    
            
                /* Wait for SPI_Slave Tx buffer empty */
                while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
                 /* Send SPI_Slave data */
                SPI_SendData8(SPI1, SPI1_Buffer_Tx[RxIdx]);
                
                /* Wait for SPI1 data reception */
                while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
                
                // Read SPI1 received data
                SPI_ReceiveData8(SPI1);  /* Clear pending RXNE*/
                // Turn on LED6
                STM_EVAL_LEDOn(LED6);
        
                RxIdx++;
            }
            
      break;    
        default:
      break;    
    }
    
     while(1){}
}

/*---------------- The Master Board -----------*/

int main(void)

{

  /* Initialize LEDs available on STM32F3-Discovery board */

  STM_EVAL_LEDInit(LED3);

  STM_EVAL_LEDInit(LED4);

                STM_EVAL_LEDInit(LED5);

                STM_EVAL_LEDInit(LED6);

  /* System clocks configuration ---------------------------------------------*/

  RCC_Configuration();

  /*GPIO configuration ------------------------------------------------------*/

  GPIO_Configuration();


  /* SPI1 Config -------------------------------------------------------------*/

  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_High;

  SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;

  SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;

  SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8;

  SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_LSB;

  SPI_InitStructure.SPI_CRCPolynomial = 7;

  SPI_Init(SPI1, &SPI_InitStructure);

 

  /* Enable SPI1 */

  SPI_Cmd(SPI1, ENABLE);

                /* Clear the RxBuffer */

  Fill_Buffer(SPI1_Buffer_Rx, RXBUFFERSIZE);  

                 /* Wait for SPIy Tx buffer empty */

   while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);

    /* Send SPI command */

   SPI_SendData8(SPI1, 0x02);

                 // get data

                 RxIdx=0;

   while (RxIdx <BufferSize)

                {

                   /* send a byte */

                  while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);            

                   // Send Dummy SPI data to get back data from DR Register 

                      SPI_SendData8(SPI1,0x01);                          

                 /* In this step, the Master recieves data from the Slave*/

                     /* Wait for SPI1 data reception */

       while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);

       /* Read SPI1 received data */

       SPI1_Buffer_Rx[RxIdx] = SPI_ReceiveData8(SPI1);

        RxIdx++;

         /* Turn on LED5 */

          STM_EVAL_LEDOn(LED5);  

          }            

 while(1){}

}

The array that i sent was this one :
uint8_t SPI1_Buffer_Tx[BufferSize] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
                                      0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E,
                                      0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
                                      0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C,
                                      0x1D, 0x1E, 0x1F, 0x20};

Any help would be appreciated
Thanks


Outcomes