Teensy 4.1 Software Reset Code

nspotts

Member
I know the Teensy 4.0/4.1 do not have reset pins or pads, but I would like to be able to reset/restart the program from a digital input (by pressing a button or an external signal). On the Teensy 4.1 page it states that a reset can be accomplished using watchdog timers or the SCB_AIRCR register. I cannot seem to find the documentation or examples of how to do this. As a side note, I would like to be able to retain the USB serial monitor connection through the reset if possible, so the serial debugging restarts after the reset. Can someone tell me how to do this?

https://www.pjrc.com/store/teensy41.html#programming
1709077241053.png
 
I had already found that and tried what was in post 3. I had the teensy hooked to a serial monitor. When I triggered the reboot, it seemed to stop everything, but it didn't restart the serial debugger. I had to do so manually. I'll double check to see if the code restarts or not.
 
USB presents on startup. Some terminal programs don't always reconnect after it drops and returns - the IDE SerMon has shown that at times.
 
I'm using PlatformIO and I'm actually testing with a Teensy 3.2 at the moment. I confirmed that the code does not restart when I change pin 23 to LOW and back HIGH until I also restart the serial monitor (the pin 13 LED stops flashing). I also confirmed if I don't have the serial monitor open when I initiate the restart, it also does not restart until I open a serial monitor. Is there another register I need to change?

C++:
#include <Arduino.h>

void doReboot() {
    SCB_AIRCR = 0x05FA0004;
}


void setup() {
Serial.begin(115200);
    while (!Serial) delay(10);

    pinMode(23, INPUT_PULLUP);
    pinMode(13, OUTPUT);
}

void loop() {
    if (digitalRead(23) == LOW) {
        doReboot();
    }

    digitalWrite(13, !digitalRead(13));
    delay(1000);

    Serial.println(digitalRead(13));
}
 
The following line while (!Serial) delay(10); will cause your program to wait forever until a serial monitor is connected...remove and/or comment out that line.

Mark J Culross
KD5RXT
 
Good call! So it now fully restarts the program, but still fails to restart logging to the serial monitor (either the Arduino IDE SM or the PlatformIO SM). Any clue has to how to reconnect the Serial monitor after restart?
 
Try like this:

Code:
void doReboot() {
    USB1_USBCMD = 0; // disconnect USB
    delay(50);       // enough time for USB hubs/ports to detect disconnect
    SCB_AIRCR = 0x05FA0004;
}

If running Windows, use version 10 or 11. Linux and MacOS are fine too. But all pre-10 Windows versions have USBSER.SYS driver bugs for USB devices unexpectedly disconnecting while the serial monitor has the port open. Know this absolutely will cause trouble with Windows 7.
 
Interesting, I'll have to try that. However, all the results I was reporting were with a windows 10 PC and a teensy 3.2. I just received my teensy 4.1 and orange pi zero 2W (running Ubuntu) today. I was able to program and monitor the 4.1 from the pi using the same code and this time the platformIO monitor reconnected immediately after restarting the teensy. Perfect. I'll test your updates with Windows tomorrow. Linux serial ports must be better :)
 
FYI, My ultimate goal is to use the pi to deliver firmware updates and allow debugging wirelessly for a hobby robotics project I'm working on. The new VSCode remote tunnel functionality is working well to run platformio remotely. Also, teamviewer is a great app for remote login to the pi. Happy so far! Thanks for all you help.
 
Last edited:
I tested the updated restart code on with a Windows 10 PC and the Teensy 4.1 with the same results as before. Neither the PlatformIO nor Arduino IDE Serial Monitors will restart after the reboot, I have to restart them manually. I did try increasing the delay to 500ms with no benefit.

Code:
void doReboot() {
    USB1_USBCMD = 0; // disconnect USB
    delay(50);       // enough time for USB hubs/ports to detect disconnect
    SCB_AIRCR = 0x05FA0004;
}

It would be nice to have a solution for Windows, but it is working fine for me on Ubuntu now, which was my intended purpose.
 
I have another issue. I've been using the Arduino-Plotter by Devin Conley and it is useful to get sensor setup and to quickly visualize datastreams. It works fine with Arduinos as well as the Teensy 3.2 (on Windows 10). However, it can't seem to detect the serial port when uploading the exact same code to the Teensy 4.1 (on Windows 10). I'm thinking it might be because of the 4.1's emulated serial port? Any suggestions? I'm open to other serial plotters. I'm aware of the Arduino IDE Serial Plotter, which I may have to switch back to as a last resort.

Also, would prefer a serial plotter that could run on Ubuntu Arm64. Haven't found one yet.
Another option would be a program to forward a serial port over Wifi (I'm not much of a networking guy).

https://github.com/devinaconley/arduino-plotter
 
Last edited:
Another option would be a program to forward a serial port over Wifi (I'm not much of a networking guy).
I found this program - tried out with the 14 day trial and it works flawlessly. It has a server install for Arm64 and after a quick config of the port to forward on the Pi and setup on Windows, I had a virtual COM port that I could connect to on my Windows PC.

However... it's $259.95 for a single license. Pretty steep. Any alternatives?
https://www.serial-over-ethernet.com/com-port-redirector/com-port-redirector-linux-windows/
 
Back
Top