Simultaneous operation of SPI0 and SPI1 on ATmega328PB

Hi All,
We have been trying to utilise both the SPI available at the ATmega328PB controller.

The problem we are solving:

  • We are trying to interface Ethernet (ENC28J60) on SPI0 bus and TLE9879 on SPI1 bus.
  • We are using the standard Ethernet and TLE9879 libraries.
  • Arduino framework is being used.

What works:

  • Ethernet and TLE9879 work independently on the SPI0 bus and SPI1 bus respectively with Arduino Example code.

Problem faced:

  • Ethernet and TLE9879 do not work simultaneously on the SPI0 bus and SPI1 bus with Arduino Example code.


  • Is there any special settings to be implemented to take the advantage of both the SPI buses simultaneously on ATmega328PB controller?

What we have tried:

  • We have tried writing a Custom transfer function for SPI1 and kept the SPI0 for Ethernet. (Works independently but not simultaneously)

Feel free to reach us for any clarifications required.

Ashraful Khan

Assuming you use

platform = atmelavr
board = ATmega328PB
framework = arduino

for your ATMega328PB board (since board = uno or board = nanoatmega328 use a ATMega328P, not a ATMega328PB), you have access to the SPI and SPI1 libraries of the used Arduino core (“MiniCore”) at MiniCore/avr/libraries at master · MCUdude/MiniCore · GitHub.

From a first glance, the SPI and SPI1 libraries use seperate objects to store their data and only write to the SPI0 / SPI1 registers, so there should be no problem. Since you are experiencing some kind of bug though (no words on the symptoms though), you should be opening an issue in Issues · MCUdude/MiniCore · GitHub.

Hi @maxgerhardt
We are using the Env Settings as you have mentioned.

platform = atmelavr
board = ATmega328PB
framework = arduino
lib_deps = 

Can you please try out both the SPI sending and receiving data concurrently?
It would be a great help.

hi all,
Any leads on the problem? I am still not able to solve it.

I have attache the project file here.
Kindly open the project using platformIO on VS Code.

Project Link:

Just so we’re clear on what’s expected, what you want to see is both SPI0 and SPI1 MISO/MOSI/SCLK/CS lines being active at the same point in time right?

But all the transaction functions are blocking for the CPU.

Thanks for pointing out the blocking of the CPU.
Is there any way to work around this issue?

Do you have any pointers on where my code is blocking the CPU?

Disclaimer: I am not aware of how the ethernet packets are sent as I am using the Ethernet library directly available for W5100 module. I think if we can know the duration of the packets.

Each time a SPI byte/word is put into the data register, it waits for the data to be transmitted fully.

This prevents the CPU from executing another piece of code which may e.g. put a piece of data in the other SPI register.

I don’t see this as an issue however… Code for interleaving writes / reads and waiting for completion is much more complicated than a blocking wait.

It should still function correctly, meaning both your sensor and the Ethernet should independently work on SPI0 and SPI1 in the same sketch.