Categories
Arduino Raspberry Pi Pico RTOS

Raspberry Pi Pico W RTOS and Wi-Fi

It has been a few months since I posted last, but I’ve had a few frustrating months working on the Speech Timer project with a Raspberry Pi Pico W, RTOS, and Wi-Fi. It does not help that I can only work on this project for about 4 to 8 hours a week. This post will go into a few of the issues that I’ve run into and hoping to get the community to help me narrow down the issue.

Starting this project, I used CircuitPython to get the project up and running. It went fairly smoothly, but I ran into some issues that made it not such a good solution. Firstly, the program ran fine most of the time, but after some time, the heap would run out of enough contiguous space and would crash. This issue was still prevalent with garbage collection being called frequently and other optimizations to reduce heap fragmentation. I then turned my attention to C++ as it is possible to create more rock solid code than CircuitPython and the behavior is more predicable. Another reason for looking into C++ is I wanted to use RTOS to help make the user interaction more responsive.

The first problems to show up were with the IRremote library but most of the issues were a result of how the library is built. Once those limitations were mostly understood, it was possible to have the IR Remote working reliably.

The next issue came with the Wi-Fi connection. I still have not sorted it out, but still trying. I can get Wi-Fi working just fine if the FreeRTOS library is not included, but as soon as it is added, the Raspberry Pi Pico W attempts to connect to Wi-Fi, then locks up.

Here is some information on my setup:

  • BOARD: Raspberry Pi Pico W
  • IDE: Arduino IDE 2.2.1
  • CORE: Raspberry Pi Pico W core written by Earle F. Philhower, III
  • PROGRAMMING METHOD: Picoprobe

I made some changes to the WiFiMulti library to allow clearing the AP list and print out some status messages to let me know where things seem to be falling apart.

Below is the modified WiFiMulti.h file.

/*
WiFiMulti.h - Choose best RSSI and connect
Copyright (c) 2022 Earle F. Philhower, III

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

Modified by Ivan Grokhotkov, January 2015 - esp8266 support
*/

#pragma once

#include <list>
#include <stdint.h>
#include "wl_definitions.h"

class WiFiMulti {
public:
WiFiMulti();
~WiFiMulti();

bool addAP(const char *ssid, const char *pass = nullptr);

void clearAPList();

uint8_t run(uint32_t to = 10000);

private:
struct _AP {
char *ssid;
char *pass;
};
std::list<struct _AP> _list;
};

Below is the modified WiFiMulti.cpp file.

/*
WiFiMulti.cpp - Choose best RSSI and connect
Copyright (c) 2022 Earle F. Philhower, III

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

Modified by Ivan Grokhotkov, January 2015 - esp8266 support
*/

#include "WiFi.h"
#include <string.h>
#include <algorithm>

WiFiMulti::WiFiMulti() {
}

WiFiMulti::~WiFiMulti() {
while (!_list.empty()) {
struct _AP ap = _list.front();
_list.pop_front();
free(ap.ssid);
free(ap.pass);
}
}

bool WiFiMulti::addAP(const char *ssid, const char *pass) {
struct _AP ap;
if (!ssid) {
return false;
}
ap.ssid = strdup(ssid);
if (!ap.ssid) {
return false;
}
if (pass) {
ap.pass = strdup(pass);
if (!ap.pass) {
free(ap.ssid);
return false;
}
} else {
ap.pass = nullptr;
}
DEBUGV("[WIFIMULTI] Adding: '%s' %s' to list\n", ap.ssid, ap.pass);
_list.push_front(ap);
return true;
}

void WiFiMulti::clearAPList() {
while (!_list.empty()) {
struct _AP ap = _list.front();
_list.pop_front();
free(ap.ssid);
free(ap.pass);
}
}


