Mutex for ESP8266

I don’t know what can interrupt user code on the ESP8266 or what might do the interrupting, the documentation is lacking in that area, but something seems to be causing me a timing condition. With a really low time out on an NTP call I seemed to end up in the receive and the timeout code at the same time. The timeout kills the connection and the receive disarms the timeout, so in theory only one should ever be run, but what happens if the timeout occurs between the receive code being called and it actually disarming the timeout? Will the timeout interrupt the receive, kill the connection and then allow the receive code to continue? That is what it looked like, so I did the obvious thing and searched the API for some kind of mutex. No joy, but the whole API isn’t documented so I grepped the SDK libraries for any function name that might be relevant, still no joy. I’ve come to the conclusion the SDK does not provide any such functionality, so I had to get my hands dirty with some assembler…

Armed with a copy of the ‘Tensilica Xtensa Instruction Set Architecture‘ reference manual I tested out the S32C1I operation. The GCC compiler doesn’t know this instruction or the special register it uses (SCOMPARE1), so I wrote something similar and patched the right opcodes into the assembled object file. Unfortunately trying to write to this register reset the device, so it looks like the ESP8266 does not support the ‘Conditional Store Option’, darn.

So, I’m not sure how I ended up at it but I found some Linux kernel code with what appears to offer the solution and forms the basis of my esp8266 mutex code. The code is easy enough to use – declare a mutex_t, call CreateMutux against it, then GetMutex and ReleaseMutex as required.

Post navigation

2 thoughts on “Mutex for ESP8266”

Hi Richard, love your work. I am doing a bunch of stuff with the micropython project on the ESP and used your mutex for synchronising the interrupt handler and user time reading or the DHT22.

Now, some time later I come back to have a look at your DS3231 i2c code (because mine is not working).

(I have also had to deal with the word boundary issue of the flash. How I solved it is I make a word array with a python app of strings and use a little decoder in C. Apparently we are getting pre-compiled bytecode streams in micropython soon so I’ll be sure storing that in the SPI connected flash).
Funny thing is I have 100 lines of python but have written 1000 of C to support it on the ESP. I’m not sure if that qualifies me as a real man or not.

Richard, does your observation on receive and timeouts looks anything like my struggle ( see below ) ? Using actually already your mutex code, thanks for that. Because from the ESP8266 doc it’s not clear what code can be interrupted by other : software & hardware timers, tasks and all sorts of callbacks. The way the SDK works feels like “callback hell”.

——
When developing the embedded software on ESP8266, I regularly come into situations where the system stalls and the watchdog kicks in to reset the system.
I get error message : “mac 674” and the system freezes.

Is there a list of all possible error messages coming from this layer ? The “mac nnn” list.

The code is in a : “while(1) ;” loop. at lmacProcessTxSuccess +0x39
See disassembly :