UART vs I2C vs SPI for inter-processor communication between microcontrollers

Drasko DRASKOVIC picture Drasko DRASKOVIC · Jan 31, 2014 · Viewed 34.1k times · Source

I am examining a way to connect two microcontrollers. On the level of serialization I am thinking of using Nano protobuffers (http://code.google.com/p/nanopb/). This way I can encode/decode messages and send them between two processors.

Basically, one small processor would be the RPC server, capable of doing several functions. Bigger processor will call there RPCs via messages sent, and then when data is ready, it will read it from smaller processor.

What would be the pros/cons of using UART, I2C or SPI?

Messages will be put in the mailbox que prior to sending.

Best regards, Drasko

Answer

jeb picture jeb · Feb 8, 2014

It depends on your total requirements and how expensive are pins.

I2C only needs two pins, but it's slow and to handle it with or without interrupts is a pain, even with the build in peripheral modules. It's a master/slave system, it's good for controlling many slow devices like temp sensors.
Only two lines for all bus devices, the selection is done via an I2C-Address in the protocol.

Uart needs two pins, it's normally faster, easier to handle, but requires (nearly) the same clocks at both sides. One to one asynchronous system, can be good if both systems needs to be send sometimes data without waiting for a master poll request.
Can also be used as a bus system, but then you need a master/slave structure or more complex protocols.

SPI needs 3 (or 4 with CS) pins, it's the fastest, simple to implement even with DMA, low cpu time overhead, often buffered. When you have enough free pins I would prefer it.