fopetesl
Posts: 44
Joined: Tue Oct 20, 2015 8:08 am

ADXL345 Bad Response?

Mon Jan 04, 2016 6:42 pm

RbPi2 to ADXL345.jpg
Hardware connection
RbPi2 to ADXL345.jpg (57.82 KiB) Viewed 2059 times
Hi, I'm using a Pi2 to try and gather accelerometer data from an Analog Devices ADXL345.
I'm using a propriety board made by Analog Devices not a Chinese one.
It's connected to the Pi as per RbPi2 to ADXL345.jpg attached.
I'm running this

Code: Select all

#include <bcm2835.h>
#include <stdio.h>

int main(int argc, char **argv)
{
    
    if (!bcm2835_init())
        return 1;
    
    bcm2835_spi_begin();
    bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST);      // The default
    bcm2835_spi_setDataMode(BCM2835_SPI_MODE0);                   // The default
    bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_65536); // The default
    bcm2835_spi_chipSelect(BCM2835_SPI_CS0);                      // The default
    bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW);      // the default
    
    char buf[] = { 0x80, 0x00, 0x00, 0x00}; // Read DeviceID of ADXL345
    bcm2835_spi_transfern(buf, sizeof(buf));
    // buf will now be filled with the data that was read from the slave
    printf("Device ID: %02X %02X %02X %02X \n", buf[0], buf[1], buf[2], buf[3]);

//    delay(1000);

    char buf2[] = { 0x2D, 0x02, 0x00}; // Setup 0x2d ( POWER_CTL Reg ) for Measure
    bcm2835_spi_transfern(buf2, sizeof(buf2)); // (B7 RESET == Write)
    printf("Setup response is: %02X %02X %02X %02X \n", buf2[3], buf2[2], buf2[1]);

    delay(1000);

    char buf3[] = { 0xB2, 0x00, 0x00}; // Read X0 Reg 0x32 (B7 SET == Read)
    bcm2835_spi_transfern(buf3, sizeof(buf3));
    printf("X Axis is: %02X %02X %02X \n", buf3[3], buf3[2], buf3[1]);

//    delay(1000);

    bcm2835_spi_end();
    return 0;
}
The response is

Code: Select all

~/Projects/ADXL $ sudo ./SPIx
Device ID: 00 00 00 00
Setup response is: F2 F2 F2 F2
X Axia is: 00 00 00
I made the Read Device ID into a four byte buffer assuming I was missing something.
Same idea with Setup Response.
The two attached oscillograms confirm the responses I get.... Edit: cannot attach 64kb max makes them too small to view.

Are these F2 responses an error code?

fopetesl
Posts: 44
Joined: Tue Oct 20, 2015 8:08 am

Re: ADXL345 Bad Response?

Tue Jan 05, 2016 12:32 pm

Any help appreciated here.

Do I have a faulty unit or is my code bad somewhere?

User avatar
joan
Posts: 14935
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: ADXL345 Bad Response?

Tue Jan 05, 2016 12:36 pm

Post a photo of your set-up. The diagram shows what you intended to do. A photo will show us what you have done.

If you configure the device as I2C rather than SPI you could try http://abyz.co.uk/rpi/pigpio/examples.h ... _adxl345_c

fopetesl
Posts: 44
Joined: Tue Oct 20, 2015 8:08 am

Re: ADXL345 Bad Response?

Tue Jan 05, 2016 3:16 pm

Hmmm. Cannot upload files bigger than 64Kb...
So pics are here: http://postimg.org/gallery/3hz3n47s2/74ad64b5/

fopetesl
Posts: 44
Joined: Tue Oct 20, 2015 8:08 am

Re: ADXL345 Bad Response?

Tue Jan 05, 2016 3:18 pm

joan wrote:If you configure the device as I2C rather than SPI you could try http://abyz.co.uk/rpi/pigpio/examples.h ... _adxl345_c
If necessary I'll do as you suggest.
Reason I have gone for SPI is bandwidth - I will need as much data as fast as possible.
I read I2C is much slower?

User avatar
joan
Posts: 14935
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: ADXL345 Bad Response?

