Program Teensy 4 from command line without pushing the button

mah115

Member
Hi,
I'm trying to figure out a way to program the Teensy 4.1 from the command line (Ubuntu) without pushing the button because it will be sealed inside a box. Are there any workarounds to be able to do this (without using hardware to pull down the programming pin)?
 
Yes, use the teensy_loader_cli with the '-s' option.

Yeah, I tried that, but it didn't work. Unless I'm doing it wrong or something...it hangs when I program it:

Code:
mike@ubuntu:~/Downloads/teensy_loader_cli-master$ teensy_loader_cli --mcu=TEENSY40 blink_slow_Teensy40.hex -vs
Teensy Loader, Command Line, Version 2.2
Read "blink_slow_Teensy40.hex": 12132 bytes, 0.6% usage
Error opening USB device: No error
Waiting for Teensy device...
 (hint: press the reset button)
 
Last edited:
We'll need more details - is it hanging on
Code:
Waiting for Teensy device...
 (hint: press the reset button)

Or is there some other problem? Have you installed udev rules? Is the teensy connected via USB and powered?
 
The "Error opening USB device: No error" may suggests a bad cable or some other software on the system has taken control of the USB Serial port or the Teensy doesn't have an active USB Stack running to recognize the incoming request.

There was a recent update to the (udev?) "RULES" with TD 1.54 Beta 7 to account for new linux behavior
 
The "Error opening USB device: No error" may suggests a bad cable or some other software on the system has taken control of the USB Serial port or the Teensy doesn't have an active USB Stack running to recognize the incoming request.

There was a recent update to the (udev?) "RULES" with TD 1.54 Beta 7 to account for new linux behavior

It works if I push the button, so it doesnt' seem to be a port access or cable issue.
I installed the UDEV rules already.

The -s options says "Perform a sort (soft?) reset request by searching for any Teensy running USB Serial code built by Teensyduino." I'm confused by that statement. What USB Serial code am I supposed to be running for it to work?
Thanks
 
I just checked my use case again. If I don't have the Teensy plugged in and powered on, my readout is:
Code:
Teensy Loader, Command Line, Version 2.1
Read "spi_example.hex": 15572 bytes, 1.5% usage
Error opening USB device: No error
Waiting for Teensy device...
 (hint: press the reset button)

With Teensy plugged in and powered on, I get:
Code:
Teensy Loader, Command Line, Version 2.1
Read "spi_example.hex": 15572 bytes, 1.5% usage
Soft reboot performed
Waiting for Teensy device...
 (hint: press the reset button)
Found HalfKay Bootloader
Read "spi_example.hex": 15572 bytes, 1.5% usage
Programming................
Booting

This is on Ubuntu 20.04. Notice I also get the "Error opening USB device: No error" if no device is present, otherwise I don't.

Typically, if my code is able to keep the Teensy up and running, the USB stack works (it's part of core) and I can soft-reboot all day long. If it's a brand new custom board, or I load some bad code (i.e. out of bounds memory access), then I need to press the button to get Teensy to come back up. This roughly correlates to the Arduino use case and whether I see Teensy in my ports list (USB working) or I see a teensy_hid device (USB not yet working).

Maybe try the command line upload with soft reboot on a simple blink sketch to rule out a bad program / sketch causing the issue?
 
When you say plugged in, you mean plugged into and powered off of the USB, or are you using an external power supply? Which Teensy are you using? I've been using the Teensy 4.0 by itself (not plugged into any board), powered off USB, and using the Blink sketch provided with the CLI loader.
 
Oh nevermind, I was testing with a Teensy 3.6, since that was what I happened to be working on. Tried again with the Teensy 4.1 and I can repeat the issue - no soft reboot working for me either.

Interesting that I'm getting a different message though:
Code:
Teensy Loader, Command Line, Version 2.1
Read "spi_example.hex": 18396 bytes, 0.9% usage
Unable to soft reboot with USB error: error sending control message: Broken pipe
Waiting for Teensy device...
 (hint: press the reset button)

Maybe because I'm not on version 2.2?
 
Thanks for testing for me, I really appreciate it :). I suppose the architecture of the Teensy 4 is a bit different.
 
