Hello everyone,
I have a LSM6DS3 sensor connected via I2C that works perfectly. Nowadays I'm working on a new project based on SPI protocol but I cannot establish any communication with the sensor.
Facts:
1) I use MODE3 because the following is mentioned in its datasheet
"Those lines are driven at the falling edge of SPC and should be captured at the rising edge of SPC."
2) When reading from sensor, MSB should be '1'. I copy from datasheet
"bit 0: READ bit. The value is 1.
bit 1-7: address AD(6:0). This is the address field of the indexed register.
bit 8-15: data DO(7:0) (read mode). This is the data that will be read from the device (MSb first).
bit 16-...: data DO(...-8). Further data in multiple byte reads."
3) The simplest communication is via register 0x0F which is tagged as WHO_AM_I and should return 0x69.
"WHO_AM_I (0Fh).
Who_AM_I register (r). This register is a read-only register. Its value is fixed at 69h"
4) I use a barebone 328P (bootloaded as Lilypad 8MHz with no external resonator) on a breadboard.
Only SPI(D10-D13, vcc, gnd) and USART(tx, rx, vcc, gnd) pins are used at this timepoint.
5) I have no access to an oscilloscope neither to a logic analyzer
6) Using Adafruit's library is depreciated.
So here I am:
#include <SPI.h>
void setup(){
Serial.begin(9600);
setup_spi();
}
void loop(){}
void setup_spi(){
const uint8_t WHO_ADR = (1<<7)|0x0F;
const uint8_t LSM_SSP = 10;
uint8_t lsm = 0; //only for debugging
uint8_t raw = 0;
Serial.print(F("SPI start\n"));
pinMode(LSM_SSP, OUTPUT);
digitalWrite(LSM_SSP, HIGH);
SPI.begin();
SPI.beginTransaction(SPISettings(250000, MSBFIRST, SPI_MODE3));
digitalWrite(LSM_SSP, LOW );
lsm = SPI.transfer(WHO_ADR);
raw = SPI.transfer(WHO_ADR);
digitalWrite(LSM_SSP, HIGH);
SPI.endTransaction();
SPI.end();
Serial.print(F("reg_address")); Serial.print(F("\t"));
Serial.print(WHO_ADR, HEX); Serial.print(F("\t"));
Serial.print(WHO_ADR, BIN); Serial.print(F("\n"));
Serial.print(F("should_get")); Serial.print(F("\t"));
Serial.print(0x69, HEX); Serial.print(F("\t"));
Serial.print(0x69, BIN); Serial.print(F("\n"));
Serial.print(F("debug_lsm")); Serial.print(F("\t"));
Serial.print(lsm, HEX); Serial.print(F("\t"));
Serial.print(lsm, BIN); Serial.print(F("\n"));
Serial.print(F("true_raw")); Serial.print(F("\t"));
Serial.print(raw, HEX); Serial.print(F("\t"));
Serial.print(raw, BIN); Serial.print(F("\n"));
Serial.print(F("true_buffer")); Serial.print(F("\t"));
Serial.print(SPDR, HEX); Serial.print(F("\t"));
Serial.print(SPDR, BIN); Serial.print(F("\n"));
Serial.print(F("SPI End\n\n"));
while(true); //only for debugging
}
and what I get is:
SPI start
reg_address 8F 10001111
should_get 69 1101001
debug_lsm FF 11111111
true_raw 7F 1111111
true_buffer 7F 1111111
SPI End
i) I've tried different speed from 4MHz to 62.5KHz, according to CLOCK_DIVx pattern.
ii) When I tried swapping MISO with MOSI I got only 0xFF.
iii) I've even tried using MODE0-MODE2 with no positive result.
iv) Even when I've tried the following code my AVR have frozen. while(!(SPSR & (1 << SPIF)));
What am I missing?

