#include <SPI.h>
#define VSPI_MISO 2
#define VSPI_MOSI 4
#define VSPI_SCLK 0
#define HSPI_MISO 26
#define HSPI_MOSI 27
#define HSPI_SCLK 25
#if CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
#define VSPI FSPI
#endif
// Uninitialized pointers to SPI objects
SPIClass* vspi = NULL;
SPIClass* hspi = NULL;
// CS (Chip Select) pins for each bus
const int vspiCS[] = {5, 16, 17}; // Change the pin numbers based on your hardware
const int hspiCS[] = {15, 14, 13}; // Change the pin numbers based on your hardware
static const int spiClk = 1000000; // 1 MHz
void setup() {
Serial.begin(115200); // Initialize Serial communication
// Initialize two instances of the SPIClass attached to VSPI and HSPI respectively
vspi = new SPIClass(VSPI);
hspi = new SPIClass(HSPI);
// Initialize VSPI with default pins or alternate pins
vspi->begin(VSPI_SCLK, VSPI_MISO, VSPI_MOSI, vspiCS[0]);
// Initialize HSPI with default pins or alternate pins
hspi->begin(HSPI_SCLK, HSPI_MISO, HSPI_MOSI, hspiCS[0]);
// Set up slave select pins as outputs
for (int i = 0; i < sizeof(vspiCS) / sizeof(vspiCS[0]); i++) {
pinMode(vspiCS[i], OUTPUT);
}
for (int i = 0; i < sizeof(hspiCS) / sizeof(hspiCS[0]); i++) {
pinMode(hspiCS[i], OUTPUT);
}
}
// The loop function runs over and over again until power down or reset
void loop() {
//Serial.println("Loop Start");
// Use the SPI buses
Serial.println("Received VSPI value from CS ");
for (int i = 0; i < sizeof(vspiCS) / sizeof(vspiCS[0]); i++) {
int vspiValue = spiCommand(vspi, vspiCS[i], 0b01010101); // Junk data to illustrate usage
//normalize values- optional
if(vspiValue==48){
vspiValue=0;
}
else{
vspiValue=1;
}
Serial.print(vspiValue);
delay(1000);
}
Serial.println(": ");
Serial.println("Received HSPI value from CS ");
for (int i = 0; i < sizeof(hspiCS) / sizeof(hspiCS[0]); i++) {
int hspiValue = spiCommand(hspi, hspiCS[i], 0b11001100);
//normalize values- optional
if(hspiValue==48){
hspiValue=0;
}
else{
hspiValue=1;
}
Serial.print(hspiValue);
delay(1000);
}
Serial.println(": ");
}
int spiCommand(SPIClass* spi, int csPin, byte data) {
spi->beginTransaction(SPISettings(spiClk, MSBFIRST, SPI_MODE0));
digitalWrite(csPin, LOW); // Pull CS low to prep other end for transfer
int receivedValue = spi->transfer(data);
digitalWrite(csPin, HIGH); // Pull CS high to signify the end of data transfer
spi->endTransaction();
return receivedValue;
}