Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 4 of 4

Thread: Controlling crash/restart behavior in Teensy 4

  1. #1

    Controlling crash/restart behavior in Teensy 4

    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.

  2. #2
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    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.

    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.

  3. #3
    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.

  4. #4
    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
    platform = teensy
    board = teensy40
    extra_scripts =
    2) Add a python script that can find and make the appropriate change:
    from os.path import join
    FRAMEWORK_DIR = join(env.PioPlatform().get_package_dir("framework-arduinoteensy"),"cores","teensy4","startup.c")
    search = "if (++count >= 80) break;  // reboot after 8 seconds"
    replace = "break;  // MODIFIED - reboot immediately"
    with open(FRAMEWORK_DIR, 'r') as file:
        data =
        if data.find(search):
            data = data.replace(search, replace)
    with open(FRAMEWORK_DIR, 'w') as file:
    # Printing Text replaced
    print(f"Updated startup.c line {search} to {replace}")

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts