How to switch between 2 different programs (different HEX) on Teensy 4/4.1?

Hi everyone,

I'm trying to figure out how to switch between 2 programs on the Teensy 4/4.1.

I would like to have an initial program that will read the state of a button when started that will redirect to 2 different programs. This same question was posted some years ago for Teensy 3.6, and I was hoping by now someone would have more information (or instructions/examples) on how to achieve this.

Paul explained that there are some difficulties jumping to different addresses, but made it clear that it's not impossible to do it.

I understand that the linker script has to be modified for the redirected programs, on Teensy 4 this is on imxrt1062.ld. However, I'm not sure how to write a code that will send me to those addresses, and it seems there might be more complications too.

The reason I want to do this is that I want to be able to change the AUDIO_BLOCK_SAMPLES 'on the fly'. This will allow me to avoid some latency issues I'm having when working with different audio applications.

If there is any other way of doing this or any help whatsoever is appreciated. Thank you.
 
Could you not use two functions, one being program#1 and the other being program#2 and determine which you use from within loop?
 
I can outline an approach, but I don't think it would be easy to do. If you search for "FlasherX" in the forum, you'll find a long thread. It's a way to update firmware over a UART serial or other comm interface rather than via the Teensy bootloader. The application that is running receives the new firmware as Intel hex records and buffers it in Flash. When the entire new firmware has been buffered and confirmed good, the existing firmware calls a function that runs from RAM and incrementally erases the existing firmware and copies the new firmware from the buffer to the "program space". When the copy is complete, the system reboots and comes up running the new program.

The way that I think you could use this to do what you want is something like this. Build your two programs and include in each of them the code from FlasherX that lets you copy a program from a specified flash location to the boot address. Find a way to combine the two hex files into a single hex file that contains 3 programs (app1, app2, app1) with the first app1 at the usual boot location, and the following app2 and app1 at known offsets from boot. Whichever app is currently running, upon a command, could copy the "buffered" app1 or app2 to the boot address and reboot. It only takes a fraction of a second to erase/write a program to flash, so maybe that's good enough?

Another way might to figure out how to compile your two application entirely in relative address mode, combine them in a single hex file as described above, and then find a way to jump to either one on command. That might be really tricky.
 
Back
Top