Maybe because I'm not on version 2.2?
The broken pipe error was caused by a too short "SET_LINE_REQUEST_TYPE" message. The system was expecting 7 bytes (4 bytes for speed, 1 for stop bit, 1 for data bits, and another for parity) but the loader was only sending 1 byte. Grab the latest version from Github; it's fixed there.
 
The broken pipe error was caused by a too short "SET_LINE_REQUEST_TYPE" message. The system was expecting 7 bytes (4 bytes for speed, 1 for stop bit, 1 for data bits, and another for parity) but the loader was only sending 1 byte. Grab the latest version from Github; it's fixed there.

Thanks, that works for me on Teensy 4.1:
Code:
Teensy Loader, Command Line, Version 2.2
Read "spi_example.hex": 18396 bytes, 0.9% usage
Soft reboot performed
Waiting for Teensy device...
 (hint: press the reset button)
Found HalfKay Bootloader
Read "spi_example.hex": 18396 bytes, 0.9% usage
Programming...............
Booting

So now mah115, I'm not sure again why it's not working for you. I just tried your exact command using the hex file from the teensy_loader_cli github and it works fine. Teensy 4.1 plugged in with a USB and that's it:
Code:
$./teensy_loader_cli --mcu=TEENSY40 blink_slow_Teensy40.hex -vs
Teensy Loader, Command Line, Version 2.2
Read "blink_slow_Teensy40.hex": 12132 bytes, 0.6% usage
Soft reboot performed
Waiting for Teensy device...
 (hint: press the reset button)
Found HalfKay Bootloader
Read "blink_slow_Teensy40.hex": 12132 bytes, 0.6% usage
Programming.........
Booting
 
I can duplicate the problem on Raspbian if I load "blink_slow_Teensy40.hex" into a Teensy4.1 and then try and load it again.
Code:
me@raspberrypi:~/Documents/src/teensy_loader_cli $ ./teensy_loader_cli --mcu=TEENSY41 blink_slow_Teensy40.hex -v -s
Teensy Loader, Command Line, Version 2.2
Read "blink_slow_Teensy40.hex": 12132 bytes, 0.1% usage
Soft reboot performed
Waiting for Teensy device...
 (hint: press the reset button)
Found HalfKay Bootloader
Read "blink_slow_Teensy40.hex": 12132 bytes, 0.1% usage
Programming.........
Booting
me@raspberrypi:~/Documents/src/teensy_loader_cli $ ./teensy_loader_cli --mcu=TEENSY41 blink_slow_Teensy40.hex -v -s
Teensy Loader, Command Line, Version 2.2
Read "blink_slow_Teensy40.hex": 12132 bytes, 0.1% usage
Error opening USB device: No error
Waiting for Teensy device...
 (hint: press the reset button)

But, if I compile the blink example for the Teensy4.1 and load that hex file, it works just fine:

Code:
me@raspberrypi:~/Documents/src/teensy_loader_cli $ ./teensy_loader_cli --mcu=TEENSY41 blink_slow_Teensy41.hex -v -s
Teensy Loader, Command Line, Version 2.2
Read "blink_slow_Teensy41.hex": 13428 bytes, 0.2% usage
Soft reboot performed
Waiting for Teensy device...
 (hint: press the reset button)
Found HalfKay Bootloader
Read "blink_slow_Teensy41.hex": 13428 bytes, 0.2% usage
Programming...........
Booting
me@raspberrypi:~/Documents/src/teensy_loader_cli $ ./teensy_loader_cli --mcu=TEENSY41 blink_slow_Teensy41.hex -v -s
Teensy Loader, Command Line, Version 2.2
Read "blink_slow_Teensy41.hex": 13428 bytes, 0.2% usage
Soft reboot performed
Waiting for Teensy device...
 (hint: press the reset button)
