FreeRTOS and WiFi

I’ve checked an old thread, I think the bug is still present.
Possible bug in FreeRTOS and AtWiFi Arduino library

I’m using rpcWiFi.h, just including the file in the code will make the program crash, without rpcWiFi.h the below program will execute as expected, and when crashed it will show the following message:

******************************
        Program start         
******************************


ASSERT: vPortEnterCritical :#420
#include <Seeed_Arduino_FreeRTOS.h>
#include <TFT_eSPI.h>
//#include "rpcWiFi.h"


TaskHandle_t Handle_aTask;
TaskHandle_t Handle_bTask;
TaskHandle_t Handle_monitorTask;
TaskHandle_t Handle_wifiscanTask;

TFT_eSPI tft = TFT_eSPI();
TFT_eSprite img = TFT_eSprite(&tft);
TFT_eSprite img2 = TFT_eSprite(&tft);

static void LCD_TASK_1 (void* pvParameters) {
    Serial.println("Thread A: Started");
    img.createSprite(100, 100);
    img.fillSprite(tft.color565(229,58,64)); // RED
    img.setTextSize(8);
    img.setTextColor(TFT_WHITE);
    for(int i = 0; i < 100; i++) {
        img.drawNumber(i, 10, 25);
        img.pushSprite(30, 70);
        img.fillSprite(tft.color565(229,58,64));
        delay(1000);
        if(i== 99) i = 0;
    }
}

static void LCD_TASK_2 (void* pvParameters) {
    Serial.println("Thread B: Started");
    img2.createSprite(100, 100);
    img2.fillSprite(tft.color565(48,179,222)); // BLUE
    img2.setTextSize(8);
    img2.setTextColor(TFT_WHITE);
    for(int x = 99; x >= 0; x--) {
        img2.drawNumber(x, 10, 25);
        img2.pushSprite(190, 70);
        img2.fillSprite(tft.color565(48,179,222));
        delay(500);
        if(x== 0) x = 99;
    }
}

static void WIFI_SCAN_TASK (void* pvParameters)
{
    Serial.println("Wifi Scan: Started");
    
    for(int y = 99; y >= 0; y--) {
        Serial.println("Test");
        delay(5000);
        if(y== 0) y = 99;
    }

}

void taskMonitor(void* pvParameters) {
    int x;
    int measurement;

    Serial.println("Task Monitor: Started");

    // run this task a few times before exiting forever
    for (x = 0; x < 10; ++x) {

        Serial.println("");
        Serial.println("******************************");
        Serial.println("[Stacks Free Bytes Remaining] ");

        measurement = uxTaskGetStackHighWaterMark(Handle_aTask);
        Serial.print("Thread A: ");
        Serial.println(measurement);

        measurement = uxTaskGetStackHighWaterMark(Handle_bTask);
        Serial.print("Thread B: ");
        Serial.println(measurement);

        measurement = uxTaskGetStackHighWaterMark(Handle_wifiscanTask);
        Serial.print("WiFi Scan: ");
        Serial.println(measurement);

        measurement = uxTaskGetStackHighWaterMark(Handle_monitorTask);
        Serial.print("Monitor Stack: ");
        Serial.println(measurement);

        Serial.println("******************************");

        delay(10000); // print every 10 seconds
    }

    // delete ourselves.
    // Have to call this or the system crashes when you reach the end bracket and then get scheduled.
    Serial.println("Task Monitor: Deleting");
    vTaskDelete(NULL);
}

void setup() {

    Serial.begin(115200);

    vNopDelayMS(1000); // prevents usb driver crash on startup, do not omit this
    while (!Serial) ;  // Wait for Serial terminal to open port before starting program

    tft.begin();
    tft.setRotation(3);
    tft.fillScreen(tft.color565(9,7,7)); // BLACK background
    tft.setTextColor(tft.color565(239,220,5)); // YELLOW text
    tft.setTextSize(2);
    tft.drawString("Thread A", 30, 50);
    tft.drawString("Thread B", 190, 50);

    Serial.println("");
    Serial.println("******************************");
    Serial.println("        Program start         ");
    Serial.println("******************************");

    //Set WiFi to station mode and disconnect from an AP if it was previously connected
    // WiFi.mode(WIFI_STA);
    // WiFi.disconnect();
    // delay(100);
    // Serial.println("WiFi disconnected");


    // Create the threads that will be managed by the rtos
    // Sets the stack size and priority of each task
    // Also initializes a handler pointer to each task, which are important to communicate with and retrieve info from tasks
    xTaskCreate(LCD_TASK_1,     "Task A",       256, NULL, tskIDLE_PRIORITY + 3, &Handle_aTask);
    xTaskCreate(LCD_TASK_2,     "Task B",       256, NULL, tskIDLE_PRIORITY + 2, &Handle_bTask);
    xTaskCreate(taskMonitor, "Task Monitor",    128, NULL, tskIDLE_PRIORITY + 1, &Handle_monitorTask);
    xTaskCreate(WIFI_SCAN_TASK,     "WiFi Scan",   256 , NULL, tskIDLE_PRIORITY + 3, &Handle_wifiscanTask);

    // Start the RTOS, this function will never return and will schedule the tasks.
    vTaskStartScheduler();
}

void loop() {
    //NOTHING
}

I second that. rpcWiFi is doing some RTOS manipulation under the hood making use of tasks impossible.
I managed to run tasks for some time by removing vTaskStartScheduler(); and locating xTaskCreate before any WiFi statements, but after connection to WiFi tasks stop.

It is really sad that this is not resolved for such a long time for such a great devices lineup. :frowning:

Any way to do this? I’m having the same problem.

Hi there,
What Hardware is this running on?
You’ll need a Dual core to do it BTW, One core runs the WIFI and they work.
Look into that.
HTH
GL :slight_smile: PJ
:v:

Hello there, thanks for answering, I am using a Wio terminal, which has an ARM Cortex-M4F, is it possible that it has only 1 core and thus cannot use FreeRTOS and WiFi at the same time?

Hi there,
No wio-terminal has a separate realtek wifi chip, so probably a configuration issue.
Check the Wio-term WiKi :v:
HTH
GL :slight_smile: PJ

I’m sorry for the inconvenience caused. We will re-evaluate this issue and provide solutions as soon as possible. I will reply under this thread as soon as I have results. Thank you for all your patience!

I just spoke with the engineer, and they confirmed that the current WiFi library already includes FreeRTOS. So you don’t need to use a separate FreeRTOS library. If you want to use both WiFi and FreeRTOS at the same time, you can just create Task directly. Below is an example of how to use the Wio Terminal’s WiFi functionality along with multiple Task:

1 Like