SPIClass not working when using FSPI and HSPI
Posted: Wed Nov 23, 2022 10:05 am
I am using an esp32s3-DevKitC board and I am trying to get two SPI buses operating. I can use SPIClass with either HSPI or FSPI and it operates correctly. However, when I try to instantiate and start two SPIClass instances, one for HSPI and one for FSPI, neither one operates correctly. This is the code I am currently running.
The code works correctly if spi_2 is never started and its calls to dac_write are removed; however, I need this to work with both SPI buses working correctly. What is going wrong here?
Code: Select all
#include "SPI.h"
#define CS1A 40
#define CS1B 39
#define CS2A 15
#define CS2B 16
#define ADC1M 1
#define ADC1A 2
#define ADC2M 8
#define ADC2A 9
#define SPI1C 12
#define SPI1D 11
#define SPI2C 36
#define SPI2D 35
SPIClass spi_1(HSPI);
SPIClass spi_2(FSPI);
SPISettings settings(100000, MSBFIRST, SPI_MODE0);
void setup_spi()
{
/* Begin SPI */
spi_1.begin(SPI1C, -1, SPI1D, -1);
spi_2.begin(SPI2C, -1, SPI2D, -1);
/* Setting chip select pins as outputs */
pinMode(CS1A, OUTPUT);
pinMode(CS1B, OUTPUT);
pinMode(CS2A, OUTPUT);
pinMode(CS2B, OUTPUT);
/* Setting chip select pins high */
digitalWrite(CS1A, HIGH);
digitalWrite(CS1B, HIGH);
digitalWrite(CS2A, HIGH);
digitalWrite(CS2B, HIGH);
}
void setup()
{
Serial.begin(115200);
delay(100);
Serial.printf("%d, %d", HSPI, FSPI);
setup_spi();
}
void loop()
{
dac_write((1 << 13) - 1, &spi_1, CS1A);
dac_write((1 << 13) - 1, &spi_1, CS1B);
dac_write((1 << 13) - 1, &spi_2, CS2A);
dac_write((1 << 13) - 1, &spi_2, CS2B);
uint16_t value1 = analogRead(ADC1A);
uint16_t value2 = analogRead(ADC2A);
dac_write((1 << 14) - 1, &spi_1, CS1A);
dac_write((1 << 14) - 1, &spi_1, CS1B);
dac_write((1 << 14) - 1, &spi_2, CS2A);
dac_write((1 << 14) - 1, &spi_2, CS2B);
delay(1);
}
void dac_write(uint16_t data, SPIClass* interface, uint8_t cs_pin)
{
interface->beginTransaction(settings);
digitalWrite(cs_pin, LOW);
interface->transfer16(data << 2);
digitalWrite(cs_pin, HIGH);
interface->endTransaction();
}