uint8_t WiFiMulti::run(uint32_t to) {
struct _scanAP {
char *ssid;
char *psk;
uint8_t bssid[6];
int rssi;
};
std::list<struct _scanAP> _scanList;

// If we're already connected, don't re-scan/etc.
if (WiFi.status() == WL_CONNECTED) {
return WL_CONNECTED;
}

DEBUGV("[WIFIMULTI] Rescanning to build new list of APs\n");
int cnt = WiFi.scanNetworks();
if (!cnt) {
return WL_DISCONNECTED;
}

// Add all matching ones to the scanList
for (int i = 0; i < cnt; i++) {
for (auto j = _list.begin(); j != _list.end(); j++) {
if (!strcmp(j->ssid, WiFi.SSID(i))) {
_scanAP itm;
itm.ssid = j->ssid;
itm.psk = j->pass;
WiFi.BSSID(i, itm.bssid);
itm.rssi = WiFi.RSSI(i);
_scanList.push_front(itm);
}
}
}
// Sort by RSSI using C++ lambda magic
_scanList.sort([](const struct _scanAP & a, const struct _scanAP & b) {
return a.rssi > b.rssi;
});
for (auto j = _scanList.begin(); j != _scanList.end(); j++) {
DEBUGV("[WIFIMULTI] scanList: SSID: '%s' -- BSSID: '%02X%02X%02X%02X%02X%02X' -- RSSI: %d\n", j->ssid,
j->bssid[0], j->bssid[1], j->bssid[2], j->bssid[3], j->bssid[4], j->bssid[5], j->rssi);
}

// Attempt to connect to each (will be in order of decreasing RSSI)
for (auto j = _scanList.begin(); j != _scanList.end(); j++) {
DEBUGV("[WIFIMULTI] Connecting to: SSID: '%s' -- BSSID: '%02X%02X%02X%02X%02X%02X' -- RSSI: %d\n", j->ssid,
j->bssid[0], j->bssid[1], j->bssid[2], j->bssid[3], j->bssid[4], j->bssid[5], j->rssi);
uint32_t start = millis();
if (j->psk) {
WiFi.begin(j->ssid, j->psk, j->bssid);
} else {
WiFi.beginBSSID(j->ssid, j->bssid);
}
while (!WiFi.connected() && (millis() - start < to)) {
Serial1.print("^");
delay(5);
}
Serial1.print("\n");
if (WiFi.status() == WL_CONNECTED) {
Serial1.println("Connected");
return WL_CONNECTED;
}
}

// Failed at this point...
Serial1.println("Failed to Connect");
return WiFi.status();
}

The Arduino Sketch contains the following files:

  • sketch_dec02a.ino – Main Sketch file
  • Clk_SdCard.h – Header file for SD Card functions
  • Clk_SdCard.cpp – Code file for SD Card functions
  • Clk_Wifi.h – Header file for Wi-Fi functions
  • Clk_Wifi.cpp – Code file for Wi-Fi functions
  • config.h – Structure for configuration file
  • DbgPrint.h – Helper file for Serial statements
  • Defines.h – Pin definitions, programming mode, and debug flag
  • StructsAndEnums.h – Various structures and enumerations

The sketch_dec2a.ino file is shown below. All other files are included in the code.zip file in the “sketch_dec02a” folder. The zip file contains two other folders. The “lib” folder holds the two WiFiMulti library files, that may be copied into the library location. On my Windows machine, the location is “C:\Users\user\AppData\Local\Arduino15\packages\rp2040\hardware\rp2040\3.6.1\libraries\WiFi\src”. The other folder is “SDCard” and contains a sample config.txt file to be edited with your Wi-Fi configuration information and copied to the micro SD Card.
NOTE: The card reader needs to be connected to the pins defined in Defines.h, or the pin definitions need to be modified to match your setup.

/*****************************************************************************

* FreeRTOS Setup *
*****************************************************************************/
#include <FreeRTOS.h>
#include <task.h>
#include <semphr.h>
#define xPortGetCoreID get_core_num

/*****************************************************************************
* Project Files *
*****************************************************************************/
#include "DbgPrint.h" // Serial helpers
#include "Defines.h" // Pin definitions, programming mode, and debug flag
#include "config.h" // Structures for the configuration file
#include "Clk_SdCard.h" // Higher level SD Card functions
#include "Clk_Wifi.h" // Higher level Wi-Fi functions

/*****************************************************************************
* Other Includes *
*****************************************************************************/
#include <map>

/*****************************************************************************
* GLOBALS *
*****************************************************************************/
// Flags to make certain that required initialization is complete before tasks start
bool setup_complete = false;

// WiFi global settings
char last_ipaddress[16] = "";
WiFiMode_t last_WiFiMode = WIFI_OFF;

// Config global settings
unsigned long configLoadMillis = 0;
unsigned long last_configLoadMillis = 0;

// Module Objects
Clk_SdCard clockSdCard = Clk_SdCard();
Clk_Wifi clockWifi = Clk_Wifi();

/*****************************************************************************
* MAPPINGS *
*****************************************************************************/
// Mapping for string lookup
std::map<WiFiMode_t, const char *> wifiModeName{ { WIFI_OFF, "WiFi Off" }, { WIFI_STA, "Station Mode" }, { WIFI_AP, "Soft-AP Mode" }, { WIFI_AP_STA, "Station + Soft-AP Mode" } };

