Hello,
I am attempting to use both cores of a Raspberry Pi Pico W:
Core 0 - WiFi and MQTT
Core 1 - Serial1 to read a serial data stream and decode it, then making it available for publication by MQTT on Core 0.
However, I end up with the Pico crashing when I attempt to pause Core 0 from Core 1.
I have simplified my attempt to date in the following test code (which excludes any of the WiFi/MQTT/actual Serial1 stuff):
-
When I simply run the risk of having Core 0 and Core 1 clash over the "stringCore1" variable (Core 1 writes to this variable; Core 0 reads it and nulls it) I have yet to have any crashes.
-
When I uncommenting out the "//rp2040.idleOtherCore();" and "//rp2040.resumeOtherCore();" code, the Pico regularly crashes (in minutes, sometimes seconds).
Any ideas? Is it 'safe' to simply not worry about pausing Core 1 from Core 0?!?
#define DEBUG // DEBUG allows for debugging information to be printed
#ifdef DEBUG
#define VERBOSE // If VERBOSE, DEBUG information includes 'Starting function()' and 'function() completed'
#endif
// heartBeat flashes onboard LED to show Pico W is operating
unsigned long heartBeatTime = millis(); // Time to make onboard LED flash when running
bool heartBeatState = true; // Onboard LED state
#ifdef DEBUG
int countCore0 = 0;
int countCore1 = 0;
String stringCore0;
String stringCore1;
#endif
void setup()
{
#ifdef DEBUG
Serial.begin(115200); // For communications to PC
rp2040.idleOtherCore(); // Allow delay to affect both cores, to ensure stable synchronisation from start
delay(10000);
rp2040.resumeOtherCore();
#endif
#ifdef VERBOSE
stringCore0 += "\n\r*** setup() started!";
#endif
pinMode(LED_BUILTIN, OUTPUT); // for heartBeat
#ifdef VERBOSE
stringCore0 += "\n\r*** setup() completed!";
#endif
}
void setup1()
{
#ifdef VERBOSE
stringCore1 += "\n\r*** setup1() started!";
#endif
#ifdef VERBOSE
stringCore1 += "\n\r*** setup1() completed!";
#endif
}
void loop()
{
#ifdef VERBOSE
stringCore0 += "\n\rStarting loop()";
countCore0++;
stringCore0 += "\n\r loop() count = " + String(countCore0);
#endif
heartBeat();
#ifdef DEBUG
printData();
#endif
#ifdef VERBOSE
stringCore0 += "\n\rloop() completed!\n\r";
#endif
}
void loop1()
{
#ifdef VERBOSE
stringCore1 += "\n\rStarting loop1()";
#endif
countCore1++;
stringCore1 += "\n\r loop1() count = " + String(countCore1);
#ifdef VERBOSE
stringCore1 += "\n\rloop1() completed!\n\r";
#endif
}
void heartBeat()
{
digitalWrite(LED_BUILTIN, heartBeatState);
if (heartBeatTime < millis())
{
heartBeatState = !heartBeatState; // Change state
heartBeatTime = millis() + 1000; // Update to 1 second from now
}
}
void printData() // Called from Core0
{
// Pause the other core (Core 1), copy stringCore1, clear stringCore1, then resume other core (Core 1)
//rp2040.idleOtherCore();
String copyOfstringCore1 = stringCore1;
stringCore1 = "";
//rp2040.resumeOtherCore();
Serial.print(stringCore0);
stringCore0 = "";
Serial.print(copyOfstringCore1);
copyOfstringCore1 = "";
}

