# ESP32 multiple slaves - same SPI bus

**URL:** <https://forum.arduino.cc/t/esp32-multiple-slaves-same-spi-bus/1098835>\
**Category:** Programming\
**Created:** [March 7, 2023, 10:47am UTC](https://forum.arduino.cc/t/esp32-multiple-slaves-same-spi-bus/1098835 "2023-03-07T10:47:31Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Phoedus](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/phoedus/32/929364_2.png) [@Phoedus](https://forum.arduino.cc/u/Phoedus)\
**Post date:** [March 7, 2023, 10:47am UTC](https://forum.arduino.cc/t/esp32-multiple-slaves-same-spi-bus/1098835/1 "2023-03-07T10:47:31Z")

</div>

Hi guys,  
I've been trying for several days to use different slaves on the same SPI bus.  
I'm using ESP32 devKit V2 (wrover kit) with 16MB memory.

If I use slaves one by one, they work very well,  
but when I try to integrate them to work in the same sketch, they no longer work correctly.

I assumed it was necessary to reconfigure the SPI at each slave change, but unfortunately this was not enough.

At the bus I have:

- SD card
- MAX3421
- w5500

What I observe is that I can get MAX3421 and SD to work, but as soon as I introduce Ethernet module (w5500), the other two peripherals stop working.

Could you please advise me the best way to deal with this problem?  
Examples or advice on the correct way to handle this situation?

Detail:  
I'm using freertos and I have a mutex, named: mutex\_SPI  
which allows me to avoid simultaneous access of the peripherals to the bus.

At the moment I don't have particular efficiency and speed constraints,  
I'm interested in starting by finding a way to make everything work.

SPI used: VSPI: SCK=18, MISO=19, MOSI=23  
CS\_SD=5  
CS\_MAX3421=27  
CS\_ETH=25

**SD:**  
I'm using "SPClass" (SPI.h): SPIClass SPI\_SD  
and class SDFS: SDFS SD = SDFS(FSImplPtr(new VFSImpl()))  
This is the initialization:

```cpp
SPI_SD.begin(SPI_SCK, SPI_MISO, SPI_MOSI, CS_SD);
if(!SD.begin(CS_SD, SPI_SD, 4000000, "", 5, false))
{
      ESP_LOGE(TAG, "ERROR SD NOT INIT");
}

```

**MAX3421:**  
I'm using "SPClass" (SPI.h): SPIClass SPI\_USB  
This is the initialization:

```cpp
SPI_USB.begin(SPI_SCK, SPI_MISO, SPI_MOSI, CS_MAX3421);
if (Usb.Init() == -1)
{
    ESP_LOGE(TAG, "USB HOST OSC did not start");
}

```

**Ethernet (w5500):**  
This is the initialization:  
simplifyed code:

```cpp
Ethernet.init(CS_ETH);
Ethernet.begin(mac);

if (Ethernet.hardwareStatus() != EthernetNoHardware)
{               
    vTaskDelay(100 / portTICK_PERIOD_MS );
                 
    if(Ethernet.linkStatus() != LinkOFF)
    {
        ESP_LOGD(TAG, "CABLE CONNECTED");
    }
    else
    {
        ESP_LOGE(TAG, "CABLE NOT CONNECTED");
    }
}

```

After the three initializations, each of which takes place in its own specific dedicated task,  
after ethernet initialization, I can no longer communicate with either the SD or the MAX3421 module, although they worked before.

And it's not enough for me to re-initialize again.  
I really don't know what to do

How do we behave in these cases?  
Thank you very much for your attention

---

<div class="post-metadata">

**Author:** ![ua6em](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ua6em/32/930389_2.png) [@ua6em](https://forum.arduino.cc/u/ua6em)\
**Post date:** [March 7, 2023, 11:05am UTC](https://forum.arduino.cc/t/esp32-multiple-slaves-same-spi-bus/1098835/2 "2023-03-07T11:05:00Z")

</div>

connect W5500 to HSPI port

---

<div class="post-metadata">

**Author:** ![Phoedus](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/phoedus/32/929364_2.png) [@Phoedus](https://forum.arduino.cc/u/Phoedus)\
**Post date:** [March 7, 2023, 11:06am UTC](https://forum.arduino.cc/t/esp32-multiple-slaves-same-spi-bus/1098835/3 "2023-03-07T11:06:36Z")

</div>

> [@ua6em](#):
>
> HSPI port

Thanks for the fast reply.

The hardware is not in prototype form,  
but it's already a PCB, do you think it's not possible to manage it on the same bus?

---

<div class="post-metadata">

**Author:** ![ua6em](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ua6em/32/930389_2.png) [@ua6em](https://forum.arduino.cc/u/ua6em)\
**Post date:** [March 7, 2023, 11:07am UTC](https://forum.arduino.cc/t/esp32-multiple-slaves-same-spi-bus/1098835/4 "2023-03-07T11:07:35Z")

</div>

I had no experience with the W5500

---

<div class="post-metadata">

**Author:** ![FernandoGarcia](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/fernandogarcia/32/214937_2.png) [@FernandoGarcia](https://forum.arduino.cc/u/FernandoGarcia)\
**Post date:** [March 7, 2023, 1:22pm UTC](https://forum.arduino.cc/t/esp32-multiple-slaves-same-spi-bus/1098835/5 "2023-03-07T13:22:22Z")

</div>

Create a function like this to manage SS pins.

> [@Connect several RFID-RC522 readers to one Arduino](https://forum.arduino.cc/t/connect-several-rfid-rc522-readers-to-one-arduino/1098510/2):
>
> Hi! It's possible. Connect all the readers in parallel on pins 11, 12 and 13. Connect SDA and RST to different pins to each reader. Then create a function to manage which reader will be used each time. // Global variables enum { RFID\_0, RFID\_1, RFID\_2, RFID\_3 }; const int SelectSlave\_RFID\_0 = x; const int SelectSlave\_RFID\_1 = x; const int SelectSlave\_RFID\_2 = x; const int SelectSlave\_RFID\_3 = x; void selectSPI(int device) { switch (device) { case 0: // Read card 0 { …

---

<div class="post-metadata">

**Author:** ![srnet](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/srnet/32/238868_2.png) [@srnet](https://forum.arduino.cc/u/srnet)\
**Post date:** [March 7, 2023, 1:48pm UTC](https://forum.arduino.cc/t/esp32-multiple-slaves-same-spi-bus/1098835/6 "2023-03-07T13:48:56Z")

</div>

I have had issues mixing SD cards and LoRa devices on the same SPI bus on an ESP32, but the same sketch code is OK on other platforms.

The workaround I chose was to use the ESP32 hardware MMC interface for SD functions althouh you could also use the other SPI interface the ESP32 has free.

---

<div class="post-metadata">

**Author:** ![srnet](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/srnet/32/238868_2.png) [@srnet](https://forum.arduino.cc/u/srnet)\
**Post date:** [March 7, 2023, 1:50pm UTC](https://forum.arduino.cc/t/esp32-multiple-slaves-same-spi-bus/1098835/7 "2023-03-07T13:50:24Z")

</div>

> [@Phoedus](#):
>
> The hardware is not in prototype form,  
> but it's already a PCB,

I did breadboard my mix of SD and LoRa before doing a PCB, and found the issue, so a workaround was not so difficult.

---

<div class="post-metadata">

**Author:** ![Phoedus](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/phoedus/32/929364_2.png) [@Phoedus](https://forum.arduino.cc/u/Phoedus)\
**Post date:** [March 7, 2023, 2:53pm UTC](https://forum.arduino.cc/t/esp32-multiple-slaves-same-spi-bus/1098835/8 "2023-03-07T14:53:13Z")

</div>

> Create a function like this to manage SS pins.

Hi @FernandoGarcia ,  
Thank you for your answer.

Unfortunately the problem is not the CS, it is managed within the functions that send the commands, it is correctly piloted.  
I checked the correct management through an oscilloscope.

The problem is that after the lowering of each CS when I have to write to SPI, I don't see any movement on the MOSI signals, the problem is therefore on another level.

---

<div class="post-metadata">

**Author:** ![Phoedus](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/phoedus/32/929364_2.png) [@Phoedus](https://forum.arduino.cc/u/Phoedus)\
**Post date:** [March 7, 2023, 3:01pm UTC](https://forum.arduino.cc/t/esp32-multiple-slaves-same-spi-bus/1098835/9 "2023-03-07T15:01:41Z")

</div>

Hi @srnet,

Thanks for the advice, in my case this can't be a solution, it's a bypass of the problem.  
The reason is that I can't use other pins, they are intended for other functions, and as I said, the hardware is now defined.

In any case, the SPI is a bus that by definition must allow the operation of multiple capacities connected to it, it was created for this. So hopefully I can find a solution with the current hardware setup 😚

---

<div class="post-metadata">

**Author:** ![FernandoGarcia](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/fernandogarcia/32/214937_2.png) [@FernandoGarcia](https://forum.arduino.cc/u/FernandoGarcia)\
**Post date:** [March 7, 2023, 3:05pm UTC](https://forum.arduino.cc/t/esp32-multiple-slaves-same-spi-bus/1098835/10 "2023-03-07T15:05:33Z")

</div>

What happens if you try call `begin` function before write to device?

---

<div class="post-metadata">

**Author:** ![ua6em](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ua6em/32/930389_2.png) [@ua6em](https://forum.arduino.cc/u/ua6em)\
**Post date:** [March 7, 2023, 3:12pm UTC](https://forum.arduino.cc/t/esp32-multiple-slaves-same-spi-bus/1098835/11 "2023-03-07T15:12:50Z")

</div>

Three mistresses in one house, this definitely will not work, you need to look at how, for example, TFT and TOUCH are processed in the TFT\_eSPI library

---

<div class="post-metadata">

**Author:** ![Phoedus](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/phoedus/32/929364_2.png) [@Phoedus](https://forum.arduino.cc/u/Phoedus)\
**Post date:** [March 7, 2023, 3:25pm UTC](https://forum.arduino.cc/t/esp32-multiple-slaves-same-spi-bus/1098835/12 "2023-03-07T15:25:06Z")

</div>

I tryed to call the init function of the MAX3421 after ethernet initialization...

```cpp
SPI_USB.begin(SPI_SCK, SPI_MISO, SPI_MOSI, CS_MAX3421);
if (Usb.Init() == -1)
{
    ESP_LOGE(TAG, "USB HOST OSC did not start");
}

```

The result: USB HOST OSC did not start  
As if it was no longer possible to communicate correctly with the device

This the body of the Init function:

```cpp
template< typename SPI_SS, typename INTR >
int8_t MAX3421e< SPI_SS, INTR >::Init() {
    XMEM_ACQUIRE_SPI();
    // Moved here.
    // you really should not init hardware in the constructor when it involves locks.
    // Also avoids the vbus flicker issue confusing some devices.
    /* pin and peripheral setup */
    SPI_SS::SetDirWrite();
    SPI_SS::Set();
    spi::init();
    INTR::SetDirRead();
    XMEM_RELEASE_SPI();
    /* MAX3421E - full-duplex SPI, level interrupt */
    // GPX pin on. Moved here, otherwise we flicker the vbus.
    regWr(rPINCTL, (bmFDUPSPI | bmINTLEVEL));
    
    if(reset() == 0) { //OSCOKIRQ hasn't asserted in time
        return ( -1);
    }
    
    regWr(rMODE, bmDPPULLDN | bmDMPULLDN | bmHOST); // set pull-downs, Host
    
    regWr(rHIEN, bmCONDETIE | bmFRAMEIE); //connection detection
    
    /* check if device is connected */
    regWr(rHCTL, bmSAMPLEBUS); // sample USB bus
    while(!(regRd(rHCTL) & bmSAMPLEBUS)); //wait for sample operation to finish
    
    busprobe(); //check if anything is connected
    
    regWr(rHIRQ, bmCONDETIRQ); //clear connection detect interrupt
    regWr(rCPUCTL, 0x01); //enable interrupt pin
    
    return ( 0);
}

```

---

<div class="post-metadata">

**Author:** ![Phoedus](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/phoedus/32/929364_2.png) [@Phoedus](https://forum.arduino.cc/u/Phoedus)\
**Post date:** [March 7, 2023, 3:29pm UTC](https://forum.arduino.cc/t/esp32-multiple-slaves-same-spi-bus/1098835/13 "2023-03-07T15:29:14Z")

</div>

> [@ua6em](#):
>
> Three mistresses in one house, this definitely will not work, you need to look at how, for example, TFT and TOUCH are processed in the TFT\_eSPI librar

LOL 🤣  
but... Thanks to the CS the three mistresses never speak or see or hear at the same time!  
Under these conditions I think it can work!! 🤣

Thanks for the idea, as soon as I can I'll study the example you kindly recommended

---

<div class="post-metadata">

**Author:** ![FernandoGarcia](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/fernandogarcia/32/214937_2.png) [@FernandoGarcia](https://forum.arduino.cc/u/FernandoGarcia)\
**Post date:** [March 7, 2023, 3:38pm UTC](https://forum.arduino.cc/t/esp32-multiple-slaves-same-spi-bus/1098835/14 "2023-03-07T15:38:31Z")

</div>

Whith complete code and schematic of your board maybe you can get more help.

---

<div class="post-metadata">

**Author:** ![ua6em](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ua6em/32/930389_2.png) [@ua6em](https://forum.arduino.cc/u/ua6em)\
**Post date:** [March 7, 2023, 3:42pm UTC](https://forum.arduino.cc/t/esp32-multiple-slaves-same-spi-bus/1098835/15 "2023-03-07T15:42:57Z")

</div>

> [@Phoedus](#):
>
> Thanks to the CS the three mistresses

once I was at a service station where a friend was adjusting the engine, I told the master, why are you suffering, CO is normal, and he answered me and CH ??? Here also )))

---

<div class="post-metadata">

**Author:** ![Adrianotiger](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/adrianotiger/32/221481_2.png) [@Adrianotiger](https://forum.arduino.cc/u/Adrianotiger)\
**Post date:** [March 7, 2023, 3:43pm UTC](https://forum.arduino.cc/t/esp32-multiple-slaves-same-spi-bus/1098835/16 "2023-03-07T15:43:02Z")

</div>

Can you take control of the CS pins?  
I had a similar problem some times ago with different SPI devices (7 display + SD card). It is never a good idea to "init" the SPI bus several times, since you have only 1 bus.  
Every module/library takes the control of the SPI and at the end you don't know if a library is setting something more than just his slave device.

In my case, I got the control of the CS and so every device is managed when I set the CS, and not when the library want to set it 😉

Maybe a SPI command is sent inside the W5500 interrupt routine?

---

<div class="post-metadata">

**Author:** ![Phoedus](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/phoedus/32/929364_2.png) [@Phoedus](https://forum.arduino.cc/u/Phoedus)\
**Post date:** [March 7, 2023, 4:03pm UTC](https://forum.arduino.cc/t/esp32-multiple-slaves-same-spi-bus/1098835/17 "2023-03-07T16:03:17Z")

</div>

You are right,  
I will try to simplify the code with only the parts of interest,  
it is a very big project, it will take me a long time, there are different tasks that communicate with the different peripherals, if I can't solve it, I will try

---

<div class="post-metadata">

**Author:** ![Phoedus](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/phoedus/32/929364_2.png) [@Phoedus](https://forum.arduino.cc/u/Phoedus)\
**Post date:** [March 7, 2023, 4:07pm UTC](https://forum.arduino.cc/t/esp32-multiple-slaves-same-spi-bus/1098835/18 "2023-03-07T16:07:25Z")

</div>

I think this is the problem,  
for the moment I'm not using the interrupt on the ethernet,  
but I see that the bus maintains the ethernet configuration.

I just implemented the function to manage the CSs but I still see the MAX3421 CS move at the wrong times, I probably have to understand the functioning of the w5500 library. What hard work 🥲

---

<div class="post-metadata">

**Author:** ![Phoedus](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/phoedus/32/929364_2.png) [@Phoedus](https://forum.arduino.cc/u/Phoedus)\
**Post date:** [March 28, 2023, 2:19pm UTC](https://forum.arduino.cc/t/esp32-multiple-slaves-same-spi-bus/1098835/19 "2023-03-28T14:19:25Z")

</div>

it was necessary to divert the ethernet to another spi BUS.  
For the moment it seems that this allows an adequate functioning of the peripheral.

The protocol is very complex and it was highly inadvisable to place other peripherals on the same bus as the Ethernet.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [September 24, 2023, 2:19pm UTC](https://forum.arduino.cc/t/esp32-multiple-slaves-same-spi-bus/1098835/20 "2023-09-24T14:19:53Z")

</div>

Closed on September 24, 2023, 2:19pm UTC

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