Found HalfKay Bootloader
Read "blink_slow_Teensy41.hex": 13428 bytes, 0.2% usage
Programming...........
Booting

The hex files for the blink example compiled for Teensy4.0 and Teensy4.1 are different.
 
Thanks everyone, I was able to get it to work on Linux (but not OSX or Windows). The culprit here was the included blink_slow_teensy40.hex file (even on the Teensy 4.0)--I recompiled a fresh one, and it works. So there's probably something incompatible with the existing hex files. I opened an issue ticket on github.
 
It was the hex file itself, not a 4.0/4.1 incompatibility? Interesting.

The soft reboot code is only implemented in the Linux portion of the code. There's just a stub for OSX and Windows.
 
I'm having what appears to be the same issue here (teensy_loader_cli fails to reboot the Teensy 4.0 from a Raspberry Pi).

If I try to do this from Windows (from PlatformIO with the Teensy GUI app in the background) then the GUI app is able to perform a reset and upload the firmware, so it appears that the HalfKay bootloader is running with my firmware.

Also to note: the firmware uploads from the RPi successfully if the button is pressed, in fact, if I press the button it reports that it found the HalfKay Bootloader:

Code:
pi@raspberrypi:~/dev/teensy_loader_cli $ ./teensy_loader_cli --mcu=TEENSY40 -w -v ../firmware.hex
Teensy Loader, Command Line, Version 2.2
Read "../firmware.hex": 77824 bytes, 3.8% usage
Waiting for Teensy device...
 (hint: press the reset button)
Found HalfKay Bootloader
Read "../firmware.hex": 77824 bytes, 3.8% usage
Programming.........................................................................
Booting

Investigating..
 
Ah looking at teensy_loader_cli.c it has

Code:
int soft_reboot(void)
{
	usb_dev_handle *serial_handle = NULL;

	serial_handle = open_usb_device([B][U]0x16C0, 0x0483[/U][/B]);
	if (!serial_handle) {
		char *error = usb_strerror();
		printf("Error opening USB device: %s\n", error);
		return 0;
	}

	char reboot_command[] = {0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08};
	int response = usb_control_msg(serial_handle, 0x21, 0x20, 0, 0, reboot_command, sizeof reboot_command, 10000);

	usb_release_interface(serial_handle, 0);
	usb_close(serial_handle);

	if (response < 0) {
		char *error = usb_strerror();
		printf("Unable to soft reboot with USB error: %s\n", error);
		return 0;
	}

	return 1;
}

but the serial device has a different product ID for Teensy 4.0:

Code:
pi@raspberrypi:~/dev/teensy_loader_cli $ lsusb
Bus 001 Device 009: ID [B][U]16c0:048a[/U][/B] Van Ooijen Technische Informatica
Bus 001 Device 004: ID 0bda:818b Realtek Semiconductor Corp. RTL8192EU 802.11b/g/n WLAN Adapter
Bus 001 Device 005: ID 0483:5750 STMicroelectronics
Bus 001 Device 006: ID 0424:7800 Standard Microsystems Corp.
Bus 001 Device 003: ID 0424:2514 Standard Microsystems Corp. USB 2.0 Hub
Bus 001 Device 002: ID 0424:2514 Standard Microsystems Corp. USB 2.0 Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

Changing that line in the code to match the correct product ID, then rebuild gets:

Code:
pi@raspberrypi:~/dev/teensy_loader_cli $ ./teensy_loader_cli --mcu=TEENSY40 -s -v ../firmware.hex
Teensy Loader, Command Line, Version 2.2
Read "../firmware.hex": 77824 bytes, 3.8% usage
Soft reboot performed
Waiting for Teensy device...
 (hint: press the reset button)
Found HalfKay Bootloader
Read "../firmware.hex": 77824 bytes, 3.8% usage
Programming.........................................................................
Booting

It automatically uploaded without pressing the reset button

SUCCESS!
 
Last edited:
Back
Top