Execute compiled code stored on an SD card from RAM

Hello all! I am currently working on a project where I would like to have different 'applications' to choose from and load them from from say an SD card into an array and execute it. I found this neat thread https://forum.pjrc.com/threads/24302-How-to-execute-code-in-SRAM-(Teensy-3) which shows how to execute Thumb-2 Instructions from RAM. The problem is I would like to be able to program the 'application' in C++, compile it, then upload it to the SD card and load it into ram and execute instead of writing the machine code / assembly and assembling it myself. The main problem I am facing, well, not really a problem but what I have came up with is that the Teensy will jump to an address similar to 0x80000000 and then start executing from there. I don't know for sure but I believe that what the compiler outputs is the full program aligned to fit within that range (I could be completely wrong). What I want to eventually get to is just a bin file that has the main app function in it (e.g. int app() ) and just copy that to an array and execute it, and then when app() returns it hands control back to the 'App loader' I have most knowledge in software instead of hardware and architecture so any help would be appreciated!
 
The memory map is different on each Teensy model, so mentioning which Teensy you're using would allow for more specific answers.

Teensy 4 also has a MMU which is enabled by default, and FlexRAM which allows the RAM1 memory to be partitioned between ITCM vs DTCM. So if you're using Teensy 4.0, 4.1, MicroMod, you'll also need to configure that stuff.

But generally speaking, the main thing you will need to do is dive into the linker script. That is the essential piece which controls how the compiler places everything into memory. Each Teensy model has its own linker script. To compile code which executes from RAM, you'll need to create your own linker script (likely copying and adapting an existing one) for the specific RAM area you wish to use.
 
I have found three linker scripts for the Teensy 4.1, which one would I be interested in?

./imxrt1062_mm.ld
./imxrt1062.ld
./imxrt1062_t41.ld
 
I have found three linker scripts for the Teensy 4.1, which one would I be interested in?

./imxrt1062_mm.ld
./imxrt1062.ld
./imxrt1062_t41.ld

Those three scripts are for the TeensyDuino devices using NXP 1062's:

./imxrt1062_mm.ld :: Sparkfun MicroMod
./imxrt1062.ld :: Teensy 4.0
./imxrt1062_t41.ld :: Teensy 4.1
 
If my understanding is correct, which it's probably not, if I make a linker script that sets the address of the executable code to some ram location that isn't being used, flash that, but not reflash the flash memory, that would keep the main "AppLoader" and I could just branch off to the address of the executable app?
 
Nope, sorry, it's nowhere near that simple.

First, Teensy Loader will only write to flash memory. It won't even open HEX files with the wrong address (for RAM).

Even if it did, the bootloader has a security feature where the first 512K of flash is always erased when you being loading new code. The first byte of incoming data causes 512K of flash to be erased, to prevent a smaller new program from being able to read the leftover portion of whatever prior program was stored in the flash. Lockable Teensy in secure mode increases the minimum erase to 1M.

You'll need to write your own code to somehow read the compiled code and put it into RAM. Teensy Loader won't do it. You have to make something, perhaps read a file from the SD card and put its data into RAM?

As I mentioned earlier, you also need to deal with the MPU. By default DTCM, RAM2 (DMAMEM, malloc) and PSRAM (EXTMEM, extmem_malloc) are not allowed to execute instructions. It is a proactive security measure meant to prevent buffer overflow bugs from easily becoming security vulnerabilities. You must change the MPU config before trying to execute any instructions from those RAM areas.
 
Back
Top