I've got an Arduino Due connected to a Raspberry Pi via Serial3. The Due will randomly stop sending data to the Pi after a few days, or sometimes after several hours. Using an oscilloscope, I can see that the Pi keeps transmitting to the Due, but there is no response from the Due. I'm also getting the Due to echo everything it gets from the Pi to Serial, which is connected to my PC. I can see that the Due will stop echoing characters mid way through receiving a message from the Pi.
I then set one of the pins as an output and just keep toggling that regularly. When the Due is waiting for data from the Pi, this signal has a frequency of about 100Hz, i.e. a period of 10ms. I have a counter that goes from 0 to 1000 to control this frequency somewhat. When this error happens and communications stops, I can see that the pulses now have a period of 2.14 seconds, i.e. 214 times slower.
I'm using Arduino-MemoryFree to check for memory leaks but I haven't found any problems yet and I don't seem to be running out of memory. In case it matters, I'm also using the ArduinoJson library. Arduino IDE version is 1.6.7, I've updated the Arduino SAM Boards package to version 1.6.9 (problem was present before the update as well).
Could someone please advise me on what could be causing this slow down?
-
3Memory leaks don't just show up as memory shrinking - they also show up as variables being corrupted from such things as buffer overruns. You need to post your code so we can see where it's likely to be failing.Majenko– Majenko2016年09月28日 12:59:14 +00:00Commented Sep 28, 2016 at 12:59
-
1Its most likely a memory leak, but another option could be memory fragmentation, due to overuse of the new and delete command.Code Gorilla– Code Gorilla2016年09月28日 13:02:57 +00:00Commented Sep 28, 2016 at 13:02
-
Maybe PMC registers gets corrupted and Main Clock was switched to Slow Clock. You can try to lock PMC settings so it can't be changed accidentaly.KIIV– KIIV2016年09月28日 17:49:14 +00:00Commented Sep 28, 2016 at 17:49
-
@KIIV How do I lock those settings? Once locked, what happens when code attempts to overwrite them? Will the Arduino Due reset, or crash thoroughly (in which case the watchdog timer can still recover so I'm happy with that too)?chippies– chippies2016年09月30日 14:00:08 +00:00Commented Sep 30, 2016 at 14:00
-
@Matt I suspected that too, except I'm not using new/delete and the ArduinoJson library uses a fixed size buffer on the stack. Could the built-in String class be causing problems (it sees a lot of use)? I read there was a problem with it, but the version number mentioned was v1.0.5 of the IDE.chippies– chippies2016年09月30日 14:03:51 +00:00Commented Sep 30, 2016 at 14:03
1 Answer 1
The Due will randomly stop sending data to the Pi after a few days, or sometimes after several hours.
Here is the typical manifestation of heap fragmentation in an Arduino sketch.
Solution:
- Remove as many
String
s as possible and replace them withchar[]
- Use a
StaticJsonBuffer
instead of aDynamicJsonBuffer
If you remove all the String
s and use a StaticJsonBuffer
, your program will run without any heap. No heap, no fragmentation! Not only it will be more reliable, it will also be smaller and faster.
If you must use String
, then call String::reserve()
to make sure the size is the same at each iteration. Indeed, repeated allocation of the same size doesn't increase fragmentation.
-
In the end, I changed the system architecture to avoid JSON processing on the Due. That fixed the issue, so I'm marking this as the solution.chippies– chippies2018年11月13日 09:52:11 +00:00Commented Nov 13, 2018 at 9:52