In many examples of using the SPI bus there is code like:
#include <SPI.h>
SPIClass hspi(HSPI);
void setup() {
Serial.begin(115200);
Serial.println("Initializing ...");
hspi.begin();
// and so on ...
}
void loop() {
Serial.println("loop");
delay(1000);
}
but for some reason my Freenove WROVER clone simply freezes and in few seconds gets restarted by the watchdog. After much fiddling with this code I found an alternative that worked just fine:
#include <SPI.h>
SPIClass * hspi = NULL; // don't instantiate just yet
void setup() {
Serial.begin(115200);
Serial.println("Initializing ...");
hspi = new SPIClass(HSPI); // initialize the SPIClass instance here
hspi->begin();
// and so on ....
}
void loop() {
Serial.println("loop");
delay(1000);
}
I wonder if anyone knows what could be the reason for this? Apparently the board feels bad about instantiating the SPIClass before the setup() but most ESP32 examples have it exactly like this.