Uploading "bigger programs" with Platformio to T4.1 switches between working and not

Uploading "bigger programs" with Platformio to T4.1 switches between working and not

Hello,

Uploading for example the "blink" works every time.

If I upload a bigger program (394240 bytes) the first time it works, the second time the teensy gets stuck in "program" mode, then it works again, then not and so it repeats (working, not, working, not, ...)

This is with the latest Teensy 4.1 (https://www.pjrc.com/store/teensy41_4.jpg) revision. With the first version of the Teensy 4.1 (img https://www.pjrc.com/wp-content/uploads/2017/07/teensy41_4.jpg) I do not have this problem.

When it does not works, I get:

pio run --target upload:
Code:
AVAILABLE: jlink, teensy-cli, teensy-gui
CURRENT: upload_protocol = teensy-cli
Rebooting...
Uploading .pio/build/teensy41/firmware.hex
Teensy Loader, Command Line, Version 2.2
Read ".pio/build/teensy41/firmware.hex": 394240 bytes, 19.4% usage
Unable to soft reboot with USB error: Success
Waiting for Teensy device...
 (hint: press the reset button)
Found HalfKay Bootloader
Read ".pio/build/teensy41/firmware.hex": 394240 bytes, 19.4% usage
error writing to Teensy

*** [upload] Error 1
===================================================================================================================================== [FAILED] Took 3.78 seconds =====================================================================================================================================

dmesg:
Code:
[Jul13 14:39] usb 3-4.4.4.2: USB disconnect, device number 58
[  +0.358092] usb 3-4.4.4.2: new high-speed USB device number 59 using xhci_hcd
[  +0.200216] usb 3-4.4.4.2: New USB device found, idVendor=16c0, idProduct=0478, bcdDevice= 1.08
[  +0.000004] usb 3-4.4.4.2: New USB device strings: Mfr=0, Product=0, SerialNumber=1
[  +0.000003] usb 3-4.4.4.2: SerialNumber: 00129024
[  +0.050249] hid-generic 0003:16C0:0478.0021: hidraw0: USB HID v1.11 Device [HID 16c0:0478] on usb-0000:0b:00.3-4.4.4.2/input0
The red light on the Teensy keeps burning.


When it works, I get:

pio run --target upload:
Code:
AVAILABLE: jlink, teensy-cli, teensy-gui
CURRENT: upload_protocol = teensy-cli
Rebooting...
Uploading .pio/build/teensy41/firmware.hex
Teensy Loader, Command Line, Version 2.2
Read ".pio/build/teensy41/firmware.hex": 394240 bytes, 19.4% usage
Found HalfKay Bootloader
Programming..............................................................................................................................................................................................................................................................................................................................................................................................
Booting
===================================================================================================================================== [SUCCESS] Took 4.50 seconds =====================================================================================================================================

dmesg:
Code:
[Jul13 14:41] usb 3-4.4.4.2: USB disconnect, device number 59
[  +0.354009] usb 3-4.4.4.2: new high-speed USB device number 60 using xhci_hcd
[  +0.200278] usb 3-4.4.4.2: New USB device found, idVendor=16c0, idProduct=0483, bcdDevice= 2.80
[  +0.000003] usb 3-4.4.4.2: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[  +0.000003] usb 3-4.4.4.2: Product: USB Serial
[  +0.000002] usb 3-4.4.4.2: Manufacturer: Teensyduino
[  +0.000001] usb 3-4.4.4.2: SerialNumber: 12165480
[  +0.054429] cdc_acm 3-4.4.4.2:1.0: ttyACM0: USB ACM device
The red light on the Teensy goes of

... and after it works, it doesn't work anymore, and so on. :)

I've update PlatformIO and everything I can think of.

Tested it with multiple T4.1's (first revision and last revision), same results.

Anyone any ideas?
 
Check https://github.com/PaulStoffregen/t...7e674f890/teensy_loader_cli.c#L194C12-L194C12 in teensy_loader_cli.c.

Code:
		r = teensy_write(buf, write_size, first_block ? 5.0 : 0.5);

Maybe you have an old version that doesn't allow 5 seconds for erasing the prior code? Or maybe it really is taking longer than 5 seconds and this needs to increase? Or you could use the GUI Teensy Loader rather than command line. Or even use Arduino IDE just for the sake of testing and comparison.

Or it could be something else? But usually this works-every-other-time problem is timeout for erase of the prior data. It succeeds the next time because the flash got erased on the prior attempt.
 
BTW:
if I increase in my project a buffer by additional 32K where the compile log tells me as "free" still 83K left
and still all clean on compile - my project FW crashes during runtime!

The generated FW seems to crash when the size (potentially on data, buffers) exceeds a specific (but unknown) size,
even compile log reports still "free".

Try to lower size of data buffers and see when it starts to fail.
 
OK, what I have learned (on another thread).

In the compile log: the "free" there is actually your stack size left (the "free for local variables" means "stack size left").
If this value becomes too small (but no idea what "too small is") - your project can crash.

Reduce the size of other data structures, buffers... (located also in RAM1 (default)) and have more "free" (for your stack variables and region).

You can place other data structures, e.g. buffers, via DMAMEM (even FASTRUN) into other memory regions (RAM2 or ITCM), making space in RAM1 (for "free" and stack size larger).
 
In the compile log: the "free" there is actually your stack size left (the "free for local variables" means "stack size left"). If this value becomes too small (but no idea what "too small is") - your project can crash.

Yes, this is a well understood problem with all software. If your program uses more memory on the stack than is available, your program will crash. This is called "stack overflow". There's even a well known programming help website named after this issue!
 
Hello Paul,

thank you!

I was using " Version 2.2" (latest), but changed the line
Code:
		r = teensy_write(buf, write_size, first_block ? 5.0 : 0.5);
to
Code:
		r = teensy_write(buf, write_size, first_block ? 5.0 : 10);
and now it works.

Only research I did in your code was seeing that
Code:
int teensy_write(void *buf, int len, double timeout);
the last parameter was a timeout.

I don't know if I "did it right", but now it works, and now I'm using " Version 2.2.mtjs" ;).

Kind regards,

Matthias.
 
Back
Top