Dear Community,
ich benutze ein ESP32 und möchte darauf einmal Master und Slave konfigurieren.
Den Master habe ich soweit konfiguriert und kann auch Daten senden, dafür nutze ich die VSPI-Schnittstelle.
Zurzeit versuche ich die HSPI-Schnittstelle als Slave zu konfigurieren.
Auf der Masterseite-VSPI benutze ich die Pins:
SCLK - 18
MISO - 19
MOSI - 23
CS - 5
Auf der Slaveseite-HSPI benutze ich die Pins:
SCLK - 14
MISO - 12
MOSI - 13
CS - 15
Die Pins habe ich auch jeweils miteinander verbunden.
Ich habe das Code-Beispiel "queue_multiple_transactions_and_poll_all_results" von der library #ESP32SPISlave.h als Vorlage genommen.
#include <ESP32SPISlave.h>
#include <SPI.h>
#include <stdlib.h>
#include "pins_arduino.h"
#include "esp32-hal-spi.h"
#include "freertos/FreeRTOS.h"
#include "freertos/semphr.h"
#include "helper.h"
#define VSPI_MISO 19
#define VSPI_MOSI 23
#define VSPI_SCLK 18
#define VSPI_CS 5
#define HSPI_MISO 12
#define HSPI_MOSI 13
#define HSPI_SCLK 14
#define HSPI_CS 15
static const int spiClk = 2000000; // 2 MHz für die CLOCK
SPIClass master(VSPI);
ESP32SPISlave slave;
static constexpr size_t BUFFER_SIZE = 1;
static constexpr size_t QUEUE_SIZE = 2;
uint8_t rx_buf[BUFFER_SIZE] {0};
void setup()
{
Serial.begin(115200);
delay(2000);
master.begin(VSPI_SCLK, VSPI_MISO, VSPI_MOSI, VSPI_CS); //SCLK, MISO, MOSI, CS
pinMode(VSPI_CS, OUTPUT); //VSPI CS
slave.setDataMode(SPI_MODE1); // default: SPI_MODE0
slave.setQueueSize(QUEUE_SIZE); // default: 1, requres 2 in this example
slave.begin(HSPI, HSPI_SCLK, HSPI_MISO, HSPI_MOSI, HSPI_CS); //SCLK, MISO, MOSI, CS
}
void loop() {
// if no transaction is in flight and all results are handled, queue new transactions
if(slave.hasTransactionsCompletedAndAllResultsHandled()) {
// initialize tx/rx buffers
Serial.println("initialize rx buffers");
memset(rx_buf, 0, BUFFER_SIZE);
Serial.print("Attempting to queue transaction... RX Buffer Size: ");
Serial.println(BUFFER_SIZE);
bool queued = slave.queue(NULL, rx_buf, BUFFER_SIZE);
Serial.print("Queue submission: ");
Serial.println(queued ? "Success" : "Failed");
Serial.println("Attempting to trigger transaction...");
slave.trigger();
Serial.println("Transaction triggered.");
size_t inFlight = slave.numTransactionsInFlight();
Serial.print("Transactions in flight: ");
Serial.println(inFlight);
Serial.println("wait for the completion of the queued transactions...");
}
master.beginTransaction(SPISettings(spiClk, MSBFIRST, SPI_MODE1));
digitalWrite(master.pinSS(), LOW); //pull SS slow to prep other end for transfer
// delayMicros(1);
master.transfer(0b11010101);
digitalWrite(master.pinSS(), HIGH); //pull ss high to signify end of data transfer
master.endTransaction();
delay(100); // Kleine Verzögerung, um dem Slave Zeit zum Verarbeiten zu geben
size_t completedTransactions = slave.numTransactionsCompleted();
Serial.print("Completed Transactions: ");
Serial.println(completedTransactions);
// if all transactions are completed and all results are ready, handle results
if (slave.hasTransactionsCompletedAndAllResultsReady(QUEUE_SIZE)) {
// process received data from slave
Serial.println("all queued transactions completed. start verifying received data from slave");
}
delay(5000);
}

Allerdings wir mir das beim serial monitor ausgegeben und anscheinend kommt es gar nicht zu der if-Bedingung --> if (slave.hasTransactionsCompletedAndAllResultsReady(QUEUE_SIZE))
Hoffe ihr könnt mir da weiter helfen. Danke für eure Zeit!
best regards,
Patrick