I am looking for an example of how to load a program from sd card to SRAM and execute it from there on the SAMD21.
I understand that the given MCU use the Von Neumann architecture and all code is privileged.
I am looking for an example of how to load a program from sd card to SRAM and execute it from there on the SAMD21.
I understand that the given MCU use the Von Neumann architecture and all code is privileged.
There's two aspects to what you want to do - one (relatively) easy, and one (comparatively) hard.
The first thing you will need is some way of getting your program into RAM. That would mean some software running on the chip that will accept the program and place it into RAM then execute it. You will have to write your own bootloader-like program that runs in Flash that will perform the task.
The second, harder, thing is to compile your program in such a way that it will execute from RAM. And that's no easy task.
Ideally you want to compile the code as position independent code (with the command line flag -fPIC), but that depends on the compiler being able to do that for this chip. If not, you will have to compile it so that the code expects to reside in RAM with all addresses used for jumps, etc, going to the right location. And that brings me to the second aspect of compiling, the linker script.
If you have your code in RAM you can't also have your variables in the same place in RAM. You will have to subdivide the RAM into two sections - the code execution section and the variable storage section. This is done through the construction of a custom linker script which is used during the linking phase of compilation. It tells the compiler where in memory to place everything - code, variables, etc. You will need to craft your own linker script to divide the RAM up in a suitable manner to give you enough room for your code while at the same time not starving it of variable storage space. This will also make sure all jumps, etc, in your code use the right addresses in case you can't (or don't want to) use -fPIC.
So, given all that, do you still really want to execute code from RAM?