cancel
Showing results for 
Search instead for 
Did you mean: 

Running/Calling code in Ram from Flash

wragle
Associate
Posted on March 09, 2009 at 12:28

Running/Calling code in Ram from Flash

7 REPLIES 7
wragle
Associate
Posted on May 17, 2011 at 13:05

Folks,

I am learning the STM32 processor family. I am using CodeSourcery G++ Lite, downloading the code to an STM3210E-EVAL board. Everything has been progressing nicely until I tried to locate some code in RAM and reference the code from a C program running in flash. At that point the linker gave the error message:

''relocation truncated to fit: R_ARM_THM_CALL against symbol `APP_FLASH_IRQHandler' defined in .iramcode section in flash.o''.

I understand that I am attempting to make a ''far'' call.

I am new to the ARM and CORTEX and so must ask:

How do I tell the compiler/linker to generate code to access a function which is far away in the memory space?

Thanks For Your Time,

Wes Ragle

picguy
Associate II
Posted on May 17, 2011 at 13:05

A bit of assembly code would do it. Your assembly stub in flash could load r3 then bx r3. (R3 assumes you have less than 4 args.) Call the assembly code.

Or your compiler may have a switch for this kind of call. It would have to be a VERY smart linker to do this w/o compiler help. Specify your tool chain and others may chime in.

domen2
Associate III
Posted on May 17, 2011 at 13:05

or just:

void (func*)() = 0x20000000; /* maybe it's *func, i always mix this up, but since only one compiles, just try */

...

func();

Dunno about the real way, with linker.

jacob23
Associate
Posted on May 17, 2011 at 13:05

I know this is an old post, but I spent most of my day developing a ''bootloader'' for an STM32-based project, so for the sake of others following after here is what I found. I am using RAISONANCE RIDE7 with gcc.

1) To locate a function in RAM I used the section attribute when declaring it. My linker script already had a section called ''.RAMtext'' that the startup-code would copy from FLASH into RAM before calling main.

void __attribute__ ((section (''.RAMtext''))) myfunction(void)

{

...

}

2) To actually call this function from FLASH I had to tell gcc to use long calls. In RIDE7 I did this by setting Project Properties -> GCC compiler -> Call-> ''Generate long calls'' to ''Yes''. What this actually does is pass the ''-mlong-calls'' option to gcc during the build.

Anyways, I hope that's helpful to someone.

nicholas2399
Associate II
Posted on May 17, 2011 at 13:05

This thread caught my interest. As from the datasheets for the STM32 (I use the F103C8T6) you cannot run code from SRAM - as the ARM M3 ICode bus only reaches the Flash.

If indeed you can run code from SRAM, I assume I will need to set up some form of scatterfile for the linker (I am using uVision which is the Keil product with the ARM tools - compiler and linker) - is there anything I need to do in the c-code or with c compiler settings to faciliate this though?

Thnaks

Nick

swhite2
Associate III
Posted on May 17, 2011 at 13:05

You CAN run code from RAM in a Cortex-M3.

FYI in the IAR 5.xx toolchain you can easily create a RAM function with the extended keyword __ramfunc.

e.g.

__ramfunc void foo(void);

The linker will locate the code in flash and the startup code will copy it to RAM at reset.Calls are to the copy in RAM.

No need for assembler unless you need it in your function.

epassoni950
Associate II
Posted on May 17, 2011 at 13:05

Hello,

Yes, I confirm there is no problem to run code from Ram. Cortex-M3 fetch opcode from ICode bus for address range between 0x00000000 and 0x1FFFFFFF. When PC is not in this range, Cortex-M3 fetch opcode from System bus to reach SRAM.

Be carefull if you try to locate interrupt routine in SRAM to obtain a better execution time. Even if Ram access are 0 wait state, when your interrupt routine have to read/write variables in Ram or peripheral registers, Cortex-M3 must use the same System bus. If your interrupt routine stay in Flash, Cortex-M3 can fetch opcode from ICode bus while accessing Ram and peripheral with System bus. With buffer latch betwen Cortex-M3 and Flash, wait state are only for a branch ; for sequential code there is no wait state to fetch next instructions.

Regards

Eric