How use 2 SPI's at a time

I am using ESP32 and the ESP32 has 3SPI.
now i want to use two SPI of esp32
the code i am using is

#include <SPI.h>
#include <LoRa.h>

// Define SPI pins for LoRa device 1
#define LORA1_SS_PIN 5
#define LORA1_SCK_PIN 18
#define LORA1_MISO_PIN 19
#define LORA1_MOSI_PIN 23

// Define SPI pins for LoRa device 2
#define LORA2_SS_PIN 15
#define LORA2_SCK_PIN 14
#define LORA2_MISO_PIN 12
#define LORA2_MOSI_PIN 13

void setup() 
{
  // Initialize LoRa device 1
  Serial.begin(115200);
  SPI.begin(LORA1_SCK_PIN, LORA1_MISO_PIN, LORA1_MOSI_PIN, LORA1_SS_PIN);
  LoRa.setPins(LORA1_SS_PIN, -1, -1); // Use -1 for unused pins
  if (!LoRa.begin(433E6)) 
  {
    Serial.println("LoRa device 1 initialization failed!");
    while (1);
  }
  Serial.println("LoRa device 1 initialization DONE");
  // Initialize LoRa device 2
  SPI.begin(LORA2_SCK_PIN, LORA2_MISO_PIN, LORA2_MOSI_PIN, LORA2_SS_PIN);
  LoRa.setPins(LORA2_SS_PIN, -1, -1); // Use -1 for unused pins
  if (!LoRa.begin(433E6)) 
  {
    Serial.println("LoRa device 2 initialization failed!");
    while (1);
  }
   Serial.println("LoRa device 2 initialization DONE ");
}

void loop() 
{
  // Your LoRa communication code here
}

when i run this code only 1st lora device is only getting initialized but not 2nd device.
i don't know why?

when i use the spi individually then it is working.

so how to make the code work?

Why are you using 2 SPI interfaces in the first place ? SPI is a bus system and multiple devices can be connected to and controlled by a single bus

What does the parameter in the LORA,begin() function do ?

hmmmm
i just want to know how to use 2 spi of esp32 at a time.

yes i just remembered it.
so i edited my previous message.
so can we use 2 spi's esp32 ?

Are you trying to use 2 LORA devices or 1 LORA device and a repRap display as you originally posted ?

yes.
sorry for dum question.

one lora and one reprap display

Then why are you calling LORA.begin() twice ?

i am sorry.
i forget the very basic concept of SPI the we can connect many slaves to the master using more chip select pins.
what i did is i used two sets spi pins and doing all the trash.

OK, but that does not explain why you called LORA.begin() twice

i am using 2 lora's.
the first lora uses different pins and the 2nd lora uses different pins.

I am about to give up

now currently i am using 2 loras.
but in my original code i need to use one lora and one reprap.

i still did not write my original code. for now i just used 2 loras.

i think after seeing my great code and the way i thought about the problem, may be both UKHELiBob and Delta_G migth lost everything.

Neither myself or @Delta_G have lost anything, except perhaps the ability to understand what you are actually doing or want to do

1 Like

Your original code just used the same default single SPI instance and a single LoRa. instance too but with different pins. Thus only the second one for LORA2 worked.

SPI.begin(LORA1_SCK_PIN, LORA1_MISO_PIN, LORA1_MOSI_PIN, LORA1_SS_PIN);
LoRa.setPins(LORA1_SS_PIN, -1, -1); // Use -1 for unused pins


SPI.begin(LORA2_SCK_PIN, LORA2_MISO_PIN, LORA2_MOSI_PIN, LORA2_SS_PIN);
LoRa.setPins(LORA2_SS_PIN, -1, -1); // Use -1 for unused pins

You can put each LoRa device on a different SPI instance, or indeed put LoRa on one and a SPI display on the other. But there is no need to do that. Those devices can share a single SPI bus\instance.

Whether the LoRa library you are using can have seperate LoRa instances, say LoRa1 and LoRa2, I am not sure, never tried.

1 Like
#include <SPI.h>

// Initialize two SPIClass objects with different names
// one on the HSPI bus and another on the VSPI bus.
vspi = new SPIClass(VSPI);
hspi = new SPIClass(HSPI);

// pass custom pins to the begin() method if needed:
vspi.begin(VSPI_CLK, VSPI_MISO, VSPI_MOSI, VSPI_SS);
hspi.begin(HSPI_CLK, HSPI_MISO, HSPI_MOSI, HSPI_SS);

//Finally set the SS pins as outputs
pinMode(VSPI_SS, OUTPUT);
pinMode(HSPI_SS, OUTPUT);

See this for details:

1 Like

Disclaimer:
I have no experience with ESP32 based boards.

According to ESP32 SPI Communication: Pins, Multiple SPI, Peripherals (Arduino) | Random Nerd Tutorials the ESP32 has 4 independent SPI buses but you can only use 2 (HSPI and VSPI).

The SPI that is used in your code is an instance of the SPIClass that defaults to the HSPI bus (SPIClass(uint8_t spi_bus=HSPI);). You are using that same instance and only change the pins; I do not know if that can successfully be done (I did not look deep enough in the core files but suspect that you need to do some detaching first).

