2

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.

artificer
  • 123
  • 3
  • According to [an important Wikipedia article](https://en.wikipedia.org/wiki/Alfred_E._Neuman), “Neuman's surname is often misspelled as "Newman"”. I guess the same thing happens to [Von Neumann](https://en.wikipedia.org/wiki/Von_Neumann_architecture). – James Waldby - jwpat7 May 10 '17 at 03:22
  • BTW, per Table 22.11 in Joseph Yiu's “Guide to ARM Cortex-M0 and Cortex-M0+ Processors” book, the Cortex-M0 and -M0+ use Von Neumann architecture, while the Cortex-M1, M3, M4, M7 use Harvard. The SAMD21 is a Cortex-M0+ processor, so Von Neumann as stated in question. – James Waldby - jwpat7 May 10 '17 at 04:00
  • @JamesWaldby-jwpat7 By the way, the proper pronunciation of Neumann is actually Noiman. It's German. – Majenko May 10 '17 at 10:33
  • @Majenko, of course that's true of Von Neumann's name, as he was born and raised in Germany; but perhaps less so of Neuman's name, which is from whole cloth. Anyhow, artificer has corrected the spelling in the question. – James Waldby - jwpat7 May 10 '17 at 14:02
  • @JamesWaldby-jwpat7 Yeah, the proper pronunciation of Neuman is "[Chumly](https://en.wikipedia.org/wiki/Cholmondeley)". – Majenko May 10 '17 at 14:08

1 Answers1

0

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?

Majenko
  • 103,876
  • 5
  • 75
  • 133
  • Thanks a lot for the linker warning. I will have to consider the SRAM size constraints. – artificer May 10 '17 at 17:07
  • 1
    After a lot of reading I found an ATMEL document about the subject. They already created the linke script! the document is named "AT07347: Executing Code from RAM" – artificer May 18 '17 at 03:34