/*****************************************************************************
* INO FUNCTIONS *
*****************************************************************************/
void debugMessage(const char *message) {
Dbg_printf("%s\t%d\t%d\n", message, xPortGetCoreID(), rp2040.getFreeHeap());
}

/*****************************************************************************
* TASKS *
*****************************************************************************/
// void checkWiFi(void *param) {
void checkWiFi() {
// Make certain that setup has completed
while (!setup_complete) {
vTaskDelay(10 / portTICK_PERIOD_MS);
}

int loopCount = 0;

while (1) {
Dbg_print("."); // Print ... while looping so we know that the loop is running
// If the SD Card was inserted/reinserted, use the new configuration to connect to the Wi-Fi AP
if (configLoadMillis != last_configLoadMillis) {
Dbg_println("START: Restart Wi-Fi with new Config");
clockWifi.begin(&clockSdCard.sdCardConfig);
Dbg_println("Wi-Fi Begin Completed");
last_configLoadMillis = configLoadMillis;
Dbg_println("END: Restart Wi-Fi with new Config");
}

// Set the IP Address property
clockWifi.hasIpAddress();

// If the IP Address has changed, print the IP Addess
if (strcmp(last_ipaddress, clockWifi.ipAddress) != 0) {
Dbg_println("START: Update IP Address");
strcpy(last_ipaddress, clockWifi.ipAddress);
Dbg_printf("IP Address has changed to %s\n", last_ipaddress);
Dbg_println("END: Update IP Address");
}

// If the Wi-Fi Mode has changed, print debug message
if (last_WiFiMode != clockWifi.wifiMode) {
Dbg_println("START: Print Debug Statement");
last_WiFiMode = clockWifi.wifiMode;
Dbg_printf("Wi-Fi has changed to %s\n", wifiModeName[last_WiFiMode]);
Dbg_println("END: Print Debug Statement");
}

loopCount++;
// Print message with stack usage once every ten loops
if (loopCount > 10) {
debugMessage("checkWiFi");
loopCount = 0;
}
vTaskDelay(250 / portTICK_PERIOD_MS);
}
}

/*****************************************************************************
* SETUP *
*****************************************************************************/
void setup() {
Dbg_begin(115200);

// Wait for the serial port to connect
while (DEBUG && !(Serial || Serial1)) {
delay(1); // wait for serial port to connect.
}

// Print 5 blank lines on startup
for (int i = 0; i < 5; i++) {
Dbg_println("");
}

clockSdCard.begin();
// NOTE: The SD Card needs to be inserted with the Config.txt file or the code will
// never attempt to connect to Wi-Fi. In the final version, there will be a task to
// check the SD Card and reload the configuration file.
if (clockSdCard.isCardPresent()) {
configLoadMillis = millis();
}

Dbg_printf("sdcard_init_complete = %s\n", configLoadMillis != last_configLoadMillis ? "true" : "false");

debugMessage("-----------------------------");

debugMessage("END: setup()");
setup_complete = true;
}

/*****************************************************************************
* LOOP *
*****************************************************************************/
void loop() {
// The core written by Earle F. Philhower, III, requires that the Wi-Fi be
// accessed from Core 0, therefore it needs to run in the loop.
checkWiFi();
}

The code typically hangs with the following output.

START: Reading Config File

0. MySSID_One Password1

1. MySSID_Two Password2

2. MySSID_Three Password3

FINISHED: Reading Config File

sdcard_init_complete = true

----------------------------- 0 165684

END: setup() 0 165684

.START: Restart Wi-Fi with new Config

------^^------ WIFI: Added AP MySSID_One ------^^------

------^^------ WIFI: Added AP MySSID_Two ------^^------

------^^------ WIFI: Added AP MySSID_Three ------^^------

Connecting WiFi...

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Once in a great while the code runs properly, but if it failed to connect, the second attempt most likely will hang.

START: Reading Config File

0. MySSID_One Password1

1. MySSID_Two Password2

2. MySSID_Three Password3

FINISHED: Reading Config File

sdcard_init_complete = true

----------------------------- 0 165684

END: setup() 0 165684

.START: Restart Wi-Fi with new Config

------^^------ WIFI: Added AP MySSID_One ------^^------

------^^------ WIFI: Added AP MySSID_Two ------^^------

------^^------ WIFI: Added AP MySSID_Three ------^^------

Connecting WiFi...

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^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^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Failed to Connect



Failed to connect to Wi-Fi. Trying again...

------^^------ WIFI: Added AP MySSID_One ------^^------

------^^------ WIFI: Added AP MySSID_Two ------^^------

------^^------ WIFI: Added AP MySSID_Three ------^^------

