greiman / ChRt

ChibiOS/RT for Arduino AVR, SAMD, Due, Teensy 3.x, Teensy 4.0.
88 stars 26 forks source link

hard fault on Serial.print #8

Open Vigeant opened 5 years ago

Vigeant commented 5 years ago

I sometimes hit a hard fault when printing a double using the Serial.print( double a, int digit). At first, I thought it was related to the absence of a heap support but could not find any evidence of heap use. It's very hard to debug hard faults on the teensy due to the absence of a debugger.

The problem is that I am not even sure that this print is the cause of the problem but I am not sure how to debug that.

Any tips on how to debug that?

I am afraid I will have to revert back to a big loop design just to make sure nothing breaks in a month or two of uptime. It would be unfortunate as this small RTOS was so promising.

greiman commented 5 years ago

I can't help on heap use in Teensy. Maybe Paul could answer that question. I know that malloc fails in threads.

If you print a double in a thread, it may be stack overflow. It could be a thread stack or the handler stack.

Increase the size of all thread stacks and use chUnusedThreadStack() to see how much was used. Stacks are filled at startup and chUnusedThreadStack() scans for the amount used.

ChibiOS uses the ARM handler mode so there is a separate handler stack for ISRs.

I don't know how much handler stack is used by Teensy I selected 400 bytes in ChRt.h:

#define HANDLER_STACK_SIZE 400

You could increase the size and check use with chUnusedHandlerStack().

See the chBlinkPrint example for checking stack use.

Also print is not thread safe so printing in more than one thread requires use of a mutex.