While there are multiple posts with the (panic, hard_fault) error, none of them relates to the issue I am having.
I am running the same program on mulitple Xenons. Each Xenon is connected to a laser sensor. Some sensors are i2c and others are UART.
In the case of i2c sensors, program runs without problems.
But in the case of UART sensors, I am getting “panic, hard_fault” on average once an hour! The program keeps reading distances from the sensor then suddenly reboots.
I have (over more than a year now!) been trying different firmwares and different baud rates but the problem persists. I am running OS firmware 1.5.1 (but same problem happened with 1.5.0). I have reduced the baud rate to 57,600 (from 115200) but this did not help.
Is there anyway I can find out what causes this error?
Minimize your program down to the simplest version that still reproduces. That might help pinpoint a programming/logic error on your end that could be causing the hard-fault. Might also try posting that version here to see if anyone can see something wrong.
It’s possible to debug but can be painful. I’ve had reasonable success debugging hard-faults in the past using a JLINK and https://www.segger.com/products/development-tools/ozone-j-link-debugger/. Letting your code run with a breakpoint in the hard-fault handler will let you catch where it is hard-faulting from. You can do the same thing from Workbench using the Particle mesh debugger, but GDB doesn’t seem to always follow the stack trace out of the hard fault handler very well.
I will try your recommendations by posting a smaller version of the program and also using JLINK although I have no experience using those types of tools.
Hard faults are often caused by overshooting buffer boundaries.
With UART I guess you are gradually filling a buffer till you get a full message you are looking for and in that it’s often easy to forget to check the buffer index while receiving data
One possible place to look at is here since you are not checking for ResponseStr+index+1 being within bounds.
Also in your lw20GetStreamResponse() I'd rather go for the safe route and change
if (responseDataSize == sizeof(responseData) - 1)
to
if (responseDataSize >= sizeof(responseData) - 1)
BTW, Serial1.available() will never be more than 64 since the RX buffer is limited to that
and since you are reading even when Serial1.available() is less than 0 you will be reading 0xFF characters till you get a \n or run into timeout.
You might be better off with using a good and tested function like Serial1.readBytesUntil() and/or Serial1.find() instead of using some half baked "solution" that doesn't take into account the actual behaviour of the interface object.