nRF905-arduino
nRF905-arduino copied to clipboard
Library not working with latest ESP32 framework (fix inside)
Hi,
I wanted to use this library for a project of mine, but encountered some problems. On my laptop the library didn't work at all and on my PC it was fine (testing with the debug sketch, via platform.io). Turns out, the ESP framework had an update recently and my laptop was using this one while my PC used a very old one. I tried a few versions, and everything above version 5.0.0 causes the library to stop working.
After some research, I found the following issue: https://github.com/espressif/arduino-esp32/issues/7556. Here a commit was references adding some functionality regarding thread safety for SPI on the ESP32: https://github.com/espressif/arduino-esp32/commit/bdeef89cc68d9076051a7d63642aab60d9d78910
This commit adds a destructor for the SPI class, which is the root of the problem. This destructor is called instantly after running the library's begin() function, causing the SPI interface to stop working (took a bit of time to figure this one out).
Inspecting the code of this library, I noticed that the SPIClass object passed in the begin() function was not passed as reference, causing the object to be copied instead. This caused the copy to be destroyed after the begin functions ends (since it no longer has references), which in turn calls the new destructor. At this point, the library doesn't work anymore. If you run the debug sketch, it will say "All registers read as 0xFF or 0x00! Is the nRF905 connected correctly?".
The fix was pretty simple though. I modified this library slightly to pass the SPI instance via the constructor as reference, and now it works again as the destructor is no longer called :) I also added that the library starts the SPI class automatically when calling begin().
You can look at it here: https://github.com/Xanderr/nRF905-arduino. If you want, I can make a PR for this?