ESP32 Mulitple SPI devices: conflict with spi->begin() in Arduino on board manager v3.0.x
Posted: Tue Jun 25, 2024 7:35 pm
Hi, hopefully this is the right place to post and ask for help.
I've been successfully using ESP32-S3 with Arduino board manager V2.0.x to support multiple SPI devices on the same VSPI bus.
Now, with migration to board manager v3.0.x, it seems there is a conflict when calling SPI->begin() more than once.
I have been implementing each SPI device with its own SPIClass, though they all point to the same VSPI bus. (simplified code below).
This works on v2.0.x, but DOES NOT work on v3.0.x. Using V3.0.x, once spi->transfer() is called, the code hangs. If you remove one of the spi->begin() lines (line 27 or 32), then it "works", but of course then the chip select pin isn't initialized properly for that spi class / device.
This wouldn't be the end of the world...I have adjusted the code to use only a single SPI class and just manage all the chip selects manually, rather than referencing SPIClass->pin_SS().
But I'm at an impasse now because I also use the u8g2 display library, which itself sets up an SPI object for the display. Once u8g2.begin() is called, the code hangs. I presume behind the scenes, the u8g2 library is initializing the display and starting an SPI transfer, which is where the example code gets stuck too.
Does anyone know what changed between v2 and v3? And is there a better way to handle SPI devices now that you seem to not be able to call spi->begin() more than once for the same bus?
Thanks for the help!
(link to related post in Arduino forum, if helpful)
https://forum.arduino.cc/t/multiple-spi ... /1275440/3
I've been successfully using ESP32-S3 with Arduino board manager V2.0.x to support multiple SPI devices on the same VSPI bus.
Now, with migration to board manager v3.0.x, it seems there is a conflict when calling SPI->begin() more than once.
I have been implementing each SPI device with its own SPIClass, though they all point to the same VSPI bus. (simplified code below).
- #include <SPI.h>
- #define SPI_SS_IMU 10
- #define SPI_MOSI 11
- #define SPI_CLK 12
- #define SPI_MISO 13
- #define SPI_SS_BARO 18
- #if CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
- #define VSPI FSPI
- #endif
- static const int spiClk = 1000000; // 1 MHz
- //uninitialised pointers to SPI objects
- SPIClass * baro_vspi = NULL;
- SPIClass * imu_vspi = NULL;
- void setup() {
- delay(1000);
- Serial.begin(115200);
- delay(1000);
- Serial.println("Starting Setup");
- baro_vspi = new SPIClass(VSPI);
- baro_vspi->begin(SPI_CLK, SPI_MISO, SPI_MOSI, SPI_SS_BARO);
- pinMode(baro_vspi->pinSS(), OUTPUT);
- digitalWrite(baro_vspi->pinSS(), HIGH);
- imu_vspi = new SPIClass(VSPI);
- imu_vspi->begin(SPI_CLK, SPI_MISO, SPI_MOSI, SPI_SS_IMU); // comment this line out to make it "work"
- pinMode(imu_vspi->pinSS(), OUTPUT);
- digitalWrite(imu_vspi->pinSS(), HIGH);
- }
- void loop() {
- spi_Command(baro_vspi, 0b01010101); // junk data to illustrate usage
- delay(100);
- spi_Command(imu_vspi, 0b01010101); // junk data to illustrate usage
- delay(100);
- }
- void spi_Command(SPIClass *spi, byte data) {
- spi->beginTransaction(SPISettings(spiClk, MSBFIRST, SPI_MODE0));
- digitalWrite(spi->pinSS(), LOW);
- spi->transfer(data); // code hangs here, doesn't return
- digitalWrite(spi->pinSS(), HIGH);
- spi->endTransaction();
- }
This wouldn't be the end of the world...I have adjusted the code to use only a single SPI class and just manage all the chip selects manually, rather than referencing SPIClass->pin_SS().
But I'm at an impasse now because I also use the u8g2 display library, which itself sets up an SPI object for the display. Once u8g2.begin() is called, the code hangs. I presume behind the scenes, the u8g2 library is initializing the display and starting an SPI transfer, which is where the example code gets stuck too.
Does anyone know what changed between v2 and v3? And is there a better way to handle SPI devices now that you seem to not be able to call spi->begin() more than once for the same bus?
Thanks for the help!
(link to related post in Arduino forum, if helpful)
https://forum.arduino.cc/t/multiple-spi ... /1275440/3