Tue Jan 05, 2016 3:28 pm

I2C defaults to 100 kbps on the Pi, so SPI can be much faster.

Some of the modules require that a line be strapped high or low to determine whether I2C or SPI is being used. How does your module decide?

fopetesl
Posts: 44
Joined: Tue Oct 20, 2015 8:08 am

Re: ADXL345 Bad Response?

Tue Jan 05, 2016 3:48 pm

joan wrote:I2C defaults to 100 kbps on the Pi, so SPI can be much faster.

Some of the modules require that a line be strapped high or low to determine whether I2C or SPI is being used. How does your module decide?
If CS is tied high, i.e. strapped to Vdd, the ADXL34x is held into I2C mode otherwise it 'sees' SPI when CS toggles low and CLK + Data(on MOSI/SDA) :
ADXL SPI 4Wire Mode.jpg
From ADXL Datasheet
ADXL SPI 4Wire Mode.jpg (27.08 KiB) Viewed 1909 times
I've also uploaded the two oscillograms: http://postimg.org/gallery/39dui4rh6/7f7fb448/
They are the same signals, just different timebase.

User avatar
joan
Posts: 14935
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: ADXL345 Bad Response?

Tue Jan 05, 2016 4:13 pm

Sorry, probably worried you for no reason.

I've just hooked up an ADXL345 in SPI mode and I get the same result.

Code: Select all

harry ~ $ sudo pigpiod
harry ~ $ pigs spio 1 50000 0
0
harry ~ $ pigs spix 0 0x80 0 0 0
4 242 242 242 242
harry ~ $ pigs -x spix 0 0x80 0 0 0
4 f2 f2 f2 f2
harry ~ $ pigs -x spix 0 0x80 0 0 0
4 f2 f2 f2 f2
harry ~ $ pigs -x spix 0 0x80 0 0 0
4 f2 f2 f2 f2
harry ~ $ pigs -x spix 0 0x80 0 0 0
4 f2 f2 f2 f2
adxl345-spi.png
adxl345-spi.png (44.16 KiB) Viewed 1888 times

fopetesl
Posts: 44
Joined: Tue Oct 20, 2015 8:08 am

Re: ADXL345 Bad Response?

Tue Jan 05, 2016 4:53 pm

joan wrote:Sorry, probably worried you for no reason.

I've just hooked up an ADXL345 in SPI mode and I get the same result.
Interesting. I only get F2 from DEVID request the very first run, there after (until cold reset of Pi) I get 00.

The F2 response is constant for every run when attempting to set POWER_CTL register.

If we both get an invalid response then it's unlikely to be a hardware issue.

The data sheet states the DEVID should be 0xE5 on reset so either the data sheet is incorrect or we are both have timing and/or software issues.

Is it a bcm library issue?

I looked at driving the ADXL using IOCTL method but I don't have the confidence I can succeed.

User avatar
joan
Posts: 14935
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: ADXL345 Bad Response?

Tue Jan 05, 2016 5:07 pm

I'm using pigpio. pigpio is similar to the bcm2835 library in that both talk directly to the SPI hardware, bypassing the Linux SPI driver. wiringPi uses the Linux SPI driver so may behave differently.

Let me have a read of the ADXL345 manual and try some tests.

User avatar
joan
Posts: 14935
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: ADXL345 Bad Response?

Tue Jan 05, 2016 5:16 pm

That was a RTFM moment.

It needs to be in SPI mode 3.

Page 15.

"and the timing scheme follows clock polarity (CPOL) = 1 and clock phase (CPHA) = 1. "

Code: Select all

harry ~ $ pigs spic 0
harry ~ $ pigs spio 1 50000 3
0
harry ~ $ pigs -x spix 0 0x80 0 0 0
4 e5 e5 e5 e5
harry ~ $ pigs -x spix 0 0x80 0 0 0
4 e5 e5 e5 e5
harry ~ $ pigs -x spix 0 0x80 0 0 0
4 e5 e5 e5 e5

fopetesl
Posts: 44
Joined: Tue Oct 20, 2015 8:08 am

