2021-07-02 07:32 AM
2021-07-02 11:37 AM
Hello @INaka.1
To elaborate more on @TDK 's response, it is possible to have a function at a predetermined location (alignment withstanding) by adding a new section in the linker file and place the function there: Linker scripts (haw-hamburg.de), a more beginner friendly guide here: Putting Code of Files into Special Section with the GNU Linker.
Some toolchains have special ways to at least suggest which address a function or a variable should be placed (like __attribute__(at()) for ARMCC/ARMLINK, the @ operator for IAR, etc...). But those should avoided even if you have access to such toolchains for portability's sake.
Otherwise, I'd have to agree with @Tesla DeLorean. An exotic solution for an issue is rarely the good solution. His proposal, which is called a branch table, is more flexible especially in case you have multiple functions which addresses must be fixed it is the way to go. In fact that how interrupt handlers have their fixed (but movable to a certain degree with SCB->VTOR for Cortex M at least) addresses. Beware that if you're targeting MISRA compliance you might run into some issue.
Code placement, and linking in general, is an interesting subject. You can place a function at a given address in more than one suboptimal to disastrous ways. Down-below are some of those *techniques* (it goes without saying that they are not recommended at all:(
Best regards,
@SBEN.2
2021-07-02 10:01 AM
There's no easy way to do this. You could create a section of flash in the linker script and put that function in there. But not if it's in the middle of the flash you're using.
2021-07-02 10:12 AM
Perhaps ponder the problem to be solved.
One frequently creates a table of address so the functions can move, whilst the table can be placed at a more fixed/stable address.
One could perhaps use AT directives or attributes, but this does give the linker headaches.
Consider using pointers, or more flexible methods of passing function addresses.
2021-07-02 11:14 AM
I'll try this solution. Thank you very much
2021-07-02 11:21 AM
Thank you for your answer
2021-07-02 11:37 AM
Hello @INaka.1
To elaborate more on @TDK 's response, it is possible to have a function at a predetermined location (alignment withstanding) by adding a new section in the linker file and place the function there: Linker scripts (haw-hamburg.de), a more beginner friendly guide here: Putting Code of Files into Special Section with the GNU Linker.
Some toolchains have special ways to at least suggest which address a function or a variable should be placed (like __attribute__(at()) for ARMCC/ARMLINK, the @ operator for IAR, etc...). But those should avoided even if you have access to such toolchains for portability's sake.
Otherwise, I'd have to agree with @Tesla DeLorean. An exotic solution for an issue is rarely the good solution. His proposal, which is called a branch table, is more flexible especially in case you have multiple functions which addresses must be fixed it is the way to go. In fact that how interrupt handlers have their fixed (but movable to a certain degree with SCB->VTOR for Cortex M at least) addresses. Beware that if you're targeting MISRA compliance you might run into some issue.
Code placement, and linking in general, is an interesting subject. You can place a function at a given address in more than one suboptimal to disastrous ways. Down-below are some of those *techniques* (it goes without saying that they are not recommended at all:(
Best regards,
@SBEN.2
2021-07-02 12:35 PM
Thank you!