arduino-device-lib icon indicating copy to clipboard operation
arduino-device-lib copied to clipboard

pgm_read_word causes core panic in ESP32

Open danalvarez opened this issue 1 year ago • 1 comments

I am posting this so that it might help others in the future. I am using an ESP32-devkitC-V4 board with an ESP32-WROOM-UE module. I am using the latest arduino-esp32 core (2.0.9). I am using this library to communicate with a lora module, I have used it before in an ATMEGA328P with no issues.

The problem

When running the DeviceInfo.ino example with the library as-is, it causes the ESP32 to go into a boot loop, throwing core panics each time. I ran the stack trace through EspExceptionDecoder and it pointed me to line 1339 in TheThingsNetwork.cpp, which is:

strcpy_P(command, (char *)pgm_read_word(&(radio_table[index])));

The solution

After some time, I found this very helpful post, in which a very similar behaviour is observed in an ESP8266. The user suggests to swap pgm_read_word for pgm_read_dword, since the latter returns a 32-bit pointer. I changed all the instances of pgm_read_word in TheThingsNetwork.cpp and, voilá, it worked! No more core panics, no more reboots.

Why?

I am still unsure why this solves the problem. I understand from the post I linked before, that pgm_read_word returns a 16-bit pointer and pgm_read_dword returns a 32-bit pointer. I've also read that an Arduino int in ESP32 is 32 bits (instead of 16 as in AVR). Maybe it's related to this? Anyone got any ideas?

Final remarks

When looking up whether this library supported ESP32, I found #241 (August 2018), which fixed a compilation error for ESP32 by adding some checks before including pgmspace.h. However, the library had a huge refactor in 8a7ee08 (April 2020), significantly reducing RAM usage by placing most strings in flash. As far as I can tell, this is when compatibility with ESP32 broke. In fact, #265 describes issues very similar to mine (i.e., a boot loop), but no other comments were given... Maybe @kevin192291 still remembers how he ended up solving the issue?

Hope this helps!

danalvarez avatar May 20 '23 01:05 danalvarez

Hi i have the same issue with my esp32 and my LoRa shield. i tried all that you said but nothing work. if i can make it work i tell you

clementpra avatar Sep 12 '23 11:09 clementpra