nravanelli
Posts: 20
Joined: Mon Dec 08, 2014 4:05 am

I2C and SPI at same time?

Mon Jan 12, 2015 1:58 am

I have yet to find the answer to this.... Would I be able to use I2C and SPI communication on the Pi simultaneously? Reason: I have a DeltaSigmaPi (https://www.abelectronics.co.uk/product ... a-Sigma-Pi) which works of I2C and am looking into a Pi TFT screen which works over SPI... would this cause any problems? They would be over different pins... correct?

User avatar
mikronauts
Posts: 2722
Joined: Sat Jan 05, 2013 7:28 pm
Contact: Website

Re: I2C and SPI at same time?

Mon Jan 12, 2015 2:25 am

Yes, you can use SPI and I2C at the same time.
nravanelli wrote:I have yet to find the answer to this.... Would I be able to use I2C and SPI communication on the Pi simultaneously? Reason: I have a DeltaSigmaPi (https://www.abelectronics.co.uk/product ... a-Sigma-Pi) which works of I2C and am looking into a Pi TFT screen which works over SPI... would this cause any problems? They would be over different pins... correct?
http://Mikronauts.com - home of EZasPi, RoboPi, Pi Rtc Dio and Pi Jumper @Mikronauts on Twitter
Advanced Robotics, I/O expansion and prototyping boards for the Raspberry Pi

User avatar
Richard-TX
Posts: 1549
Joined: Tue May 28, 2013 3:24 pm
Location: North Texas

Re: I2C and SPI at same time?

Wed Jan 14, 2015 3:59 pm

I2C and SPI are on different pins and yes you can use both at the same time. I have done it myself.
Richard
Doing Unix since 1985.
The 9-25-2013 image of Wheezy can be found at:
http://downloads.raspberrypi.org/raspbian/images/raspbian-2013-09-27/2013-09-25-wheezy-raspbian.zip

Donny3000
Posts: 2
Joined: Fri Feb 19, 2016 9:53 pm

Re: I2C and SPI at same time?

Fri Feb 19, 2016 10:02 pm

Richard-TX wrote:I2C and SPI are on different pins and yes you can use both at the same time. I have done it myself.
I'm trying to do the same thing on my pi, but I'm having issues with the I2C running correctly while running the SPI as well. What clock rates are you running the SPI and I2C interfaces at? Is each interface running in a separate thread or executable? I have to run the SPI interface at 8Mhz for one device, but I leave the I2C device clocked at the default 100kHz.

User avatar
experix
Posts: 204
Joined: Mon Nov 10, 2014 7:39 pm
Location: Coquille OR
Contact: Website

Re: I2C and SPI at same time?

Sat Feb 20, 2016 4:53 pm

The sense-hat (astro-pi) uses SPI for talking to the MCU (or AVR as it is also called) and I2C to talk to the pressure sensor, temp/humidity sensor and accelerometer/gyroscope/magnetometer. I am developing a set of header files, documentation aids and test/example programs for this. You can find it at http://experix.sourceforge.net in the Downloads section, my-hat directory. (What's there now has a few errors and I am getting ready to post corrections and a temperature-measuring example.)

User avatar
joan
Posts: 14264
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: I2C and SPI at same time?

Sat Feb 20, 2016 5:39 pm

Donny3000 wrote:
Richard-TX wrote:I2C and SPI are on different pins and yes you can use both at the same time. I have done it myself.
I'm trying to do the same thing on my pi, but I'm having issues with the I2C running correctly while running the SPI as well. What clock rates are you running the SPI and I2C interfaces at? Is each interface running in a separate thread or executable? I have to run the SPI interface at 8Mhz for one device, but I leave the I2C device clocked at the default 100kHz.
The SPI and I2C clock rates are independent and the actual transfers are handled by dedicated hardware. It makes no difference if you run separate threads or not.

Donny3000
Posts: 2
Joined: Fri Feb 19, 2016 9:53 pm

Re: I2C and SPI at same time?

Mon Feb 22, 2016 3:23 pm

joan wrote:
Donny3000 wrote:
Richard-TX wrote:I2C and SPI are on different pins and yes you can use both at the same time. I have done it myself.
I'm trying to do the same thing on my pi, but I'm having issues with the I2C running correctly while running the SPI as well. What clock rates are you running the SPI and I2C interfaces at? Is each interface running in a separate thread or executable? I have to run the SPI interface at 8Mhz for one device, but I leave the I2C device clocked at the default 100kHz.
The SPI and I2C clock rates are independent and the actual transfers are handled by dedicated hardware. It makes no difference if you run separate threads or not.
That's what I had figured, but I wasn't sure because if I run both of my executables at the same time my I2C readings goes to hell. If I don't run my SPI and I2C executables simultaneously, but run them independently, then they run perfectly (reading data at the rates they are configured without data corruption). But, if I start my I2C executable first and then start my SPI executable, the I2C readings come through corrupted or don't come through at all. Although, the SPI readings come through fine. So I was trying to see if anyone has experienced this as well.

luisibaja3
Posts: 1
Joined: Thu Jul 28, 2016 3:45 pm

Re: I2C and SPI at same time?

Thu Jul 28, 2016 3:48 pm

Hello, i'm having the same problem. I'm using a LCD in I2C 1 and a ATtiny85 in the SPI, but the I2C stops working when using the SPI, any idea?

amcdonley
Posts: 181
Joined: Mon Jan 26, 2015 5:56 pm
Location: Florida, USA

Re: I2C and SPI at same time?

Tue Aug 02, 2016 4:10 pm

I have been using a threading lock in communications where multiple threads talk to SPI to clear up occasional barfing.

Read or write functions grab the lock, open spi, perform the read or write, then close and release the lock.

Code: Select all

spi_lock=threading.Lock()


def readx():
  values=None
  with spi_lock:
      spi.open(...
      values=spi.xfer(...
      spi.close(...
  return values

Return to “Automation, sensing and robotics”