Pages: [1]   Go Down
Author Topic: Switching between I2C and SPI  (Read 616 times)
0 Members and 1 Guest are viewing this topic.
Colorado
Offline Offline
Edison Member
*
Karma: 47
Posts: 1562
Reviving dead brain cells with Arduinos.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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)?
« Last Edit: November 11, 2013, 05:22:41 am by KirAsh4 » Logged

Valencia, Spain
Offline Offline
Faraday Member
**
Karma: 143
Posts: 5301
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

No, I don't answer questions sent in private messages (but I do accept thank-you notes...)

Austin, TX
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6106
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Colorado
Offline Offline
Edison Member
*
Karma: 47
Posts: 1562
Reviving dead brain cells with Arduinos.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: November 11, 2013, 11:45:54 am by KirAsh4 » Logged

Austin, TX
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6106
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

0
Offline Offline
Shannon Member
****
Karma: 199
Posts: 11671
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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).
Logged

[ I won't respond to messages, use the forum please ]

Colorado
Offline Offline
Edison Member
*
Karma: 47
Posts: 1562
Reviving dead brain cells with Arduinos.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Austin, TX
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6106
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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. 
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Offline Offline
Full Member
***
Karma: 1
Posts: 186
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: