Electronic – Why execute code from RAM

microcontrollerram

I've just come across some macros for my microcontroller compiler to force (or suggest) a function be executed from RAM.

https://siliconlabs.github.io/Gecko_SDK_Doc/efr32mg1/html/group__RAMFUNC.html#gac6abbc7f869eec9fb47e57427587c556

http://processors.wiki.ti.com/index.php/Placing_functions_in_RAM

https://www.iar.com/support/tech-notes/linker/controlling-placement-of-the-section-where-__ramfunc-functions-reside-ewarm-5.x–6.x/

https://community.nxp.com/thread/389099

In what cases is this valuable? Why wouldn't I just always execute from RAM if the benefit is only increased speed? Does this generally cause higher current draw?

Best Answer

In addition to the speed & other features which others have already mentioned, executing code from RAM can be useful in bootloaders where you need to reprogram your micro's flash - you can't execute code from flash which you're in the middle of erasing & reprogramming.