SPI Communication with MPU6000 on APM 2.6

Hey guys,

I have some problems with the ArduPilot APM 2.6. I am trying to read a register out of the MPU6000 chip but I just can’t get it to Work. I always get the Output “255” or binary “11111111”. I tried to change the SPI-Mode but it won’t change anything. I did it exactly as in the Tutorial on SPI - with the specifications of the MPU6000 of course - but it won’t work.
I hope you can help me. Here is my code:

#include <SPI.h>

const int slaveSelect = 53;
  

void setup () {
  
  Serial.begin(9600);
  
  pinMode(slaveSelect, OUTPUT);
  
  
  
  
  SPI.begin();
  SPI.setBitOrder(MSBFIRST);
  SPI.setClockDivider(SPI_CLOCK_DIV16);
  SPI.setDataMode(SPI_MODE2);
  delay(100);
}

void loop () {
  
  byte resultat = spi_read(0x1B | 0b10000000);
  Serial.println(resultat, BIN);
  delay(1000);

}

byte spi_read(byte read_command) {
  digitalWrite(slaveSelect, LOW);
  SPI.transfer(read_command);
  byte miso_data = SPI.transfer(0);
  digitalWrite(slaveSelect, HIGH);
  return miso_data;
}

Thanks in advance.

Cheers, Daniel.

It appears by the MPU6000 datasheet, the SPI mode is mode 3. That is page 19 here. Mode 3 is SCK HIGH with LOW pulses, and the data capture is on the rising edge of SCK.

Are you certain the SPI slave select for the MPU6000 is D53? Do you have a link to the APM 2.6 docs that show the SPI connections for the MPU6000?

edit: You are reading the gyro config register. I don't know what it is supposed to return. Try reading one of the gyro or accelerometer outputs. You can find the register numbers on pages 6 and 7 here.

Hi,

I have the schematics printed out. Don't know where I found them anymore, but it says that the Arduino Pin 53 (SS/PCINT0) leads to the /CS Pin on the MPU6000 (with a voltage converter in between of course).

I tried to read from every register on that Chip so far and I always got 255 for an answer.

Edit: I found the schematics: http://freeimu.varesano.net/sites/freeimu.varesano.net/files/forum_attachments/APM_v252_RELEASE.png

Edit: Maybe you guys know where the standard ports for SPI Communication are defined (MOSI,MISO,CLK) so that I can check whether they really are the Ports I am using ?!

I agree with SurferTim that according to the datasheet SPI mode 3 is used and not mode 2 as your code specifies. Have you changed that? What are the results?

Edit: Maybe you guys know where the standard ports for SPI Communication are defined (MOSI,MISO,CLK) so that I can check whether they really are the Ports I am using ?!

They use Port B, pins 1 to 3. I thought you're using APM 2.6 where these connections are on board? How would you use different ports?

In another forum I found a very strange solution to get it to work. I had to set PIN 40 to OUTPUT and HIGH, to "stop the barometer from holding the SPI bus" ( Look here (line 49) ). But according to the APM schematics there is nothing attached to that pin, or do i just not see it ?

Anyway, now I have some output values that that make sense ( e.g. acceleration values changing when moving the APM around ). I will try to read some other registers tomorrow to be sure that the SPI bus is now working properly.

But according to the APM schematics there is nothing attached to that pin, or do i just not see it ?

If you know where to look for: It's in the upper right of the MPU/Pressure are of the schematics. The CS of the pressure sensor chip is connected to PG1 (D40) by the way of a voltage divider.

You would have to know where to look. That is a contender for the worst schematic I have ever seen.

Thanks pylon, now I see it and it all makes sense as this is the Slave Select for another SPI slave on the Same bus. Yep SurferTim I agree it's pretty confusing. Thanks for the help guys.