How does Arduino communicate with different libraries using SPI?

Hello!
I am wandering how does the Arduino communicate via SPI with two or more sensors, each of which has its own library. I know that the peripheral selection happens with the CS setting. But how does it actually happen? If the two sensors have different libraries, then they are bound to have different event handlers on their SPI instances. What happens after that? Are the event handlers combined?

Thank you,
Kostas

Welcome to the forum

Why do you anticipate a problem using different libraries ? As you say, the code for each sensor will be active only when its CS pin is active

1 Like

If the different libraries for hardware devices use different SPI settings then the librairies need to have implmented the recommended SPI.beginTransaction() command before each SPI access.

As I said, each SPI transaction will generate a completion event. Each library though, has a different event handling function for SPI events. When I combine the two libraries on one SPI, which event handler will be executed? Will both be executed?

Thank you.

When I combine the two libraries on one SPI, which event handler will be executed? Will both be executed?

there is the SPI library used by both libraries for devices.
if they don't use the SPI library then they will not work on same pins

Can you say which two libraries using SPI that you had in mind ?

You seem to assume that SPI is being handled by interrupt. This isn't usually the case. Most of the code I've seen cues up a transmission and then blocks waiting for it to end.

It is possible to do SPI using an interrupt. If you want to have more than one thing then whenever you engage your CS pin you attach the appropriate interrupt.

They are two custom libraries. One I made for reading BME280 sensors and one I made to communicate with an LCD.

Why are you seemingly using interrupts at SPI.transfer() level ?

Because you have a single SPI , the only one SPI event handler can be installed at the time. So if you use a two libraries, and each of them want to install its own handler, the SPI will execute the handler, that was installed last.

I am just wondering how two libraries can use the same SPI line, if they are written to be used on different lines.

What does it mean? The most SPI libraries written to use a same line. And most of then do not install its own event handler, so no problem at all

Hm, are you thinking of hardware and software SPI here? Or that a library has it's own implementation of SPI? Or both?

I have used my own LoRa (SPI) library, with both an TFT display (SPI) and SD card (SPI). No issues to worry about.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.