Intercepting teensy 4 firmware update?


Hello, is there a way of getting a "heads up" flag or similar when a teensy 4 firmware update is going to happen? I have some DC motors connected to my Teensy 4 and whenever i engage in a firmware update they go full blast while the update is happening. I'm using PWM and some control pins to control the motors and they're set so that the motors are turned off at startup, but it seems the update is changing some pins but doesn't reset them all to their default condition?


To get this you would need to edit the USB code which receives the request from your PC to reboot into programming mode.

The earliest part is inside the endpoint0_complete() function.

You can see this starts a software timer which causes the actual reboot to happen 10ms later. So if you call a function here, you'll get a 10ms early warning. You could also edit the code to increase the amount of time.

FWIW, this software timer probably isn't necessary anymore. In the early days of Teensy many Windows computers had really buggy USB drivers where disconnecting the USB while in the middle of a USB control transfer sequence could crash the USB stack.

To call your code, you'll face 2 possible issues. Since this is a C file, any function in C++ you want to call needs to be create with extern "C". This code runs as an interrupt and most Arduino functions and libraries which process data aren't safe to use from interrupts. You may need to set a volatile variable, and hope your program is able to notice that variable changed within the 10ms time.

Later you can see the _reboot_Teensyduino_() function actually does the real work.

If you want to do something at the very last moment, this is the place.