issue while reading data from SPI

1 post / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

i was trying to read  data from ADXL345 by using SPI serial communication,i configured ADXL is slave device and SAMC21 is master device, when iam trying to read device ID from ADXL in SAMC21 by using SPI,it is giving every time same value (255) even iam try read any another register also it is giving same value (255). and if i was read multiple time, in that time only iam getting value(255) otherwise iam getting NULL. i studied for SPI we have to read multiple times that's iam reading multiple times 

can any one tell me why it is giving the same value and how can i solve this issue.

 

#define SPI_CLK_FREQ 4000000
#define SPI_BAUD 1000000

char tempvar=0;
uint8_t k,j;
 

 

void SPI_configure(Sercom* SERCOM_NO)
{
    SERCOM_NO->SPI.CTRLA.bit.ENABLE = 0;
    while(SERCOM_NO->SPI.SYNCBUSY.bit.ENABLE);
    
    SERCOM_NO->SPI.CTRLA.bit.SWRST = 0;
    while(SERCOM_NO->SPI.SYNCBUSY.bit.SWRST);
    
    SERCOM_NO->SPI.CTRLA.reg |=SERCOM_SPI_CTRLA_MODE(0x03); ///master
    SERCOM_NO->SPI.CTRLA.reg |= SERCOM_SPI_CTRLA_CPHA | SERCOM_SPI_CTRLA_CPOL; //CPHA=1 and CPOL=1
    SERCOM_NO->SPI.CTRLA.reg |= SERCOM_SPI_CTRLA_DIPO(0) | SERCOM_SPI_CTRLA_DOPO(1);
    SERCOM_NO->SPI.CTRLA.reg |= SERCOM_SPI_CTRLA_FORM(0);
    SERCOM_NO->SPI.CTRLA.reg |= SERCOM_SPI_CTRLA_DORD;
    SERCOM_NO->SPI.CTRLA.reg |= SERCOM_SPI_CTRLA_IBON;
    
    uint16_t BAUD_REG = ((float)SPI_CLK_FREQ / (float)(2 * SPI_BAUD)) - 1;    //Calculate BAUD value
    SERCOM_NO->SPI.BAUD.reg =    SERCOM_SPI_BAUD_BAUD(BAUD_REG);
    SERCOM_NO->SPI.CTRLA.reg |= SERCOM_SPI_CTRLA_RUNSTDBY;

    SERCOM_NO->SPI.CTRLB.reg |= SERCOM_SPI_CTRLB_RXEN | SERCOM_SPI_CTRLB_CHSIZE(0)|SERCOM_SPI_CTRLB_SSDE;
    while(SERCOM_NO->SPI.SYNCBUSY.bit.CTRLB);
    
    SERCOM_NO->SPI.CTRLB.reg |= SERCOM_SPI_CTRLB_MSSEN;
    while(SERCOM_NO->SPI.SYNCBUSY.bit.CTRLB);    
    
    SERCOM_NO->SPI.CTRLA.bit.ENABLE =1;
    while(SERCOM_NO->SPI.SYNCBUSY.bit.ENABLE);    
}

 

 

void readfun(Sercom* SERCOM_NO)
{

    for( k=3;k>0;k--)
    {
    while(SERCOM_NO->SPI.INTFLAG.bit.DRE==0);
    SERCOM_NO->SPI.DATA.reg=(1<<7|0X00);     /* for read operation*/

   while(SERCOM_NO->SPI.INTFLAG.bit.RXC==0);

    tempvar=SERCOM_NO->SPI.DATA.reg;
    }
  
}