0

I am programming my embedded system that is based on the SAMD51 chip with C++. This is the memory map of the chip: samd51memmap

What I want is to make a function in the RAM. The function should not be in the flash, but in RAM. So that I can JMP to the function and it works all from the ram.

Why I want this: I will create an application that essentially overwrites itself, and I want the writing function to be in the ram, so that it does not overwrite the writing function. I am well aware that this could corrupt data on a sudden power loss.

Nikos C.
  • 50,738
  • 9
  • 71
  • 96
  • 1
    Is there a reason your C++ question has the `c` tag? – Eljay Oct 19 '20 at 14:42
  • @Eljay I dont really know if the thing i am trying to do is c++ specific or not – James B. Reese Oct 19 '20 at 14:44
  • Do you know if the chip has a MMU (these can prevent writing to code regions)? Secondly, you can specify the location of sections (.text holds the code) to the linker, which should allow you to use the SRAM for the code – TSG Oct 19 '20 at 14:59
  • 1
    You want to write "position independent code" which is copied from part of the flash executable code? – Weather Vane Oct 19 '20 at 14:59
  • 1
    @JamesB.Reese look at linker scripts, described in [this answer](https://stackoverflow.com/questions/9508290/how-to-specify-base-addresses-for-sections-when-linking-or-alternatively-how-to) – TSG Oct 19 '20 at 15:00
  • @TSG The chip does have an MMU but I can disable it. When I use technique you said, will the function be there when I turn the device off and back on? I want it to be there. Like, when the main program starts, it copies the function there and starts working normally. – James B. Reese Oct 19 '20 at 15:02
  • @WeatherVane not exactly. I want to erase the flash and write something else to it while the system is working, so I want to place the eraser and flasher code to ram to prevent it overwriting itself. – James B. Reese Oct 19 '20 at 15:03
  • The use of MMU determines two factors: does your device include RWX protections? For simple uprocessors, there may not be. The second factor is that with an MMU, the addresses are virtual addresses, and may not reflect physical addresses. You need to specify proper physical addresses, in order to specifically use SRAM. Unless required by your app, I'd recommend not using the MMU – TSG Oct 19 '20 at 15:05
  • I thought that was what I wrote. But you can't just copy code which has absolute memory addresses of jumps and calls and expect it to run. That is what "position independent" means. All self-referencing addresses are either relative, or computed at run time. – Weather Vane Oct 19 '20 at 15:05
  • @TSG I am not running an OS on this chip, just the program itself, so I'm assuming I don't need the MMU. – James B. Reese Oct 19 '20 at 15:06
  • Finally, you need to determine what 'loads' the program. Does it simply write to the flash? Can it write to SRAM? I think the best option is to have a small routine which copies the code from flash to SRAM and then jumps to it – TSG Oct 19 '20 at 15:06
  • The use of MMU depends on whether you want to create a product where security matters. If you are testing stuff for fun, feel free to turn the MMU off – TSG Oct 19 '20 at 15:07
  • Note, you can use `uintptr_t` to craft pointers. Like `uintptr_t sram = (uintptr_t) 0x4700 0000;` – TSG Oct 19 '20 at 15:09
  • @TSG when I copy the function from the flash to the sram, will it continue to function normally? As all the variables defined in the function are in the stack (at least that is what I know)? – James B. Reese Oct 19 '20 at 15:09
  • The instruction pointer defines the code that will run. The code should run perfectly if it is "position independent" as pointed out by @WeatherVane. The linker script approach should work with non-PI code, if the loader can handle it. – TSG Oct 19 '20 at 17:46
  • @TSG so if I write the function to be position-independent, can I just copy the function from the flash to the SRAM and run from there? – James B. Reese Oct 19 '20 at 17:48
  • 1
    Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/223312/discussion-between-tsg-and-james-b-reese). – TSG Oct 19 '20 at 18:32
  • linker sections and such can help as well. – Michael Dorgan Oct 19 '20 at 18:54
  • As a side note, executing code from RAM for the purpose of flash programming is almost never a good idea. It's complex, in terms of calling convention and MMU setup. What you ought to do is to look for a part with data flash, then see if you can run code from there instead. Alternatively, some parts have multiple flash banks, so you can run code from one bank while programming the other. I know that SAMD does allow something like that - one project I was involved in used flash logging in run-time using SAMD, but that's data not code. Start by reading the app notes. – Lundin Oct 20 '20 at 06:57
  • (Btw using C++ on these chips is probably not a good idea.) – Lundin Oct 20 '20 at 06:59

0 Answers0