arduinoesp32spiarduino-esp32lora

Multiple SPI ports usage on ESP32


I'm fighting with this code to make it work... The aim of it is to control a LoRa module from the hspi pins of the esp32 while the vspi pins will be used by an sd module (not provided in this code).

I tried to make it work with default pins and it work! But I can't achieve when I set other pins (which are designed to it).

The LoRa return "not init" when the code is working

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

// Task dual core
TaskHandle_t DataReadertask;
TaskHandle_t DataWritertask;

//-----------Communication avec sd et LoRa--------------//
    // SD
#define VSPI_MISO 19
#define VSPI_MOSI 23
#define VSPI_SCLK 18
#define VSPI_SS 5

    // LoRa
#define HSPI_MISO 12
#define HSPI_MOSI 13
#define HSPI_SCLK 14
#define HSPI_SS 15
#define NSS_LoRa 27
#define RST_LoRa 26
#define DI0_LoRa 4

// uninitalised pointers to SPI objects
SPIClass vspi(VSPI);
SPIClass hspi(HSPI);

static const int spiClk = 1000000; // 1 MHz

void setup() {
    Serial.begin(9600);
//--------------------Dual Core------------------------------------//
    xTaskCreatePinnedToCore(DataReader, "DataReadertask", 20000, NULL, 1, &DataReadertask, 0);
    xTaskCreatePinnedToCore(DataWriter, "DataWritertask", 20000, NULL, 1, &DataWritertask, 1);

    //-----------------------SPI--------------------------------------//
    vspi.begin(VSPI_SCLK, VSPI_MISO, VSPI_MOSI, VSPI_SS);

    hspi.begin(HSPI_SCLK, HSPI_MISO, HSPI_MOSI, HSPI_SS); // SCLK, MISO, MOSI, SS

    pinMode(VSPI_SS, OUTPUT);
    pinMode(HSPI_SS, OUTPUT);

    // LoRa
    // setup LoRa sender
    LoRa.setPins(NSS_LoRa, RST_LoRa, DI0_LoRa);

    // 866E6 for Europe
    while (!LoRa.begin(433E6)) {
        Serial.println("not init");
        delay(500);
    }

    // Lora code, max 0-0xFF
    LoRa.setSyncWord(0x25);
    Serial.println("LoRa Initializing Successful!");
}

void loop() {}

//-------------------------Dévelopement des taches--------------------------------//
    // Coeur 0
void DataReader(void *pvParameters) {
    for (;;) {
        vTaskDelay(pdMS_TO_TICKS(1000));
    }
}

    // Coeur 1
void DataWriter(void *pvParameters) {
    for (;;) {
        launchPaquet("test", 0.1);
        vTaskDelay(pdMS_TO_TICKS(1000));
    }
}

//-------------------------------------------------------------------------------

//-------------------------------LoRa----------------------------//
void launchPaquet(char type[],float data) {
    LoRa.beginPacket();
    LoRa.print("El:");
    // LoRa.print(String(type));
    LoRa.print(":");
    LoRa.println(String(data));
    LoRa.endPacket();
}

//------------------------SPI-------------------------------------//
void spiCommand(SPIClass *spi, byte data) {
    spi->beginTransaction(SPISettings(spiClk, MSBFIRST, SPI_MODE0));
    digitalWrite(VSPI_SS, LOW); // Utilisez VSPI_SS ou HSPI_SS selon le cas
    spi->transfer(data);
    digitalWrite(VSPI_SS, HIGH); // Même chose ici
    spi->endTransaction();
}

Does anyone know a solution to it?

Making the LoRa to work with the hspi pins.


Solution

  • The Lora.h library by default uses the SPI Class object directly. See source code.

    To override the default SPI interface used by the library, you need to call LoRa.setSPI(hspi); before LoRa.begin(). This is documented in the API.md.