SPI problem

Code:

local z=0;

server.log(“started”);
hardware.spi189.configure(4000);

function spi() {

hardware.spi189.write("\\x0B,\\x08");   
z = hardware.spi189.read(32);
server.show(z);   

}

imp.configure(“SPI”, [], []);
spi();

What I am trying to do is read an accelerometer. The acceleromter (adxl362) uses spi, and runs as a slave. The CS pin is grounded low on the accelerometer for now. The adxl362 needs 1Mhz - 5Mhz so i put 4MHZ.

So i write 0x0A command which tells the adxl to read, and 0x08 is the x-axis register. Then i read. But what i am getting back is “null” on the planner.

Anything I’m doing wrong on the imp?

Thanks!

Accelerometer datasheet:

http://www.analog.com/static/imported-files/data_sheets/ADXL362.pdf

Any help would be really nice thanks guys!

The current SPI API can only be used byte at a time. Something like this should do the burst read you’re looking for:

`// set CS line low
hardware.pinx.write(0); // depends on where you put this

// Send command and address
hardware.spi189.write("\x0b");
hardware.spi189.read(1); // discard
hardware.spi189.write("\x08");
hardware.spi189.read(1); // discard

// Read X/Y/Z
local s="";
for(local a=0;a<3;a++) {
hardware.spi189.write(0); // junk byte write
s+=hardware.spi189.read(1);
}

// set CS line high
hardware.pinx.write(1);

server.log(format(“x/y/z=%02x/%02x/%02x”,s[0],s[1],s[2]));
`

You must write a byte every time you want to read a byte right now; the newer API lets you just read (ie, the imp will generate zeros and send them automatically if you don’t care what you’re writing) or just write (the imp will read and discard received values if you don’t care what’s coming back).

Thanks for your help Hugo !!
I was still having a hard time so I decided to use an analog accelerometer instead :slight_smile:
The adc in the imp was extremely easy to get working !

Thanks again!