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.

Questions:

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

  • Any other suggestions will be appreciated.

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.

Edit:
Project Block Diagram along with their product part numbers are attached here.
Ethernet and Motor Drive

--

Regards

Ashraful Khan

What Arduino board are you using? What core are you using that supports the ATmega328PB?

Depending on what you mean by "simultaneously" It's either impossible on your hardware or only possible if you get rid of all the used libraries and implement everything from scratch. The later is necessary if you want to the SPI hardware to run the two buses concurrently so that SPI0 is transferring while SPI1 is also transferring.

My opinion: if you need that little bit additional speed, use faster board. Most probably it's not worth the effort for the minimal speed-up you can get.

The ATmega328 does not have two SPI busses, only one. What makes you think it has two?

Probably because the ATMEGA328PB datasheet says so ...

And what Arduino board is this you are using?

I did say ATmega328 and deliberately left out the PB off the end. Your lack of a reply to what sort of Ardunio you are using is more interesting. Are you actually testing a new sort of Arduino that you were not supposed to tell people about?

We have purchased the ATmega328PB IC and made a custom board on a breakout board.

  • All functionality works fine as mentioned in my post.
  • Simultaneous operation of SPI0 and SPI1 does not work.

As per the datasheet, we know that we have 2 independent SPI buses available on ATmega328PB.
We have purchased ATmega328PB for that reason and trying to test this feature on a breakout board before we develop the custom PCB for it.

As mentioned in my post, Both SPI0 and SPI1 work independently but do not work when concurrently. i.e when both SPI0 and SPI1 are trying to transfer data.

Hi @pylon
Thanks for your reply.
Have you tried both the SPI bus operating together.
Can you share your sample codes if possible.

I have the below resources with me:

  • Multiple Arduino Boards with ATmega328P
  • Custom made Boards with ATmega328PB
  • Ethernet board with ENC26J80

In case you suggest something else, we will try to arrange thouse hardware as well.

Our primary intention is to test both the SPI working together.

OK, so it is not an Arduino, so there is little chance that many people here would have experience with it.

However, there is no need to make a custom board to experiment with this processor. It seems they are available from the major distributors, at not much money, here is one example.
atmega328pb evaluation board

My guess at your problem would be the SPI drivers you are using are keeping the interrupts disabled until a transaction has been completed. All very well with only one SPI but not with two.

I assume you have written a core file for this. Want to share it for others that might be interested in this processor? Along with your test code, that shows this problem.

I also think you would be better of in this section of the forum, so I have moved the thread here. To where it mentions "Stand alone or alternative microprocessors" in the title description.

How have you modified the standard ethernet and TLE9879 libraries to support SPI1 on one of them? AFAIK, neither of those is "multiple SPI bus ready."
(In other words, let's see your code, and "what core are you using.")

We have modified and re-written the library for TLE9879 to use SPI1 as the standard library is using SPI0 by default.
We have not modified the library Ethernet. Standard Library is used.
Here are the links to the library.

Note: Test Project was developed using PlatformIO on VS Code.

Hi Guys,
I have uploaded my code. Kindly check and give me comments if I am doing something wrong.

I couldn't find the bits where you define the SPI1 library.
Can you point it out please.

Hi Grumpy_Mike,

I think it is there in his git hub repo file, motor_driver.h & motor_driver.cpp. He is including "SPI1.h" in motor_driver.h.

So, in motor_driver.cpp,
He is using the functionality for SPI1, which has been dedicated to TLE9879.

in main.cpp,
He is directly using the functionality for SPI0, which has been dedicated to Ethernet separately.
He has created a pointer of the Motor class, defined in motor_driver.h, into main.cpp and uses that pointer to call functions of the Motor class/TLE9879 library.

Logically, these two are separate SPI bus line communications and should work as expected by the post.

The only question I have is that does HAL layer of the controller really has the capability to treat both buses separately. I am not sure about other reasons as well.

You still didn't tell us what "operating together" exactly means.

What exact limitation do you experience? The processor has only one core so it cannot run two threads at the same time. But it should be possible to transfer a byte on both SPI buses at the same time but as I wrote, that cannot be achieved using the libraries which expects only one SPI bus to exist.

Yes I can see the code is doing a #include but I still can't see where the two SPI drivers are actually defined.

Can you confirm you have used platformIO to open the project? If not you will not be able to see the SPI.h and SPI1.h files. Anyways the headers are the standards from the Arduino framework.

Make sure you open the project from platform Io on VS code.

Can you confirm you have used platformIO to open the project? If not you will not be able to see the SPI.h and SPI1.h files. Anyways the headers are the standards from the Arduino framework.

Make sure you open the project from platform Io on VS code.