How to create Run-In-RAM code using SDCC for STM8? Is this possible in principle? Is no problem to create custom code section and place its to fixed RAM position. But unknown how to place code section optimally in order auto-initializing code in RAM as constants from flash. Without this, it is impossible to implement ULP mode STM8L15x MCU family. I would appreciate any help or advice. I asked this question to the ST Community but haven’t received an answer yet.
Thanks in advance.
Engineer Enthusiast
Vyacheslav Azarov
Last edit: Vyacheslav Azarov 2019-10-13
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Yes, it is very simple for the trivial programs. But if the compiled code include direct or absolute addressing, calls library arithmetics and shareable data then it doesn't seem simple. So far I myself have not found a good solution. May be try to apply the custom linking using stm8-ld? For an example I will give code fragments.
In limit, this is possible by editing assembly files and manually linking sections. However, is would like the compiler to be able to create relocatable code. Thanks.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Sure, it would be good to have better support for functions in RAM in SDCC. Feel free to open a feature request, but be aware that there are lots of features being requested, so it will take a while until SDCC gets support.
A workaround idea that I didn't try yet:
1) Compile your code that you want to run in RAM, link it without startup code. Arrange for the function f you want to call from the code in Flash to be at the start of the binary (e.g. by using the --code-seg option and a linker script). Convert to raw binary (e.g. using objcopy), then use xxd -i to get a char array.
2) Then use that char array in your whole program. Cast the pointer to it into a function pointer, call it (AFAIK that won't work for the large memory model, but will work for the default medium one).
3) Now the absolute addressing is still wrong in the code in RAM. But that can be fixed by just doing another iteration: Get the address of the char array from the .map file from step 2, and redo step 1) using -b in the linkers script with the address. Now redo step 2, and it all should work: You now have a copy of function f, and all functions that it calls (including your own functions, standard library functions, internal support functions from the compiler), in RAM.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Yes, too many features exist what in the SDCC would be nice to add. I will think about opening the requests. Good method, but there are other ways to get the Run-in-Ram code by interfering with the stnadard assembly process. They all require re-linking to figure out the size of the sections and rearranging the ROM image. It would be good to understand what additional functions of the compiler are sufficient to create Run-in-RAM code, for saving of the standard code building process.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hello everyone,
How to create Run-In-RAM code using SDCC for STM8? Is this possible in principle? Is no problem to create custom code section and place its to fixed RAM position. But unknown how to place code section optimally in order auto-initializing code in RAM as constants from flash. Without this, it is impossible to implement ULP mode STM8L15x MCU family. I would appreciate any help or advice. I asked this question to the ST Community but haven’t received an answer yet.
Thanks in advance.
Engineer Enthusiast
Vyacheslav Azarov
Last edit: Vyacheslav Azarov 2019-10-13
SDCC currently won't put the function in RAM automatically; you'll have to copy it manually, e.g. using memcpy().
An example:
https://lujji.github.io/blog/executing-code-from-ram-on-stm8/
but I guess there ar emore elegant ways to do this.
Thank you very much, Philipp.
Yes, it is very simple for the trivial programs. But if the compiled code include direct or absolute addressing, calls library arithmetics and shareable data then it doesn't seem simple. So far I myself have not found a good solution. May be try to apply the custom linking using stm8-ld? For an example I will give code fragments.
Last edit: Vyacheslav Azarov 2019-10-18
In limit, this is possible by editing assembly files and manually linking sections. However, is would like the compiler to be able to create relocatable code. Thanks.
Sure, it would be good to have better support for functions in RAM in SDCC. Feel free to open a feature request, but be aware that there are lots of features being requested, so it will take a while until SDCC gets support.
A workaround idea that I didn't try yet:
1) Compile your code that you want to run in RAM, link it without startup code. Arrange for the function f you want to call from the code in Flash to be at the start of the binary (e.g. by using the --code-seg option and a linker script). Convert to raw binary (e.g. using objcopy), then use xxd -i to get a char array.
2) Then use that char array in your whole program. Cast the pointer to it into a function pointer, call it (AFAIK that won't work for the large memory model, but will work for the default medium one).
3) Now the absolute addressing is still wrong in the code in RAM. But that can be fixed by just doing another iteration: Get the address of the char array from the .map file from step 2, and redo step 1) using -b in the linkers script with the address. Now redo step 2, and it all should work: You now have a copy of function f, and all functions that it calls (including your own functions, standard library functions, internal support functions from the compiler), in RAM.
Yes, too many features exist what in the SDCC would be nice to add. I will think about opening the requests. Good method, but there are other ways to get the Run-in-Ram code by interfering with the stnadard assembly process. They all require re-linking to figure out the size of the sections and rearranging the ROM image. It would be good to understand what additional functions of the compiler are sufficient to create Run-in-RAM code, for saving of the standard code building process.
Thank you again. The method that you proposed is the simplest and safest relative to other.