Re: ADXL345 Bad Response?

Tue Jan 05, 2016 5:31 pm

So that rules out the bcm library unless both have an unlikely bug.

Interesting to see what the answer will be.

I said earlier that I bought a pukka Analog Devices board but I begin to wonder whether ALL are made in China.
The Chinese eBay boards have almost identical ADXL idents so apart from the ~£20 difference perhaps they are the same.

>> this was written before your Eureka! moment. More to follow...

fopetesl
Posts: 44
Joined: Tue Oct 20, 2015 8:08 am

Re: ADXL345 Bad Response?

Tue Jan 05, 2016 5:55 pm

joan wrote:That was a RTFM moment.

It needs to be in SPI mode 3.

Page 15.

"and the timing scheme follows clock polarity (CPOL) = 1 and clock phase (CPHA) = 1. "
Fantastic!! :o :o
I must have read up on SPI modes yonks of times as well as this page 15 and it didn't click.

Reading wikipedia now makes less sense than I thought it did
At CPOL=1 the base value of the clock is one (inversion of CPOL=0), i.e. the active state is 0 and idle state is 1.
For CPHA=0, data are captured on clock's falling edge and data is output on a rising edge.
For CPHA=1, data are captured on clock's rising edge and data is output on a falling edge.
what hadn't registered is that BOTH clock edges are significant.
I fell into the trap of, "Data valid on clock rising edge", end of story.

However, it needs to be noted that there is more than one version of SPI Modes, e.g. "NCPHA is the inversion of CPHA".

I'll recode and come back with result.

fopetesl
Posts: 44
Joined: Tue Oct 20, 2015 8:08 am

Re: ADXL345 Bad Response?

Tue Jan 05, 2016 6:13 pm

Now we're humming :)
Works great so far

Code: Select all

#include <bcm2835.h>
#include <stdio.h>

int main(int argc, char **argv)
{
    
    if (!bcm2835_init())
        return 1;
    
    bcm2835_spi_begin();
    bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST);      // The default
    bcm2835_spi_setDataMode(BCM2835_SPI_MODE3);               // NOT the default. Required to at least initialise ASDX34x
    bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_65536); // The default
    bcm2835_spi_chipSelect(BCM2835_SPI_CS0);                      // The default
    bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW);      // the default
    
    char buf[] = { 0x80, 0x00}; // Read DeviceID of ADXL345
    bcm2835_spi_transfern(buf, sizeof(buf));
    // buf will now be filled with the data that was read from the slave
    printf("Device ID: %02X \n\n", buf[0]);

    char buf2[] = { 0x2D, 0x08, 0x00, 0x00}; // Setup 0x2d ( POWER_CTL Reg ) for Measure
    bcm2835_spi_transfern(buf2, sizeof(buf2)); // (B7 RESET == Write)

    delay(1000);
    int i;
 for( i = 0; i < 10; i++) {

    char buf4[] = { 0xB3, 0x00, 0x00}; // Read X0 Reg 0x33 (B7 SET == Read)
    bcm2835_spi_transfern(buf4, sizeof(buf4));
    printf("X Axis is: %02X:", buf4[1] );
    char buf3[] = { 0xB2, 0x00, 0x00}; // Read X0 Reg 0x32 (B7 SET == Read)
    bcm2835_spi_transfern(buf3, sizeof(buf3));
    printf("%02X \n", buf3[1] );

 }
    bcm2835_spi_end();
    return 0;
}
And the result

Code: Select all

pi@raspberrypi ~/Projects/ADXL $ sudo ./SPIx
Device ID: E5

X Axis is: 00:F6
X Axis is: 00:F7
X Axis is: 00:F7
X Axis is: 00:F5
X Axis is: 00:F6
X Axis is: 00:F6
X Axis is: 00:F7
X Axis is: 00:F8
X Axis is: 00:F8
X Axis is: 00:F7
pi@raspberrypi ~/Projects/ADXL $ 
Marvellous ;)
Thanks (many) for the help.
Hope someone else may have their problem solved with this also.

Return to “Beginners”