Controlling crash/restart behavior in Teensy 4

dmhummel

Well-known member
I have a remote installation of a Teensy 4 controlling electric motors. Occasionally, the system can crash due to a bug or extremely invalid input situations. This in itself is rare, and the occurrence with self-restart is fine. However, the pins controlling PWM signals output arbitrary data and potentially cause the electric motors to spin unsafely for multiple seconds.

Is it possible to capture the general class of exceptions that cause a system crash to ensure a controlled restart? Can I use any further tools to capture the relevant information and communicate it back? Any other suggestions?

A hardware/transistor/mosfet solution to disconnect the PWM pins or motors power supply is not possible at this time.
 
Yes, you can customize fault handling.

Teensy 4 already has a default fault handler which captures basic info. You can read the info at startup with CrashReport.

Probably the simplest way to get started would involve editing the default handler. Currently it's in startup.c.

https://github.com/PaulStoffregen/cores/blob/master/teensy4/startup.c#L542

If you just want to reboot faster, you could edit or delete the loop which waits for 8 seconds. Or you could edit this code any way you like.

If you want to customize for specific ARM exceptions instead of using 1 default handler for all faults, you could create your own handler and write its address into the interrupt vector table.

But to get started, probably easiest to just modify the existing default handler.
 
Thanks, Paul! That was a great answer, and I learned quite a bit by reading through the code. Two followup questions:

1) I assume the timers controlling the PWM behavior are no longer operating. The external motor controller interprets the pins as being on a 50hz 2MS duty cycle so probably they have been pushed high? Would it be reasonable to set the pins low (7,8,14,15) in the fault handler, similar to the commented code for blinking the LED?

2) What is the recommended way to override the handler function? I am using platformIO and could modify the teensy framework src, but that would be reverted each time I set up/clean the environment.
 
For others using platformIO, I found this to be a reasonable approach to replacing the file:
1) Add a pre-build script to your env
Code:
[env:teensy_flight_computer]
platform = teensy
board = teensy40
extra_scripts = pre:apply_patches.py

2) Add a python script that can find and make the appropriate change:
Code:
from os.path import join

Import("env")

FRAMEWORK_DIR = join(env.PioPlatform().get_package_dir("framework-arduinoteensy"),"cores","teensy4","startup.c")
print(FRAMEWORK_DIR)


search = "if (++count >= 80) break;  // reboot after 8 seconds"
replace = "break;  // MODIFIED - reboot immediately"
  
with open(FRAMEWORK_DIR, 'r') as file:
  

    data = file.read()

    if data.find(search):
        data = data.replace(search, replace)
    else:
        exit()
  

with open(FRAMEWORK_DIR, 'w') as file:
    file.write(data)
  
# Printing Text replaced
print(f"Updated startup.c line {search} to {replace}")
 
Back
Top