SPI two way ESP32

Hi All

Im working on a project where i require multiple ESP32 boards to send and receive data to/from one ESP32 board simultaneously. Over SPI Bus, i cannot find any examples online , just one way communication between boards which i have gad success with on previous projects.

I went down the ESP-NOW route which was great however i exceeded the maximum bytes capacity of
250 bytes.

The reason why i have choosen ESP32 is my boards are custom and have had great success creating custom boards with ESP32 as apposed to the Tennsy 4.1 custom boards.

Look forward to your repsonse.

Thanks

How "simultaneously"? Unlike I2C the SPI bus does not have broadcasting nor multi-master capability, so really simultaneous is not achievable. An SPI bus has one dedicated master and can have multiple slaves.

In the typical approach the SPI master polls the slaves every now and then and does more communication with a slave on demand. A common interrupt line can be added to notify the master of required attention.

1 Like

But you could implement broadcasting by asserting multiple select lines simultaneously*.

Similarly, you could devise a scheme of multi-master operation - but you'd have to design & develop the protocol yourself.

Again, what, exactly, do you mean by that?

Broadcasting can send from one sender to multiple listeners simultaneously, but you cannot have multiple senders on a single bus at the same time - their transmissions will "collide" and trash each other.

EDIT

* Usually, asserting the select is what enables the driver on the slave's MISO output - so you'd probably have to take care of that, too ...

Example of SPI Multi Master - how ST do it in their STM32F030:

Maybe the ESP32 has its own implementation?

And how do you signal that the slaves should turn off their MISO line? Or use wired-AND for MISO and ignore the feedback in a broadcast?

How that? How would you split and rearrange the MISO line with multiple masters?

Right, that's the major trouble maker.

That's only a point-to-point implementation of 2 SPI devices, with logically reversible MISO and MOSI lines controlled by NSS. This pattern can not be extended to more than 2 devices.

I didn't say it was easy - just possible!

Depends how desperate @caller890 is for this ...

Indeed - I added an edit on that.

You could just send UDP packets, or split the payload into several 250 byte parts

1 Like

Sorry for late repsonse , busy day at work , so what i understand is when the select pin is low i can send and reeceive data between one loop simultaneously.

What im trying to achive is when that CS pin is low the master can send data to the slave and the slave can send data to the master simultaneously. Which the SPI protocol states it can do.

I just cant find any examples of this two way protocol

This is excactly what im trying to achive multi master

I have time , im not in a rush , been working on tnis project for some time. I like the idea on this as ESP-NOW is great

That is exactly what SPI does - it is full-duplex.

As data is being clocked-in to the slave on MOSI, data is simultaneously clocked out of the slave on MISO.

No, that is something completely different!

Like this:


From SPI Protocol - Serial Peripheral Interface - Working Explained

See also: https://www.avrfreaks.net/s/topic/a5C3l000000UacsEAC/t154582?comment=P-1246580 - which includes a link that separates-out the steps of that animation.

Thanks Awneil , im going to do some research on these articles you have sent and Full Duplex and play around with some code.

I managed to get IC2 between multiple teensy 4.1 to do excatly what i needed however not simultaneously, the timings were close enough for it to work , just cant get custom Teeny 4.1 to fuction well with the complexity of SMD soldering of the processor.

Did you mean I2C ?

In I2C a master can broadcast to all slaves simultaneously.

1 Like

Possibly another SPI option: instead of having all the devices in "parallel" on the bus, use the "daisy chain" configuration?

That would get the replies from all slaves in a single transaction ...

image

image

https://learn.sparkfun.com/tutorials/serial-peripheral-interface-spi

1 Like

The issue i have is i need data back from the slave simultaneously, if you have any articles on this please share im all for different options on how this data is transferred.

Again, what does "simultaneously" actually mean? Give numbers!

As already noted:

That applies to any transmission medium - not just SPI.

If you want them to all send at the same time, then you will need a separate channel for each one.

That the raises the question of whether your processor/chip can actually read those channels at the same time...

Perhaps explain why you think you need the reads to be "simultaneous"?

Hi Awneil

Apologies for my lack understanding, i am a amature so limited on knowledge.

Ok ill do my best here goes! What im about to explain i have achieved with ESPNOW.

I will explain my project as you may understand more what im trying to accomplish.

I have one master PCB that receives sensor data from 4 other PCB boards and saves received data as variables , this data is then processed with if statements and check sums then issues a command back to each of the 4 boards using simple x = 0 & Y = 1.

The master PCB is linked to my GUI that i have build in processing as user interface that allows me in real time to monitor data and issue commands.

See below each board explained with some examples of data transmitted and received.

Board 1 Power module:-
Monitors Battery Cells
Monitor's Battery Temperature
Monitors 24v bus voltage and current
Monitors 5v bus voltage and current
Moset to enable 24v Bus
Moset to enable 5v Bus

All Monitored data is transmitted to the master PCB, for any reason the MPU if statements are checked then a command is issued back to this board via the master baord for example to disable a serving a bus.

Board 2 Solenoid Valve Board:-
Mosef to enable 24bus to each solenoid valve (12nr mosfets in total)
Monitors if mosfets are enabled and voltage and current is detected when mosfets are enabled.

If the master PCB if statements are checked then the master board will issue a command back to enable or disable mosfets, i can also manually enable or disable mosfet via my GUI. An if statement example if pressure sesnor exceed a vaule then X = 0 , this means close valve.

Board 3 Sesnor Module:-
This board is stacked with all different sensors from pressure , temperature, barometers, accelerometers, GPS, etc

All data is transmitted to the master baord , some data is just monitored, others serve if statements and check sums.

The code i have written has state functions within the loop, each state has different loop codes different tasks being asked for.

The master board issues command to this board to change the state function for example of x = 1 then state 1, if x = 2 then state 2.

Board 4 telemetry:-
This board receives all data received and transmitted data on the master board as a packet and saves to data logger. In addition transmits and receives all data from my GUI over LoRA.

When i use the word simultaneously i refer to when the the master board receives any data from each of the other board it can issue back any command i.e. X = 0 during the same loop to that particular board.

I understand that i cant transmit between all boards simultaneously due to data clash. Hence why i like the SPI function as i understand it should be simple due to the CS pin.

For example
If CS pin to board 1 is low then receive data and transmit back. Store all data into variables

Followed by

If CS pin to baord 2 is low then receive data and transmit back. Store all data into variables.

And so on for the remaining 2 boards.

Lastly wether is this right or wrong i transmit all data as Arrays, i find it simple and easy to break apart the data. If there is a better way i will listen to those more experienced.

Sorry for the long repsonse, trying to give you as much information as possible , i can share some of my code but its hundres of lines long on my smallest board.

Again thanks for your patience.

Thanks