Pages: [1]   Go Down
Author Topic: Simultaneous i2c and SPI not working on Due  (Read 665 times)
0 Members and 1 Guest are viewing this topic.
Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm having trouble communicating using both i2c and SPI in the same sketch. I've been trying to use the Arduino Due to gather measurements from an ADXL345 (using SPI), an L3G4200 (using SPI) and an HMC5883L (using i2c). I have a separate sketch that only communicates with the HMC5883 and everything works fine and I receive measurements. But in my main sketch, which includes both the Wire.h and SPI.h libraries, the communication over i2c stops working. I have checked the SCL and SDA pins and both appear to be stuck high. Has anyone else experienced similar behaviour?
Logged

Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I created a minimal working example and that seemed to sort out my issue. Something else in my original code must have been causing the issue, not the Due's libraries.

Just for the heck of it here is my minimal example in case it is useful for anyone.

Code:
/* minimal working example using both i2c and spi
  hardware:
  - Arduino Due
  - ADXL345 (accel)
  - HMC5883L (mag)
  - L3G4200 (gyro)
*/

#include <Wire.h>
#include <SPI.h>

#define MAG_ADDRESS ((char) 0x1E) // i2c device address
#define ACCEL_SS_PIN 10 // SPI device slave select pin
#define GYRO_SS_PIN 52 // second SPI device pin
#define READ   0x80 // general SPI transfer bits
#define WRITE  0x00
#define MULTI  0x40
#define SINGLE 0x00

void setup() {
  Serial.begin(115200);
 
  // initialize i2c
  Wire.begin();

  // initialize first device SPI
  SPI.begin(ACCEL_SS_PIN);
  SPI.setBitOrder(ACCEL_SS_PIN, MSBFIRST);
  SPI.setDataMode(ACCEL_SS_PIN, SPI_MODE3);
 
  // initialize second device SPI
  SPI.begin(GYRO_SS_PIN);
  SPI.setBitOrder(GYRO_SS_PIN, MSBFIRST);
  SPI.setDataMode(GYRO_SS_PIN, SPI_MODE0);
 
  delay(500);
 
 
  // test i2c by getting the device identifier registry
  Wire.beginTransmission(MAG_ADDRESS);
  Wire.write(0x0A); // identification register, should equal 0x48
  Wire.endTransmission();
 
  Wire.beginTransmission(MAG_ADDRESS);
  Wire.requestFrom(MAG_ADDRESS,1);
  uint8_t mag_name = Wire.read();
  Wire.endTransmission();
  Serial.print(mag_name, HEX); Serial.println(" value recieved, should be 0x48");
 
 
  // test SPI by getting the first device identifier registry
  SPI.transfer(ACCEL_SS_PIN, READ | SINGLE | 0x00, SPI_CONTINUE);
  uint8_t accel_name = SPI.transfer(ACCEL_SS_PIN, 0x00);
  Serial.print(accel_name, HEX); Serial.println(" value recieved, should be 0xE5");
 
 
  // test SPI by getting the second device identifier registry
  SPI.transfer(GYRO_SS_PIN, READ | SINGLE | 0x0F, SPI_CONTINUE);
  uint8_t gyro_name = SPI.transfer(GYRO_SS_PIN, 0x00);
  Serial.print(gyro_name, HEX); Serial.println(" value recieved, should be 0xD3");
 
}

void loop() {
 
}
Logged

Pages: [1]   Go Up
Jump to: