Go Down

Topic: Switching between I2C and SPI (Read 763 times) previous topic - next topic

KirAsh4

Nov 11, 2013, 10:43 am Last Edit: Nov 11, 2013, 11:22 am by KirAsh4 Reason: 1
I'm working on a design that incorporates an ADXL345 accelerometer.  It's able to work either through I2C or SPI.  What I'm trying to figure out is if there is a way to make that selectable by the end-user, perhaps by means of setting a jumper.  For example, if the user wants to use the accelerometer as an I2C device they set a jumper or if they want to use it as an SPI device, they don't set the jumper (or move it between three pins, so that when pin 1 and 2 are shorted it's in one mode and when pins 2 and 3 are shorted, it's in the other.)

Note, this is *not* meant to be a running change.  It's meant to be configured prior to turning the whole unit on and programming it.  So is there some way to do this?  Some way to switch how it communicates?

Relevant info on the ADXL345:
For SPI communications (4-wire), the pins are wired as follows:
Quote
CS (pin 7) -> Arduino SS
SDI (pin 13) -> Arduino MOSI
SDO (pin 12) -> Arduino MISO
SCLK (pin 14) -> Arduino SCK


For I2C communications, the pins are wired as follows:
Quote
CS (pin 7) -> VCC
SDA (pin 13) -> Arduino SDA
SCL (pin 14) -> Arduino SCL


Notice how in both setups, pins 7, 13, and 14 are used.  If pin 7 is tied to VCC, the device will be in I2C mode, and if it's tied to SS, it'll work in SPI (4-wire) mode.

I can probably do this with a mechanical switch, but I'm hoping for a simpler (for the end-user) method, just using a jumper to switch things around.  Much like there are some devices out there that have a jumper on them to switch between 3.3V or 5V operations.

So, help anyone?  Schematic?  Parts (if any)?

fungus

You can do it if you implement I2C in software on pins 13/14

Fortunately I2C master is quite easy to do. I bet there's even a library for it if you google "softi2c" or something like that.
No, I don't answer questions sent in private messages (but I do accept thank-you notes...)

James C4S


Relevant info on the ADXL345:
For SPI communications (4-wire), the pins are wired as follows:
Quote
CS (pin 7) -> Arduino SS
SDI (pin 13) -> Arduino MOSI
SDO (pin 12) -> Arduino MISO
SCLK (pin 14) -> Arduino SCK


For I2C communications, the pins are wired as follows:
Quote
CS (pin 7) -> VCC
SDA (pin 13) -> Arduino SDA
SCL (pin 14) -> Arduino SCL

Where are you getting these pins outs from?
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

KirAsh4

#3
Nov 11, 2013, 05:38 pm Last Edit: Nov 11, 2013, 05:45 pm by KirAsh4 Reason: 1
Data sheet.  It can also be configured as a 3-wire SPI device but I'm not interested in that.  Just the 4-wire setup or I2C.

James C4S

What datasheet?  The ADXL345 is a chip that doesn't know anything about an Arduino, so why would its datasheet have Arduino pin connections listed?
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

MarkT

Its not actually SPI, IIRC, since you cannot share the SPI pins with another device
(true SPI devices ignore SCLK,MOSI when CS is high, and set MISO high-Z).
[ I won't respond to messages, use the forum please ]

KirAsh4

True, no device knows about an Arduino, or RPi, or MS430.  However, they do know whatever pins they need for communication, just like any controller also knows it's own pins for whatever bus.

The datasheet clearly explains how to wire it up for either SPI 4-wire, SPI 3-wire, or I2C.  Knowing that, and knowing the different pins on whatever controller, including Arduino, you get the pin mapping I provided above.

However, it makes absolutely no difference what controller is being used, not for the purpose of my question.  I could've just as easily said it's being connected to pins John, David, and Paul in one configuration and John, David, Paul, and Matthew in another.  All I wanted to know was if there is a way to mechanically configure which configuration it's using.

But it's obvious to me that I'm way too dumb for the likes of JamesCS4.  So I thank you for wasting my time, sorry for having wasted yours.  I already got my answer elsewhere with the same exact  original post.  Thanks for reminding me why I stopped using these forums.

James C4S


True, no device knows about an Arduino, or RPi, or MS430.  However, they do know whatever pins they need for communication, just like any controller also knows it's own pins for whatever bus.

The point I was trying to make is that when I asked you where the pinout listed came from, you said "the datasheet."   Either you are using a break-out board of some type, you made up the Arduino pins assignments yourself, or you got them from some existing tutorial/library.  That was not at all clear, because you are leaving out critical information.  What's calling out the Arduino pins?

If you're using a break-out board or library, a link to the board will make it possible to answer your question.

The Arduino's I2C pins aren't on pins 13 and 14.  So like fungus said, you'd have to implement software I2C instead of the hardware based I2C in the ATmega chips.

Reading the AXDL345 datasheet, yes it does look possible to make a "jumper" to configure the chip for either communication.  Apparently tieing CS to Vdd forces the chip into I2C mode. 
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

sirch

I'm interested to know why you would want to make it selectable? Seems like a fair bit of effort, what is the benefit?

Go Up