cancel
Showing results for 
Search instead for 
Did you mean: 

SPI Clock problem

pranathi1091
Associate II
Posted on December 21, 2012 at 08:32

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();

}

#spi
3 REPLIES 3
Posted on December 21, 2012 at 13:40

You have to send data for the clocks to be generated. As the transmit data clocks out, the receive data clocks in. Reading the DR just clears a flags, and provides the register content, it does not initiate a transfer. SPI_I2S_ReceiveData() just reads the DR, nothing else.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
pranathi1091
Associate II
Posted on December 24, 2012 at 05:47

Thanks clive

I could get Transmit working and the clock is generated while sending data out.

How do i do with the reception. If I am correct, the recent data will be in DR register and hence i read Data register.

Could u pls correct me or let me what needs to be done for reception to work

Thanks

pranathi1091
Associate II
Posted on December 28, 2012 at 07:20

I could resolve the issue when i send and receive sequence of bytes.

I dont have any probelm in reading a single byte from Accelreometer.

But i am facing problem when i try to read multibyte data from ADXL345, which always gives 00 from all the axis.