Loop stop being executed with SoftAP running

I have another strange bug.

I’m using the SoftAP to host a small website (html, css and javascript file) to configure my device from the web interface.
I’m using: SYSTEM_THREAD(ENABLED);

The files (html, js and css) and on an external memory.

When refresh the webpage, this function is called once per file.

char GetFileFromFlash(const char* url, const char* filename, const char * txtType, ResponseCallback* cb, void* cbArg, Writer* result)
{
	
	if (strcmp(url, String::format("/%s", filename).c_str()) == 0) {
		Serial.printlnf("Reading %s from flash", filename);
		cb(cbArg, 0, 200, txtType, nullptr);

		// Dump file from memory...
		stopSPI = 1;
		destinationFile = myFileOpen(filename);
		if (destinationFile < 0)
		{
			// Try again
			destinationFile = myFileOpen(filename);
			if (destinationFile < 0)
			{
				ExitMyPage(cb, cbArg, result, "Open error", true);
				stopSPI = 0;
				return 1;
			}
		}

		// File length
		int size = myFileGetSize(destinationFile);

		while (size > 0)
		{
			int toRead = (size > sizeof(contentData)) ? sizeof(contentData) : size;
			int read = 0;
			read = myFileRead(destinationFile, contentData, toRead);
			if (read == toRead)
			{
				result->write(contentData, read);
 				uint32_t mil = millis();
				while ((millis() - mil) < 50)
				{
					Particle.process(); 
				}
				size -= read;
			}
			else
			{
				Serial.printlnf("%s read error: %d", filename, read);
				result->write("Read Error");
				size = 0; // on quitte
			}
		}
		myFileClose(destinationFile);
		stopSPI = 0;
		Serial.printlnf("%s read ended", filename);
		return 1;
	}
	stopSPI = 0;
	return 0;
} 

I have a Serial.println(“Loop”); in my loop code to monitor when my loop is called.

I noticed that if the Process is too short (like 10ms instead of 50ms), the loop is stop being called and never called anymore. But the softap continue to work and I can refresh webpages again and again but my usercode is not executed anymore until next reset.

uint32_t mil = millis();
while ((millis() - mil) < 50)
{
	Particle.process(); 
}

My files are bigger are between 2k and 20k. Maybe there is a buffer overflow somewhere related to the write function?

Found something just after posting. :confused:

It look like sharing the SPI is the problem so using a SINGLE_THREADED_BLOCK() for my loop code solved the problem. I might also try to use the WITH_LOCK.

2 Likes