I would like to see how the FreeRTOS works in response to the change of a volatile global variable. However, I found that the following code will generate a “fatal error” and will not respond to any input.
And I have to do a procedure to reset it: program with CircuitPython image, and then program with Arduino.
The code is here:
#include <Seeed_Arduino_FreeRTOS.h>
TaskHandle_t Handle_aTask;
TaskHandle_t Handle_bTask;
bool stringComplete = false; // whether the string is complete
volatile int a = 0;
volatile int b = 3;
static void TASK_a (void* pvParameters) {
switch (a)
{
case 0: Serial.println("A0");break;
case 1: Serial.println("A1");break;
case 2: Serial.println("A2");break;
default: Serial.println("A Default");break;
}
}
static void TASK_b (void* pvParameters) {
switch (b)
{
case 0: Serial.println("B0");break;
case 1: Serial.println("B1");break;
case 2: Serial.println("B2");break;
default: Serial.println("B Default");break;
}
}
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
vNopDelayMS(1000); // prevents usb driver crash on startup, do not omit this
while (!Serial);
xTaskCreate(TASK_a, "Task A", 128, NULL, tskIDLE_PRIORITY + 2, &Handle_aTask);
xTaskCreate(TASK_b, "Task B", 128, NULL, tskIDLE_PRIORITY + 1, &Handle_bTask);
// Start the RTOS, this function will never return and will schedule the tasks.
vTaskStartScheduler();
}
void loop() {
// put your main code here, to run repeatedly:
if (stringComplete) {
a++;
b--;
stringComplete = false;
}
}
void serialEvent() {
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// if the incoming character is a newline, set a flag so the main loop can
// do something about it:
if (inChar == '\n') {
stringComplete = true;
}
}
}
This will generate the following sequence:
A0
fatal error
A0
fatal error
And then it does not respond to any input. Arduino will also get stuck until I unplug the Wio terminal.
Is there something wrong with my code? Or is this a problem with the library?