Connecting WiFi...

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Then once in a while it will connect just fine.

START: Reading Config File

0. MySSID_One Password1

1. MySSID_Two Password2

2. MySSID_Three Password3

FINISHED: Reading Config File

sdcard_init_complete = true

----------------------------- 0 165684

END: setup() 0 165684

.START: Restart Wi-Fi with new Config

------^^------ WIFI: Added AP MySSID_One ------^^------

------^^------ WIFI: Added AP MySSID_Two ------^^------

------^^------ WIFI: Added AP MySSID_Three ------^^------

Connecting WiFi...

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^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i-Fi with new Config

START: Update IP Address

IP Address has changed to 192.168.1.45

END: Update IP Address

START: Print Debug Statement

Wi-Fi has changed to Station Mode

END: Print Debug Statement

..........checkWiFi 0 164684

...........checkWiFi 0 164684

...........checkWiFi 0 164684

...........checkWiFi 0 164684

...........checkWiFi 0 164684

...........checkWiFi 0 164684

.......

If you have any thoughts on what may be happening, please comment to let me know. Also check the discussion board on Earle’s core library on GitHub at https://github.com/earlephilhower/arduino-pico/discussions?discussions_q=RTOS. I plan to post a comment there and point to this post.

Thank you in advance for any help on this issue.

UPDATE 3 December 2023

I had found that the delay statement in the WiFIMulti.ccp file was causing the issue with FreeRTOS. I modified it with the blink without delay example and the code worked. I created a pull request for the change and hope it will become part of the code base in the future so hopefully no one else will run into this issue.

UPDATE 4 December 2023

It seems that it was dumb luck yesterday that made me think the issue was with the delay statement. I had created a pull request for the change and fortunately, Earle F. Philhower, III pushed back a bit and stated that it did not make sense that delay would be causing an issue. I was doing some troubleshooting with him and Maximilian Gerhardt, then it appeared that my fix was no longer working. I had forgot that I had modified the fix to handle a rollover in millis, which was really unnecessary. When I removed that segment, the timing loop replacement for delay behaved exactly the same way. I then modified the code to use delay and remove the timing loop and put the if block back in and it worked. It appears that having only a delay in the main loop is the cause of the issue. If anyone has an idea why, please feel free to submit a comment or better yet go to the pull request and add a comment there. The link to the pull request is https://github.com/earlephilhower/arduino-pico/pull/1878.

Original block of code.

while (!WiFi.connected() && (millis() - start < to)) {
    delay(5);
}

My original modification.

while (!WiFi.connected() && (millis() - start < to)) {
	// Replaced delay(5); with the following to prevent
	// failure when using RTOS
	unsigned long tWifiDelayForRtos_start = millis();
	while (millis() - tWifiDelayForRtos_start <= 5) {
		if (millis() < tWifiDelayForRtos_start) {
			// millis wrapped around to zero. Rare to hit this
			// issue, but it will do this every 49 days.
			tWifiDelayForRtos_start = millis();
		}
	}
}

Modification without the if block. Behaved the same as the original code.

while (!WiFi.connected() && (millis() - start < to)) {
	// Replaced delay(5); with the following to prevent
	// failure when using RTOS
	unsigned long tWifiDelayForRtos_start = millis();
	while (millis() - tWifiDelayForRtos_start <= 5) {
		;
	}
}

Modification with delay. Note that all the other code in while loop is unnecessary as it does nothing useful. It is only needed to keep the code running, but why?

while (!WiFi.connected() && (millis() - start < to)) {
	// Replaced delay(5); with the following to prevent
	// failure when using RTOS
	unsigned long tWifiDelayForRtos_start = millis();
	if (millis() < tWifiDelayForRtos_start) {
		// millis wrapped around to zero. Rare to hit this
		// issue, but it will do this every 49 days.
		tWifiDelayForRtos_start = millis();
	}
	delay(5);
}

UPDATE 5 December 2023

Finally got to the bottom of this issue. There was a check-in of new code around the same time as I posted about this issue. The change was in the FreeRTOS code in the file variantHooks.cpp. The call to portENABLE_INTERRUPTS(); was moved from before the call to vTaskPreemptionEnable(nullptr) to after the call. That change addressed the exact problems that I was seeing.

Glad to see that this is finally addressed. I think this may be related to some other issues that I experienced, but not certain. Now I can get back to the project and hopefully wrap it up soon.

If you are interested, the commit for the actual fix is d2461a1.

By richteel

Software and hardware developer who likes learning new things with a passion of sharing knowledge with others.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from TeelSys

Subscribe now to keep reading and get access to the full archive.

Continue reading