To use the VSPI bus you need to create a new instance of the SPI class for the second bus

#include <SPI.h>

SPIClass secondSPI(VSPI);

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}

You can treat that second SPI bus just like you treat the normal SPI (e.g. secondSPI.begin()).

Alternatively, there is an example that (though slightly different from the above) how to use multiple SPI buses (examples (heading ESP32 dev module) / SPI / SPI_Multiple_Buses).

Note
As already mentioned by others, why use two SPI buses; there does not seem to be a need.

1 Like

thanks a lot

hi guys i am using REP-RAP display with esp32.

REP RAP DISPLAY


this display will work on SPI protocol.
this display has a knob. i do not know how it works.

now this REP-RAP board is connect to ESP32.
this ESP32 is also connected to LORA which also works on SPI.

now my problem is LORA module is not getting initialize .

i do not know why???

the code is :slight_smile:



#include <Arduino.h>
#include <SPI.h>
#include <menu.h>
#include <menuIO/u8g2Out.h>
#include <menuIO/encoderIn.h>
#include <menuIO/chainStream.h>
#include <menuIO/serialOut.h>
#include <menuIO/serialIn.h>
#include <menuIO/U8GLibOut.h>
#include <ClickEncoder.h>
#include <menuIO/clickEncoderIn.h>
#include <EEPROM.h>

/*==============================================
  Declrations:Rotary Encoder pins
  ==============================================*/
#define encA    14
#define encB    15
#define encBtn  25
hw_timer_t * timer = NULL;
ClickEncoder clickEncoder(encA, encB, encBtn, 2);
ClickEncoderStream encStream(clickEncoder, 1);
MENU_INPUTS(in, &encStream);
portMUX_TYPE timerMux = portMUX_INITIALIZER_UNLOCKED;
void IRAM_ATTR onTimer() {
  portENTER_CRITICAL_ISR(&timerMux);
  clickEncoder.service();
  portEXIT_CRITICAL_ISR(&timerMux);
}

/*==============================================
  Declrations: Display Configuration
  ==============================================*/
#define USE_SWSPI
#define fontName  u8g2_font_5x7_tf
#define offsetX 0
#define offsetY 0
#define U8_Width 128
#define U8_Height 64
int x = 0;
int scroll_direction = 1;
U8G2_ST7920_128X64_F_SW_SPI u8g2(U8G2_R0, /* clock=*/ 18 /* A4 */ , /* data=*/ 23 /* A2 */, /* CS=*/ 5 /* A3 */, /* reset=*/ U8X8_PIN_NONE);
const colorDef<uint8_t> colors[6] MEMMODE = {
  {{0, 0}, {0, 1, 1}}, //bgColor
  {{1, 1}, {1, 0, 0}}, //fgColor
  {{1, 1}, {1, 0, 0}}, //valColor
  {{1, 1}, {1, 0, 0}}, //unitColor
  {{0, 1}, {0, 0, 1}}, //cursorColor
  {{1, 1}, {1, 0, 0}}, //titleColor
};

#define display_ss 5

//******************LORA PINS

#include <LoRa.h>

#define ss 4
#define rst 12
#define dio0 13
int counter=0;
void setup() 
{
  Serial.begin(115200);
  pinMode(display_ss, OUTPUT);
  pinMode(ss, OUTPUT);
  SPI.begin(18,19,23,5);
  u8g2.begin();
  //pinMode(encBtn, INPUT_PULLUP);
  x = x + scroll_direction;
  if (x > 40) scroll_direction = -1;
  if (x < 1) scroll_direction = 1;
  u8g2.setFont(fontName);
  nav.idleTask = idle;
  digitalWrite(display_ss,HIGH);
  digitalWrite(ss,LOW);
  
  Serial.println("WELCOME TO LORA TESTING");
  SPI.begin(18,19,23,ss);
  LoRa.setPins(ss, rst, dio0);
  while (!LoRa.begin(433E6)) 
  {
    Serial.println("Starting LoRa failed!");
    delay(1000);
  }
  Serial.println("LORA CONNECTED");
  digitalWrite(display_ss,LOW);
  digitalWrite(ss,HIGH);
  
}

void loop() 
{
  Encoderrun();
  delay(1000);
  sendMessage();

}

i do not understand what to do?
@Delta_G , @UKHeliBob , @sterretje , @srnet , @b707

you did not understand what i mean, perhaps it is regarding my English grammar or may be some wrong sentence alingment.

what i mean is

"i think after seeing my great code"
the worst code written by me. i mean you have seen it. i have written it with out basics.

"the way i thought about the problem"
that means we can connect many slaves by different chip select pin. i forget about that and i tried to use two different busses.

"may be both UKHELiBob and Delta_G might lost everything"

this means that after seeing my sense less code you both have lost everything you have learned because how i have written code is to use two SPI busses instead of using two chip select pins.

i do not know how you understand @Delta_G
i tried to play a joke but failed i am sorry @Delta_G .
i am also sorry to @UKHeliBob if you also feel the same way.