AnsweredAssumed Answered

SPI Clock problem

Question asked by sd.pranathi on Dec 21, 2012
Latest reply on Dec 28, 2012 by sd.pranathi
Hello ALL,

I am using stm32f2xxx.
I am programing An accelerometer which is interfaced via SPI. The problem i m facing is the clock is not always continous. My HSEValue is 8Mhz.
When i get the clcok properly i could see RXNE flag and TXE flag set and it receives valid data. But when i dont get the clock properly i see only TXE flag set.
Below is my init, read and write codes

void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA ,ENABLE);

GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_DOWN;

_InitStructure.GPIO_Pin=SPI1_SCK_PIN;
GPIO_Init(SPI1_GPIO_PORT,&GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin=SPI1_MOSI_PIN;
GPIO_Init(SPI1_GPIO_PORT,&GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin=SPI1_MISO_PIN;
GPIO_Init(SPI1_GPIO_PORT,&GPIO_InitStructure);

GPIO_PinAFConfig(GPIOA,SPI1_SCK_SOURCE,GPIO_AF_SPI1);
GPIO_PinAFConfig(GPIOA,SPI1_MOSI_SOURCE,GPIO_AF_SPI1);
GPIO_PinAFConfig(GPIOA,SPI1_MISO_SOURCE,GPIO_AF_SPI1);

/*Configure  CS pin in output pushpull mode*/
  GPIO_InitStructure.GPIO_Pin = SPI1_CS_PIN;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

  GPIO_Init(SPI1_GPIO_PORT, &GPIO_InitStructure);
}

void SPIConfiguration(void)
{

unsigned char ucdummy;
static char ucindex = 20;

unsigned char rxbuf[20];

void SPI_Config()
{
SPI_InitTypeDef SPI_InitStructure;

RCC_APB2PeriphClockCmd (RCC_APB2Periph_SPI1, ENABLE);

/*SPI initializations*/
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_2;
SPI_InitStructure.SPI_FirstBit=SPI_FirstBit_MSB;
//SPI_InitStructure.SPI_CRCPolynomial=7;
SPI_InitStructure.SPI_CRCPolynomial=0;

SPI_Init(SPI1,&SPI_InitStructure);
SPI_SSOutputCmd(SPI1,ENABLE);

SPI_Cmd(SPI1,ENABLE);
}

uint8_t SPIXLWriteData(uint8_t Add, uint16_t Data)
{
uint32_t TimeOut;

while ((SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_BSY)))
{
if (TimeOut--==0)
{
return SPI_ERROR;
}
}
CS_LOW();
SPI_I2S_SendData(SPI1, ADXL345_W|Add);        
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);        
ClrRcvBuf();        
        
SPI_I2S_SendData(SPI1, Data);        
ClrRcvBuf();        
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_BSY) == SET);        
CS_HIGH();

return 1;
}


uint8_t SPIReadData(uint8_t RegisterAddress, uint8_t NoOfRgisters)
{
uint32_t TimeOut;
uint16_t RxBuffer;
/* Select Slave*/
CS_LOW();
/*Make sure the bus is not busy*/
while ((SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_BSY)))
{
if (TimeOut--==0)
{
return SPI_TIMEOUT_ERROR;
}
}
//RxBuffer = SPI_I2S_ReceiveData(SPI1);
SPI_I2S_SendData(SPI1, XL345_R|RegisterAddress);

ClrRcvBuf();
RxBuffer=SPI_I2S_ReceiveData(SPI1);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);        
//dummy byte*/        
SPI_I2S_SendData(SPI1, 0x00);                       
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);      the   
RxBuffer = SPI_I2S_ReceiveData(SPI1);        
 while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_TXE) != SET);       
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_BSY) == SET);         
     
        
CS_HIGH();

return  RxBuffer;
}

void SPIXLReadSequence(uint8_t reg,uint8_t count)
{        
uint8_t i = 0;
         
CS_LOW();              
SPI_I2S_SendData(SPI1, ADXL345_R | ADXL345_MB | reg);               
ClrRcvBuf();              
 while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);                       
SPI_I2S_SendData(SPI1, 0xFF);                
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);                
 data[i]= SPI_I2S_ReceiveData(SPI1);                                 
 i++;       
  }       
 while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_BSY) == SET);        
CS_HIGH();

}

Outcomes