Page 1 of 1

I2C and SPI at same time?

Posted: Mon Jan 12, 2015 1:58 am
by nravanelli
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?

Re: I2C and SPI at same time?

Posted: Mon Jan 12, 2015 2:25 am
by mikronauts
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?

Re: I2C and SPI at same time?

Posted: Wed Jan 14, 2015 3:59 pm
by Richard-TX
I2C and SPI are on different pins and yes you can use both at the same time. I have done it myself.

Re: I2C and SPI at same time?

Posted: Fri Feb 19, 2016 10:02 pm
by Donny3000
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.

Re: I2C and SPI at same time?

Posted: Sat Feb 20, 2016 4:53 pm
by experix
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.)

Re: I2C and SPI at same time?

Posted: Sat Feb 20, 2016 5:39 pm
by joan
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.

Re: I2C and SPI at same time?

Posted: Mon Feb 22, 2016 3:23 pm
by Donny3000
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.

Re: I2C and SPI at same time?

Posted: Thu Jul 28, 2016 3:48 pm
by luisibaja3
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?

Re: I2C and SPI at same time?

Posted: Tue Aug 02, 2016 4:10 pm
by amcdonley
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