This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

ADXL346 interface with CC2540

Other Parts Discussed in Thread: CC2540

Hello All,

Has anyone successfully interfaced CC2540 with ADXL346 accelerometer? I am having lot of trouble in interfacing over SPI. If some one has already done and dont mind sharing some hints, I will be grateful!

Thanks!

  • Here is my code: 

    #include<iocc2540.h>
    
    void accInit(void);
    void accWriteReg(short reg, short val);
    void accReadReg(short, short *);
    void spiWriteByte(short write);
    void spiReadByte(short *read, short write);
    void multiread(short, short *);
    
    short pval[6];
    
    #define WAIT_1_3US(t)                   \
        do{                                 \
            for (short i = 0; i<t; i++)     \
                asm("NOP");                 \
        }while(0)
    
    
    #define SCK             P1_5
    #define MISO            P1_7
    #define MOSI            P1_6
    
    #define CS              P2_0
    
    
    #define CS_DISABLED     1
    #define CS_ENABLED      0
    
    short test1,test2,test3;
    
    int main( void )
    {
      CLKCONCMD = 0x80;
      
      WAIT_1_3US(3000);
      
      accInit();
      
      accReadReg(0xc0,&test1);
      //accReadReg(0x80,&test1);
      
      accWriteReg(0x71,0x0b);
      accWriteReg(0x6d,0x08);
      accWriteReg(0x6e,0x80);
     //while(1){
      accReadReg(0xf1,&test1);
      accReadReg(0xed,&test2);
      accReadReg(0xee,&test3);
     //  accReadReg(0xee,&test3);
      //}
      while(P1_3!=1);
    accReadReg(0xf2,pval);
      accReadReg(0xf3,pval+1);
      accReadReg(0xf4,pval+2);
      accReadReg(0xf5,pval+3);
      accReadReg(0xf6,pval+4);
      accReadReg(0xf7,pval+5);
      
     // multiread(0xf2,pval);
      return 0;
    }
    
    //brief	Initialize SPI interface and adxl346 accelerometer
    
    
    void accInit(void)
    {
        //*** Setup USART 1 SPI at alternate location 2 ***
    
        // USART 1 at alternate location 2
        PERCFG |= 0x02;
        // Peripheral function on SCK, MISO and MOSI (P1_5-7)
        P1SEL |= 0xE0;
        // Configure CS (P2_0) output
    
        P2DIR |= 0x01;
        
        P0DIR |= 0x81;
        P0 |= 0x81;
    
        CS = CS_DISABLED;
        
        WAIT_1_3US(2);
        //*** Setup the SPI interface ***
        // SPI master mode
        U1CSR = 0x00;
          // MSB first
        U1GCR|= 0xe0;
         // SCK frequency = 1 MHz
        U1GCR |= 15;
        U1BAUD = 0;    
        
       
    }
    
    void accWriteReg(short reg, short val)
    {
        CS = CS_ENABLED;
        WAIT_1_3US(2);
        spiWriteByte(reg);
        spiWriteByte(val);
        CS = CS_DISABLED;
    }
    
    
    void accReadReg(short reg, short *pVal)
    {
        CS = CS_ENABLED;
        WAIT_1_3US(2);
        spiWriteByte(reg);
        spiReadByte(pVal, 0xFF);
        CS = CS_DISABLED;
    }
    
    
    void spiWriteByte(short write)
    {
            U1CSR &= ~0x02;                 // Clear TX_BYTE
            U1DBUF = write;
            while (!(U1CSR & 0x02));        // Wait for TX_BYTE to be set
    }
    
    void spiReadByte(short *read, short write)
    {
            U1CSR &= ~0x02;                 // Clear TX_BYTE
            U1DBUF = write;
            while (!(U1CSR & 0x02));        // Wait for TX_BYTE to be set
            *read = U1DBUF;
    }
    
    
    void multiread(short reg, short *pVal)
    {
        CS = CS_ENABLED;
        WAIT_1_3US(2);
        spiWriteByte(reg);
        spiReadByte(pVal, 0xFF);
        spiReadByte(pVal+1, 0xFF);
        spiReadByte(pVal+2, 0xFF);
        spiReadByte(pVal+3, 0xFF);
        spiReadByte(pVal+4, 0xFF);
        spiReadByte(pVal+5, 0xFF);
        CS = CS_DISABLED;
    }

    I am able to properly communicate with the ADXL346 eval break out board, but not able to do so in my own board. The schematic concerned with ADXL346 is below:

    Can some one kindly help me please?

    Thanks!

  • I would suggest you using scope to check your CS and SCLK signal is correct first.

  • hI Yikai, I have verified and they are proper, but nothing works!

    Thanks..

  • Then, you need to make sure MO is P1_6 and MI is P1_7. If your MO signal is also correct, the problem must be on ADXL346.

  • The issue was basically from the reserved pin which needs to be connected to Vs, which was left floating in our design. I shorted it with Vs now and everything works nice and clean! Thanks for your help!

    Best regards!