We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Hello, what I would like to do is to copy a function from SRAM to flash and decide in the main if I want to execute from flash or from SRAM. What I observed it is that if this function is calling a subroutine placed in SRAM, when I execute it from flash the jump will not be set correctly: it will try to jump to a subroutine placed in flash, but this function is located in SRAM!!! What can I do to have the correct address when I execute from flash? I tried with the PI qualifiers ropi, but it does not work... I am using a device with Cortex M4
You may have to settle for a pointer table to your functions.
Position-independent code can be moved. But moved as a complete block - not move a single function.
Function calls are either relative - in which case both the source and target functions must be moved the same number of bytes to make sure the relative jump is correct.
Function calls can be indexed relative a pointer - but same there. The assumption is that the pointer points to the start of the block, and both source and destination function have a fixed offset within the block.
Function calls can be absolute - in which case the linker script specifies the memory region the functions should use. Note that the linker script can have one address range where the code is stored. And a different address range where the functions should be used. So the linker can link the functions for use in RAM, but store the code in flash. The instructions can then be copied into RAM before the functions are called.
If the code is built with absolute addressing, then you can't have the function usable from both flash and RAM, unless you have a processor capable of remapping memory pages so flash or RAM can be mapped on top of each other.
If the code is built with relative addressing, then the full block must be copied flash->RAM.
I forgot to ask - but exactly _why_ do you want main() to decide at runtime if it should call a specific function in RAM or in flash? What problem do you hope to solve? You are introducing lots of problems with this specific requirement.
Hi,
Thank you very much for your quick answer:-) So to better explain lets' say I have the function f calling functions g and l:
void f (void) { call g; call l; }
all these functions are in SRAM, then I copied f in flash but I want that g and l are still in SRAM when I execute the code from flash. How can I do that?
Alex
No, that doesn't address the question at all:
"exactly _why_ do you want main() to decide at runtime if it should call a specific function in RAM or in flash? What problem do you hope to solve?. You are introducing lots of problems with this specific requirement."
www.catb.org/.../smart-questions.html
Hello,
This is a customer application and this requiremets was given to me by my customer. The customer wants the possibility to have a function in sram and the same in flash and to decide when to use when or the other.
Easy - make two copies of the function. One linked for RAM and one linked for flash.
Good solution? Not at all. But the customer gets what he wants - and gets to pay the price from wanting what he shouldn't.