AnsweredAssumed Answered

SPI Full Duplex looping with SPI_CPHA_2Edge

Question asked by valdez.jess on Aug 18, 2012
I am trying to receive data over full duplex SPI but STM32 does not line up clock and data if I configure in  SPI_CPHA_2Edge. However if I configure in SPI_CPHA_1Edge, everything works fine. My slave however sends data in 2Edge. Does anybody have a workaround on this?

void configure_spi(void)
{
  //Init GPIO structure 
  GPIO_InitTypeDef GPIO_InitStructure;
  //Init SPI structure 
  SPI_InitTypeDef SPI_InitStructure; 
  
   //Enable SPI clocks  
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1,ENABLE); 
  
  RCC_PCLK2Config(RCC_HCLK_Div1); //DEFAULT WAS Div2
  
  /* Enable GPIO clocks */
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
  
  //Set GPIOs 
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; 
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; 
  //GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7; 
  //GPIO_Init(GPIOA, &GPIO_InitStructure); 
  
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; //SS
  GPIO_Init(GPIOA, &GPIO_InitStructure); 
  
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //CLK
  GPIO_Init(GPIOA, &GPIO_InitStructure); 
  
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; //MISO
  GPIO_Init(GPIOA, &GPIO_InitStructure); 
  
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; //MOSI
  GPIO_Init(GPIOA, &GPIO_InitStructure); 
    


  /* Connect SPI pins to AF5 */  
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_SPI1); //CLK
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_SPI1); //MOSI
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_SPI1); //MISO


  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_Hard; 
  SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; 
  SPI_InitStructure.SPI_CRCPolynomial = 7; 
  
  //SPI_BaudRatePrescaler_8 = 6.7MHz
  //SPI_BaudRatePrescaler_4 = 13.xMHz
  //SPI_BaudRatePrescaler_2 = 26.8MHz
  SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8; 
  
  //SPI1- for CLK, DATA, CS(DIR) 
  SPI_Init(SPI1,&SPI_InitStructure);
  //SPI_TIModeCmd(SPI1, ENABLE);
  
  //SPI_I2S_ITConfig(SPI1, SPI_I2S_IT_RXNE, ENABLE); 
  SPI_SSOutputCmd(SPI1,ENABLE); 
  SPI_Cmd(SPI1,ENABLE); 
}

uint8_t mip_Send_Receive_Byte(uint8_t byte)
{
  /*!< Loop while DR register in not emplty */
  while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);


  /*!< Send byte through the SPI1 peripheral */
  SPI_I2S_SendData(SPI1, byte);


  /*!< Wait to receive a byte */
  while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);


  /*!< Return the byte read from the SPI bus */
  return SPI_I2S_ReceiveData(SPI1);
}


Outcomes