Electronic – How to use SPI and I²C at the same time

i2cspi

I have a couple of A/D converters and am reading them every 100 µs over SPI, using the timer interrupt routine with SPI clocked at 16 MHz.

Additionally, I have five digital temperature sensors using the I²C bus in a timer interrupt routine executed every second. With a 100 kHz I²C bus and two bytes per temperature sensor, it takes almost 1 ms to read the temperature.

Obviously while reading temperature values I cannot use the SPI bus, missing almost 10 A/D readouts while I²C is active.

How can I use slow and fast peripherals and have timely readouts?


Thanks, these are all great answers!

I am actually using Teensy 3.2 development board and would be nice to have a solution with ready-to-use Arduino IDE libraries. There is a library for Teensy 3.2 for I2C communication with DMA.

I think that the simplest solution is to read A/D converters using 100 us timer interrupt and increment a variable inside the interrupt routine to initiate I2C communication in the main code every second (Microchip TC74 temperature sensors).

I have never used DMA before and my question is what happens when I2C bus is active (reading), and 100us timer interrupt comes. Will it be accepted right away or uC will wait until the byte readout is finished on I2C, or interrupt will be accepted and I2c reading will continue independently?

Best Answer

Obviously while reading temperature values I cannot use SPI bus, missing almost 10 A/D readouts while I2C is active.

Not at all - with many microcontrollers you can put the bytes you want sent into a buffer and return from the interrupt, then arrange to get another interrupt later when it's done. Interrupt handlers should never be waiting for something to complete.

Related Topic