AnsweredAssumed Answered

Hello everyone,could u please help me with a problem I got in learning the SPI of Stm32,thank you very much

Question asked by zijun.wan on Jun 5, 2015
Latest reply on Jun 9, 2015 by Bessone.Danilo
I am a Stm32 new learner.Recently I have had two Stm32 chips to communicated with eachother by using it wardware SPI.After I wrote the master and slave program and downloaded it into both stm32 chips.I used a logic analytical device to observe the wave of MOSI,MISO and clock.I found that the wave(the master date) from the Master was correct,but the data the master received from the slave was wrong.For example I want to send 0xff to the slave and the slave feedback 0x32 to the master(I can see 11111111 coming out from the MOSI,but from the MISO I saw 11001000 not 00110010).I don't know why,I checked both of my program,the polarity are the same and the mode setting is right.
here is my master program
int main(void)
{
          SPI_InitTypeDef  SPI_InitStructure;
          GPIO_InitTypeDef  GPIO_InitStructure;
       ErrorStatus HSEStartUpStatus;
          RCC_DeInit();                                                //ÅäÖÃRCCʱÖÓ//
       RCC_HSEConfig(RCC_HSE_ON);
          HSEStartUpStatus = RCC_WaitForHSEStartUp();
          if(HSEStartUpStatus == SUCCESS)
  {
    RCC_HCLKConfig(RCC_SYSCLK_Div1); 
    RCC_PCLK2Config(RCC_HCLK_Div1); 
    RCC_PCLK1Config(RCC_HCLK_Div2);
    FLASH_SetLatency(FLASH_Latency_2);
    FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
    RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
    RCC_PLLCmd(ENABLE);
    while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
    {
    }
  }
          RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
     
          RCC_APB2PeriphClockCmd(      RCC_APB2Periph_GPIOA, ENABLE );                                        //³õʼ»¯GPIO¡¡//
          GPIO_InitStructure.GPIO_Pin = (GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7);                
          GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  
          GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
          GPIO_Init(GPIOA, &GPIO_InitStructure);     
          GPIO_SetBits(GPIOA,GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7);
     
          RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE );                                        //³õʼ»¯SPI
          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_32;          
          SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;     
          SPI_InitStructure.SPI_CRCPolynomial = 7;     
          SPI_Init(SPI1, &SPI_InitStructure); 
       SPI_Cmd(SPI1, ENABLE); 
          SPI_I2S_ITConfig(SPI1, SPI_I2S_IT_RXNE, ENABLE);
          SPI1_ReadWriteByte(0xff);               
     
               while(1);
          
}
the slave program
int main(void)
{
          SPI_InitTypeDef  SPI_InitStructure;
          GPIO_InitTypeDef  GPIO_InitStructure;
       ErrorStatus HSEStartUpStatus;
          RCC_DeInit();                                                //ÅäÖÃRCCʱÖÓ//
       RCC_HSEConfig(RCC_HSE_ON);
          HSEStartUpStatus = RCC_WaitForHSEStartUp();
          if(HSEStartUpStatus == SUCCESS)
  {
    RCC_HCLKConfig(RCC_SYSCLK_Div1); 
    RCC_PCLK2Config(RCC_HCLK_Div1); 
    RCC_PCLK1Config(RCC_HCLK_Div2);
    FLASH_SetLatency(FLASH_Latency_2);
    FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
    RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
    RCC_PLLCmd(ENABLE);
    while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
    {
    }
  }
          RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
     
          RCC_APB2PeriphClockCmd(      RCC_APB2Periph_GPIOA, ENABLE );                                        //³õʼ»¯GPIO¡¡//
          GPIO_InitStructure.GPIO_Pin = (GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7);                
          GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  
          GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
          GPIO_Init(GPIOA, &GPIO_InitStructure);     
          GPIO_SetBits(GPIOA,GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7);
     
          RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE );                                        //³õʼ»¯SPI
          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_32;          
          SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;     
          SPI_InitStructure.SPI_CRCPolynomial = 7;     
          SPI_Init(SPI1, &SPI_InitStructure); 
       SPI_Cmd(SPI1, ENABLE); 
          SPI_I2S_ITConfig(SPI1, SPI_I2S_IT_RXNE, ENABLE);
          SPI1->DR=0x32;          
     
               while(1);
          




}

I want to explain why I only write SPI1->DR=0x32 in slave program.cos I readed some documents about SPI,It tells me that SPI slave will communicate with the master automatically as the clock signal coming from the master.I just want them exchange one byte data,So I gave one date to SPI->DR directively.What's wrong with my program or did I get the SPI process wrong???I am curious if SPI->DR=0xdata works,it can be worng,and I see the sample of ST,they also use SPI->DR=0xdata,but now I got a wrong MISO wave....why???
I sincerely want to get some help from you.Thank you very much
 best regards

Outcomes